xref: /dokuwiki/inc/html.php (revision d5acc30de20298eb6ed7545e70484599c4d95867)
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')) die('meh.');
10if(!defined('NL')) define('NL',"\n");
11
12/**
13 * Convenience function to quickly build a wikilink
14 *
15 * @author Andreas Gohr <andi@splitbrain.org>
16 */
17function html_wikilink($id,$name=null,$search=''){
18    static $xhtml_renderer = null;
19    if(is_null($xhtml_renderer)){
20        $xhtml_renderer = p_get_renderer('xhtml');
21    }
22
23    return $xhtml_renderer->internallink($id,$name,$search,true,'navigation');
24}
25
26/**
27 * Helps building long attribute lists
28 *
29 * @deprecated Use buildAttributes instead
30 * @author Andreas Gohr <andi@splitbrain.org>
31 */
32function html_attbuild($attributes){
33    $ret = '';
34    foreach ( $attributes as $key => $value ) {
35        $ret .= $key.'="'.formText($value).'" ';
36    }
37    return trim($ret);
38}
39
40/**
41 * The loginform
42 *
43 * @author   Andreas Gohr <andi@splitbrain.org>
44 */
45function html_login(){
46    global $lang;
47    global $conf;
48    global $ID;
49
50    print p_locale_xhtml('login');
51    print '<div class="centeralign">'.NL;
52    $form = new Doku_Form(array('id' => 'dw__login'));
53    $form->startFieldset($lang['btn_login']);
54    $form->addHidden('id', $ID);
55    $form->addHidden('do', 'login');
56    $form->addElement(form_makeTextField('u', ((!$_REQUEST['http_credentials']) ? $_REQUEST['u'] : ''), $lang['user'], 'focus__this', 'block'));
57    $form->addElement(form_makePasswordField('p', $lang['pass'], '', 'block'));
58    if($conf['rememberme']) {
59        $form->addElement(form_makeCheckboxField('r', '1', $lang['remember'], 'remember__me', 'simple'));
60    }
61    $form->addElement(form_makeButton('submit', '', $lang['btn_login']));
62    $form->endFieldset();
63
64    if(actionOK('register')){
65        $form->addElement('<p>'.$lang['reghere'].': '.tpl_actionlink('register','','','',true).'</p>');
66    }
67
68    if (actionOK('resendpwd')) {
69        $form->addElement('<p>'.$lang['pwdforget'].': '.tpl_actionlink('resendpwd','','','',true).'</p>');
70    }
71
72    html_form('login', $form);
73    print '</div>'.NL;
74}
75
76/**
77 * inserts section edit buttons if wanted or removes the markers
78 *
79 * @author Andreas Gohr <andi@splitbrain.org>
80 */
81function html_secedit($text,$show=true){
82    global $INFO;
83
84    $regexp = '#<!-- EDIT(\d+) ([A-Z_]+) (?:"([^"]*)" )?\[(\d+-\d*)\] -->#';
85
86    if(!$INFO['writable'] || !$show || $INFO['rev']){
87        return preg_replace($regexp,'',$text);
88    }
89
90    return preg_replace_callback($regexp,
91                'html_secedit_button', $text);
92}
93
94/**
95 * prepares section edit button data for event triggering
96 * used as a callback in html_secedit
97 *
98 * @triggers HTML_SECEDIT_BUTTON
99 * @author Andreas Gohr <andi@splitbrain.org>
100 */
101function html_secedit_button($matches){
102    $data = array('secid'  => $matches[1],
103                  'target' => strtolower($matches[2]),
104                  'range'  => $matches[count($matches) - 1]);
105    if (count($matches) === 5) {
106        $data['name'] = $matches[3];
107    }
108
109    return trigger_event('HTML_SECEDIT_BUTTON', $data,
110                         'html_secedit_get_button');
111}
112
113/**
114 * prints a section editing button
115 * used as default action form HTML_SECEDIT_BUTTON
116 *
117 * @author Adrian Lang <lang@cosmocode.de>
118 */
119function html_secedit_get_button($data) {
120    global $ID;
121    global $INFO;
122
123    if (!isset($data['name']) || $data['name'] === '') return;
124
125    $name = $data['name'];
126    unset($data['name']);
127
128    $secid = $data['secid'];
129    unset($data['secid']);
130
131    return "<div class='secedit editbutton_" . $data['target'] .
132                       " editbutton_" . $secid . "'>" .
133           html_btn('secedit', $ID, '',
134                    array_merge(array('do'  => 'edit',
135                                      'rev' => $INFO['lastmod'],
136                                      'summary' => '['.$name.'] '), $data),
137                    'post', $name) . '</div>';
138}
139
140/**
141 * Just the back to top button (in its own form)
142 *
143 * @author Andreas Gohr <andi@splitbrain.org>
144 */
145function html_topbtn(){
146    global $lang;
147
148    $ret  = '';
149    $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>';
150
151    return $ret;
152}
153
154/**
155 * Displays a button (using its own form)
156 * If tooltip exists, the access key tooltip is replaced.
157 *
158 * @author Andreas Gohr <andi@splitbrain.org>
159 */
160function html_btn($name,$id,$akey,$params,$method='get',$tooltip='',$label=false){
161    global $conf;
162    global $lang;
163
164    if (!$label)
165        $label = $lang['btn_'.$name];
166
167    $ret = '';
168    $tip = '';
169
170    //filter id (without urlencoding)
171    $id = idfilter($id,false);
172
173    //make nice URLs even for buttons
174    if($conf['userewrite'] == 2){
175        $script = DOKU_BASE.DOKU_SCRIPT.'/'.$id;
176    }elseif($conf['userewrite']){
177        $script = DOKU_BASE.$id;
178    }else{
179        $script = DOKU_BASE.DOKU_SCRIPT;
180        $params['id'] = $id;
181    }
182
183    $ret .= '<form class="button btn_'.$name.'" method="'.$method.'" action="'.$script.'"><div class="no">';
184
185    if(is_array($params)){
186        reset($params);
187        while (list($key, $val) = each($params)) {
188            $ret .= '<input type="hidden" name="'.$key.'" ';
189            $ret .= 'value="'.htmlspecialchars($val).'" />';
190        }
191    }
192
193    if ($tooltip!='') {
194        $tip = htmlspecialchars($tooltip);
195    }else{
196        $tip = htmlspecialchars($label);
197    }
198
199    $ret .= '<input type="submit" value="'.hsc($label).'" class="button" ';
200    if($akey){
201        $tip .= ' ['.strtoupper($akey).']';
202        $ret .= 'accesskey="'.$akey.'" ';
203    }
204    $ret .= 'title="'.$tip.'" ';
205    $ret .= '/>';
206    $ret .= '</div></form>';
207
208    return $ret;
209}
210
211/**
212 * show a wiki page
213 *
214 * @author Andreas Gohr <andi@splitbrain.org>
215 */
216function html_show($txt=null){
217    global $ID;
218    global $REV;
219    global $HIGH;
220    global $INFO;
221    //disable section editing for old revisions or in preview
222    if($txt || $REV){
223        $secedit = false;
224    }else{
225        $secedit = true;
226    }
227
228    if (!is_null($txt)){
229        //PreviewHeader
230        echo '<br id="scroll__here" />';
231        echo p_locale_xhtml('preview');
232        echo '<div class="preview">';
233        $html = html_secedit(p_render('xhtml',p_get_instructions($txt),$info),$secedit);
234        if($INFO['prependTOC']) $html = tpl_toc(true).$html;
235        echo $html;
236        echo '<div class="clearer"></div>';
237        echo '</div>';
238
239    }else{
240        if ($REV) print p_locale_xhtml('showrev');
241        $html = p_wiki_xhtml($ID,$REV,true);
242        $html = html_secedit($html,$secedit);
243        if($INFO['prependTOC']) $html = tpl_toc(true).$html;
244        $html = html_hilight($html,$HIGH);
245        echo $html;
246    }
247}
248
249/**
250 * ask the user about how to handle an exisiting draft
251 *
252 * @author Andreas Gohr <andi@splitbrain.org>
253 */
254function html_draft(){
255    global $INFO;
256    global $ID;
257    global $lang;
258    global $conf;
259    $draft = unserialize(io_readFile($INFO['draft'],false));
260    $text  = cleanText(con($draft['prefix'],$draft['text'],$draft['suffix'],true));
261
262    print p_locale_xhtml('draft');
263    $form = new Doku_Form(array('id' => 'dw__editform'));
264    $form->addHidden('id', $ID);
265    $form->addHidden('date', $draft['date']);
266    $form->addElement(form_makeWikiText($text, array('readonly'=>'readonly')));
267    $form->addElement(form_makeOpenTag('div', array('id'=>'draft__status')));
268    $form->addElement($lang['draftdate'].' '. dformat(filemtime($INFO['draft'])));
269    $form->addElement(form_makeCloseTag('div'));
270    $form->addElement(form_makeButton('submit', 'recover', $lang['btn_recover'], array('tabindex'=>'1')));
271    $form->addElement(form_makeButton('submit', 'draftdel', $lang['btn_draftdel'], array('tabindex'=>'2')));
272    $form->addElement(form_makeButton('submit', 'show', $lang['btn_cancel'], array('tabindex'=>'3')));
273    html_form('draft', $form);
274}
275
276/**
277 * Highlights searchqueries in HTML code
278 *
279 * @author Andreas Gohr <andi@splitbrain.org>
280 * @author Harry Fuecks <hfuecks@gmail.com>
281 */
282function html_hilight($html,$phrases){
283    $phrases = array_filter((array) $phrases);
284    $regex = join('|',array_map('ft_snippet_re_preprocess', array_map('preg_quote_cb',$phrases)));
285
286    if ($regex === '') return $html;
287    if (!utf8_check($regex)) return $html;
288    $html = @preg_replace_callback("/((<[^>]*)|$regex)/ui",'html_hilight_callback',$html);
289    return $html;
290}
291
292/**
293 * Callback used by html_hilight()
294 *
295 * @author Harry Fuecks <hfuecks@gmail.com>
296 */
297function html_hilight_callback($m) {
298    $hlight = unslash($m[0]);
299    if ( !isset($m[2])) {
300        $hlight = '<span class="search_hit">'.$hlight.'</span>';
301    }
302    return $hlight;
303}
304
305/**
306 * Run a search and display the result
307 *
308 * @author Andreas Gohr <andi@splitbrain.org>
309 */
310function html_search(){
311    global $conf;
312    global $QUERY;
313    global $ID;
314    global $lang;
315
316    $intro = p_locale_xhtml('searchpage');
317    // allow use of placeholder in search intro
318    $intro = str_replace(
319                array('@QUERY@','@SEARCH@'),
320                array(hsc(rawurlencode($QUERY)),hsc($QUERY)),
321                $intro);
322    echo $intro;
323    flush();
324
325    //show progressbar
326    print '<div class="centeralign" id="dw__loading">'.NL;
327    print '<script type="text/javascript" charset="utf-8"><!--//--><![CDATA[//><!--'.NL;
328    print 'showLoadBar();'.NL;
329    print '//--><!]]></script>'.NL;
330    print '<br /></div>'.NL;
331    flush();
332
333    //do quick pagesearch
334    $data = array();
335
336    $data = ft_pageLookup($QUERY,true,useHeading('navigation'));
337    if(count($data)){
338        print '<div class="search_quickresult">';
339        print '<h3>'.$lang['quickhits'].':</h3>';
340        print '<ul class="search_quickhits">';
341        foreach($data as $id => $title){
342            print '<li> ';
343            if (useHeading('navigation')) {
344                $name = $title;
345            }else{
346                $ns = getNS($id);
347                if($ns){
348                    $name = shorten(noNS($id), ' ('.$ns.')',30);
349                }else{
350                    $name = $id;
351                }
352            }
353            print html_wikilink(':'.$id,$name);
354            print '</li> ';
355        }
356        print '</ul> ';
357        //clear float (see http://www.complexspiral.com/publications/containing-floats/)
358        print '<div class="clearer"></div>';
359        print '</div>';
360    }
361    flush();
362
363    //do fulltext search
364    $data = ft_pageSearch($QUERY,$regex);
365    if(count($data)){
366        $num = 1;
367        foreach($data as $id => $cnt){
368            print '<div class="search_result">';
369            print html_wikilink(':'.$id,useHeading('navigation')?null:$id,$regex);
370            if($cnt !== 0){
371                print ': <span class="search_cnt">'.$cnt.' '.$lang['hits'].'</span><br />';
372                if($num < FT_SNIPPET_NUMBER){ // create snippets for the first number of matches only
373                    print '<div class="search_snippet">'.ft_snippet($id,$regex).'</div>';
374                }
375                $num++;
376            }
377            print '</div>';
378            flush();
379        }
380    }else{
381        print '<div class="nothing">'.$lang['nothingfound'].'</div>';
382    }
383
384    //hide progressbar
385    print '<script type="text/javascript" charset="utf-8"><!--//--><![CDATA[//><!--'.NL;
386    print 'hideLoadBar("dw__loading");'.NL;
387    print '//--><!]]></script>'.NL;
388    flush();
389}
390
391/**
392 * Display error on locked pages
393 *
394 * @author Andreas Gohr <andi@splitbrain.org>
395 */
396function html_locked(){
397    global $ID;
398    global $conf;
399    global $lang;
400    global $INFO;
401
402    $locktime = filemtime(wikiLockFN($ID));
403    $expire = dformat($locktime + $conf['locktime']);
404    $min    = round(($conf['locktime'] - (time() - $locktime) )/60);
405
406    print p_locale_xhtml('locked');
407    print '<ul>';
408    print '<li><div class="li"><strong>'.$lang['lockedby'].':</strong> '.editorinfo($INFO['locked']).'</div></li>';
409    print '<li><div class="li"><strong>'.$lang['lockexpire'].':</strong> '.$expire.' ('.$min.' min)</div></li>';
410    print '</ul>';
411}
412
413/**
414 * list old revisions
415 *
416 * @author Andreas Gohr <andi@splitbrain.org>
417 * @author Ben Coburn <btcoburn@silicodon.net>
418 * @author Kate Arzamastseva <pshns@ukr.net>
419 */
420function html_revisions($first=0, $media_id = false){
421    global $ID;
422    global $INFO;
423    global $conf;
424    global $lang;
425    $id = $ID;
426    /* we need to get one additionally log entry to be able to
427     * decide if this is the last page or is there another one.
428     * see html_recent()
429     */
430    if (!$media_id) $revisions = getRevisions($ID, $first, $conf['recent']+1);
431    else {
432        $revisions = getRevisions($media_id, $first, $conf['recent']+1, 8192, true);
433        $id = $media_id;
434    }
435
436    if(count($revisions)==0 && $first!=0){
437        $first=0;
438        if (!$media_id) $revisions = getRevisions($ID, $first, $conf['recent']+1);
439        else $revisions = getRevisions($media_id, $first, $conf['recent']+1, 8192, true);
440    }
441    $hasNext = false;
442    if (count($revisions)>$conf['recent']) {
443        $hasNext = true;
444        array_pop($revisions); // remove extra log entry
445    }
446
447    if (!$media_id) $date = dformat($INFO['lastmod']);
448    else $date = dformat(@filemtime(mediaFN($id)));
449
450    if (!$media_id) print p_locale_xhtml('revisions');
451
452    $params = array('id' => 'page__revisions', 'class' => 'changes');
453    if ($media_id) $params['action'] = media_managerURL(array('image' => $media_id), '&');
454
455    $form = new Doku_Form($params);
456    $form->addElement(form_makeOpenTag('ul'));
457
458    if (!$media_id) $exists = $INFO['exists'];
459    else $exists = @file_exists(mediaFN($id));
460
461    if($exists && $first==0){
462        if (!$media_id && isset($INFO['meta']) && isset($INFO['meta']['last_change']) && $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT)
463            $form->addElement(form_makeOpenTag('li', array('class' => 'minor')));
464        else
465            $form->addElement(form_makeOpenTag('li'));
466        $form->addElement(form_makeOpenTag('div', array('class' => 'li')));
467        $form->addElement(form_makeTag('input', array(
468                        'type' => 'checkbox',
469                        'name' => 'rev2[]',
470                        'value' => 'current')));
471
472        $form->addElement(form_makeOpenTag('span', array('class' => 'date')));
473        $form->addElement($date);
474        $form->addElement(form_makeCloseTag('span'));
475
476        $form->addElement('<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />');
477
478        if (!$media_id) $href = wl($id);
479        else $href = media_managerURL(array('image' => $id, 'tab_details' => 'view'), '&');
480        $form->addElement(form_makeOpenTag('a', array(
481                        'class' => 'wikilink1',
482                        'href'  => $href)));
483        $form->addElement($id);
484        $form->addElement(form_makeCloseTag('a'));
485
486        if ($media_id) $form->addElement(form_makeOpenTag('div'));
487
488        if (!$media_id) {
489            $form->addElement(form_makeOpenTag('span', array('class' => 'sum')));
490            $form->addElement(' &ndash; ');
491            $form->addElement(htmlspecialchars($INFO['sum']));
492            $form->addElement(form_makeCloseTag('span'));
493        }
494
495        $form->addElement(form_makeOpenTag('span', array('class' => 'user')));
496        if (!$media_id) $editor = $INFO['editor'];
497        else {
498            $revinfo = getRevisionInfo($id, @filemtime(fullpath(mediaFN($id))), 1024, true);
499            if($revinfo['user']){
500                $editor = $revinfo['user'];
501            }else{
502                $editor = $revinfo['ip'];
503            }
504        }
505        $form->addElement((empty($editor))?('('.$lang['external_edit'].')'):editorinfo($editor));
506        $form->addElement(form_makeCloseTag('span'));
507
508        $form->addElement('('.$lang['current'].')');
509
510        if ($media_id) $form->addElement(form_makeCloseTag('div'));
511
512        $form->addElement(form_makeCloseTag('div'));
513        $form->addElement(form_makeCloseTag('li'));
514    }
515
516    foreach($revisions as $rev){
517        $date = dformat($rev);
518        if (!$media_id) {
519            $info = getRevisionInfo($id,$rev,true);
520            $exists = page_exists($id,$rev);
521        }  else {
522            $info = getRevisionInfo($id,$rev,true,true);
523            $exists = @file_exists(mediaFN($id,$rev));
524        }
525
526        if ($info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT)
527            $form->addElement(form_makeOpenTag('li', array('class' => 'minor')));
528        else
529            $form->addElement(form_makeOpenTag('li'));
530        $form->addElement(form_makeOpenTag('div', array('class' => 'li')));
531        if($exists){
532            $form->addElement(form_makeTag('input', array(
533                            'type' => 'checkbox',
534                            'name' => 'rev2[]',
535                            'value' => $rev)));
536        }else{
537            $form->addElement('<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />');
538        }
539
540        $form->addElement(form_makeOpenTag('span', array('class' => 'date')));
541        $form->addElement($date);
542        $form->addElement(form_makeCloseTag('span'));
543
544        if($exists){
545            if (!$media_id) $href = wl($id,"rev=$rev,do=diff", false, '&');
546            else $href = media_managerURL(array('image' => $id, 'rev' => $rev, 'mediado' => 'diff'), '&');
547            $form->addElement(form_makeOpenTag('a', array('href' => $href, 'class' => 'diff_link')));
548            $form->addElement(form_makeTag('img', array(
549                            'src'    => DOKU_BASE.'lib/images/diff.png',
550                            'width'  => 15,
551                            'height' => 11,
552                            'title'  => $lang['diff'],
553                            'alt'    => $lang['diff'])));
554            $form->addElement(form_makeCloseTag('a'));
555            if (!$media_id) $href = wl($id,"rev=$rev",false,'&');
556            else $href = media_managerURL(array('image' => $id, 'tab_details' => 'view', 'rev' => $rev), '&');
557            $form->addElement(form_makeOpenTag('a', array('href' => $href, 'class' => 'wikilink1')));
558            $form->addElement($id);
559            $form->addElement(form_makeCloseTag('a'));
560        }else{
561            $form->addElement('<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />');
562            $form->addElement($id);
563        }
564
565        if ($media_id) $form->addElement(form_makeOpenTag('div'));
566
567        if ($info['sum']) {
568            $form->addElement(form_makeOpenTag('span', array('class' => 'sum')));
569            if (!$media_id) $form->addElement(' &ndash; ');
570            $form->addElement(htmlspecialchars($info['sum']));
571            $form->addElement(form_makeCloseTag('span'));
572        }
573
574        $form->addElement(form_makeOpenTag('span', array('class' => 'user')));
575        if($info['user']){
576            $form->addElement(editorinfo($info['user']));
577            if(auth_ismanager()){
578                $form->addElement(' ('.$info['ip'].')');
579            }
580        }else{
581            $form->addElement($info['ip']);
582        }
583        $form->addElement(form_makeCloseTag('span'));
584
585        if ($media_id) $form->addElement(form_makeCloseTag('div'));
586
587        $form->addElement(form_makeCloseTag('div'));
588        $form->addElement(form_makeCloseTag('li'));
589    }
590    $form->addElement(form_makeCloseTag('ul'));
591    if (!$media_id) {
592        $form->addElement(form_makeButton('submit', 'diff', $lang['diff2']));
593    } else {
594        $form->addHidden('mediado', 'diff');
595        $form->addElement(form_makeButton('submit', '', $lang['diff2']));
596    }
597    html_form('revisions', $form);
598
599    print '<div class="pagenav">';
600    $last = $first + $conf['recent'];
601    if ($first > 0) {
602        $first -= $conf['recent'];
603        if ($first < 0) $first = 0;
604        print '<div class="pagenav-prev">';
605        if ($media_id) {
606            print html_btn('newer',$media_id,"p",media_managerURL(array('first' => $first), '&amp;', false, true));
607        } else {
608            print html_btn('newer',$id,"p",array('do' => 'revisions', 'first' => $first));
609        }
610        print '</div>';
611    }
612    if ($hasNext) {
613        print '<div class="pagenav-next">';
614        if ($media_id) {
615            print html_btn('older',$media_id,"n",media_managerURL(array('first' => $last), '&amp;', false, true));
616        } else {
617            print html_btn('older',$id,"n",array('do' => 'revisions', 'first' => $last));
618        }
619        print '</div>';
620    }
621    print '</div>';
622
623}
624
625/**
626 * display recent changes
627 *
628 * @author Andreas Gohr <andi@splitbrain.org>
629 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
630 * @author Ben Coburn <btcoburn@silicodon.net>
631 * @author Kate Arzamastseva <pshns@ukr.net>
632 */
633function html_recent($first=0, $show_changes='both'){
634    global $conf;
635    global $lang;
636    global $ID;
637    /* we need to get one additionally log entry to be able to
638     * decide if this is the last page or is there another one.
639     * This is the cheapest solution to get this information.
640     */
641    $flags = 0;
642    if ($show_changes == 'mediafiles' && $conf['mediarevisions']) {
643        $flags = RECENTS_MEDIA_CHANGES;
644    } elseif ($show_changes == 'pages') {
645        $flags = 0;
646    } elseif ($conf['mediarevisions']) {
647        $show_changes = 'both';
648        $flags = RECENTS_MEDIA_PAGES_MIXED;
649    }
650
651    $recents = getRecents($first,$conf['recent'] + 1,getNS($ID),$flags);
652    if(count($recents) == 0 && $first != 0){
653        $first=0;
654        $recents = getRecents($first,$conf['recent'] + 1,getNS($ID),$flags);
655    }
656    $hasNext = false;
657    if (count($recents)>$conf['recent']) {
658        $hasNext = true;
659        array_pop($recents); // remove extra log entry
660    }
661
662    print p_locale_xhtml('recent');
663
664    if (getNS($ID) != '')
665        print '<div class="level1"><p>' . sprintf($lang['recent_global'], getNS($ID), wl('', 'do=recent')) . '</p></div>';
666
667    $form = new Doku_Form(array('id' => 'dw__recent', 'method' => 'GET', 'class' => 'changes'));
668    $form->addHidden('sectok', null);
669    $form->addHidden('do', 'recent');
670    $form->addHidden('id', $ID);
671
672    if ($conf['mediarevisions']) {
673        $form->addElement('<div class="changeType">');
674        $form->addElement(form_makeListboxField(
675                    'show_changes',
676                    array(
677                        'pages'      => $lang['pages_changes'],
678                        'mediafiles' => $lang['media_changes'],
679                        'both'       => $lang['both_changes']),
680                    $show_changes,
681                    $lang['changes_type'],
682                    '','',
683                    array('class'=>'quickselect')));
684
685        $form->addElement(form_makeButton('submit', 'recent', $lang['btn_apply']));
686        $form->addElement('</div>');
687    }
688
689    $form->addElement(form_makeOpenTag('ul'));
690
691    foreach($recents as $recent){
692        $date = dformat($recent['date']);
693        if ($recent['type']===DOKU_CHANGE_TYPE_MINOR_EDIT)
694            $form->addElement(form_makeOpenTag('li', array('class' => 'minor')));
695        else
696            $form->addElement(form_makeOpenTag('li'));
697
698        $form->addElement(form_makeOpenTag('div', array('class' => 'li')));
699
700        if ($recent['media']) {
701            $form->addElement(media_printicon($recent['id']));
702        } else {
703            $icon = DOKU_BASE.'lib/images/fileicons/file.png';
704            $form->addElement('<img src="'.$icon.'" alt="'.$filename.'" class="icon" />');
705        }
706
707        $form->addElement(form_makeOpenTag('span', array('class' => 'date')));
708        $form->addElement($date);
709        $form->addElement(form_makeCloseTag('span'));
710
711        if ($recent['media']) {
712            $diff = (count(getRevisions($recent['id'], 0, 1, 8192, true)) && @file_exists(mediaFN($recent['id'])));
713            if ($diff) {
714                $href = media_managerURL(array('tab_details' => 'history',
715                    'mediado' => 'diff', 'image' => $recent['id'], 'ns' => getNS($recent['id'])), '&');
716            }
717        } else {
718            $href = wl($recent['id'],"do=diff", false, '&');
719        }
720
721        if ($recent['media'] && !$diff) {
722            $form->addElement('<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />');
723        } else {
724            $form->addElement(form_makeOpenTag('a', array('class' => 'diff_link', 'href' => $href)));
725            $form->addElement(form_makeTag('img', array(
726                            'src'   => DOKU_BASE.'lib/images/diff.png',
727                            'width' => 15,
728                            'height'=> 11,
729                            'title' => $lang['diff'],
730                            'alt'   => $lang['diff']
731                            )));
732            $form->addElement(form_makeCloseTag('a'));
733        }
734
735        if ($recent['media']) {
736            $href = media_managerURL(array('tab_details' => 'history',
737                'image' => $recent['id'], 'ns' => getNS($recent['id'])), '&');
738        } else {
739            $href = wl($recent['id'],"do=revisions",false,'&');
740        }
741        $form->addElement(form_makeOpenTag('a', array('class' => 'revisions_link', 'href' => $href)));
742        $form->addElement(form_makeTag('img', array(
743                        'src'   => DOKU_BASE.'lib/images/history.png',
744                        'width' => 12,
745                        'height'=> 14,
746                        'title' => $lang['btn_revs'],
747                        'alt'   => $lang['btn_revs']
748                        )));
749        $form->addElement(form_makeCloseTag('a'));
750
751        if ($recent['media']) {
752            $href = media_managerURL(array('tab_details' => 'view', 'image' => $recent['id'], 'ns' => getNS($recent['id'])), '&');
753            $class = (file_exists(mediaFN($recent['id']))) ? 'wikilink1' : $class = 'wikilink2';
754            $form->addElement(form_makeOpenTag('a', array('class' => $class, 'href' => $href)));
755            $form->addElement($recent['id']);
756            $form->addElement(form_makeCloseTag('a'));
757        } else {
758            $form->addElement(html_wikilink(':'.$recent['id'],useHeading('navigation')?null:$recent['id']));
759        }
760        $form->addElement(form_makeOpenTag('span', array('class' => 'sum')));
761        $form->addElement(' &ndash; '.htmlspecialchars($recent['sum']));
762        $form->addElement(form_makeCloseTag('span'));
763
764        $form->addElement(form_makeOpenTag('span', array('class' => 'user')));
765        if($recent['user']){
766            $form->addElement(editorinfo($recent['user']));
767            if(auth_ismanager()){
768                $form->addElement(' ('.$recent['ip'].')');
769            }
770        }else{
771            $form->addElement($recent['ip']);
772        }
773        $form->addElement(form_makeCloseTag('span'));
774
775        $form->addElement(form_makeCloseTag('div'));
776        $form->addElement(form_makeCloseTag('li'));
777    }
778    $form->addElement(form_makeCloseTag('ul'));
779
780    $form->addElement(form_makeOpenTag('div', array('class' => 'pagenav')));
781    $last = $first + $conf['recent'];
782    if ($first > 0) {
783        $first -= $conf['recent'];
784        if ($first < 0) $first = 0;
785        $form->addElement(form_makeOpenTag('div', array('class' => 'pagenav-prev')));
786        $form->addElement(form_makeTag('input', array(
787                    'type'  => 'submit',
788                    'name'  => 'first['.$first.']',
789                    'value' => $lang['btn_newer'],
790                    'accesskey' => 'n',
791                    'title' => $lang['btn_newer'].' [N]',
792                    'class' => 'button show'
793                    )));
794        $form->addElement(form_makeCloseTag('div'));
795    }
796    if ($hasNext) {
797        $form->addElement(form_makeOpenTag('div', array('class' => 'pagenav-next')));
798        $form->addElement(form_makeTag('input', array(
799                        'type'  => 'submit',
800                        'name'  => 'first['.$last.']',
801                        'value' => $lang['btn_older'],
802                        'accesskey' => 'p',
803                        'title' => $lang['btn_older'].' [P]',
804                        'class' => 'button show'
805                        )));
806        $form->addElement(form_makeCloseTag('div'));
807    }
808    $form->addElement(form_makeCloseTag('div'));
809    html_form('recent', $form);
810}
811
812/**
813 * Display page index
814 *
815 * @author Andreas Gohr <andi@splitbrain.org>
816 */
817function html_index($ns){
818    global $conf;
819    global $ID;
820    $dir = $conf['datadir'];
821    $ns  = cleanID($ns);
822    #fixme use appropriate function
823    if(empty($ns)){
824        $ns = dirname(str_replace(':','/',$ID));
825        if($ns == '.') $ns ='';
826    }
827    $ns  = utf8_encodeFN(str_replace(':','/',$ns));
828
829    echo p_locale_xhtml('index');
830    echo '<div id="index__tree">';
831
832    $data = array();
833    search($data,$conf['datadir'],'search_index',array('ns' => $ns));
834    echo html_buildlist($data,'idx','html_list_index','html_li_index');
835
836    echo '</div>';
837}
838
839/**
840 * Index item formatter
841 *
842 * User function for html_buildlist()
843 *
844 * @author Andreas Gohr <andi@splitbrain.org>
845 */
846function html_list_index($item){
847    global $ID;
848    $ret = '';
849    $base = ':'.$item['id'];
850    $base = substr($base,strrpos($base,':')+1);
851    if($item['type']=='d'){
852        $ret .= '<a href="'.wl($ID,'idx='.rawurlencode($item['id'])).'" class="idx_dir"><strong>';
853        $ret .= $base;
854        $ret .= '</strong></a>';
855    }else{
856        $ret .= html_wikilink(':'.$item['id']);
857    }
858    return $ret;
859}
860
861/**
862 * Index List item
863 *
864 * This user function is used in html_buildlist to build the
865 * <li> tags for namespaces when displaying the page index
866 * it gives different classes to opened or closed "folders"
867 *
868 * @author Andreas Gohr <andi@splitbrain.org>
869 */
870function html_li_index($item){
871    if($item['type'] == "f"){
872        return '<li class="level'.$item['level'].'">';
873    }elseif($item['open']){
874        return '<li class="open">';
875    }else{
876        return '<li class="closed">';
877    }
878}
879
880/**
881 * Default List item
882 *
883 * @author Andreas Gohr <andi@splitbrain.org>
884 */
885function html_li_default($item){
886    return '<li class="level'.$item['level'].'">';
887}
888
889/**
890 * Build an unordered list
891 *
892 * Build an unordered list from the given $data array
893 * Each item in the array has to have a 'level' property
894 * the item itself gets printed by the given $func user
895 * function. The second and optional function is used to
896 * print the <li> tag. Both user function need to accept
897 * a single item.
898 *
899 * Both user functions can be given as array to point to
900 * a member of an object.
901 *
902 * @author Andreas Gohr <andi@splitbrain.org>
903 */
904function html_buildlist($data,$class,$func,$lifunc='html_li_default',$forcewrapper=false){
905    if (count($data) === 0) {
906        return '';
907    }
908
909    $start_level = $data[0]['level'];
910    $level = $start_level;
911    $ret   = '';
912    $open  = 0;
913
914    foreach ($data as $item){
915
916        if( $item['level'] > $level ){
917            //open new list
918            for($i=0; $i<($item['level'] - $level); $i++){
919                if ($i) $ret .= "<li class=\"clear\">";
920                $ret .= "\n<ul class=\"$class\">\n";
921                $open++;
922            }
923            $level = $item['level'];
924
925        }elseif( $item['level'] < $level ){
926            //close last item
927            $ret .= "</li>\n";
928            while( $level > $item['level'] && $open > 0 ){
929                //close higher lists
930                $ret .= "</ul>\n</li>\n";
931                $level--;
932                $open--;
933            }
934        } elseif ($ret !== '') {
935            //close previous item
936            $ret .= "</li>\n";
937        }
938
939        //print item
940        $ret .= call_user_func($lifunc,$item);
941        $ret .= '<div class="li">';
942
943        $ret .= call_user_func($func,$item);
944        $ret .= '</div>';
945    }
946
947    //close remaining items and lists
948    $ret .= "</li>\n";
949    while($open-- > 0) {
950        $ret .= "</ul></li>\n";
951    }
952
953    if ($forcewrapper || $start_level < 2) {
954        // Trigger building a wrapper ul if the first level is
955        // 0 (we have a root object) or 1 (just the root content)
956        $ret = "\n<ul class=\"$class\">\n".$ret."</ul>\n";
957    }
958
959    return $ret;
960}
961
962/**
963 * display backlinks
964 *
965 * @author Andreas Gohr <andi@splitbrain.org>
966 * @author Michael Klier <chi@chimeric.de>
967 */
968function html_backlinks(){
969    global $ID;
970    global $conf;
971    global $lang;
972
973    print p_locale_xhtml('backlinks');
974
975    $data = ft_backlinks($ID);
976
977    if(!empty($data)) {
978        print '<ul class="idx">';
979        foreach($data as $blink){
980            print '<li><div class="li">';
981            print html_wikilink(':'.$blink,useHeading('navigation')?null:$blink);
982            print '</div></li>';
983        }
984        print '</ul>';
985    } else {
986        print '<div class="level1"><p>' . $lang['nothingfound'] . '</p></div>';
987    }
988}
989
990function html_diff_head($l_rev, $r_rev, $id = null, $media = false) {
991    global $lang;
992    if ($id === null) {
993        global $ID;
994        $id = $ID;
995    }
996    $media_or_wikiFN = $media ? 'mediaFN' : 'wikiFN';
997    $ml_or_wl = $media ? 'ml' : 'wl';
998    $l_minor = $r_minor = '';
999
1000    if(!$l_rev){
1001        $l_head = '&mdash;';
1002    }else{
1003        $l_info   = getRevisionInfo($id,$l_rev,true, $media);
1004        if($l_info['user']){
1005            $l_user = editorinfo($l_info['user']);
1006            if(auth_ismanager()) $l_user .= ' ('.$l_info['ip'].')';
1007        } else {
1008            $l_user = $l_info['ip'];
1009        }
1010        $l_user  = '<span class="user">'.$l_user.'</span>';
1011        $l_sum   = ($l_info['sum']) ? '<span class="sum">'.hsc($l_info['sum']).'</span>' : '';
1012        if ($l_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $l_minor = 'class="minor"';
1013
1014        $l_head_title = ($media) ? dformat($l_rev) : $id.' ['.dformat($l_rev).']';
1015        $l_head = '<a class="wikilink1" href="'.$ml_or_wl($id,"rev=$l_rev").'">'.
1016        $l_head_title.'</a>'.
1017        '<br />'.$l_user.' '.$l_sum;
1018    }
1019
1020    if($r_rev){
1021        $r_info   = getRevisionInfo($id,$r_rev,true, $media);
1022        if($r_info['user']){
1023            $r_user = editorinfo($r_info['user']);
1024            if(auth_ismanager()) $r_user .= ' ('.$r_info['ip'].')';
1025        } else {
1026            $r_user = $r_info['ip'];
1027        }
1028        $r_user = '<span class="user">'.$r_user.'</span>';
1029        $r_sum  = ($r_info['sum']) ? '<span class="sum">'.hsc($r_info['sum']).'</span>' : '';
1030        if ($r_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $r_minor = 'class="minor"';
1031
1032        $r_head_title = ($media) ? dformat($r_rev) : $id.' ['.dformat($r_rev).']';
1033        $r_head = '<a class="wikilink1" href="'.$ml_or_wl($id,"rev=$r_rev").'">'.
1034        $r_head_title.'</a>'.
1035        '<br />'.$r_user.' '.$r_sum;
1036    }elseif($_rev = @filemtime($media_or_wikiFN($id))){
1037        $_info   = getRevisionInfo($id,$_rev,true, $media);
1038        if($_info['user']){
1039            $_user = editorinfo($_info['user']);
1040            if(auth_ismanager()) $_user .= ' ('.$_info['ip'].')';
1041        } else {
1042            $_user = $_info['ip'];
1043        }
1044        $_user = '<span class="user">'.$_user.'</span>';
1045        $_sum  = ($_info['sum']) ? '<span class="sum">'.hsc($_info['sum']).'</span>' : '';
1046        if ($_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $r_minor = 'class="minor"';
1047
1048        $r_head_title = ($media) ? dformat($_rev) : $id.' ['.dformat($_rev).']';
1049        $r_head  = '<a class="wikilink1" href="'.$ml_or_wl($id).'">'.
1050        $r_head_title.'</a> '.
1051        '('.$lang['current'].')'.
1052        '<br />'.$_user.' '.$_sum;
1053    }else{
1054        $r_head = '&mdash; ('.$lang['current'].')';
1055    }
1056
1057    return array($l_head, $r_head, $l_minor, $r_minor);
1058}
1059
1060/**
1061 * show diff
1062 *
1063 * @author Andreas Gohr <andi@splitbrain.org>
1064 * @param  string $text - compare with this text with most current version
1065 * @param  bool   $intr - display the intro text
1066 */
1067function html_diff($text='',$intro=true,$type=null){
1068    global $ID;
1069    global $REV;
1070    global $lang;
1071    global $conf;
1072
1073    if(!$type) $type = $_REQUEST['difftype'];
1074    if($type != 'inline') $type = 'sidebyside';
1075
1076    // we're trying to be clever here, revisions to compare can be either
1077    // given as rev and rev2 parameters, with rev2 being optional. Or in an
1078    // array in rev2.
1079    $rev1 = $REV;
1080
1081    if(is_array($_REQUEST['rev2'])){
1082        $rev1 = (int) $_REQUEST['rev2'][0];
1083        $rev2 = (int) $_REQUEST['rev2'][1];
1084
1085        if(!$rev1){
1086            $rev1 = $rev2;
1087            unset($rev2);
1088        }
1089    }else{
1090        $rev2 = (int) $_REQUEST['rev2'];
1091    }
1092
1093    $r_minor = '';
1094    $l_minor = '';
1095
1096    if($text){                      // compare text to the most current revision
1097        $l_rev   = '';
1098        $l_text  = rawWiki($ID,'');
1099        $l_head  = '<a class="wikilink1" href="'.wl($ID).'">'.
1100            $ID.' '.dformat((int) @filemtime(wikiFN($ID))).'</a> '.
1101            $lang['current'];
1102
1103        $r_rev   = '';
1104        $r_text  = cleanText($text);
1105        $r_head  = $lang['yours'];
1106    }else{
1107        if($rev1 && $rev2){            // two specific revisions wanted
1108            // make sure order is correct (older on the left)
1109            if($rev1 < $rev2){
1110                $l_rev = $rev1;
1111                $r_rev = $rev2;
1112            }else{
1113                $l_rev = $rev2;
1114                $r_rev = $rev1;
1115            }
1116        }elseif($rev1){                // single revision given, compare to current
1117            $r_rev = '';
1118            $l_rev = $rev1;
1119        }else{                        // no revision was given, compare previous to current
1120            $r_rev = '';
1121            $revs = getRevisions($ID, 0, 1);
1122            $l_rev = $revs[0];
1123            $REV = $l_rev; // store revision back in $REV
1124        }
1125
1126        // when both revisions are empty then the page was created just now
1127        if(!$l_rev && !$r_rev){
1128            $l_text = '';
1129        }else{
1130            $l_text = rawWiki($ID,$l_rev);
1131        }
1132        $r_text = rawWiki($ID,$r_rev);
1133
1134        list($l_head, $r_head, $l_minor, $r_minor) = html_diff_head($l_rev, $r_rev);
1135    }
1136
1137    $df = new Diff(explode("\n",htmlspecialchars($l_text)),
1138        explode("\n",htmlspecialchars($r_text)));
1139
1140    if($type == 'inline'){
1141        $tdf = new InlineDiffFormatter();
1142    } else {
1143        $tdf = new TableDiffFormatter();
1144    }
1145
1146    if($intro) print p_locale_xhtml('diff');
1147
1148    if (!$text) {
1149        ptln('<div class="diffoptions">');
1150
1151        $form = new Doku_Form(array('action'=>wl()));
1152        $form->addHidden('id',$ID);
1153        $form->addHidden('rev2[0]',$l_rev);
1154        $form->addHidden('rev2[1]',$r_rev);
1155        $form->addHidden('do','diff');
1156        $form->addElement(form_makeListboxField(
1157                            'difftype',
1158                            array(
1159                                'sidebyside' => $lang['diff_side'],
1160                                'inline'     => $lang['diff_inline']),
1161                            $type,
1162                            $lang['diff_type'],
1163                            '','',
1164                            array('class'=>'quickselect')));
1165        $form->addElement(form_makeButton('submit', 'diff','Go'));
1166        $form->printForm();
1167
1168        $diffurl = wl($ID, array(
1169                        'do'       => 'diff',
1170                        'rev2[0]'  => $l_rev,
1171                        'rev2[1]'  => $r_rev,
1172                        'difftype' => $type,
1173                      ));
1174        ptln('<p><a class="wikilink1" href="'.$diffurl.'">'.$lang['difflink'].'</a></p>');
1175        ptln('</div>');
1176    }
1177    ?>
1178    <div class="table">
1179    <table class="diff diff_<?php echo $type?>">
1180    <tr>
1181    <th colspan="2" <?php echo $l_minor?>>
1182    <?php echo $l_head?>
1183    </th>
1184    <th colspan="2" <?php echo $r_minor?>>
1185    <?php echo $r_head?>
1186    </th>
1187    </tr>
1188    <?php echo $tdf->format($df)?>
1189    </table>
1190    </div>
1191    <?php
1192}
1193
1194/**
1195 * show warning on conflict detection
1196 *
1197 * @author Andreas Gohr <andi@splitbrain.org>
1198 */
1199function html_conflict($text,$summary){
1200    global $ID;
1201    global $lang;
1202
1203    print p_locale_xhtml('conflict');
1204    $form = new Doku_Form(array('id' => 'dw__editform'));
1205    $form->addHidden('id', $ID);
1206    $form->addHidden('wikitext', $text);
1207    $form->addHidden('summary', $summary);
1208    $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('accesskey'=>'s')));
1209    $form->addElement(form_makeButton('submit', 'cancel', $lang['btn_cancel']));
1210    html_form('conflict', $form);
1211    print '<br /><br /><br /><br />'.NL;
1212}
1213
1214/**
1215 * Prints the global message array
1216 *
1217 * @author Andreas Gohr <andi@splitbrain.org>
1218 */
1219function html_msgarea(){
1220    global $MSG, $MSG_shown;
1221    // store if the global $MSG has already been shown and thus HTML output has been started
1222    $MSG_shown = true;
1223
1224    if(!isset($MSG)) return;
1225
1226    $shown = array();
1227    foreach($MSG as $msg){
1228        $hash = md5($msg['msg']);
1229        if(isset($shown[$hash])) continue; // skip double messages
1230        print '<div class="'.$msg['lvl'].'">';
1231        print $msg['msg'];
1232        print '</div>';
1233        $shown[$hash] = 1;
1234    }
1235
1236    unset($GLOBALS['MSG']);
1237}
1238
1239/**
1240 * Prints the registration form
1241 *
1242 * @author Andreas Gohr <andi@splitbrain.org>
1243 */
1244function html_register(){
1245    global $lang;
1246    global $conf;
1247    global $ID;
1248
1249    print p_locale_xhtml('register');
1250    print '<div class="centeralign">'.NL;
1251    $form = new Doku_Form(array('id' => 'dw__register'));
1252    $form->startFieldset($lang['btn_register']);
1253    $form->addHidden('do', 'register');
1254    $form->addHidden('save', '1');
1255    $form->addElement(form_makeTextField('login', $_POST['login'], $lang['user'], '', 'block', array('size'=>'50')));
1256    if (!$conf['autopasswd']) {
1257        $form->addElement(form_makePasswordField('pass', $lang['pass'], '', 'block', array('size'=>'50')));
1258        $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
1259    }
1260    $form->addElement(form_makeTextField('fullname', $_POST['fullname'], $lang['fullname'], '', 'block', array('size'=>'50')));
1261    $form->addElement(form_makeTextField('email', $_POST['email'], $lang['email'], '', 'block', array('size'=>'50')));
1262    $form->addElement(form_makeButton('submit', '', $lang['btn_register']));
1263    $form->endFieldset();
1264    html_form('register', $form);
1265
1266    print '</div>'.NL;
1267}
1268
1269/**
1270 * Print the update profile form
1271 *
1272 * @author Christopher Smith <chris@jalakai.co.uk>
1273 * @author Andreas Gohr <andi@splitbrain.org>
1274 */
1275function html_updateprofile(){
1276    global $lang;
1277    global $conf;
1278    global $ID;
1279    global $INFO;
1280    global $auth;
1281
1282    print p_locale_xhtml('updateprofile');
1283
1284    if (empty($_POST['fullname'])) $_POST['fullname'] = $INFO['userinfo']['name'];
1285    if (empty($_POST['email'])) $_POST['email'] = $INFO['userinfo']['mail'];
1286    print '<div class="centeralign">'.NL;
1287    $form = new Doku_Form(array('id' => 'dw__register'));
1288    $form->startFieldset($lang['profile']);
1289    $form->addHidden('do', 'profile');
1290    $form->addHidden('save', '1');
1291    $form->addElement(form_makeTextField('fullname', $_SERVER['REMOTE_USER'], $lang['user'], '', 'block', array('size'=>'50', 'disabled'=>'disabled')));
1292    $attr = array('size'=>'50');
1293    if (!$auth->canDo('modName')) $attr['disabled'] = 'disabled';
1294    $form->addElement(form_makeTextField('fullname', $_POST['fullname'], $lang['fullname'], '', 'block', $attr));
1295    $attr = array('size'=>'50');
1296    if (!$auth->canDo('modMail')) $attr['disabled'] = 'disabled';
1297    $form->addElement(form_makeTextField('email', $_POST['email'], $lang['email'], '', 'block', $attr));
1298    $form->addElement(form_makeTag('br'));
1299    if ($auth->canDo('modPass')) {
1300        $form->addElement(form_makePasswordField('newpass', $lang['newpass'], '', 'block', array('size'=>'50')));
1301        $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
1302    }
1303    if ($conf['profileconfirm']) {
1304        $form->addElement(form_makeTag('br'));
1305        $form->addElement(form_makePasswordField('oldpass', $lang['oldpass'], '', 'block', array('size'=>'50')));
1306    }
1307    $form->addElement(form_makeButton('submit', '', $lang['btn_save']));
1308    $form->addElement(form_makeButton('reset', '', $lang['btn_reset']));
1309    $form->endFieldset();
1310    html_form('updateprofile', $form);
1311    print '</div>'.NL;
1312}
1313
1314/**
1315 * Preprocess edit form data
1316 *
1317 * @author   Andreas Gohr <andi@splitbrain.org>
1318 *
1319 * @triggers HTML_EDITFORM_OUTPUT
1320 */
1321function html_edit(){
1322    global $ID;
1323    global $REV;
1324    global $DATE;
1325    global $PRE;
1326    global $SUF;
1327    global $INFO;
1328    global $SUM;
1329    global $lang;
1330    global $conf;
1331    global $TEXT;
1332    global $RANGE;
1333
1334    if (isset($_REQUEST['changecheck'])) {
1335        $check = $_REQUEST['changecheck'];
1336    } elseif(!$INFO['exists']){
1337        // $TEXT has been loaded from page template
1338        $check = md5('');
1339    } else {
1340        $check = md5($TEXT);
1341    }
1342    $mod = md5($TEXT) !== $check;
1343
1344    $wr = $INFO['writable'] && !$INFO['locked'];
1345    $include = 'edit';
1346    if($wr){
1347        if ($REV) $include = 'editrev';
1348    }else{
1349        // check pseudo action 'source'
1350        if(!actionOK('source')){
1351            msg('Command disabled: source',-1);
1352            return;
1353        }
1354        $include = 'read';
1355    }
1356
1357    global $license;
1358
1359    $form = new Doku_Form(array('id' => 'dw__editform'));
1360    $form->addHidden('id', $ID);
1361    $form->addHidden('rev', $REV);
1362    $form->addHidden('date', $DATE);
1363    $form->addHidden('prefix', $PRE . '.');
1364    $form->addHidden('suffix', $SUF);
1365    $form->addHidden('changecheck', $check);
1366
1367    $data = array('form' => $form,
1368                  'wr'   => $wr,
1369                  'media_manager' => true,
1370                  'target' => (isset($_REQUEST['target']) && $wr &&
1371                               $RANGE !== '') ? $_REQUEST['target'] : 'section',
1372                  'intro_locale' => $include);
1373
1374    if ($data['target'] !== 'section') {
1375        // Only emit event if page is writable, section edit data is valid and
1376        // edit target is not section.
1377        trigger_event('HTML_EDIT_FORMSELECTION', $data, 'html_edit_form', true);
1378    } else {
1379        html_edit_form($data);
1380    }
1381    if (isset($data['intro_locale'])) {
1382        echo p_locale_xhtml($data['intro_locale']);
1383    }
1384
1385    $form->addHidden('target', $data['target']);
1386    $form->addElement(form_makeOpenTag('div', array('id'=>'wiki__editbar', 'class'=>'editBar')));
1387    $form->addElement(form_makeOpenTag('div', array('id'=>'size__ctl')));
1388    $form->addElement(form_makeCloseTag('div'));
1389    if ($wr) {
1390        $form->addElement(form_makeOpenTag('div', array('class'=>'editButtons')));
1391        $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('id'=>'edbtn__save', 'accesskey'=>'s', 'tabindex'=>'4')));
1392        $form->addElement(form_makeButton('submit', 'preview', $lang['btn_preview'], array('id'=>'edbtn__preview', 'accesskey'=>'p', 'tabindex'=>'5')));
1393        $form->addElement(form_makeButton('submit', 'draftdel', $lang['btn_cancel'], array('tabindex'=>'6')));
1394        $form->addElement(form_makeCloseTag('div'));
1395        $form->addElement(form_makeOpenTag('div', array('class'=>'summary')));
1396        $form->addElement(form_makeTextField('summary', $SUM, $lang['summary'], 'edit__summary', 'nowrap', array('size'=>'50', 'tabindex'=>'2')));
1397        $elem = html_minoredit();
1398        if ($elem) $form->addElement($elem);
1399        $form->addElement(form_makeCloseTag('div'));
1400    }
1401    $form->addElement(form_makeCloseTag('div'));
1402    if($wr && $conf['license']){
1403        $form->addElement(form_makeOpenTag('div', array('class'=>'license')));
1404        $out  = $lang['licenseok'];
1405        $out .= ' <a href="'.$license[$conf['license']]['url'].'" rel="license" class="urlextern"';
1406        if($conf['target']['extern']) $out .= ' target="'.$conf['target']['extern'].'"';
1407        $out .= '>'.$license[$conf['license']]['name'].'</a>';
1408        $form->addElement($out);
1409        $form->addElement(form_makeCloseTag('div'));
1410    }
1411
1412    if ($wr) {
1413        // sets changed to true when previewed
1414        echo '<script type="text/javascript" charset="utf-8"><!--//--><![CDATA[//><!--'. NL;
1415        echo 'textChanged = ' . ($mod ? 'true' : 'false');
1416        echo '//--><!]]></script>' . NL;
1417    } ?>
1418    <div class="editBox">
1419
1420    <div class="toolbar">
1421        <div id="draft__status"><?php if(!empty($INFO['draft'])) echo $lang['draftdate'].' '.dformat();?></div>
1422        <div id="tool__bar"><?php if ($wr && $data['media_manager']){?><a href="<?php echo DOKU_BASE?>lib/exe/mediamanager.php?ns=<?php echo $INFO['namespace']?>"
1423            target="_blank"><?php echo $lang['mediaselect'] ?></a><?php }?></div>
1424    </div>
1425    <?php
1426
1427    html_form('edit', $form);
1428    print '</div>'.NL;
1429}
1430
1431/**
1432 * Display the default edit form
1433 *
1434 * Is the default action for HTML_EDIT_FORMSELECTION.
1435 */
1436function html_edit_form($param) {
1437    global $TEXT;
1438
1439    if ($param['target'] !== 'section') {
1440        msg('No editor for edit target ' . $param['target'] . ' found.', -1);
1441    }
1442
1443    $attr = array('tabindex'=>'1');
1444    if (!$param['wr']) $attr['readonly'] = 'readonly';
1445
1446    $param['form']->addElement(form_makeWikiText($TEXT, $attr));
1447}
1448
1449/**
1450 * Adds a checkbox for minor edits for logged in users
1451 *
1452 * @author Andreas Gohr <andi@splitbrain.org>
1453 */
1454function html_minoredit(){
1455    global $conf;
1456    global $lang;
1457    // minor edits are for logged in users only
1458    if(!$conf['useacl'] || !$_SERVER['REMOTE_USER']){
1459        return false;
1460    }
1461
1462    $p = array();
1463    $p['tabindex'] = 3;
1464    if(!empty($_REQUEST['minor'])) $p['checked']='checked';
1465    return form_makeCheckboxField('minor', '1', $lang['minoredit'], 'minoredit', 'nowrap', $p);
1466}
1467
1468/**
1469 * prints some debug info
1470 *
1471 * @author Andreas Gohr <andi@splitbrain.org>
1472 */
1473function html_debug(){
1474    global $conf;
1475    global $lang;
1476    global $auth;
1477    global $INFO;
1478
1479    //remove sensitive data
1480    $cnf = $conf;
1481    debug_guard($cnf);
1482    $nfo = $INFO;
1483    debug_guard($nfo);
1484    $ses = $_SESSION;
1485    debug_guard($ses);
1486
1487    print '<html><body>';
1488
1489    print '<p>When reporting bugs please send all the following ';
1490    print 'output as a mail to andi@splitbrain.org ';
1491    print 'The best way to do this is to save this page in your browser</p>';
1492
1493    print '<b>$INFO:</b><pre>';
1494    print_r($nfo);
1495    print '</pre>';
1496
1497    print '<b>$_SERVER:</b><pre>';
1498    print_r($_SERVER);
1499    print '</pre>';
1500
1501    print '<b>$conf:</b><pre>';
1502    print_r($cnf);
1503    print '</pre>';
1504
1505    print '<b>DOKU_BASE:</b><pre>';
1506    print DOKU_BASE;
1507    print '</pre>';
1508
1509    print '<b>abs DOKU_BASE:</b><pre>';
1510    print DOKU_URL;
1511    print '</pre>';
1512
1513    print '<b>rel DOKU_BASE:</b><pre>';
1514    print dirname($_SERVER['PHP_SELF']).'/';
1515    print '</pre>';
1516
1517    print '<b>PHP Version:</b><pre>';
1518    print phpversion();
1519    print '</pre>';
1520
1521    print '<b>locale:</b><pre>';
1522    print setlocale(LC_ALL,0);
1523    print '</pre>';
1524
1525    print '<b>encoding:</b><pre>';
1526    print $lang['encoding'];
1527    print '</pre>';
1528
1529    if($auth){
1530        print '<b>Auth backend capabilities:</b><pre>';
1531        print_r($auth->cando);
1532        print '</pre>';
1533    }
1534
1535    print '<b>$_SESSION:</b><pre>';
1536    print_r($ses);
1537    print '</pre>';
1538
1539    print '<b>Environment:</b><pre>';
1540    print_r($_ENV);
1541    print '</pre>';
1542
1543    print '<b>PHP settings:</b><pre>';
1544    $inis = ini_get_all();
1545    print_r($inis);
1546    print '</pre>';
1547
1548    print '</body></html>';
1549}
1550
1551/**
1552 * List available Administration Tasks
1553 *
1554 * @author Andreas Gohr <andi@splitbrain.org>
1555 * @author Håkan Sandell <hakan.sandell@home.se>
1556 */
1557function html_admin(){
1558    global $ID;
1559    global $INFO;
1560    global $lang;
1561    global $conf;
1562    global $auth;
1563
1564    // build menu of admin functions from the plugins that handle them
1565    $pluginlist = plugin_list('admin');
1566    $menu = array();
1567    foreach ($pluginlist as $p) {
1568        if($obj =& plugin_load('admin',$p) === null) continue;
1569
1570        // check permissions
1571        if($obj->forAdminOnly() && !$INFO['isadmin']) continue;
1572
1573        $menu[$p] = array('plugin' => $p,
1574                'prompt' => $obj->getMenuText($conf['lang']),
1575                'sort' => $obj->getMenuSort()
1576                );
1577    }
1578
1579    // data security check
1580    // @todo: could be checked and only displayed if $conf['savedir'] is under the web root
1581    echo '<a style="border:none; float:right;"
1582            href="http://www.dokuwiki.org/security#web_access_security">
1583            <img src="data/security.png" alt="Your data directory seems to be protected properly."
1584             onerror="this.parentNode.style.display=\'none\'" /></a>';
1585
1586    print p_locale_xhtml('admin');
1587
1588    // Admin Tasks
1589    if($INFO['isadmin']){
1590        ptln('<ul class="admin_tasks">');
1591
1592        if($menu['usermanager'] && $auth && $auth->canDo('getUsers')){
1593            ptln('  <li class="admin_usermanager"><div class="li">'.
1594                    '<a href="'.wl($ID, array('do' => 'admin','page' => 'usermanager')).'">'.
1595                    $menu['usermanager']['prompt'].'</a></div></li>');
1596        }
1597        unset($menu['usermanager']);
1598
1599        if($menu['acl']){
1600            ptln('  <li class="admin_acl"><div class="li">'.
1601                    '<a href="'.wl($ID, array('do' => 'admin','page' => 'acl')).'">'.
1602                    $menu['acl']['prompt'].'</a></div></li>');
1603        }
1604        unset($menu['acl']);
1605
1606        if($menu['plugin']){
1607            ptln('  <li class="admin_plugin"><div class="li">'.
1608                    '<a href="'.wl($ID, array('do' => 'admin','page' => 'plugin')).'">'.
1609                    $menu['plugin']['prompt'].'</a></div></li>');
1610        }
1611        unset($menu['plugin']);
1612
1613        if($menu['config']){
1614            ptln('  <li class="admin_config"><div class="li">'.
1615                    '<a href="'.wl($ID, array('do' => 'admin','page' => 'config')).'">'.
1616                    $menu['config']['prompt'].'</a></div></li>');
1617        }
1618        unset($menu['config']);
1619    }
1620    ptln('</ul>');
1621
1622    // Manager Tasks
1623    ptln('<ul class="admin_tasks">');
1624
1625    if($menu['revert']){
1626        ptln('  <li class="admin_revert"><div class="li">'.
1627                '<a href="'.wl($ID, array('do' => 'admin','page' => 'revert')).'">'.
1628                $menu['revert']['prompt'].'</a></div></li>');
1629    }
1630    unset($menu['revert']);
1631
1632    if($menu['popularity']){
1633        ptln('  <li class="admin_popularity"><div class="li">'.
1634                '<a href="'.wl($ID, array('do' => 'admin','page' => 'popularity')).'">'.
1635                $menu['popularity']['prompt'].'</a></div></li>');
1636    }
1637    unset($menu['popularity']);
1638
1639    // print DokuWiki version:
1640    ptln('</ul>');
1641    echo '<div id="admin__version">';
1642    echo getVersion();
1643    echo '</div>';
1644
1645    // print the rest as sorted list
1646    if(count($menu)){
1647        usort($menu, 'p_sort_modes');
1648        // output the menu
1649        ptln('<div class="clearer"></div>');
1650        print p_locale_xhtml('adminplugins');
1651        ptln('<ul>');
1652        foreach ($menu as $item) {
1653            if (!$item['prompt']) continue;
1654            ptln('  <li><div class="li"><a href="'.wl($ID, 'do=admin&amp;page='.$item['plugin']).'">'.$item['prompt'].'</a></div></li>');
1655        }
1656        ptln('</ul>');
1657    }
1658}
1659
1660/**
1661 * Form to request a new password for an existing account
1662 *
1663 * @author Benoit Chesneau <benoit@bchesneau.info>
1664 * @author Andreas Gohr <gohr@cosmocode.de>
1665 */
1666function html_resendpwd() {
1667    global $lang;
1668    global $conf;
1669    global $ID;
1670
1671    $token = preg_replace('/[^a-f0-9]+/','',$_REQUEST['pwauth']);
1672
1673    if(!$conf['autopasswd'] && $token){
1674        print p_locale_xhtml('resetpwd');
1675        print '<div class="centeralign">'.NL;
1676        $form = new Doku_Form(array('id' => 'dw__resendpwd'));
1677        $form->startFieldset($lang['btn_resendpwd']);
1678        $form->addHidden('token', $token);
1679        $form->addHidden('do', 'resendpwd');
1680
1681        $form->addElement(form_makePasswordField('pass', $lang['pass'], '', 'block', array('size'=>'50')));
1682        $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
1683
1684        $form->addElement(form_makeButton('submit', '', $lang['btn_resendpwd']));
1685        $form->endFieldset();
1686        html_form('resendpwd', $form);
1687        print '</div>'.NL;
1688    }else{
1689        print p_locale_xhtml('resendpwd');
1690        print '<div class="centeralign">'.NL;
1691        $form = new Doku_Form(array('id' => 'dw__resendpwd'));
1692        $form->startFieldset($lang['resendpwd']);
1693        $form->addHidden('do', 'resendpwd');
1694        $form->addHidden('save', '1');
1695        $form->addElement(form_makeTag('br'));
1696        $form->addElement(form_makeTextField('login', $_POST['login'], $lang['user'], '', 'block'));
1697        $form->addElement(form_makeTag('br'));
1698        $form->addElement(form_makeTag('br'));
1699        $form->addElement(form_makeButton('submit', '', $lang['btn_resendpwd']));
1700        $form->endFieldset();
1701        html_form('resendpwd', $form);
1702        print '</div>'.NL;
1703    }
1704}
1705
1706/**
1707 * Return the TOC rendered to XHTML
1708 *
1709 * @author Andreas Gohr <andi@splitbrain.org>
1710 */
1711function html_TOC($toc){
1712    if(!count($toc)) return '';
1713    global $lang;
1714    $out  = '<!-- TOC START -->'.DOKU_LF;
1715    $out .= '<div id="dw__toc">'.DOKU_LF;
1716    $out .= '<h3>';
1717    $out .= $lang['toc'];
1718    $out .= '</h3>'.DOKU_LF;
1719    $out .= '<div>'.DOKU_LF;
1720    $out .= html_buildlist($toc,'toc','html_list_toc','html_li_default',true);
1721    $out .= '</div>'.DOKU_LF.'</div>'.DOKU_LF;
1722    $out .= '<!-- TOC END -->'.DOKU_LF;
1723    return $out;
1724}
1725
1726/**
1727 * Callback for html_buildlist
1728 */
1729function html_list_toc($item){
1730    if(isset($item['hid'])){
1731        $link = '#'.$item['hid'];
1732    }else{
1733        $link = $item['link'];
1734    }
1735
1736    return '<a href="'.$link.'">'.hsc($item['title']).'</a>';
1737}
1738
1739/**
1740 * Helper function to build TOC items
1741 *
1742 * Returns an array ready to be added to a TOC array
1743 *
1744 * @param string $link  - where to link (if $hash set to '#' it's a local anchor)
1745 * @param string $text  - what to display in the TOC
1746 * @param int    $level - nesting level
1747 * @param string $hash  - is prepended to the given $link, set blank if you want full links
1748 */
1749function html_mktocitem($link, $text, $level, $hash='#'){
1750    global $conf;
1751    return  array( 'link'  => $hash.$link,
1752            'title' => $text,
1753            'type'  => 'ul',
1754            'level' => $level);
1755}
1756
1757/**
1758 * Output a Doku_Form object.
1759 * Triggers an event with the form name: HTML_{$name}FORM_OUTPUT
1760 *
1761 * @author Tom N Harris <tnharris@whoopdedo.org>
1762 */
1763function html_form($name, &$form) {
1764    // Safety check in case the caller forgets.
1765    $form->endFieldset();
1766    trigger_event('HTML_'.strtoupper($name).'FORM_OUTPUT', $form, 'html_form_output', false);
1767}
1768
1769/**
1770 * Form print function.
1771 * Just calls printForm() on the data object.
1772 */
1773function html_form_output($data) {
1774    $data->printForm();
1775}
1776
1777/**
1778 * Embed a flash object in HTML
1779 *
1780 * This will create the needed HTML to embed a flash movie in a cross browser
1781 * compatble way using valid XHTML
1782 *
1783 * The parameters $params, $flashvars and $atts need to be associative arrays.
1784 * No escaping needs to be done for them. The alternative content *has* to be
1785 * escaped because it is used as is. If no alternative content is given
1786 * $lang['noflash'] is used.
1787 *
1788 * @author Andreas Gohr <andi@splitbrain.org>
1789 * @link   http://latrine.dgx.cz/how-to-correctly-insert-a-flash-into-xhtml
1790 *
1791 * @param string $swf      - the SWF movie to embed
1792 * @param int $width       - width of the flash movie in pixels
1793 * @param int $height      - height of the flash movie in pixels
1794 * @param array $params    - additional parameters (<param>)
1795 * @param array $flashvars - parameters to be passed in the flashvar parameter
1796 * @param array $atts      - additional attributes for the <object> tag
1797 * @param string $alt      - alternative content (is NOT automatically escaped!)
1798 * @returns string         - the XHTML markup
1799 */
1800function html_flashobject($swf,$width,$height,$params=null,$flashvars=null,$atts=null,$alt=''){
1801    global $lang;
1802
1803    $out = '';
1804
1805    // prepare the object attributes
1806    if(is_null($atts)) $atts = array();
1807    $atts['width']  = (int) $width;
1808    $atts['height'] = (int) $height;
1809    if(!$atts['width'])  $atts['width']  = 425;
1810    if(!$atts['height']) $atts['height'] = 350;
1811
1812    // add object attributes for standard compliant browsers
1813    $std = $atts;
1814    $std['type'] = 'application/x-shockwave-flash';
1815    $std['data'] = $swf;
1816
1817    // add object attributes for IE
1818    $ie  = $atts;
1819    $ie['classid'] = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
1820
1821    // open object (with conditional comments)
1822    $out .= '<!--[if !IE]> -->'.NL;
1823    $out .= '<object '.buildAttributes($std).'>'.NL;
1824    $out .= '<!-- <![endif]-->'.NL;
1825    $out .= '<!--[if IE]>'.NL;
1826    $out .= '<object '.buildAttributes($ie).'>'.NL;
1827    $out .= '    <param name="movie" value="'.hsc($swf).'" />'.NL;
1828    $out .= '<!--><!-- -->'.NL;
1829
1830    // print params
1831    if(is_array($params)) foreach($params as $key => $val){
1832        $out .= '  <param name="'.hsc($key).'" value="'.hsc($val).'" />'.NL;
1833    }
1834
1835    // add flashvars
1836    if(is_array($flashvars)){
1837        $out .= '  <param name="FlashVars" value="'.buildURLparams($flashvars).'" />'.NL;
1838    }
1839
1840    // alternative content
1841    if($alt){
1842        $out .= $alt.NL;
1843    }else{
1844        $out .= $lang['noflash'].NL;
1845    }
1846
1847    // finish
1848    $out .= '</object>'.NL;
1849    $out .= '<!-- <![endif]-->'.NL;
1850
1851    return $out;
1852}
1853
1854function html_tabs($tabs, $current_tab = null) {
1855    echo '<ul class="tabs">'.NL;
1856
1857    foreach($tabs as $id => $tab) {
1858        html_tab($tab['href'], $tab['caption'], $id === $current_tab);
1859    }
1860
1861    echo '</ul>'.NL;
1862}
1863/**
1864 * Prints a single tab
1865 *
1866 * @author Kate Arzamastseva <pshns@ukr.net>
1867 * @author Adrian Lang <mail@adrianlang.de>
1868 *
1869 * @param string $href - tab href
1870 * @param string $caption - tab caption
1871 * @param boolean $selected - is tab selected
1872 */
1873
1874function html_tab($href, $caption, $selected=false) {
1875    $tab = '<li>';
1876    if ($selected) {
1877        $tab .= '<strong>';
1878    } else {
1879        $tab .= '<a href="' . hsc($href) . '">';
1880    }
1881    $tab .= hsc($caption)
1882         .  '</' . ($selected ? 'strong' : 'a') . '>'
1883         .  '</li>'.NL;
1884    echo $tab;
1885}
1886
1887