xref: /dokuwiki/inc/template.php (revision c19fe9c0f68f58ff9c18f0e185a5bc6b591bf798)
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 * @todo   add a hierachical breadcrumb function
266 * @author Andreas Gohr <andi@splitbrain.org>
267 */
268function tpl_breadcrumbs(){
269  global $lang;
270  global $conf;
271
272  //check if enabled
273  if(!$conf['breadcrumbs']) return;
274
275  $crumbs = breadcrumbs(); //setup crumb trace
276  print $lang['breadcrumb'].':';
277  foreach ($crumbs as $crumb){
278    print ' &raquo; ';
279    tpl_link(wl($crumb),noNS($crumb),'class="breadcrumbs" title="'.$crumb.'"');
280  }
281}
282
283/**
284 * Print info if the user is logged in
285 *
286 * Could be enhanced with a profile link in future?
287 *
288 * @author Andreas Gohr <andi@splitbrain.org>
289 */
290function tpl_userinfo(){
291  global $lang;
292  if($_SERVER['REMOTE_USER'])
293    print $lang['loggedinas'].': '.$_SERVER['REMOTE_USER'];
294}
295
296/**
297 * Print some info about the current page
298 *
299 * @author Andreas Gohr <andi@splitbrain.org>
300 */
301function tpl_pageinfo(){
302  global $conf;
303  global $lang;
304  global $INFO;
305  global $REV;
306
307  // prepare date and path
308  $fn = $INFO['filepath'];
309  if(!$conf['fullpath']){
310    if($REV){
311      $fn = str_replace(realpath($conf['olddir']).DIRECTORY_SEPARATOR,'',$fn);
312    }else{
313      $fn = str_replace(realpath($conf['datadir']).DIRECTORY_SEPARATOR,'',$fn);
314    }
315  }
316  $date = date($conf['dformat'],$INFO['lastmod']);
317
318  // print it
319  if($INFO['exists']){
320    print $fn;
321    print ' &middot; ';
322    print $lang['lastmod'];
323    print ': ';
324    print $date;
325    if($INFO['editor']){
326      print ' '.$lang['by'].' ';
327      print $INFO['editor'];
328    }
329    if($INFO['locked']){
330      print ' &middot; ';
331      print $lang['lockedby'];
332      print ': ';
333      print $INFO['locked'];
334    }
335  }
336}
337
338/**
339 * Print a list of namespaces containing media files
340 *
341 * @author Andreas Gohr <andi@splitbrain.org>
342 */
343function tpl_medianamespaces(){
344	global $conf;
345
346  $data = array();
347  search($data,$conf['mediadir'],'search_namespaces',array());
348  print html_buildlist($data,'idx',media_html_list_namespaces);
349}
350
351/**
352 * Print a list of mediafiles in the current namespace
353 *
354 * @author Andreas Gohr <andi@splitbrain.org>
355 */
356function tpl_mediafilelist(){
357  global $conf;
358  global $lang;
359  global $NS;
360  $dir = utf8_encodeFN(str_replace(':','/',$NS));
361
362  $data = array();
363  search($data,$conf['mediadir'],'search_media',array(),$dir);
364
365  if(!count($data)){
366    ptln('<div class="nothing">'.$lang['nothingfound'].'<div>');
367    return;
368  }
369
370  ptln('<ul>',2);
371  foreach($data as $item){
372    ptln('<li>',4);
373    ptln('<a href="javascript:mediaSelect(\''.$item['id'].'\')">'.
374         utf8_decodeFN($item['file']).
375         '</a>',6);
376    if($item['isimg']){
377      ptln('('.$item['info'][0].'&#215;'.$item['info'][1].
378           ' '.filesize_h($item['size']).')<br />',6);
379
380      # build thumbnail
381      $link=array();
382      $link['name']=$item['id'];
383      if($item['info'][0]>120) $link['name'] .= '?120';
384      $link = format_link_media($link);
385      ptln($link['name'],6);
386
387    }else{
388      ptln ('('.filesize_h($item['size']).')',6);
389    }
390    ptln('</li>',4);
391  }
392  ptln('</ul>',2);
393}
394
395/**
396 * Print the media upload form if permissions are correct
397 *
398 * @author Andreas Gohr <andi@splitbrain.org>
399 */
400function tpl_mediauploadform(){
401  global $NS;
402  global $UPLOADOK;
403  global $lang;
404
405  if(!$UPLOADOK) return;
406
407  ptln('<form action="'.$_SERVER['PHP_SELF'].'" name="upload"'.
408       ' method="post" enctype="multipart/form-data">',2);
409  ptln($lang['txt_upload'].':<br />',4);
410  ptln('<input type="file" name="upload" class="edit" onchange="suggestWikiname();" />',4);
411  ptln('<input type="hidden" name="ns" value="'.hsc($NS).'" /><br />',4);
412  ptln($lang['txt_filename'].'<br />',4);
413  ptln('<input type="text" name="id" class="edit" />',4);
414  ptln('<input type="submit" class="button" value="'.$lang['btn_upload'].'" accesskey="s" />',4);
415  ptln('</form>',2);
416}
417
418?>
419