xref: /dokuwiki/inc/template.php (revision 9cc3a670ebed73f52634dd9b0a4f121cf65b5cd9)
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_INC.'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 * Print the content
36 *
37 * This function is used for printing all the usual content
38 * (defined by the global $ACT var) by calling the appropriate
39 * outputfunction(s) from html.php
40 *
41 * Everything that doesn't use the default template isn't
42 * handled by this function. ACL stuff is not done either.
43 *
44 * @author Andreas Gohr <andi@splitbrain.org>
45 */
46function tpl_content(){
47  global $ACT;
48  global $TEXT;
49  global $PRE;
50  global $SUF;
51  global $SUM;
52  global $IDX;
53
54  switch($ACT){
55    case 'show':
56      html_show();
57      break;
58    case 'preview':
59      html_edit($TEXT);
60      html_show($TEXT);
61      break;
62    case 'edit':
63      html_edit();
64      break;
65    case 'wordblock':
66      html_edit($TEXT,'wordblock');
67      break;
68    case 'search':
69      html_search();
70      break;
71    case 'revisions':
72      html_revisions();
73      break;
74    case 'diff':
75      html_diff();
76      break;
77    case 'recent':
78      $first = is_numeric($_REQUEST['first']) ? intval($_REQUEST['first']) : 0;
79      html_recent($first);
80      break;
81    case 'index':
82      html_index($IDX); #FIXME can this be pulled from globals? is it sanitized correctly?
83      break;
84    case 'backlink':
85      html_backlinks();
86      break;
87    case 'conflict':
88      html_conflict(con($PRE,$TEXT,$SUF),$SUM);
89      html_diff(con($PRE,$TEXT,$SUF),false);
90      break;
91    case 'locked':
92      html_locked();
93      break;
94    case 'login':
95      html_login();
96      break;
97    case 'register':
98      html_register();
99      break;
100    case 'denied':
101      print p_locale_xhtml('denied');
102			break;
103    case 'admin':
104      tpl_admin();
105      break;
106    default:
107			msg("Failed to handle command: ".hsc($ACT),-1);
108  }
109}
110
111/**
112 * Handle the admin page contents
113 *
114 * @author Andreas Gohr <andi@splitbrain.org>
115 */
116function tpl_admin(){
117  switch($_REQUEST['page']){
118		case 'acl':
119			admin_acl_html();
120			break;
121    default:
122			html_admin();
123	}
124}
125
126/**
127 * Print the correct HTML meta headers
128 *
129 * This has to go into the head section of your template.
130 *
131 * @author Andreas Gohr <andi@splitbrain.org>
132 */
133function tpl_metaheaders(){
134  global $ID;
135  global $INFO;
136  global $ACT;
137  global $lang;
138  global $conf;
139  $it=2;
140
141  // the usual stuff
142  ptln('<meta name="generator" content="DokuWiki '.getVersion().'" />',$it);
143  ptln('<link rel="start" href="'.DOKU_BASE.'" />',$it);
144  ptln('<link rel="contents" href="'.wl($ID,'do=index').'" title="'.$lang['index'].'" />',$it);
145  ptln('<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="'.DOKU_BASE.'feed.php" />',$it);
146  ptln('<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="'.DOKU_BASE.'feed.php?mode=list&amp;ns='.$INFO['namespace'].'" />',$it);
147  ptln('<link rel="alternate" type="text/html" title="Plain HTML" href="'.wl($ID,'do=export_html').'" />',$it);
148  ptln('<link rel="alternate" type="text/plain" title="Wiki Markup" href="'.wl($ID, 'do=export_raw').'" />',$it);
149  ptln('<link rel="stylesheet" media="screen" type="text/css" href="'.DOKU_BASE.'lib/styles/style.css" />',$it);
150
151  // setup robot tags apropriate for different modes
152  if( ($ACT=='show' || $ACT=='export_html') && !$REV){
153    if($INFO['exists']){
154      ptln('<meta name="date" content="'.date('Y-m-d\TH:i:sO',$INFO['lastmod']).'" />',$it);
155      //delay indexing:
156      if((time() - $INFO['lastmod']) >= $conf['indexdelay']){
157        ptln('<meta name="robots" content="index,follow" />',$it);
158      }else{
159        ptln('<meta name="robots" content="noindex,nofollow" />',$it);
160      }
161    }else{
162      ptln('<meta name="robots" content="noindex,follow" />',$it);
163    }
164  }else{
165    ptln('<meta name="robots" content="noindex,nofollow" />',$it);
166  }
167
168  // include some JavaScript language strings
169  ptln('<script language="javascript" type="text/javascript" charset="utf-8">',$it);
170  ptln("  var alertText   = '".$lang['qb_alert']."'",$it);
171  ptln("  var notSavedYet = '".$lang['notsavedyet']."'",$it);
172  ptln("  var DOKU_BASE   = '".DOKU_BASE."'",$it);
173  ptln('</script>',$it);
174
175  // load the default JavaScript files
176  ptln('<script language="javascript" type="text/javascript" charset="utf-8" src="'.
177       DOKU_BASE.'lib/scripts/script.js"></script>',$it);
178  ptln('<script language="javascript" type="text/javascript" charset="utf-8" src="'.
179       DOKU_BASE.'lib/scripts/tw-sack.js"></script>',$it);
180  ptln('<script language="javascript" type="text/javascript" charset="utf-8" src="'.
181       DOKU_BASE.'lib/scripts/ajax.js"></script>',$it);
182
183  // load spellchecker script if wanted
184  if($conf['spellchecker'] && ($ACT=='edit' || $ACT=='preview')){
185    ptln('<script language="javascript" type="text/javascript" charset="utf-8" src="'.
186       DOKU_BASE.'lib/scripts/spellcheck.js"></script>',$it);
187  }
188
189  //FIXME include some default CSS ? IE FIX?
190}
191
192/**
193 * Print a link
194 *
195 * Just builds a link but adds additional JavaScript needed for
196 * the unsaved data check needed in the edit form.
197 *
198 * @author Andreas Gohr <andi@splitbrain.org>
199 */
200function tpl_link($url,$name,$more=''){
201  print '<a href="'.$url.'" onclick="return svchk()" onkeypress="return svchk()"';
202  if ($more) print ' '.$more;
203  print ">$name</a>";
204}
205
206/**
207 * get the parent page
208 *
209 * Tries to find out which page is parent.
210 * returns false if none is available
211 *
212 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
213 */
214function tpl_getparent($ID){
215  global $conf;
216
217  if ($ID != $conf['start']) {
218    $idparts = explode(':', $ID);
219    $pn = array_pop($idparts);    // get the page name
220
221    for ($n=0; $n < 2; $n++) {
222      if (count($idparts) == 0) {
223        $ID = $conf['start'];     // go to topmost page
224        break;
225      }else{
226        $ns = array_pop($idparts);     // get the last part of namespace
227        if ($pn != $ns) {                 // are we already home?
228          array_push($idparts, $ns, $ns); // no, then add a page with same name
229          $ID = implode (':', $idparts); // as the namespace and recombine $ID
230          break;
231        }
232      }
233    }
234
235    if (@file_exists(wikiFN($ID))) {
236      return $ID;
237    }
238  }
239  return false;
240}
241
242/**
243 * Print one of the buttons
244 *
245 * Available Buttons are
246 *
247 *  edit    - edit/create/show button
248 *  history - old revisions
249 *  recent  - recent changes
250 *  login    - login/logout button - if ACL enabled
251 *  index     - The index
252 *  admin      - admin page - if enough rights
253 *  top         - a back to top button
254 *  back        - a back to parent button - if available
255 *  backtomedia - returns to the mediafile upload dialog
256 *                after references have been displayed
257 *
258 * @author Andreas Gohr <andi@splitbrain.org>
259 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
260 */
261function tpl_button($type){
262  global $ID;
263  global $NS;
264  global $INFO;
265  global $conf;
266
267  switch($type){
268    case 'edit':
269      print html_editbutton();
270      break;
271    case 'history':
272      print html_btn('revs',$ID,'o',array('do' => 'revisions'));
273      break;
274    case 'recent':
275      print html_btn('recent','','r',array('do' => 'recent'));
276      break;
277    case 'index':
278      print html_btn('index',$ID,'x',array('do' => 'index'));
279      break;
280    case 'back':
281      if ($ID = tpl_getparent($ID)) {
282        print html_btn('back',$ID,'b',array('do' => 'show'));
283      }
284      break;
285    case 'top':
286      print html_topbtn();
287      break;
288    case 'login':
289      if($conf['useacl']){
290        if($_SERVER['REMOTE_USER']){
291          print html_btn('logout',$ID,'',array('do' => 'logout',));
292        }else{
293          print html_btn('login',$ID,'',array('do' => 'login'));
294        }
295      }
296      break;
297    case 'admin':
298      if($INFO['perm'] == AUTH_ADMIN)
299        print html_btn('admin',$ID,'',array('do' => 'admin'));
300      break;
301    case 'backtomedia':
302      print html_backtomedia_button(array('ns' => $NS),'b');
303      break;
304		default:
305			print '[unknown button type]';
306  }
307}
308
309/**
310 * Like the action buttons but links
311 *
312 * Available links are
313 *
314 *  edit    - edit/create/show button
315 *  history - old revisions
316 *  recent  - recent changes
317 *  login   - login/logout button - if ACL enabled
318 *  index   - The index
319 *  admin   - admin page - if enough rights
320 *  top     - a back to top button
321 *  back    - a back to parent button - if available
322 *
323 * @author Andreas Gohr <andi@splitbrain.org>
324 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
325 * @see    tpl_button
326 */
327function tpl_actionlink($type,$pre='',$suf=''){
328  global $ID;
329  global $INFO;
330  global $REV;
331  global $ACT;
332  global $conf;
333  global $lang;
334
335  switch($type){
336    case 'edit':
337      #most complicated type - we need to decide on current action
338      if($ACT == 'show' || $ACT == 'search'){
339        if($INFO['writable']){
340          if($INFO['exists']){
341            tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
342                     $pre.$lang['btn_edit'].$suf,
343                     'class="action" accesskey="e" rel="nofollow"');
344          }else{
345            tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
346                     $pre.$lang['btn_create'].$suf,
347                     'class="action" accesskey="e" rel="nofollow"');
348          }
349        }else{
350          tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
351                   $pre.$lang['btn_source'].$suf,
352                   'class="action" accesskey="v" rel="nofollow"');
353        }
354      }else{
355          tpl_link(wl($ID,'do=show'),
356                   $pre.$lang['btn_show'].$suf,
357                   'class="action" accesskey="v" rel="nofollow"');
358      }
359      break;
360    case 'history':
361      tpl_link(wl($ID,'do=revisions'),$pre.$lang['btn_revs'].$suf,'class="action" accesskey="o"');
362      break;
363    case 'recent':
364      tpl_link(wl($ID,'do=recent'),$pre.$lang['btn_recent'].$suf,'class="action" accesskey="r"');
365      break;
366    case 'index':
367      tpl_link(wl($ID,'do=index'),$pre.$lang['btn_index'].$suf,'class="action" accesskey="x"');
368      break;
369    case 'top':
370      print '<a href="#top" class="action" accesskey="x">'.$pre.$lang['btn_top'].$suf.'</a>';
371      break;
372    case 'back':
373      if ($ID = tpl_getparent($ID)) {
374        tpl_link(wl($ID,'do=show'),$pre.$lang['btn_back'].$suf,'class="action" accesskey="b"');
375      }
376      break;
377    case 'login':
378      if($conf['useacl']){
379        if($_SERVER['REMOTE_USER']){
380          tpl_link(wl($ID,'do=logout'),$pre.$lang['btn_logout'].$suf,'class="action"');
381        }else{
382          tpl_link(wl($ID,'do=login'),$pre.$lang['btn_login'].$suf,'class="action"');
383        }
384      }
385      break;
386    case 'admin':
387      if($INFO['perm'] == AUTH_ADMIN)
388        tpl_link(wl($ID,'do=admin'),$pre.$lang['btn_admin'].$suf,'class="action"');
389      break;
390    default:
391      print '[unknown link type]';
392  }
393}
394
395/**
396 * Print the search form
397 *
398 * @author Andreas Gohr <andi@splitbrain.org>
399 */
400function tpl_searchform(){
401  global $lang;
402  global $ACT;
403
404  print '<form action="'.wl().'" accept-charset="utf-8" class="search" name="search" onsubmit="return svchk()">';
405  print '<input type="hidden" name="do" value="search" />';
406  print '<input type="text" ';
407
408  if ($ACT == 'search')
409    print 'value="'.$_REQUEST['id'].'" '; /* keep search input as long as user stays on search page */
410
411  print 'id="qsearch_in" accesskey="f" name="id" class="edit" onkeyup="ajax_qsearch.call(\'qsearch_in\',\'qsearch_out\')" />';
412  print '<input type="submit" value="'.$lang['btn_search'].'" class="button" />';
413  print '<div id="qsearch_out" class="ajax_qsearch" onclick="this.style.display=\'none\'"></div>';
414  print '</form>';
415}
416
417/**
418 * Print the breadcrumbs trace
419 *
420 * @author Andreas Gohr <andi@splitbrain.org>
421 */
422function tpl_breadcrumbs(){
423  global $lang;
424  global $conf;
425
426  //check if enabled
427  if(!$conf['breadcrumbs']) return;
428
429  $crumbs = breadcrumbs(); //setup crumb trace
430
431  //reverse crumborder in right-to-left mode
432  if($lang['direction'] == 'rtl') $crumbs = array_reverse($crumbs,true);
433
434  //render crumbs, highlight the last one
435  print $lang['breadcrumb'].':';
436  $last = count($crumbs);
437  $i = 0;
438  foreach ($crumbs as $id => $name){
439    $i++;
440    print ' <span class="bcsep">&raquo;</span> ';
441    if ($i == $last) print '<span class="curid">';
442    tpl_link(wl($id),$name,'class="breadcrumbs" title="'.$id.'"');
443    if ($i == $last) print '</span>';
444  }
445}
446
447/**
448 * Hierarchical breadcrumbs
449 *
450 * This code was suggested as replacement for the usual breadcrumbs
451 * trail in the Wiki and was modified by me.
452 * It only makes sense with a deep site structure.
453 *
454 * @author Andreas Gohr <andi@splitbrain.org>
455 * @link   http://wiki.splitbrain.org/wiki:tipsandtricks:hierarchicalbreadcrumbs
456 * @todo   May behave starngely in RTL languages
457 */
458function tpl_youarehere(){
459  global $conf;
460  global $ID;
461  global $lang;
462
463
464  $parts     = explode(':', $ID);
465
466  print $lang['breadcrumb'].': ';
467
468  //always print the startpage
469  if( $a_part[0] != $conf['start'] )
470    tpl_link(wl($conf['start']),$conf['start'],'title="'.$conf['start'].'"');
471
472  $page = '';
473  foreach ($parts as $part){
474	  print ' &raquo; ';
475    $page .= $part;
476
477    if(file_exists(wikiFN($page))){
478      tpl_link(wl($page),$part,'title="'.$page.'"');
479    }else{
480      print $page;
481    }
482
483    $page .= ':';
484  }
485}
486
487/**
488 * Print info if the user is logged in
489 * and show full name in that case
490 *
491 * Could be enhanced with a profile link in future?
492 *
493 * @author Andreas Gohr <andi@splitbrain.org>
494 */
495function tpl_userinfo(){
496  global $lang;
497  global $INFO;
498  if($_SERVER['REMOTE_USER'])
499    print $lang['loggedinas'].': '.$INFO['userinfo']['name'];
500}
501
502/**
503 * Print some info about the current page
504 *
505 * @author Andreas Gohr <andi@splitbrain.org>
506 */
507function tpl_pageinfo(){
508  global $conf;
509  global $lang;
510  global $INFO;
511  global $REV;
512
513  // prepare date and path
514  $fn = $INFO['filepath'];
515  if(!$conf['fullpath']){
516    if($REV){
517      $fn = str_replace(realpath($conf['olddir']).DIRECTORY_SEPARATOR,'',$fn);
518    }else{
519      $fn = str_replace(realpath($conf['datadir']).DIRECTORY_SEPARATOR,'',$fn);
520    }
521  }
522  $fn = utf8_decodeFN($fn);
523  $date = date($conf['dformat'],$INFO['lastmod']);
524
525  // print it
526  if($INFO['exists']){
527    print $fn;
528    print ' &middot; ';
529    print $lang['lastmod'];
530    print ': ';
531    print $date;
532    if($INFO['editor']){
533      print ' '.$lang['by'].' ';
534      print $INFO['editor'];
535    }
536    if($INFO['locked']){
537      print ' &middot; ';
538      print $lang['lockedby'];
539      print ': ';
540      print $INFO['locked'];
541    }
542  }
543}
544
545/**
546 * Print a list of namespaces containing media files
547 *
548 * @author Andreas Gohr <andi@splitbrain.org>
549 */
550function tpl_medianamespaces(){
551	global $conf;
552
553  $data = array();
554  search($data,$conf['mediadir'],'search_namespaces',array());
555  print html_buildlist($data,'idx',media_html_list_namespaces);
556}
557
558/**
559 * Print a list of mediafiles in the current namespace
560 *
561 * @author Andreas Gohr <andi@splitbrain.org>
562 */
563function tpl_mediafilelist(){
564  global $conf;
565  global $lang;
566  global $NS;
567  global $AUTH;
568  $dir = utf8_encodeFN(str_replace(':','/',$NS));
569
570  $data = array();
571  search($data,$conf['mediadir'],'search_media',array(),$dir);
572
573  if(!count($data)){
574    ptln('<div class="nothing">'.$lang['nothingfound'].'<div>');
575    return;
576  }
577
578  ptln('<ul>',2);
579  foreach($data as $item){
580    ptln('<li>',4);
581    ptln('<a href="javascript:mediaSelect(\''.$item['id'].'\')">'.
582         utf8_decodeFN($item['file']).
583         '</a>',6);
584
585    //prepare deletion button
586    if($AUTH >= AUTH_DELETE){
587      $ask  = $lang['del_confirm'].'\\n';
588      $ask .= $item['id'];
589
590      $del = '<a href="'.DOKU_BASE.'lib/exe/media.php?delete='.urlencode($item['id']).'" '.
591             'onclick="return confirm(\''.$ask.'\')" onkeypress="return confirm(\''.$ask.'\')">'.
592             '<img src="'.DOKU_BASE.'lib/images/del.png" alt="'.$lang['btn_delete'].'" '.
593             'align="bottom" title="'.$lang['btn_delete'].'" /></a>';
594    }else{
595      $del = '';
596    }
597
598    if($item['isimg']){
599      $w = $item['info'][0];
600      $h = $item['info'][1];
601
602      ptln('('.$w.'&#215;'.$h.' '.filesize_h($item['size']).')',6);
603      ptln($del.'<br />',6);
604      ptln('<a href="javascript:mediaSelect(\''.$item['id'].'\')">');
605
606      if($w>120){
607        print '<img src="'.DOKU_BASE.'lib/exe/fetch.php?w=120&amp;media='.urlencode($item['id']).'" width="120" />';
608      }else{
609        print '<img src="'.DOKU_BASE.'lib/exe/fetch.php?media='.urlencode($item['id']).'" width="'.$w.'" height="'.$h.'" />';
610      }
611      print '</a>';
612
613    }else{
614      ptln ('('.filesize_h($item['size']).')',6);
615      ptln($del,6);
616    }
617    ptln('</li>',4);
618  }
619  ptln('</ul>',2);
620}
621
622/**
623 * show references to a media file
624 * References uses the same visual as search results and share
625 * their CSS tags except pagenames won't be links.
626 *
627 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
628 */
629function tpl_showreferences(&$data){
630  global $lang;
631
632  $hidden=0; //count of hits without read permission
633
634  if(count($data)){
635    usort($data,'sort_search_fulltext');
636    foreach($data as $row){
637      if(auth_quickaclcheck($row['id']) >= AUTH_READ){
638        print '<div class="search_result">';
639        print '<span class="mediaref_ref">'.$row['id'].'</span>';
640        print ': <span class="search_cnt">'.$row['count'].' '.$lang['hits'].'</span><br />';
641        print '<div class="search_snippet">'.$row['snippet'].'</div>';
642        print '</div>';
643      }else
644        $hidden++;
645    }
646    if ($hidden){
647      print '<div class="mediaref_hidden">'.$lang['ref_hidden'].'</div>';
648    }
649  }
650}
651
652/**
653 * Print the media upload form if permissions are correct
654 *
655 * @author Andreas Gohr <andi@splitbrain.org>
656 */
657function tpl_mediauploadform(){
658  global $NS;
659  global $UPLOADOK;
660  global $AUTH;
661  global $lang;
662
663  if(!$UPLOADOK) return;
664
665  ptln('<form action="'.DOKU_BASE.'lib/exe/media.php" name="upload"'.
666       ' method="post" enctype="multipart/form-data">',2);
667  ptln($lang['txt_upload'].':<br />',4);
668  ptln('<input type="file" name="upload" class="edit" onchange="suggestWikiname();" />',4);
669  ptln('<input type="hidden" name="ns" value="'.hsc($NS).'" /><br />',4);
670  ptln($lang['txt_filename'].'<br />',4);
671  ptln('<input type="text" name="id" class="edit" />',4);
672  ptln('<input type="submit" class="button" value="'.$lang['btn_upload'].'" accesskey="s" />',4);
673  if($AUTH >= AUTH_DELETE){
674    ptln('<label for="ow" class="simple"><input type="checkbox" name="ow" value="1" id="ow">'.$lang['txt_overwrt'].'</label>',4);
675  }
676  ptln('</form>',2);
677}
678
679
680//Setup VIM: ex: et ts=2 enc=utf-8 :
681