xref: /dokuwiki/inc/template.php (revision 6cd2b1e4b7588c56b52418c91396c264538d5a54)
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'=>'start', 'href'=>DOKU_BASE );
183  $head['link'][] = array( 'rel'=>'contents', 'href'=> wl($ID,'do=index',false,'&'),
184                           'title'=>$lang['btn_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(!empty($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' || $ACT=='wordblock' ) ? 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_OUTPUT',$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(!empty($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(!empty($INFO['draft'])) {
486            tpl_link(wl($ID,'do=draft'),
487                       $pre.$lang['btn_draft'].$suf,
488                       'class="action edit" acceskey="e" rel="nofollow"');
489          } else {
490            if($INFO['exists']){
491              tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
492                       $pre.$lang['btn_edit'].$suf,
493                       'class="action edit" accesskey="e" rel="nofollow"');
494            }else{
495              tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
496                       $pre.$lang['btn_create'].$suf,
497                       'class="action create" accesskey="e" rel="nofollow"');
498            }
499          }
500        }else{
501          if(!actionOK('source')) return false; //pseudo action
502          tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
503                   $pre.$lang['btn_source'].$suf,
504                   'class="action source" accesskey="v" rel="nofollow"');
505        }
506      }else{
507          tpl_link(wl($ID,'do=show'),
508                   $pre.$lang['btn_show'].$suf,
509                   'class="action show" accesskey="v" rel="nofollow"');
510      }
511      return true;
512    case 'history':
513      tpl_link(wl($ID,'do=revisions'),$pre.$lang['btn_revs'].$suf,'class="action revisions" accesskey="o"');
514      return true;
515    case 'recent':
516      tpl_link(wl($ID,'do=recent'),$pre.$lang['btn_recent'].$suf,'class="action recent" accesskey="r"');
517      return true;
518    case 'index':
519      tpl_link(wl($ID,'do=index'),$pre.$lang['btn_index'].$suf,'class="action index" accesskey="x"');
520      return true;
521    case 'top':
522      print '<a href="#dokuwiki__top" class="action top" accesskey="x">'.$pre.$lang['btn_top'].$suf.'</a>';
523      return true;
524    case 'back':
525      if ($ID = tpl_getparent($ID)) {
526        tpl_link(wl($ID,'do=show'),$pre.$lang['btn_back'].$suf,'class="action back" accesskey="b"');
527        return true;
528      }
529      return false;
530    case 'login':
531      if($conf['useacl']){
532        if($_SERVER['REMOTE_USER']){
533          tpl_link(wl($ID,'do=logout'),$pre.$lang['btn_logout'].$suf,'class="action logout"');
534        }else{
535          tpl_link(wl($ID,'do=login'),$pre.$lang['btn_login'].$suf,'class="action logout"');
536        }
537        return true;
538      }
539      return false;
540    case 'admin':
541      if($INFO['perm'] == AUTH_ADMIN){
542        tpl_link(wl($ID,'do=admin'),$pre.$lang['btn_admin'].$suf,'class="action admin"');
543        return true;
544      }
545      return false;
546   case 'subscribe':
547   case 'subscription':
548      if($conf['useacl'] && $ACT == 'show' && $conf['subscribers'] == 1){
549        if($_SERVER['REMOTE_USER']){
550          if($INFO['subscribed']) {
551            tpl_link(wl($ID,'do=unsubscribe'),$pre.$lang['btn_unsubscribe'].$suf,'class="action unsubscribe"');
552          } else {
553            tpl_link(wl($ID,'do=subscribe'),$pre.$lang['btn_subscribe'].$suf,'class="action subscribe"');
554          }
555          return true;
556        }
557      }
558      return false;
559    case 'backlink':
560      tpl_link(wl($ID,'do=backlink'),$pre.$lang['btn_backlink'].$suf, 'class="action backlink"');
561      return true;
562    case 'profile':
563      if($conf['useacl'] && $_SERVER['REMOTE_USER'] &&
564         $auth->canDo('Profile') && ($ACT!='profile')){
565        tpl_link(wl($ID,'do=profile'),$pre.$lang['btn_profile'].$suf, 'class="action profile"');
566        return true;
567      }
568      return false;
569    default:
570      print '[unknown link type]';
571      return true;
572  }
573}
574
575/**
576 * Print the search form
577 *
578 * If the first parameter is given a div with the ID 'qsearch_out' will
579 * be added which instructs the ajax pagequicksearch to kick in and place
580 * its output into this div. The second parameter controls the propritary
581 * attribute autocomplete. If set to false this attribute will be set with an
582 * value of "off" to instruct the browser to disable it's own built in
583 * autocompletion feature (MSIE and Firefox)
584 *
585 * @author Andreas Gohr <andi@splitbrain.org>
586 */
587function tpl_searchform($ajax=true,$autocomplete=true){
588  global $lang;
589  global $ACT;
590
591  print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">';
592  print '<input type="hidden" name="do" value="search" />';
593  print '<input type="text" ';
594  if($ACT == 'search') print 'value="'.htmlspecialchars($_REQUEST['id']).'" ';
595  if(!$autocomplete) print 'autocomplete="off" ';
596  print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />';
597  print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />';
598  if($ajax) print '<div id="qsearch__out" class="ajax_qsearch JSpopup"></div>';
599  print '</div></form>';
600}
601
602/**
603 * Print the breadcrumbs trace
604 *
605 * @author Andreas Gohr <andi@splitbrain.org>
606 */
607function tpl_breadcrumbs(){
608  global $lang;
609  global $conf;
610
611  //check if enabled
612  if(!$conf['breadcrumbs']) return;
613
614  $crumbs = breadcrumbs(); //setup crumb trace
615
616  //reverse crumborder in right-to-left mode
617  if($lang['direction'] == 'rtl') $crumbs = array_reverse($crumbs,true);
618
619  //render crumbs, highlight the last one
620  print $lang['breadcrumb'].':';
621  $last = count($crumbs);
622  $i = 0;
623  foreach ($crumbs as $id => $name){
624    $i++;
625    print ' <span class="bcsep">&raquo;</span> ';
626    if ($i == $last) print '<span class="curid">';
627    tpl_link(wl($id),$name,'class="breadcrumbs" title="'.$id.'"');
628    if ($i == $last) print '</span>';
629  }
630}
631
632/**
633 * Hierarchical breadcrumbs
634 *
635 * This code was suggested as replacement for the usual breadcrumbs.
636 * It only makes sense with a deep site structure.
637 *
638 * @author Andreas Gohr <andi@splitbrain.org>
639 * @author Nigel McNie <oracle.shinoda@gmail.com>
640 * @author Sean Coates <sean@caedmon.net>
641 * @link   http://wiki.splitbrain.org/wiki:tipsandtricks:hierarchicalbreadcrumbs
642 * @todo   May behave strangely in RTL languages
643 */
644function tpl_youarehere($sep=' &raquo; '){
645  global $conf;
646  global $ID;
647  global $lang;
648
649  // check if enabled
650  if(!$conf['youarehere']) return;
651
652  $parts = explode(':', $ID);
653  $count = count($parts);
654
655  echo $lang['youarehere'].': ';
656
657  // always print the startpage
658  $title = p_get_first_heading($conf['start']);
659  if(!$title) $title = $conf['start'];
660  tpl_link(wl($conf['start']),$title,'title="'.$conf['start'].'"');
661
662  // print intermediate namespace links
663  $part = '';
664  for($i=0; $i<$count - 1; $i++){
665    $part .= $parts[$i].':';
666    $page = $part;
667    resolve_pageid('',$page,$exists);
668    if ($page == $conf['start']) continue; // Skip startpage
669
670    // output
671    echo $sep;
672    if($exists){
673      $title = p_get_first_heading($page);
674      if(!$title) $title = $parts[$i];
675      tpl_link(wl($page),$title,'title="'.$page.'"');
676    }else{
677      tpl_link(wl($page),$parts[$i],'title="'.$page.'" class="wikilink2"');
678    }
679  }
680
681  // print current page, skipping start page, skipping for namespace index
682  if(isset($page) && $page==$part.$parts[$i]) return;
683  $page = $part.$parts[$i];
684  if($page == $conf['start']) return;
685  echo $sep;
686  if(@file_exists(wikiFN($page))){
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/**
696 * Print info if the user is logged in
697 * and show full name in that case
698 *
699 * Could be enhanced with a profile link in future?
700 *
701 * @author Andreas Gohr <andi@splitbrain.org>
702 */
703function tpl_userinfo(){
704  global $lang;
705  global $INFO;
706  if($_SERVER['REMOTE_USER'])
707    print $lang['loggedinas'].': '.$INFO['userinfo']['name'];
708}
709
710/**
711 * Print some info about the current page
712 *
713 * @author Andreas Gohr <andi@splitbrain.org>
714 */
715function tpl_pageinfo(){
716  global $conf;
717  global $lang;
718  global $INFO;
719  global $REV;
720
721  // prepare date and path
722  $fn = $INFO['filepath'];
723  if(!$conf['fullpath']){
724    if($REV){
725      $fn = str_replace(realpath($conf['olddir']).DIRECTORY_SEPARATOR,'',$fn);
726    }else{
727      $fn = str_replace(realpath($conf['datadir']).DIRECTORY_SEPARATOR,'',$fn);
728    }
729  }
730  $fn = utf8_decodeFN($fn);
731  $date = date($conf['dformat'],$INFO['lastmod']);
732
733  // print it
734  if($INFO['exists']){
735    print $fn;
736    print ' &middot; ';
737    print $lang['lastmod'];
738    print ': ';
739    print $date;
740    if($INFO['editor']){
741      print ' '.$lang['by'].' ';
742      print $INFO['editor'];
743    }
744    if($INFO['locked']){
745      print ' &middot; ';
746      print $lang['lockedby'];
747      print ': ';
748      print $INFO['locked'];
749    }
750  }
751}
752
753/**
754 * Prints or returns the name of the given page (current one if none given).
755 *
756 * If useheading is enabled this will use the first headline else
757 * the given ID is used.
758 *
759 * @author Andreas Gohr <andi@splitbrain.org>
760 */
761function tpl_pagetitle($id=null, $ret=false){
762  global $conf;
763  if(is_null($id)){
764    global $ID;
765    $id = $ID;
766  }
767
768  $name = $id;
769  if ($conf['useheading']) {
770    $title = p_get_first_heading($id);
771    if ($title) $name = $title;
772  }
773
774  if ($ret) {
775      return hsc($name);
776  } else {
777      print hsc($name);
778  }
779}
780
781/**
782 * Returns the requested EXIF/IPTC tag from the current image
783 *
784 * If $tags is an array all given tags are tried until a
785 * value is found. If no value is found $alt is returned.
786 *
787 * Which texts are known is defined in the functions _exifTagNames
788 * and _iptcTagNames() in inc/jpeg.php (You need to prepend IPTC
789 * to the names of the latter one)
790 *
791 * Only allowed in: detail.php
792 *
793 * @author Andreas Gohr <andi@splitbrain.org>
794 */
795function tpl_img_getTag($tags,$alt='',$src=null){
796  // Init Exif Reader
797  global $SRC;
798
799  if(is_null($src)) $src = $SRC;
800
801  static $meta = null;
802  if(is_null($meta)) $meta = new JpegMeta($src);
803  if($meta === false) return $alt;
804  $info = $meta->getField($tags);
805  if($info == false) return $alt;
806  return $info;
807}
808
809/**
810 * Prints the image with a link to the full sized version
811 *
812 * Only allowed in: detail.php
813 */
814function tpl_img($maxwidth=0,$maxheight=0){
815  global $IMG;
816  $w = tpl_img_getTag('File.Width');
817  $h = tpl_img_getTag('File.Height');
818
819  //resize to given max values
820  $ratio = 1;
821  if($w >= $h){
822    if($maxwidth && $w >= $maxwidth){
823      $ratio = $maxwidth/$w;
824    }elseif($maxheight && $h > $maxheight){
825      $ratio = $maxheight/$h;
826    }
827  }else{
828    if($maxheight && $h >= $maxheight){
829      $ratio = $maxheight/$h;
830    }elseif($maxwidth && $w > $maxwidth){
831      $ratio = $maxwidth/$w;
832    }
833  }
834  if($ratio){
835    $w = floor($ratio*$w);
836    $h = floor($ratio*$h);
837  }
838
839  //prepare URLs
840  $url=ml($IMG,array('cache'=>$_REQUEST['cache']));
841  $src=ml($IMG,array('cache'=>$_REQUEST['cache'],'w'=>$w,'h'=>$h));
842
843  //prepare attributes
844  $alt=tpl_img_getTag('Simple.Title');
845  $p = array();
846  if($w) $p['width']  = $w;
847  if($h) $p['height'] = $h;
848         $p['class']  = 'img_detail';
849  if($alt){
850    $p['alt']   = $alt;
851    $p['title'] = $alt;
852  }else{
853    $p['alt'] = '';
854  }
855  $p = buildAttributes($p);
856
857  print '<a href="'.$url.'">';
858  print '<img src="'.$src.'" '.$p.'/>';
859  print '</a>';
860}
861
862/**
863 * This function inserts a 1x1 pixel gif which in reality
864 * is the inexer function.
865 *
866 * Should be called somewhere at the very end of the main.php
867 * template
868 */
869function tpl_indexerWebBug(){
870  global $ID;
871  global $INFO;
872  if(!$INFO['exists']) return;
873
874  if(isHiddenPage($ID)) return; //no need to index hidden pages
875
876  $p = array();
877  $p['src']    = DOKU_BASE.'lib/exe/indexer.php?id='.rawurlencode($ID).
878                 '&'.time();
879  $p['width']  = 1;
880  $p['height'] = 1;
881  $p['alt']    = '';
882  $att = buildAttributes($p);
883  print "<img $att />";
884}
885
886// configuration methods
887/**
888 * tpl_getConf($id)
889 *
890 * use this function to access template configuration variables
891 */
892function tpl_getConf($id){
893  global $conf;
894  global $tpl_configloaded;
895
896  $tpl = $conf['template'];
897
898  if (!$tpl_configloaded){
899    $tconf = tpl_loadConfig();
900    if ($tconf !== false){
901      foreach ($tconf as $key => $value){
902        if (isset($conf['tpl'][$tpl][$key])) continue;
903        $conf['tpl'][$tpl][$key] = $value;
904      }
905      $tpl_configloaded = true;
906    }
907  }
908
909  return $conf['tpl'][$tpl][$id];
910}
911
912/**
913 * tpl_loadConfig()
914 * reads all template configuration variables
915 * this function is automatically called by tpl_getConf()
916 */
917function tpl_loadConfig(){
918
919  $file = DOKU_TPLINC.'/conf/default.php';
920  $conf = array();
921
922  if (!@file_exists($file)) return false;
923
924  // load default config file
925  include($file);
926
927  return $conf;
928}
929
930/**
931 * prints the "main content" in the mediamanger popup
932 *
933 * Depending on the user's actions this may be a list of
934 * files in a namespace, the meta editing dialog or
935 * a message of referencing pages
936 *
937 * Only allowed in mediamanager.php
938 *
939 * @author Andreas Gohr <andi@splitbrain.org>
940 */
941function tpl_mediaContent(){
942  global $IMG;
943  global $AUTH;
944  global $INUSE;
945  global $NS;
946  global $JUMPTO;
947
948  ptln('<div id="media__content">');
949  if($_REQUEST['edit']){
950    media_metaform($IMG,$AUTH);
951  }elseif(is_array($INUSE)){
952    media_filesinuse($INUSE,$IMG);
953  }else{
954    media_filelist($NS,$AUTH,$JUMPTO);
955  }
956  ptln('</div>');
957}
958
959/**
960 * prints the namespace tree in the mediamanger popup
961 *
962 * Only allowed in mediamanager.php
963 *
964 * @author Andreas Gohr <andi@splitbrain.org>
965 */
966function tpl_mediaTree(){
967  global $NS;
968
969  ptln('<div id="media__tree">');
970  media_nstree($NS);
971  ptln('</div>');
972}
973
974//Setup VIM: ex: et ts=2 enc=utf-8 :
975