xref: /dokuwiki/inc/template.php (revision 78a6aeb15ad85c8be4a7e39307b7d9aa0512742c)
1<?php
2/**
3 * DokuWiki template functions
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
10  require_once(DOKU_CONF.'dokuwiki.php');
11
12/**
13 * Wrapper around htmlspecialchars()
14 *
15 * @author Andreas Gohr <andi@splitbrain.org>
16 * @see    htmlspecialchars()
17 */
18function hsc($string){
19  return htmlspecialchars($string);
20}
21
22/**
23 * print a newline terminated string
24 *
25 * You can give an indention as optional parameter
26 *
27 * @author Andreas Gohr <andi@splitbrain.org>
28 */
29function ptln($string,$intend=0){
30  for($i=0; $i<$intend; $i++) print ' ';
31  print"$string\n";
32}
33
34/**
35 * Returns the path to the given template, uses
36 * default one if the custom version doesn't exist
37 *
38 * @author Andreas Gohr <andi@splitbrain.org>
39 */
40function template($tpl){
41  global $conf;
42
43  if(@is_readable(DOKU_INC.'lib/tpl/'.$conf['template'].'/'.$tpl))
44    return DOKU_INC.'lib/tpl/'.$conf['template'].'/'.$tpl;
45
46  return DOKU_INC.'lib/tpl/default/'.$tpl;
47}
48
49/**
50 * Print the content
51 *
52 * This function is used for printing all the usual content
53 * (defined by the global $ACT var) by calling the appropriate
54 * outputfunction(s) from html.php
55 *
56 * Everything that doesn't use the default template isn't
57 * handled by this function. ACL stuff is not done either.
58 *
59 * @author Andreas Gohr <andi@splitbrain.org>
60 */
61function tpl_content(){
62  global $ACT;
63  global $TEXT;
64  global $PRE;
65  global $SUF;
66  global $SUM;
67  global $IDX;
68
69  switch($ACT){
70    case 'show':
71      html_show();
72      break;
73    case 'preview':
74      html_edit($TEXT);
75      html_show($TEXT);
76      break;
77    case 'edit':
78      html_edit();
79      break;
80    case 'wordblock':
81      html_edit($TEXT,'wordblock');
82      break;
83    case 'search':
84      html_search();
85      break;
86    case 'revisions':
87      html_revisions();
88      break;
89    case 'diff':
90      html_diff();
91      break;
92    case 'recent':
93      $first = is_numeric($_REQUEST['first']) ? intval($_REQUEST['first']) : 0;
94      html_recent($first);
95      break;
96    case 'index':
97      html_index($IDX); #FIXME can this be pulled from globals? is it sanitized correctly?
98      break;
99    case 'backlink':
100      html_backlinks();
101      break;
102    case 'conflict':
103      html_conflict(con($PRE,$TEXT,$SUF),$SUM);
104      html_diff(con($PRE,$TEXT,$SUF),false);
105      break;
106    case 'locked':
107      html_locked();
108      break;
109    case 'login':
110      html_login();
111      break;
112    case 'register':
113      html_register();
114      break;
115    case 'resendpwd':
116      html_resendpwd();
117      break;
118    case 'denied':
119      print p_locale_xhtml('denied');
120      break;
121    case 'profile' :
122      html_updateprofile();
123      break;
124    case 'admin':
125      tpl_admin();
126      break;
127    default:
128            msg("Failed to handle command: ".hsc($ACT),-1);
129  }
130}
131
132/**
133 * Handle the admin page contents
134 *
135 * @author Andreas Gohr <andi@splitbrain.org>
136 */
137function tpl_admin(){
138
139    $plugin = NULL;
140    if ($_REQUEST['page']) {
141        $pluginlist = plugin_list('admin');
142
143        if (in_array($_REQUEST['page'], $pluginlist)) {
144
145          // attempt to load the plugin
146          $plugin =& plugin_load('admin',$_REQUEST['page']);
147        }
148    }
149
150    if ($plugin !== NULL)
151        $plugin->html();
152    else
153        html_admin();
154}
155
156/**
157 * Print the correct HTML meta headers
158 *
159 * This has to go into the head section of your template.
160 *
161 * @author Andreas Gohr <andi@splitbrain.org>
162 */
163function tpl_metaheaders(){
164  global $ID;
165  global $INFO;
166  global $ACT;
167  global $lang;
168  global $conf;
169  $it=2;
170
171  // the usual stuff
172  ptln('<meta name="generator" content="DokuWiki '.getVersion().'" />',$it);
173  ptln('<link rel="start" href="'.DOKU_BASE.'" />',$it);
174  ptln('<link rel="contents" href="'.wl($ID,'do=index').'" title="'.$lang['index'].'" />',$it);
175  ptln('<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="'.DOKU_BASE.'feed.php" />',$it);
176  ptln('<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="'.DOKU_BASE.'feed.php?mode=list&amp;ns='.$INFO['namespace'].'" />',$it);
177  ptln('<link rel="alternate" type="text/html" title="Plain HTML" href="'.wl($ID,'do=export_html').'" />',$it);
178  ptln('<link rel="alternate" type="text/plain" title="Wiki Markup" href="'.wl($ID, 'do=export_raw').'" />',$it);
179
180  // setup robot tags apropriate for different modes
181  if( ($ACT=='show' || $ACT=='export_html') && !$REV){
182    if($INFO['exists']){
183      ptln('<meta name="date" content="'.date('Y-m-d\TH:i:sO',$INFO['lastmod']).'" />',$it);
184      //delay indexing:
185      if((time() - $INFO['lastmod']) >= $conf['indexdelay']){
186        ptln('<meta name="robots" content="index,follow" />',$it);
187      }else{
188        ptln('<meta name="robots" content="noindex,nofollow" />',$it);
189      }
190    }else{
191      ptln('<meta name="robots" content="noindex,follow" />',$it);
192    }
193  }else{
194    ptln('<meta name="robots" content="noindex,nofollow" />',$it);
195  }
196
197  // load stylesheets
198  ptln('<link rel="stylesheet" media="screen" type="text/css" href="'.DOKU_BASE.'lib/exe/css.php" />',$it);
199  ptln('<link rel="stylesheet" media="print" type="text/css" href="'.DOKU_BASE.'lib/exe/css.php?print=1" />',$it);
200
201  // load javascript
202  $js_edit  = ($ACT=='edit' || $ACT=='preview') ? 1 : 0;
203  $js_write = ($INFO['writable']) ? 1 : 0;
204  $js_sig   = ($conf['useacl'] && $_SERVER['REMOTE_USER']) ? 1 : 0;
205  ptln('<script language="javascript" type="text/javascript" charset="utf-8" src="'.
206       DOKU_BASE.'lib/exe/js.php?edit='.$js_edit.'&amp;write='.$js_write.'&amp;sig='.$js_sig.'"></script>',$it);
207}
208
209/**
210 * Print a link
211 *
212 * Just builds a link.
213 *
214 * @author Andreas Gohr <andi@splitbrain.org>
215 */
216function tpl_link($url,$name,$more=''){
217  print '<a href="'.$url.'" ';
218  if ($more) print ' '.$more;
219  print ">$name</a>";
220}
221
222/**
223 * Prints a link to a WikiPage
224 *
225 * Wrapper around html_wikilink
226 *
227 * @author Andreas Gohr <andi@splitbrain.org>
228 */
229function tpl_pagelink($id,$name=NULL){
230  print html_wikilink($id,$name);
231}
232
233/**
234 * get the parent page
235 *
236 * Tries to find out which page is parent.
237 * returns false if none is available
238 *
239 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
240 */
241function tpl_getparent($ID){
242  global $conf;
243
244  if ($ID != $conf['start']) {
245    $idparts = explode(':', $ID);
246    $pn = array_pop($idparts);    // get the page name
247
248    for ($n=0; $n < 2; $n++) {
249      if (count($idparts) == 0) {
250        $ID = $conf['start'];     // go to topmost page
251        break;
252      }else{
253        $ns = array_pop($idparts);     // get the last part of namespace
254        if ($pn != $ns) {                 // are we already home?
255          array_push($idparts, $ns, $ns); // no, then add a page with same name
256          $ID = implode (':', $idparts); // as the namespace and recombine $ID
257          break;
258        }
259      }
260    }
261
262    if (@file_exists(wikiFN($ID))) {
263      return $ID;
264    }
265  }
266  return false;
267}
268
269/**
270 * Print one of the buttons
271 *
272 * Available Buttons are
273 *
274 *  edit        - edit/create/show button
275 *  history     - old revisions
276 *  recent      - recent changes
277 *  login       - login/logout button - if ACL enabled
278 *  index       - The index
279 *  admin       - admin page - if enough rights
280 *  top         - a back to top button
281 *  back        - a back to parent button - if available
282 *  backtomedia - returns to the mediafile upload dialog
283 *                after references have been displayed
284 *  backlink    - links to the list of backlinks
285 *
286 * @author Andreas Gohr <andi@splitbrain.org>
287 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
288 */
289function tpl_button($type){
290  global $ACT;
291  global $ID;
292  global $NS;
293  global $INFO;
294  global $conf;
295
296  switch($type){
297    case 'edit':
298      print html_editbutton();
299      break;
300    case 'history':
301      print html_btn('revs',$ID,'o',array('do' => 'revisions'));
302      break;
303    case 'recent':
304      print html_btn('recent','','r',array('do' => 'recent'));
305      break;
306    case 'index':
307      print html_btn('index',$ID,'x',array('do' => 'index'));
308      break;
309    case 'back':
310      if ($parent = tpl_getparent($ID)) {
311        print html_btn('back',$parent,'b',array('do' => 'show'));
312      }
313      break;
314    case 'top':
315      print html_topbtn();
316      break;
317    case 'login':
318      if($conf['useacl']){
319        if($_SERVER['REMOTE_USER']){
320          print html_btn('logout',$ID,'',array('do' => 'logout',));
321        }else{
322          print html_btn('login',$ID,'',array('do' => 'login'));
323        }
324      }
325      break;
326    case 'admin':
327      if($INFO['perm'] == AUTH_ADMIN)
328        print html_btn('admin',$ID,'',array('do' => 'admin'));
329      break;
330    case 'backtomedia':
331      print html_backtomedia_button(array('ns' => $NS),'b');
332      break;
333    case 'subscription':
334      if($conf['useacl'] && $ACT == 'show' && $conf['subscribers'] == 1){
335        if($_SERVER['REMOTE_USER']){
336          if($INFO['subscribed']){
337            print html_btn('unsubscribe',$ID,'',array('do' => 'unsubscribe',));
338          } else {
339            print html_btn('subscribe',$ID,'',array('do' => 'subscribe',));
340          }
341        }
342      }
343      break;
344    case 'backlink':
345      print html_btn('backlink',$ID,'',array('do' => 'backlink'));
346      break;
347    case 'profile':
348      if(($_SERVER['REMOTE_USER']) && auth_canDo('modifyUser') && ($ACT!='profile')){
349        print html_btn('profile',$ID,'',array('do' => 'profile'));
350      }
351      break;
352    default:
353      print '[unknown button type]';
354  }
355}
356
357/**
358 * Like the action buttons but links
359 *
360 * Available links are
361 *
362 *  edit    - edit/create/show button
363 *  history - old revisions
364 *  recent  - recent changes
365 *  login   - login/logout button - if ACL enabled
366 *  index   - The index
367 *  admin   - admin page - if enough rights
368 *  top     - a back to top button
369 *  back    - a back to parent button - if available
370 * backlink - links to the list of backlinks
371 *
372 * @author Andreas Gohr <andi@splitbrain.org>
373 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
374 * @see    tpl_button
375 */
376function tpl_actionlink($type,$pre='',$suf=''){
377  global $ID;
378  global $INFO;
379  global $REV;
380  global $ACT;
381  global $conf;
382  global $lang;
383
384  switch($type){
385    case 'edit':
386      #most complicated type - we need to decide on current action
387      if($ACT == 'show' || $ACT == 'search'){
388        if($INFO['writable']){
389          if($INFO['exists']){
390            tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
391                     $pre.$lang['btn_edit'].$suf,
392                     'class="action edit" accesskey="e" rel="nofollow"');
393          }else{
394            tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
395                     $pre.$lang['btn_create'].$suf,
396                     'class="action create" accesskey="e" rel="nofollow"');
397          }
398        }else{
399          tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
400                   $pre.$lang['btn_source'].$suf,
401                   'class="action source" accesskey="v" rel="nofollow"');
402        }
403      }else{
404          tpl_link(wl($ID,'do=show'),
405                   $pre.$lang['btn_show'].$suf,
406                   'class="action show" accesskey="v" rel="nofollow"');
407      }
408      break;
409    case 'history':
410      tpl_link(wl($ID,'do=revisions'),$pre.$lang['btn_revs'].$suf,'class="action revisions" accesskey="o"');
411      break;
412    case 'recent':
413      tpl_link(wl($ID,'do=recent'),$pre.$lang['btn_recent'].$suf,'class="action recent" accesskey="r"');
414      break;
415    case 'index':
416      tpl_link(wl($ID,'do=index'),$pre.$lang['btn_index'].$suf,'class="action index" accesskey="x"');
417      break;
418    case 'top':
419      print '<a href="#top" class="action top" accesskey="x">'.$pre.$lang['btn_top'].$suf.'</a>';
420      break;
421    case 'back':
422      if ($ID = tpl_getparent($ID)) {
423        tpl_link(wl($ID,'do=show'),$pre.$lang['btn_back'].$suf,'class="action back" accesskey="b"');
424      }
425      break;
426    case 'login':
427      if($conf['useacl']){
428        if($_SERVER['REMOTE_USER']){
429          tpl_link(wl($ID,'do=logout'),$pre.$lang['btn_logout'].$suf,'class="action logout"');
430        }else{
431          tpl_link(wl($ID,'do=login'),$pre.$lang['btn_login'].$suf,'class="action logout"');
432        }
433      }
434      break;
435    case 'admin':
436      if($INFO['perm'] == AUTH_ADMIN)
437        tpl_link(wl($ID,'do=admin'),$pre.$lang['btn_admin'].$suf,'class="action admin"');
438      break;
439   case 'subscribe':
440   case 'subscription':
441      if($conf['useacl'] && $ACT == 'show' && $conf['subscribers'] == 1){
442        if($_SERVER['REMOTE_USER']){
443          if($INFO['subscribed']) {
444            tpl_link(wl($ID,'do=unsubscribe'),$pre.$lang['btn_unsubscribe'].$suf,'class="action unsubscribe"');
445          } else {
446            tpl_link(wl($ID,'do=subscribe'),$pre.$lang['btn_subscribe'].$suf,'class="action subscribe"');
447          }
448        }
449      }
450      break;
451    case 'backlink':
452      tpl_link(wl($ID,'do=backlink'),$pre.$lang['btn_backlink'].$suf, 'class="action backlink"');
453      break;
454    case 'profile':
455      if(($_SERVER['REMOTE_USER']) && auth_canDo('modifyUser') && ($ACT!='profile')){
456        tpl_link(wl($ID,'do=profile'),$pre.$lang['btn_profile'].$suf, 'class="action profile"');
457      }
458      break;
459    default:
460      print '[unknown link type]';
461  }
462}
463
464/**
465 * Print the search form
466 *
467 * @author Andreas Gohr <andi@splitbrain.org>
468 */
469function tpl_searchform($withajax=true){
470  global $lang;
471  global $ACT;
472
473  print '<form action="'.wl().'" accept-charset="utf-8" class="search" name="search">';
474  print '<input type="hidden" name="do" value="search" />';
475  print '<input type="text" ';
476  if ($ACT == 'search') print 'value="'.htmlspecialchars($_REQUEST['id']).'" ';
477  print 'id="qsearch_in" accesskey="f" name="id" class="edit" />';
478  print '<input type="submit" value="'.$lang['btn_search'].'" class="button" />';
479  print '<div id="qsearch_out" class="ajax_qsearch JSpopup"></div>';
480  print '</form>';
481}
482
483/**
484 * Print the breadcrumbs trace
485 *
486 * @author Andreas Gohr <andi@splitbrain.org>
487 */
488function tpl_breadcrumbs(){
489  global $lang;
490  global $conf;
491
492  //check if enabled
493  if(!$conf['breadcrumbs']) return;
494
495  $crumbs = breadcrumbs(); //setup crumb trace
496
497  //reverse crumborder in right-to-left mode
498  if($lang['direction'] == 'rtl') $crumbs = array_reverse($crumbs,true);
499
500  //render crumbs, highlight the last one
501  print $lang['breadcrumb'].':';
502  $last = count($crumbs);
503  $i = 0;
504  foreach ($crumbs as $id => $name){
505    $i++;
506    print ' <span class="bcsep">&raquo;</span> ';
507    if ($i == $last) print '<span class="curid">';
508    tpl_link(wl($id),$name,'class="breadcrumbs" title="'.$id.'"');
509    if ($i == $last) print '</span>';
510  }
511}
512
513/**
514 * Hierarchical breadcrumbs
515 *
516 * This code was suggested as replacement for the usual breadcrumbs
517 * trail in the Wiki and was modified by me.
518 * It only makes sense with a deep site structure.
519 *
520 * @author Andreas Gohr <andi@splitbrain.org>
521 * @author Nigel McNie <oracle.shinoda@gmail.com>
522 * @link   http://wiki.splitbrain.org/wiki:tipsandtricks:hierarchicalbreadcrumbs
523 * @todo   May behave strangely in RTL languages
524 */
525function tpl_youarehere(){
526  global $conf;
527  global $ID;
528  global $lang;
529
530
531  $parts     = explode(':', $ID);
532
533  // Perhaps a $lang['tree'] could be defined? "Trace" isn't too appropriate
534  //print $lang['tree'].': ';
535  print $lang['breadcrumb'].': ';
536
537  //always print the startpage
538  if( $a_part[0] != $conf['start'] )
539    tpl_link(wl($conf['start']),$conf['start'],'title="'.$conf['start'].'"');
540
541  $page = '';
542  foreach ($parts as $part){
543        // Skip startpage if already done
544        if ($part == $conf['start']) continue;
545
546          print ' &raquo; ';
547    $page .= $part;
548
549    if(file_exists(wikiFN($page))){
550      tpl_link(wl($page),$part,'title="'.$page.'"');
551    }else{
552      // Print the link, but mark as not-existing, as for other non-existing links
553      tpl_link(wl($page),$part,'title="'.$page.'" class="wikilink2"');
554      //print $page;
555    }
556
557    $page .= ':';
558  }
559}
560
561/**
562 * Print info if the user is logged in
563 * and show full name in that case
564 *
565 * Could be enhanced with a profile link in future?
566 *
567 * @author Andreas Gohr <andi@splitbrain.org>
568 */
569function tpl_userinfo(){
570  global $lang;
571  global $INFO;
572  if($_SERVER['REMOTE_USER'])
573    print $lang['loggedinas'].': '.$INFO['userinfo']['name'];
574}
575
576/**
577 * Print some info about the current page
578 *
579 * @author Andreas Gohr <andi@splitbrain.org>
580 */
581function tpl_pageinfo(){
582  global $conf;
583  global $lang;
584  global $INFO;
585  global $REV;
586
587  // prepare date and path
588  $fn = $INFO['filepath'];
589  if(!$conf['fullpath']){
590    if($REV){
591      $fn = str_replace(realpath($conf['olddir']).DIRECTORY_SEPARATOR,'',$fn);
592    }else{
593      $fn = str_replace(realpath($conf['datadir']).DIRECTORY_SEPARATOR,'',$fn);
594    }
595  }
596  $fn = utf8_decodeFN($fn);
597  $date = date($conf['dformat'],$INFO['lastmod']);
598
599  // print it
600  if($INFO['exists']){
601    print $fn;
602    print ' &middot; ';
603    print $lang['lastmod'];
604    print ': ';
605    print $date;
606    if($INFO['editor']){
607      print ' '.$lang['by'].' ';
608      print $INFO['editor'];
609    }
610    if($INFO['locked']){
611      print ' &middot; ';
612      print $lang['lockedby'];
613      print ': ';
614      print $INFO['locked'];
615    }
616  }
617}
618
619/**
620 * Print a list of namespaces containing media files
621 *
622 * @author Andreas Gohr <andi@splitbrain.org>
623 */
624function tpl_medianamespaces(){
625    global $conf;
626
627  $data = array();
628  search($data,$conf['mediadir'],'search_namespaces',array());
629  print html_buildlist($data,'idx',media_html_list_namespaces);
630}
631
632/**
633 * Print a list of mediafiles in the current namespace
634 *
635 * @author Andreas Gohr <andi@splitbrain.org>
636 */
637function tpl_mediafilelist(){
638  global $conf;
639  global $lang;
640  global $NS;
641  global $AUTH;
642  $dir = utf8_encodeFN(str_replace(':','/',$NS));
643
644  $data = array();
645  search($data,$conf['mediadir'],'search_media',array(),$dir);
646
647  if(!count($data)){
648    ptln('<div class="nothing">'.$lang['nothingfound'].'</div>');
649    return;
650  }
651
652  ptln('<ul>',2);
653  foreach($data as $item){
654    ptln('<li>',4);
655    ptln('<a href="javascript:mediaSelect(\':'.$item['id'].'\')">'.
656         utf8_decodeFN($item['file']).
657         '</a>',6);
658
659    //prepare deletion button
660    if($AUTH >= AUTH_DELETE){
661      $ask  = $lang['del_confirm'].'\\n';
662      $ask .= $item['id'];
663
664      $del = '<a href="'.DOKU_BASE.'lib/exe/media.php?delete='.urlencode($item['id']).'" '.
665             'onclick="return confirm(\''.$ask.'\')" onkeypress="return confirm(\''.$ask.'\')">'.
666             '<img src="'.DOKU_BASE.'lib/images/del.png" alt="'.$lang['btn_delete'].'" '.
667             'align="bottom" title="'.$lang['btn_delete'].'" /></a>';
668    }else{
669      $del = '';
670    }
671
672    if($item['isimg']){
673      $w = $item['meta']->getField('File.Width');
674      $h = $item['meta']->getField('File.Height');
675
676      ptln('('.$w.'&#215;'.$h.' '.filesize_h($item['size']).')',6);
677      ptln($del.'<br />',6);
678      ptln('<div class="imagemeta">',6);
679
680      //build thumbnail
681      print '<a href="javascript:mediaSelect(\''.$item['id'].'\')">';
682
683      if($w>120 || $h>120){
684        $ratio = $item['meta']->getResizeRatio(120);
685        $w = floor($w * $ratio);
686        $h = floor($h * $ratio);
687      }
688
689      $src = ml($item['id'],array('w'=>$w,'h'=>$h));
690
691      $p = array();
692      $p['width']  = $w;
693      $p['height'] = $h;
694      $p['alt']    = $item['id'];
695      $p['class']  = 'thumb';
696      $att = buildAttributes($p);
697
698      print '<img src="'.$src.'" '.$att.' />';
699      print '</a>';
700
701      //read EXIF/IPTC data
702      $t = $item['meta']->getField('IPTC.Headline');
703      if($t) print '<b>'.$t.'</b><br />';
704
705      $t = $item['meta']->getField(array('IPTC.Caption','EXIF.UserComment',
706                                         'EXIF.TIFFImageDescription',
707                                         'EXIF.TIFFUserComment'));
708      if($t) print $t.'<br />';
709
710      $t = $item['meta']->getField(array('IPTC.Keywords','IPTC.Category'));
711      if($t) print '<i>'.$t.'</i><br />';
712
713      //add edit button
714      if($AUTH >= AUTH_UPLOAD && $item['meta']->getField('File.Mime') == 'image/jpeg'){
715        print '<a href="'.DOKU_BASE.'lib/exe/media.php?edit='.urlencode($item['id']).'">';
716        print '<img src="'.DOKU_BASE.'lib/images/edit.gif" alt="'.$lang['metaedit'].'" title="'.$lang['metaedit'].'" />';
717        print '</a>';
718      }
719
720      ptln('</div>',6);
721    }else{
722      ptln ('('.filesize_h($item['size']).')',6);
723      ptln($del,6);
724    }
725    ptln('</li>',4);
726  }
727  ptln('</ul>',2);
728}
729
730/**
731 * show references to a media file
732 * References uses the same visual as search results and share
733 * their CSS tags except pagenames won't be links.
734 *
735 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
736 */
737function tpl_showreferences(&$data){
738  global $lang;
739
740  $hidden=0; //count of hits without read permission
741
742  if(count($data)){
743    usort($data,'sort_search_fulltext');
744    foreach($data as $row){
745      if(auth_quickaclcheck($row['id']) >= AUTH_READ){
746        print '<div class="search_result">';
747        print '<span class="mediaref_ref">'.$row['id'].'</span>';
748        print ': <span class="search_cnt">'.$row['count'].' '.$lang['hits'].'</span><br />';
749        print '<div class="search_snippet">'.$row['snippet'].'</div>';
750        print '</div>';
751      }else
752        $hidden++;
753    }
754    if ($hidden){
755      print '<div class="mediaref_hidden">'.$lang['ref_hidden'].'</div>';
756    }
757  }
758}
759
760/**
761 * Print the media upload form if permissions are correct
762 *
763 * @author Andreas Gohr <andi@splitbrain.org>
764 */
765function tpl_mediauploadform(){
766  global $NS;
767  global $UPLOADOK;
768  global $AUTH;
769  global $lang;
770
771  if(!$UPLOADOK) return;
772
773  ptln('<form action="'.DOKU_BASE.'lib/exe/media.php" name="upload"'.
774       ' method="post" enctype="multipart/form-data">',2);
775  ptln($lang['txt_upload'].':<br />',4);
776  ptln('<input type="file" name="upload" class="edit" onchange="suggestWikiname();" />',4);
777  ptln('<input type="hidden" name="ns" value="'.hsc($NS).'" /><br />',4);
778  ptln($lang['txt_filename'].'<br />',4);
779  ptln('<input type="text" name="id" class="edit" />',4);
780  ptln('<input type="submit" class="button" value="'.$lang['btn_upload'].'" accesskey="s" />',4);
781  if($AUTH >= AUTH_DELETE){
782    ptln('<label for="ow"><input type="checkbox" name="ow" value="1" id="ow">'.$lang['txt_overwrt'].'</label>',4);
783  }
784  ptln('</form>',2);
785}
786
787/**
788 * Prints the name of the given page (current one if none given).
789 *
790 * If useheading is enabled this will use the first headline else
791 * the given ID is printed.
792 *
793 * @author Andreas Gohr <andi@splitbrain.org>
794 */
795function tpl_pagetitle($id=null){
796  global $conf;
797  if(is_null($id)){
798    global $ID;
799    $id = $ID;
800  }
801
802  $name = $id;
803  if ($conf['useheading']) {
804    $title = p_get_first_heading($id);
805    if ($title) $name = $title;
806  }
807  print hsc($name);
808}
809
810/**
811 * Returns the requested EXIF/IPTC tag from the current image
812 *
813 * If $tags is an array all given tags are tried until a
814 * value is found. If no value is found $alt is returned.
815 *
816 * Which texts are known is defined in the functions _exifTagNames
817 * and _iptcTagNames() in inc/jpeg.php (You need to prepend IPTC
818 * to the names of the latter one)
819 *
820 * Only allowed in: detail.php, mediaedit.php
821 *
822 * @author Andreas Gohr <andi@splitbrain.org>
823 */
824function tpl_img_getTag($tags,$alt=''){
825  // Init Exif Reader
826  global $SRC;
827  static $meta = null;
828  if(is_null($meta)) $meta = new JpegMeta($SRC);
829  if($meta === false) return $alt;
830  $info = $meta->getField($tags);
831  if($info == false) return $alt;
832  return $info;
833}
834
835/**
836 * Prints the image with a link to the full sized version
837 *
838 * Only allowed in: detail.php
839 */
840function tpl_img($maxwidth=0,$maxheight=0){
841  global $IMG;
842  $w = tpl_img_getTag('File.Width');
843  $h = tpl_img_getTag('File.Height');
844
845  //resize to given max values
846  $ratio = 1;
847  if($w >= $h){
848    if($maxwidth && $w >= $maxwidth){
849      $ratio = $maxwidth/$w;
850    }elseif($maxheight && $h > $maxheight){
851      $ratio = $maxheight/$h;
852    }
853  }else{
854    if($maxheight && $h >= $maxheight){
855      $ratio = $maxheight/$h;
856    }elseif($maxwidth && $w > $maxwidth){
857      $ratio = $maxwidth/$w;
858    }
859  }
860  if($ratio){
861    $w = floor($ratio*$w);
862    $h = floor($ratio*$h);
863  }
864
865  //prepare URLs
866  $url=ml($IMG,array('cache'=>$_REQUEST['cache']));
867  $src=ml($IMG,array('cache'=>$_REQUEST['cache'],'w'=>$w,'h'=>$h));
868
869  //prepare attributes
870  $alt=tpl_img_getTag('Simple.Title');
871  $p = array();
872  if($w) $p['width']  = $w;
873  if($h) $p['height'] = $h;
874         $p['class']  = 'img_detail';
875  if($alt){
876    $p['alt']   = $alt;
877    $p['title'] = $alt;
878  }else{
879    $p['alt'] = '';
880  }
881  $p = buildAttributes($p);
882
883  print '<a href="'.$url.'">';
884  print '<img src="'.$src.'" '.$p.'/>';
885  print '</a>';
886}
887
888/**
889 * This function inserts a 1x1 pixel gif which in reality
890 * is the inexer function.
891 *
892 * Should be called somewhere at the very end of the main.php
893 * template
894 */
895function tpl_indexerWebBug(){
896  global $ID;
897  global $INFO;
898  if(!$INFO['exists']) return;
899
900  $p = array();
901  $p['src']    = DOKU_BASE.'lib/exe/indexer.php?id='.urlencode($ID).
902                 '&'.time();
903  $p['width']  = 1;
904  $p['height'] = 1;
905  $p['alt']    = '';
906  $att = buildAttributes($p);
907  print "<img $att />";
908}
909
910//Setup VIM: ex: et ts=2 enc=utf-8 :
911