xref: /dokuwiki/inc/template.php (revision ae112d8c54bbccff31a9c9bd59ef9b3db8a70582)
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',fullpath(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 *
16 * @author Andreas Gohr <andi@splitbrain.org>
17 */
18function template($tpl){
19  global $conf;
20
21  if(@is_readable(DOKU_INC.'lib/tpl/'.$conf['template'].'/'.$tpl))
22    return DOKU_INC.'lib/tpl/'.$conf['template'].'/'.$tpl;
23
24  return DOKU_INC.'lib/tpl/default/'.$tpl;
25}
26
27/**
28 * Print the content
29 *
30 * This function is used for printing all the usual content
31 * (defined by the global $ACT var) by calling the appropriate
32 * outputfunction(s) from html.php
33 *
34 * Everything that doesn't use the main template file isn't
35 * handled by this function. ACL stuff is not done here either.
36 *
37 * @author Andreas Gohr <andi@splitbrain.org>
38 */
39function tpl_content($prependTOC=true) {
40    global $ACT;
41    global $INFO;
42    $INFO['prependTOC'] = $prependTOC;
43
44    ob_start();
45    trigger_event('TPL_ACT_RENDER',$ACT,'tpl_content_core');
46    $html_output = ob_get_clean();
47    trigger_event('TPL_CONTENT_DISPLAY',$html_output,'ptln');
48
49    return !empty($html_output);
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      return false;
132  }
133  return true;
134}
135
136/**
137 * Places the TOC where the function is called
138 *
139 * If you use this you most probably want to call tpl_content with
140 * a false argument
141 *
142 * @author Andreas Gohr <andi@splitbrain.org>
143 */
144function tpl_toc($return=false){
145    global $TOC;
146    global $ACT;
147    global $ID;
148    global $REV;
149    global $INFO;
150    $toc = array();
151
152    if(is_array($TOC)){
153        // if a TOC was prepared in global scope, always use it
154        $toc = $TOC;
155    }elseif($ACT == 'show' && !$REV){
156        // get TOC from metadata, render if neccessary
157        $meta = p_get_metadata($ID, false, true);
158        if(isset($meta['internal']['toc'])){
159            $tocok = $meta['internal']['toc'];
160        }else{
161            $tocok = true;
162        }
163        $toc   = $meta['description']['tableofcontents'];
164        if(!$tocok || !is_array($toc) || count($toc) < 3){
165            $toc = array();
166        }
167    }elseif($ACT == 'admin'){
168        // try to load admin plugin TOC FIXME: duplicates code from tpl_admin
169        $plugin = null;
170        if (!empty($_REQUEST['page'])) {
171            $pluginlist = plugin_list('admin');
172            if (in_array($_REQUEST['page'], $pluginlist)) {
173                // attempt to load the plugin
174                $plugin =& plugin_load('admin',$_REQUEST['page']);
175            }
176        }
177        if ( ($plugin !== null) &&
178             (!$plugin->forAdminOnly() || $INFO['isadmin']) ){
179            $toc = $plugin->getTOC();
180            $TOC = $toc; // avoid later rebuild
181        }
182    }
183
184    $html = html_TOC($toc);
185    if($return) return $html;
186    echo $html;
187}
188
189/**
190 * Handle the admin page contents
191 *
192 * @author Andreas Gohr <andi@splitbrain.org>
193 */
194function tpl_admin(){
195    global $INFO;
196    global $TOC;
197
198    $plugin = null;
199    if (!empty($_REQUEST['page'])) {
200        $pluginlist = plugin_list('admin');
201
202        if (in_array($_REQUEST['page'], $pluginlist)) {
203
204          // attempt to load the plugin
205          $plugin =& plugin_load('admin',$_REQUEST['page']);
206        }
207    }
208
209    if ($plugin !== null){
210        if($plugin->forAdminOnly() && !$INFO['isadmin']){
211            msg('For admins only',-1);
212            html_admin();
213        }else{
214            if(!is_array($TOC)) $TOC = $plugin->getTOC(); //if TOC wasn't requested yet
215            if($INFO['prependTOC']) tpl_toc();
216            $plugin->html();
217        }
218    }else{
219        html_admin();
220    }
221    return true;
222}
223
224/**
225 * Print the correct HTML meta headers
226 *
227 * This has to go into the head section of your template.
228 *
229 * @triggers TPL_METAHEADER_OUTPUT
230 * @param  boolean $alt Should feeds and alternative format links be added?
231 * @author Andreas Gohr <andi@splitbrain.org>
232 */
233function tpl_metaheaders($alt=true){
234  global $ID;
235  global $REV;
236  global $INFO;
237  global $ACT;
238  global $QUERY;
239  global $lang;
240  global $conf;
241  $it=2;
242
243  // prepare the head array
244  $head = array();
245
246
247  // the usual stuff
248  $head['meta'][] = array( 'name'=>'generator', 'content'=>'DokuWiki '.getVersion() );
249  $head['link'][] = array( 'rel'=>'search', 'type'=>'application/opensearchdescription+xml',
250                           'href'=>DOKU_BASE.'lib/exe/opensearch.php', 'title'=>$conf['title'] );
251  $head['link'][] = array( 'rel'=>'start', 'href'=>DOKU_BASE );
252  $head['link'][] = array( 'rel'=>'contents', 'href'=> wl($ID,'do=index',false,'&'),
253                           'title'=>$lang['btn_index'] );
254
255  if($alt){
256    $head['link'][] = array( 'rel'=>'alternate', 'type'=>'application/rss+xml',
257                             'title'=>'Recent Changes', 'href'=>DOKU_BASE.'feed.php');
258    $head['link'][] = array( 'rel'=>'alternate', 'type'=>'application/rss+xml',
259                             'title'=>'Current Namespace',
260                             'href'=>DOKU_BASE.'feed.php?mode=list&ns='.$INFO['namespace']);
261    if($ACT == 'search'){
262      $head['link'][] = array( 'rel'=>'alternate', 'type'=>'application/rss+xml',
263                               'title'=>'Search Result',
264                               'href'=>DOKU_BASE.'feed.php?mode=search&q='.$QUERY);
265    }
266
267    if(actionOK('export_xhtml')){
268      $head['link'][] = array( 'rel'=>'alternate', 'type'=>'text/html', 'title'=>'Plain HTML',
269                               'href'=>exportlink($ID, 'xhtml', '', false, '&'));
270    }
271
272    if(actionOK('export_raw')){
273      $head['link'][] = array( 'rel'=>'alternate', 'type'=>'text/plain', 'title'=>'Wiki Markup',
274                               'href'=>exportlink($ID, 'raw', '', false, '&'));
275    }
276  }
277
278  // setup robot tags apropriate for different modes
279  if( ($ACT=='show' || $ACT=='export_xhtml') && !$REV){
280    if($INFO['exists']){
281      //delay indexing:
282      if((time() - $INFO['lastmod']) >= $conf['indexdelay']){
283        $head['meta'][] = array( 'name'=>'robots', 'content'=>'index,follow');
284      }else{
285        $head['meta'][] = array( 'name'=>'robots', 'content'=>'noindex,nofollow');
286      }
287    }else{
288      $head['meta'][] = array( 'name'=>'robots', 'content'=>'noindex,follow');
289    }
290  }elseif(defined('DOKU_MEDIADETAIL')){
291    $head['meta'][] = array( 'name'=>'robots', 'content'=>'index,follow');
292  }else{
293    $head['meta'][] = array( 'name'=>'robots', 'content'=>'noindex,nofollow');
294  }
295
296  // set metadata
297  if($ACT == 'show' || $ACT=='export_xhtml'){
298    // date of modification
299    if($REV){
300      $head['meta'][] = array( 'name'=>'date', 'content'=>date('Y-m-d\TH:i:sO',$REV));
301    }else{
302      $head['meta'][] = array( 'name'=>'date', 'content'=>date('Y-m-d\TH:i:sO',$INFO['lastmod']));
303    }
304
305    // keywords (explicit or implicit)
306    if(!empty($INFO['meta']['subject'])){
307      $head['meta'][] = array( 'name'=>'keywords', 'content'=>join(',',$INFO['meta']['subject']));
308    }else{
309      $head['meta'][] = array( 'name'=>'keywords', 'content'=>str_replace(':',',',$ID));
310    }
311  }
312
313  // load stylesheets
314  $head['link'][] = array('rel'=>'stylesheet', 'media'=>'all', 'type'=>'text/css',
315                          'href'=>DOKU_BASE.'lib/exe/css.php?s=all');
316  $head['link'][] = array('rel'=>'stylesheet', 'media'=>'screen', 'type'=>'text/css',
317                          'href'=>DOKU_BASE.'lib/exe/css.php');
318  $head['link'][] = array('rel'=>'stylesheet', 'media'=>'print', 'type'=>'text/css',
319                          'href'=>DOKU_BASE.'lib/exe/css.php?s=print');
320
321  // load javascript
322  $js_edit  = ($ACT=='edit' || $ACT=='preview' || $ACT=='recover' || $ACT=='wordblock' ) ? 1 : 0;
323  $js_write = ($INFO['writable']) ? 1 : 0;
324  if(defined('DOKU_MEDIAMANAGER')){
325    $js_edit  = 1;
326    $js_write = 0;
327  }
328  if(($js_edit && $js_write) || defined('DOKU_MEDIAMANAGER')){
329    $script = "NS='".$INFO['namespace']."';";
330    if($conf['useacl'] && $_SERVER['REMOTE_USER']){
331      require_once(DOKU_INC.'inc/toolbar.php');
332      $script .= "SIG='".toolbar_signature()."';";
333    }
334    $head['script'][] = array( 'type'=>'text/javascript', 'charset'=>'utf-8',
335                               '_data'=> $script);
336  }
337  $head['script'][] = array( 'type'=>'text/javascript', 'charset'=>'utf-8', '_data'=>'',
338                             'src'=>DOKU_BASE.'lib/exe/js.php?edit='.$js_edit.'&write='.$js_write);
339
340  // trigger event here
341  trigger_event('TPL_METAHEADER_OUTPUT',$head,'_tpl_metaheaders_action',true);
342  return true;
343}
344
345/**
346 * prints the array build by tpl_metaheaders
347 *
348 * $data is an array of different header tags. Each tag can have multiple
349 * instances. Attributes are given as key value pairs. Values will be HTML
350 * encoded automatically so they should be provided as is in the $data array.
351 *
352 * For tags having a body attribute specify the the body data in the special
353 * attribute '_data'. This field will NOT BE ESCAPED automatically.
354 *
355 * @author Andreas Gohr <andi@splitbrain.org>
356 */
357function _tpl_metaheaders_action($data){
358  foreach($data as $tag => $inst){
359    foreach($inst as $attr){
360      echo '<',$tag,' ',buildAttributes($attr);
361      if(isset($attr['_data'])){
362          if($tag == 'script' && $attr['_data'])
363            $attr['_data'] = "<!--//--><![CDATA[//><!--\n".
364                             $attr['_data'].
365                             "\n//--><!]]>";
366
367          echo '>',$attr['_data'],'</',$tag,'>';
368      }else{
369        echo '/>';
370      }
371      echo "\n";
372    }
373  }
374}
375
376/**
377 * Print a link
378 *
379 * Just builds a link.
380 *
381 * @author Andreas Gohr <andi@splitbrain.org>
382 */
383function tpl_link($url,$name,$more=''){
384  print '<a href="'.$url.'" ';
385  if ($more) print ' '.$more;
386  print ">$name</a>";
387  return true;
388}
389
390/**
391 * Prints a link to a WikiPage
392 *
393 * Wrapper around html_wikilink
394 *
395 * @author Andreas Gohr <andi@splitbrain.org>
396 */
397function tpl_pagelink($id,$name=NULL){
398  print html_wikilink($id,$name);
399  return true;
400}
401
402/**
403 * get the parent page
404 *
405 * Tries to find out which page is parent.
406 * returns false if none is available
407 *
408 * @author Andreas Gohr <andi@splitbrain.org>
409 */
410function tpl_getparent($id){
411  global $conf;
412  $parent = getNS($id).':';
413  resolve_pageid('',$parent,$exists);
414  if($parent == $id) {
415    $pos = strrpos (getNS($id),':');
416    $parent = substr($parent,0,$pos).':';
417    resolve_pageid('',$parent,$exists);
418    if($parent == $id) return false;
419  }
420  return $parent;
421}
422
423/**
424 * Print one of the buttons
425 *
426 * Available Buttons are
427 *
428 *  edit        - edit/create/show/draft button
429 *  history     - old revisions
430 *  recent      - recent changes
431 *  login       - login/logout button - if ACL enabled
432 *  profile     - user profile button (if logged in)
433 *  index       - The index
434 *  admin       - admin page - if enough rights
435 *  top         - a back to top button
436 *  back        - a back to parent button - if available
437 *  backlink    - links to the list of backlinks
438 *  subscription- subscribe/unsubscribe button
439 *
440 * @author Andreas Gohr <andi@splitbrain.org>
441 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
442 */
443function tpl_button($type){
444  global $ACT;
445  global $ID;
446  global $REV;
447  global $NS;
448  global $INFO;
449  global $conf;
450  global $auth;
451
452  // check disabled actions and fix the badly named ones
453  $ctype = $type;
454  if($type == 'history') $ctype='revisions';
455  if(!actionOK($ctype)) return false;
456
457  switch($type){
458    case 'edit':
459      #most complicated type - we need to decide on current action
460      if($ACT == 'show' || $ACT == 'search'){
461        if($INFO['writable']){
462          if(!empty($INFO['draft'])){
463            echo html_btn('draft',$ID,'e',array('do' => 'draft'),'post');
464          }else{
465            if($INFO['exists']){
466              echo html_btn('edit',$ID,'e',array('do' => 'edit','rev' => $REV),'post');
467            }else{
468              echo html_btn('create',$ID,'e',array('do' => 'edit','rev' => $REV),'post');
469            }
470          }
471        }else{
472          if(!actionOK('source')) return false; //pseudo action
473          echo html_btn('source',$ID,'v',array('do' => 'edit','rev' => $REV),'post');
474        }
475      }else{
476          echo html_btn('show',$ID,'v',array('do' => 'show'));
477      }
478      return true;
479    case 'history':
480      print html_btn('revs',$ID,'o',array('do' => 'revisions'));
481      return true;
482    case 'recent':
483      print html_btn('recent','','r',array('do' => 'recent'));
484      return true;
485    case 'index':
486      print html_btn('index',$ID,'x',array('do' => 'index'));
487      return true;
488    case 'back':
489      if ($parent = tpl_getparent($ID)) {
490        print html_btn('back',$parent,'b',array('do' => 'show'));
491        return true;
492      }
493      return false;
494    case 'top':
495      print html_topbtn();
496      return true;
497    case 'login':
498      if($conf['useacl'] && $auth){
499        if($_SERVER['REMOTE_USER']){
500          print html_btn('logout',$ID,'',array('do' => 'logout', 'sectok' => getSecurityToken()));
501        }else{
502          print html_btn('login',$ID,'',array('do' => 'login', 'sectok' => getSecurityToken()));
503        }
504        return true;
505      }
506      return false;
507    case 'admin':
508      if($INFO['ismanager']){
509        print html_btn('admin',$ID,'',array('do' => 'admin'));
510        return true;
511      }
512      return false;
513    case 'subscription':
514      if($conf['useacl'] && $auth && $ACT == 'show' && $conf['subscribers'] == 1){
515        if($_SERVER['REMOTE_USER']){
516          if($INFO['subscribed']){
517            print html_btn('unsubscribe',$ID,'',array('do' => 'unsubscribe',));
518          } else {
519            print html_btn('subscribe',$ID,'',array('do' => 'subscribe',));
520          }
521          return true;
522        }
523      }
524      return false;
525    case 'backlink':
526      print html_btn('backlink',$ID,'',array('do' => 'backlink'));
527      return true;
528    case 'profile':
529      if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $auth &&
530         $auth->canDo('Profile') && ($ACT!='profile')){
531        print html_btn('profile',$ID,'',array('do' => 'profile'));
532        return true;
533      }
534      return false;
535    default:
536      print '[unknown button type]';
537      return true;
538  }
539}
540
541/**
542 * Like the action buttons but links
543 *
544 * Available links are
545 *
546 *  edit    - edit/create/show link
547 *  history - old revisions
548 *  recent  - recent changes
549 *  login   - login/logout link - if ACL enabled
550 *  profile - user profile link (if logged in)
551 *  index   - The index
552 *  admin   - admin page - if enough rights
553 *  top     - a back to top link
554 *  back    - a back to parent link - if available
555 *  backlink - links to the list of backlinks
556 *  subscribe/subscription - subscribe/unsubscribe link
557 *
558 * @author Andreas Gohr <andi@splitbrain.org>
559 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
560 * @see    tpl_button
561 */
562function tpl_actionlink($type,$pre='',$suf='',$inner=''){
563  global $ID;
564  global $INFO;
565  global $REV;
566  global $ACT;
567  global $conf;
568  global $lang;
569  global $auth;
570
571  // check disabled actions and fix the badly named ones
572  $ctype = $type;
573  if($type == 'history') $ctype='revisions';
574  if(!actionOK($ctype)) return false;
575
576  switch($type){
577    case 'edit':
578      #most complicated type - we need to decide on current action
579      if($ACT == 'show' || $ACT == 'search'){
580        if($INFO['writable']){
581          if(!empty($INFO['draft'])) {
582            tpl_link(wl($ID,'do=draft'),
583                       $pre.(($inner)?$inner:$lang['btn_draft']).$suf,
584                       'class="action edit" accesskey="e" rel="nofollow"');
585          } else {
586            if($INFO['exists']){
587              tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
588                       $pre.(($inner)?$inner:$lang['btn_edit']).$suf,
589                       'class="action edit" accesskey="e" rel="nofollow"');
590            }else{
591              tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
592                       $pre.(($inner)?$inner:$lang['btn_create']).$suf,
593                       'class="action create" accesskey="e" rel="nofollow"');
594            }
595          }
596        }else{
597          if(!actionOK('source')) return false; //pseudo action
598          tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
599                   $pre.(($inner)?$inner:$lang['btn_source']).$suf,
600                   'class="action source" accesskey="v" rel="nofollow"');
601        }
602      }else{
603          tpl_link(wl($ID,'do=show'),
604                   $pre.(($inner)?$inner:$lang['btn_show']).$suf,
605                   'class="action show" accesskey="v" rel="nofollow"');
606      }
607      return true;
608    case 'history':
609      tpl_link(wl($ID,'do=revisions'),
610               $pre.(($inner)?$inner:$lang['btn_revs']).$suf,
611               'class="action revisions" accesskey="o" rel="nofollow"');
612      return true;
613    case 'recent':
614      tpl_link(wl('','do=recent'),
615               $pre.(($inner)?$inner:$lang['btn_recent']).$suf,
616              'class="action recent" accesskey="r" rel="nofollow"');
617      return true;
618    case 'index':
619      tpl_link(wl($ID,'do=index'),
620               $pre.(($inner)?$inner:$lang['btn_index']).$suf,
621              'class="action index" accesskey="x" rel="nofollow"');
622      return true;
623    case 'top':
624      print '<a href="#dokuwiki__top" class="action top" accesskey="x">'.
625            $pre.(($inner)?$inner:$lang['btn_top']).$suf.'</a>';
626      return true;
627    case 'back':
628      if ($parent = tpl_getparent($ID)) {
629        tpl_link(wl($parent,'do=show'),
630        $pre.(($inner)?$inner:$lang['btn_back']).$suf,
631        'class="action back" accesskey="b" rel="nofollow"');
632        return true;
633      }
634      return false;
635    case 'login':
636      if($conf['useacl'] && $auth){
637        if($_SERVER['REMOTE_USER']){
638          tpl_link(wl($ID,'do=logout&amp;sectok='.getSecurityToken()),
639                   $pre.(($inner)?$inner:$lang['btn_logout']).$suf,
640                   'class="action logout" rel="nofollow"');
641        }else{
642          tpl_link(wl($ID,'do=login&amp;sectok='.getSecurityToken()),
643                   $pre.(($inner)?$inner:$lang['btn_login']).$suf,
644                   'class="action login" rel="nofollow"');
645        }
646        return true;
647      }
648      return false;
649    case 'admin':
650      if($INFO['ismanager']){
651        tpl_link(wl($ID,'do=admin'),
652                 $pre.(($inner)?$inner:$lang['btn_admin']).$suf,
653                 'class="action admin" rel="nofollow"');
654        return true;
655      }
656      return false;
657   case 'subscribe':
658   case 'subscription':
659      if($conf['useacl'] && $auth && $ACT == 'show' && $conf['subscribers'] == 1){
660        if($_SERVER['REMOTE_USER']){
661          if($INFO['subscribed']) {
662            tpl_link(wl($ID,'do=unsubscribe'),
663                     $pre.(($inner)?$inner:$lang['btn_unsubscribe']).$suf,
664                     'class="action unsubscribe" rel="nofollow"');
665          } else {
666            tpl_link(wl($ID,'do=subscribe'),
667                     $pre.(($inner)?$inner:$lang['btn_subscribe']).$suf,
668                     'class="action subscribe" rel="nofollow"');
669          }
670          return true;
671        }
672      }
673      return false;
674    case 'backlink':
675      tpl_link(wl($ID,'do=backlink'),
676               $pre.(($inner)?$inner:$lang['btn_backlink']).$suf,
677               'class="action backlink" rel="nofollow"');
678      return true;
679    case 'profile':
680      if($conf['useacl'] && $auth && $_SERVER['REMOTE_USER'] &&
681         $auth->canDo('Profile') && ($ACT!='profile')){
682        tpl_link(wl($ID,'do=profile'),
683                 $pre.(($inner)?$inner:$lang['btn_profile']).$suf,
684                 'class="action profile" rel="nofollow"');
685        return true;
686      }
687      return false;
688    default:
689      print '[unknown link type]';
690      return true;
691  }
692}
693
694/**
695 * Print the search form
696 *
697 * If the first parameter is given a div with the ID 'qsearch_out' will
698 * be added which instructs the ajax pagequicksearch to kick in and place
699 * its output into this div. The second parameter controls the propritary
700 * attribute autocomplete. If set to false this attribute will be set with an
701 * value of "off" to instruct the browser to disable it's own built in
702 * autocompletion feature (MSIE and Firefox)
703 *
704 * @author Andreas Gohr <andi@splitbrain.org>
705 */
706function tpl_searchform($ajax=true,$autocomplete=true){
707  global $lang;
708  global $ACT;
709
710  // don't print the search form if search action has been disabled
711  if (!actionOk('search')) return false;
712
713  print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">';
714  print '<input type="hidden" name="do" value="search" />';
715  print '<input type="text" ';
716  if($ACT == 'search') print 'value="'.htmlspecialchars($_REQUEST['id']).'" ';
717  if(!$autocomplete) print 'autocomplete="off" ';
718  print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />';
719  print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />';
720  if($ajax) print '<div id="qsearch__out" class="ajax_qsearch JSpopup"></div>';
721  print '</div></form>';
722  return true;
723}
724
725/**
726 * Print the breadcrumbs trace
727 *
728 * @author Andreas Gohr <andi@splitbrain.org>
729 */
730function tpl_breadcrumbs($sep='&raquo;'){
731  global $lang;
732  global $conf;
733
734  //check if enabled
735  if(!$conf['breadcrumbs']) return false;
736
737  $crumbs = breadcrumbs(); //setup crumb trace
738
739  //reverse crumborder in right-to-left mode, add RLM character to fix heb/eng display mixups
740  if($lang['direction'] == 'rtl') {
741    $crumbs = array_reverse($crumbs,true);
742    $crumbs_sep = ' &#8207;<span class="bcsep">'.$sep.'</span>&#8207; ';
743  } else {
744    $crumbs_sep = ' <span class="bcsep">'.$sep.'</span> ';
745  }
746
747  //render crumbs, highlight the last one
748  print '<span class="bchead">'.$lang['breadcrumb'].':</span>';
749  $last = count($crumbs);
750  $i = 0;
751  foreach ($crumbs as $id => $name){
752    $i++;
753    echo $crumbs_sep;
754    if ($i == $last) print '<span class="curid">';
755    tpl_link(wl($id),hsc($name),'class="breadcrumbs" title="'.$id.'"');
756    if ($i == $last) print '</span>';
757  }
758  return true;
759}
760
761/**
762 * Hierarchical breadcrumbs
763 *
764 * This code was suggested as replacement for the usual breadcrumbs.
765 * It only makes sense with a deep site structure.
766 *
767 * @author Andreas Gohr <andi@splitbrain.org>
768 * @author Nigel McNie <oracle.shinoda@gmail.com>
769 * @author Sean Coates <sean@caedmon.net>
770 * @link   http://wiki.splitbrain.org/wiki:tipsandtricks:hierarchicalbreadcrumbs
771 * @todo   May behave strangely in RTL languages
772 */
773function tpl_youarehere($sep=' &raquo; '){
774  global $conf;
775  global $ID;
776  global $lang;
777
778  // check if enabled
779  if(!$conf['youarehere']) return false;
780
781  $parts = explode(':', $ID);
782  $count = count($parts);
783
784  echo '<span class="bchead">'.$lang['youarehere'].': </span>';
785
786  // always print the startpage
787  $title = p_get_first_heading($conf['start']);
788  if(!$title) $title = $conf['start'];
789  tpl_link(wl($conf['start']),hsc($title),'title="'.$conf['start'].'"');
790
791  // print intermediate namespace links
792  $part = '';
793  for($i=0; $i<$count - 1; $i++){
794    $part .= $parts[$i].':';
795    $page = $part;
796    resolve_pageid('',$page,$exists);
797    if ($page == $conf['start']) continue; // Skip startpage
798
799    // output
800    echo $sep;
801    if($exists){
802      $title = p_get_first_heading($page);
803      if(!$title) $title = $parts[$i];
804      tpl_link(wl($page),hsc($title),'title="'.$page.'"');
805    }else{
806      tpl_link(wl($page),$parts[$i],'title="'.$page.'" class="wikilink2"');
807    }
808  }
809
810  // print current page, skipping start page, skipping for namespace index
811  if(isset($page) && $page==$part.$parts[$i]) return;
812  $page = $part.$parts[$i];
813  if($page == $conf['start']) return;
814  echo $sep;
815  if(page_exists($page)){
816    $title = p_get_first_heading($page);
817    if(!$title) $title = $parts[$i];
818    tpl_link(wl($page),hsc($title),'title="'.$page.'"');
819  }else{
820    tpl_link(wl($page),$parts[$i],'title="'.$page.'" class="wikilink2"');
821  }
822  return true;
823}
824
825/**
826 * Print info if the user is logged in
827 * and show full name in that case
828 *
829 * Could be enhanced with a profile link in future?
830 *
831 * @author Andreas Gohr <andi@splitbrain.org>
832 */
833function tpl_userinfo(){
834  global $lang;
835  global $INFO;
836  if($_SERVER['REMOTE_USER']){
837    print $lang['loggedinas'].': '.$INFO['userinfo']['name'];
838    return true;
839  }
840  return false;
841}
842
843/**
844 * Print some info about the current page
845 *
846 * @author Andreas Gohr <andi@splitbrain.org>
847 */
848function tpl_pageinfo(){
849  global $conf;
850  global $lang;
851  global $INFO;
852  global $REV;
853  global $ID;
854
855  // return if we are not allowed to view the page
856  if (!auth_quickaclcheck($ID)) { return; }
857
858  // prepare date and path
859  $fn = $INFO['filepath'];
860  if(!$conf['fullpath']){
861    if($REV){
862      $fn = str_replace(fullpath($conf['olddir']).DIRECTORY_SEPARATOR,'',$fn);
863    }else{
864      $fn = str_replace(fullpath($conf['datadir']).DIRECTORY_SEPARATOR,'',$fn);
865    }
866  }
867  $fn = utf8_decodeFN($fn);
868  $date = date($conf['dformat'],$INFO['lastmod']);
869
870  // print it
871  if($INFO['exists']){
872    print $fn;
873    print ' &middot; ';
874    print $lang['lastmod'];
875    print ': ';
876    print $date;
877    if($INFO['editor']){
878      print ' '.$lang['by'].' ';
879      print $INFO['editor'];
880    }else{
881      print ' ('.$lang['external_edit'].')';
882    }
883    if($INFO['locked']){
884      print ' &middot; ';
885      print $lang['lockedby'];
886      print ': ';
887      print $INFO['locked'];
888    }
889    return true;
890  }
891  return false;
892}
893
894/**
895 * Prints or returns the name of the given page (current one if none given).
896 *
897 * If useheading is enabled this will use the first headline else
898 * the given ID is used.
899 *
900 * @author Andreas Gohr <andi@splitbrain.org>
901 */
902function tpl_pagetitle($id=null, $ret=false){
903  global $conf;
904  if(is_null($id)){
905    global $ID;
906    $id = $ID;
907  }
908
909  $name = $id;
910  if ($conf['useheading']) {
911    $title = p_get_first_heading($id);
912    if ($title) $name = $title;
913  }
914
915  if ($ret) {
916      return hsc($name);
917  } else {
918      print hsc($name);
919      return true;
920  }
921}
922
923/**
924 * Returns the requested EXIF/IPTC tag from the current image
925 *
926 * If $tags is an array all given tags are tried until a
927 * value is found. If no value is found $alt is returned.
928 *
929 * Which texts are known is defined in the functions _exifTagNames
930 * and _iptcTagNames() in inc/jpeg.php (You need to prepend IPTC
931 * to the names of the latter one)
932 *
933 * Only allowed in: detail.php
934 *
935 * @author Andreas Gohr <andi@splitbrain.org>
936 */
937function tpl_img_getTag($tags,$alt='',$src=null){
938  // Init Exif Reader
939  global $SRC;
940
941  if(is_null($src)) $src = $SRC;
942
943  static $meta = null;
944  if(is_null($meta)) $meta = new JpegMeta($src);
945  if($meta === false) return $alt;
946  $info = $meta->getField($tags);
947  if($info == false) return $alt;
948  return $info;
949}
950
951/**
952 * Prints the image with a link to the full sized version
953 *
954 * Only allowed in: detail.php
955 */
956function tpl_img($maxwidth=0,$maxheight=0){
957  global $IMG;
958  $w = tpl_img_getTag('File.Width');
959  $h = tpl_img_getTag('File.Height');
960
961  //resize to given max values
962  $ratio = 1;
963  if($w >= $h){
964    if($maxwidth && $w >= $maxwidth){
965      $ratio = $maxwidth/$w;
966    }elseif($maxheight && $h > $maxheight){
967      $ratio = $maxheight/$h;
968    }
969  }else{
970    if($maxheight && $h >= $maxheight){
971      $ratio = $maxheight/$h;
972    }elseif($maxwidth && $w > $maxwidth){
973      $ratio = $maxwidth/$w;
974    }
975  }
976  if($ratio){
977    $w = floor($ratio*$w);
978    $h = floor($ratio*$h);
979  }
980
981  //prepare URLs
982  $url=ml($IMG,array('cache'=>$_REQUEST['cache']));
983  $src=ml($IMG,array('cache'=>$_REQUEST['cache'],'w'=>$w,'h'=>$h));
984
985  //prepare attributes
986  $alt=tpl_img_getTag('Simple.Title');
987  $p = array();
988  if($w) $p['width']  = $w;
989  if($h) $p['height'] = $h;
990         $p['class']  = 'img_detail';
991  if($alt){
992    $p['alt']   = $alt;
993    $p['title'] = $alt;
994  }else{
995    $p['alt'] = '';
996  }
997  $p = buildAttributes($p);
998
999  print '<a href="'.$url.'">';
1000  print '<img src="'.$src.'" '.$p.'/>';
1001  print '</a>';
1002  return true;
1003}
1004
1005/**
1006 * This function inserts a 1x1 pixel gif which in reality
1007 * is the inexer function.
1008 *
1009 * Should be called somewhere at the very end of the main.php
1010 * template
1011 */
1012function tpl_indexerWebBug(){
1013  global $ID;
1014  global $INFO;
1015  if(!$INFO['exists']) return false;
1016
1017  if(isHiddenPage($ID)) return false; //no need to index hidden pages
1018
1019  $p = array();
1020  $p['src']    = DOKU_BASE.'lib/exe/indexer.php?id='.rawurlencode($ID).
1021                 '&'.time();
1022  $p['width']  = 1;
1023  $p['height'] = 1;
1024  $p['alt']    = '';
1025  $att = buildAttributes($p);
1026  print "<img $att />";
1027  return true;
1028}
1029
1030// configuration methods
1031/**
1032 * tpl_getConf($id)
1033 *
1034 * use this function to access template configuration variables
1035 */
1036function tpl_getConf($id){
1037  global $conf;
1038  global $tpl_configloaded;
1039
1040  $tpl = $conf['template'];
1041
1042  if (!$tpl_configloaded){
1043    $tconf = tpl_loadConfig();
1044    if ($tconf !== false){
1045      foreach ($tconf as $key => $value){
1046        if (isset($conf['tpl'][$tpl][$key])) continue;
1047        $conf['tpl'][$tpl][$key] = $value;
1048      }
1049      $tpl_configloaded = true;
1050    }
1051  }
1052
1053  return $conf['tpl'][$tpl][$id];
1054}
1055
1056/**
1057 * tpl_loadConfig()
1058 * reads all template configuration variables
1059 * this function is automatically called by tpl_getConf()
1060 */
1061function tpl_loadConfig(){
1062
1063  $file = DOKU_TPLINC.'/conf/default.php';
1064  $conf = array();
1065
1066  if (!@file_exists($file)) return false;
1067
1068  // load default config file
1069  include($file);
1070
1071  return $conf;
1072}
1073
1074/**
1075 * prints the "main content" in the mediamanger popup
1076 *
1077 * Depending on the user's actions this may be a list of
1078 * files in a namespace, the meta editing dialog or
1079 * a message of referencing pages
1080 *
1081 * Only allowed in mediamanager.php
1082 *
1083 * @author Andreas Gohr <andi@splitbrain.org>
1084 */
1085function tpl_mediaContent(){
1086  global $IMG;
1087  global $AUTH;
1088  global $INUSE;
1089  global $NS;
1090  global $JUMPTO;
1091
1092  ptln('<div id="media__content">');
1093  if($_REQUEST['edit']){
1094    media_metaform($IMG,$AUTH);
1095  }elseif(is_array($INUSE)){
1096    media_filesinuse($INUSE,$IMG);
1097  }else{
1098    media_filelist($NS,$AUTH,$JUMPTO);
1099  }
1100  ptln('</div>');
1101}
1102
1103/**
1104 * prints the namespace tree in the mediamanger popup
1105 *
1106 * Only allowed in mediamanager.php
1107 *
1108 * @author Andreas Gohr <andi@splitbrain.org>
1109 */
1110function tpl_mediaTree(){
1111  global $NS;
1112
1113  ptln('<div id="media__tree">');
1114  media_nstree($NS);
1115  ptln('</div>');
1116}
1117
1118//Setup VIM: ex: et ts=4 enc=utf-8 :
1119