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