xref: /dokuwiki/inc/html.php (revision 124af657c8d7563757e72334573c08aec6457680)
1<?php
2/**
3 * HTML output functions
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
10if(!defined('NL')) define('NL',"\n");
11require_once(DOKU_INC.'inc/parserutils.php');
12require_once(DOKU_INC.'inc/form.php');
13
14/**
15 * Convenience function to quickly build a wikilink
16 *
17 * @author Andreas Gohr <andi@splitbrain.org>
18 */
19function html_wikilink($id,$name=NULL,$search=''){
20  static $xhtml_renderer = NULL;
21  if(is_null($xhtml_renderer)){
22    require_once(DOKU_INC.'inc/parser/xhtml.php');
23    $xhtml_renderer = new Doku_Renderer_xhtml();
24  }
25
26  return $xhtml_renderer->internallink($id,$name,$search,true);
27}
28
29/**
30 * Helps building long attribute lists
31 *
32 * @author Andreas Gohr <andi@splitbrain.org>
33 */
34function html_attbuild($attributes){
35  $ret = '';
36  foreach ( $attributes as $key => $value ) {
37    $ret .= $key.'="'.formtext($value).'" ';
38  }
39  return trim($ret);
40}
41
42/**
43 * The loginform
44 *
45 * @author   Andreas Gohr <andi@splitbrain.org>
46 */
47function html_login(){
48  global $lang;
49  global $conf;
50  global $ID;
51  global $auth;
52
53  print p_locale_xhtml('login');
54  print '<div class="centeralign">'.NL;
55  $form = new Doku_Form('dw__login');
56  $form->startFieldset($lang['btn_login']);
57  $form->addHidden('id', $ID);
58  $form->addHidden('do', 'login');
59  $form->addElement(form_makeTextField('u', $_REQUEST['u'], $lang['user'], 'focus__this', 'block'));
60  $form->addElement(form_makePasswordField('p', $lang['pass'], '', 'block'));
61  $form->addElement(form_makeCheckboxField('r', '1', $lang['remember'], 'remember__me', 'simple'));
62  $form->addElement(form_makeButton('submit', '', $lang['btn_login']));
63  $form->endFieldset();
64  html_form('login', $form);
65
66  if($auth && $auth->canDo('addUser') && actionOK('register')){
67    print '<p>';
68    print $lang['reghere'];
69    print ': <a href="'.wl($ID,'do=register').'" rel="nofollow" class="wikilink1">'.$lang['register'].'</a>';
70    print '</p>';
71  }
72
73  if ($auth && $auth->canDo('modPass') && actionOK('resendpwd')) {
74    print '<p>';
75    print $lang['pwdforget'];
76    print ': <a href="'.wl($ID,'do=resendpwd').'" rel="nofollow" class="wikilink1">'.$lang['btn_resendpwd'].'</a>';
77    print '</p>';
78  }
79  print '</div>'.NL;
80}
81
82/**
83 * prints a section editing button
84 * used as a callback in html_secedit
85 *
86 * @author Andreas Gohr <andi@splitbrain.org>
87 */
88function html_secedit_button($matches){
89  global $ID;
90  global $INFO;
91
92  $section = $matches[2];
93  $name = $matches[1];
94
95  $secedit  = '';
96  $secedit .= '<div class="secedit">';
97  $secedit .= html_btn('secedit',$ID,'',
98                        array('do'      => 'edit',
99                              'lines'   => "$section",
100                              'rev' => $INFO['lastmod']),
101                              'post', $name);
102  $secedit .= '</div>';
103  return $secedit;
104}
105
106/**
107 * inserts section edit buttons if wanted or removes the markers
108 *
109 * @author Andreas Gohr <andi@splitbrain.org>
110 */
111function html_secedit($text,$show=true){
112  global $INFO;
113
114  if($INFO['writable'] && $show && !$INFO['rev']){
115    $text = preg_replace_callback('#<!-- SECTION "(.*?)" \[(\d+-\d*)\] -->#',
116                         'html_secedit_button', $text);
117  }else{
118    $text = preg_replace('#<!-- SECTION "(.*?)" \[(\d+-\d*)\] -->#','',$text);
119  }
120
121  return $text;
122}
123
124/**
125 * Just the back to top button (in its own form)
126 *
127 * @author Andreas Gohr <andi@splitbrain.org>
128 */
129function html_topbtn(){
130  global $lang;
131
132  $ret  = '';
133  $ret  = '<a class="nolink" href="#dokuwiki__top"><input type="button" class="button" value="'.$lang['btn_top'].'" onclick="window.scrollTo(0, 0)" title="'.$lang['btn_top'].'" /></a>';
134
135  return $ret;
136}
137
138/**
139 * Displays a button (using its own form)
140 * If tooltip exists, the access key tooltip is replaced.
141 *
142 * @author Andreas Gohr <andi@splitbrain.org>
143 */
144function html_btn($name,$id,$akey,$params,$method='get',$tooltip=''){
145  global $conf;
146  global $lang;
147
148  $label = $lang['btn_'.$name];
149
150  $ret = '';
151  $tip = '';
152
153  //filter id (without urlencoding)
154  $id = idfilter($id,false);
155
156  //make nice URLs even for buttons
157  if($conf['userewrite'] == 2){
158    $script = DOKU_BASE.DOKU_SCRIPT.'/'.$id;
159  }elseif($conf['userewrite']){
160    $script = DOKU_BASE.$id;
161  }else{
162    $script = DOKU_BASE.DOKU_SCRIPT;
163    $params['id'] = $id;
164  }
165
166  $ret .= '<form class="button btn_'.$name.'" method="'.$method.'" action="'.$script.'"><div class="no">';
167
168  if(is_array($params)){
169    reset($params);
170    while (list($key, $val) = each($params)) {
171      $ret .= '<input type="hidden" name="'.$key.'" ';
172      $ret .= 'value="'.htmlspecialchars($val).'" />';
173    }
174  }
175
176  if ($tooltip!='') {
177      $tip = htmlspecialchars($tooltip);
178  }else{
179      $tip = htmlspecialchars($label);
180  }
181
182  $ret .= '<input type="submit" value="'.htmlspecialchars($label).'" class="button" ';
183  if($akey){
184    $tip .= ' [ALT+'.strtoupper($akey).']';
185    $ret .= 'accesskey="'.$akey.'" ';
186  }
187  $ret .= 'title="'.$tip.'" ';
188  $ret .= '/>';
189  $ret .= '</div></form>';
190
191  return $ret;
192}
193
194/**
195 * show a wiki page
196 *
197 * @author Andreas Gohr <andi@splitbrain.org>
198 */
199function html_show($txt=''){
200  global $ID;
201  global $REV;
202  global $HIGH;
203  global $INFO;
204  //disable section editing for old revisions or in preview
205  if($txt || $REV){
206    $secedit = false;
207  }else{
208    $secedit = true;
209  }
210
211  if ($txt){
212    //PreviewHeader
213    echo '<br id="scroll__here" />';
214    echo p_locale_xhtml('preview');
215    echo '<div class="preview">';
216    $html = html_secedit(p_render('xhtml',p_get_instructions($txt),$info),$secedit);
217    if($INFO['prependTOC']) $html = tpl_toc(true).$html;
218    echo $html;
219    echo '<div class="clearer"></div>';
220    echo '</div>';
221
222  }else{
223    if ($REV) print p_locale_xhtml('showrev');
224    $html = p_wiki_xhtml($ID,$REV,true);
225    $html = html_secedit($html,$secedit);
226    if($INFO['prependTOC']) $html = tpl_toc(true).$html;
227    $html = html_hilight($html,$HIGH);
228    echo $html;
229  }
230}
231
232/**
233 * ask the user about how to handle an exisiting draft
234 *
235 * @author Andreas Gohr <andi@splitbrain.org>
236 */
237function html_draft(){
238  global $INFO;
239  global $ID;
240  global $lang;
241  global $conf;
242  $draft = unserialize(io_readFile($INFO['draft'],false));
243  $text  = cleanText(con($draft['prefix'],$draft['text'],$draft['suffix'],true));
244
245  print p_locale_xhtml('draft');
246  $form = new Doku_Form('dw__editform');
247  $form->addHidden('id', $ID);
248  $form->addHidden('date', $draft['date']);
249  $form->addElement(form_makeWikiText($text, array('readonly'=>'readonly')));
250  $form->addElement(form_makeOpenTag('div', array('id'=>'draft__status')));
251  $form->addElement($lang['draftdate'].' '. strftime($conf['dformat'],filemtime($INFO['draft'])));
252  $form->addElement(form_makeCloseTag('div'));
253  $form->addElement(form_makeButton('submit', 'recover', $lang['btn_recover'], array('tabindex'=>'1')));
254  $form->addElement(form_makeButton('submit', 'draftdel', $lang['btn_draftdel'], array('tabindex'=>'2')));
255  $form->addElement(form_makeButton('submit', 'show', $lang['btn_cancel'], array('tabindex'=>'3')));
256  html_form('draft', $form);
257}
258
259/**
260 * Highlights searchqueries in HTML code
261 *
262 * @author Andreas Gohr <andi@splitbrain.org>
263 * @author Harry Fuecks <hfuecks@gmail.com>
264 */
265function html_hilight($html,$regex){
266  // strip everything that's special except pipes:
267  $regex = preg_replace('![\[\]()/\\\\?\.+*]+!','',$regex);
268  $regex = join('|',array_filter(array_map('trim',explode('|',$regex))));
269
270  if ($regex === '') return $html;
271  $html = preg_replace_callback("/((<[^>]*)|$regex)/ui",'html_hilight_callback',$html);
272  return $html;
273}
274
275/**
276 * Callback used by html_hilight()
277 *
278 * @author Harry Fuecks <hfuecks@gmail.com>
279 */
280function html_hilight_callback($m) {
281  $hlight = unslash($m[0]);
282  if ( !isset($m[2])) {
283    $hlight = '<span class="search_hit">'.$hlight.'</span>';
284  }
285  return $hlight;
286}
287
288/**
289 * Run a search and display the result
290 *
291 * @author Andreas Gohr <andi@splitbrain.org>
292 */
293function html_search(){
294  require_once(DOKU_INC.'inc/search.php');
295  require_once(DOKU_INC.'inc/fulltext.php');
296  global $conf;
297  global $QUERY;
298  global $ID;
299  global $lang;
300
301  print p_locale_xhtml('searchpage');
302  flush();
303
304  //check if search is restricted to namespace
305  if(preg_match('/([^@]*)@([^@]*)/',$QUERY,$match)) {
306      $id = cleanID($match[1]);
307      if(empty($id)) {
308        print '<div class="nothing">'.$lang['nothingfound'].'</div>';
309        flush();
310        return;
311      }
312  } else {
313      $id = cleanID($QUERY);
314  }
315
316  //show progressbar
317  print '<div class="centeralign" id="dw__loading">'.NL;
318  print '<script type="text/javascript" charset="utf-8"><!--//--><![CDATA[//><!--'.NL;
319  print 'showLoadBar();'.NL;
320  print '//--><!]]></script>'.NL;
321  print '<br /></div>'.NL;
322  flush();
323
324  //do quick pagesearch
325  $data = array();
326
327  $data = ft_pageLookup($id);
328  if(count($data)){
329    sort($data);
330    print '<div class="search_quickresult">';
331    print '<h3>'.$lang['quickhits'].':</h3>';
332    print '<ul class="search_quickhits">';
333    foreach($data as $id){
334      print '<li> ';
335      print html_wikilink(':'.$id,noNS($id));
336      print '</li> ';
337    }
338    print '</ul> ';
339    //clear float (see http://www.complexspiral.com/publications/containing-floats/)
340    print '<div class="clearer">&nbsp;</div>';
341    print '</div>';
342  }
343  flush();
344
345  //do fulltext search
346  $data = ft_pageSearch($QUERY,$regex);
347  if(count($data)){
348    $num = 1;
349    foreach($data as $id => $cnt){
350      print '<div class="search_result">';
351      print html_wikilink(':'.$id,$conf['useheading']?NULL:$id,$regex);
352      print ': <span class="search_cnt">'.$cnt.' '.$lang['hits'].'</span><br />';
353      if($num < 15){ // create snippets for the first number of matches only #FIXME add to conf ?
354        print '<div class="search_snippet">'.ft_snippet($id,$regex).'</div>';
355      }
356      print '</div>';
357      flush();
358      $num++;
359    }
360  }else{
361    print '<div class="nothing">'.$lang['nothingfound'].'</div>';
362  }
363
364  //hide progressbar
365  print '<script type="text/javascript" charset="utf-8"><!--//--><![CDATA[//><!--'.NL;
366  print 'hideLoadBar("dw__loading");'.NL;
367  print '//--><!]]></script>'.NL;
368  flush();
369}
370
371/**
372 * Display error on locked pages
373 *
374 * @author Andreas Gohr <andi@splitbrain.org>
375 */
376function html_locked(){
377  global $ID;
378  global $conf;
379  global $lang;
380  global $INFO;
381
382  $locktime = filemtime(wikiLockFN($ID));
383  $expire = @strftime($conf['dformat'], $locktime + $conf['locktime'] );
384  $min    = round(($conf['locktime'] - (time() - $locktime) )/60);
385
386  print p_locale_xhtml('locked');
387  print '<ul>';
388  print '<li><div class="li"><strong>'.$lang['lockedby'].':</strong> '.$INFO['locked'].'</li>';
389  print '<li><div class="li"><strong>'.$lang['lockexpire'].':</strong> '.$expire.' ('.$min.' min)</div></li>';
390  print '</ul>';
391}
392
393/**
394 * list old revisions
395 *
396 * @author Andreas Gohr <andi@splitbrain.org>
397 * @author Ben Coburn <btcoburn@silicodon.net>
398 */
399function html_revisions($first=0){
400  global $ID;
401  global $INFO;
402  global $conf;
403  global $lang;
404  /* we need to get one additionally log entry to be able to
405   * decide if this is the last page or is there another one.
406   * see html_recent()
407   */
408  $revisions = getRevisions($ID, $first, $conf['recent']+1);
409  if(count($revisions)==0 && $first!=0){
410    $first=0;
411    $revisions = getRevisions($ID, $first, $conf['recent']+1);;
412  }
413  $hasNext = false;
414  if (count($revisions)>$conf['recent']) {
415    $hasNext = true;
416    array_pop($revisions); // remove extra log entry
417  }
418
419  $date = @strftime($conf['dformat'],$INFO['lastmod']);
420
421  print p_locale_xhtml('revisions');
422  print '<form action="'.wl($ID).'" method="post" id="page__revisions">';
423  print '<ul>';
424  if($INFO['exists'] && $first==0){
425    print (isset($INFO['meta']) && isset($INFO['meta']['last_change']) && $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) ? '<li class="minor">' : '<li>';
426    print '<div class="li">';
427    print '<input type="checkbox" name="rev2[]" value="current" /> ';
428
429    print $date;
430
431    print ' <img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" /> ';
432
433    print '<a class="wikilink1" href="'.wl($ID).'">'.$ID.'</a> ';
434
435    print ' &ndash; ';
436    print $INFO['sum'];
437    print ' <span class="user">';
438    print (empty($INFO['editor']))?('('.$lang['external_edit'].')'):$INFO['editor'];
439    print '</span> ';
440
441    print '('.$lang['current'].')';
442    print '</div>';
443    print '</li>';
444  }
445
446  foreach($revisions as $rev){
447    $date   = strftime($conf['dformat'],$rev);
448    $info   = getRevisionInfo($ID,$rev,true);
449    $exists = page_exists($ID,$rev);
450
451    print ($info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) ? '<li class="minor">' : '<li>';
452    print '<div class="li">';
453    if($exists){
454      print '<input type="checkbox" name="rev2[]" value="'.$rev.'" /> ';
455    }else{
456      print '<img src="'.DOKU_BASE.'lib/images/blank.gif" width="14" height="11" alt="" /> ';
457    }
458    print $date;
459
460    if($exists){
461      print ' <a href="'.wl($ID,"rev=$rev,do=diff").'">';
462      $p = array();
463      $p['src']    = DOKU_BASE.'lib/images/diff.png';
464      $p['width']  = 15;
465      $p['height'] = 11;
466      $p['title']  = $lang['diff'];
467      $p['alt']    = $lang['diff'];
468      $att = buildAttributes($p);
469      print "<img $att />";
470      print '</a> ';
471
472      print '<a class="wikilink1" href="'.wl($ID,"rev=$rev").'">'.$ID.'</a>';
473    }else{
474      print ' <img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" /> ';
475      print $ID;
476    }
477
478    print ' &ndash; ';
479    print htmlspecialchars($info['sum']);
480    print ' <span class="user">';
481    if($info['user']){
482      print $info['user'];
483    }else{
484      print $info['ip'];
485    }
486    print '</span>';
487
488    print '</div>';
489    print '</li>';
490  }
491  print '</ul>';
492  print '<input name="do[diff]" type="submit" value="'.$lang['diff2'].'" class="button" />';
493  print '</form>';
494
495  print '<div class="pagenav">';
496  $last = $first + $conf['recent'];
497  if ($first > 0) {
498    $first -= $conf['recent'];
499    if ($first < 0) $first = 0;
500    print '<div class="pagenav-prev">';
501    print html_btn('newer',$ID,"p",array('do' => 'revisions', 'first' => $first));
502    print '</div>';
503  }
504  if ($hasNext) {
505    print '<div class="pagenav-next">';
506    print html_btn('older',$ID,"n",array('do' => 'revisions', 'first' => $last));
507    print '</div>';
508  }
509  print '</div>';
510
511}
512
513/**
514 * display recent changes
515 *
516 * @author Andreas Gohr <andi@splitbrain.org>
517 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
518 * @author Ben Coburn <btcoburn@silicodon.net>
519 */
520function html_recent($first=0){
521  global $conf;
522  global $lang;
523  global $ID;
524  /* we need to get one additionally log entry to be able to
525   * decide if this is the last page or is there another one.
526   * This is the cheapest solution to get this information.
527   */
528  $recents = getRecents($first,$conf['recent'] + 1,getNS($ID));
529  if(count($recents) == 0 && $first != 0){
530    $first=0;
531    $recents = getRecents($first,$conf['recent'] + 1,getNS($ID));
532  }
533  $hasNext = false;
534  if (count($recents)>$conf['recent']) {
535    $hasNext = true;
536    array_pop($recents); // remove extra log entry
537  }
538
539  print p_locale_xhtml('recent');
540  print '<ul>';
541
542  foreach($recents as $recent){
543    $date = strftime($conf['dformat'],$recent['date']);
544    print ($recent['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) ? '<li class="minor">' : '<li>';
545    print '<div class="li">';
546
547    print $date.' ';
548
549    print '<a href="'.wl($recent['id'],"do=diff").'">';
550    $p = array();
551    $p['src']    = DOKU_BASE.'lib/images/diff.png';
552    $p['width']  = 15;
553    $p['height'] = 11;
554    $p['title']  = $lang['diff'];
555    $p['alt']    = $lang['diff'];
556    $att = buildAttributes($p);
557    print "<img $att />";
558    print '</a> ';
559
560    print '<a href="'.wl($recent['id'],"do=revisions").'">';
561    $p = array();
562    $p['src']    = DOKU_BASE.'lib/images/history.png';
563    $p['width']  = 12;
564    $p['height'] = 14;
565    $p['title']  = $lang['btn_revs'];
566    $p['alt']    = $lang['btn_revs'];
567    $att = buildAttributes($p);
568    print "<img $att />";
569    print '</a> ';
570
571    print html_wikilink(':'.$recent['id'],$conf['useheading']?NULL:$recent['id']);
572    print ' &ndash; '.htmlspecialchars($recent['sum']);
573
574    print ' <span class="user">';
575    if($recent['user']){
576      print $recent['user'];
577    }else{
578      print $recent['ip'];
579    }
580    print '</span>';
581
582    print '</div>';
583    print '</li>';
584  }
585  print '</ul>';
586
587  print '<div class="pagenav">';
588  $last = $first + $conf['recent'];
589  if ($first > 0) {
590    $first -= $conf['recent'];
591    if ($first < 0) $first = 0;
592    print '<div class="pagenav-prev">';
593    print html_btn('newer','',"p",array('do' => 'recent', 'first' => $first));
594    print '</div>';
595  }
596  if ($hasNext) {
597    print '<div class="pagenav-next">';
598    print html_btn('older','',"n",array('do' => 'recent', 'first' => $last));
599    print '</div>';
600  }
601  print '</div>';
602}
603
604/**
605 * Display page index
606 *
607 * @author Andreas Gohr <andi@splitbrain.org>
608 */
609function html_index($ns){
610  require_once(DOKU_INC.'inc/search.php');
611  global $conf;
612  global $ID;
613  $dir = $conf['datadir'];
614  $ns  = cleanID($ns);
615  #fixme use appropriate function
616  if(empty($ns)){
617    $ns = dirname(str_replace(':','/',$ID));
618    if($ns == '.') $ns ='';
619  }
620  $ns  = utf8_encodeFN(str_replace(':','/',$ns));
621
622  echo p_locale_xhtml('index');
623  echo '<div id="index__tree">';
624
625  $data = array();
626  search($data,$conf['datadir'],'search_index',array('ns' => $ns));
627  echo html_buildlist($data,'idx','html_list_index','html_li_index');
628
629  echo '</div>';
630}
631
632/**
633 * Index item formatter
634 *
635 * User function for html_buildlist()
636 *
637 * @author Andreas Gohr <andi@splitbrain.org>
638 */
639function html_list_index($item){
640  global $ID;
641  $ret = '';
642  $base = ':'.$item['id'];
643  $base = substr($base,strrpos($base,':')+1);
644  if($item['type']=='d'){
645    $ret .= '<a href="'.wl($ID,'idx='.rawurlencode($item['id'])).'" class="idx_dir"><strong>';
646    $ret .= $base;
647    $ret .= '</strong></a>';
648  }else{
649    $ret .= html_wikilink(':'.$item['id']);
650  }
651  return $ret;
652}
653
654/**
655 * Index List item
656 *
657 * This user function is used in html_build_lidt to build the
658 * <li> tags for namespaces when displaying the page index
659 * it gives different classes to opened or closed "folders"
660 *
661 * @author Andreas Gohr <andi@splitbrain.org>
662 */
663function html_li_index($item){
664  if($item['type'] == "f"){
665    return '<li class="level'.$item['level'].'">';
666  }elseif($item['open']){
667    return '<li class="open">';
668  }else{
669    return '<li class="closed">';
670  }
671}
672
673/**
674 * Default List item
675 *
676 * @author Andreas Gohr <andi@splitbrain.org>
677 */
678function html_li_default($item){
679  return '<li class="level'.$item['level'].'">';
680}
681
682/**
683 * Build an unordered list
684 *
685 * Build an unordered list from the given $data array
686 * Each item in the array has to have a 'level' property
687 * the item itself gets printed by the given $func user
688 * function. The second and optional function is used to
689 * print the <li> tag. Both user function need to accept
690 * a single item.
691 *
692 * Both user functions can be given as array to point to
693 * a member of an object.
694 *
695 * @author Andreas Gohr <andi@splitbrain.org>
696 */
697function html_buildlist($data,$class,$func,$lifunc='html_li_default'){
698  $level = 0;
699  $opens = 0;
700  $ret   = '';
701
702  foreach ($data as $item){
703
704    if( $item['level'] > $level ){
705      //open new list
706      for($i=0; $i<($item['level'] - $level); $i++){
707        if ($i) $ret .= "<li class=\"clear\">\n";
708        $ret .= "\n<ul class=\"$class\">\n";
709      }
710    }elseif( $item['level'] < $level ){
711      //close last item
712      $ret .= "</li>\n";
713      for ($i=0; $i<($level - $item['level']); $i++){
714        //close higher lists
715        $ret .= "</ul>\n</li>\n";
716      }
717    }else{
718      //close last item
719      $ret .= "</li>\n";
720    }
721
722    //remember current level
723    $level = $item['level'];
724
725    //print item
726    $ret .= call_user_func($lifunc,$item);
727    $ret .= '<div class="li">';
728
729    $ret .= call_user_func($func,$item);
730    $ret .= '</div>';
731  }
732
733  //close remaining items and lists
734  for ($i=0; $i < $level; $i++){
735    $ret .= "</li></ul>\n";
736  }
737
738  return $ret;
739}
740
741/**
742 * display backlinks
743 *
744 * @author Andreas Gohr <andi@splitbrain.org>
745 * @author Michael Klier <chi@chimeric.de>
746 */
747function html_backlinks(){
748  require_once(DOKU_INC.'inc/fulltext.php');
749  global $ID;
750  global $conf;
751  global $lang;
752
753  print p_locale_xhtml('backlinks');
754
755  $data = ft_backlinks($ID);
756
757  if(!empty($data)) {
758      print '<ul class="idx">';
759      foreach($data as $blink){
760        print '<li><div class="li">';
761        print html_wikilink(':'.$blink,$conf['useheading']?NULL:$blink);
762        print '</div></li>';
763      }
764      print '</ul>';
765  } else {
766      print '<div class="level1"><p>' . $lang['nothingfound'] . '</p></div>';
767  }
768}
769
770/**
771 * show diff
772 *
773 * @author Andreas Gohr <andi@splitbrain.org>
774 */
775function html_diff($text='',$intro=true){
776  require_once(DOKU_INC.'inc/DifferenceEngine.php');
777  global $ID;
778  global $REV;
779  global $lang;
780  global $conf;
781
782  // we're trying to be clever here, revisions to compare can be either
783  // given as rev and rev2 parameters, with rev2 being optional. Or in an
784  // array in rev2.
785  $rev1 = $REV;
786
787  if(is_array($_REQUEST['rev2'])){
788    $rev1 = (int) $_REQUEST['rev2'][0];
789    $rev2 = (int) $_REQUEST['rev2'][1];
790  }else{
791    $rev2 = (int) $_REQUEST['rev2'];
792  }
793
794  if($text){                      // compare text to the most current revision
795    $l_rev   = '';
796    $l_text  = rawWiki($ID,'');
797    $l_head  = '<a class="wikilink1" href="'.wl($ID).'">'.
798               $ID.' '.strftime($conf['dformat'],@filemtime(wikiFN($ID))).'</a> '.
799               $lang['current'];
800
801    $r_rev   = '';
802    $r_text  = cleanText($text);
803    $r_head  = $lang['yours'];
804  }else{
805    if($rev1 && $rev2){            // two specific revisions wanted
806      // make sure order is correct (older on the right)
807      if($rev1 < $rev2){
808        $l_rev = $rev1;
809        $r_rev = $rev2;
810      }else{
811        $l_rev = $rev2;
812        $r_rev = $rev1;
813      }
814    }elseif($rev1){                // single revision given, compare to current
815      $r_rev = '';
816      $l_rev = $rev1;
817    }else{                        // no revision was given, compare previous to current
818      $r_rev = '';
819      $revs = getRevisions($ID, 0, 1);
820      $l_rev = $revs[0];
821    }
822
823    // when both revisions are empty then the page was created just now
824    if(!$l_rev && !$r_rev){
825      $l_text = '';
826    }else{
827      $l_text = rawWiki($ID,$l_rev);
828    }
829    $r_text = rawWiki($ID,$r_rev);
830
831
832    if(!$l_rev){
833      $l_head = '&mdash;';
834    }else{
835      $l_head = '<a class="wikilink1" href="'.wl($ID,"rev=$l_rev").'">'.
836                $ID.' '.strftime($conf['dformat'],$l_rev).'</a>';
837    }
838
839    if($r_rev){
840      $r_head = '<a class="wikilink1" href="'.wl($ID,"rev=$r_rev").'">'.
841                $ID.' '.strftime($conf['dformat'],$r_rev).'</a>';
842    }elseif($_rev = @filemtime(wikiFN($ID))){
843      $r_head  = '<a class="wikilink1" href="'.wl($ID).'">'.
844               $ID.' '.strftime($conf['dformat'],$_rev).'</a> '.
845               $lang['current'];
846    }else{
847      $r_head = '&mdash; '.$lang['current'];
848    }
849  }
850
851  $df = new Diff(explode("\n",htmlspecialchars($l_text)),
852                 explode("\n",htmlspecialchars($r_text)));
853
854  $tdf = new TableDiffFormatter();
855  if($intro) print p_locale_xhtml('diff');
856  ?>
857    <table class="diff">
858      <tr>
859        <th colspan="2">
860          <?php echo $l_head?>
861        </th>
862        <th colspan="2">
863          <?php echo $r_head?>
864        </th>
865      </tr>
866      <?php echo $tdf->format($df)?>
867    </table>
868  <?php
869}
870
871/**
872 * show warning on conflict detection
873 *
874 * @author Andreas Gohr <andi@splitbrain.org>
875 */
876function html_conflict($text,$summary){
877  global $ID;
878  global $lang;
879
880  print p_locale_xhtml('conflict');
881  $form = new Doku_Form('dw__editform');
882  $form->addHidden('id', $ID);
883  $form->addHidden('wikitext', $text);
884  $form->addHidden('summary', $summary);
885  $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('accesskey'=>'s')));
886  $form->addElement(form_makeButton('submit', 'cancel', $lang['btn_cancel']));
887  html_form('conflict', $form);
888  print '<br /><br /><br /><br />'.NL;
889}
890
891/**
892 * Prints the global message array
893 *
894 * @author Andreas Gohr <andi@splitbrain.org>
895 */
896function html_msgarea(){
897  global $MSG;
898
899  if(!isset($MSG)) return;
900
901  foreach($MSG as $msg){
902    print '<div class="'.$msg['lvl'].'">';
903    print $msg['msg'];
904    print '</div>';
905  }
906}
907
908/**
909 * Prints the registration form
910 *
911 * @author Andreas Gohr <andi@splitbrain.org>
912 */
913function html_register(){
914  global $lang;
915  global $conf;
916  global $ID;
917
918  print p_locale_xhtml('register');
919  print '<div class="centeralign">'.NL;
920  $form = new Doku_Form('dw__register', wl($ID));
921  $form->startFieldset($lang['register']);
922  $form->addHidden('do', 'register');
923  $form->addHidden('save', '1');
924  $form->addElement(form_makeTextField('login', $_POST['login'], $lang['user'], null, 'block', array('size'=>'50')));
925  if (!$conf['autopasswd']) {
926    $form->addElement(form_makePasswordField('pass', $lang['pass'], '', 'block', array('size'=>'50')));
927    $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
928  }
929  $form->addElement(form_makeTextField('fullname', $_POST['fullname'], $lang['fullname'], '', 'block', array('size'=>'50')));
930  $form->addElement(form_makeTextField('email', $_POST['email'], $lang['email'], '', 'block', array('size'=>'50')));
931  $form->addElement(form_makeButton('submit', '', $lang['register']));
932  $form->endFieldset();
933  html_form('register', $form);
934
935  print '</div>'.NL;
936}
937
938/**
939 * Print the update profile form
940 *
941 * @author Christopher Smith <chris@jalakai.co.uk>
942 * @author Andreas Gohr <andi@splitbrain.org>
943 */
944function html_updateprofile(){
945  global $lang;
946  global $conf;
947  global $ID;
948  global $INFO;
949  global $auth;
950
951  print p_locale_xhtml('updateprofile');
952
953  if (empty($_POST['fullname'])) $_POST['fullname'] = $INFO['userinfo']['name'];
954  if (empty($_POST['email'])) $_POST['email'] = $INFO['userinfo']['mail'];
955  print '<div class="centeralign">'.NL;
956  $form = new Doku_Form('dw__register', wl($ID));
957  $form->startFieldset($lang['profile']);
958  $form->addHidden('do', 'profile');
959  $form->addHidden('save', '1');
960  $form->addElement(form_makeTextField('fullname', $_SERVER['REMOTE_USER'], $lang['user'], '', 'block', array('size'=>'50', 'disabled'=>'disabled')));
961  $attr = array('size'=>'50');
962  if (!$auth->canDo('modName')) $attr['disabled'] = 'disabled';
963  $form->addElement(form_makeTextField('fullname', $_POST['fullname'], $lang['fullname'], '', 'block', $attr));
964  $attr = array('size'=>'50');
965  if (!$auth->canDo('modMail')) $attr['disabled'] = 'disabled';
966  $form->addElement(form_makeTextField('email', $_POST['email'], $lang['email'], '', 'block', $attr));
967  $form->addElement(form_makeTag('br'));
968  if ($auth->canDo('modPass')) {
969    $form->addElement(form_makePasswordField('newpass', $lang['newpass'], '', 'block', array('size'=>'50')));
970    $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
971  }
972  if ($conf['profileconfirm']) {
973    $form->addElement(form_makeTag('br'));
974    $form->addElement(form_makePasswordField('oldpass', $lang['oldpass'], '', 'block', array('size'=>'50')));
975  }
976  $form->addElement(form_makeButton('submit', '', $lang['btn_save']));
977  $form->addElement(form_makeButton('reset', '', $lang['btn_reset']));
978  $form->endFieldset();
979  html_form('updateprofile', $form);
980  print '</div>'.NL;
981}
982
983/**
984 * This displays the edit form (lots of logic included)
985 *
986 * @fixme    this is a huge lump of code and should be modularized
987 * @triggers HTML_PAGE_FROMTEMPLATE
988 * @triggers HTML_EDITFORM_INJECTION
989 * @author   Andreas Gohr <andi@splitbrain.org>
990 */
991function html_edit($text=null,$include='edit'){ //FIXME: include needed?
992  global $ID;
993  global $REV;
994  global $DATE;
995  global $RANGE;
996  global $PRE;
997  global $SUF;
998  global $INFO;
999  global $SUM;
1000  global $lang;
1001  global $conf;
1002
1003  //set summary default
1004  if(!$SUM){
1005    if($REV){
1006      $SUM = $lang['restored'];
1007    }elseif(!$INFO['exists']){
1008      $SUM = $lang['created'];
1009    }
1010  }
1011
1012  //no text? Load it!
1013  if(!isset($text)){
1014    $pr = false; //no preview mode
1015    if($INFO['exists']){
1016      if($RANGE){
1017        list($PRE,$text,$SUF) = rawWikiSlices($RANGE,$ID,$REV);
1018      }else{
1019        $text = rawWiki($ID,$REV);
1020      }
1021      $check = md5($text);
1022      $mod = false;
1023    }else{
1024      //try to load a pagetemplate
1025      $data = array($ID);
1026      $text = trigger_event('HTML_PAGE_FROMTEMPLATE',$data,'pageTemplate',true);
1027      $check = md5('');
1028      $mod = $text!=='';
1029    }
1030  }else{
1031    $pr = true; //preview mode
1032    if (isset($_REQUEST['changecheck'])) {
1033      $check = $_REQUEST['changecheck'];
1034      $mod = md5($text)!==$check;
1035    } else {
1036      // Why? Assume default text is unmodified.
1037      $check = md5($text);
1038      $mod = false;
1039    }
1040  }
1041
1042  $wr = $INFO['writable'];
1043  if($wr){
1044    if ($REV) print p_locale_xhtml('editrev');
1045    print p_locale_xhtml($include);
1046  }else{
1047    // check pseudo action 'source'
1048    if(!actionOK('source')){
1049      msg('Command disabled: source',-1);
1050      return;
1051    }
1052    print p_locale_xhtml('read');
1053  }
1054  if(!$DATE) $DATE = $INFO['lastmod'];
1055
1056
1057?>
1058  <div style="width:99%;">
1059
1060   <div class="toolbar">
1061      <div id="draft__status"><?php if(!empty($INFO['draft'])) echo $lang['draftdate'].' '.strftime($conf['dformat']);?></div>
1062      <div id="tool__bar"><?php if($wr){?><a href="<?php echo DOKU_BASE?>lib/exe/mediamanager.php?ns=<?php echo $INFO['namespace']?>"
1063      target="_blank"><?php echo $lang['mediaselect'] ?></a><?php }?></div>
1064
1065      <?php if($wr){?>
1066      <script type="text/javascript" charset="utf-8"><!--//--><![CDATA[//><!--
1067        <?php /* sets changed to true when previewed */?>
1068        textChanged = <?php ($mod) ? print 'true' : print 'false' ?>;
1069      //--><!]]></script>
1070      <span id="spell__action"></span>
1071      <div id="spell__suggest"></div>
1072      <?php } ?>
1073   </div>
1074   <div id="spell__result"></div>
1075<?php
1076  $form = new Doku_Form('dw__editform');
1077  $form->addHidden('id', $ID);
1078  $form->addHidden('rev', $REV);
1079  $form->addHidden('date', $DATE);
1080  $form->addHidden('prefix', $PRE);
1081  $form->addHidden('suffix', $SUF);
1082  $form->addHidden('changecheck', $check);
1083  $attr = array('tabindex'=>'1');
1084  if (!$wr) $attr['readonly'] = 'readonly';
1085  $form->addElement(form_makeWikiText($text, $attr));
1086  $form->addElement(form_makeOpenTag('div', array('id'=>'wiki__editbar')));
1087  $form->addElement(form_makeOpenTag('div', array('id'=>'size__ctl')));
1088  $form->addElement(form_makeCloseTag('div'));
1089  if ($wr) {
1090    $form->addElement(form_makeOpenTag('div', array('class'=>'editButtons')));
1091    $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('id'=>'edbtn__save', 'accesskey'=>'s', 'tabindex'=>'4')));
1092    $form->addElement(form_makeButton('submit', 'preview', $lang['btn_preview'], array('id'=>'edbtn__preview', 'accesskey'=>'p', 'tabindex'=>'5')));
1093    $form->addElement(form_makeButton('submit', 'draftdel', $lang['btn_cancel'], array('tabindex'=>'6')));
1094    $form->addElement(form_makeCloseTag('div'));
1095    $form->addElement(form_makeOpenTag('div', array('class'=>'summary')));
1096    $form->addElement(form_makeTextField('summary', $SUM, $lang['summary'], 'edit__summary', 'nowrap', array('size'=>'50', 'tabindex'=>'2')));
1097    $elem = html_minoredit();
1098    if ($elem) $form->addElement($elem);
1099    $form->addElement(form_makeCloseTag('div'));
1100  }
1101  $form->addElement(form_makeCloseTag('div'));
1102  html_form('edit', $form);
1103  print '</div>'.NL;
1104}
1105
1106/**
1107 * Adds a checkbox for minor edits for logged in users
1108 *
1109 * @author Andrea Gohr <andi@splitbrain.org>
1110 */
1111function html_minoredit(){
1112  global $conf;
1113  global $lang;
1114  // minor edits are for logged in users only
1115  if(!$conf['useacl'] || !$_SERVER['REMOTE_USER']){
1116    return false;
1117  }
1118
1119  $p = array();
1120  $p['tabindex'] = 3;
1121  if(!empty($_REQUEST['minor'])) $p['checked']='checked';
1122  return form_makeCheckboxField('minor', '1', $lang['minoredit'], 'minoredit', 'nowrap', $p);
1123}
1124
1125/**
1126 * prints some debug info
1127 *
1128 * @author Andreas Gohr <andi@splitbrain.org>
1129 */
1130function html_debug(){
1131  global $conf;
1132  global $lang;
1133  global $auth;
1134  global $INFO;
1135
1136  //remove sensitive data
1137  $cnf = $conf;
1138  $cnf['auth']='***';
1139  $cnf['notify']='***';
1140  $cnf['ftp']='***';
1141  $nfo = $INFO;
1142  $nfo['userinfo'] = '***';
1143  $ses = $_SESSION;
1144  $ses[$conf['title']]['auth'] = '***';
1145
1146  print '<html><body>';
1147
1148  print '<p>When reporting bugs please send all the following ';
1149  print 'output as a mail to andi@splitbrain.org ';
1150  print 'The best way to do this is to save this page in your browser</p>';
1151
1152  print '<b>$INFO:</b><pre>';
1153  print_r($nfo);
1154  print '</pre>';
1155
1156  print '<b>$_SERVER:</b><pre>';
1157  print_r($_SERVER);
1158  print '</pre>';
1159
1160  print '<b>$conf:</b><pre>';
1161  print_r($cnf);
1162  print '</pre>';
1163
1164  print '<b>DOKU_BASE:</b><pre>';
1165  print DOKU_BASE;
1166  print '</pre>';
1167
1168  print '<b>abs DOKU_BASE:</b><pre>';
1169  print DOKU_URL;
1170  print '</pre>';
1171
1172  print '<b>rel DOKU_BASE:</b><pre>';
1173  print dirname($_SERVER['PHP_SELF']).'/';
1174  print '</pre>';
1175
1176  print '<b>PHP Version:</b><pre>';
1177  print phpversion();
1178  print '</pre>';
1179
1180  print '<b>locale:</b><pre>';
1181  print setlocale(LC_ALL,0);
1182  print '</pre>';
1183
1184  print '<b>encoding:</b><pre>';
1185  print $lang['encoding'];
1186  print '</pre>';
1187
1188  if($auth){
1189    print '<b>Auth backend capabilities:</b><pre>';
1190    print_r($auth->cando);
1191    print '</pre>';
1192  }
1193
1194  print '<b>$_SESSION:</b><pre>';
1195  print_r($ses);
1196  print '</pre>';
1197
1198  print '<b>Environment:</b><pre>';
1199  print_r($_ENV);
1200  print '</pre>';
1201
1202  print '<b>PHP settings:</b><pre>';
1203  $inis = ini_get_all();
1204  print_r($inis);
1205  print '</pre>';
1206
1207  print '</body></html>';
1208}
1209
1210function html_admin(){
1211  global $ID;
1212  global $INFO;
1213  global $lang;
1214  global $conf;
1215
1216  print p_locale_xhtml('admin');
1217
1218  // build menu of admin functions from the plugins that handle them
1219  $pluginlist = plugin_list('admin');
1220  $menu = array();
1221  foreach ($pluginlist as $p) {
1222    if($obj =& plugin_load('admin',$p) === NULL) continue;
1223
1224    // check permissions
1225    if($obj->forAdminOnly() && !$INFO['isadmin']) continue;
1226
1227    $menu[] = array('plugin' => $p,
1228                    'prompt' => $obj->getMenuText($conf['lang']),
1229                    'sort' => $obj->getMenuSort()
1230                   );
1231  }
1232
1233  usort($menu, 'p_sort_modes');
1234
1235  // output the menu
1236  ptln('<ul>');
1237
1238  foreach ($menu as $item) {
1239    if (!$item['prompt']) continue;
1240    ptln('  <li><div class="li"><a href="'.wl($ID, 'do=admin&amp;page='.$item['plugin']).'">'.$item['prompt'].'</a></div></li>');
1241  }
1242
1243  ptln('</ul>');
1244}
1245
1246/**
1247 * Form to request a new password for an existing account
1248 *
1249 * @author Benoit Chesneau <benoit@bchesneau.info>
1250 */
1251function html_resendpwd() {
1252  global $lang;
1253  global $conf;
1254  global $ID;
1255
1256  print p_locale_xhtml('resendpwd');
1257  print '<div class="centeralign">'.NL;
1258  $form = new Doku_Form('dw__resendpwd', wl($ID));
1259  $form->startFieldset($lang['resendpwd']);
1260  $form->addHidden('do', 'resendpwd');
1261  $form->addHidden('save', '1');
1262  $form->addElement(form_makeTag('br'));
1263  $form->addElement(form_makeTextField('login', $_POST['login'], $lang['user'], '', 'block'));
1264  $form->addElement(form_makeTag('br'));
1265  $form->addElement(form_makeTag('br'));
1266  $form->addElement(form_makeButton('submit', '', $lang['btn_resendpwd']));
1267  $form->endFieldset();
1268  html_form('resendpwd', $form);
1269  print '</div>'.NL;
1270}
1271
1272/**
1273 * Return the TOC rendered to XHTML
1274 *
1275 * @author Andreas Gohr <andi@splitbrain.org>
1276 */
1277function html_TOC($toc){
1278    if(!count($toc)) return '';
1279    global $lang;
1280    $out  = '<!-- TOC START -->'.DOKU_LF;
1281    $out .= '<div class="toc">'.DOKU_LF;
1282    $out .= '<div class="tocheader toctoggle" id="toc__header">';
1283    $out .= $lang['toc'];
1284    $out .= '</div>'.DOKU_LF;
1285    $out .= '<div id="toc__inside">'.DOKU_LF;
1286    $out .= html_buildlist($toc,'toc','html_list_toc');
1287    $out .= '</div>'.DOKU_LF.'</div>'.DOKU_LF;
1288    $out .= '<!-- TOC END -->'.DOKU_LF;
1289    return $out;                                                                                                }
1290
1291/**
1292 * Callback for html_buildlist
1293 */
1294function html_list_toc($item){
1295    if($item['hid']){
1296        $link = '#'.$item['hid'];
1297    }else{
1298        $link = $item['link'];
1299    }
1300
1301    return '<span class="li"><a href="'.$link.'" class="toc">'.
1302           hsc($item['title']).'</a></span>';
1303}
1304
1305/**
1306 * Helper function to build TOC items
1307 *
1308 * Returns an array ready to be added to a TOC array
1309 *
1310 * @param string $link  - where to link (if $hash set to '#' it's a local anchor)
1311 * @param string $text  - what to display in the TOC
1312 * @param int    $level - nesting level
1313 * @param string $hash  - is prepended to the given $link, set blank if you want full links
1314 */
1315function html_mktocitem($link, $text, $level, $hash='#'){
1316    global $conf;
1317    return  array( 'link'  => $hash.$link,
1318                   'title' => $text,
1319                   'type'  => 'ul',
1320                   'level' => $level);
1321}
1322
1323/**
1324 * Output a Doku_Form object.
1325 * Triggers an event with the form name: HTML_{$name}FORM_OUTPUT
1326 *
1327 * @author Tom N Harris <tnharris@whoopdedo.org>
1328 */
1329function html_form($name, &$form) {
1330  // Safety check in case the caller forgets.
1331  $form->endFieldset();
1332  trigger_event('HTML_'.strtoupper($name).'FORM_OUTPUT', $form, 'html_form_output', false);
1333}
1334
1335/**
1336 * Form print function.
1337 * Just calls printForm() on the data object.
1338 */
1339function html_form_output($data) {
1340  $data->printForm();
1341}
1342
1343//Setup VIM: ex: et ts=2 enc=utf-8 :
1344