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