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