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