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