xref: /dokuwiki/inc/template.php (revision 0cecf9d507451346a32ddf45a85b425784fbb0f8)
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 * Print the search form
250 *
251 * @author Andreas Gohr <andi@splitbrain.org>
252 */
253function tpl_searchform(){
254  global $lang;
255  print '<form action="'.wl().'" accept-charset="utf-8" class="search" onsubmit="return svchk()">';
256  print '<input type="hidden" name="do" value="search" />';
257  print '<input type="text" accesskey="f" name="id" class="edit" />';
258  print '<input type="submit" value="'.$lang['btn_search'].'" class="button" />';
259  print '</form>';
260}
261
262/**
263 * Print the breadcrumbs trace
264 *
265 * @author Andreas Gohr <andi@splitbrain.org>
266 */
267function tpl_breadcrumbs(){
268  global $lang;
269  global $conf;
270
271  //check if enabled
272  if(!$conf['breadcrumbs']) return;
273
274  $crumbs = breadcrumbs(); //setup crumb trace
275  print $lang['breadcrumb'].':';
276  foreach ($crumbs as $id => $name){
277    print ' &raquo; ';
278    tpl_link(wl($id),$name,'class="breadcrumbs" title="'.$id.'"');
279  }
280}
281
282/**
283 * Hierarchical breadcrumbs
284 *
285 * This code was suggested as replacement for the usual breadcrumbs
286 * trail in the Wiki and was modified by me.
287 * It only makes sense with a deep site structure.
288 *
289 * @author Andreas Gohr <andi@splitbrain.org>
290 * @link   http://wiki.splitbrain.org/wiki:tipsandtricks:hierarchicalbreadcrumbs
291 */
292function tpl_youarehere(){
293  global $conf;
294  global $ID;
295  global $lang;
296
297
298  $parts     = explode(':', $ID);
299
300  print $lang['breadcrumb'].': ';
301
302  //always print the startpage
303  if( $a_part[0] != $conf['start'] )
304    tpl_link(wl($conf['start']),$conf['start'],'title="'.$conf['start'].'"');
305
306  $page = '';
307  foreach ($parts as $part){
308	  print ' &raquo; ';
309    $page .= $part;
310
311    if(file_exists(wikiFN($page))){
312      tpl_link(wl($page),$part,'title="'.$page.'"');
313    }else{
314      print $page;
315    }
316
317    $page .= ':';
318  }
319}
320
321/**
322 * Print info if the user is logged in
323 *
324 * Could be enhanced with a profile link in future?
325 *
326 * @author Andreas Gohr <andi@splitbrain.org>
327 */
328function tpl_userinfo(){
329  global $lang;
330  if($_SERVER['REMOTE_USER'])
331    print $lang['loggedinas'].': '.$_SERVER['REMOTE_USER'];
332}
333
334/**
335 * Print some info about the current page
336 *
337 * @author Andreas Gohr <andi@splitbrain.org>
338 */
339function tpl_pageinfo(){
340  global $conf;
341  global $lang;
342  global $INFO;
343  global $REV;
344
345  // prepare date and path
346  $fn = $INFO['filepath'];
347  if(!$conf['fullpath']){
348    if($REV){
349      $fn = str_replace(realpath($conf['olddir']).DIRECTORY_SEPARATOR,'',$fn);
350    }else{
351      $fn = str_replace(realpath($conf['datadir']).DIRECTORY_SEPARATOR,'',$fn);
352    }
353  }
354  $fn = utf8_decodeFN($fn);
355  $date = date($conf['dformat'],$INFO['lastmod']);
356
357  // print it
358  if($INFO['exists']){
359    print $fn;
360    print ' &middot; ';
361    print $lang['lastmod'];
362    print ': ';
363    print $date;
364    if($INFO['editor']){
365      print ' '.$lang['by'].' ';
366      print $INFO['editor'];
367    }
368    if($INFO['locked']){
369      print ' &middot; ';
370      print $lang['lockedby'];
371      print ': ';
372      print $INFO['locked'];
373    }
374  }
375}
376
377/**
378 * Print a list of namespaces containing media files
379 *
380 * @author Andreas Gohr <andi@splitbrain.org>
381 */
382function tpl_medianamespaces(){
383	global $conf;
384
385  $data = array();
386  search($data,$conf['mediadir'],'search_namespaces',array());
387  print html_buildlist($data,'idx',media_html_list_namespaces);
388}
389
390/**
391 * Print a list of mediafiles in the current namespace
392 *
393 * @author Andreas Gohr <andi@splitbrain.org>
394 */
395function tpl_mediafilelist(){
396  global $conf;
397  global $lang;
398  global $NS;
399  $dir = utf8_encodeFN(str_replace(':','/',$NS));
400
401  $data = array();
402  search($data,$conf['mediadir'],'search_media',array(),$dir);
403
404  if(!count($data)){
405    ptln('<div class="nothing">'.$lang['nothingfound'].'<div>');
406    return;
407  }
408
409  ptln('<ul>',2);
410  foreach($data as $item){
411    ptln('<li>',4);
412    ptln('<a href="javascript:mediaSelect(\''.$item['id'].'\')">'.
413         utf8_decodeFN($item['file']).
414         '</a>',6);
415    if($item['isimg']){
416      ptln('('.$item['info'][0].'&#215;'.$item['info'][1].
417           ' '.filesize_h($item['size']).')<br />',6);
418
419      # build thumbnail
420      $link=array();
421      $link['name']=$item['id'];
422      if($item['info'][0]>120) $link['name'] .= '?120';
423      $link = format_link_media($link);
424      ptln($link['name'],6);
425
426    }else{
427      ptln ('('.filesize_h($item['size']).')',6);
428    }
429    ptln('</li>',4);
430  }
431  ptln('</ul>',2);
432}
433
434/**
435 * Print the media upload form if permissions are correct
436 *
437 * @author Andreas Gohr <andi@splitbrain.org>
438 */
439function tpl_mediauploadform(){
440  global $NS;
441  global $UPLOADOK;
442  global $lang;
443
444  if(!$UPLOADOK) return;
445
446  ptln('<form action="'.$_SERVER['PHP_SELF'].'" name="upload"'.
447       ' method="post" enctype="multipart/form-data">',2);
448  ptln($lang['txt_upload'].':<br />',4);
449  ptln('<input type="file" name="upload" class="edit" onchange="suggestWikiname();" />',4);
450  ptln('<input type="hidden" name="ns" value="'.hsc($NS).'" /><br />',4);
451  ptln($lang['txt_filename'].'<br />',4);
452  ptln('<input type="text" name="id" class="edit" />',4);
453  ptln('<input type="submit" class="button" value="'.$lang['btn_upload'].'" accesskey="s" />',4);
454  ptln('</form>',2);
455}
456
457?>
458