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