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