xref: /dokuwiki/inc/template.php (revision f00151b4e3d17bcf50767f821c0e3834d36e7b4f)
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    case 'backlink':
350      print html_btn('backlink',$ID,'',array('do' => 'backlink'));
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 *
371 * @author Andreas Gohr <andi@splitbrain.org>
372 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
373 * @see    tpl_button
374 */
375function tpl_actionlink($type,$pre='',$suf=''){
376  global $ID;
377  global $INFO;
378  global $REV;
379  global $ACT;
380  global $conf;
381  global $lang;
382
383  switch($type){
384    case 'edit':
385      #most complicated type - we need to decide on current action
386      if($ACT == 'show' || $ACT == 'search'){
387        if($INFO['writable']){
388          if($INFO['exists']){
389            tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
390                     $pre.$lang['btn_edit'].$suf,
391                     'class="action" accesskey="e" rel="nofollow"');
392          }else{
393            tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
394                     $pre.$lang['btn_create'].$suf,
395                     'class="action" accesskey="e" rel="nofollow"');
396          }
397        }else{
398          tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
399                   $pre.$lang['btn_source'].$suf,
400                   'class="action" accesskey="v" rel="nofollow"');
401        }
402      }else{
403          tpl_link(wl($ID,'do=show'),
404                   $pre.$lang['btn_show'].$suf,
405                   'class="action" accesskey="v" rel="nofollow"');
406      }
407      break;
408    case 'history':
409      tpl_link(wl($ID,'do=revisions'),$pre.$lang['btn_revs'].$suf,'class="action" accesskey="o"');
410      break;
411    case 'recent':
412      tpl_link(wl($ID,'do=recent'),$pre.$lang['btn_recent'].$suf,'class="action" accesskey="r"');
413      break;
414    case 'index':
415      tpl_link(wl($ID,'do=index'),$pre.$lang['btn_index'].$suf,'class="action" accesskey="x"');
416      break;
417    case 'top':
418      print '<a href="#top" class="action" accesskey="x">'.$pre.$lang['btn_top'].$suf.'</a>';
419      break;
420    case 'back':
421      if ($ID = tpl_getparent($ID)) {
422        tpl_link(wl($ID,'do=show'),$pre.$lang['btn_back'].$suf,'class="action" accesskey="b"');
423      }
424      break;
425    case 'login':
426      if($conf['useacl']){
427        if($_SERVER['REMOTE_USER']){
428          tpl_link(wl($ID,'do=logout'),$pre.$lang['btn_logout'].$suf,'class="action"');
429        }else{
430          tpl_link(wl($ID,'do=login'),$pre.$lang['btn_login'].$suf,'class="action"');
431        }
432      }
433      break;
434    case 'admin':
435      if($INFO['perm'] == AUTH_ADMIN)
436        tpl_link(wl($ID,'do=admin'),$pre.$lang['btn_admin'].$suf,'class="action"');
437      break;
438   case 'subscribe':
439      if($conf['useacl'] && $ACT == 'show' && $conf['subscribers'] == 1){
440        if($_SERVER['REMOTE_USER']){
441          if($info['subscribed']) {
442            tpl_link(wl($ID,'do=unsubscribe'),$pre.$lang['btn_unsubscribe'].$suf,'class="action"');
443          } else {
444            tpl_link(wl($ID,'do=subscribe'),$pre.$lang['btn_subscribe'].$suf,'class="action"');
445          }
446        }
447      }
448      break;
449	case 'backlink':
450	  tpl_link(wl($ID,'do=backlink'),$pre.$lang['btn_backlink'].$suf, 'class="action"');
451	  break;
452    default:
453      print '[unknown link type]';
454  }
455}
456
457/**
458 * Print the search form
459 *
460 * @author Andreas Gohr <andi@splitbrain.org>
461 */
462function tpl_searchform(){
463  global $lang;
464  global $ACT;
465
466  print '<form action="'.wl().'" accept-charset="utf-8" class="search" name="search" onsubmit="return svchk()">';
467  print '<input type="hidden" name="do" value="search" />';
468  print '<input type="text" ';
469
470  if ($ACT == 'search')
471    print 'value="'.$_REQUEST['id'].'" '; /* keep search input as long as user stays on search page */
472
473  print 'id="qsearch_in" accesskey="f" name="id" class="edit" onkeyup="ajax_qsearch.call(\'qsearch_in\',\'qsearch_out\')" />';
474  print '<input type="submit" value="'.$lang['btn_search'].'" class="button" />';
475  print '<div id="qsearch_out" class="ajax_qsearch" onclick="this.style.display=\'none\'"></div>';
476  print '</form>';
477}
478
479/**
480 * Print the breadcrumbs trace
481 *
482 * @author Andreas Gohr <andi@splitbrain.org>
483 */
484function tpl_breadcrumbs(){
485  global $lang;
486  global $conf;
487
488  //check if enabled
489  if(!$conf['breadcrumbs']) return;
490
491  $crumbs = breadcrumbs(); //setup crumb trace
492
493  //reverse crumborder in right-to-left mode
494  if($lang['direction'] == 'rtl') $crumbs = array_reverse($crumbs,true);
495
496  //render crumbs, highlight the last one
497  print $lang['breadcrumb'].':';
498  $last = count($crumbs);
499  $i = 0;
500  foreach ($crumbs as $id => $name){
501    $i++;
502    print ' <span class="bcsep">&raquo;</span> ';
503    if ($i == $last) print '<span class="curid">';
504    tpl_link(wl($id),$name,'class="breadcrumbs" title="'.$id.'"');
505    if ($i == $last) print '</span>';
506  }
507}
508
509/**
510 * Hierarchical breadcrumbs
511 *
512 * This code was suggested as replacement for the usual breadcrumbs
513 * trail in the Wiki and was modified by me.
514 * It only makes sense with a deep site structure.
515 *
516 * @author Andreas Gohr <andi@splitbrain.org>
517 * @link   http://wiki.splitbrain.org/wiki:tipsandtricks:hierarchicalbreadcrumbs
518 * @todo   May behave starngely in RTL languages
519 */
520function tpl_youarehere(){
521  global $conf;
522  global $ID;
523  global $lang;
524
525
526  $parts     = explode(':', $ID);
527
528  print $lang['breadcrumb'].': ';
529
530  //always print the startpage
531  if( $a_part[0] != $conf['start'] )
532    tpl_link(wl($conf['start']),$conf['start'],'title="'.$conf['start'].'"');
533
534  $page = '';
535  foreach ($parts as $part){
536	  print ' &raquo; ';
537    $page .= $part;
538
539    if(file_exists(wikiFN($page))){
540      tpl_link(wl($page),$part,'title="'.$page.'"');
541    }else{
542      print $page;
543    }
544
545    $page .= ':';
546  }
547}
548
549/**
550 * Print info if the user is logged in
551 * and show full name in that case
552 *
553 * Could be enhanced with a profile link in future?
554 *
555 * @author Andreas Gohr <andi@splitbrain.org>
556 */
557function tpl_userinfo(){
558  global $lang;
559  global $INFO;
560  if($_SERVER['REMOTE_USER'])
561    print $lang['loggedinas'].': '.$INFO['userinfo']['name'];
562}
563
564/**
565 * Print some info about the current page
566 *
567 * @author Andreas Gohr <andi@splitbrain.org>
568 */
569function tpl_pageinfo(){
570  global $conf;
571  global $lang;
572  global $INFO;
573  global $REV;
574
575  // prepare date and path
576  $fn = $INFO['filepath'];
577  if(!$conf['fullpath']){
578    if($REV){
579      $fn = str_replace(realpath($conf['olddir']).DIRECTORY_SEPARATOR,'',$fn);
580    }else{
581      $fn = str_replace(realpath($conf['datadir']).DIRECTORY_SEPARATOR,'',$fn);
582    }
583  }
584  $fn = utf8_decodeFN($fn);
585  $date = date($conf['dformat'],$INFO['lastmod']);
586
587  // print it
588  if($INFO['exists']){
589    print $fn;
590    print ' &middot; ';
591    print $lang['lastmod'];
592    print ': ';
593    print $date;
594    if($INFO['editor']){
595      print ' '.$lang['by'].' ';
596      print $INFO['editor'];
597    }
598    if($INFO['locked']){
599      print ' &middot; ';
600      print $lang['lockedby'];
601      print ': ';
602      print $INFO['locked'];
603    }
604  }
605}
606
607/**
608 * Print a list of namespaces containing media files
609 *
610 * @author Andreas Gohr <andi@splitbrain.org>
611 */
612function tpl_medianamespaces(){
613	global $conf;
614
615  $data = array();
616  search($data,$conf['mediadir'],'search_namespaces',array());
617  print html_buildlist($data,'idx',media_html_list_namespaces);
618}
619
620/**
621 * Print a list of mediafiles in the current namespace
622 *
623 * @author Andreas Gohr <andi@splitbrain.org>
624 */
625function tpl_mediafilelist(){
626  global $conf;
627  global $lang;
628  global $NS;
629  global $AUTH;
630  $dir = utf8_encodeFN(str_replace(':','/',$NS));
631
632  $data = array();
633  search($data,$conf['mediadir'],'search_media',array(),$dir);
634
635  if(!count($data)){
636    ptln('<div class="nothing">'.$lang['nothingfound'].'</div>');
637    return;
638  }
639
640  ptln('<ul>',2);
641  foreach($data as $item){
642    ptln('<li>',4);
643    ptln('<a href="javascript:mediaSelect(\''.$item['id'].'\')">'.
644         utf8_decodeFN($item['file']).
645         '</a>',6);
646
647    //prepare deletion button
648    if($AUTH >= AUTH_DELETE){
649      $ask  = $lang['del_confirm'].'\\n';
650      $ask .= $item['id'];
651
652      $del = '<a href="'.DOKU_BASE.'lib/exe/media.php?delete='.urlencode($item['id']).'" '.
653             'onclick="return confirm(\''.$ask.'\')" onkeypress="return confirm(\''.$ask.'\')">'.
654             '<img src="'.DOKU_BASE.'lib/images/del.png" alt="'.$lang['btn_delete'].'" '.
655             'align="bottom" title="'.$lang['btn_delete'].'" /></a>';
656    }else{
657      $del = '';
658    }
659
660    if($item['isimg']){
661      $w = $item['meta']->getField('File.Width');
662      $h = $item['meta']->getField('File.Height');
663
664      ptln('('.$w.'&#215;'.$h.' '.filesize_h($item['size']).')',6);
665      ptln($del.'<br />',6);
666      ptln('<div class="meta">',6);
667
668      //build thumbnail
669      print '<a href="javascript:mediaSelect(\''.$item['id'].'\')">';
670
671      if($w>120 || $h>120){
672        $ratio = $item['meta']->getResizeRatio(120);
673        $w = floor($w * $ratio);
674        $h = floor($h * $ratio);
675      }
676
677      $src = ml($item['id'],array('w'=>$w,'h'=>$h));
678
679      $p = array();
680      $p['width']  = $w;
681      $p['height'] = $h;
682      $p['alt']    = $item['id'];
683      $p['class']  = 'thumb';
684      $att = buildAttributes($p);
685
686      print '<img src="'.$src.'" '.$att.' />';
687      print '</a>';
688
689      //read EXIF/IPTC data
690      $t = $item['meta']->getField('IPTC.Headline');
691      if($t) print '<b>'.$t.'</b><br />';
692
693      $t = $item['meta']->getField(array('IPTC.Caption','EXIF.UserComment',
694                                         'EXIF.TIFFImageDescription',
695                                         'EXIF.TIFFUserComment'));
696      if($t) print $t.'<br />';
697
698      $t = $item['meta']->getField(array('IPTC.Keywords','IPTC.Category'));
699      if($t) print '<i>'.$t.'</i><br />';
700
701      //add edit button
702      if($AUTH >= AUTH_UPLOAD && $item['meta']->getField('File.Mime') == 'image/jpeg'){
703        print '<a href="'.DOKU_BASE.'lib/exe/media.php?edit='.urlencode($item['id']).'">';
704        print '<img src="'.DOKU_BASE.'lib/images/edit.gif" alt="'.$lang['metaedit'].'" title="'.$lang['metaedit'].'" />';
705        print '</a>';
706      }
707
708      ptln('</div>',6);
709    }else{
710      ptln ('('.filesize_h($item['size']).')',6);
711      ptln($del,6);
712    }
713    ptln('</li>',4);
714  }
715  ptln('</ul>',2);
716}
717
718/**
719 * show references to a media file
720 * References uses the same visual as search results and share
721 * their CSS tags except pagenames won't be links.
722 *
723 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
724 */
725function tpl_showreferences(&$data){
726  global $lang;
727
728  $hidden=0; //count of hits without read permission
729
730  if(count($data)){
731    usort($data,'sort_search_fulltext');
732    foreach($data as $row){
733      if(auth_quickaclcheck($row['id']) >= AUTH_READ){
734        print '<div class="search_result">';
735        print '<span class="mediaref_ref">'.$row['id'].'</span>';
736        print ': <span class="search_cnt">'.$row['count'].' '.$lang['hits'].'</span><br />';
737        print '<div class="search_snippet">'.$row['snippet'].'</div>';
738        print '</div>';
739      }else
740        $hidden++;
741    }
742    if ($hidden){
743      print '<div class="mediaref_hidden">'.$lang['ref_hidden'].'</div>';
744    }
745  }
746}
747
748/**
749 * Print the media upload form if permissions are correct
750 *
751 * @author Andreas Gohr <andi@splitbrain.org>
752 */
753function tpl_mediauploadform(){
754  global $NS;
755  global $UPLOADOK;
756  global $AUTH;
757  global $lang;
758
759  if(!$UPLOADOK) return;
760
761  ptln('<form action="'.DOKU_BASE.'lib/exe/media.php" name="upload"'.
762       ' method="post" enctype="multipart/form-data">',2);
763  ptln($lang['txt_upload'].':<br />',4);
764  ptln('<input type="file" name="upload" class="edit" onchange="suggestWikiname();" />',4);
765  ptln('<input type="hidden" name="ns" value="'.hsc($NS).'" /><br />',4);
766  ptln($lang['txt_filename'].'<br />',4);
767  ptln('<input type="text" name="id" class="edit" />',4);
768  ptln('<input type="submit" class="button" value="'.$lang['btn_upload'].'" accesskey="s" />',4);
769  if($AUTH >= AUTH_DELETE){
770    ptln('<label for="ow" class="simple"><input type="checkbox" name="ow" value="1" id="ow">'.$lang['txt_overwrt'].'</label>',4);
771  }
772  ptln('</form>',2);
773}
774
775/**
776 * Prints the name of the given page (current one if none given).
777 *
778 * If useheading is enabled this will use the first headline else
779 * the given ID is printed.
780 *
781 * @author Andreas Gohr <andi@splitbrain.org>
782 */
783function tpl_pagetitle($id=null){
784  global $conf;
785  if(is_null($id)){
786    global $ID;
787    $id = $ID;
788  }
789
790  $name = $id;
791  if ($conf['useheading']) {
792    $title = p_get_first_heading($id);
793    if ($title) $name = $title;
794  }
795  print hsc($name);
796}
797
798/**
799 * Returns the requested EXIF/IPTC tag from the current image
800 *
801 * If $tags is an array all given tags are tried until a
802 * value is found. If no value is found $alt is returned.
803 *
804 * Which texts are known is defined in the functions _exifTagNames
805 * and _iptcTagNames() in inc/jpeg.php (You need to prepend IPTC
806 * to the names of the latter one)
807 *
808 * Only allowed in: detail.php, mediaedit.php
809 *
810 * @author Andreas Gohr <andi@splitbrain.org>
811 */
812function tpl_img_getTag($tags,$alt=''){
813  // Init Exif Reader
814  global $SRC;
815  static $meta = null;
816  if(is_null($meta)) $meta = new JpegMeta($SRC);
817  if($meta === false) return $alt;
818  $info = $meta->getField($tags);
819  if($info == false) return $alt;
820  return $info;
821}
822
823/**
824 * Prints the image with a link to the full sized version
825 *
826 * Only allowed in: detail.php
827 */
828function tpl_img($maxwidth=900,$maxheight=700){
829  global $IMG;
830  $w = tpl_img_getTag('File.Width');
831  $h = tpl_img_getTag('File.Height');
832
833  //resize to given max values
834  $ratio = 1;
835  if($w >= $h){
836    if($w >= $maxwidth){
837      $ratio = $maxwidth/$w;
838    }elseif($h > $maxheight){
839      $ratio = $maxheight/$h;
840    }
841  }else{
842    if($h >= $maxheight){
843      $ratio = $maxheight/$h;
844    }elseif($w > $maxwidth){
845      $ratio = $maxwidth/$w;
846    }
847  }
848  if($ratio){
849    $w = floor($ratio*$w);
850    $h = floor($ratio*$h);
851  }
852
853  //prepare URLs
854  $url=ml($IMG,array('cache'=>$_REQUEST['cache']));
855  $src=ml($IMG,array('cache'=>$_REQUEST['cache'],'w'=>$w,'h'=>$h));
856
857  //prepare attributes
858  $alt=tpl_img_getTag('Simple.Title');
859  $p = array();
860  if($w) $p['width']  = $w;
861  if($h) $p['height'] = $h;
862         $p['class']  = 'img_detail';
863  if($alt){
864    $p['alt']   = $alt;
865    $p['title'] = $alt;
866  }else{
867    $p['alt'] = '';
868  }
869  $p = buildAttributes($p);
870
871  print '<a href="'.$url.'">';
872  print '<img src="'.$src.'" '.$p.'/>';
873  print '</a>';
874}
875
876/**
877 * This function inserts a 1x1 pixel gif which in reality
878 * is the inexer function.
879 *
880 * Should be called somewhere at the very end of the main.php
881 * template
882 */
883function tpl_indexerWebBug(){
884  global $ID;
885  global $INFO;
886  if(!$INFO['exists']) return;
887
888  $p = array();
889  $p['src']    = DOKU_BASE.'lib/exe/indexer.php?id='.urlencode($ID).
890                 '&'.time();
891  $p['width']  = 1;
892  $p['height'] = 1;
893  $p['alt']    = '';
894  $att = buildAttributes($p);
895  print "<img $att />";
896}
897
898//Setup VIM: ex: et ts=2 enc=utf-8 :
899