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