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