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