xref: /dokuwiki/inc/template.php (revision bc3b6aec0f5bdef988488010807a94bee0808426)
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  $it=2;
139
140  // the usual stuff
141  ptln('<meta name="generator" content="DokuWiki '.getVersion().'" />',$it);
142  ptln('<link rel="start" href="'.DOKU_BASE.'" />',$it);
143  ptln('<link rel="contents" href="'.wl($ID,'do=index').'" title="'.$lang['index'].'" />',$it);
144  ptln('<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="'.DOKU_BASE.'feed.php" />',$it);
145  ptln('<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="'.DOKU_BASE.'feed.php?mode=list&amp;ns='.$INFO['namespace'].'" />',$it);
146  ptln('<link rel="alternate" type="text/html" title="Plain HTML" href="'.wl($ID,'do=export_html').'" />',$it);
147  ptln('<link rel="alternate" type="text/plain" title="Wiki Markup" href="'.wl($ID, 'do=export_raw').'" />',$it);
148  ptln('<link rel="stylesheet" media="screen" type="text/css" href="'.DOKU_BASE.'lib/styles/style.css" />',$it);
149
150  // setup robot tags apropriate for different modes
151  if( ($ACT=='show' || $ACT=='export_html') && !$REV){
152    if($INFO['exists']){
153      ptln('<meta name="date" content="'.date('Y-m-d\TH:i:sO',$INFO['lastmod']).'" />',$it);
154      //delay indexing:
155      if((time() - $INFO['lastmod']) >= $conf['indexdelay']){
156        ptln('<meta name="robots" content="index,follow" />',$it);
157      }else{
158        ptln('<meta name="robots" content="noindex,nofollow" />',$it);
159      }
160    }else{
161      ptln('<meta name="robots" content="noindex,follow" />',$it);
162    }
163  }else{
164    ptln('<meta name="robots" content="noindex,nofollow" />',$it);
165  }
166
167  // include some JavaScript language strings
168  ptln('<script language="JavaScript" type="text/javascript">',$it);
169  ptln("  var alertText   = '".$lang['qb_alert']."'",$it);
170  ptln("  var notSavedYet = '".$lang['notsavedyet']."'",$it);
171  ptln("  var DOKU_BASE   = '".DOKU_BASE."'",$it);
172  ptln('</script>',$it);
173
174  // load the default JavaScript files
175  ptln('<script language="JavaScript" type="text/javascript" src="'.
176       DOKU_BASE.'lib/scripts/script.js"></script>',$it);
177  ptln('<script language="JavaScript" type="text/javascript" src="'.
178       DOKU_BASE.'lib/scripts/tw-sack.js"></script>',$it);
179  ptln('<script language="JavaScript" type="text/javascript" src="'.
180       DOKU_BASE.'lib/scripts/ajax.js"></script>',$it);
181
182  //FIXME include some default CSS ? IE FIX?
183}
184
185/**
186 * Print a link
187 *
188 * Just builds a link but adds additional JavaScript needed for
189 * the unsaved data check needed in the edit form.
190 *
191 * @author Andreas Gohr <andi@splitbrain.org>
192 */
193function tpl_link($url,$name,$more=''){
194  print '<a href="'.$url.'" onclick="return svchk()" onkeypress="return svchk()"';
195  if ($more) print ' '.$more;
196  print ">$name</a>";
197}
198
199/**
200 * get the parent page
201 *
202 * Tries to find out which page is parent.
203 * returns false if none is available
204 *
205 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
206 */
207function tpl_getparent($ID){
208  global $conf;
209
210  if ($ID != $conf['start']) {
211    $idparts = explode(':', $ID);
212    $pn = array_pop($idparts);    // get the page name
213
214    for ($n=0; $n < 2; $n++) {
215      if (count($idparts) == 0) {
216        $ID = $conf['start'];     // go to topmost page
217        break;
218      }else{
219        $ns = array_pop($idparts);     // get the last part of namespace
220        if ($pn != $ns) {                 // are we already home?
221          array_push($idparts, $ns, $ns); // no, then add a page with same name
222          $ID = implode (':', $idparts); // as the namespace and recombine $ID
223          break;
224        }
225      }
226    }
227
228    if (@file_exists(wikiFN($ID))) {
229      return $ID;
230    }
231  }
232  return false;
233}
234
235/**
236 * Print one of the buttons
237 *
238 * Available Buttons are
239 *
240 *  edit    - edit/create/show button
241 *  history - old revisions
242 *  recent  - recent changes
243 *  login   - login/logout button - if ACL enabled
244 *  index   - The index
245 *  admin   - admin page - if enough rights
246 *  top     - a back to top button
247 *  back    - a back to parent button - if available
248 *
249 * @author Andreas Gohr <andi@splitbrain.org>
250 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
251 */
252function tpl_button($type){
253  global $ID;
254  global $INFO;
255  global $conf;
256
257  switch($type){
258    case 'edit':
259      print html_editbutton();
260      break;
261    case 'history':
262      print html_btn(revs,$ID,'o',array('do' => 'revisions'));
263      break;
264    case 'recent':
265      print html_btn(recent,'','r',array('do' => 'recent'));
266      break;
267    case 'index':
268      print html_btn(index,$ID,'x',array('do' => 'index'));
269      break;
270    case 'back':
271      if ($ID = tpl_getparent($ID)) {
272        print html_btn(back,$ID,'b',array('do' => 'show'));
273      }
274      break;
275    case 'top':
276      print html_topbtn();
277      break;
278    case 'login':
279      if($conf['useacl']){
280        if($_SERVER['REMOTE_USER']){
281          print html_btn('logout',$ID,'',array('do' => 'logout',));
282        }else{
283          print html_btn('login',$ID,'',array('do' => 'login'));
284        }
285      }
286      break;
287    case 'admin':
288      if($INFO['perm'] == AUTH_ADMIN)
289        print html_btn(admin,$ID,'',array('do' => 'admin'));
290      break;
291		default:
292			print '[unknown button type]';
293  }
294}
295
296/**
297 * Like the action buttons but links
298 *
299 * Available links are
300 *
301 *  edit    - edit/create/show button
302 *  history - old revisions
303 *  recent  - recent changes
304 *  login   - login/logout button - if ACL enabled
305 *  index   - The index
306 *  admin   - admin page - if enough rights
307 *  top     - a back to top button
308 *  back    - a back to parent button - if available
309 *
310 * @author Andreas Gohr <andi@splitbrain.org>
311 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
312 * @see    tpl_button
313 */
314function tpl_actionlink($type,$pre='',$suf=''){
315  global $ID;
316  global $INFO;
317  global $REV;
318  global $ACT;
319  global $conf;
320  global $lang;
321
322  switch($type){
323    case 'edit':
324      #most complicated type - we need to decide on current action
325      if($ACT == 'show' || $ACT == 'search'){
326        if($INFO['writable']){
327          if($INFO['exists']){
328            tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
329                     $pre.$lang['btn_edit'].$suf,
330                     'class="action" accesskey="e" rel="nofollow"');
331          }else{
332            tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
333                     $pre.$lang['btn_create'].$suf,
334                     'class="action" accesskey="e" rel="nofollow"');
335          }
336        }else{
337          tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
338                   $pre.$lang['btn_source'].$suf,
339                   'class="action" accesskey="v" rel="nofollow"');
340        }
341      }else{
342          tpl_link(wl($ID,'do=show'),
343                   $pre.$lang['btn_show'].$suf,
344                   'class="action" accesskey="v" rel="nofollow"');
345      }
346      break;
347    case 'history':
348      tpl_link(wl($ID,'do=revisions'),$pre.$lang['btn_revs'].$suf,'class="action" accesskey="o"');
349      break;
350    case 'recent':
351      tpl_link(wl($ID,'do=recent'),$pre.$lang['btn_recent'].$suf,'class="action" accesskey="r"');
352      break;
353    case 'index':
354      tpl_link(wl($ID,'do=index'),$pre.$lang['btn_index'].$suf,'class="action" accesskey="x"');
355      break;
356    case 'top':
357      print '<a href="#top" class="action" accesskey="x">'.$pre.$lang['btn_top'].$suf.'</a>';
358      break;
359    case 'back':
360      if ($ID = tpl_getparent($ID)) {
361        tpl_link(wl($ID,'do=show'),$pre.$lang['btn_back'].$suf,'class="action" accesskey="b"');
362      }
363      break;
364    case 'login':
365      if($conf['useacl']){
366        if($_SERVER['REMOTE_USER']){
367          tpl_link(wl($ID,'do=logout'),$pre.$lang['btn_logout'].$suf,'class="action"');
368        }else{
369          tpl_link(wl($ID,'do=login'),$pre.$lang['btn_login'].$suf,'class="action"');
370        }
371      }
372      break;
373    case 'admin':
374      if($INFO['perm'] == AUTH_ADMIN)
375        tpl_link(wl($ID,'do=admin'),$pre.$lang['btn_admin'].$suf,'class="action"');
376      break;
377    default:
378      print '[unknown link type]';
379  }
380}
381
382/**
383 * Print the search form
384 *
385 * @author Andreas Gohr <andi@splitbrain.org>
386 */
387function tpl_searchform(){
388  global $lang;
389  global $ACT;
390
391  print '<form action="'.wl().'" accept-charset="utf-8" class="search" name="search" onsubmit="return svchk()">';
392  print '<input type="hidden" name="do" value="search" />';
393  print '<input type="text" ';
394
395  if ($ACT == 'search')
396    print 'value="'.$_REQUEST['id'].'" '; /* keep search input as long as user stays on search page */
397
398  print 'id="qsearch_in" accesskey="f" name="id" class="edit" onkeyup="ajax_qsearch.call(\'qsearch_in\',\'qsearch_out\')" />';
399  print '<input type="submit" value="'.$lang['btn_search'].'" class="button" />';
400  print '<div id="qsearch_out" class="ajax_qsearch" onclick="this.style.display=\'none\'"></div>';
401  print '</form>';
402}
403
404/**
405 * Print the breadcrumbs trace
406 *
407 * @author Andreas Gohr <andi@splitbrain.org>
408 */
409function tpl_breadcrumbs(){
410  global $lang;
411  global $conf;
412
413  //check if enabled
414  if(!$conf['breadcrumbs']) return;
415
416  $crumbs = breadcrumbs(); //setup crumb trace
417
418  //reverse crumborder in right-to-left mode
419  if($lang['direction'] == 'rtl') $crumbs = array_reverse($crumbs,true);
420
421  //render crumbs, highlight the last one
422  print $lang['breadcrumb'].':';
423  $last = count($crumbs);
424  $i = 0;
425  foreach ($crumbs as $id => $name){
426    $i++;
427    print ' <span class="bcsep">&raquo;</span> ';
428    if ($i == $last) print '<span class="curid">';
429    tpl_link(wl($id),$name,'class="breadcrumbs" title="'.$id.'"');
430    if ($i == $last) print '</span>';
431  }
432}
433
434/**
435 * Hierarchical breadcrumbs
436 *
437 * This code was suggested as replacement for the usual breadcrumbs
438 * trail in the Wiki and was modified by me.
439 * It only makes sense with a deep site structure.
440 *
441 * @author Andreas Gohr <andi@splitbrain.org>
442 * @link   http://wiki.splitbrain.org/wiki:tipsandtricks:hierarchicalbreadcrumbs
443 * @todo   May behave starngely in RTL languages
444 */
445function tpl_youarehere(){
446  global $conf;
447  global $ID;
448  global $lang;
449
450
451  $parts     = explode(':', $ID);
452
453  print $lang['breadcrumb'].': ';
454
455  //always print the startpage
456  if( $a_part[0] != $conf['start'] )
457    tpl_link(wl($conf['start']),$conf['start'],'title="'.$conf['start'].'"');
458
459  $page = '';
460  foreach ($parts as $part){
461	  print ' &raquo; ';
462    $page .= $part;
463
464    if(file_exists(wikiFN($page))){
465      tpl_link(wl($page),$part,'title="'.$page.'"');
466    }else{
467      print $page;
468    }
469
470    $page .= ':';
471  }
472}
473
474/**
475 * Print info if the user is logged in
476 * and show full name in that case
477 *
478 * Could be enhanced with a profile link in future?
479 *
480 * @author Andreas Gohr <andi@splitbrain.org>
481 */
482function tpl_userinfo(){
483  global $lang;
484  global $INFO;
485  if($_SERVER['REMOTE_USER'])
486    print $lang['loggedinas'].': '.$INFO['userinfo']['name'];
487}
488
489/**
490 * Print some info about the current page
491 *
492 * @author Andreas Gohr <andi@splitbrain.org>
493 */
494function tpl_pageinfo(){
495  global $conf;
496  global $lang;
497  global $INFO;
498  global $REV;
499
500  // prepare date and path
501  $fn = $INFO['filepath'];
502  if(!$conf['fullpath']){
503    if($REV){
504      $fn = str_replace(realpath($conf['olddir']).DIRECTORY_SEPARATOR,'',$fn);
505    }else{
506      $fn = str_replace(realpath($conf['datadir']).DIRECTORY_SEPARATOR,'',$fn);
507    }
508  }
509  $fn = utf8_decodeFN($fn);
510  $date = date($conf['dformat'],$INFO['lastmod']);
511
512  // print it
513  if($INFO['exists']){
514    print $fn;
515    print ' &middot; ';
516    print $lang['lastmod'];
517    print ': ';
518    print $date;
519    if($INFO['editor']){
520      print ' '.$lang['by'].' ';
521      print $INFO['editor'];
522    }
523    if($INFO['locked']){
524      print ' &middot; ';
525      print $lang['lockedby'];
526      print ': ';
527      print $INFO['locked'];
528    }
529  }
530}
531
532/**
533 * Print a list of namespaces containing media files
534 *
535 * @author Andreas Gohr <andi@splitbrain.org>
536 */
537function tpl_medianamespaces(){
538	global $conf;
539
540  $data = array();
541  search($data,$conf['mediadir'],'search_namespaces',array());
542  print html_buildlist($data,'idx',media_html_list_namespaces);
543}
544
545/**
546 * Print a list of mediafiles in the current namespace
547 *
548 * @author Andreas Gohr <andi@splitbrain.org>
549 */
550function tpl_mediafilelist(){
551  global $conf;
552  global $lang;
553  global $NS;
554  global $AUTH;
555  $dir = utf8_encodeFN(str_replace(':','/',$NS));
556
557  $data = array();
558  search($data,$conf['mediadir'],'search_media',array(),$dir);
559
560  if(!count($data)){
561    ptln('<div class="nothing">'.$lang['nothingfound'].'<div>');
562    return;
563  }
564
565  ptln('<ul>',2);
566  foreach($data as $item){
567    ptln('<li>',4);
568    ptln('<a href="javascript:mediaSelect(\''.$item['id'].'\')">'.
569         utf8_decodeFN($item['file']).
570         '</a>',6);
571
572    //prepare deletion button
573    if($AUTH >= AUTH_DELETE){
574      $ask  = $lang['del_confirm'].'\\n';
575      $ask .= $item['id'];
576
577      $del = '<a href="'.DOKU_BASE.'lib/exe/media.php?delete='.urlencode($item['id']).'" '.
578             'onclick="return confirm(\''.$ask.'\')" onkeypress="return confirm(\''.$ask.'\')">'.
579             '<img src="'.DOKU_BASE.'lib/images/del.png" alt="'.$lang['btn_delete'].'" '.
580             'align="bottom" title="'.$lang['btn_delete'].'" /></a>';
581    }else{
582      $del = '';
583    }
584
585
586    if($item['isimg']){
587      $w = $item['info'][0];
588      $h = $item['info'][1];
589
590      ptln('('.$w.'&#215;'.$h.' '.filesize_h($item['size']).')',6);
591      ptln($del.'<br />',6);
592      ptln('<a href="javascript:mediaSelect(\''.$item['id'].'\')">');
593
594      if($w>120){
595        print '<img src="'.DOKU_BASE.'lib/exe/fetch.php?w=120&amp;media='.urlencode($item['id']).'" width="120" />';
596      }else{
597        print '<img src="'.DOKU_BASE.'lib/exe/fetch.php?media='.urlencode($item['id']).'" width="'.$w.'" height="'.$h.'" />';
598      }
599      print '</a>';
600
601    }else{
602      ptln ('('.filesize_h($item['size']).')',6);
603      ptln($del,6);
604    }
605    ptln('</li>',4);
606  }
607  ptln('</ul>',2);
608}
609
610/**
611 * Print the media upload form if permissions are correct
612 *
613 * @author Andreas Gohr <andi@splitbrain.org>
614 */
615function tpl_mediauploadform(){
616  global $NS;
617  global $UPLOADOK;
618  global $AUTH;
619  global $lang;
620
621  if(!$UPLOADOK) return;
622
623  ptln('<form action="'.$_SERVER['PHP_SELF'].'" name="upload"'.
624       ' method="post" enctype="multipart/form-data">',2);
625  ptln($lang['txt_upload'].':<br />',4);
626  ptln('<input type="file" name="upload" class="edit" onchange="suggestWikiname();" />',4);
627  ptln('<input type="hidden" name="ns" value="'.hsc($NS).'" /><br />',4);
628  ptln($lang['txt_filename'].'<br />',4);
629  ptln('<input type="text" name="id" class="edit" />',4);
630  ptln('<input type="submit" class="button" value="'.$lang['btn_upload'].'" accesskey="s" />',4);
631  if($AUTH >= AUTH_DELETE){
632    ptln('<label for="ow" class="simple"><input type="checkbox" name="ow" value="1" id="ow">'.$lang['txt_overwrt'].'</label>',4);
633  }
634  ptln('</form>',2);
635}
636
637
638//Setup VIM: ex: et ts=2 enc=utf-8 :
639