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