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