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