xref: /dokuwiki/inc/template.php (revision 577a76c97762ccf810098522aa719e1a4d7b80aa)
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 * Returns the path to the given template, uses
14 * default one if the custom version doesn't exist
15 *
16 * @author Andreas Gohr <andi@splitbrain.org>
17 */
18function template($tpl){
19  global $conf;
20
21  if(@is_readable(DOKU_INC.'lib/tpl/'.$conf['template'].'/'.$tpl))
22    return DOKU_INC.'lib/tpl/'.$conf['template'].'/'.$tpl;
23
24  return DOKU_INC.'lib/tpl/default/'.$tpl;
25}
26
27/**
28 * Print the content
29 *
30 * This function is used for printing all the usual content
31 * (defined by the global $ACT var) by calling the appropriate
32 * outputfunction(s) from html.php
33 *
34 * Everything that doesn't use the main template file isn't
35 * handled by this function. ACL stuff is not done here either.
36 *
37 * @author Andreas Gohr <andi@splitbrain.org>
38 */
39function tpl_content() {
40  global $ACT;
41
42  ob_start();
43
44  trigger_event('TPL_ACT_RENDER',$ACT,tpl_content_core);
45
46  $html_output = ob_get_clean();
47
48  trigger_event('TPL_CONTENT_DISPLAY',$html_output,ptln);
49}
50
51function tpl_content_core(){
52  global $ACT;
53  global $TEXT;
54  global $PRE;
55  global $SUF;
56  global $SUM;
57  global $IDX;
58
59  switch($ACT){
60    case 'show':
61      html_show();
62      break;
63    case 'preview':
64      html_edit($TEXT);
65      html_show($TEXT);
66      break;
67    case 'recover':
68      html_edit($TEXT);
69      break;
70    case 'edit':
71      html_edit();
72      break;
73    case 'draft':
74      html_draft();
75      break;
76    case 'wordblock':
77      html_edit($TEXT,'wordblock');
78      break;
79    case 'search':
80      html_search();
81      break;
82    case 'revisions':
83      html_revisions();
84      break;
85    case 'diff':
86      html_diff();
87      break;
88    case 'recent':
89      $first = is_numeric($_REQUEST['first']) ? intval($_REQUEST['first']) : 0;
90      html_recent($first);
91      break;
92    case 'index':
93      html_index($IDX); #FIXME can this be pulled from globals? is it sanitized correctly?
94      break;
95    case 'backlink':
96      html_backlinks();
97      break;
98    case 'conflict':
99      html_conflict(con($PRE,$TEXT,$SUF),$SUM);
100      html_diff(con($PRE,$TEXT,$SUF),false);
101      break;
102    case 'locked':
103      html_locked();
104      break;
105    case 'login':
106      html_login();
107      break;
108    case 'register':
109      html_register();
110      break;
111    case 'resendpwd':
112      html_resendpwd();
113      break;
114    case 'denied':
115      print p_locale_xhtml('denied');
116      break;
117    case 'profile' :
118      html_updateprofile();
119      break;
120    case 'admin':
121      tpl_admin();
122      break;
123    default:
124      $evt = new Doku_Event('TPL_ACT_UNKNOWN',$ACT);
125      if ($evt->advise_before())
126        msg("Failed to handle command: ".hsc($ACT),-1);
127      $evt->advise_after();
128      unset($evt);
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 * @param  boolean $alt Should feeds and alternative format links be added?
162 * @author Andreas Gohr <andi@splitbrain.org>
163 */
164function tpl_metaheaders($alt=true){
165  global $ID;
166  global $INFO;
167  global $ACT;
168  global $lang;
169  global $conf;
170  $it=2;
171
172  // the usual stuff
173  ptln('<meta name="generator" content="DokuWiki '.getVersion().'" />',$it);
174  ptln('<link rel="start" href="'.DOKU_BASE.'" />',$it);
175  ptln('<link rel="contents" href="'.wl($ID,'do=index').'" title="'.$lang['index'].'" />',$it);
176
177  if($alt){
178    ptln('<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="'.DOKU_BASE.'feed.php" />',$it);
179    ptln('<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="'.DOKU_BASE.'feed.php?mode=list&amp;ns='.$INFO['namespace'].'" />',$it);
180    ptln('<link rel="alternate" type="text/html" title="Plain HTML" href="'.exportlink($ID, 'xhtml').'" />',$it);
181    ptln('<link rel="alternate" type="text/plain" title="Wiki Markup" href="'.exportlink($ID, 'raw').'" />',$it);
182  }
183
184  // setup robot tags apropriate for different modes
185  if( ($ACT=='show' || $ACT=='export_xhtml') && !$REV){
186    if($INFO['exists']){
187      ptln('<meta name="date" content="'.date('Y-m-d\TH:i:sO',$INFO['lastmod']).'" />',$it);
188      //delay indexing:
189      if((time() - $INFO['lastmod']) >= $conf['indexdelay']){
190        ptln('<meta name="robots" content="index,follow" />',$it);
191      }else{
192        ptln('<meta name="robots" content="noindex,nofollow" />',$it);
193      }
194    }else{
195      ptln('<meta name="robots" content="noindex,follow" />',$it);
196    }
197  }else{
198    ptln('<meta name="robots" content="noindex,nofollow" />',$it);
199  }
200
201  // load stylesheets
202  ptln('<link rel="stylesheet" media="screen" type="text/css" href="'.DOKU_BASE.'lib/exe/css.php" />',$it);
203  ptln('<link rel="stylesheet" media="print" type="text/css" href="'.DOKU_BASE.'lib/exe/css.php?print=1" />',$it);
204
205  // load javascript
206  $js_edit  = ($ACT=='edit' || $ACT=='preview' || $ACT=='recover') ? 1 : 0;
207  $js_write = ($INFO['writable']) ? 1 : 0;
208  if($js_edit && $js_write){
209    ptln('<script type="text/javascript" charset="utf-8">',$it);
210    ptln("NS='".$INFO['namespace']."';",$it+2);
211    if($conf['useacl'] && $_SERVER['REMOTE_USER']){
212      require_once(DOKU_INC.'inc/toolbar.php');
213      ptln("SIG='".toolbar_signature()."';",$it+2);
214    }
215    ptln('</script>',$it);
216  }
217  ptln('<script type="text/javascript" charset="utf-8" src="'.
218       DOKU_BASE.'lib/exe/js.php?edit='.$js_edit.'&amp;write='.$js_write.'"></script>',$it);
219}
220
221/**
222 * Print a link
223 *
224 * Just builds a link.
225 *
226 * @author Andreas Gohr <andi@splitbrain.org>
227 */
228function tpl_link($url,$name,$more=''){
229  print '<a href="'.$url.'" ';
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/draft 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 *  backlink    - links to the list of backlinks
297 *
298 * @author Andreas Gohr <andi@splitbrain.org>
299 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
300 */
301function tpl_button($type){
302  global $ACT;
303  global $ID;
304  global $NS;
305  global $INFO;
306  global $conf;
307  global $auth;
308
309  switch($type){
310    case 'edit':
311      print html_editbutton();
312      break;
313    case 'history':
314      print html_btn('revs',$ID,'o',array('do' => 'revisions'));
315      break;
316    case 'recent':
317      print html_btn('recent','','r',array('do' => 'recent'));
318      break;
319    case 'index':
320      print html_btn('index',$ID,'x',array('do' => 'index'));
321      break;
322    case 'back':
323      if ($parent = tpl_getparent($ID)) {
324        print html_btn('back',$parent,'b',array('do' => 'show'));
325      }
326      break;
327    case 'top':
328      print html_topbtn();
329      break;
330    case 'login':
331      if($conf['useacl']){
332        if($_SERVER['REMOTE_USER']){
333          print html_btn('logout',$ID,'',array('do' => 'logout',));
334        }else{
335          print html_btn('login',$ID,'',array('do' => 'login'));
336        }
337      }
338      break;
339    case 'admin':
340      if($INFO['perm'] == AUTH_ADMIN)
341        print html_btn('admin',$ID,'',array('do' => 'admin'));
342      break;
343    case 'backtomedia':
344      print html_backtomedia_button(array('ns' => $NS),'b');
345      break;
346    case 'subscription':
347      if($conf['useacl'] && $ACT == 'show' && $conf['subscribers'] == 1){
348        if($_SERVER['REMOTE_USER']){
349          if($INFO['subscribed']){
350            print html_btn('unsubscribe',$ID,'',array('do' => 'unsubscribe',));
351          } else {
352            print html_btn('subscribe',$ID,'',array('do' => 'subscribe',));
353          }
354        }
355      }
356      break;
357    case 'backlink':
358      print html_btn('backlink',$ID,'',array('do' => 'backlink'));
359      break;
360    case 'profile':
361      if($conf['useacl'] && $_SERVER['REMOTE_USER'] &&
362         $auth->canDo('Profile') && ($ACT!='profile')){
363        print html_btn('profile',$ID,'',array('do' => 'profile'));
364      }
365      break;
366    default:
367      print '[unknown button type]';
368  }
369}
370
371/**
372 * Like the action buttons but links
373 *
374 * Available links are
375 *
376 *  edit    - edit/create/show button
377 *  history - old revisions
378 *  recent  - recent changes
379 *  login   - login/logout button - if ACL enabled
380 *  index   - The index
381 *  admin   - admin page - if enough rights
382 *  top     - a back to top button
383 *  back    - a back to parent button - if available
384 * backlink - links to the list of backlinks
385 *
386 * @author Andreas Gohr <andi@splitbrain.org>
387 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
388 * @see    tpl_button
389 */
390function tpl_actionlink($type,$pre='',$suf=''){
391  global $ID;
392  global $INFO;
393  global $REV;
394  global $ACT;
395  global $conf;
396  global $lang;
397  global $auth;
398
399  switch($type){
400    case 'edit':
401      #most complicated type - we need to decide on current action
402      if($ACT == 'show' || $ACT == 'search'){
403        if($INFO['writable']){
404          if($INFO['exists']){
405            tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
406                     $pre.$lang['btn_edit'].$suf,
407                     'class="action edit" accesskey="e" rel="nofollow"');
408          }else{
409            tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
410                     $pre.$lang['btn_create'].$suf,
411                     'class="action create" accesskey="e" rel="nofollow"');
412          }
413        }else{
414          tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
415                   $pre.$lang['btn_source'].$suf,
416                   'class="action source" accesskey="v" rel="nofollow"');
417        }
418      }else{
419          tpl_link(wl($ID,'do=show'),
420                   $pre.$lang['btn_show'].$suf,
421                   'class="action show" accesskey="v" rel="nofollow"');
422      }
423      return true;
424    case 'history':
425      tpl_link(wl($ID,'do=revisions'),$pre.$lang['btn_revs'].$suf,'class="action revisions" accesskey="o"');
426      return true;
427    case 'recent':
428      tpl_link(wl($ID,'do=recent'),$pre.$lang['btn_recent'].$suf,'class="action recent" accesskey="r"');
429      return true;
430    case 'index':
431      tpl_link(wl($ID,'do=index'),$pre.$lang['btn_index'].$suf,'class="action index" accesskey="x"');
432      return true;
433    case 'top':
434      print '<a href="#dokuwiki__top" class="action top" accesskey="x">'.$pre.$lang['btn_top'].$suf.'</a>';
435      return true;
436    case 'back':
437      if ($ID = tpl_getparent($ID)) {
438        tpl_link(wl($ID,'do=show'),$pre.$lang['btn_back'].$suf,'class="action back" accesskey="b"');
439        return true;
440      }
441      return false;
442    case 'login':
443      if($conf['useacl']){
444        if($_SERVER['REMOTE_USER']){
445          tpl_link(wl($ID,'do=logout'),$pre.$lang['btn_logout'].$suf,'class="action logout"');
446        }else{
447          tpl_link(wl($ID,'do=login'),$pre.$lang['btn_login'].$suf,'class="action logout"');
448        }
449        return true;
450      }
451      return false;
452    case 'admin':
453      if($INFO['perm'] == AUTH_ADMIN){
454        tpl_link(wl($ID,'do=admin'),$pre.$lang['btn_admin'].$suf,'class="action admin"');
455        return true;
456      }
457      return false;
458   case 'subscribe':
459   case 'subscription':
460      if($conf['useacl'] && $ACT == 'show' && $conf['subscribers'] == 1){
461        if($_SERVER['REMOTE_USER']){
462          if($INFO['subscribed']) {
463            tpl_link(wl($ID,'do=unsubscribe'),$pre.$lang['btn_unsubscribe'].$suf,'class="action unsubscribe"');
464          } else {
465            tpl_link(wl($ID,'do=subscribe'),$pre.$lang['btn_subscribe'].$suf,'class="action subscribe"');
466          }
467          return true;
468        }
469      }
470      return false;
471    case 'backlink':
472      tpl_link(wl($ID,'do=backlink'),$pre.$lang['btn_backlink'].$suf, 'class="action backlink"');
473      return true;
474    case 'profile':
475      if($conf['useacl'] && $_SERVER['REMOTE_USER'] &&
476         $auth->canDo('Profile') && ($ACT!='profile')){
477        tpl_link(wl($ID,'do=profile'),$pre.$lang['btn_profile'].$suf, 'class="action profile"');
478        return true;
479      }
480      return false;
481    default:
482      print '[unknown link type]';
483      return true;
484  }
485}
486
487/**
488 * Print the search form
489 *
490 * If the first parameter is given a div with the ID 'qsearch_out' will
491 * be added which instructs the ajax pagequicksearch to kick in and place
492 * its output into this div. The second parameter controls the propritary
493 * attribute autocomplete. If set to false this attribute will be set with an
494 * value of "off" to instruct the browser to disable it's own built in
495 * autocompletion feature (MSIE and Firefox)
496 *
497 * @author Andreas Gohr <andi@splitbrain.org>
498 */
499function tpl_searchform($ajax=true,$autocomplete=true){
500  global $lang;
501  global $ACT;
502
503  print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search"><div class="no">';
504  print '<input type="hidden" name="do" value="search" />';
505  print '<input type="text" ';
506  if($ACT == 'search') print 'value="'.htmlspecialchars($_REQUEST['id']).'" ';
507  if(!$autocomplete) print 'autocomplete="off" ';
508  print 'id="qsearch__in" accesskey="f" name="id" class="edit" />';
509  print '<input type="submit" value="'.$lang['btn_search'].'" class="button" />';
510  if($ajax) print '<div id="qsearch__out" class="ajax_qsearch JSpopup"></div>';
511  print '</div></form>';
512}
513
514/**
515 * Print the breadcrumbs trace
516 *
517 * @author Andreas Gohr <andi@splitbrain.org>
518 */
519function tpl_breadcrumbs(){
520  global $lang;
521  global $conf;
522
523  //check if enabled
524  if(!$conf['breadcrumbs']) return;
525
526  $crumbs = breadcrumbs(); //setup crumb trace
527
528  //reverse crumborder in right-to-left mode
529  if($lang['direction'] == 'rtl') $crumbs = array_reverse($crumbs,true);
530
531  //render crumbs, highlight the last one
532  print $lang['breadcrumb'].':';
533  $last = count($crumbs);
534  $i = 0;
535  foreach ($crumbs as $id => $name){
536    $i++;
537    print ' <span class="bcsep">&raquo;</span> ';
538    if ($i == $last) print '<span class="curid">';
539    tpl_link(wl($id),$name,'class="breadcrumbs" title="'.$id.'"');
540    if ($i == $last) print '</span>';
541  }
542}
543
544/**
545 * Hierarchical breadcrumbs
546 *
547 * This code was suggested as replacement for the usual breadcrumbs.
548 * It only makes sense with a deep site structure.
549 *
550 * @author Andreas Gohr <andi@splitbrain.org>
551 * @author Nigel McNie <oracle.shinoda@gmail.com>
552 * @author Sean Coates <sean@caedmon.net>
553 * @link   http://wiki.splitbrain.org/wiki:tipsandtricks:hierarchicalbreadcrumbs
554 * @todo   May behave strangely in RTL languages
555 */
556function tpl_youarehere(){
557  global $conf;
558  global $ID;
559  global $lang;
560
561  //check if enabled
562  if(!$conf['youarehere']) return;
563
564  $parts     = explode(':', $ID);
565
566  print $lang['youarehere'].': ';
567
568  //always print the startpage
569  if( $a_part[0] != $conf['start']){
570    if($conf['useheading']){
571      $pageName = p_get_first_heading($conf['start']);
572    }else{
573      $pageName = $conf['start'];
574    }
575    tpl_link(wl($conf['start']),$pageName,'title="'.$pageName.'"');
576  }
577
578  $page = '';
579  foreach ($parts as $part){
580        // Skip startpage if already done
581        if ($part == $conf['start']) continue;
582
583          print ' &raquo; ';
584    $page .= $part;
585
586    if(file_exists(wikiFN($page))){
587      if($conf['useheading']){
588        $pageName = p_get_first_heading($page);
589        $partName = $pageName;
590      }else{
591        $pageName = $page;
592        $partName = $part;
593      }
594      tpl_link(wl($page),$partName,'title="'.$pageName.'"');
595    }else{
596      // Print the link, but mark as not-existing, as for other non-existing links
597      tpl_link(wl($page),$part,'title="'.$page.'" class="wikilink2"');
598      //print $page;
599    }
600
601    $page .= ':';
602  }
603}
604
605/**
606 * Print info if the user is logged in
607 * and show full name in that case
608 *
609 * Could be enhanced with a profile link in future?
610 *
611 * @author Andreas Gohr <andi@splitbrain.org>
612 */
613function tpl_userinfo(){
614  global $lang;
615  global $INFO;
616  if($_SERVER['REMOTE_USER'])
617    print $lang['loggedinas'].': '.$INFO['userinfo']['name'];
618}
619
620/**
621 * Print some info about the current page
622 *
623 * @author Andreas Gohr <andi@splitbrain.org>
624 */
625function tpl_pageinfo(){
626  global $conf;
627  global $lang;
628  global $INFO;
629  global $REV;
630
631  // prepare date and path
632  $fn = $INFO['filepath'];
633  if(!$conf['fullpath']){
634    if($REV){
635      $fn = str_replace(realpath($conf['olddir']).DIRECTORY_SEPARATOR,'',$fn);
636    }else{
637      $fn = str_replace(realpath($conf['datadir']).DIRECTORY_SEPARATOR,'',$fn);
638    }
639  }
640  $fn = utf8_decodeFN($fn);
641  $date = date($conf['dformat'],$INFO['lastmod']);
642
643  // print it
644  if($INFO['exists']){
645    print $fn;
646    print ' &middot; ';
647    print $lang['lastmod'];
648    print ': ';
649    print $date;
650    if($INFO['editor']){
651      print ' '.$lang['by'].' ';
652      print $INFO['editor'];
653    }
654    if($INFO['locked']){
655      print ' &middot; ';
656      print $lang['lockedby'];
657      print ': ';
658      print $INFO['locked'];
659    }
660  }
661}
662
663/**
664 * Print a list of namespaces containing media files
665 *
666 * @author Andreas Gohr <andi@splitbrain.org>
667 */
668function tpl_medianamespaces(){
669    global $conf;
670
671  $data = array();
672  search($data,$conf['mediadir'],'search_namespaces',array());
673  print html_buildlist($data,'idx',media_html_list_namespaces);
674}
675
676/**
677 * Print a list of mediafiles in the current namespace
678 *
679 * @author Andreas Gohr <andi@splitbrain.org>
680 */
681function tpl_mediafilelist(){
682  global $conf;
683  global $lang;
684  global $NS;
685  global $AUTH;
686  $dir = utf8_encodeFN(str_replace(':','/',$NS));
687
688  $data = array();
689  search($data,$conf['mediadir'],'search_media',array(),$dir);
690
691  if(!count($data)){
692    ptln('<div class="nothing">'.$lang['nothingfound'].'</div>');
693    return;
694  }
695
696  ptln('<ul>',2);
697  foreach($data as $item){
698    if(!$item['isimg']){
699      // add file icons
700      list($ext,$mime) = mimetype($item['file']);
701      $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
702      $class = ' class="mediafile mf_'.$class.'"';
703    }else{
704      $class = '';
705    }
706
707    ptln('<li><div class="li">',4);
708    ptln('<a href="javascript:mediaSelect(\':'.$item['id'].'\')"'.$class.'>'.
709         utf8_decodeFN($item['file']).
710         '</a>',6);
711
712    //prepare deletion button
713    if($AUTH >= AUTH_DELETE){
714      $ask  = addslashes($lang['del_confirm']).'\\n';
715      $ask .= addslashes($item['id']);
716
717      $del = '<a href="'.DOKU_BASE.'lib/exe/media.php?delete='.rawurlencode($item['id']).'" '.
718             'onclick="return confirm(\''.$ask.'\')" onkeypress="return confirm(\''.$ask.'\')">'.
719             '<img src="'.DOKU_BASE.'lib/images/del.png" alt="'.$lang['btn_delete'].'" '.
720             'title="'.$lang['btn_delete'].'" /></a>';
721    }else{
722      $del = '';
723    }
724
725    if($item['isimg']){
726      $w = (int) $item['meta']->getField('File.Width');
727      $h = (int) $item['meta']->getField('File.Height');
728
729      ptln('('.$w.'&#215;'.$h.' '.filesize_h($item['size']).')',6);
730      ptln($del.'<br />',6);
731      ptln('<div class="imagemeta">',6);
732
733      //build thumbnail
734      print '<a href="javascript:mediaSelect(\':'.$item['id'].'\')">';
735
736      if($w>120 || $h>120){
737        $ratio = $item['meta']->getResizeRatio(120);
738        $w = floor($w * $ratio);
739        $h = floor($h * $ratio);
740      }
741
742      $src = ml($item['id'],array('w'=>$w,'h'=>$h));
743
744      $p = array();
745      $p['width']  = $w;
746      $p['height'] = $h;
747      $p['alt']    = $item['id'];
748      $p['class']  = 'thumb';
749      $att = buildAttributes($p);
750
751      print '<img src="'.$src.'" '.$att.' />';
752      print '</a>';
753
754      //read EXIF/IPTC data
755      $t = $item['meta']->getField('IPTC.Headline');
756      if($t) print '<strong>'.htmlspecialchars($t).'</strong><br />';
757
758      $t = $item['meta']->getField(array('IPTC.Caption','EXIF.UserComment',
759                                         'EXIF.TIFFImageDescription',
760                                         'EXIF.TIFFUserComment'));
761      if($t) print htmlspecialchars($t).'<br />';
762
763      $t = $item['meta']->getField(array('IPTC.Keywords','IPTC.Category'));
764      if($t) print '<em>'.htmlspecialchars($t).'</em><br />';
765
766      //add edit button
767      if($AUTH >= AUTH_UPLOAD && $item['meta']->getField('File.Mime') == 'image/jpeg'){
768        print '<a href="'.DOKU_BASE.'lib/exe/media.php?edit='.rawurlencode($item['id']).'">';
769        print '<img src="'.DOKU_BASE.'lib/images/edit.gif" alt="'.$lang['metaedit'].'" title="'.$lang['metaedit'].'" />';
770        print '</a>';
771      }
772
773      ptln('</div>',6);
774    }else{
775      ptln ('('.filesize_h($item['size']).')',6);
776      ptln($del,6);
777    }
778    ptln('</div></li>',4);
779  }
780  ptln('</ul>',2);
781}
782
783/**
784 * show references to a media file
785 * References uses the same visual as search results and share
786 * their CSS tags except pagenames won't be links.
787 *
788 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
789 */
790function tpl_showreferences(&$data){
791  global $lang;
792
793  $hidden=0; //count of hits without read permission
794
795  if(count($data)){
796    usort($data,'sort_search_fulltext');
797    foreach($data as $row){
798      if(auth_quickaclcheck($row['id']) >= AUTH_READ){
799        print '<div class="search_result">';
800        print '<span class="mediaref_ref">'.$row['id'].'</span>';
801        print ': <span class="search_cnt">'.$row['count'].' '.$lang['hits'].'</span><br />';
802        print '<div class="search_snippet">'.$row['snippet'].'</div>';
803        print '</div>';
804      }else
805        $hidden++;
806    }
807    if ($hidden){
808      print '<div class="mediaref_hidden">'.$lang['ref_hidden'].'</div>';
809    }
810  }
811}
812
813/**
814 * Print the media upload form if permissions are correct
815 *
816 * @author Andreas Gohr <andi@splitbrain.org>
817 */
818function tpl_mediauploadform(){
819  global $NS;
820  global $UPLOADOK;
821  global $AUTH;
822  global $lang;
823
824  if(!$UPLOADOK) return;
825
826  ptln('<form action="'.DOKU_BASE.'lib/exe/media.php" id="dw__upload"'.
827       ' method="post" enctype="multipart/form-data">',2);
828  ptln($lang['txt_upload'].':<br />',4);
829  ptln('<input type="file" name="upload" class="edit" onchange="suggestWikiname();" />',4);
830  ptln('<input type="hidden" name="ns" value="'.hsc($NS).'" /><br />',4);
831  ptln($lang['txt_filename'].'<br />',4);
832  ptln('<input type="text" name="id" class="edit" />',4);
833  ptln('<input type="submit" class="button" value="'.$lang['btn_upload'].'" accesskey="s" />',4);
834  if($AUTH >= AUTH_DELETE){
835    ptln('<label for="dw__ow"><input type="checkbox" name="ow" value="1" id="dw__ow" />'.$lang['txt_overwrt'].'</label>',4);
836  }
837  ptln('</form>',2);
838}
839
840/**
841 * Prints or returns the name of the given page (current one if none given).
842 *
843 * If useheading is enabled this will use the first headline else
844 * the given ID is used.
845 *
846 * @author Andreas Gohr <andi@splitbrain.org>
847 */
848function tpl_pagetitle($id=null, $ret=false){
849  global $conf;
850  if(is_null($id)){
851    global $ID;
852    $id = $ID;
853  }
854
855  $name = $id;
856  if ($conf['useheading']) {
857    $title = p_get_first_heading($id);
858    if ($title) $name = $title;
859  }
860
861  if ($ret) {
862      return hsc($name);
863  } else {
864      print hsc($name);
865  }
866}
867
868/**
869 * Returns the requested EXIF/IPTC tag from the current image
870 *
871 * If $tags is an array all given tags are tried until a
872 * value is found. If no value is found $alt is returned.
873 *
874 * Which texts are known is defined in the functions _exifTagNames
875 * and _iptcTagNames() in inc/jpeg.php (You need to prepend IPTC
876 * to the names of the latter one)
877 *
878 * Only allowed in: detail.php, mediaedit.php
879 *
880 * @author Andreas Gohr <andi@splitbrain.org>
881 */
882function tpl_img_getTag($tags,$alt=''){
883  // Init Exif Reader
884  global $SRC;
885  static $meta = null;
886  if(is_null($meta)) $meta = new JpegMeta($SRC);
887  if($meta === false) return $alt;
888  $info = $meta->getField($tags);
889  if($info == false) return $alt;
890  return $info;
891}
892
893/**
894 * Prints the image with a link to the full sized version
895 *
896 * Only allowed in: detail.php
897 */
898function tpl_img($maxwidth=0,$maxheight=0){
899  global $IMG;
900  $w = tpl_img_getTag('File.Width');
901  $h = tpl_img_getTag('File.Height');
902
903  //resize to given max values
904  $ratio = 1;
905  if($w >= $h){
906    if($maxwidth && $w >= $maxwidth){
907      $ratio = $maxwidth/$w;
908    }elseif($maxheight && $h > $maxheight){
909      $ratio = $maxheight/$h;
910    }
911  }else{
912    if($maxheight && $h >= $maxheight){
913      $ratio = $maxheight/$h;
914    }elseif($maxwidth && $w > $maxwidth){
915      $ratio = $maxwidth/$w;
916    }
917  }
918  if($ratio){
919    $w = floor($ratio*$w);
920    $h = floor($ratio*$h);
921  }
922
923  //prepare URLs
924  $url=ml($IMG,array('cache'=>$_REQUEST['cache']));
925  $src=ml($IMG,array('cache'=>$_REQUEST['cache'],'w'=>$w,'h'=>$h));
926
927  //prepare attributes
928  $alt=tpl_img_getTag('Simple.Title');
929  $p = array();
930  if($w) $p['width']  = $w;
931  if($h) $p['height'] = $h;
932         $p['class']  = 'img_detail';
933  if($alt){
934    $p['alt']   = $alt;
935    $p['title'] = $alt;
936  }else{
937    $p['alt'] = '';
938  }
939  $p = buildAttributes($p);
940
941  print '<a href="'.$url.'">';
942  print '<img src="'.$src.'" '.$p.'/>';
943  print '</a>';
944}
945
946/**
947 * This function inserts a 1x1 pixel gif which in reality
948 * is the inexer function.
949 *
950 * Should be called somewhere at the very end of the main.php
951 * template
952 */
953function tpl_indexerWebBug(){
954  global $ID;
955  global $INFO;
956  if(!$INFO['exists']) return;
957
958  if(isHiddenPage($ID)) return; //no need to index hidden pages
959
960  $p = array();
961  $p['src']    = DOKU_BASE.'lib/exe/indexer.php?id='.rawurlencode($ID).
962                 '&'.time();
963  $p['width']  = 1;
964  $p['height'] = 1;
965  $p['alt']    = '';
966  $att = buildAttributes($p);
967  print "<img $att />";
968}
969
970// configuration methods
971/**
972 * tpl_getConf($id)
973 *
974 * use this function to access template configuration variables
975 */
976function tpl_getConf($id){
977  global $conf;
978  global $tpl_configloaded;
979
980  $tpl = $conf['template'];
981
982  if (!$tpl_configloaded){
983    $tconf = tpl_loadConfig();
984    if ($tconf !== false){
985      foreach ($tconf as $key => $value){
986        if (isset($conf['tpl'][$tpl][$key])) continue;
987        $conf['tpl'][$tpl][$key] = $value;
988      }
989      $tpl_configloaded = true;
990    }
991  }
992
993  return $conf['tpl'][$tpl][$id];
994}
995
996/**
997 * tpl_loadConfig()
998 * reads all template configuration variables
999 * this function is automatically called by tpl_getConf()
1000 */
1001function tpl_loadConfig(){
1002
1003  $file = DOKU_TPLINC.'/conf/default.php';
1004  $conf = array();
1005
1006  if (!@file_exists($file)) return false;
1007
1008  // load default config file
1009  include($file);
1010
1011  return $conf;
1012}
1013
1014//Setup VIM: ex: et ts=2 enc=utf-8 :
1015