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