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