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