xref: /dokuwiki/inc/template.php (revision 524be65dda410b9a3ced3fa06e672b4fd2cda5e4)
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 ($conf['gzip_output'] && extension_loaded('zlib') && preg_match('/gzip|deflate/', $_SERVER['HTTP_ACCEPT_ENCODING'])) {
23    ob_start('ob_gzhandler');
24  }
25
26  if(@is_readable(DOKU_INC.'lib/tpl/'.$conf['template'].'/'.$tpl))
27    return DOKU_INC.'lib/tpl/'.$conf['template'].'/'.$tpl;
28
29  return DOKU_INC.'lib/tpl/default/'.$tpl;
30}
31
32/**
33 * Print the content
34 *
35 * This function is used for printing all the usual content
36 * (defined by the global $ACT var) by calling the appropriate
37 * outputfunction(s) from html.php
38 *
39 * Everything that doesn't use the main template file isn't
40 * handled by this function. ACL stuff is not done here either.
41 *
42 * @author Andreas Gohr <andi@splitbrain.org>
43 */
44function tpl_content() {
45  global $ACT;
46
47  ob_start();
48
49  trigger_event('TPL_ACT_RENDER',$ACT,tpl_content_core);
50
51  $html_output = ob_get_clean();
52
53  trigger_event('TPL_CONTENT_DISPLAY',$html_output,ptln);
54}
55
56function tpl_content_core(){
57  global $ACT;
58  global $TEXT;
59  global $PRE;
60  global $SUF;
61  global $SUM;
62  global $IDX;
63
64  switch($ACT){
65    case 'show':
66      html_show();
67      break;
68    case 'preview':
69      html_edit($TEXT);
70      html_show($TEXT);
71      break;
72    case 'recover':
73      html_edit($TEXT);
74      break;
75    case 'edit':
76      html_edit();
77      break;
78    case 'draft':
79      html_draft();
80      break;
81    case 'wordblock':
82      html_edit($TEXT,'wordblock');
83      break;
84    case 'search':
85      html_search();
86      break;
87    case 'revisions':
88      html_revisions();
89      break;
90    case 'diff':
91      html_diff();
92      break;
93    case 'recent':
94      $first = is_numeric($_REQUEST['first']) ? intval($_REQUEST['first']) : 0;
95      html_recent($first);
96      break;
97    case 'index':
98      html_index($IDX); #FIXME can this be pulled from globals? is it sanitized correctly?
99      break;
100    case 'backlink':
101      html_backlinks();
102      break;
103    case 'conflict':
104      html_conflict(con($PRE,$TEXT,$SUF),$SUM);
105      html_diff(con($PRE,$TEXT,$SUF),false);
106      break;
107    case 'locked':
108      html_locked();
109      break;
110    case 'login':
111      html_login();
112      break;
113    case 'register':
114      html_register();
115      break;
116    case 'resendpwd':
117      html_resendpwd();
118      break;
119    case 'denied':
120      print p_locale_xhtml('denied');
121      break;
122    case 'profile' :
123      html_updateprofile();
124      break;
125    case 'admin':
126      tpl_admin();
127      break;
128    default:
129      $evt = new Doku_Event('TPL_ACT_UNKNOWN',$ACT);
130      if ($evt->advise_before())
131        msg("Failed to handle command: ".hsc($ACT),-1);
132      $evt->advise_after();
133      unset($evt);
134  }
135}
136
137/**
138 * Handle the admin page contents
139 *
140 * @author Andreas Gohr <andi@splitbrain.org>
141 */
142function tpl_admin(){
143
144    $plugin = NULL;
145    if ($_REQUEST['page']) {
146        $pluginlist = plugin_list('admin');
147
148        if (in_array($_REQUEST['page'], $pluginlist)) {
149
150          // attempt to load the plugin
151          $plugin =& plugin_load('admin',$_REQUEST['page']);
152        }
153    }
154
155    if ($plugin !== NULL)
156        $plugin->html();
157    else
158        html_admin();
159}
160
161/**
162 * Print the correct HTML meta headers
163 *
164 * This has to go into the head section of your template.
165 *
166 * @param  boolean $alt Should feeds and alternative format links be added?
167 * @author Andreas Gohr <andi@splitbrain.org>
168 */
169function tpl_metaheaders($alt=true){
170  global $ID;
171  global $INFO;
172  global $ACT;
173  global $lang;
174  global $conf;
175  $it=2;
176
177  // the usual stuff
178  ptln('<meta name="generator" content="DokuWiki '.getVersion().'" />',$it);
179  ptln('<link rel="start" href="'.DOKU_BASE.'" />',$it);
180  ptln('<link rel="contents" href="'.wl($ID,'do=index').'" title="'.$lang['index'].'" />',$it);
181
182  if($alt){
183    ptln('<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="'.DOKU_BASE.'feed.php" />',$it);
184    ptln('<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="'.DOKU_BASE.'feed.php?mode=list&amp;ns='.$INFO['namespace'].'" />',$it);
185    ptln('<link rel="alternate" type="text/html" title="Plain HTML" href="'.exportlink($ID, 'xhtml').'" />',$it);
186    ptln('<link rel="alternate" type="text/plain" title="Wiki Markup" href="'.exportlink($ID, 'raw').'" />',$it);
187  }
188
189  // setup robot tags apropriate for different modes
190  if( ($ACT=='show' || $ACT=='export_xhtml') && !$REV){
191    if($INFO['exists']){
192      ptln('<meta name="date" content="'.date('Y-m-d\TH:i:sO',$INFO['lastmod']).'" />',$it);
193      //delay indexing:
194      if((time() - $INFO['lastmod']) >= $conf['indexdelay']){
195        ptln('<meta name="robots" content="index,follow" />',$it);
196      }else{
197        ptln('<meta name="robots" content="noindex,nofollow" />',$it);
198      }
199    }else{
200      ptln('<meta name="robots" content="noindex,follow" />',$it);
201    }
202  }else{
203    ptln('<meta name="robots" content="noindex,nofollow" />',$it);
204  }
205
206  // load stylesheets
207  ptln('<link rel="stylesheet" media="screen" type="text/css" href="'.DOKU_BASE.'lib/exe/css.php" />',$it);
208  ptln('<link rel="stylesheet" media="print" type="text/css" href="'.DOKU_BASE.'lib/exe/css.php?print=1" />',$it);
209
210  // load javascript
211  $js_edit  = ($ACT=='edit' || $ACT=='preview' || $ACT=='recover') ? 1 : 0;
212  $js_write = ($INFO['writable']) ? 1 : 0;
213  if($js_edit && $js_write){
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(){
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
571  print $lang['youarehere'].': ';
572
573  //always print the startpage
574  if( $a_part[0] != $conf['start']){
575    if($conf['useheading']){
576      $pageName = p_get_first_heading($conf['start']);
577    }else{
578      $pageName = $conf['start'];
579    }
580    tpl_link(wl($conf['start']),$pageName,'title="'.$pageName.'"');
581  }
582
583  $page = '';
584  foreach ($parts as $part){
585        // Skip startpage if already done
586        if ($part == $conf['start']) continue;
587
588          print ' &raquo; ';
589    $page .= $part;
590
591    if(file_exists(wikiFN($page))){
592      if($conf['useheading']){
593        $pageName = p_get_first_heading($page);
594        $partName = $pageName;
595      }else{
596        $pageName = $page;
597        $partName = $part;
598      }
599      tpl_link(wl($page),$partName,'title="'.$pageName.'"');
600    }else{
601      // Print the link, but mark as not-existing, as for other non-existing links
602      tpl_link(wl($page),$part,'title="'.$page.'" class="wikilink2"');
603      //print $page;
604    }
605
606    $page .= ':';
607  }
608}
609
610/**
611 * Print info if the user is logged in
612 * and show full name in that case
613 *
614 * Could be enhanced with a profile link in future?
615 *
616 * @author Andreas Gohr <andi@splitbrain.org>
617 */
618function tpl_userinfo(){
619  global $lang;
620  global $INFO;
621  if($_SERVER['REMOTE_USER'])
622    print $lang['loggedinas'].': '.$INFO['userinfo']['name'];
623}
624
625/**
626 * Print some info about the current page
627 *
628 * @author Andreas Gohr <andi@splitbrain.org>
629 */
630function tpl_pageinfo(){
631  global $conf;
632  global $lang;
633  global $INFO;
634  global $REV;
635
636  // prepare date and path
637  $fn = $INFO['filepath'];
638  if(!$conf['fullpath']){
639    if($REV){
640      $fn = str_replace(realpath($conf['olddir']).DIRECTORY_SEPARATOR,'',$fn);
641    }else{
642      $fn = str_replace(realpath($conf['datadir']).DIRECTORY_SEPARATOR,'',$fn);
643    }
644  }
645  $fn = utf8_decodeFN($fn);
646  $date = date($conf['dformat'],$INFO['lastmod']);
647
648  // print it
649  if($INFO['exists']){
650    print $fn;
651    print ' &middot; ';
652    print $lang['lastmod'];
653    print ': ';
654    print $date;
655    if($INFO['editor']){
656      print ' '.$lang['by'].' ';
657      print $INFO['editor'];
658    }
659    if($INFO['locked']){
660      print ' &middot; ';
661      print $lang['lockedby'];
662      print ': ';
663      print $INFO['locked'];
664    }
665  }
666}
667
668/**
669 * Print a list of namespaces containing media files
670 *
671 * @author Andreas Gohr <andi@splitbrain.org>
672 */
673function tpl_medianamespaces(){
674    global $conf;
675
676  $data = array();
677  search($data,$conf['mediadir'],'search_namespaces',array());
678  print html_buildlist($data,'idx',media_html_list_namespaces);
679}
680
681/**
682 * Print a list of mediafiles in the current namespace
683 *
684 * @author Andreas Gohr <andi@splitbrain.org>
685 */
686function tpl_mediafilelist(){
687  global $conf;
688  global $lang;
689  global $NS;
690  global $AUTH;
691  $dir = utf8_encodeFN(str_replace(':','/',$NS));
692
693  $data = array();
694  search($data,$conf['mediadir'],'search_media',array(),$dir);
695
696  if(!count($data)){
697    ptln('<div class="nothing">'.$lang['nothingfound'].'</div>');
698    return;
699  }
700
701  ptln('<ul>',2);
702  foreach($data as $item){
703    if(!$item['isimg']){
704      // add file icons
705      list($ext,$mime) = mimetype($item['file']);
706      $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
707      $class = ' class="mediafile mf_'.$class.'"';
708    }else{
709      $class = '';
710    }
711
712    ptln('<li><div class="li">',4);
713    ptln('<a href="javascript:mediaSelect(\':'.$item['id'].'\')"'.$class.'>'.
714         utf8_decodeFN($item['file']).
715         '</a>',6);
716
717    //prepare deletion button
718    if($AUTH >= AUTH_DELETE){
719      $ask  = addslashes($lang['del_confirm']).'\\n';
720      $ask .= addslashes($item['id']);
721
722      $del = '<a href="'.DOKU_BASE.'lib/exe/media.php?delete='.rawurlencode($item['id']).'" '.
723             'onclick="return confirm(\''.$ask.'\')" onkeypress="return confirm(\''.$ask.'\')">'.
724             '<img src="'.DOKU_BASE.'lib/images/del.png" alt="'.$lang['btn_delete'].'" '.
725             'title="'.$lang['btn_delete'].'" /></a>';
726    }else{
727      $del = '';
728    }
729
730    if($item['isimg']){
731      $w = (int) $item['meta']->getField('File.Width');
732      $h = (int) $item['meta']->getField('File.Height');
733
734      ptln('('.$w.'&#215;'.$h.' '.filesize_h($item['size']).')',6);
735      ptln($del.'<br />',6);
736      ptln('<div class="imagemeta">',6);
737
738      //build thumbnail
739      print '<a href="javascript:mediaSelect(\':'.$item['id'].'\')">';
740
741      if($w>120 || $h>120){
742        $ratio = $item['meta']->getResizeRatio(120);
743        $w = floor($w * $ratio);
744        $h = floor($h * $ratio);
745      }
746
747      $src = ml($item['id'],array('w'=>$w,'h'=>$h));
748
749      $p = array();
750      $p['width']  = $w;
751      $p['height'] = $h;
752      $p['alt']    = $item['id'];
753      $p['class']  = 'thumb';
754      $att = buildAttributes($p);
755
756      print '<img src="'.$src.'" '.$att.' />';
757      print '</a>';
758
759      //read EXIF/IPTC data
760      $t = $item['meta']->getField('IPTC.Headline');
761      if($t) print '<strong>'.htmlspecialchars($t).'</strong><br />';
762
763      $t = $item['meta']->getField(array('IPTC.Caption','EXIF.UserComment',
764                                         'EXIF.TIFFImageDescription',
765                                         'EXIF.TIFFUserComment'));
766      if($t) print htmlspecialchars($t).'<br />';
767
768      $t = $item['meta']->getField(array('IPTC.Keywords','IPTC.Category'));
769      if($t) print '<em>'.htmlspecialchars($t).'</em><br />';
770
771      //add edit button
772      if($AUTH >= AUTH_UPLOAD && $item['meta']->getField('File.Mime') == 'image/jpeg'){
773        print '<a href="'.DOKU_BASE.'lib/exe/media.php?edit='.rawurlencode($item['id']).'">';
774        print '<img src="'.DOKU_BASE.'lib/images/edit.gif" alt="'.$lang['metaedit'].'" title="'.$lang['metaedit'].'" />';
775        print '</a>';
776      }
777
778      ptln('</div>',6);
779    }else{
780      ptln ('('.filesize_h($item['size']).')',6);
781      ptln($del,6);
782    }
783    ptln('</div></li>',4);
784  }
785  ptln('</ul>',2);
786}
787
788/**
789 * show references to a media file
790 * References uses the same visual as search results and share
791 * their CSS tags except pagenames won't be links.
792 *
793 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
794 */
795function tpl_showreferences(&$data){
796  global $lang;
797
798  $hidden=0; //count of hits without read permission
799
800  if(count($data)){
801    usort($data,'sort_search_fulltext');
802    foreach($data as $row){
803      if(auth_quickaclcheck($row['id']) >= AUTH_READ){
804        print '<div class="search_result">';
805        print '<span class="mediaref_ref">'.$row['id'].'</span>';
806        print ': <span class="search_cnt">'.$row['count'].' '.$lang['hits'].'</span><br />';
807        print '<div class="search_snippet">'.$row['snippet'].'</div>';
808        print '</div>';
809      }else
810        $hidden++;
811    }
812    if ($hidden){
813      print '<div class="mediaref_hidden">'.$lang['ref_hidden'].'</div>';
814    }
815  }
816}
817
818/**
819 * Print the media upload form if permissions are correct
820 *
821 * @author Andreas Gohr <andi@splitbrain.org>
822 */
823function tpl_mediauploadform(){
824  global $NS;
825  global $UPLOADOK;
826  global $AUTH;
827  global $lang;
828
829  if(!$UPLOADOK) return;
830
831  ptln('<form action="'.DOKU_BASE.'lib/exe/media.php" id="dw__upload"'.
832       ' method="post" enctype="multipart/form-data">',2);
833  ptln($lang['txt_upload'].':<br />',4);
834  ptln('<input type="file" name="upload" class="edit" onchange="suggestWikiname();" />',4);
835  ptln('<input type="hidden" name="ns" value="'.hsc($NS).'" /><br />',4);
836  ptln($lang['txt_filename'].'<br />',4);
837  ptln('<input type="text" name="id" class="edit" />',4);
838  ptln('<input type="submit" class="button" value="'.$lang['btn_upload'].'" accesskey="s" />',4);
839  if($AUTH >= AUTH_DELETE){
840    ptln('<label for="dw__ow"><input type="checkbox" name="ow" value="1" id="dw__ow" />'.$lang['txt_overwrt'].'</label>',4);
841  }
842  ptln('</form>',2);
843}
844
845/**
846 * Prints or returns the name of the given page (current one if none given).
847 *
848 * If useheading is enabled this will use the first headline else
849 * the given ID is used.
850 *
851 * @author Andreas Gohr <andi@splitbrain.org>
852 */
853function tpl_pagetitle($id=null, $ret=false){
854  global $conf;
855  if(is_null($id)){
856    global $ID;
857    $id = $ID;
858  }
859
860  $name = $id;
861  if ($conf['useheading']) {
862    $title = p_get_first_heading($id);
863    if ($title) $name = $title;
864  }
865
866  if ($ret) {
867      return hsc($name);
868  } else {
869      print hsc($name);
870  }
871}
872
873/**
874 * Returns the requested EXIF/IPTC tag from the current image
875 *
876 * If $tags is an array all given tags are tried until a
877 * value is found. If no value is found $alt is returned.
878 *
879 * Which texts are known is defined in the functions _exifTagNames
880 * and _iptcTagNames() in inc/jpeg.php (You need to prepend IPTC
881 * to the names of the latter one)
882 *
883 * Only allowed in: detail.php, mediaedit.php
884 *
885 * @author Andreas Gohr <andi@splitbrain.org>
886 */
887function tpl_img_getTag($tags,$alt=''){
888  // Init Exif Reader
889  global $SRC;
890  static $meta = null;
891  if(is_null($meta)) $meta = new JpegMeta($SRC);
892  if($meta === false) return $alt;
893  $info = $meta->getField($tags);
894  if($info == false) return $alt;
895  return $info;
896}
897
898/**
899 * Prints the image with a link to the full sized version
900 *
901 * Only allowed in: detail.php
902 */
903function tpl_img($maxwidth=0,$maxheight=0){
904  global $IMG;
905  $w = tpl_img_getTag('File.Width');
906  $h = tpl_img_getTag('File.Height');
907
908  //resize to given max values
909  $ratio = 1;
910  if($w >= $h){
911    if($maxwidth && $w >= $maxwidth){
912      $ratio = $maxwidth/$w;
913    }elseif($maxheight && $h > $maxheight){
914      $ratio = $maxheight/$h;
915    }
916  }else{
917    if($maxheight && $h >= $maxheight){
918      $ratio = $maxheight/$h;
919    }elseif($maxwidth && $w > $maxwidth){
920      $ratio = $maxwidth/$w;
921    }
922  }
923  if($ratio){
924    $w = floor($ratio*$w);
925    $h = floor($ratio*$h);
926  }
927
928  //prepare URLs
929  $url=ml($IMG,array('cache'=>$_REQUEST['cache']));
930  $src=ml($IMG,array('cache'=>$_REQUEST['cache'],'w'=>$w,'h'=>$h));
931
932  //prepare attributes
933  $alt=tpl_img_getTag('Simple.Title');
934  $p = array();
935  if($w) $p['width']  = $w;
936  if($h) $p['height'] = $h;
937         $p['class']  = 'img_detail';
938  if($alt){
939    $p['alt']   = $alt;
940    $p['title'] = $alt;
941  }else{
942    $p['alt'] = '';
943  }
944  $p = buildAttributes($p);
945
946  print '<a href="'.$url.'">';
947  print '<img src="'.$src.'" '.$p.'/>';
948  print '</a>';
949}
950
951/**
952 * This function inserts a 1x1 pixel gif which in reality
953 * is the inexer function.
954 *
955 * Should be called somewhere at the very end of the main.php
956 * template
957 */
958function tpl_indexerWebBug(){
959  global $ID;
960  global $INFO;
961  if(!$INFO['exists']) return;
962
963  if(isHiddenPage($ID)) return; //no need to index hidden pages
964
965  $p = array();
966  $p['src']    = DOKU_BASE.'lib/exe/indexer.php?id='.rawurlencode($ID).
967                 '&'.time();
968  $p['width']  = 1;
969  $p['height'] = 1;
970  $p['alt']    = '';
971  $att = buildAttributes($p);
972  print "<img $att />";
973}
974
975// configuration methods
976/**
977 * tpl_getConf($id)
978 *
979 * use this function to access template configuration variables
980 */
981function tpl_getConf($id){
982  global $conf;
983  global $tpl_configloaded;
984
985  $tpl = $conf['template'];
986
987  if (!$tpl_configloaded){
988    $tconf = tpl_loadConfig();
989    if ($tconf !== false){
990      foreach ($tconf as $key => $value){
991        if (isset($conf['tpl'][$tpl][$key])) continue;
992        $conf['tpl'][$tpl][$key] = $value;
993      }
994      $tpl_configloaded = true;
995    }
996  }
997
998  return $conf['tpl'][$tpl][$id];
999}
1000
1001/**
1002 * tpl_loadConfig()
1003 * reads all template configuration variables
1004 * this function is automatically called by tpl_getConf()
1005 */
1006function tpl_loadConfig(){
1007
1008  $file = DOKU_TPLINC.'/conf/default.php';
1009  $conf = array();
1010
1011  if (!@file_exists($file)) return false;
1012
1013  // load default config file
1014  include($file);
1015
1016  return $conf;
1017}
1018
1019//Setup VIM: ex: et ts=2 enc=utf-8 :
1020