xref: /dokuwiki/inc/template.php (revision b65e3b9fe4b5cfcf50be37ec6f33146579ee4e3c)
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 && $INFO['exists']){
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          if($INFO['subscribedns']){
522            print html_btn('unsubscribens',$ID,'',array('do' => 'unsubscribens',));
523          } else {
524            print html_btn('subscribens',$ID,'',array('do' => 'subscribens',));
525          }
526          return true;
527        }
528      }
529      return false;
530    case 'backlink':
531      print html_btn('backlink',$ID,'',array('do' => 'backlink'));
532      return true;
533    case 'profile':
534      if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $auth &&
535         $auth->canDo('Profile') && ($ACT!='profile')){
536        print html_btn('profile',$ID,'',array('do' => 'profile'));
537        return true;
538      }
539      return false;
540    default:
541      print '[unknown button type]';
542      return true;
543  }
544}
545
546/**
547 * Like the action buttons but links
548 *
549 * Available links are
550 *
551 *  edit    - edit/create/show link
552 *  history - old revisions
553 *  recent  - recent changes
554 *  login   - login/logout link - if ACL enabled
555 *  profile - user profile link (if logged in)
556 *  index   - The index
557 *  admin   - admin page - if enough rights
558 *  top     - a back to top link
559 *  back    - a back to parent link - if available
560 *  backlink - links to the list of backlinks
561 *  subscribe/subscription - subscribe/unsubscribe link
562 *
563 * @author Andreas Gohr <andi@splitbrain.org>
564 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
565 * @see    tpl_button
566 */
567function tpl_actionlink($type,$pre='',$suf='',$inner=''){
568  global $ID;
569  global $INFO;
570  global $REV;
571  global $ACT;
572  global $conf;
573  global $lang;
574  global $auth;
575
576  // check disabled actions and fix the badly named ones
577  $ctype = $type;
578  if($type == 'history') $ctype='revisions';
579  if(!actionOK($ctype)) return false;
580
581  switch($type){
582    case 'edit':
583      #most complicated type - we need to decide on current action
584      if($ACT == 'show' || $ACT == 'search'){
585        if($INFO['writable']){
586          if(!empty($INFO['draft'])) {
587            tpl_link(wl($ID,'do=draft'),
588                       $pre.(($inner)?$inner:$lang['btn_draft']).$suf,
589                       'class="action edit" accesskey="e" rel="nofollow"');
590          } else {
591            if($INFO['exists']){
592              tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
593                       $pre.(($inner)?$inner:$lang['btn_edit']).$suf,
594                       'class="action edit" accesskey="e" rel="nofollow"');
595            }else{
596              tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
597                       $pre.(($inner)?$inner:$lang['btn_create']).$suf,
598                       'class="action create" accesskey="e" rel="nofollow"');
599            }
600          }
601        }else{
602          if(!actionOK('source')) return false; //pseudo action
603          tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
604                   $pre.(($inner)?$inner:$lang['btn_source']).$suf,
605                   'class="action source" accesskey="v" rel="nofollow"');
606        }
607      }else{
608          tpl_link(wl($ID,'do=show'),
609                   $pre.(($inner)?$inner:$lang['btn_show']).$suf,
610                   'class="action show" accesskey="v" rel="nofollow"');
611      }
612      return true;
613    case 'history':
614      tpl_link(wl($ID,'do=revisions'),
615               $pre.(($inner)?$inner:$lang['btn_revs']).$suf,
616               'class="action revisions" accesskey="o" rel="nofollow"');
617      return true;
618    case 'recent':
619      tpl_link(wl('','do=recent'),
620               $pre.(($inner)?$inner:$lang['btn_recent']).$suf,
621              'class="action recent" accesskey="r" rel="nofollow"');
622      return true;
623    case 'index':
624      tpl_link(wl($ID,'do=index'),
625               $pre.(($inner)?$inner:$lang['btn_index']).$suf,
626              'class="action index" accesskey="x" rel="nofollow"');
627      return true;
628    case 'top':
629      print '<a href="#dokuwiki__top" class="action top" accesskey="x">'.
630            $pre.(($inner)?$inner:$lang['btn_top']).$suf.'</a>';
631      return true;
632    case 'back':
633      if ($parent = tpl_getparent($ID)) {
634        tpl_link(wl($parent,'do=show'),
635        $pre.(($inner)?$inner:$lang['btn_back']).$suf,
636        'class="action back" accesskey="b" rel="nofollow"');
637        return true;
638      }
639      return false;
640    case 'login':
641      if($conf['useacl'] && $auth){
642        if($_SERVER['REMOTE_USER']){
643          tpl_link(wl($ID,'do=logout&amp;sectok='.getSecurityToken()),
644                   $pre.(($inner)?$inner:$lang['btn_logout']).$suf,
645                   'class="action logout" rel="nofollow"');
646        }else{
647          tpl_link(wl($ID,'do=login&amp;sectok='.getSecurityToken()),
648                   $pre.(($inner)?$inner:$lang['btn_login']).$suf,
649                   'class="action login" rel="nofollow"');
650        }
651        return true;
652      }
653      return false;
654    case 'admin':
655      if($INFO['ismanager']){
656        tpl_link(wl($ID,'do=admin'),
657                 $pre.(($inner)?$inner:$lang['btn_admin']).$suf,
658                 'class="action admin" rel="nofollow"');
659        return true;
660      }
661      return false;
662   case 'subscribe':
663   case 'subscription':
664      if($conf['useacl'] && $auth && $ACT == 'show' && $conf['subscribers'] == 1){
665        if($_SERVER['REMOTE_USER']){
666          if($INFO['subscribed']) {
667            tpl_link(wl($ID,'do=unsubscribe'),
668                     $pre.(($inner)?$inner:$lang['btn_unsubscribe']).$suf,
669                     'class="action unsubscribe" rel="nofollow"');
670          } else {
671            tpl_link(wl($ID,'do=subscribe'),
672                     $pre.(($inner)?$inner:$lang['btn_subscribe']).$suf,
673                     'class="action subscribe" rel="nofollow"');
674          }
675          return true;
676        }
677      }
678      return false;
679    case 'backlink':
680      tpl_link(wl($ID,'do=backlink'),
681               $pre.(($inner)?$inner:$lang['btn_backlink']).$suf,
682               'class="action backlink" rel="nofollow"');
683      return true;
684    case 'profile':
685      if($conf['useacl'] && $auth && $_SERVER['REMOTE_USER'] &&
686         $auth->canDo('Profile') && ($ACT!='profile')){
687        tpl_link(wl($ID,'do=profile'),
688                 $pre.(($inner)?$inner:$lang['btn_profile']).$suf,
689                 'class="action profile" rel="nofollow"');
690        return true;
691      }
692      return false;
693    default:
694      print '[unknown link type]';
695      return true;
696  }
697}
698
699/**
700 * Print the search form
701 *
702 * If the first parameter is given a div with the ID 'qsearch_out' will
703 * be added which instructs the ajax pagequicksearch to kick in and place
704 * its output into this div. The second parameter controls the propritary
705 * attribute autocomplete. If set to false this attribute will be set with an
706 * value of "off" to instruct the browser to disable it's own built in
707 * autocompletion feature (MSIE and Firefox)
708 *
709 * @author Andreas Gohr <andi@splitbrain.org>
710 */
711function tpl_searchform($ajax=true,$autocomplete=true){
712  global $lang;
713  global $ACT;
714
715  // don't print the search form if search action has been disabled
716  if (!actionOk('search')) return false;
717
718  print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">';
719  print '<input type="hidden" name="do" value="search" />';
720  print '<input type="text" ';
721  if($ACT == 'search') print 'value="'.htmlspecialchars($_REQUEST['id']).'" ';
722  if(!$autocomplete) print 'autocomplete="off" ';
723  print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[ALT+F]" />';
724  print '<input type="submit" value="'.$lang['btn_search'].'" class="button" title="'.$lang['btn_search'].'" />';
725  if($ajax) print '<div id="qsearch__out" class="ajax_qsearch JSpopup"></div>';
726  print '</div></form>';
727  return true;
728}
729
730/**
731 * Print the breadcrumbs trace
732 *
733 * @author Andreas Gohr <andi@splitbrain.org>
734 */
735function tpl_breadcrumbs($sep='&raquo;'){
736  global $lang;
737  global $conf;
738
739  //check if enabled
740  if(!$conf['breadcrumbs']) return false;
741
742  $crumbs = breadcrumbs(); //setup crumb trace
743
744  //reverse crumborder in right-to-left mode, add RLM character to fix heb/eng display mixups
745  if($lang['direction'] == 'rtl') {
746    $crumbs = array_reverse($crumbs,true);
747    $crumbs_sep = ' &#8207;<span class="bcsep">'.$sep.'</span>&#8207; ';
748  } else {
749    $crumbs_sep = ' <span class="bcsep">'.$sep.'</span> ';
750  }
751
752  //render crumbs, highlight the last one
753  print '<span class="bchead">'.$lang['breadcrumb'].':</span>';
754  $last = count($crumbs);
755  $i = 0;
756  foreach ($crumbs as $id => $name){
757    $i++;
758    echo $crumbs_sep;
759    if ($i == $last) print '<span class="curid">';
760    tpl_link(wl($id),hsc($name),'class="breadcrumbs" title="'.$id.'"');
761    if ($i == $last) print '</span>';
762  }
763  return true;
764}
765
766/**
767 * Hierarchical breadcrumbs
768 *
769 * This code was suggested as replacement for the usual breadcrumbs.
770 * It only makes sense with a deep site structure.
771 *
772 * @author Andreas Gohr <andi@splitbrain.org>
773 * @author Nigel McNie <oracle.shinoda@gmail.com>
774 * @author Sean Coates <sean@caedmon.net>
775 * @link   http://wiki.splitbrain.org/wiki:tipsandtricks:hierarchicalbreadcrumbs
776 * @todo   May behave strangely in RTL languages
777 */
778function tpl_youarehere($sep=' &raquo; '){
779  global $conf;
780  global $ID;
781  global $lang;
782
783  // check if enabled
784  if(!$conf['youarehere']) return false;
785
786  $parts = explode(':', $ID);
787  $count = count($parts);
788
789  echo '<span class="bchead">'.$lang['youarehere'].': </span>';
790
791  // always print the startpage
792  $title = p_get_first_heading($conf['start']);
793  if(!$title) $title = $conf['start'];
794  tpl_link(wl($conf['start']),hsc($title),'title="'.$conf['start'].'"');
795
796  // print intermediate namespace links
797  $part = '';
798  for($i=0; $i<$count - 1; $i++){
799    $part .= $parts[$i].':';
800    $page = $part;
801    resolve_pageid('',$page,$exists);
802    if ($page == $conf['start']) continue; // Skip startpage
803
804    // output
805    echo $sep;
806    if($exists){
807      $title = p_get_first_heading($page);
808      if(!$title) $title = $parts[$i];
809      tpl_link(wl($page),hsc($title),'title="'.$page.'"');
810    }else{
811      tpl_link(wl($page),$parts[$i],'title="'.$page.'" class="wikilink2" rel="nofollow"');
812    }
813  }
814
815  // print current page, skipping start page, skipping for namespace index
816  if(isset($page) && $page==$part.$parts[$i]) return;
817  $page = $part.$parts[$i];
818  if($page == $conf['start']) return;
819  echo $sep;
820  if(page_exists($page)){
821    $title = p_get_first_heading($page);
822    if(!$title) $title = $parts[$i];
823    tpl_link(wl($page),hsc($title),'title="'.$page.'"');
824  }else{
825    tpl_link(wl($page),$parts[$i],'title="'.$page.'" class="wikilink2" rel="nofollow"');
826  }
827  return true;
828}
829
830/**
831 * Print info if the user is logged in
832 * and show full name in that case
833 *
834 * Could be enhanced with a profile link in future?
835 *
836 * @author Andreas Gohr <andi@splitbrain.org>
837 */
838function tpl_userinfo(){
839  global $lang;
840  global $INFO;
841  if($_SERVER['REMOTE_USER']){
842    print $lang['loggedinas'].': '.$INFO['userinfo']['name'];
843    return true;
844  }
845  return false;
846}
847
848/**
849 * Print some info about the current page
850 *
851 * @author Andreas Gohr <andi@splitbrain.org>
852 */
853function tpl_pageinfo(){
854  global $conf;
855  global $lang;
856  global $INFO;
857  global $REV;
858  global $ID;
859
860  // return if we are not allowed to view the page
861  if (!auth_quickaclcheck($ID)) { return; }
862
863  // prepare date and path
864  $fn = $INFO['filepath'];
865  if(!$conf['fullpath']){
866    if($REV){
867      $fn = str_replace(fullpath($conf['olddir']).DIRECTORY_SEPARATOR,'',$fn);
868    }else{
869      $fn = str_replace(fullpath($conf['datadir']).DIRECTORY_SEPARATOR,'',$fn);
870    }
871  }
872  $fn = utf8_decodeFN($fn);
873  $date = strftime($conf['dformat'],$INFO['lastmod']);
874
875  // print it
876  if($INFO['exists']){
877    print $fn;
878    print ' &middot; ';
879    print $lang['lastmod'];
880    print ': ';
881    print $date;
882    if($INFO['editor']){
883      print ' '.$lang['by'].' ';
884      print $INFO['editor'];
885    }else{
886      print ' ('.$lang['external_edit'].')';
887    }
888    if($INFO['locked']){
889      print ' &middot; ';
890      print $lang['lockedby'];
891      print ': ';
892      print $INFO['locked'];
893    }
894    return true;
895  }
896  return false;
897}
898
899/**
900 * Prints or returns the name of the given page (current one if none given).
901 *
902 * If useheading is enabled this will use the first headline else
903 * the given ID is used.
904 *
905 * @author Andreas Gohr <andi@splitbrain.org>
906 */
907function tpl_pagetitle($id=null, $ret=false){
908  global $conf;
909  if(is_null($id)){
910    global $ID;
911    $id = $ID;
912  }
913
914  $name = $id;
915  if ($conf['useheading']) {
916    $title = p_get_first_heading($id);
917    if ($title) $name = $title;
918  }
919
920  if ($ret) {
921      return hsc($name);
922  } else {
923      print hsc($name);
924      return true;
925  }
926}
927
928/**
929 * Returns the requested EXIF/IPTC tag from the current image
930 *
931 * If $tags is an array all given tags are tried until a
932 * value is found. If no value is found $alt is returned.
933 *
934 * Which texts are known is defined in the functions _exifTagNames
935 * and _iptcTagNames() in inc/jpeg.php (You need to prepend IPTC
936 * to the names of the latter one)
937 *
938 * Only allowed in: detail.php
939 *
940 * @author Andreas Gohr <andi@splitbrain.org>
941 */
942function tpl_img_getTag($tags,$alt='',$src=null){
943  // Init Exif Reader
944  global $SRC;
945
946  if(is_null($src)) $src = $SRC;
947
948  static $meta = null;
949  if(is_null($meta)) $meta = new JpegMeta($src);
950  if($meta === false) return $alt;
951  $info = $meta->getField($tags);
952  if($info == false) return $alt;
953  return $info;
954}
955
956/**
957 * Prints the image with a link to the full sized version
958 *
959 * Only allowed in: detail.php
960 */
961function tpl_img($maxwidth=0,$maxheight=0){
962  global $IMG;
963  $w = tpl_img_getTag('File.Width');
964  $h = tpl_img_getTag('File.Height');
965
966  //resize to given max values
967  $ratio = 1;
968  if($w >= $h){
969    if($maxwidth && $w >= $maxwidth){
970      $ratio = $maxwidth/$w;
971    }elseif($maxheight && $h > $maxheight){
972      $ratio = $maxheight/$h;
973    }
974  }else{
975    if($maxheight && $h >= $maxheight){
976      $ratio = $maxheight/$h;
977    }elseif($maxwidth && $w > $maxwidth){
978      $ratio = $maxwidth/$w;
979    }
980  }
981  if($ratio){
982    $w = floor($ratio*$w);
983    $h = floor($ratio*$h);
984  }
985
986  //prepare URLs
987  $url=ml($IMG,array('cache'=>$_REQUEST['cache']));
988  $src=ml($IMG,array('cache'=>$_REQUEST['cache'],'w'=>$w,'h'=>$h));
989
990  //prepare attributes
991  $alt=tpl_img_getTag('Simple.Title');
992  $p = array();
993  if($w) $p['width']  = $w;
994  if($h) $p['height'] = $h;
995         $p['class']  = 'img_detail';
996  if($alt){
997    $p['alt']   = $alt;
998    $p['title'] = $alt;
999  }else{
1000    $p['alt'] = '';
1001  }
1002  $p = buildAttributes($p);
1003
1004  print '<a href="'.$url.'">';
1005  print '<img src="'.$src.'" '.$p.'/>';
1006  print '</a>';
1007  return true;
1008}
1009
1010/**
1011 * This function inserts a 1x1 pixel gif which in reality
1012 * is the inexer function.
1013 *
1014 * Should be called somewhere at the very end of the main.php
1015 * template
1016 */
1017function tpl_indexerWebBug(){
1018  global $ID;
1019  global $INFO;
1020  if(!$INFO['exists']) return false;
1021
1022  if(isHiddenPage($ID)) return false; //no need to index hidden pages
1023
1024  $p = array();
1025  $p['src']    = DOKU_BASE.'lib/exe/indexer.php?id='.rawurlencode($ID).
1026                 '&'.time();
1027  $p['width']  = 1;
1028  $p['height'] = 1;
1029  $p['alt']    = '';
1030  $att = buildAttributes($p);
1031  print "<img $att />";
1032  return true;
1033}
1034
1035// configuration methods
1036/**
1037 * tpl_getConf($id)
1038 *
1039 * use this function to access template configuration variables
1040 */
1041function tpl_getConf($id){
1042  global $conf;
1043  global $tpl_configloaded;
1044
1045  $tpl = $conf['template'];
1046
1047  if (!$tpl_configloaded){
1048    $tconf = tpl_loadConfig();
1049    if ($tconf !== false){
1050      foreach ($tconf as $key => $value){
1051        if (isset($conf['tpl'][$tpl][$key])) continue;
1052        $conf['tpl'][$tpl][$key] = $value;
1053      }
1054      $tpl_configloaded = true;
1055    }
1056  }
1057
1058  return $conf['tpl'][$tpl][$id];
1059}
1060
1061/**
1062 * tpl_loadConfig()
1063 * reads all template configuration variables
1064 * this function is automatically called by tpl_getConf()
1065 */
1066function tpl_loadConfig(){
1067
1068  $file = DOKU_TPLINC.'/conf/default.php';
1069  $conf = array();
1070
1071  if (!@file_exists($file)) return false;
1072
1073  // load default config file
1074  include($file);
1075
1076  return $conf;
1077}
1078
1079/**
1080 * prints the "main content" in the mediamanger popup
1081 *
1082 * Depending on the user's actions this may be a list of
1083 * files in a namespace, the meta editing dialog or
1084 * a message of referencing pages
1085 *
1086 * Only allowed in mediamanager.php
1087 *
1088 * @author Andreas Gohr <andi@splitbrain.org>
1089 */
1090function tpl_mediaContent(){
1091  global $IMG;
1092  global $AUTH;
1093  global $INUSE;
1094  global $NS;
1095  global $JUMPTO;
1096
1097  ptln('<div id="media__content">');
1098  if($_REQUEST['edit']){
1099    media_metaform($IMG,$AUTH);
1100  }elseif(is_array($INUSE)){
1101    media_filesinuse($INUSE,$IMG);
1102  }else{
1103    media_filelist($NS,$AUTH,$JUMPTO);
1104  }
1105  ptln('</div>');
1106}
1107
1108/**
1109 * prints the namespace tree in the mediamanger popup
1110 *
1111 * Only allowed in mediamanager.php
1112 *
1113 * @author Andreas Gohr <andi@splitbrain.org>
1114 */
1115function tpl_mediaTree(){
1116  global $NS;
1117
1118  ptln('<div id="media__tree">');
1119  media_nstree($NS);
1120  ptln('</div>');
1121}
1122
1123//Setup VIM: ex: et ts=4 enc=utf-8 :
1124