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