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