xref: /dokuwiki/inc/template.php (revision bb0a59d46949357f85e4a8deabff585e56b906e5)
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($lockedby);
92      break;
93    case 'login':
94      html_login();
95      break;
96    case 'register':
97      html_register();
98      break;
99    case 'denied':
100      print parsedLocale('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" onsubmit="return svchk()">';
335  print '<input type="hidden" name="do" value="search" />';
336  print '<input type="text" accesskey="f" name="id" class="edit" />';
337  print '<input type="submit" value="'.$lang['btn_search'].'" class="button" />';
338  print '</form>';
339}
340
341/**
342 * Print the breadcrumbs trace
343 *
344 * @author Andreas Gohr <andi@splitbrain.org>
345 */
346function tpl_breadcrumbs(){
347  global $lang;
348  global $conf;
349
350  //check if enabled
351  if(!$conf['breadcrumbs']) return;
352
353  $crumbs = breadcrumbs(); //setup crumb trace
354  print $lang['breadcrumb'].':';
355  foreach ($crumbs as $id => $name){
356    print ' &raquo; ';
357    tpl_link(wl($id),$name,'class="breadcrumbs" title="'.$id.'"');
358  }
359}
360
361/**
362 * Hierarchical breadcrumbs
363 *
364 * This code was suggested as replacement for the usual breadcrumbs
365 * trail in the Wiki and was modified by me.
366 * It only makes sense with a deep site structure.
367 *
368 * @author Andreas Gohr <andi@splitbrain.org>
369 * @link   http://wiki.splitbrain.org/wiki:tipsandtricks:hierarchicalbreadcrumbs
370 */
371function tpl_youarehere(){
372  global $conf;
373  global $ID;
374  global $lang;
375
376
377  $parts     = explode(':', $ID);
378
379  print $lang['breadcrumb'].': ';
380
381  //always print the startpage
382  if( $a_part[0] != $conf['start'] )
383    tpl_link(wl($conf['start']),$conf['start'],'title="'.$conf['start'].'"');
384
385  $page = '';
386  foreach ($parts as $part){
387	  print ' &raquo; ';
388    $page .= $part;
389
390    if(file_exists(wikiFN($page))){
391      tpl_link(wl($page),$part,'title="'.$page.'"');
392    }else{
393      print $page;
394    }
395
396    $page .= ':';
397  }
398}
399
400/**
401 * Print info if the user is logged in
402 *
403 * Could be enhanced with a profile link in future?
404 *
405 * @author Andreas Gohr <andi@splitbrain.org>
406 */
407function tpl_userinfo(){
408  global $lang;
409  if($_SERVER['REMOTE_USER'])
410    print $lang['loggedinas'].': '.$_SERVER['REMOTE_USER'];
411}
412
413/**
414 * Print some info about the current page
415 *
416 * @author Andreas Gohr <andi@splitbrain.org>
417 */
418function tpl_pageinfo(){
419  global $conf;
420  global $lang;
421  global $INFO;
422  global $REV;
423
424  // prepare date and path
425  $fn = $INFO['filepath'];
426  if(!$conf['fullpath']){
427    if($REV){
428      $fn = str_replace(realpath($conf['olddir']).DIRECTORY_SEPARATOR,'',$fn);
429    }else{
430      $fn = str_replace(realpath($conf['datadir']).DIRECTORY_SEPARATOR,'',$fn);
431    }
432  }
433  $fn = utf8_decodeFN($fn);
434  $date = date($conf['dformat'],$INFO['lastmod']);
435
436  // print it
437  if($INFO['exists']){
438    print $fn;
439    print ' &middot; ';
440    print $lang['lastmod'];
441    print ': ';
442    print $date;
443    if($INFO['editor']){
444      print ' '.$lang['by'].' ';
445      print $INFO['editor'];
446    }
447    if($INFO['locked']){
448      print ' &middot; ';
449      print $lang['lockedby'];
450      print ': ';
451      print $INFO['locked'];
452    }
453  }
454}
455
456/**
457 * Print a list of namespaces containing media files
458 *
459 * @author Andreas Gohr <andi@splitbrain.org>
460 */
461function tpl_medianamespaces(){
462	global $conf;
463
464  $data = array();
465  search($data,$conf['mediadir'],'search_namespaces',array());
466  print html_buildlist($data,'idx',media_html_list_namespaces);
467}
468
469/**
470 * Print a list of mediafiles in the current namespace
471 *
472 * @author Andreas Gohr <andi@splitbrain.org>
473 */
474function tpl_mediafilelist(){
475  global $conf;
476  global $lang;
477  global $NS;
478  $dir = utf8_encodeFN(str_replace(':','/',$NS));
479
480  $data = array();
481  search($data,$conf['mediadir'],'search_media',array(),$dir);
482
483  if(!count($data)){
484    ptln('<div class="nothing">'.$lang['nothingfound'].'<div>');
485    return;
486  }
487
488  ptln('<ul>',2);
489  foreach($data as $item){
490    ptln('<li>',4);
491    ptln('<a href="javascript:mediaSelect(\''.$item['id'].'\')">'.
492         utf8_decodeFN($item['file']).
493         '</a>',6);
494    if($item['isimg']){
495      ptln('('.$item['info'][0].'&#215;'.$item['info'][1].
496           ' '.filesize_h($item['size']).')<br />',6);
497
498      # build thumbnail
499      $link=array();
500      $link['name']=$item['id'];
501      if($item['info'][0]>120) $link['name'] .= '?120';
502      $link = format_link_media($link);
503      ptln($link['name'],6);
504
505    }else{
506      ptln ('('.filesize_h($item['size']).')',6);
507    }
508    ptln('</li>',4);
509  }
510  ptln('</ul>',2);
511}
512
513/**
514 * Print the media upload form if permissions are correct
515 *
516 * @author Andreas Gohr <andi@splitbrain.org>
517 */
518function tpl_mediauploadform(){
519  global $NS;
520  global $UPLOADOK;
521  global $lang;
522
523  if(!$UPLOADOK) return;
524
525  ptln('<form action="'.$_SERVER['PHP_SELF'].'" name="upload"'.
526       ' method="post" enctype="multipart/form-data">',2);
527  ptln($lang['txt_upload'].':<br />',4);
528  ptln('<input type="file" name="upload" class="edit" onchange="suggestWikiname();" />',4);
529  ptln('<input type="hidden" name="ns" value="'.hsc($NS).'" /><br />',4);
530  ptln($lang['txt_filename'].'<br />',4);
531  ptln('<input type="text" name="id" class="edit" />',4);
532  ptln('<input type="submit" class="button" value="'.$lang['btn_upload'].'" accesskey="s" />',4);
533  ptln('</form>',2);
534}
535
536
537//Setup VIM: ex: et ts=2 enc=utf-8 :
538