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