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