xref: /dokuwiki/inc/html.php (revision 1a58f8112fd940be413a68db76322c85c3230918)
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',realpath(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" 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'].' '. date($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,$query){
266  //split at common delimiters
267  $queries = preg_split ('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/',$query,-1,PREG_SPLIT_NO_EMPTY);
268  foreach ($queries as $q){
269     $q = preg_quote($q,'/');
270     $html = preg_replace_callback("/((<[^>]*)|$q)/i",'html_hilight_callback',$html);
271  }
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,$conf['useheading']?NULL:$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,$poswords);
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,$poswords);
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,$poswords).'</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 = @date($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 = @date($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   = date($conf['dformat'],$rev);
448    $info   = getRevisionInfo($ID,$rev,true);
449    $exists = @file_exists(wikiFN($ID,$rev));
450
451
452
453    print ($info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) ? '<li class="minor">' : '<li>';
454    print '<div class="li">';
455    if($exists){
456      print '<input type="checkbox" name="rev2[]" value="'.$rev.'" /> ';
457    }else{
458      print '<img src="'.DOKU_BASE.'lib/images/blank.gif" width="14" height="11" alt="" /> ';
459    }
460    print $date;
461
462    if($exists){
463      print ' <a href="'.wl($ID,"rev=$rev,do=diff").'">';
464      $p = array();
465      $p['src']    = DOKU_BASE.'lib/images/diff.png';
466      $p['width']  = 15;
467      $p['height'] = 11;
468      $p['title']  = $lang['diff'];
469      $p['alt']    = $lang['diff'];
470      $att = buildAttributes($p);
471      print "<img $att />";
472      print '</a> ';
473
474      print '<a class="wikilink1" href="'.wl($ID,"rev=$rev").'">'.$ID.'</a>';
475    }else{
476      print ' <img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" /> ';
477      print $ID;
478    }
479
480    print ' &ndash; ';
481    print htmlspecialchars($info['sum']);
482    print ' <span class="user">';
483    if($info['user']){
484      print $info['user'];
485    }else{
486      print $info['ip'];
487    }
488    print '</span>';
489
490    print '</div>';
491    print '</li>';
492  }
493  print '</ul>';
494  print '<input name="do[diff]" type="submit" value="'.$lang['diff2'].'" class="button" />';
495  print '</form>';
496
497  print '<div class="pagenav">';
498  $last = $first + $conf['recent'];
499  if ($first > 0) {
500    $first -= $conf['recent'];
501    if ($first < 0) $first = 0;
502    print '<div class="pagenav-prev">';
503    print html_btn('newer',$ID,"p",array('do' => 'revisions', 'first' => $first));
504    print '</div>';
505  }
506  if ($hasNext) {
507    print '<div class="pagenav-next">';
508    print html_btn('older',$ID,"n",array('do' => 'revisions', 'first' => $last));
509    print '</div>';
510  }
511  print '</div>';
512
513}
514
515/**
516 * display recent changes
517 *
518 * @author Andreas Gohr <andi@splitbrain.org>
519 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
520 * @author Ben Coburn <btcoburn@silicodon.net>
521 */
522function html_recent($first=0){
523  global $conf;
524  global $lang;
525  global $ID;
526  /* we need to get one additionally log entry to be able to
527   * decide if this is the last page or is there another one.
528   * This is the cheapest solution to get this information.
529   */
530  $recents = getRecents($first,$conf['recent'] + 1,getNS($ID));
531  if(count($recents) == 0 && $first != 0){
532    $first=0;
533    $recents = getRecents($first,$conf['recent'] + 1,getNS($ID));
534  }
535  $hasNext = false;
536  if (count($recents)>$conf['recent']) {
537    $hasNext = true;
538    array_pop($recents); // remove extra log entry
539  }
540
541  print p_locale_xhtml('recent');
542  print '<ul>';
543
544  foreach($recents as $recent){
545    $date = date($conf['dformat'],$recent['date']);
546    print ($recent['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) ? '<li class="minor">' : '<li>';
547    print '<div class="li">';
548
549    print $date.' ';
550
551    print '<a href="'.wl($recent['id'],"do=diff").'">';
552    $p = array();
553    $p['src']    = DOKU_BASE.'lib/images/diff.png';
554    $p['width']  = 15;
555    $p['height'] = 11;
556    $p['title']  = $lang['diff'];
557    $p['alt']    = $lang['diff'];
558    $att = buildAttributes($p);
559    print "<img $att />";
560    print '</a> ';
561
562    print '<a href="'.wl($recent['id'],"do=revisions").'">';
563    $p = array();
564    $p['src']    = DOKU_BASE.'lib/images/history.png';
565    $p['width']  = 12;
566    $p['height'] = 14;
567    $p['title']  = $lang['btn_revs'];
568    $p['alt']    = $lang['btn_revs'];
569    $att = buildAttributes($p);
570    print "<img $att />";
571    print '</a> ';
572
573    print html_wikilink(':'.$recent['id'],$conf['useheading']?NULL:$recent['id']);
574    print ' &ndash; '.htmlspecialchars($recent['sum']);
575
576    print ' <span class="user">';
577    if($recent['user']){
578      print $recent['user'];
579    }else{
580      print $recent['ip'];
581    }
582    print '</span>';
583
584    print '</div>';
585    print '</li>';
586  }
587  print '</ul>';
588
589  print '<div class="pagenav">';
590  $last = $first + $conf['recent'];
591  if ($first > 0) {
592    $first -= $conf['recent'];
593    if ($first < 0) $first = 0;
594    print '<div class="pagenav-prev">';
595    print html_btn('newer','',"p",array('do' => 'recent', 'first' => $first));
596    print '</div>';
597  }
598  if ($hasNext) {
599    print '<div class="pagenav-next">';
600    print html_btn('older','',"n",array('do' => 'recent', 'first' => $last));
601    print '</div>';
602  }
603  print '</div>';
604}
605
606/**
607 * Display page index
608 *
609 * @author Andreas Gohr <andi@splitbrain.org>
610 */
611function html_index($ns){
612  require_once(DOKU_INC.'inc/search.php');
613  global $conf;
614  global $ID;
615  $dir = $conf['datadir'];
616  $ns  = cleanID($ns);
617  #fixme use appropriate function
618  if(empty($ns)){
619    $ns = dirname(str_replace(':','/',$ID));
620    if($ns == '.') $ns ='';
621  }
622  $ns  = utf8_encodeFN(str_replace(':','/',$ns));
623
624  echo p_locale_xhtml('index');
625  echo '<div id="index__tree">';
626
627  $data = array();
628  search($data,$conf['datadir'],'search_index',array('ns' => $ns));
629  echo html_buildlist($data,'idx','html_list_index','html_li_index');
630
631  echo '</div>';
632}
633
634/**
635 * Index item formatter
636 *
637 * User function for html_buildlist()
638 *
639 * @author Andreas Gohr <andi@splitbrain.org>
640 */
641function html_list_index($item){
642  global $ID;
643  $ret = '';
644  $base = ':'.$item['id'];
645  $base = substr($base,strrpos($base,':')+1);
646  if($item['type']=='d'){
647    $ret .= '<a href="'.wl($ID,'idx='.rawurlencode($item['id'])).'" class="idx_dir"><strong>';
648    $ret .= $base;
649    $ret .= '</strong></a>';
650  }else{
651    $ret .= html_wikilink(':'.$item['id']);
652  }
653  return $ret;
654}
655
656/**
657 * Index List item
658 *
659 * This user function is used in html_build_lidt to build the
660 * <li> tags for namespaces when displaying the page index
661 * it gives different classes to opened or closed "folders"
662 *
663 * @author Andreas Gohr <andi@splitbrain.org>
664 */
665function html_li_index($item){
666  if($item['type'] == "f"){
667    return '<li class="level'.$item['level'].'">';
668  }elseif($item['open']){
669    return '<li class="open">';
670  }else{
671    return '<li class="closed">';
672  }
673}
674
675/**
676 * Default List item
677 *
678 * @author Andreas Gohr <andi@splitbrain.org>
679 */
680function html_li_default($item){
681  return '<li class="level'.$item['level'].'">';
682}
683
684/**
685 * Build an unordered list
686 *
687 * Build an unordered list from the given $data array
688 * Each item in the array has to have a 'level' property
689 * the item itself gets printed by the given $func user
690 * function. The second and optional function is used to
691 * print the <li> tag. Both user function need to accept
692 * a single item.
693 *
694 * Both user functions can be given as array to point to
695 * a member of an object.
696 *
697 * @author Andreas Gohr <andi@splitbrain.org>
698 */
699function html_buildlist($data,$class,$func,$lifunc='html_li_default'){
700  $level = 0;
701  $opens = 0;
702  $ret   = '';
703
704  foreach ($data as $item){
705
706    if( $item['level'] > $level ){
707      //open new list
708      for($i=0; $i<($item['level'] - $level); $i++){
709        if ($i) $ret .= "<li class=\"clear\">\n";
710        $ret .= "\n<ul class=\"$class\">\n";
711      }
712    }elseif( $item['level'] < $level ){
713      //close last item
714      $ret .= "</li>\n";
715      for ($i=0; $i<($level - $item['level']); $i++){
716        //close higher lists
717        $ret .= "</ul>\n</li>\n";
718      }
719    }else{
720      //close last item
721      $ret .= "</li>\n";
722    }
723
724    //remember current level
725    $level = $item['level'];
726
727    //print item
728    $ret .= call_user_func($lifunc,$item);
729    $ret .= '<div class="li">';
730
731    $ret .= call_user_func($func,$item);
732    $ret .= '</div>';
733  }
734
735  //close remaining items and lists
736  for ($i=0; $i < $level; $i++){
737    $ret .= "</li></ul>\n";
738  }
739
740  return $ret;
741}
742
743/**
744 * display backlinks
745 *
746 * @author Andreas Gohr <andi@splitbrain.org>
747 * @author Michael Klier <chi@chimeric.de>
748 */
749function html_backlinks(){
750  require_once(DOKU_INC.'inc/fulltext.php');
751  global $ID;
752  global $conf;
753  global $lang;
754
755  print p_locale_xhtml('backlinks');
756
757  $data = ft_backlinks($ID);
758
759  if(!empty($data)) {
760      print '<ul class="idx">';
761      foreach($data as $blink){
762        print '<li><div class="li">';
763        print html_wikilink(':'.$blink,$conf['useheading']?NULL:$blink);
764        print '</div></li>';
765      }
766      print '</ul>';
767  } else {
768      print '<div class="level1"><p>' . $lang['nothingfound'] . '</p></div>';
769  }
770}
771
772/**
773 * show diff
774 *
775 * @author Andreas Gohr <andi@splitbrain.org>
776 */
777function html_diff($text='',$intro=true){
778  require_once(DOKU_INC.'inc/DifferenceEngine.php');
779  global $ID;
780  global $REV;
781  global $lang;
782  global $conf;
783
784  // we're trying to be clever here, revisions to compare can be either
785  // given as rev and rev2 parameters, with rev2 being optional. Or in an
786  // array in rev2.
787  $rev1 = $REV;
788  if(is_array($_REQUEST['rev2'])){
789    $rev1 = (int) $_REQUEST['rev2'][0];
790    $rev2 = (int) $_REQUEST['rev2'][1];
791  }else{
792    $rev2 = (int) $_REQUEST['rev2'];
793  }
794
795  if($text){                      // compare text to the most current revision
796    $l_rev   = '';
797    $l_text  = rawWiki($ID,'');
798    $l_head  = '<a class="wikilink1" href="'.wl($ID).'">'.
799               $ID.' '.date($conf['dformat'],@filemtime(wikiFN($ID))).'</a> '.
800               $lang['current'];
801
802    $r_rev   = '';
803    $r_text  = cleanText($text);
804    $r_head  = $lang['yours'];
805  }else{
806    if($rev1 && $rev2){            // two specific revisions wanted
807      // make sure order is correct (older on the right)
808      if($rev1 < $rev2){
809        $l_rev = $rev1;
810        $r_rev = $rev2;
811      }else{
812        $l_rev = $rev2;
813        $r_rev = $rev1;
814      }
815    }elseif($rev1){                // single revision given, compare to current
816      $r_rev = '';
817      $l_rev = $rev1;
818    }else{                        // no revision was given, compare previous to current
819      $r_rev = '';
820      $revs = getRevisions($ID, 0, 1);
821      $l_rev = $revs[0];
822    }
823
824    $l_text = rawWiki($ID,$l_rev);
825    $r_text = rawWiki($ID,$r_rev);
826
827    $l_head = '<a class="wikilink1" href="'.wl($ID,"rev=$l_rev").'">'.
828              $ID.' '.date($conf['dformat'],$l_rev).'</a>';
829
830    if($r_rev){
831      $r_head = '<a class="wikilink1" href="'.wl($ID,"rev=$r_rev").'">'.
832                $ID.' '.date($conf['dformat'],$r_rev).'</a>';
833    }else{
834      $r_head  = '<a class="wikilink1" href="'.wl($ID).'">'.
835               $ID.' '.date($conf['dformat'],@filemtime(wikiFN($ID))).'</a> '.
836               $lang['current'];
837    }
838  }
839
840  $df = new Diff(explode("\n",htmlspecialchars($l_text)),
841                 explode("\n",htmlspecialchars($r_text)));
842
843  $tdf = new TableDiffFormatter();
844  if($intro) print p_locale_xhtml('diff');
845  ?>
846    <table class="diff">
847      <tr>
848        <th colspan="2">
849          <?php echo $l_head?>
850        </th>
851        <th colspan="2">
852          <?php echo $r_head?>
853        </th>
854      </tr>
855      <?php echo $tdf->format($df)?>
856    </table>
857  <?php
858}
859
860/**
861 * show warning on conflict detection
862 *
863 * @author Andreas Gohr <andi@splitbrain.org>
864 */
865function html_conflict($text,$summary){
866  global $ID;
867  global $lang;
868
869  print p_locale_xhtml('conflict');
870  $form = new Doku_Form('dw__editform');
871  $form->addHidden('id', $ID);
872  $form->addHidden('wikitext', $text);
873  $form->addHidden('summary', $summary);
874  $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('accesskey'=>'s')));
875  $form->addElement(form_makeButton('submit', 'cancel', $lang['btn_cancel']));
876  html_form('conflict', $form);
877  print '<br /><br /><br /><br />'.NL;
878}
879
880/**
881 * Prints the global message array
882 *
883 * @author Andreas Gohr <andi@splitbrain.org>
884 */
885function html_msgarea(){
886  global $MSG;
887
888  if(!isset($MSG)) return;
889
890  foreach($MSG as $msg){
891    print '<div class="'.$msg['lvl'].'">';
892    print $msg['msg'];
893    print '</div>';
894  }
895}
896
897/**
898 * Prints the registration form
899 *
900 * @author Andreas Gohr <andi@splitbrain.org>
901 */
902function html_register(){
903  global $lang;
904  global $conf;
905  global $ID;
906
907  print p_locale_xhtml('register');
908  print '<div class="centeralign">'.NL;
909  $form = new Doku_Form('dw__register', wl($ID));
910  $form->startFieldset($lang['register']);
911  $form->addHidden('do', 'register');
912  $form->addHidden('save', '1');
913  $form->addElement(form_makeTextField('login', $_POST['login'], $lang['user'], null, 'block', array('size'=>'50')));
914  if (!$conf['autopasswd']) {
915    $form->addElement(form_makePasswordField('pass', $lang['pass'], '', 'block', array('size'=>'50')));
916    $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
917  }
918  $form->addElement(form_makeTextField('fullname', $_POST['fullname'], $lang['fullname'], '', 'block', array('size'=>'50')));
919  $form->addElement(form_makeTextField('email', $_POST['email'], $lang['email'], '', 'block', array('size'=>'50')));
920  $form->addElement(form_makeButton('submit', '', $lang['register']));
921  $form->endFieldset();
922  html_form('register', $form);
923
924  print '</div>'.NL;
925}
926
927/**
928 * Print the update profile form
929 *
930 * @author Christopher Smith <chris@jalakai.co.uk>
931 * @author Andreas Gohr <andi@splitbrain.org>
932 */
933function html_updateprofile(){
934  global $lang;
935  global $conf;
936  global $ID;
937  global $INFO;
938  global $auth;
939
940  print p_locale_xhtml('updateprofile');
941
942  if (empty($_POST['fullname'])) $_POST['fullname'] = $INFO['userinfo']['name'];
943  if (empty($_POST['email'])) $_POST['email'] = $INFO['userinfo']['mail'];
944  print '<div class="centeralign">'.NL;
945  $form = new Doku_Form('dw__register', wl($ID));
946  $form->startFieldset($lang['profile']);
947  $form->addHidden('do', 'profile');
948  $form->addHidden('save', '1');
949  $form->addElement(form_makeTextField('fullname', $_SERVER['REMOTE_USER'], $lang['user'], '', 'block', array('size'=>'50', 'disabled'=>'disabled')));
950  $attr = array('size'=>'50');
951  if (!$auth->canDo('modName')) $attr['disabled'] = 'disabled';
952  $form->addElement(form_makeTextField('fullname', $_POST['fullname'], $lang['fullname'], '', 'block', $attr));
953  $form->addElement(form_makeTextField('email', $_POST['email'], $lang['email'], '', 'block', $attr));
954  $form->addElement(form_makeTag('br'));
955  if ($auth->canDo('modPass')) {
956    $form->addElement(form_makePasswordField('newpass', $lang['newpass'], '', 'block', array('size'=>'50')));
957    $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
958  }
959  if ($conf['profileconfirm']) {
960    $form->addElement(form_makeTag('br'));
961    $form->addElement(form_makePasswordField('oldpass', $lang['oldpass'], '', 'block', array('size'=>'50')));
962  }
963  $form->addElement(form_makeButton('submit', '', $lang['btn_save']));
964  $form->addElement(form_makeButton('reset', '', $lang['btn_reset']));
965  $form->endFieldset();
966  html_form('updateprofile', $form);
967  print '</div>'.NL;
968}
969
970/**
971 * This displays the edit form (lots of logic included)
972 *
973 * @fixme    this is a huge lump of code and should be modularized
974 * @triggers HTML_PAGE_FROMTEMPLATE
975 * @triggers HTML_EDITFORM_INJECTION
976 * @author   Andreas Gohr <andi@splitbrain.org>
977 */
978function html_edit($text=null,$include='edit'){ //FIXME: include needed?
979  global $ID;
980  global $REV;
981  global $DATE;
982  global $RANGE;
983  global $PRE;
984  global $SUF;
985  global $INFO;
986  global $SUM;
987  global $lang;
988  global $conf;
989
990  //set summary default
991  if(!$SUM){
992    if($REV){
993      $SUM = $lang['restored'];
994    }elseif(!$INFO['exists']){
995      $SUM = $lang['created'];
996    }
997  }
998
999  //no text? Load it!
1000  if(!isset($text)){
1001    $pr = false; //no preview mode
1002    if($INFO['exists']){
1003      if($RANGE){
1004        list($PRE,$text,$SUF) = rawWikiSlices($RANGE,$ID,$REV);
1005      }else{
1006        $text = rawWiki($ID,$REV);
1007      }
1008      $check = md5($text);
1009      $mod = false;
1010    }else{
1011      //try to load a pagetemplate
1012      $data = array($ID);
1013      $text = trigger_event('HTML_PAGE_FROMTEMPLATE',$data,'pageTemplate',true);
1014      $check = md5('');
1015      $mod = $text!=='';
1016    }
1017  }else{
1018    $pr = true; //preview mode
1019    if (isset($_REQUEST['changecheck'])) {
1020      $check = $_REQUEST['changecheck'];
1021      $mod = md5($text)!==$check;
1022    } else {
1023      // Why? Assume default text is unmodified.
1024      $check = md5($text);
1025      $mod = false;
1026    }
1027  }
1028
1029  $wr = $INFO['writable'];
1030  if($wr){
1031    if ($REV) print p_locale_xhtml('editrev');
1032    print p_locale_xhtml($include);
1033  }else{
1034    // check pseudo action 'source'
1035    if(!actionOK('source')){
1036      msg('Command disabled: source',-1);
1037      return;
1038    }
1039    print p_locale_xhtml('read');
1040  }
1041  if(!$DATE) $DATE = $INFO['lastmod'];
1042
1043
1044?>
1045  <div style="width:99%;">
1046
1047   <div class="toolbar">
1048      <div id="draft__status"><?php if(!empty($INFO['draft'])) echo $lang['draftdate'].' '.date($conf['dformat']);?></div>
1049      <div id="tool__bar"><?php if($wr){?><a href="<?php echo DOKU_BASE?>lib/exe/mediamanager.php?ns=<?php echo $INFO['namespace']?>"
1050      target="_blank"><?php echo $lang['mediaselect'] ?></a><?php }?></div>
1051
1052      <?php if($wr){?>
1053      <script type="text/javascript" charset="utf-8"><!--//--><![CDATA[//><!--
1054        <?php /* sets changed to true when previewed */?>
1055        textChanged = <?php ($mod) ? print 'true' : print 'false' ?>;
1056      //--><!]]></script>
1057      <span id="spell__action"></span>
1058      <div id="spell__suggest"></div>
1059      <?php } ?>
1060   </div>
1061   <div id="spell__result"></div>
1062<?php
1063  $form = new Doku_Form('dw__editform');
1064  $form->addHidden('id', $ID);
1065  $form->addHidden('rev', $REV);
1066  $form->addHidden('date', $DATE);
1067  $form->addHidden('prefix', $PRE);
1068  $form->addHidden('suffix', $SUF);
1069  $form->addHidden('changecheck', $check);
1070  $attr = array('tabindex'=>'1');
1071  if (!$wr) $attr['readonly'] = 'readonly';
1072  $form->addElement(form_makeWikiText($text, $attr));
1073  $form->addElement(form_makeOpenTag('div', array('id'=>'wiki__editbar')));
1074  $form->addElement(form_makeOpenTag('div', array('id'=>'size__ctl')));
1075  $form->addElement(form_makeCloseTag('div'));
1076  if ($wr) {
1077    $form->addElement(form_makeOpenTag('div', array('class'=>'editButtons')));
1078    $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('id'=>'edbtn__save', 'accesskey'=>'s', 'tabindex'=>'4')));
1079    $form->addElement(form_makeButton('submit', 'preview', $lang['btn_preview'], array('id'=>'edbtn__preview', 'accesskey'=>'p', 'tabindex'=>'5')));
1080    $form->addElement(form_makeButton('submit', 'draftdel', $lang['btn_cancel'], array('tabindex'=>'6')));
1081    $form->addElement(form_makeCloseTag('div'));
1082    $form->addElement(form_makeOpenTag('div', array('class'=>'summary')));
1083    $form->addElement(form_makeTextField('summary', $SUM, $lang['summary'], 'edit__summary', 'nowrap', array('size'=>'50', 'tabindex'=>'2')));
1084    $elem = html_minoredit();
1085    if ($elem) $form->addElement($elem);
1086    $form->addElement(form_makeCloseTag('div'));
1087  }
1088  $form->addElement(form_makeCloseTag('div'));
1089  html_form('edit', $form);
1090  print '</div>'.NL;
1091}
1092
1093/**
1094 * Adds a checkbox for minor edits for logged in users
1095 *
1096 * @author Andrea Gohr <andi@splitbrain.org>
1097 */
1098function html_minoredit(){
1099  global $conf;
1100  global $lang;
1101  // minor edits are for logged in users only
1102  if(!$conf['useacl'] || !$_SERVER['REMOTE_USER']){
1103    return false;
1104  }
1105
1106  $p = array();
1107  $p['tabindex'] = 3;
1108  if(!empty($_REQUEST['minor'])) $p['checked']='checked';
1109  return form_makeCheckboxField('minor', '1', $lang['minoredit'], 'minoredit', 'nowrap', $p);
1110}
1111
1112/**
1113 * prints some debug info
1114 *
1115 * @author Andreas Gohr <andi@splitbrain.org>
1116 */
1117function html_debug(){
1118  global $conf;
1119  global $lang;
1120  global $auth;
1121  global $INFO;
1122
1123  //remove sensitive data
1124  $cnf = $conf;
1125  $cnf['auth']='***';
1126  $cnf['notify']='***';
1127  $cnf['ftp']='***';
1128  $nfo = $INFO;
1129  $nfo['userinfo'] = '***';
1130  $ses = $_SESSION;
1131  $ses[$conf['title']]['auth'] = '***';
1132
1133  print '<html><body>';
1134
1135  print '<p>When reporting bugs please send all the following ';
1136  print 'output as a mail to andi@splitbrain.org ';
1137  print 'The best way to do this is to save this page in your browser</p>';
1138
1139  print '<b>$INFO:</b><pre>';
1140  print_r($nfo);
1141  print '</pre>';
1142
1143  print '<b>$_SERVER:</b><pre>';
1144  print_r($_SERVER);
1145  print '</pre>';
1146
1147  print '<b>$conf:</b><pre>';
1148  print_r($cnf);
1149  print '</pre>';
1150
1151  print '<b>DOKU_BASE:</b><pre>';
1152  print DOKU_BASE;
1153  print '</pre>';
1154
1155  print '<b>abs DOKU_BASE:</b><pre>';
1156  print DOKU_URL;
1157  print '</pre>';
1158
1159  print '<b>rel DOKU_BASE:</b><pre>';
1160  print dirname($_SERVER['PHP_SELF']).'/';
1161  print '</pre>';
1162
1163  print '<b>PHP Version:</b><pre>';
1164  print phpversion();
1165  print '</pre>';
1166
1167  print '<b>locale:</b><pre>';
1168  print setlocale(LC_ALL,0);
1169  print '</pre>';
1170
1171  print '<b>encoding:</b><pre>';
1172  print $lang['encoding'];
1173  print '</pre>';
1174
1175  if($auth){
1176    print '<b>Auth backend capabilities:</b><pre>';
1177    print_r($auth->cando);
1178    print '</pre>';
1179  }
1180
1181  print '<b>$_SESSION:</b><pre>';
1182  print_r($ses);
1183  print '</pre>';
1184
1185  print '<b>Environment:</b><pre>';
1186  print_r($_ENV);
1187  print '</pre>';
1188
1189  print '<b>PHP settings:</b><pre>';
1190  $inis = ini_get_all();
1191  print_r($inis);
1192  print '</pre>';
1193
1194  print '</body></html>';
1195}
1196
1197function html_admin(){
1198  global $ID;
1199  global $INFO;
1200  global $lang;
1201  global $conf;
1202
1203  print p_locale_xhtml('admin');
1204
1205  // build menu of admin functions from the plugins that handle them
1206  $pluginlist = plugin_list('admin');
1207  $menu = array();
1208  foreach ($pluginlist as $p) {
1209    if($obj =& plugin_load('admin',$p) === NULL) continue;
1210
1211    // check permissions
1212    if($obj->forAdminOnly() && !$INFO['isadmin']) continue;
1213
1214    $menu[] = array('plugin' => $p,
1215                    'prompt' => $obj->getMenuText($conf['lang']),
1216                    'sort' => $obj->getMenuSort()
1217                   );
1218  }
1219
1220  usort($menu, 'p_sort_modes');
1221
1222  // output the menu
1223  ptln('<ul>');
1224
1225  foreach ($menu as $item) {
1226    if (!$item['prompt']) continue;
1227    ptln('  <li><div class="li"><a href="'.wl($ID, 'do=admin&amp;page='.$item['plugin']).'">'.$item['prompt'].'</a></div></li>');
1228  }
1229
1230  ptln('</ul>');
1231}
1232
1233/**
1234 * Form to request a new password for an existing account
1235 *
1236 * @author Benoit Chesneau <benoit@bchesneau.info>
1237 */
1238function html_resendpwd() {
1239  global $lang;
1240  global $conf;
1241  global $ID;
1242
1243  print p_locale_xhtml('resendpwd');
1244  print '<div class="centeralign">'.NL;
1245  $form = new Doku_Form('dw__resendpwd', wl($ID));
1246  $form->startFieldset($lang['resendpwd']);
1247  $form->addHidden('do', 'resendpwd');
1248  $form->addHidden('save', '1');
1249  $form->addElement(form_makeTag('br'));
1250  $form->addElement(form_makeTextField('login', $_POST['login'], $lang['user'], '', 'block'));
1251  $form->addElement(form_makeTag('br'));
1252  $form->addElement(form_makeTag('br'));
1253  $form->addElement(form_makeButton('submit', '', $lang['btn_resendpwd']));
1254  $form->endFieldset();
1255  html_form('resendpwd', $form);
1256  print '</div>'.NL;
1257}
1258
1259/**
1260 * Return the TOC rendered to XHTML
1261 *
1262 * @author Andreas Gohr <andi@splitbrain.org>
1263 */
1264function html_TOC($toc){
1265    if(!count($toc)) return '';
1266    global $lang;
1267    $out  = '<!-- TOC START -->'.DOKU_LF;
1268    $out .= '<div class="toc">'.DOKU_LF;
1269    $out .= '<div class="tocheader toctoggle" id="toc__header">';
1270    $out .= $lang['toc'];
1271    $out .= '</div>'.DOKU_LF;
1272    $out .= '<div id="toc__inside">'.DOKU_LF;
1273    $out .= html_buildlist($toc,'toc','html_list_toc');
1274    $out .= '</div>'.DOKU_LF.'</div>'.DOKU_LF;
1275    $out .= '<!-- TOC END -->'.DOKU_LF;
1276    return $out;                                                                                                }
1277
1278/**
1279 * Callback for html_buildlist
1280 */
1281function html_list_toc($item){
1282    if($item['hid']){
1283        $link = '#'.$item['hid'];
1284    }else{
1285        $link = $item['link'];
1286    }
1287
1288    return '<span class="li"><a href="'.$link.'" class="toc">'.
1289           hsc($item['title']).'</a></span>';
1290}
1291
1292/**
1293 * Helper function to build TOC items
1294 *
1295 * Returns an array ready to be added to a TOC array
1296 *
1297 * @param string $link  - where to link (if $hash set to '#' it's a local anchor)
1298 * @param string $text  - what to display in the TOC
1299 * @param int    $level - nesting level
1300 * @param string $hash  - is prepended to the given $link, set blank if you want full links
1301 */
1302function html_mktocitem($link, $text, $level, $hash='#'){
1303    global $conf;
1304    return  array( 'link'  => $hash.$link,
1305                   'title' => $text,
1306                   'type'  => 'ul',
1307                   'level' => $level);
1308}
1309
1310/**
1311 * Output a Doku_Form object.
1312 * Triggers an event with the form name: HTML_{$name}FORM_OUTPUT
1313 *
1314 * @author Tom N Harris <tnharris@whoopdedo.org>
1315 */
1316function html_form($name, &$form) {
1317  // Safety check in case the caller forgets.
1318  $form->endFieldset();
1319  trigger_event('HTML_'.strtoupper($name).'FORM_OUTPUT', $form, 'html_form_output', false);
1320}
1321
1322/**
1323 * Form print function.
1324 * Just calls printForm() on the data object.
1325 */
1326function html_form_output($data) {
1327  $data->printForm();
1328}
1329
1330//Setup VIM: ex: et ts=2 enc=utf-8 :
1331