xref: /dokuwiki/inc/template.php (revision f93b78f844341907b4c1953f01ef6cae86abeddf)
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',fullpath(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($prependTOC=true) {
40    global $ACT;
41    global $INFO;
42    $INFO['prependTOC'] = $prependTOC;
43
44    ob_start();
45    trigger_event('TPL_ACT_RENDER',$ACT,'tpl_content_core');
46    $html_output = ob_get_clean();
47    trigger_event('TPL_CONTENT_DISPLAY',$html_output,'ptln');
48
49    return !empty($html_output);
50}
51
52function tpl_content_core(){
53  global $ACT;
54  global $TEXT;
55  global $PRE;
56  global $SUF;
57  global $SUM;
58  global $IDX;
59
60  switch($ACT){
61    case 'show':
62      html_show();
63      break;
64    case 'preview':
65      html_edit($TEXT);
66      html_show($TEXT);
67      break;
68    case 'recover':
69      html_edit($TEXT);
70      break;
71    case 'edit':
72      html_edit();
73      break;
74    case 'draft':
75      html_draft();
76      break;
77    case 'wordblock':
78      html_edit($TEXT,'wordblock');
79      break;
80    case 'search':
81      html_search();
82      break;
83    case 'revisions':
84      $first = is_numeric($_REQUEST['first']) ? intval($_REQUEST['first']) : 0;
85      html_revisions($first);
86      break;
87    case 'diff':
88      html_diff();
89      break;
90    case 'recent':
91      $first = is_numeric($_REQUEST['first']) ? intval($_REQUEST['first']) : 0;
92      html_recent($first);
93      break;
94    case 'index':
95      html_index($IDX); #FIXME can this be pulled from globals? is it sanitized correctly?
96      break;
97    case 'backlink':
98      html_backlinks();
99      break;
100    case 'conflict':
101      html_conflict(con($PRE,$TEXT,$SUF),$SUM);
102      html_diff(con($PRE,$TEXT,$SUF),false);
103      break;
104    case 'locked':
105      html_locked();
106      break;
107    case 'login':
108      html_login();
109      break;
110    case 'register':
111      html_register();
112      break;
113    case 'resendpwd':
114      html_resendpwd();
115      break;
116    case 'denied':
117      print p_locale_xhtml('denied');
118      break;
119    case 'profile' :
120      html_updateprofile();
121      break;
122    case 'admin':
123      tpl_admin();
124      break;
125    default:
126      $evt = new Doku_Event('TPL_ACT_UNKNOWN',$ACT);
127      if ($evt->advise_before())
128        msg("Failed to handle command: ".hsc($ACT),-1);
129      $evt->advise_after();
130      unset($evt);
131      return false;
132  }
133  return true;
134}
135
136/**
137 * Places the TOC where the function is called
138 *
139 * If you use this you most probably want to call tpl_content with
140 * a false argument
141 *
142 * @author Andreas Gohr <andi@splitbrain.org>
143 */
144function tpl_toc($return=false){
145    global $TOC;
146    global $ACT;
147    global $ID;
148    global $REV;
149    global $INFO;
150    $toc = array();
151
152    if(is_array($TOC)){
153        // if a TOC was prepared in global scope, always use it
154        $toc = $TOC;
155    }elseif(($ACT == 'show' || substr($ACT,0,6) == 'export') && !$REV && $INFO['exists']){
156        // get TOC from metadata, render if neccessary
157        $meta = p_get_metadata($ID, false, true);
158        if(isset($meta['internal']['toc'])){
159            $tocok = $meta['internal']['toc'];
160        }else{
161            $tocok = true;
162        }
163        $toc   = $meta['description']['tableofcontents'];
164        if(!$tocok || !is_array($toc) || count($toc) < 3){
165            $toc = array();
166        }
167    }elseif($ACT == 'admin'){
168        // try to load admin plugin TOC FIXME: duplicates code from tpl_admin
169        $plugin = null;
170        if (!empty($_REQUEST['page'])) {
171            $pluginlist = plugin_list('admin');
172            if (in_array($_REQUEST['page'], $pluginlist)) {
173                // attempt to load the plugin
174                $plugin =& plugin_load('admin',$_REQUEST['page']);
175            }
176        }
177        if ( ($plugin !== null) &&
178             (!$plugin->forAdminOnly() || $INFO['isadmin']) ){
179            $toc = $plugin->getTOC();
180            $TOC = $toc; // avoid later rebuild
181        }
182    }
183
184    $html = html_TOC($toc);
185    if($return) return $html;
186    echo $html;
187}
188
189/**
190 * Handle the admin page contents
191 *
192 * @author Andreas Gohr <andi@splitbrain.org>
193 */
194function tpl_admin(){
195    global $INFO;
196    global $TOC;
197
198    $plugin = null;
199    if (!empty($_REQUEST['page'])) {
200        $pluginlist = plugin_list('admin');
201
202        if (in_array($_REQUEST['page'], $pluginlist)) {
203
204          // attempt to load the plugin
205          $plugin =& plugin_load('admin',$_REQUEST['page']);
206        }
207    }
208
209    if ($plugin !== null){
210        if($plugin->forAdminOnly() && !$INFO['isadmin']){
211            msg('For admins only',-1);
212            html_admin();
213        }else{
214            if(!is_array($TOC)) $TOC = $plugin->getTOC(); //if TOC wasn't requested yet
215            if($INFO['prependTOC']) tpl_toc();
216            $plugin->html();
217        }
218    }else{
219        html_admin();
220    }
221    return true;
222}
223
224/**
225 * Print the correct HTML meta headers
226 *
227 * This has to go into the head section of your template.
228 *
229 * @triggers TPL_METAHEADER_OUTPUT
230 * @param  boolean $alt Should feeds and alternative format links be added?
231 * @author Andreas Gohr <andi@splitbrain.org>
232 */
233function tpl_metaheaders($alt=true){
234  global $ID;
235  global $REV;
236  global $INFO;
237  global $ACT;
238  global $QUERY;
239  global $lang;
240  global $conf;
241  $it=2;
242
243  // prepare the head array
244  $head = array();
245
246
247  // the usual stuff
248  $head['meta'][] = array( 'name'=>'generator', 'content'=>'DokuWiki '.getVersion() );
249  $head['link'][] = array( 'rel'=>'search', 'type'=>'application/opensearchdescription+xml',
250                           'href'=>DOKU_BASE.'lib/exe/opensearch.php', 'title'=>$conf['title'] );
251  $head['link'][] = array( 'rel'=>'start', 'href'=>DOKU_BASE );
252  $head['link'][] = array( 'rel'=>'contents', 'href'=> wl($ID,'do=index',false,'&'),
253                           'title'=>$lang['btn_index'] );
254
255  if($alt){
256    $head['link'][] = array( 'rel'=>'alternate', 'type'=>'application/rss+xml',
257                             'title'=>'Recent Changes', 'href'=>DOKU_BASE.'feed.php');
258    $head['link'][] = array( 'rel'=>'alternate', 'type'=>'application/rss+xml',
259                             'title'=>'Current Namespace',
260                             'href'=>DOKU_BASE.'feed.php?mode=list&ns='.$INFO['namespace']);
261    if($ACT == 'search'){
262      $head['link'][] = array( 'rel'=>'alternate', 'type'=>'application/rss+xml',
263                               'title'=>'Search Result',
264                               'href'=>DOKU_BASE.'feed.php?mode=search&q='.$QUERY);
265    }
266
267    if(actionOK('export_xhtml')){
268      $head['link'][] = array( 'rel'=>'alternate', 'type'=>'text/html', 'title'=>'Plain HTML',
269                               'href'=>exportlink($ID, 'xhtml', '', false, '&'));
270    }
271
272    if(actionOK('export_raw')){
273      $head['link'][] = array( 'rel'=>'alternate', 'type'=>'text/plain', 'title'=>'Wiki Markup',
274                               'href'=>exportlink($ID, 'raw', '', false, '&'));
275    }
276  }
277
278  // setup robot tags apropriate for different modes
279  if( ($ACT=='show' || $ACT=='export_xhtml') && !$REV){
280    if($INFO['exists']){
281      //delay indexing:
282      if((time() - $INFO['lastmod']) >= $conf['indexdelay']){
283        $head['meta'][] = array( 'name'=>'robots', 'content'=>'index,follow');
284      }else{
285        $head['meta'][] = array( 'name'=>'robots', 'content'=>'noindex,nofollow');
286      }
287    }else{
288      $head['meta'][] = array( 'name'=>'robots', 'content'=>'noindex,follow');
289    }
290  }elseif(defined('DOKU_MEDIADETAIL')){
291    $head['meta'][] = array( 'name'=>'robots', 'content'=>'index,follow');
292  }else{
293    $head['meta'][] = array( 'name'=>'robots', 'content'=>'noindex,nofollow');
294  }
295
296  // set metadata
297  if($ACT == 'show' || $ACT=='export_xhtml'){
298    // date of modification
299    if($REV){
300      $head['meta'][] = array( 'name'=>'date', 'content'=>date('Y-m-d\TH:i:sO',$REV));
301    }else{
302      $head['meta'][] = array( 'name'=>'date', 'content'=>date('Y-m-d\TH:i:sO',$INFO['lastmod']));
303    }
304
305    // keywords (explicit or implicit)
306    if(!empty($INFO['meta']['subject'])){
307      $head['meta'][] = array( 'name'=>'keywords', 'content'=>join(',',$INFO['meta']['subject']));
308    }else{
309      $head['meta'][] = array( 'name'=>'keywords', 'content'=>str_replace(':',',',$ID));
310    }
311  }
312
313  // load stylesheets
314  $head['link'][] = array('rel'=>'stylesheet', 'media'=>'all', 'type'=>'text/css',
315                          'href'=>DOKU_BASE.'lib/exe/css.php?s=all&t='.$conf['template']);
316  $head['link'][] = array('rel'=>'stylesheet', 'media'=>'screen', 'type'=>'text/css',
317                          'href'=>DOKU_BASE.'lib/exe/css.php?t='.$conf['template']);
318  $head['link'][] = array('rel'=>'stylesheet', 'media'=>'print', 'type'=>'text/css',
319                          'href'=>DOKU_BASE.'lib/exe/css.php?s=print&t='.$conf['template']);
320
321  // load javascript
322  $js_edit  = ($ACT=='edit' || $ACT=='preview' || $ACT=='recover' || $ACT=='wordblock' ) ? 1 : 0;
323  $js_write = ($INFO['writable']) ? 1 : 0;
324  if(defined('DOKU_MEDIAMANAGER')){
325    $js_edit  = 1;
326    $js_write = 0;
327  }
328  if(($js_edit && $js_write) || defined('DOKU_MEDIAMANAGER')){
329    $script = "NS='".$INFO['namespace']."';";
330    if($conf['useacl'] && $_SERVER['REMOTE_USER']){
331      require_once(DOKU_INC.'inc/toolbar.php');
332      $script .= "SIG='".toolbar_signature()."';";
333    }
334    $head['script'][] = array( 'type'=>'text/javascript', 'charset'=>'utf-8',
335                               '_data'=> $script);
336  }
337  $head['script'][] = array( 'type'=>'text/javascript', 'charset'=>'utf-8', '_data'=>'',
338                             'src'=>DOKU_BASE.'lib/exe/js.php?edit='.$js_edit.'&write='.$js_write);
339
340  // trigger event here
341  trigger_event('TPL_METAHEADER_OUTPUT',$head,'_tpl_metaheaders_action',true);
342  return true;
343}
344
345/**
346 * prints the array build by tpl_metaheaders
347 *
348 * $data is an array of different header tags. Each tag can have multiple
349 * instances. Attributes are given as key value pairs. Values will be HTML
350 * encoded automatically so they should be provided as is in the $data array.
351 *
352 * For tags having a body attribute specify the the body data in the special
353 * attribute '_data'. This field will NOT BE ESCAPED automatically.
354 *
355 * @author Andreas Gohr <andi@splitbrain.org>
356 */
357function _tpl_metaheaders_action($data){
358  foreach($data as $tag => $inst){
359    foreach($inst as $attr){
360      echo '<',$tag,' ',buildAttributes($attr);
361      if(isset($attr['_data'])){
362          if($tag == 'script' && $attr['_data'])
363            $attr['_data'] = "<!--//--><![CDATA[//><!--\n".
364                             $attr['_data'].
365                             "\n//--><!]]>";
366
367          echo '>',$attr['_data'],'</',$tag,'>';
368      }else{
369        echo '/>';
370      }
371      echo "\n";
372    }
373  }
374}
375
376/**
377 * Print a link
378 *
379 * Just builds a link.
380 *
381 * @author Andreas Gohr <andi@splitbrain.org>
382 */
383function tpl_link($url,$name,$more=''){
384  print '<a href="'.$url.'" ';
385  if ($more) print ' '.$more;
386  print ">$name</a>";
387  return true;
388}
389
390/**
391 * Prints a link to a WikiPage
392 *
393 * Wrapper around html_wikilink
394 *
395 * @author Andreas Gohr <andi@splitbrain.org>
396 */
397function tpl_pagelink($id,$name=NULL){
398  print html_wikilink($id,$name);
399  return true;
400}
401
402/**
403 * get the parent page
404 *
405 * Tries to find out which page is parent.
406 * returns false if none is available
407 *
408 * @author Andreas Gohr <andi@splitbrain.org>
409 */
410function tpl_getparent($id){
411  global $conf;
412  $parent = getNS($id).':';
413  resolve_pageid('',$parent,$exists);
414  if($parent == $id) {
415    $pos = strrpos (getNS($id),':');
416    $parent = substr($parent,0,$pos).':';
417    resolve_pageid('',$parent,$exists);
418    if($parent == $id) return false;
419  }
420  return $parent;
421}
422
423/**
424 * Print one of the buttons
425 *
426 * Available Buttons are
427 *
428 *  edit        - edit/create/show/draft button
429 *  history     - old revisions
430 *  recent      - recent changes
431 *  login       - login/logout button - if ACL enabled
432 *  profile     - user profile button (if logged in)
433 *  index       - The index
434 *  admin       - admin page - if enough rights
435 *  top         - a back to top button
436 *  back        - a back to parent button - if available
437 *  backlink    - links to the list of backlinks
438 *  subscription- subscribe/unsubscribe button
439 *
440 * @author Andreas Gohr <andi@splitbrain.org>
441 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
442 */
443function tpl_button($type){
444  global $ACT;
445  global $ID;
446  global $REV;
447  global $NS;
448  global $INFO;
449  global $conf;
450  global $auth;
451
452  // check disabled actions and fix the badly named ones
453  $ctype = $type;
454  if($type == 'history') $ctype='revisions';
455  if(!actionOK($ctype)) return false;
456
457  switch($type){
458    case 'edit':
459      #most complicated type - we need to decide on current action
460      if($ACT == 'show' || $ACT == 'search'){
461        if($INFO['writable']){
462          if(!empty($INFO['draft'])){
463            echo html_btn('draft',$ID,'e',array('do' => 'draft'),'post');
464          }else{
465            if($INFO['exists']){
466              echo html_btn('edit',$ID,'e',array('do' => 'edit','rev' => $REV),'post');
467            }else{
468              echo html_btn('create',$ID,'e',array('do' => 'edit','rev' => $REV),'post');
469            }
470          }
471        }else{
472          if(!actionOK('source')) return false; //pseudo action
473          echo html_btn('source',$ID,'v',array('do' => 'edit','rev' => $REV),'post');
474        }
475      }else{
476          echo html_btn('show',$ID,'v',array('do' => 'show'));
477      }
478      return true;
479    case 'history':
480      print html_btn('revs',$ID,'o',array('do' => 'revisions'));
481      return true;
482    case 'recent':
483      print html_btn('recent','','r',array('do' => 'recent'));
484      return true;
485    case 'index':
486      print html_btn('index',$ID,'x',array('do' => 'index'));
487      return true;
488    case 'back':
489      if ($parent = tpl_getparent($ID)) {
490        print html_btn('back',$parent,'b',array('do' => 'show'));
491        return true;
492      }
493      return false;
494    case 'top':
495      print html_topbtn();
496      return true;
497    case 'login':
498      if($conf['useacl'] && $auth){
499        if($_SERVER['REMOTE_USER']){
500          print html_btn('logout',$ID,'',array('do' => 'logout', 'sectok' => getSecurityToken()));
501        }else{
502          print html_btn('login',$ID,'',array('do' => 'login', 'sectok' => getSecurityToken()));
503        }
504        return true;
505      }
506      return false;
507    case 'admin':
508      if($INFO['ismanager']){
509        print html_btn('admin',$ID,'',array('do' => 'admin'));
510        return true;
511      }
512      return false;
513    case 'subscribe':
514    case 'subscription':
515      if($conf['useacl'] && $auth && $ACT == 'show' && $conf['subscribers'] == 1){
516        if($_SERVER['REMOTE_USER']){
517          if($INFO['subscribed']){
518            print html_btn('unsubscribe',$ID,'',array('do' => 'unsubscribe',));
519          } else {
520            print html_btn('subscribe',$ID,'',array('do' => 'subscribe',));
521          }
522          if($type == 'subscribe') return true;
523        }
524      }
525      if($type == 'subscribe') return false;
526      // fall through for backward compatibility
527    case 'subscribens':
528      if($conf['useacl'] && $auth && $ACT == 'show' && $conf['subscribers'] == 1){
529        if($_SERVER['REMOTE_USER']){
530          if($INFO['subscribedns']){
531            print html_btn('unsubscribens',$ID,'',array('do' => 'unsubscribens',));
532          } else {
533            print html_btn('subscribens',$ID,'',array('do' => 'subscribens',));
534          }
535          return true;
536        }
537      }
538      return false;
539    case 'backlink':
540      print html_btn('backlink',$ID,'',array('do' => 'backlink'));
541      return true;
542    case 'profile':
543      if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $auth &&
544         $auth->canDo('Profile') && ($ACT!='profile')){
545        print html_btn('profile',$ID,'',array('do' => 'profile'));
546        return true;
547      }
548      return false;
549    default:
550      print '[unknown button type]';
551      return true;
552  }
553}
554
555/**
556 * Like the action buttons but links
557 *
558 * Available links are
559 *
560 *  edit    - edit/create/show link
561 *  history - old revisions
562 *  recent  - recent changes
563 *  login   - login/logout link - if ACL enabled
564 *  profile - user profile link (if logged in)
565 *  index   - The index
566 *  admin   - admin page - if enough rights
567 *  top     - a back to top link
568 *  back    - a back to parent link - if available
569 *  backlink - links to the list of backlinks
570 *  subscribe/subscription - subscribe/unsubscribe link
571 *
572 * @author Andreas Gohr <andi@splitbrain.org>
573 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
574 * @see    tpl_button
575 */
576function tpl_actionlink($type,$pre='',$suf='',$inner=''){
577  global $ID;
578  global $INFO;
579  global $REV;
580  global $ACT;
581  global $conf;
582  global $lang;
583  global $auth;
584
585  // check disabled actions and fix the badly named ones
586  $ctype = $type;
587  if($type == 'history') $ctype='revisions';
588  if(!actionOK($ctype)) return false;
589
590  switch($type){
591    case 'edit':
592      #most complicated type - we need to decide on current action
593      if($ACT == 'show' || $ACT == 'search'){
594        if($INFO['writable']){
595          if(!empty($INFO['draft'])) {
596            tpl_link(wl($ID,'do=draft'),
597                       $pre.(($inner)?$inner:$lang['btn_draft']).$suf,
598                       'class="action edit" accesskey="e" rel="nofollow"');
599          } else {
600            if($INFO['exists']){
601              tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
602                       $pre.(($inner)?$inner:$lang['btn_edit']).$suf,
603                       'class="action edit" accesskey="e" rel="nofollow"');
604            }else{
605              tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
606                       $pre.(($inner)?$inner:$lang['btn_create']).$suf,
607                       'class="action create" accesskey="e" rel="nofollow"');
608            }
609          }
610        }else{
611          if(!actionOK('source')) return false; //pseudo action
612          tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
613                   $pre.(($inner)?$inner:$lang['btn_source']).$suf,
614                   'class="action source" accesskey="v" rel="nofollow"');
615        }
616      }else{
617          tpl_link(wl($ID,'do=show'),
618                   $pre.(($inner)?$inner:$lang['btn_show']).$suf,
619                   'class="action show" accesskey="v" rel="nofollow"');
620      }
621      return true;
622    case 'history':
623      tpl_link(wl($ID,'do=revisions'),
624               $pre.(($inner)?$inner:$lang['btn_revs']).$suf,
625               'class="action revisions" accesskey="o" rel="nofollow"');
626      return true;
627    case 'recent':
628      tpl_link(wl('','do=recent'),
629               $pre.(($inner)?$inner:$lang['btn_recent']).$suf,
630              'class="action recent" accesskey="r" rel="nofollow"');
631      return true;
632    case 'index':
633      tpl_link(wl($ID,'do=index'),
634               $pre.(($inner)?$inner:$lang['btn_index']).$suf,
635              'class="action index" accesskey="x" rel="nofollow"');
636      return true;
637    case 'top':
638      print '<a href="#dokuwiki__top" class="action top" accesskey="x">'.
639            $pre.(($inner)?$inner:$lang['btn_top']).$suf.'</a>';
640      return true;
641    case 'back':
642      if ($parent = tpl_getparent($ID)) {
643        tpl_link(wl($parent,'do=show'),
644        $pre.(($inner)?$inner:$lang['btn_back']).$suf,
645        'class="action back" accesskey="b" rel="nofollow"');
646        return true;
647      }
648      return false;
649    case 'login':
650      if($conf['useacl'] && $auth){
651        if($_SERVER['REMOTE_USER']){
652          tpl_link(wl($ID,'do=logout&amp;sectok='.getSecurityToken()),
653                   $pre.(($inner)?$inner:$lang['btn_logout']).$suf,
654                   'class="action logout" rel="nofollow"');
655        }else{
656          tpl_link(wl($ID,'do=login&amp;sectok='.getSecurityToken()),
657                   $pre.(($inner)?$inner:$lang['btn_login']).$suf,
658                   'class="action login" rel="nofollow"');
659        }
660        return true;
661      }
662      return false;
663    case 'admin':
664      if($INFO['ismanager']){
665        tpl_link(wl($ID,'do=admin'),
666                 $pre.(($inner)?$inner:$lang['btn_admin']).$suf,
667                 'class="action admin" rel="nofollow"');
668        return true;
669      }
670      return false;
671   case 'subscribe':
672   case 'subscription':
673      if($conf['useacl'] && $auth && $ACT == 'show' && $conf['subscribers'] == 1){
674        if($_SERVER['REMOTE_USER']){
675          if($INFO['subscribed']) {
676            tpl_link(wl($ID,'do=unsubscribe'),
677                     $pre.(($inner)?$inner:$lang['btn_unsubscribe']).$suf,
678                     'class="action unsubscribe" rel="nofollow"');
679          } else {
680            tpl_link(wl($ID,'do=subscribe'),
681                     $pre.(($inner)?$inner:$lang['btn_subscribe']).$suf,
682                     'class="action subscribe" rel="nofollow"');
683          }
684          return true;
685        }
686      }
687      return false;
688    case 'subscribens':
689      if($conf['useacl'] && $auth && $ACT == 'show' && $conf['subscribers'] == 1){
690        if($_SERVER['REMOTE_USER']){
691          if($INFO['subscribedns']) {
692            tpl_link(wl($ID,'do=unsubscribens'),
693                     $pre.(($inner)?$inner:$lang['btn_unsubscribens']).$suf,
694                     'class="action unsubscribens" rel="nofollow"');
695          } else {
696            tpl_link(wl($ID,'do=subscribens'),
697                     $pre.(($inner)?$inner:$lang['btn_subscribens']).$suf,
698                     'class="action subscribens" rel="nofollow"');
699          }
700          return true;
701        }
702      }
703      return false;
704    case 'backlink':
705      tpl_link(wl($ID,'do=backlink'),
706               $pre.(($inner)?$inner:$lang['btn_backlink']).$suf,
707               'class="action backlink" rel="nofollow"');
708      return true;
709    case 'profile':
710      if($conf['useacl'] && $auth && $_SERVER['REMOTE_USER'] &&
711         $auth->canDo('Profile') && ($ACT!='profile')){
712        tpl_link(wl($ID,'do=profile'),
713                 $pre.(($inner)?$inner:$lang['btn_profile']).$suf,
714                 'class="action profile" rel="nofollow"');
715        return true;
716      }
717      return false;
718    default:
719      print '[unknown link type]';
720      return true;
721  }
722}
723
724/**
725 * Print the search form
726 *
727 * If the first parameter is given a div with the ID 'qsearch_out' will
728 * be added which instructs the ajax pagequicksearch to kick in and place
729 * its output into this div. The second parameter controls the propritary
730 * attribute autocomplete. If set to false this attribute will be set with an
731 * value of "off" to instruct the browser to disable it's own built in
732 * autocompletion feature (MSIE and Firefox)
733 *
734 * @author Andreas Gohr <andi@splitbrain.org>
735 */
736function tpl_searchform($ajax=true,$autocomplete=true){
737  global $lang;
738  global $ACT;
739
740  // don't print the search form if search action has been disabled
741  if (!actionOk('search')) return false;
742
743  print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">';
744  print '<input type="hidden" name="do" value="search" />';
745  print '<input type="text" ';
746  if($ACT == 'search') print 'value="'.htmlspecialchars($_REQUEST['id']).'" ';
747  if(!$autocomplete) print 'autocomplete="off" ';
748  print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />';
749  print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />';
750  if($ajax) print '<div id="qsearch__out" class="ajax_qsearch JSpopup"></div>';
751  print '</div></form>';
752  return true;
753}
754
755/**
756 * Print the breadcrumbs trace
757 *
758 * @author Andreas Gohr <andi@splitbrain.org>
759 */
760function tpl_breadcrumbs($sep='&raquo;'){
761  global $lang;
762  global $conf;
763
764  //check if enabled
765  if(!$conf['breadcrumbs']) return false;
766
767  $crumbs = breadcrumbs(); //setup crumb trace
768
769  //reverse crumborder in right-to-left mode, add RLM character to fix heb/eng display mixups
770  if($lang['direction'] == 'rtl') {
771    $crumbs = array_reverse($crumbs,true);
772    $crumbs_sep = ' &#8207;<span class="bcsep">'.$sep.'</span>&#8207; ';
773  } else {
774    $crumbs_sep = ' <span class="bcsep">'.$sep.'</span> ';
775  }
776
777  //render crumbs, highlight the last one
778  print '<span class="bchead">'.$lang['breadcrumb'].':</span>';
779  $last = count($crumbs);
780  $i = 0;
781  foreach ($crumbs as $id => $name){
782    $i++;
783    echo $crumbs_sep;
784    if ($i == $last) print '<span class="curid">';
785    tpl_link(wl($id),hsc($name),'class="breadcrumbs" title="'.$id.'"');
786    if ($i == $last) print '</span>';
787  }
788  return true;
789}
790
791/**
792 * Hierarchical breadcrumbs
793 *
794 * This code was suggested as replacement for the usual breadcrumbs.
795 * It only makes sense with a deep site structure.
796 *
797 * @author Andreas Gohr <andi@splitbrain.org>
798 * @author Nigel McNie <oracle.shinoda@gmail.com>
799 * @author Sean Coates <sean@caedmon.net>
800 * @link   http://wiki.splitbrain.org/wiki:tipsandtricks:hierarchicalbreadcrumbs
801 * @todo   May behave strangely in RTL languages
802 */
803function tpl_youarehere($sep=' &raquo; '){
804  global $conf;
805  global $ID;
806  global $lang;
807
808  // check if enabled
809  if(!$conf['youarehere']) return false;
810
811  $parts = explode(':', $ID);
812  $count = count($parts);
813
814  echo '<span class="bchead">'.$lang['youarehere'].': </span>';
815
816  // always print the startpage
817  $title = p_get_first_heading($conf['start']);
818  if(!$title) $title = $conf['start'];
819  tpl_link(wl($conf['start']),hsc($title),'title="'.$conf['start'].'"');
820
821  // print intermediate namespace links
822  $part = '';
823  for($i=0; $i<$count - 1; $i++){
824    $part .= $parts[$i].':';
825    $page = $part;
826    resolve_pageid('',$page,$exists);
827    if ($page == $conf['start']) continue; // Skip startpage
828
829    // output
830    echo $sep;
831    if($exists){
832      $title = p_get_first_heading($page);
833      if(!$title) $title = $parts[$i];
834      tpl_link(wl($page),hsc($title),'title="'.$page.'"');
835    }else{
836      tpl_link(wl($page),$parts[$i],'title="'.$page.'" class="wikilink2" rel="nofollow"');
837    }
838  }
839
840  // print current page, skipping start page, skipping for namespace index
841  if(isset($page) && $page==$part.$parts[$i]) return;
842  $page = $part.$parts[$i];
843  if($page == $conf['start']) return;
844  echo $sep;
845  if(page_exists($page)){
846    $title = p_get_first_heading($page);
847    if(!$title) $title = $parts[$i];
848    tpl_link(wl($page),hsc($title),'title="'.$page.'"');
849  }else{
850    tpl_link(wl($page),$parts[$i],'title="'.$page.'" class="wikilink2" rel="nofollow"');
851  }
852  return true;
853}
854
855/**
856 * Print info if the user is logged in
857 * and show full name in that case
858 *
859 * Could be enhanced with a profile link in future?
860 *
861 * @author Andreas Gohr <andi@splitbrain.org>
862 */
863function tpl_userinfo(){
864  global $lang;
865  global $INFO;
866  if($_SERVER['REMOTE_USER']){
867    print $lang['loggedinas'].': '.$INFO['userinfo']['name'];
868    return true;
869  }
870  return false;
871}
872
873/**
874 * Print some info about the current page
875 *
876 * @author Andreas Gohr <andi@splitbrain.org>
877 */
878function tpl_pageinfo($ret=false){
879  global $conf;
880  global $lang;
881  global $INFO;
882  global $REV;
883  global $ID;
884
885  // return if we are not allowed to view the page
886  if (!auth_quickaclcheck($ID)) { return false; }
887
888  // prepare date and path
889  $fn = $INFO['filepath'];
890  if(!$conf['fullpath']){
891    if($REV){
892      $fn = str_replace(fullpath($conf['olddir']).'/','',$fn);
893    }else{
894      $fn = str_replace(fullpath($conf['datadir']).'/','',$fn);
895    }
896  }
897  $fn = utf8_decodeFN($fn);
898  $date = strftime($conf['dformat'],$INFO['lastmod']);
899
900  // print it
901  if($INFO['exists']){
902    $out = '';
903    $out .= $fn;
904    $out .= ' &middot; ';
905    $out .= $lang['lastmod'];
906    $out .= ': ';
907    $out .= $date;
908    if($INFO['editor']){
909      $out .= ' '.$lang['by'].' ';
910      $out .= $INFO['editor'];
911    }else{
912      $out .= ' ('.$lang['external_edit'].')';
913    }
914    if($INFO['locked']){
915      $out .= ' &middot; ';
916      $out .= $lang['lockedby'];
917      $out .= ': ';
918      $out .= $INFO['locked'];
919    }
920    if($ret){
921        return $out;
922    }else{
923        echo $out;
924        return true;
925    }
926  }
927  return false;
928}
929
930/**
931 * Prints or returns the name of the given page (current one if none given).
932 *
933 * If useheading is enabled this will use the first headline else
934 * the given ID is used.
935 *
936 * @author Andreas Gohr <andi@splitbrain.org>
937 */
938function tpl_pagetitle($id=null, $ret=false){
939  global $conf;
940  if(is_null($id)){
941    global $ID;
942    $id = $ID;
943  }
944
945  $name = $id;
946  if ($conf['useheading']) {
947    $title = p_get_first_heading($id);
948    if ($title) $name = $title;
949  }
950
951  if ($ret) {
952      return hsc($name);
953  } else {
954      print hsc($name);
955      return true;
956  }
957}
958
959/**
960 * Returns the requested EXIF/IPTC tag from the current image
961 *
962 * If $tags is an array all given tags are tried until a
963 * value is found. If no value is found $alt is returned.
964 *
965 * Which texts are known is defined in the functions _exifTagNames
966 * and _iptcTagNames() in inc/jpeg.php (You need to prepend IPTC
967 * to the names of the latter one)
968 *
969 * Only allowed in: detail.php
970 *
971 * @author Andreas Gohr <andi@splitbrain.org>
972 */
973function tpl_img_getTag($tags,$alt='',$src=null){
974  // Init Exif Reader
975  global $SRC;
976
977  if(is_null($src)) $src = $SRC;
978
979  static $meta = null;
980  if(is_null($meta)) $meta = new JpegMeta($src);
981  if($meta === false) return $alt;
982  $info = $meta->getField($tags);
983  if($info == false) return $alt;
984  return $info;
985}
986
987/**
988 * Prints the image with a link to the full sized version
989 *
990 * Only allowed in: detail.php
991 */
992function tpl_img($maxwidth=0,$maxheight=0){
993  global $IMG;
994  $w = tpl_img_getTag('File.Width');
995  $h = tpl_img_getTag('File.Height');
996
997  //resize to given max values
998  $ratio = 1;
999  if($w >= $h){
1000    if($maxwidth && $w >= $maxwidth){
1001      $ratio = $maxwidth/$w;
1002    }elseif($maxheight && $h > $maxheight){
1003      $ratio = $maxheight/$h;
1004    }
1005  }else{
1006    if($maxheight && $h >= $maxheight){
1007      $ratio = $maxheight/$h;
1008    }elseif($maxwidth && $w > $maxwidth){
1009      $ratio = $maxwidth/$w;
1010    }
1011  }
1012  if($ratio){
1013    $w = floor($ratio*$w);
1014    $h = floor($ratio*$h);
1015  }
1016
1017  //prepare URLs
1018  $url=ml($IMG,array('cache'=>$_REQUEST['cache']));
1019  $src=ml($IMG,array('cache'=>$_REQUEST['cache'],'w'=>$w,'h'=>$h));
1020
1021  //prepare attributes
1022  $alt=tpl_img_getTag('Simple.Title');
1023  $p = array();
1024  if($w) $p['width']  = $w;
1025  if($h) $p['height'] = $h;
1026         $p['class']  = 'img_detail';
1027  if($alt){
1028    $p['alt']   = $alt;
1029    $p['title'] = $alt;
1030  }else{
1031    $p['alt'] = '';
1032  }
1033  $p = buildAttributes($p);
1034
1035  print '<a href="'.$url.'">';
1036  print '<img src="'.$src.'" '.$p.'/>';
1037  print '</a>';
1038  return true;
1039}
1040
1041/**
1042 * This function inserts a 1x1 pixel gif which in reality
1043 * is the inexer function.
1044 *
1045 * Should be called somewhere at the very end of the main.php
1046 * template
1047 */
1048function tpl_indexerWebBug(){
1049  global $ID;
1050  global $INFO;
1051  if(!$INFO['exists']) return false;
1052
1053  if(isHiddenPage($ID)) return false; //no need to index hidden pages
1054
1055  $p = array();
1056  $p['src']    = DOKU_BASE.'lib/exe/indexer.php?id='.rawurlencode($ID).
1057                 '&'.time();
1058  $p['width']  = 1;
1059  $p['height'] = 1;
1060  $p['alt']    = '';
1061  $att = buildAttributes($p);
1062  print "<img $att />";
1063  return true;
1064}
1065
1066// configuration methods
1067/**
1068 * tpl_getConf($id)
1069 *
1070 * use this function to access template configuration variables
1071 */
1072function tpl_getConf($id){
1073  global $conf;
1074  global $tpl_configloaded;
1075
1076  $tpl = $conf['template'];
1077
1078  if (!$tpl_configloaded){
1079    $tconf = tpl_loadConfig();
1080    if ($tconf !== false){
1081      foreach ($tconf as $key => $value){
1082        if (isset($conf['tpl'][$tpl][$key])) continue;
1083        $conf['tpl'][$tpl][$key] = $value;
1084      }
1085      $tpl_configloaded = true;
1086    }
1087  }
1088
1089  return $conf['tpl'][$tpl][$id];
1090}
1091
1092/**
1093 * tpl_loadConfig()
1094 * reads all template configuration variables
1095 * this function is automatically called by tpl_getConf()
1096 */
1097function tpl_loadConfig(){
1098
1099  $file = DOKU_TPLINC.'/conf/default.php';
1100  $conf = array();
1101
1102  if (!@file_exists($file)) return false;
1103
1104  // load default config file
1105  include($file);
1106
1107  return $conf;
1108}
1109
1110/**
1111 * prints the "main content" in the mediamanger popup
1112 *
1113 * Depending on the user's actions this may be a list of
1114 * files in a namespace, the meta editing dialog or
1115 * a message of referencing pages
1116 *
1117 * Only allowed in mediamanager.php
1118 *
1119 * @author Andreas Gohr <andi@splitbrain.org>
1120 */
1121function tpl_mediaContent(){
1122  global $IMG;
1123  global $AUTH;
1124  global $INUSE;
1125  global $NS;
1126  global $JUMPTO;
1127
1128  ptln('<div id="media__content">');
1129  if($_REQUEST['edit']){
1130    media_metaform($IMG,$AUTH);
1131  }elseif(is_array($INUSE)){
1132    media_filesinuse($INUSE,$IMG);
1133  }else{
1134    media_filelist($NS,$AUTH,$JUMPTO);
1135  }
1136  ptln('</div>');
1137}
1138
1139/**
1140 * prints the namespace tree in the mediamanger popup
1141 *
1142 * Only allowed in mediamanager.php
1143 *
1144 * @author Andreas Gohr <andi@splitbrain.org>
1145 */
1146function tpl_mediaTree(){
1147  global $NS;
1148
1149  ptln('<div id="media__tree">');
1150  media_nstree($NS);
1151  ptln('</div>');
1152}
1153
1154//Setup VIM: ex: et ts=4 enc=utf-8 :
1155