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