xref: /dokuwiki/inc/template.php (revision ee20e7d16637625e900f518b6b46a61bfa30fe6e)
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      html_recent();
79      break;
80    case 'index':
81      html_index($IDX); #FIXME can this be pulled from globals? is it sanitized correctly?
82      break;
83    case 'backlink':
84      html_backlinks();
85      break;
86    case 'conflict':
87      html_conflict(con($PRE,$TEXT,$SUF),$SUM);
88      html_diff(con($PRE,$TEXT,$SUF),false);
89      break;
90    case 'locked':
91      html_locked();
92      break;
93    case 'login':
94      html_login();
95      break;
96    case 'register':
97      html_register();
98      break;
99    case 'denied':
100      print p_locale_xhtml('denied');
101			break;
102    case 'admin':
103      tpl_admin();
104      break;
105    default:
106			msg("Failed to handle command: ".hsc($ACT),-1);
107  }
108}
109
110/**
111 * Handle the admin page contents
112 *
113 * @author Andreas Gohr <andi@splitbrain.org>
114 */
115function tpl_admin(){
116  switch($_REQUEST['page']){
117		case 'acl':
118			admin_acl_html();
119			break;
120    default:
121			html_admin();
122	}
123}
124
125/**
126 * Print the correct HTML meta headers
127 *
128 * This has to go into the head section of your template.
129 *
130 * @author Andreas Gohr <andi@splitbrain.org>
131 */
132function tpl_metaheaders(){
133  global $ID;
134  global $INFO;
135  global $ACT;
136  global $lang;
137  $it=2;
138
139  // the usual stuff
140  ptln('<meta name="generator" content="DokuWiki '.getVersion().'" />',$it);
141  ptln('<link rel="start" href="'.DOKU_BASE.'" />',$it);
142  ptln('<link rel="contents" href="'.wl($ID,'do=index').'" title="'.$lang['index'].'" />',$it);
143  ptln('<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="'.DOKU_BASE.'feed.php" />',$it);
144  ptln('<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="'.DOKU_BASE.'feed.php?mode=list&amp;ns='.$INFO['namespace'].'" />',$it);
145  ptln('<link rel="alternate" type="text/html" title="Plain HTML" href="'.wl($ID,'do=export_html').'" />',$it);
146  ptln('<link rel="alternate" type="text/plain" title="Wiki Markup" href="'.wl($ID, 'do=export_raw').'" />',$it);
147  ptln('<link rel="stylesheet" media="screen" type="text/css" href="'.DOKU_BASE.'style.css" />',$it);
148
149  // setup robot tags apropriate for different modes
150  if( ($ACT=='show' || $ACT=='export_html') && !$REV){
151    if($INFO['exists']){
152      ptln('<meta name="date" content="'.date('Y-m-d\TH:i:sO',$INFO['lastmod']).'" />',$it);
153      //delay indexing:
154      if((time() - $INFO['lastmod']) >= $conf['indexdelay']){
155        ptln('<meta name="robots" content="index,follow" />',$it);
156      }else{
157        ptln('<meta name="robots" content="noindex,nofollow" />',$it);
158      }
159    }else{
160      ptln('<meta name="robots" content="noindex,follow" />',$it);
161    }
162  }else{
163    ptln('<meta name="robots" content="noindex,nofollow" />',$it);
164  }
165
166  // include some JavaScript language strings
167  ptln('<script language="JavaScript" type="text/javascript">',$it);
168  ptln("  var alertText   = '".$lang['qb_alert']."'",$it);
169  ptln("  var notSavedYet = '".$lang['notsavedyet']."'",$it);
170  ptln("  var DOKU_BASE   = '".DOKU_BASE."'",$it);
171  ptln('</script>',$it);
172
173  // load the default JavaScript file
174  ptln('<script language="JavaScript" type="text/javascript" src="'.DOKU_BASE.'script.js"></script>',$it);
175
176
177  //FIXME include some default CSS ? IE FIX?
178}
179
180/**
181 * Print a link
182 *
183 * Just builds a link but adds additional JavaScript needed for
184 * the unsaved data check needed in the edit form.
185 *
186 * @author Andreas Gohr <andi@splitbrain.org>
187 */
188function tpl_link($url,$name,$more=''){
189  print '<a href="'.$url.'" onclick="return svchk()" onkeypress="return svchk()"';
190  if ($more) print ' '.$more;
191  print ">$name</a>";
192}
193
194/**
195 * Print one of the buttons
196 *
197 * Available Buttons are
198 *
199 *  edit    - edit/create/show button
200 *  history - old revisions
201 *  recent  - recent changes
202 *  login   - login/logout button - if ACL enabled
203 *  index   - The index
204 *  admin   - admin page - if enough rights
205 *  top     - a back to top button
206 *
207 * @author Andreas Gohr <andi@splitbrain.org>
208 */
209function tpl_button($type){
210  global $ID;
211  global $INFO;
212  global $conf;
213
214  switch($type){
215    case 'edit':
216      print html_editbutton();
217      break;
218    case 'history':
219      print html_btn(revs,$ID,'o',array('do' => 'revisions'));
220      break;
221    case 'recent':
222      print html_btn(recent,'','r',array('do' => 'recent'));
223      break;
224    case 'index':
225      print html_btn(index,$ID,'x',array('do' => 'index'));
226      break;
227    case 'top':
228      print html_topbtn();
229      break;
230    case 'login':
231      if($conf['useacl']){
232        if($_SERVER['REMOTE_USER']){
233          print html_btn('logout',$ID,'',array('do' => 'logout',));
234        }else{
235          print html_btn('login',$ID,'',array('do' => 'login'));
236        }
237      }
238      break;
239    case 'admin':
240      if($INFO['perm'] == AUTH_ADMIN)
241        print html_btn(admin,$ID,'',array('do' => 'admin'));
242      break;
243		default:
244			print '[unknown button type]';
245  }
246}
247
248/**
249 * Like the action buttons but links
250 *
251 * Available links are
252 *
253 *  edit    - edit/create/show button
254 *  history - old revisions
255 *  recent  - recent changes
256 *  login   - login/logout button - if ACL enabled
257 *  index   - The index
258 *  admin   - admin page - if enough rights
259 *  top     - a back to top button
260 *
261 * @author Andreas Gohr <andi@splitbrain.org>
262 * @see    tpl_button
263 */
264function tpl_actionlink($type,$pre='',$suf=''){
265  global $ID;
266  global $INFO;
267  global $REV;
268  global $ACT;
269  global $conf;
270  global $lang;
271
272  switch($type){
273    case 'edit':
274      #most complicated type - we need to decide on current action
275      if($ACT == 'show' || $ACT == 'search'){
276        if($INFO['writable']){
277          if($INFO['exists']){
278            tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
279                     $pre.$lang['btn_edit'].$suf,
280                     'class="action" accesskey="e" rel="nofollow"');
281          }else{
282            tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
283                     $pre.$lang['btn_create'].$suf,
284                     'class="action" accesskey="e" rel="nofollow"');
285          }
286        }else{
287          tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
288                   $pre.$lang['btn_source'].$suf,
289                   'class="action" accesskey="v" rel="nofollow"');
290        }
291      }else{
292          tpl_link(wl($ID,'do=show'),
293                   $pre.$lang['btn_show'].$suf,
294                   'class="action" accesskey="v" rel="nofollow"');
295      }
296      break;
297    case 'history':
298      tpl_link(wl($ID,'do=revisions'),$pre.$lang['btn_revs'].$suf,'class="action" accesskey="o"');
299      break;
300    case 'recent':
301      tpl_link(wl($ID,'do=recent'),$pre.$lang['btn_recent'].$suf,'class="action" accesskey="r"');
302      break;
303    case 'index':
304      tpl_link(wl($ID,'do=index'),$pre.$lang['btn_index'].$suf,'class="action" accesskey="x"');
305      break;
306    case 'top':
307      print '<a href="#top" class="action" accesskey="x">'.$pre.$lang['btn_top'].$suf.'</a>';
308      break;
309    case 'login':
310      if($conf['useacl']){
311        if($_SERVER['REMOTE_USER']){
312          tpl_link(wl($ID,'do=logout'),$pre.$lang['btn_logout'].$suf,'class="action"');
313        }else{
314          tpl_link(wl($ID,'do=login'),$pre.$lang['btn_login'].$suf,'class="action"');
315        }
316      }
317      break;
318    case 'admin':
319      if($INFO['perm'] == AUTH_ADMIN)
320        tpl_link(wl($ID,'do=admin'),$pre.$lang['btn_admin'].$suf,'class="action"');
321      break;
322    default:
323      print '[unknown link type]';
324  }
325}
326
327/**
328 * Print the search form
329 *
330 * @author Andreas Gohr <andi@splitbrain.org>
331 */
332function tpl_searchform(){
333  global $lang;
334  print '<form action="'.wl().'" accept-charset="utf-8" class="search" name="search" onsubmit="return svchk()">';
335
336//FIXME this should be moved somewhere else
337?>
338<script type="text/javascript" src="<?=DOKU_BASE?>tw-sack.js"></script>
339<script type="text/javascript">
340  ajax = new sack('<?=DOKU_BASE?>ajax.php');
341  ajax.AjaxFailedAlert = '';
342
343  var qr = null;
344  function ajax_runqsearch(){
345    ajax_stopqsearch();
346    qr = window.setTimeout('ajax_qsearch()',500);
347  }
348
349  function ajax_stopqsearch(){
350    document.getElementById('ajax_qsearch').innerHTML = '';
351    if(qr != null){
352      window.clearTimeout(qr);
353      qr = null;
354    }
355  }
356
357  function ajax_qsearch(){
358    var value = document.search.id.value;
359    if(value=='') return;
360    ajax.element = 'ajax_qsearch';
361    ajax.encodeURIString = false;
362    var call = 'call=qsearch&q=';
363    call += encodeURI(value);
364    ajax.runAJAX(call);
365
366    ajax_stopqsearch()
367  }
368</script>
369<?php
370  print '<input type="hidden" name="do" value="search" />';
371  #print '<input type="text" accesskey="f" name="id" class="edit" autocomplete="off" onkeyup="ajax_qsearch(this.value)" />';
372  print '<input type="text" accesskey="f" name="id" class="edit" onkeypress="ajax_runqsearch()" />';
373  print '<input type="submit" value="'.$lang['btn_search'].'" class="button" />';
374  print '<div id="ajax_qsearch" class="ajax_qsearch"></div>';
375  print '</form>';
376}
377
378/**
379 * Print the breadcrumbs trace
380 *
381 * @author Andreas Gohr <andi@splitbrain.org>
382 */
383function tpl_breadcrumbs(){
384  global $lang;
385  global $conf;
386
387  //check if enabled
388  if(!$conf['breadcrumbs']) return;
389
390  $crumbs = breadcrumbs(); //setup crumb trace
391
392  //reverse crumborder in right-to-left mode
393  if($lang['direction'] == 'rtl') $crumbs = array_reverse($crumbs,true);
394
395  //render crumbs, highlight the last one
396  print $lang['breadcrumb'].':';
397  $last = count($crumbs);
398  $i = 0;
399  foreach ($crumbs as $id => $name){
400    $i++;
401    print ' <span class="bcsep">&raquo;</span> ';
402    if ($i == $last) print '<span class="curid">';
403    tpl_link(wl($id),$name,'class="breadcrumbs" title="'.$id.'"');
404    if ($i == $last) print '</span>';
405  }
406}
407
408/**
409 * Hierarchical breadcrumbs
410 *
411 * This code was suggested as replacement for the usual breadcrumbs
412 * trail in the Wiki and was modified by me.
413 * It only makes sense with a deep site structure.
414 *
415 * @author Andreas Gohr <andi@splitbrain.org>
416 * @link   http://wiki.splitbrain.org/wiki:tipsandtricks:hierarchicalbreadcrumbs
417 * @todo   May behave starngely in RTL languages
418 */
419function tpl_youarehere(){
420  global $conf;
421  global $ID;
422  global $lang;
423
424
425  $parts     = explode(':', $ID);
426
427  print $lang['breadcrumb'].': ';
428
429  //always print the startpage
430  if( $a_part[0] != $conf['start'] )
431    tpl_link(wl($conf['start']),$conf['start'],'title="'.$conf['start'].'"');
432
433  $page = '';
434  foreach ($parts as $part){
435	  print ' &raquo; ';
436    $page .= $part;
437
438    if(file_exists(wikiFN($page))){
439      tpl_link(wl($page),$part,'title="'.$page.'"');
440    }else{
441      print $page;
442    }
443
444    $page .= ':';
445  }
446}
447
448/**
449 * Print info if the user is logged in
450 * and show full name in that case
451 *
452 * Could be enhanced with a profile link in future?
453 *
454 * @author Andreas Gohr <andi@splitbrain.org>
455 */
456function tpl_userinfo(){
457  global $lang;
458  global $INFO;
459  if($_SERVER['REMOTE_USER'])
460    print $lang['loggedinas'].': '.$INFO['userinfo']['name'];
461}
462
463/**
464 * Print some info about the current page
465 *
466 * @author Andreas Gohr <andi@splitbrain.org>
467 */
468function tpl_pageinfo(){
469  global $conf;
470  global $lang;
471  global $INFO;
472  global $REV;
473
474  // prepare date and path
475  $fn = $INFO['filepath'];
476  if(!$conf['fullpath']){
477    if($REV){
478      $fn = str_replace(realpath($conf['olddir']).DIRECTORY_SEPARATOR,'',$fn);
479    }else{
480      $fn = str_replace(realpath($conf['datadir']).DIRECTORY_SEPARATOR,'',$fn);
481    }
482  }
483  $fn = utf8_decodeFN($fn);
484  $date = date($conf['dformat'],$INFO['lastmod']);
485
486  // print it
487  if($INFO['exists']){
488    print $fn;
489    print ' &middot; ';
490    print $lang['lastmod'];
491    print ': ';
492    print $date;
493    if($INFO['editor']){
494      print ' '.$lang['by'].' ';
495      print $INFO['editor'];
496    }
497    if($INFO['locked']){
498      print ' &middot; ';
499      print $lang['lockedby'];
500      print ': ';
501      print $INFO['locked'];
502    }
503  }
504}
505
506/**
507 * Print a list of namespaces containing media files
508 *
509 * @author Andreas Gohr <andi@splitbrain.org>
510 */
511function tpl_medianamespaces(){
512	global $conf;
513
514  $data = array();
515  search($data,$conf['mediadir'],'search_namespaces',array());
516  print html_buildlist($data,'idx',media_html_list_namespaces);
517}
518
519/**
520 * Print a list of mediafiles in the current namespace
521 *
522 * @author Andreas Gohr <andi@splitbrain.org>
523 */
524function tpl_mediafilelist(){
525  global $conf;
526  global $lang;
527  global $NS;
528  $dir = utf8_encodeFN(str_replace(':','/',$NS));
529
530  $data = array();
531  search($data,$conf['mediadir'],'search_media',array(),$dir);
532
533  if(!count($data)){
534    ptln('<div class="nothing">'.$lang['nothingfound'].'<div>');
535    return;
536  }
537
538  ptln('<ul>',2);
539  foreach($data as $item){
540    ptln('<li>',4);
541    ptln('<a href="javascript:mediaSelect(\''.$item['id'].'\')">'.
542         utf8_decodeFN($item['file']).
543         '</a>',6);
544    if($item['isimg']){
545      $w = $item['info'][0];
546      $h = $item['info'][1];
547
548      ptln('('.$w.'&#215;'.$h.' '.filesize_h($item['size']).')<br />',6);
549      ptln('<a href="javascript:mediaSelect(\''.$item['id'].'\')">');
550
551      if($w>120){
552        print '<img src="'.DOKU_BASE.'fetch.php?w=120&amp;media='.urlencode($item['id']).'" width="120" />';
553      }else{
554        print '<img src="'.DOKU_BASE.'fetch.php?media='.urlencode($item['id']).'" width="'.$w.'" height="'.$h.'" />';
555      }
556      print '</a>';
557
558    }else{
559      ptln ('('.filesize_h($item['size']).')',6);
560    }
561    ptln('</li>',4);
562  }
563  ptln('</ul>',2);
564}
565
566/**
567 * Print the media upload form if permissions are correct
568 *
569 * @author Andreas Gohr <andi@splitbrain.org>
570 */
571function tpl_mediauploadform(){
572  global $NS;
573  global $UPLOADOK;
574  global $lang;
575
576  if(!$UPLOADOK) return;
577
578  ptln('<form action="'.$_SERVER['PHP_SELF'].'" name="upload"'.
579       ' method="post" enctype="multipart/form-data">',2);
580  ptln($lang['txt_upload'].':<br />',4);
581  ptln('<input type="file" name="upload" class="edit" onchange="suggestWikiname();" />',4);
582  ptln('<input type="hidden" name="ns" value="'.hsc($NS).'" /><br />',4);
583  ptln($lang['txt_filename'].'<br />',4);
584  ptln('<input type="text" name="id" class="edit" />',4);
585  ptln('<input type="submit" class="button" value="'.$lang['btn_upload'].'" accesskey="s" />',4);
586  ptln('</form>',2);
587}
588
589
590//Setup VIM: ex: et ts=2 enc=utf-8 :
591