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