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