xref: /dokuwiki/inc/template.php (revision 5749f1ce740e4cfd6d886e8d2fda6b8782389d33)
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.'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="'.DOKU_BASE.'script.js"></script>',$it);
176  ptln('<script language="JavaScript" type="text/javascript" src="'.DOKU_BASE.'tw-sack.js"></script>',$it);
177  ptln('<script language="JavaScript" type="text/javascript" src="'.DOKU_BASE.'ajax.js"></script>',$it);
178
179  //FIXME include some default CSS ? IE FIX?
180}
181
182/**
183 * Print a link
184 *
185 * Just builds a link but adds additional JavaScript needed for
186 * the unsaved data check needed in the edit form.
187 *
188 * @author Andreas Gohr <andi@splitbrain.org>
189 */
190function tpl_link($url,$name,$more=''){
191  print '<a href="'.$url.'" onclick="return svchk()" onkeypress="return svchk()"';
192  if ($more) print ' '.$more;
193  print ">$name</a>";
194}
195
196/**
197 * Print one of the buttons
198 *
199 * Available Buttons are
200 *
201 *  edit    - edit/create/show button
202 *  history - old revisions
203 *  recent  - recent changes
204 *  login   - login/logout button - if ACL enabled
205 *  index   - The index
206 *  admin   - admin page - if enough rights
207 *  top     - a back to top button
208 *
209 * @author Andreas Gohr <andi@splitbrain.org>
210 */
211function tpl_button($type){
212  global $ID;
213  global $INFO;
214  global $conf;
215
216  switch($type){
217    case 'edit':
218      print html_editbutton();
219      break;
220    case 'history':
221      print html_btn(revs,$ID,'o',array('do' => 'revisions'));
222      break;
223    case 'recent':
224      print html_btn(recent,'','r',array('do' => 'recent'));
225      break;
226    case 'index':
227      print html_btn(index,$ID,'x',array('do' => 'index'));
228      break;
229    case 'top':
230      print html_topbtn();
231      break;
232    case 'login':
233      if($conf['useacl']){
234        if($_SERVER['REMOTE_USER']){
235          print html_btn('logout',$ID,'',array('do' => 'logout',));
236        }else{
237          print html_btn('login',$ID,'',array('do' => 'login'));
238        }
239      }
240      break;
241    case 'admin':
242      if($INFO['perm'] == AUTH_ADMIN)
243        print html_btn(admin,$ID,'',array('do' => 'admin'));
244      break;
245		default:
246			print '[unknown button type]';
247  }
248}
249
250/**
251 * Like the action buttons but links
252 *
253 * Available links are
254 *
255 *  edit    - edit/create/show button
256 *  history - old revisions
257 *  recent  - recent changes
258 *  login   - login/logout button - if ACL enabled
259 *  index   - The index
260 *  admin   - admin page - if enough rights
261 *  top     - a back to top button
262 *
263 * @author Andreas Gohr <andi@splitbrain.org>
264 * @see    tpl_button
265 */
266function tpl_actionlink($type,$pre='',$suf=''){
267  global $ID;
268  global $INFO;
269  global $REV;
270  global $ACT;
271  global $conf;
272  global $lang;
273
274  switch($type){
275    case 'edit':
276      #most complicated type - we need to decide on current action
277      if($ACT == 'show' || $ACT == 'search'){
278        if($INFO['writable']){
279          if($INFO['exists']){
280            tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
281                     $pre.$lang['btn_edit'].$suf,
282                     'class="action" accesskey="e" rel="nofollow"');
283          }else{
284            tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
285                     $pre.$lang['btn_create'].$suf,
286                     'class="action" accesskey="e" rel="nofollow"');
287          }
288        }else{
289          tpl_link(wl($ID,'do=edit&amp;rev='.$REV),
290                   $pre.$lang['btn_source'].$suf,
291                   'class="action" accesskey="v" rel="nofollow"');
292        }
293      }else{
294          tpl_link(wl($ID,'do=show'),
295                   $pre.$lang['btn_show'].$suf,
296                   'class="action" accesskey="v" rel="nofollow"');
297      }
298      break;
299    case 'history':
300      tpl_link(wl($ID,'do=revisions'),$pre.$lang['btn_revs'].$suf,'class="action" accesskey="o"');
301      break;
302    case 'recent':
303      tpl_link(wl($ID,'do=recent'),$pre.$lang['btn_recent'].$suf,'class="action" accesskey="r"');
304      break;
305    case 'index':
306      tpl_link(wl($ID,'do=index'),$pre.$lang['btn_index'].$suf,'class="action" accesskey="x"');
307      break;
308    case 'top':
309      print '<a href="#top" class="action" accesskey="x">'.$pre.$lang['btn_top'].$suf.'</a>';
310      break;
311    case 'login':
312      if($conf['useacl']){
313        if($_SERVER['REMOTE_USER']){
314          tpl_link(wl($ID,'do=logout'),$pre.$lang['btn_logout'].$suf,'class="action"');
315        }else{
316          tpl_link(wl($ID,'do=login'),$pre.$lang['btn_login'].$suf,'class="action"');
317        }
318      }
319      break;
320    case 'admin':
321      if($INFO['perm'] == AUTH_ADMIN)
322        tpl_link(wl($ID,'do=admin'),$pre.$lang['btn_admin'].$suf,'class="action"');
323      break;
324    default:
325      print '[unknown link type]';
326  }
327}
328
329/**
330 * Print the search form
331 *
332 * @author Andreas Gohr <andi@splitbrain.org>
333 */
334function tpl_searchform(){
335  global $lang;
336  print '<form action="'.wl().'" accept-charset="utf-8" class="search" name="search" onsubmit="return svchk()">';
337  print '<input type="hidden" name="do" value="search" />';
338  print '<input type="text" id="qsearch_in" accesskey="f" name="id" class="edit" onkeyup="ajax_qsearch.call(\'qsearch_in\',\'qsearch_out\')" />';
339  print '<input type="submit" value="'.$lang['btn_search'].'" class="button" />';
340  print '<div id="qsearch_out" class="ajax_qsearch" onclick="this.style.display=\'none\'"></div>';
341  print '</form>';
342}
343
344/**
345 * Print the breadcrumbs trace
346 *
347 * @author Andreas Gohr <andi@splitbrain.org>
348 */
349function tpl_breadcrumbs(){
350  global $lang;
351  global $conf;
352
353  //check if enabled
354  if(!$conf['breadcrumbs']) return;
355
356  $crumbs = breadcrumbs(); //setup crumb trace
357
358  //reverse crumborder in right-to-left mode
359  if($lang['direction'] == 'rtl') $crumbs = array_reverse($crumbs,true);
360
361  //render crumbs, highlight the last one
362  print $lang['breadcrumb'].':';
363  $last = count($crumbs);
364  $i = 0;
365  foreach ($crumbs as $id => $name){
366    $i++;
367    print ' <span class="bcsep">&raquo;</span> ';
368    if ($i == $last) print '<span class="curid">';
369    tpl_link(wl($id),$name,'class="breadcrumbs" title="'.$id.'"');
370    if ($i == $last) print '</span>';
371  }
372}
373
374/**
375 * Hierarchical breadcrumbs
376 *
377 * This code was suggested as replacement for the usual breadcrumbs
378 * trail in the Wiki and was modified by me.
379 * It only makes sense with a deep site structure.
380 *
381 * @author Andreas Gohr <andi@splitbrain.org>
382 * @link   http://wiki.splitbrain.org/wiki:tipsandtricks:hierarchicalbreadcrumbs
383 * @todo   May behave starngely in RTL languages
384 */
385function tpl_youarehere(){
386  global $conf;
387  global $ID;
388  global $lang;
389
390
391  $parts     = explode(':', $ID);
392
393  print $lang['breadcrumb'].': ';
394
395  //always print the startpage
396  if( $a_part[0] != $conf['start'] )
397    tpl_link(wl($conf['start']),$conf['start'],'title="'.$conf['start'].'"');
398
399  $page = '';
400  foreach ($parts as $part){
401	  print ' &raquo; ';
402    $page .= $part;
403
404    if(file_exists(wikiFN($page))){
405      tpl_link(wl($page),$part,'title="'.$page.'"');
406    }else{
407      print $page;
408    }
409
410    $page .= ':';
411  }
412}
413
414/**
415 * Print info if the user is logged in
416 * and show full name in that case
417 *
418 * Could be enhanced with a profile link in future?
419 *
420 * @author Andreas Gohr <andi@splitbrain.org>
421 */
422function tpl_userinfo(){
423  global $lang;
424  global $INFO;
425  if($_SERVER['REMOTE_USER'])
426    print $lang['loggedinas'].': '.$INFO['userinfo']['name'];
427}
428
429/**
430 * Print some info about the current page
431 *
432 * @author Andreas Gohr <andi@splitbrain.org>
433 */
434function tpl_pageinfo(){
435  global $conf;
436  global $lang;
437  global $INFO;
438  global $REV;
439
440  // prepare date and path
441  $fn = $INFO['filepath'];
442  if(!$conf['fullpath']){
443    if($REV){
444      $fn = str_replace(realpath($conf['olddir']).DIRECTORY_SEPARATOR,'',$fn);
445    }else{
446      $fn = str_replace(realpath($conf['datadir']).DIRECTORY_SEPARATOR,'',$fn);
447    }
448  }
449  $fn = utf8_decodeFN($fn);
450  $date = date($conf['dformat'],$INFO['lastmod']);
451
452  // print it
453  if($INFO['exists']){
454    print $fn;
455    print ' &middot; ';
456    print $lang['lastmod'];
457    print ': ';
458    print $date;
459    if($INFO['editor']){
460      print ' '.$lang['by'].' ';
461      print $INFO['editor'];
462    }
463    if($INFO['locked']){
464      print ' &middot; ';
465      print $lang['lockedby'];
466      print ': ';
467      print $INFO['locked'];
468    }
469  }
470}
471
472/**
473 * Print a list of namespaces containing media files
474 *
475 * @author Andreas Gohr <andi@splitbrain.org>
476 */
477function tpl_medianamespaces(){
478	global $conf;
479
480  $data = array();
481  search($data,$conf['mediadir'],'search_namespaces',array());
482  print html_buildlist($data,'idx',media_html_list_namespaces);
483}
484
485/**
486 * Print a list of mediafiles in the current namespace
487 *
488 * @author Andreas Gohr <andi@splitbrain.org>
489 */
490function tpl_mediafilelist(){
491  global $conf;
492  global $lang;
493  global $NS;
494  $dir = utf8_encodeFN(str_replace(':','/',$NS));
495
496  $data = array();
497  search($data,$conf['mediadir'],'search_media',array(),$dir);
498
499  if(!count($data)){
500    ptln('<div class="nothing">'.$lang['nothingfound'].'<div>');
501    return;
502  }
503
504  ptln('<ul>',2);
505  foreach($data as $item){
506    ptln('<li>',4);
507    ptln('<a href="javascript:mediaSelect(\''.$item['id'].'\')">'.
508         utf8_decodeFN($item['file']).
509         '</a>',6);
510    if($item['isimg']){
511      $w = $item['info'][0];
512      $h = $item['info'][1];
513
514      ptln('('.$w.'&#215;'.$h.' '.filesize_h($item['size']).')<br />',6);
515      ptln('<a href="javascript:mediaSelect(\''.$item['id'].'\')">');
516
517      if($w>120){
518        print '<img src="'.DOKU_BASE.'fetch.php?w=120&amp;media='.urlencode($item['id']).'" width="120" />';
519      }else{
520        print '<img src="'.DOKU_BASE.'fetch.php?media='.urlencode($item['id']).'" width="'.$w.'" height="'.$h.'" />';
521      }
522      print '</a>';
523
524    }else{
525      ptln ('('.filesize_h($item['size']).')',6);
526    }
527    ptln('</li>',4);
528  }
529  ptln('</ul>',2);
530}
531
532/**
533 * Print the media upload form if permissions are correct
534 *
535 * @author Andreas Gohr <andi@splitbrain.org>
536 */
537function tpl_mediauploadform(){
538  global $NS;
539  global $UPLOADOK;
540  global $lang;
541
542  if(!$UPLOADOK) return;
543
544  ptln('<form action="'.$_SERVER['PHP_SELF'].'" name="upload"'.
545       ' method="post" enctype="multipart/form-data">',2);
546  ptln($lang['txt_upload'].':<br />',4);
547  ptln('<input type="file" name="upload" class="edit" onchange="suggestWikiname();" />',4);
548  ptln('<input type="hidden" name="ns" value="'.hsc($NS).'" /><br />',4);
549  ptln($lang['txt_filename'].'<br />',4);
550  ptln('<input type="text" name="id" class="edit" />',4);
551  ptln('<input type="submit" class="button" value="'.$lang['btn_upload'].'" accesskey="s" />',4);
552  ptln('</form>',2);
553}
554
555
556//Setup VIM: ex: et ts=2 enc=utf-8 :
557