xref: /dokuwiki/inc/template.php (revision 1380fc452d56dd6f48ddbfa3a6b0e69edd043b04)
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    case 'track':
122      html_track();
123      break;
124    case 'ignore':
125      html_ignore();
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  switch($_REQUEST['page']){
139		case 'acl':
140			admin_acl_html();
141			break;
142    default:
143			html_admin();
144	}
145}
146
147/**
148 * Print the correct HTML meta headers
149 *
150 * This has to go into the head section of your template.
151 *
152 * @author Andreas Gohr <andi@splitbrain.org>
153 */
154function tpl_metaheaders(){
155  global $ID;
156  global $INFO;
157  global $ACT;
158  global $lang;
159  global $conf;
160  $it=2;
161
162  // the usual stuff
163  ptln('<meta name="generator" content="DokuWiki '.getVersion().'" />',$it);
164  ptln('<link rel="start" href="'.DOKU_BASE.'" />',$it);
165  ptln('<link rel="contents" href="'.wl($ID,'do=index').'" title="'.$lang['index'].'" />',$it);
166  ptln('<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="'.DOKU_BASE.'feed.php" />',$it);
167  ptln('<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="'.DOKU_BASE.'feed.php?mode=list&amp;ns='.$INFO['namespace'].'" />',$it);
168  ptln('<link rel="alternate" type="text/html" title="Plain HTML" href="'.wl($ID,'do=export_html').'" />',$it);
169  ptln('<link rel="alternate" type="text/plain" title="Wiki Markup" href="'.wl($ID, 'do=export_raw').'" />',$it);
170  ptln('<link rel="stylesheet" media="screen" type="text/css" href="'.DOKU_BASE.'lib/styles/style.css" />',$it);
171
172  // setup robot tags apropriate for different modes
173  if( ($ACT=='show' || $ACT=='export_html') && !$REV){
174    if($INFO['exists']){
175      ptln('<meta name="date" content="'.date('Y-m-d\TH:i:sO',$INFO['lastmod']).'" />',$it);
176      //delay indexing:
177      if((time() - $INFO['lastmod']) >= $conf['indexdelay']){
178        ptln('<meta name="robots" content="index,follow" />',$it);
179      }else{
180        ptln('<meta name="robots" content="noindex,nofollow" />',$it);
181      }
182    }else{
183      ptln('<meta name="robots" content="noindex,follow" />',$it);
184    }
185  }else{
186    ptln('<meta name="robots" content="noindex,nofollow" />',$it);
187  }
188
189  // include some JavaScript language strings
190  ptln('<script language="javascript" type="text/javascript" charset="utf-8">',$it);
191  ptln("  var alertText   = '".$lang['qb_alert']."'",$it);
192  ptln("  var notSavedYet = '".$lang['notsavedyet']."'",$it);
193  ptln("  var DOKU_BASE   = '".DOKU_BASE."'",$it);
194  ptln('</script>',$it);
195
196  // load the default JavaScript files
197  ptln('<script language="javascript" type="text/javascript" charset="utf-8" src="'.
198       DOKU_BASE.'lib/scripts/script.js"></script>',$it);
199  ptln('<script language="javascript" type="text/javascript" charset="utf-8" src="'.
200       DOKU_BASE.'lib/scripts/tw-sack.js"></script>',$it);
201  ptln('<script language="javascript" type="text/javascript" charset="utf-8" src="'.
202       DOKU_BASE.'lib/scripts/ajax.js"></script>',$it);
203
204  // load spellchecker script if wanted
205  if($conf['spellchecker'] && ($ACT=='edit' || $ACT=='preview')){
206    ptln('<script language="javascript" type="text/javascript" charset="utf-8" src="'.
207       DOKU_BASE.'lib/scripts/spellcheck.js"></script>',$it);
208  }
209
210  // dom tool tip library, for insitu footnotes
211  ptln('<script language="javascript" type="text/javascript" charset="utf-8" src="'.
212       DOKU_BASE.'lib/scripts/domLib.js"></script>',$it);
213  ptln('<script language="javascript" type="text/javascript" charset="utf-8" src="'.
214       DOKU_BASE.'lib/scripts/domTT.js"></script>',$it);
215
216  // plugin stylesheets and Scripts
217  plugin_printCSSJS();
218}
219
220/**
221 * Print a link
222 *
223 * Just builds a link but adds additional JavaScript needed for
224 * the unsaved data check needed in the edit form.
225 *
226 * @author Andreas Gohr <andi@splitbrain.org>
227 */
228function tpl_link($url,$name,$more=''){
229  print '<a href="'.$url.'" onclick="return svchk()" onkeypress="return svchk()"';
230  if ($more) print ' '.$more;
231  print ">$name</a>";
232}
233
234/**
235 * Prints a link to a WikiPage
236 *
237 * Wrapper around html_wikilink
238 *
239 * @author Andreas Gohr <andi@splitbrain.org>
240 */
241function tpl_pagelink($id,$name=NULL){
242  print html_wikilink($id,$name);
243}
244
245/**
246 * get the parent page
247 *
248 * Tries to find out which page is parent.
249 * returns false if none is available
250 *
251 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
252 */
253function tpl_getparent($ID){
254  global $conf;
255
256  if ($ID != $conf['start']) {
257    $idparts = explode(':', $ID);
258    $pn = array_pop($idparts);    // get the page name
259
260    for ($n=0; $n < 2; $n++) {
261      if (count($idparts) == 0) {
262        $ID = $conf['start'];     // go to topmost page
263        break;
264      }else{
265        $ns = array_pop($idparts);     // get the last part of namespace
266        if ($pn != $ns) {                 // are we already home?
267          array_push($idparts, $ns, $ns); // no, then add a page with same name
268          $ID = implode (':', $idparts); // as the namespace and recombine $ID
269          break;
270        }
271      }
272    }
273
274    if (@file_exists(wikiFN($ID))) {
275      return $ID;
276    }
277  }
278  return false;
279}
280
281/**
282 * Print one of the buttons
283 *
284 * Available Buttons are
285 *
286 *  edit    - edit/create/show button
287 *  history - old revisions
288 *  recent  - recent changes
289 *  login    - login/logout button - if ACL enabled
290 *  index     - The index
291 *  admin      - admin page - if enough rights
292 *  top         - a back to top button
293 *  back        - a back to parent button - if available
294 *  backtomedia - returns to the mediafile upload dialog
295 *                after references have been displayed
296 *
297 * @author Andreas Gohr <andi@splitbrain.org>
298 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
299 */
300function tpl_button($type){
301  global $ACT;
302  global $ID;
303  global $NS;
304  global $INFO;
305  global $conf;
306
307  switch($type){
308    case 'edit':
309      print html_editbutton();
310      break;
311    case 'history':
312      print html_btn('revs',$ID,'o',array('do' => 'revisions'));
313      break;
314    case 'recent':
315      print html_btn('recent','','r',array('do' => 'recent'));
316      break;
317    case 'index':
318      print html_btn('index',$ID,'x',array('do' => 'index'));
319      break;
320    case 'back':
321      if ($ID = tpl_getparent($ID)) {
322        print html_btn('back',$ID,'b',array('do' => 'show'));
323      }
324      break;
325    case 'top':
326      print html_topbtn();
327      break;
328    case 'login':
329      if($conf['useacl']){
330        if($_SERVER['REMOTE_USER']){
331          print html_btn('logout',$ID,'',array('do' => 'logout',));
332        }else{
333          print html_btn('login',$ID,'',array('do' => 'login'));
334        }
335      }
336      break;
337    case 'admin':
338      if($INFO['perm'] == AUTH_ADMIN)
339        print html_btn('admin',$ID,'',array('do' => 'admin'));
340      break;
341    case 'backtomedia':
342      print html_backtomedia_button(array('ns' => $NS),'b');
343      break;
344    case 'subscription':
345      if($conf['useacl'] && $ACT == 'show'){
346        if($_SERVER['REMOTE_USER']){
347          if($INFO['subscribed']){
348            print html_btn('unsubscribe',$ID,'',array('do' => 'unsubscribe',));
349          } else {
350            print html_btn('subscribe',$ID,'',array('do' => 'subscribe',));
351          }
352        }
353      }
354      break;
355		default:
356			print '[unknown button type]';
357  }
358}
359
360/**
361 * Like the action buttons but links
362 *
363 * Available links are
364 *
365 *  edit    - edit/create/show button
366 *  history - old revisions
367 *  recent  - recent changes
368 *  login   - login/logout button - if ACL enabled
369 *  index   - The index
370 *  admin   - admin page - if enough rights
371 *  top     - a back to top button
372 *  back    - a back to parent button - if available
373 *
374 * @author Andreas Gohr <andi@splitbrain.org>
375 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
376 * @see    tpl_button
377 */
378function tpl_actionlink($type,$pre='',$suf=''){
379  global $ID;
380  global $INFO;
381  global $REV;
382  global $ACT;
383  global $conf;
384  global $lang;
385
386  switch($type){
387    case 'edit':
388      #most complicated type - we need to decide on current action
389      if($ACT == 'show' || $ACT == 'search'){
390        if($INFO['writable']){
391          if($INFO['exists']){
392            tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
393                     $pre.$lang['btn_edit'].$suf,
394                     'class="action" accesskey="e" rel="nofollow"');
395          }else{
396            tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
397                     $pre.$lang['btn_create'].$suf,
398                     'class="action" accesskey="e" rel="nofollow"');
399          }
400        }else{
401          tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
402                   $pre.$lang['btn_source'].$suf,
403                   'class="action" accesskey="v" rel="nofollow"');
404        }
405      }else{
406          tpl_link(wl($ID,'do=show'),
407                   $pre.$lang['btn_show'].$suf,
408                   'class="action" accesskey="v" rel="nofollow"');
409      }
410      break;
411    case 'history':
412      tpl_link(wl($ID,'do=revisions'),$pre.$lang['btn_revs'].$suf,'class="action" accesskey="o"');
413      break;
414    case 'recent':
415      tpl_link(wl($ID,'do=recent'),$pre.$lang['btn_recent'].$suf,'class="action" accesskey="r"');
416      break;
417    case 'index':
418      tpl_link(wl($ID,'do=index'),$pre.$lang['btn_index'].$suf,'class="action" accesskey="x"');
419      break;
420    case 'top':
421      print '<a href="#top" class="action" accesskey="x">'.$pre.$lang['btn_top'].$suf.'</a>';
422      break;
423    case 'back':
424      if ($ID = tpl_getparent($ID)) {
425        tpl_link(wl($ID,'do=show'),$pre.$lang['btn_back'].$suf,'class="action" accesskey="b"');
426      }
427      break;
428    case 'login':
429      if($conf['useacl']){
430        if($_SERVER['REMOTE_USER']){
431          tpl_link(wl($ID,'do=logout'),$pre.$lang['btn_logout'].$suf,'class="action"');
432        }else{
433          tpl_link(wl($ID,'do=login'),$pre.$lang['btn_login'].$suf,'class="action"');
434        }
435      }
436      break;
437    case 'admin':
438      if($INFO['perm'] == AUTH_ADMIN)
439        tpl_link(wl($ID,'do=admin'),$pre.$lang['btn_admin'].$suf,'class="action"');
440      break;
441   case 'track':
442      if($conf['useacl'] && $ACT == 'show'){
443        if($_SERVER['REMOTE_USER']){
444          if(tracking($ID,$_SERVER['REMOTE_USER'])){
445            tpl_link(wl($ID,'do=ignore'),$pre.$lang['btn_ignore'].$suf,'class="action"');
446          } else {
447            tpl_link(wl($ID,'do=track'),$pre.$lang['btn_track'].$suf,'class="action"');
448          }
449        }
450      }
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      $p = array();
678      $p['w'] = $w;
679      $p['h'] = $h;
680      $p['media'] = $item['id'];
681      $src = DOKU_BASE.'lib/exe/fetch.php?'.buildURLParams($p);
682
683      $p = array();
684      $p['width']  = $w;
685      $p['height'] = $h;
686      $p['alt']    = $item['id'];
687      $p['class']  = 'thumb';
688      $att = buildAttributes($p);
689
690      print '<img src="'.$src.'" '.$att.' />';
691      print '</a>';
692
693      //read EXIF/IPTC data
694      $t = $item['meta']->getField('IPTC.Headline');
695      if($t) print '<b>'.$t.'</b><br />';
696
697      $t = $item['meta']->getField(array('IPTC.Caption','EXIF.UserComment',
698                                         'EXIF.TIFFImageDescription',
699                                         'EXIF.TIFFUserComment'));
700      if($t) print $t.'<br />';
701
702      $t = $item['meta']->getField(array('IPTC.Keywords','IPTC.Category'));
703      if($t) print '<i>'.$t.'</i><br />';
704
705      //add edit button
706      if($AUTH >= AUTH_UPLOAD && $item['meta']->getField('File.Mime') == 'image/jpeg'){
707        print '<a href="'.DOKU_BASE.'lib/exe/media.php?edit='.urlencode($item['id']).'">';
708        print '<img src="'.DOKU_BASE.'lib/images/edit.gif" alt="'.$lang['metaedit'].'" title="'.$lang['metaedit'].'" />';
709        print '</a>';
710      }
711
712      ptln('</div>',6);
713    }else{
714      ptln ('('.filesize_h($item['size']).')',6);
715      ptln($del,6);
716    }
717    ptln('</li>',4);
718  }
719  ptln('</ul>',2);
720}
721
722/**
723 * show references to a media file
724 * References uses the same visual as search results and share
725 * their CSS tags except pagenames won't be links.
726 *
727 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
728 */
729function tpl_showreferences(&$data){
730  global $lang;
731
732  $hidden=0; //count of hits without read permission
733
734  if(count($data)){
735    usort($data,'sort_search_fulltext');
736    foreach($data as $row){
737      if(auth_quickaclcheck($row['id']) >= AUTH_READ){
738        print '<div class="search_result">';
739        print '<span class="mediaref_ref">'.$row['id'].'</span>';
740        print ': <span class="search_cnt">'.$row['count'].' '.$lang['hits'].'</span><br />';
741        print '<div class="search_snippet">'.$row['snippet'].'</div>';
742        print '</div>';
743      }else
744        $hidden++;
745    }
746    if ($hidden){
747      print '<div class="mediaref_hidden">'.$lang['ref_hidden'].'</div>';
748    }
749  }
750}
751
752/**
753 * Print the media upload form if permissions are correct
754 *
755 * @author Andreas Gohr <andi@splitbrain.org>
756 */
757function tpl_mediauploadform(){
758  global $NS;
759  global $UPLOADOK;
760  global $AUTH;
761  global $lang;
762
763  if(!$UPLOADOK) return;
764
765  ptln('<form action="'.DOKU_BASE.'lib/exe/media.php" name="upload"'.
766       ' method="post" enctype="multipart/form-data">',2);
767  ptln($lang['txt_upload'].':<br />',4);
768  ptln('<input type="file" name="upload" class="edit" onchange="suggestWikiname();" />',4);
769  ptln('<input type="hidden" name="ns" value="'.hsc($NS).'" /><br />',4);
770  ptln($lang['txt_filename'].'<br />',4);
771  ptln('<input type="text" name="id" class="edit" />',4);
772  ptln('<input type="submit" class="button" value="'.$lang['btn_upload'].'" accesskey="s" />',4);
773  if($AUTH >= AUTH_DELETE){
774    ptln('<label for="ow" class="simple"><input type="checkbox" name="ow" value="1" id="ow">'.$lang['txt_overwrt'].'</label>',4);
775  }
776  ptln('</form>',2);
777}
778
779/**
780 * Prints the name of the given page (current one if none given).
781 *
782 * If useheading is enabled this will use the first headline else
783 * the given ID is printed.
784 *
785 * @author Andreas Gohr <andi@splitbrain.org>
786 */
787function tpl_pagetitle($id=null){
788  global $conf;
789  if(is_null($id)){
790    global $ID;
791    $id = $ID;
792  }
793
794  $name = $id;
795  if ($conf['useheading']) {
796    $title = p_get_first_heading($id);
797    if ($title) $name = $title;
798  }
799  print hsc($name);
800}
801
802/**
803 * Returns the requested EXIF/IPTC tag from the current image
804 *
805 * If $tags is an array all given tags are tried until a
806 * value is found. If no value is found $alt is returned.
807 *
808 * Which texts are known is defined in the functions _exifTagNames
809 * and _iptcTagNames() in inc/jpeg.php (You need to prepend IPTC
810 * to the names of the latter one)
811 *
812 * Only allowed in: detail.php, mediaedit.php
813 *
814 * @author Andreas Gohr <andi@splitbrain.org>
815 */
816function tpl_img_getTag($tags,$alt=''){
817  // Init Exif Reader
818  global $SRC;
819  static $meta = null;
820  if(is_null($meta)) $meta = new JpegMeta($SRC);
821  if($meta === false) return $alt;
822  $info = $meta->getField($tags);
823  if($info == false) return $alt;
824  return $info;
825}
826
827/**
828 * Prints the image with a link to the full sized version
829 *
830 * Only allowed in: detail.php
831 */
832function tpl_img($maxwidth=900,$maxheight=700){
833  global $IMG;
834  $w = tpl_img_getTag('File.Width');
835  $h = tpl_img_getTag('File.Height');
836
837  //resize to given max values
838  $ratio = 1;
839  if($w >= $h){
840    if($w >= $maxwidth){
841      $ratio = $maxwidth/$w;
842    }elseif($h > $maxheight){
843      $ratio = $maxheight/$h;
844    }
845  }else{
846    if($h >= $maxheight){
847      $ratio = $maxheight/$h;
848    }elseif($w > $maxwidth){
849      $ratio = $maxwidth/$w;
850    }
851  }
852  if($ratio){
853    $w = floor($ratio*$w);
854    $h = floor($ratio*$h);
855  }
856
857  //prepare URL
858  $p = array();
859  $p['cache'] = $_REQUEST['cache'];
860  $p['media'] = $IMG;
861  $p = buildURLparams($p);
862  $url=DOKU_BASE.'lib/exe/fetch.php?'.$p;
863
864  //prepare attributes
865  $alt=tpl_img_getTag('Simple.Title');
866  $p = array();
867  if($w) $p['width']  = $w;
868  if($h) $p['height'] = $h;
869         $p['class']  = 'img_detail';
870  if($alt){
871    $p['alt']   = $alt;
872    $p['title'] = $alt;
873  }else{
874    $p['alt'] = '';
875  }
876  $p = buildAttributes($p);
877
878  print '<a href="'.$url.'">';
879  print '<img src="'.$url.'&amp;w='.$w.'&amp;h='.$w.'" '.$p.'/>';
880  print '</a>';
881}
882
883//Setup VIM: ex: et ts=2 enc=utf-8 :
884