xref: /dokuwiki/inc/html.php (revision e52fdae2b7d40e3bb4e274a7522d3173935a3a8d)
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'].' '. 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 = 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 = date($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  if(is_array($_REQUEST['rev2'])){
787    $rev1 = (int) $_REQUEST['rev2'][0];
788    $rev2 = (int) $_REQUEST['rev2'][1];
789  }else{
790    $rev2 = (int) $_REQUEST['rev2'];
791  }
792
793  if($text){                      // compare text to the most current revision
794    $l_rev   = '';
795    $l_text  = rawWiki($ID,'');
796    $l_head  = '<a class="wikilink1" href="'.wl($ID).'">'.
797               $ID.' '.date($conf['dformat'],@filemtime(wikiFN($ID))).'</a> '.
798               $lang['current'];
799
800    $r_rev   = '';
801    $r_text  = cleanText($text);
802    $r_head  = $lang['yours'];
803  }else{
804    if($rev1 && $rev2){            // two specific revisions wanted
805      // make sure order is correct (older on the right)
806      if($rev1 < $rev2){
807        $l_rev = $rev1;
808        $r_rev = $rev2;
809      }else{
810        $l_rev = $rev2;
811        $r_rev = $rev1;
812      }
813    }elseif($rev1){                // single revision given, compare to current
814      $r_rev = '';
815      $l_rev = $rev1;
816    }else{                        // no revision was given, compare previous to current
817      $r_rev = '';
818      $revs = getRevisions($ID, 0, 1);
819      $l_rev = $revs[0];
820    }
821
822    $l_text = rawWiki($ID,$l_rev);
823    $r_text = rawWiki($ID,$r_rev);
824
825    $l_head = '<a class="wikilink1" href="'.wl($ID,"rev=$l_rev").'">'.
826              $ID.' '.date($conf['dformat'],$l_rev).'</a>';
827
828    if($r_rev){
829      $r_head = '<a class="wikilink1" href="'.wl($ID,"rev=$r_rev").'">'.
830                $ID.' '.date($conf['dformat'],$r_rev).'</a>';
831    }else{
832      $r_head  = '<a class="wikilink1" href="'.wl($ID).'">'.
833               $ID.' '.date($conf['dformat'],@filemtime(wikiFN($ID))).'</a> '.
834               $lang['current'];
835    }
836  }
837
838  $df = new Diff(explode("\n",htmlspecialchars($l_text)),
839                 explode("\n",htmlspecialchars($r_text)));
840
841  $tdf = new TableDiffFormatter();
842  if($intro) print p_locale_xhtml('diff');
843  ?>
844    <table class="diff">
845      <tr>
846        <th colspan="2">
847          <?php echo $l_head?>
848        </th>
849        <th colspan="2">
850          <?php echo $r_head?>
851        </th>
852      </tr>
853      <?php echo $tdf->format($df)?>
854    </table>
855  <?php
856}
857
858/**
859 * show warning on conflict detection
860 *
861 * @author Andreas Gohr <andi@splitbrain.org>
862 */
863function html_conflict($text,$summary){
864  global $ID;
865  global $lang;
866
867  print p_locale_xhtml('conflict');
868  $form = new Doku_Form('dw__editform');
869  $form->addHidden('id', $ID);
870  $form->addHidden('wikitext', $text);
871  $form->addHidden('summary', $summary);
872  $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('accesskey'=>'s')));
873  $form->addElement(form_makeButton('submit', 'cancel', $lang['btn_cancel']));
874  html_form('conflict', $form);
875  print '<br /><br /><br /><br />'.NL;
876}
877
878/**
879 * Prints the global message array
880 *
881 * @author Andreas Gohr <andi@splitbrain.org>
882 */
883function html_msgarea(){
884  global $MSG;
885
886  if(!isset($MSG)) return;
887
888  foreach($MSG as $msg){
889    print '<div class="'.$msg['lvl'].'">';
890    print $msg['msg'];
891    print '</div>';
892  }
893}
894
895/**
896 * Prints the registration form
897 *
898 * @author Andreas Gohr <andi@splitbrain.org>
899 */
900function html_register(){
901  global $lang;
902  global $conf;
903  global $ID;
904
905  print p_locale_xhtml('register');
906  print '<div class="centeralign">'.NL;
907  $form = new Doku_Form('dw__register', wl($ID));
908  $form->startFieldset($lang['register']);
909  $form->addHidden('do', 'register');
910  $form->addHidden('save', '1');
911  $form->addElement(form_makeTextField('login', $_POST['login'], $lang['user'], null, 'block', array('size'=>'50')));
912  if (!$conf['autopasswd']) {
913    $form->addElement(form_makePasswordField('pass', $lang['pass'], '', 'block', array('size'=>'50')));
914    $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
915  }
916  $form->addElement(form_makeTextField('fullname', $_POST['fullname'], $lang['fullname'], '', 'block', array('size'=>'50')));
917  $form->addElement(form_makeTextField('email', $_POST['email'], $lang['email'], '', 'block', array('size'=>'50')));
918  $form->addElement(form_makeButton('submit', '', $lang['register']));
919  $form->endFieldset();
920  html_form('register', $form);
921
922  print '</div>'.NL;
923}
924
925/**
926 * Print the update profile form
927 *
928 * @author Christopher Smith <chris@jalakai.co.uk>
929 * @author Andreas Gohr <andi@splitbrain.org>
930 */
931function html_updateprofile(){
932  global $lang;
933  global $conf;
934  global $ID;
935  global $INFO;
936  global $auth;
937
938  print p_locale_xhtml('updateprofile');
939
940  if (empty($_POST['fullname'])) $_POST['fullname'] = $INFO['userinfo']['name'];
941  if (empty($_POST['email'])) $_POST['email'] = $INFO['userinfo']['mail'];
942  print '<div class="centeralign">'.NL;
943  $form = new Doku_Form('dw__register', wl($ID));
944  $form->startFieldset($lang['profile']);
945  $form->addHidden('do', 'profile');
946  $form->addHidden('save', '1');
947  $form->addElement(form_makeTextField('fullname', $_SERVER['REMOTE_USER'], $lang['user'], '', 'block', array('size'=>'50', 'disabled'=>'disabled')));
948  $attr = array('size'=>'50');
949  if (!$auth->canDo('modName')) $attr['disabled'] = 'disabled';
950  $form->addElement(form_makeTextField('fullname', $_POST['fullname'], $lang['fullname'], '', 'block', $attr));
951  $form->addElement(form_makeTextField('email', $_POST['email'], $lang['email'], '', 'block', $attr));
952  $form->addElement(form_makeTag('br'));
953  if ($auth->canDo('modPass')) {
954    $form->addElement(form_makePasswordField('newpass', $lang['newpass'], '', 'block', array('size'=>'50')));
955    $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
956  }
957  if ($conf['profileconfirm']) {
958    $form->addElement(form_makeTag('br'));
959    $form->addElement(form_makePasswordField('oldpass', $lang['oldpass'], '', 'block', array('size'=>'50')));
960  }
961  $form->addElement(form_makeButton('submit', '', $lang['btn_save']));
962  $form->addElement(form_makeButton('reset', '', $lang['btn_reset']));
963  $form->endFieldset();
964  html_form('updateprofile', $form);
965  print '</div>'.NL;
966}
967
968/**
969 * This displays the edit form (lots of logic included)
970 *
971 * @fixme    this is a huge lump of code and should be modularized
972 * @triggers HTML_PAGE_FROMTEMPLATE
973 * @triggers HTML_EDITFORM_INJECTION
974 * @author   Andreas Gohr <andi@splitbrain.org>
975 */
976function html_edit($text=null,$include='edit'){ //FIXME: include needed?
977  global $ID;
978  global $REV;
979  global $DATE;
980  global $RANGE;
981  global $PRE;
982  global $SUF;
983  global $INFO;
984  global $SUM;
985  global $lang;
986  global $conf;
987
988  //set summary default
989  if(!$SUM){
990    if($REV){
991      $SUM = $lang['restored'];
992    }elseif(!$INFO['exists']){
993      $SUM = $lang['created'];
994    }
995  }
996
997  //no text? Load it!
998  if(!isset($text)){
999    $pr = false; //no preview mode
1000    if($INFO['exists']){
1001      if($RANGE){
1002        list($PRE,$text,$SUF) = rawWikiSlices($RANGE,$ID,$REV);
1003      }else{
1004        $text = rawWiki($ID,$REV);
1005      }
1006      $check = md5($text);
1007      $mod = false;
1008    }else{
1009      //try to load a pagetemplate
1010      $data = array($ID);
1011      $text = trigger_event('HTML_PAGE_FROMTEMPLATE',$data,'pageTemplate',true);
1012      $check = md5('');
1013      $mod = $text!=='';
1014    }
1015  }else{
1016    $pr = true; //preview mode
1017    if (isset($_REQUEST['changecheck'])) {
1018      $check = $_REQUEST['changecheck'];
1019      $mod = md5($text)!==$check;
1020    } else {
1021      // Why? Assume default text is unmodified.
1022      $check = md5($text);
1023      $mod = false;
1024    }
1025  }
1026
1027  $wr = $INFO['writable'];
1028  if($wr){
1029    if ($REV) print p_locale_xhtml('editrev');
1030    print p_locale_xhtml($include);
1031  }else{
1032    // check pseudo action 'source'
1033    if(!actionOK('source')){
1034      msg('Command disabled: source',-1);
1035      return;
1036    }
1037    print p_locale_xhtml('read');
1038  }
1039  if(!$DATE) $DATE = $INFO['lastmod'];
1040
1041
1042?>
1043  <div style="width:99%;">
1044
1045   <div class="toolbar">
1046      <div id="draft__status"><?php if(!empty($INFO['draft'])) echo $lang['draftdate'].' '.date($conf['dformat']);?></div>
1047      <div id="tool__bar"><?php if($wr){?><a href="<?php echo DOKU_BASE?>lib/exe/mediamanager.php?ns=<?php echo $INFO['namespace']?>"
1048      target="_blank"><?php echo $lang['mediaselect'] ?></a><?php }?></div>
1049
1050      <?php if($wr){?>
1051      <script type="text/javascript" charset="utf-8"><!--//--><![CDATA[//><!--
1052        <?php /* sets changed to true when previewed */?>
1053        textChanged = <?php ($mod) ? print 'true' : print 'false' ?>;
1054      //--><!]]></script>
1055      <span id="spell__action"></span>
1056      <div id="spell__suggest"></div>
1057      <?php } ?>
1058   </div>
1059   <div id="spell__result"></div>
1060<?php
1061  $form = new Doku_Form('dw__editform');
1062  $form->addHidden('id', $ID);
1063  $form->addHidden('rev', $REV);
1064  $form->addHidden('date', $DATE);
1065  $form->addHidden('prefix', $PRE);
1066  $form->addHidden('suffix', $SUF);
1067  $form->addHidden('changecheck', $check);
1068  $attr = array('tabindex'=>'1');
1069  if (!$wr) $attr['readonly'] = 'readonly';
1070  $form->addElement(form_makeWikiText($text, $attr));
1071  $form->addElement(form_makeOpenTag('div', array('id'=>'wiki__editbar')));
1072  $form->addElement(form_makeOpenTag('div', array('id'=>'size__ctl')));
1073  $form->addElement(form_makeCloseTag('div'));
1074  if ($wr) {
1075    $form->addElement(form_makeOpenTag('div', array('class'=>'editButtons')));
1076    $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('id'=>'edbtn__save', 'accesskey'=>'s', 'tabindex'=>'4')));
1077    $form->addElement(form_makeButton('submit', 'preview', $lang['btn_preview'], array('id'=>'edbtn__preview', 'accesskey'=>'p', 'tabindex'=>'5')));
1078    $form->addElement(form_makeButton('submit', 'draftdel', $lang['btn_cancel'], array('tabindex'=>'6')));
1079    $form->addElement(form_makeCloseTag('div'));
1080    $form->addElement(form_makeOpenTag('div', array('class'=>'summary')));
1081    $form->addElement(form_makeTextField('summary', $SUM, $lang['summary'], 'edit__summary', 'nowrap', array('size'=>'50', 'tabindex'=>'2')));
1082    $elem = html_minoredit();
1083    if ($elem) $form->addElement($elem);
1084    $form->addElement(form_makeCloseTag('div'));
1085  }
1086  $form->addElement(form_makeCloseTag('div'));
1087  html_form('edit', $form);
1088  print '</div>'.NL;
1089}
1090
1091/**
1092 * Adds a checkbox for minor edits for logged in users
1093 *
1094 * @author Andrea Gohr <andi@splitbrain.org>
1095 */
1096function html_minoredit(){
1097  global $conf;
1098  global $lang;
1099  // minor edits are for logged in users only
1100  if(!$conf['useacl'] || !$_SERVER['REMOTE_USER']){
1101    return false;
1102  }
1103
1104  $p = array();
1105  $p['tabindex'] = 3;
1106  if(!empty($_REQUEST['minor'])) $p['checked']='checked';
1107  return form_makeCheckboxField('minor', '1', $lang['minoredit'], 'minoredit', 'nowrap', $p);
1108}
1109
1110/**
1111 * prints some debug info
1112 *
1113 * @author Andreas Gohr <andi@splitbrain.org>
1114 */
1115function html_debug(){
1116  global $conf;
1117  global $lang;
1118  global $auth;
1119  global $INFO;
1120
1121  //remove sensitive data
1122  $cnf = $conf;
1123  $cnf['auth']='***';
1124  $cnf['notify']='***';
1125  $cnf['ftp']='***';
1126  $nfo = $INFO;
1127  $nfo['userinfo'] = '***';
1128  $ses = $_SESSION;
1129  $ses[$conf['title']]['auth'] = '***';
1130
1131  print '<html><body>';
1132
1133  print '<p>When reporting bugs please send all the following ';
1134  print 'output as a mail to andi@splitbrain.org ';
1135  print 'The best way to do this is to save this page in your browser</p>';
1136
1137  print '<b>$INFO:</b><pre>';
1138  print_r($nfo);
1139  print '</pre>';
1140
1141  print '<b>$_SERVER:</b><pre>';
1142  print_r($_SERVER);
1143  print '</pre>';
1144
1145  print '<b>$conf:</b><pre>';
1146  print_r($cnf);
1147  print '</pre>';
1148
1149  print '<b>DOKU_BASE:</b><pre>';
1150  print DOKU_BASE;
1151  print '</pre>';
1152
1153  print '<b>abs DOKU_BASE:</b><pre>';
1154  print DOKU_URL;
1155  print '</pre>';
1156
1157  print '<b>rel DOKU_BASE:</b><pre>';
1158  print dirname($_SERVER['PHP_SELF']).'/';
1159  print '</pre>';
1160
1161  print '<b>PHP Version:</b><pre>';
1162  print phpversion();
1163  print '</pre>';
1164
1165  print '<b>locale:</b><pre>';
1166  print setlocale(LC_ALL,0);
1167  print '</pre>';
1168
1169  print '<b>encoding:</b><pre>';
1170  print $lang['encoding'];
1171  print '</pre>';
1172
1173  if($auth){
1174    print '<b>Auth backend capabilities:</b><pre>';
1175    print_r($auth->cando);
1176    print '</pre>';
1177  }
1178
1179  print '<b>$_SESSION:</b><pre>';
1180  print_r($ses);
1181  print '</pre>';
1182
1183  print '<b>Environment:</b><pre>';
1184  print_r($_ENV);
1185  print '</pre>';
1186
1187  print '<b>PHP settings:</b><pre>';
1188  $inis = ini_get_all();
1189  print_r($inis);
1190  print '</pre>';
1191
1192  print '</body></html>';
1193}
1194
1195function html_admin(){
1196  global $ID;
1197  global $INFO;
1198  global $lang;
1199  global $conf;
1200
1201  print p_locale_xhtml('admin');
1202
1203  // build menu of admin functions from the plugins that handle them
1204  $pluginlist = plugin_list('admin');
1205  $menu = array();
1206  foreach ($pluginlist as $p) {
1207    if($obj =& plugin_load('admin',$p) === NULL) continue;
1208
1209    // check permissions
1210    if($obj->forAdminOnly() && !$INFO['isadmin']) continue;
1211
1212    $menu[] = array('plugin' => $p,
1213                    'prompt' => $obj->getMenuText($conf['lang']),
1214                    'sort' => $obj->getMenuSort()
1215                   );
1216  }
1217
1218  usort($menu, 'p_sort_modes');
1219
1220  // output the menu
1221  ptln('<ul>');
1222
1223  foreach ($menu as $item) {
1224    if (!$item['prompt']) continue;
1225    ptln('  <li><div class="li"><a href="'.wl($ID, 'do=admin&amp;page='.$item['plugin']).'">'.$item['prompt'].'</a></div></li>');
1226  }
1227
1228  ptln('</ul>');
1229}
1230
1231/**
1232 * Form to request a new password for an existing account
1233 *
1234 * @author Benoit Chesneau <benoit@bchesneau.info>
1235 */
1236function html_resendpwd() {
1237  global $lang;
1238  global $conf;
1239  global $ID;
1240
1241  print p_locale_xhtml('resendpwd');
1242  print '<div class="centeralign">'.NL;
1243  $form = new Doku_Form('dw__resendpwd', wl($ID));
1244  $form->startFieldset($lang['resendpwd']);
1245  $form->addHidden('do', 'resendpwd');
1246  $form->addHidden('save', '1');
1247  $form->addElement(form_makeTag('br'));
1248  $form->addElement(form_makeTextField('login', $_POST['login'], $lang['user'], '', 'block'));
1249  $form->addElement(form_makeTag('br'));
1250  $form->addElement(form_makeTag('br'));
1251  $form->addElement(form_makeButton('submit', '', $lang['btn_resendpwd']));
1252  $form->endFieldset();
1253  html_form('resendpwd', $form);
1254  print '</div>'.NL;
1255}
1256
1257/**
1258 * Return the TOC rendered to XHTML
1259 *
1260 * @author Andreas Gohr <andi@splitbrain.org>
1261 */
1262function html_TOC($toc){
1263    if(!count($toc)) return '';
1264    global $lang;
1265    $out  = '<!-- TOC START -->'.DOKU_LF;
1266    $out .= '<div class="toc">'.DOKU_LF;
1267    $out .= '<div class="tocheader toctoggle" id="toc__header">';
1268    $out .= $lang['toc'];
1269    $out .= '</div>'.DOKU_LF;
1270    $out .= '<div id="toc__inside">'.DOKU_LF;
1271    $out .= html_buildlist($toc,'toc','html_list_toc');
1272    $out .= '</div>'.DOKU_LF.'</div>'.DOKU_LF;
1273    $out .= '<!-- TOC END -->'.DOKU_LF;
1274    return $out;                                                                                                }
1275
1276/**
1277 * Callback for html_buildlist
1278 */
1279function html_list_toc($item){
1280    if($item['hid']){
1281        $link = '#'.$item['hid'];
1282    }else{
1283        $link = $item['link'];
1284    }
1285
1286    return '<span class="li"><a href="'.$link.'" class="toc">'.
1287           hsc($item['title']).'</a></span>';
1288}
1289
1290/**
1291 * Helper function to build TOC items
1292 *
1293 * Returns an array ready to be added to a TOC array
1294 *
1295 * @param string $link  - where to link (if $hash set to '#' it's a local anchor)
1296 * @param string $text  - what to display in the TOC
1297 * @param int    $level - nesting level
1298 * @param string $hash  - is prepended to the given $link, set blank if you want full links
1299 */
1300function html_mktocitem($link, $text, $level, $hash='#'){
1301    global $conf;
1302    return  array( 'link'  => $hash.$link,
1303                   'title' => $text,
1304                   'type'  => 'ul',
1305                   'level' => $level);
1306}
1307
1308/**
1309 * Output a Doku_Form object.
1310 * Triggers an event with the form name: HTML_{$name}FORM_OUTPUT
1311 *
1312 * @author Tom N Harris <tnharris@whoopdedo.org>
1313 */
1314function html_form($name, &$form) {
1315  // Safety check in case the caller forgets.
1316  $form->endFieldset();
1317  trigger_event('HTML_'.strtoupper($name).'FORM_OUTPUT', $form, 'html_form_output', false);
1318}
1319
1320/**
1321 * Form print function.
1322 * Just calls printForm() on the data object.
1323 */
1324function html_form_output($data) {
1325  $data->printForm();
1326}
1327
1328//Setup VIM: ex: et ts=2 enc=utf-8 :
1329