xref: /dokuwiki/inc/html.php (revision c62a5f5846e3477044ebc93e96f98b878d3e7a59)
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');
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'));
668    $form->addHidden('sectok', null);
669    $form->addHidden('do', 'recent');
670    $form->addHidden('id', $ID);
671
672    if ($conf['mediarevisions']) {
673        $form->addElement(form_makeListboxField(
674                    'show_changes',
675                    array(
676                        'pages'      => $lang['pages_changes'],
677                        'mediafiles' => $lang['media_changes'],
678                        'both'       => $lang['both_changes']),
679                    $show_changes,
680                    $lang['changes_type'],
681                    '','',
682                    array('class'=>'quickselect')));
683
684        $form->addElement(form_makeButton('submit', 'recent', $lang['btn_apply']));
685    }
686
687    $form->addElement(form_makeOpenTag('ul'));
688
689    foreach($recents as $recent){
690        $date = dformat($recent['date']);
691        if ($recent['type']===DOKU_CHANGE_TYPE_MINOR_EDIT)
692            $form->addElement(form_makeOpenTag('li', array('class' => 'minor')));
693        else
694            $form->addElement(form_makeOpenTag('li'));
695
696        $form->addElement(form_makeOpenTag('div', array('class' => 'li')));
697
698        if ($recent['media']) {
699            $form->addElement(media_printicon($recent['id']));
700        } else {
701            $icon = DOKU_BASE.'lib/images/fileicons/file.png';
702            $form->addElement('<img src="'.$icon.'" alt="'.$filename.'" class="icon" />');
703        }
704
705        $form->addElement(form_makeOpenTag('span', array('class' => 'date')));
706        $form->addElement($date);
707        $form->addElement(form_makeCloseTag('span'));
708
709        if ($recent['media']) {
710            $diff = (count(getRevisions($recent['id'], 0, 1, 8192, true)) && @file_exists(mediaFN($recent['id'])));
711            if ($diff) {
712                $href = media_managerURL(array('tab_details' => 'history',
713                    'mediado' => 'diff', 'image' => $recent['id'], 'ns' => getNS($recent['id'])), '&');
714            }
715        } else {
716            $href = wl($recent['id'],"do=diff", false, '&');
717        }
718
719        if ($recent['media'] && !$diff) {
720            $form->addElement('<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />');
721        } else {
722            $form->addElement(form_makeOpenTag('a', array('class' => 'diff_link', 'href' => $href)));
723            $form->addElement(form_makeTag('img', array(
724                            'src'   => DOKU_BASE.'lib/images/diff.png',
725                            'width' => 15,
726                            'height'=> 11,
727                            'title' => $lang['diff'],
728                            'alt'   => $lang['diff']
729                            )));
730            $form->addElement(form_makeCloseTag('a'));
731        }
732
733        if ($recent['media']) {
734            $href = media_managerURL(array('tab_details' => 'history',
735                'image' => $recent['id'], 'ns' => getNS($recent['id'])), '&');
736        } else {
737            $href = wl($recent['id'],"do=revisions",false,'&');
738        }
739        $form->addElement(form_makeOpenTag('a', array('class' => 'revisions_link', 'href' => $href)));
740        $form->addElement(form_makeTag('img', array(
741                        'src'   => DOKU_BASE.'lib/images/history.png',
742                        'width' => 12,
743                        'height'=> 14,
744                        'title' => $lang['btn_revs'],
745                        'alt'   => $lang['btn_revs']
746                        )));
747        $form->addElement(form_makeCloseTag('a'));
748
749        if ($recent['media']) {
750            $href = media_managerURL(array('tab_details' => 'view', 'image' => $recent['id'], 'ns' => getNS($recent['id'])), '&');
751            $class = (file_exists(mediaFN($recent['id']))) ? 'wikilink1' : $class = 'wikilink2';
752            $form->addElement(form_makeOpenTag('a', array('class' => $class, 'href' => $href)));
753            $form->addElement($recent['id']);
754            $form->addElement(form_makeCloseTag('a'));
755        } else {
756            $form->addElement(html_wikilink(':'.$recent['id'],useHeading('navigation')?null:$recent['id']));
757        }
758        $form->addElement(form_makeOpenTag('span', array('class' => 'sum')));
759        $form->addElement(' &ndash; '.htmlspecialchars($recent['sum']));
760        $form->addElement(form_makeCloseTag('span'));
761
762        $form->addElement(form_makeOpenTag('span', array('class' => 'user')));
763        if($recent['user']){
764            $form->addElement(editorinfo($recent['user']));
765            if(auth_ismanager()){
766                $form->addElement(' ('.$recent['ip'].')');
767            }
768        }else{
769            $form->addElement($recent['ip']);
770        }
771        $form->addElement(form_makeCloseTag('span'));
772
773        $form->addElement(form_makeCloseTag('div'));
774        $form->addElement(form_makeCloseTag('li'));
775    }
776    $form->addElement(form_makeCloseTag('ul'));
777
778    $form->addElement(form_makeOpenTag('div', array('class' => 'pagenav')));
779    $last = $first + $conf['recent'];
780    if ($first > 0) {
781        $first -= $conf['recent'];
782        if ($first < 0) $first = 0;
783        $form->addElement(form_makeOpenTag('div', array('class' => 'pagenav-prev')));
784        $form->addElement(form_makeTag('input', array(
785                    'type'  => 'submit',
786                    'name'  => 'first['.$first.']',
787                    'value' => $lang['btn_newer'],
788                    'accesskey' => 'n',
789                    'title' => $lang['btn_newer'].' [N]',
790                    'class' => 'button show'
791                    )));
792        $form->addElement(form_makeCloseTag('div'));
793    }
794    if ($hasNext) {
795        $form->addElement(form_makeOpenTag('div', array('class' => 'pagenav-next')));
796        $form->addElement(form_makeTag('input', array(
797                        'type'  => 'submit',
798                        'name'  => 'first['.$last.']',
799                        'value' => $lang['btn_older'],
800                        'accesskey' => 'p',
801                        'title' => $lang['btn_older'].' [P]',
802                        'class' => 'button show'
803                        )));
804        $form->addElement(form_makeCloseTag('div'));
805    }
806    $form->addElement(form_makeCloseTag('div'));
807    html_form('recent', $form);
808}
809
810/**
811 * Display page index
812 *
813 * @author Andreas Gohr <andi@splitbrain.org>
814 */
815function html_index($ns){
816    global $conf;
817    global $ID;
818    $dir = $conf['datadir'];
819    $ns  = cleanID($ns);
820    #fixme use appropriate function
821    if(empty($ns)){
822        $ns = dirname(str_replace(':','/',$ID));
823        if($ns == '.') $ns ='';
824    }
825    $ns  = utf8_encodeFN(str_replace(':','/',$ns));
826
827    echo p_locale_xhtml('index');
828    echo '<div id="index__tree">';
829
830    $data = array();
831    search($data,$conf['datadir'],'search_index',array('ns' => $ns));
832    echo html_buildlist($data,'idx','html_list_index','html_li_index');
833
834    echo '</div>';
835}
836
837/**
838 * Index item formatter
839 *
840 * User function for html_buildlist()
841 *
842 * @author Andreas Gohr <andi@splitbrain.org>
843 */
844function html_list_index($item){
845    global $ID;
846    $ret = '';
847    $base = ':'.$item['id'];
848    $base = substr($base,strrpos($base,':')+1);
849    if($item['type']=='d'){
850        $ret .= '<a href="'.wl($ID,'idx='.rawurlencode($item['id'])).'" class="idx_dir"><strong>';
851        $ret .= $base;
852        $ret .= '</strong></a>';
853    }else{
854        $ret .= html_wikilink(':'.$item['id']);
855    }
856    return $ret;
857}
858
859/**
860 * Index List item
861 *
862 * This user function is used in html_buildlist to build the
863 * <li> tags for namespaces when displaying the page index
864 * it gives different classes to opened or closed "folders"
865 *
866 * @author Andreas Gohr <andi@splitbrain.org>
867 */
868function html_li_index($item){
869    if($item['type'] == "f"){
870        return '<li class="level'.$item['level'].'">';
871    }elseif($item['open']){
872        return '<li class="open">';
873    }else{
874        return '<li class="closed">';
875    }
876}
877
878/**
879 * Default List item
880 *
881 * @author Andreas Gohr <andi@splitbrain.org>
882 */
883function html_li_default($item){
884    return '<li class="level'.$item['level'].'">';
885}
886
887/**
888 * Build an unordered list
889 *
890 * Build an unordered list from the given $data array
891 * Each item in the array has to have a 'level' property
892 * the item itself gets printed by the given $func user
893 * function. The second and optional function is used to
894 * print the <li> tag. Both user function need to accept
895 * a single item.
896 *
897 * Both user functions can be given as array to point to
898 * a member of an object.
899 *
900 * @author Andreas Gohr <andi@splitbrain.org>
901 */
902function html_buildlist($data,$class,$func,$lifunc='html_li_default'){
903    if (count($data) === 0) {
904        return '';
905    }
906
907    $start_level = $data[0]['level'];
908    $ret   = '';
909
910    if ($start_level < 2) {
911        // Trigger building a wrapper ul if the first level is
912        // 0 (we have a root object) or 1 (just the root content)
913        --$start_level;
914    }
915
916    $level = $start_level;
917
918    foreach ($data as $item){
919
920        if( $item['level'] > $level ){
921            //open new list
922            for($i=0; $i<($item['level'] - $level); $i++){
923                if ($i) $ret .= "<li class=\"clear\">\n";
924                $ret .= "\n<ul class=\"$class\">\n";
925            }
926        }elseif( $item['level'] < $level ){
927            //close last item
928            $ret .= "</li>\n";
929            for ($i=0; $i<($level - $item['level']); $i++){
930                //close higher lists
931                $ret .= "</ul>\n</li>\n";
932            }
933        } elseif ($ret !== '') {
934            //close last item
935            $ret .= "</li>\n";
936        }
937
938        //remember current level
939        $level = $item['level'];
940
941        //print item
942        $ret .= call_user_func($lifunc,$item);
943        $ret .= '<div class="li">';
944
945        $ret .= call_user_func($func,$item);
946        $ret .= '</div>';
947    }
948
949    //close remaining items and lists
950    while(--$level >= $start_level) {
951        $ret .= "</li></ul>\n";
952    }
953
954    return $ret;
955}
956
957/**
958 * display backlinks
959 *
960 * @author Andreas Gohr <andi@splitbrain.org>
961 * @author Michael Klier <chi@chimeric.de>
962 */
963function html_backlinks(){
964    global $ID;
965    global $conf;
966    global $lang;
967
968    print p_locale_xhtml('backlinks');
969
970    $data = ft_backlinks($ID);
971
972    if(!empty($data)) {
973        print '<ul class="idx">';
974        foreach($data as $blink){
975            print '<li><div class="li">';
976            print html_wikilink(':'.$blink,useHeading('navigation')?null:$blink);
977            print '</div></li>';
978        }
979        print '</ul>';
980    } else {
981        print '<div class="level1"><p>' . $lang['nothingfound'] . '</p></div>';
982    }
983}
984
985/**
986 * show diff
987 *
988 * @author Andreas Gohr <andi@splitbrain.org>
989 * @param  string $text - compare with this text with most current version
990 * @param  bool   $intr - display the intro text
991 */
992function html_diff($text='',$intro=true,$type=null){
993    global $ID;
994    global $REV;
995    global $lang;
996    global $conf;
997
998    if(!$type) $type = $_REQUEST['difftype'];
999    if($type != 'inline') $type = 'sidebyside';
1000
1001    // we're trying to be clever here, revisions to compare can be either
1002    // given as rev and rev2 parameters, with rev2 being optional. Or in an
1003    // array in rev2.
1004    $rev1 = $REV;
1005
1006    if(is_array($_REQUEST['rev2'])){
1007        $rev1 = (int) $_REQUEST['rev2'][0];
1008        $rev2 = (int) $_REQUEST['rev2'][1];
1009
1010        if(!$rev1){
1011            $rev1 = $rev2;
1012            unset($rev2);
1013        }
1014    }else{
1015        $rev2 = (int) $_REQUEST['rev2'];
1016    }
1017
1018    $r_minor = '';
1019    $l_minor = '';
1020
1021    if($text){                      // compare text to the most current revision
1022        $l_rev   = '';
1023        $l_text  = rawWiki($ID,'');
1024        $l_head  = '<a class="wikilink1" href="'.wl($ID).'">'.
1025            $ID.' '.dformat((int) @filemtime(wikiFN($ID))).'</a> '.
1026            $lang['current'];
1027
1028        $r_rev   = '';
1029        $r_text  = cleanText($text);
1030        $r_head  = $lang['yours'];
1031    }else{
1032        if($rev1 && $rev2){            // two specific revisions wanted
1033            // make sure order is correct (older on the left)
1034            if($rev1 < $rev2){
1035                $l_rev = $rev1;
1036                $r_rev = $rev2;
1037            }else{
1038                $l_rev = $rev2;
1039                $r_rev = $rev1;
1040            }
1041        }elseif($rev1){                // single revision given, compare to current
1042            $r_rev = '';
1043            $l_rev = $rev1;
1044        }else{                        // no revision was given, compare previous to current
1045            $r_rev = '';
1046            $revs = getRevisions($ID, 0, 1);
1047            $l_rev = $revs[0];
1048            $REV = $l_rev; // store revision back in $REV
1049        }
1050
1051        // when both revisions are empty then the page was created just now
1052        if(!$l_rev && !$r_rev){
1053            $l_text = '';
1054        }else{
1055            $l_text = rawWiki($ID,$l_rev);
1056        }
1057        $r_text = rawWiki($ID,$r_rev);
1058
1059        if(!$l_rev){
1060            $l_head = '&mdash;';
1061        }else{
1062            $l_info   = getRevisionInfo($ID,$l_rev,true);
1063            if($l_info['user']){
1064                $l_user = editorinfo($l_info['user']);
1065                if(auth_ismanager()) $l_user .= ' ('.$l_info['ip'].')';
1066            } else {
1067                $l_user = $l_info['ip'];
1068            }
1069            $l_user  = '<span class="user">'.$l_user.'</span>';
1070            $l_sum   = ($l_info['sum']) ? '<span class="sum">'.hsc($l_info['sum']).'</span>' : '';
1071            if ($l_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $l_minor = 'class="minor"';
1072
1073            $l_head = '<a class="wikilink1" href="'.wl($ID,"rev=$l_rev").'">'.
1074            $ID.' ['.dformat($l_rev).']</a>'.
1075            '<br />'.$l_user.' '.$l_sum;
1076        }
1077
1078        if($r_rev){
1079            $r_info   = getRevisionInfo($ID,$r_rev,true);
1080            if($r_info['user']){
1081                $r_user = editorinfo($r_info['user']);
1082                if(auth_ismanager()) $r_user .= ' ('.$r_info['ip'].')';
1083            } else {
1084                $r_user = $r_info['ip'];
1085            }
1086            $r_user = '<span class="user">'.$r_user.'</span>';
1087            $r_sum  = ($r_info['sum']) ? '<span class="sum">'.hsc($r_info['sum']).'</span>' : '';
1088            if ($r_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $r_minor = 'class="minor"';
1089
1090            $r_head = '<a class="wikilink1" href="'.wl($ID,"rev=$r_rev").'">'.
1091            $ID.' ['.dformat($r_rev).']</a>'.
1092            '<br />'.$r_user.' '.$r_sum;
1093        }elseif($_rev = @filemtime(wikiFN($ID))){
1094            $_info   = getRevisionInfo($ID,$_rev,true);
1095            if($_info['user']){
1096                $_user = editorinfo($_info['user']);
1097                if(auth_ismanager()) $_user .= ' ('.$_info['ip'].')';
1098            } else {
1099                $_user = $_info['ip'];
1100            }
1101            $_user = '<span class="user">'.$_user.'</span>';
1102            $_sum  = ($_info['sum']) ? '<span class="sum">'.hsc($_info['sum']).'</span>' : '';
1103            if ($_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $r_minor = 'class="minor"';
1104
1105            $r_head  = '<a class="wikilink1" href="'.wl($ID).'">'.
1106            $ID.' ['.dformat($_rev).']</a> '.
1107            '('.$lang['current'].')'.
1108            '<br />'.$_user.' '.$_sum;
1109        }else{
1110            $r_head = '&mdash; ('.$lang['current'].')';
1111        }
1112    }
1113
1114    $df = new Diff(explode("\n",htmlspecialchars($l_text)),
1115        explode("\n",htmlspecialchars($r_text)));
1116
1117    if($type == 'inline'){
1118        $tdf = new InlineDiffFormatter();
1119    } else {
1120        $tdf = new TableDiffFormatter();
1121    }
1122
1123
1124
1125    if($intro) print p_locale_xhtml('diff');
1126
1127    if (!$text) {
1128        ptln('<div class="diffoptions">');
1129
1130        $form = new Doku_Form(array('action'=>wl()));
1131        $form->addHidden('id',$ID);
1132        $form->addHidden('rev2[0]',$l_rev);
1133        $form->addHidden('rev2[1]',$r_rev);
1134        $form->addHidden('do','diff');
1135        $form->addElement(form_makeListboxField(
1136                            'difftype',
1137                            array(
1138                                'sidebyside' => $lang['diff_side'],
1139                                'inline'     => $lang['diff_inline']),
1140                            $type,
1141                            $lang['diff_type'],
1142                            '','',
1143                            array('class'=>'quickselect')));
1144        $form->addElement(form_makeButton('submit', 'diff','Go'));
1145        $form->printForm();
1146
1147
1148        $diffurl = wl($ID, array(
1149                        'do'       => 'diff',
1150                        'rev2[0]'  => $l_rev,
1151                        'rev2[1]'  => $r_rev,
1152                        'difftype' => $type,
1153                      ));
1154        ptln('<p><a class="wikilink1" href="'.$diffurl.'">'.$lang['difflink'].'</a></p>');
1155        ptln('</div>');
1156    }
1157    ?>
1158    <table class="diff diff_<?php echo $type?>">
1159    <tr>
1160    <th colspan="2" <?php echo $l_minor?>>
1161    <?php echo $l_head?>
1162    </th>
1163    <th colspan="2" <?php echo $r_minor?>>
1164    <?php echo $r_head?>
1165    </th>
1166    </tr>
1167    <?php echo $tdf->format($df)?>
1168    </table>
1169    <?php
1170}
1171
1172/**
1173 * show warning on conflict detection
1174 *
1175 * @author Andreas Gohr <andi@splitbrain.org>
1176 */
1177function html_conflict($text,$summary){
1178    global $ID;
1179    global $lang;
1180
1181    print p_locale_xhtml('conflict');
1182    $form = new Doku_Form(array('id' => 'dw__editform'));
1183    $form->addHidden('id', $ID);
1184    $form->addHidden('wikitext', $text);
1185    $form->addHidden('summary', $summary);
1186    $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('accesskey'=>'s')));
1187    $form->addElement(form_makeButton('submit', 'cancel', $lang['btn_cancel']));
1188    html_form('conflict', $form);
1189    print '<br /><br /><br /><br />'.NL;
1190}
1191
1192/**
1193 * Prints the global message array
1194 *
1195 * @author Andreas Gohr <andi@splitbrain.org>
1196 */
1197function html_msgarea(){
1198    global $MSG, $MSG_shown;
1199    // store if the global $MSG has already been shown and thus HTML output has been started
1200    $MSG_shown = true;
1201
1202    if(!isset($MSG)) return;
1203
1204    $shown = array();
1205    foreach($MSG as $msg){
1206        $hash = md5($msg['msg']);
1207        if(isset($shown[$hash])) continue; // skip double messages
1208        print '<div class="'.$msg['lvl'].'">';
1209        print $msg['msg'];
1210        print '</div>';
1211        $shown[$hash] = 1;
1212    }
1213
1214    unset($GLOBALS['MSG']);
1215}
1216
1217/**
1218 * Prints the registration form
1219 *
1220 * @author Andreas Gohr <andi@splitbrain.org>
1221 */
1222function html_register(){
1223    global $lang;
1224    global $conf;
1225    global $ID;
1226
1227    print p_locale_xhtml('register');
1228    print '<div class="centeralign">'.NL;
1229    $form = new Doku_Form(array('id' => 'dw__register'));
1230    $form->startFieldset($lang['btn_register']);
1231    $form->addHidden('do', 'register');
1232    $form->addHidden('save', '1');
1233    $form->addElement(form_makeTextField('login', $_POST['login'], $lang['user'], '', 'block', array('size'=>'50')));
1234    if (!$conf['autopasswd']) {
1235        $form->addElement(form_makePasswordField('pass', $lang['pass'], '', 'block', array('size'=>'50')));
1236        $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
1237    }
1238    $form->addElement(form_makeTextField('fullname', $_POST['fullname'], $lang['fullname'], '', 'block', array('size'=>'50')));
1239    $form->addElement(form_makeTextField('email', $_POST['email'], $lang['email'], '', 'block', array('size'=>'50')));
1240    $form->addElement(form_makeButton('submit', '', $lang['btn_register']));
1241    $form->endFieldset();
1242    html_form('register', $form);
1243
1244    print '</div>'.NL;
1245}
1246
1247/**
1248 * Print the update profile form
1249 *
1250 * @author Christopher Smith <chris@jalakai.co.uk>
1251 * @author Andreas Gohr <andi@splitbrain.org>
1252 */
1253function html_updateprofile(){
1254    global $lang;
1255    global $conf;
1256    global $ID;
1257    global $INFO;
1258    global $auth;
1259
1260    print p_locale_xhtml('updateprofile');
1261
1262    if (empty($_POST['fullname'])) $_POST['fullname'] = $INFO['userinfo']['name'];
1263    if (empty($_POST['email'])) $_POST['email'] = $INFO['userinfo']['mail'];
1264    print '<div class="centeralign">'.NL;
1265    $form = new Doku_Form(array('id' => 'dw__register'));
1266    $form->startFieldset($lang['profile']);
1267    $form->addHidden('do', 'profile');
1268    $form->addHidden('save', '1');
1269    $form->addElement(form_makeTextField('fullname', $_SERVER['REMOTE_USER'], $lang['user'], '', 'block', array('size'=>'50', 'disabled'=>'disabled')));
1270    $attr = array('size'=>'50');
1271    if (!$auth->canDo('modName')) $attr['disabled'] = 'disabled';
1272    $form->addElement(form_makeTextField('fullname', $_POST['fullname'], $lang['fullname'], '', 'block', $attr));
1273    $attr = array('size'=>'50');
1274    if (!$auth->canDo('modMail')) $attr['disabled'] = 'disabled';
1275    $form->addElement(form_makeTextField('email', $_POST['email'], $lang['email'], '', 'block', $attr));
1276    $form->addElement(form_makeTag('br'));
1277    if ($auth->canDo('modPass')) {
1278        $form->addElement(form_makePasswordField('newpass', $lang['newpass'], '', 'block', array('size'=>'50')));
1279        $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
1280    }
1281    if ($conf['profileconfirm']) {
1282        $form->addElement(form_makeTag('br'));
1283        $form->addElement(form_makePasswordField('oldpass', $lang['oldpass'], '', 'block', array('size'=>'50')));
1284    }
1285    $form->addElement(form_makeButton('submit', '', $lang['btn_save']));
1286    $form->addElement(form_makeButton('reset', '', $lang['btn_reset']));
1287    $form->endFieldset();
1288    html_form('updateprofile', $form);
1289    print '</div>'.NL;
1290}
1291
1292/**
1293 * Preprocess edit form data
1294 *
1295 * @author   Andreas Gohr <andi@splitbrain.org>
1296 *
1297 * @triggers HTML_EDITFORM_OUTPUT
1298 */
1299function html_edit(){
1300    global $ID;
1301    global $REV;
1302    global $DATE;
1303    global $PRE;
1304    global $SUF;
1305    global $INFO;
1306    global $SUM;
1307    global $lang;
1308    global $conf;
1309    global $TEXT;
1310    global $RANGE;
1311
1312    if (isset($_REQUEST['changecheck'])) {
1313        $check = $_REQUEST['changecheck'];
1314    } elseif(!$INFO['exists']){
1315        // $TEXT has been loaded from page template
1316        $check = md5('');
1317    } else {
1318        $check = md5($TEXT);
1319    }
1320    $mod = md5($TEXT) !== $check;
1321
1322    $wr = $INFO['writable'] && !$INFO['locked'];
1323    $include = 'edit';
1324    if($wr){
1325        if ($REV) $include = 'editrev';
1326    }else{
1327        // check pseudo action 'source'
1328        if(!actionOK('source')){
1329            msg('Command disabled: source',-1);
1330            return;
1331        }
1332        $include = 'read';
1333    }
1334
1335    global $license;
1336
1337    $form = new Doku_Form(array('id' => 'dw__editform'));
1338    $form->addHidden('id', $ID);
1339    $form->addHidden('rev', $REV);
1340    $form->addHidden('date', $DATE);
1341    $form->addHidden('prefix', $PRE . '.');
1342    $form->addHidden('suffix', $SUF);
1343    $form->addHidden('changecheck', $check);
1344
1345    $data = array('form' => $form,
1346                  'wr'   => $wr,
1347                  'media_manager' => true,
1348                  'target' => (isset($_REQUEST['target']) && $wr &&
1349                               $RANGE !== '') ? $_REQUEST['target'] : 'section',
1350                  'intro_locale' => $include);
1351
1352    if ($data['target'] !== 'section') {
1353        // Only emit event if page is writable, section edit data is valid and
1354        // edit target is not section.
1355        trigger_event('HTML_EDIT_FORMSELECTION', $data, 'html_edit_form', true);
1356    } else {
1357        html_edit_form($data);
1358    }
1359    if (isset($data['intro_locale'])) {
1360        echo p_locale_xhtml($data['intro_locale']);
1361    }
1362
1363    $form->addHidden('target', $data['target']);
1364    $form->addElement(form_makeOpenTag('div', array('id'=>'wiki__editbar')));
1365    $form->addElement(form_makeOpenTag('div', array('id'=>'size__ctl')));
1366    $form->addElement(form_makeCloseTag('div'));
1367    if ($wr) {
1368        $form->addElement(form_makeOpenTag('div', array('class'=>'editButtons')));
1369        $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('id'=>'edbtn__save', 'accesskey'=>'s', 'tabindex'=>'4')));
1370        $form->addElement(form_makeButton('submit', 'preview', $lang['btn_preview'], array('id'=>'edbtn__preview', 'accesskey'=>'p', 'tabindex'=>'5')));
1371        $form->addElement(form_makeButton('submit', 'draftdel', $lang['btn_cancel'], array('tabindex'=>'6')));
1372        $form->addElement(form_makeCloseTag('div'));
1373        $form->addElement(form_makeOpenTag('div', array('class'=>'summary')));
1374        $form->addElement(form_makeTextField('summary', $SUM, $lang['summary'], 'edit__summary', 'nowrap', array('size'=>'50', 'tabindex'=>'2')));
1375        $elem = html_minoredit();
1376        if ($elem) $form->addElement($elem);
1377        $form->addElement(form_makeCloseTag('div'));
1378    }
1379    $form->addElement(form_makeCloseTag('div'));
1380    if($wr && $conf['license']){
1381        $form->addElement(form_makeOpenTag('div', array('class'=>'license')));
1382        $out  = $lang['licenseok'];
1383        $out .= ' <a href="'.$license[$conf['license']]['url'].'" rel="license" class="urlextern"';
1384        if($conf['target']['extern']) $out .= ' target="'.$conf['target']['extern'].'"';
1385        $out .= '>'.$license[$conf['license']]['name'].'</a>';
1386        $form->addElement($out);
1387        $form->addElement(form_makeCloseTag('div'));
1388    }
1389
1390    if ($wr) {
1391        // sets changed to true when previewed
1392        echo '<script type="text/javascript" charset="utf-8"><!--//--><![CDATA[//><!--'. NL;
1393        echo 'textChanged = ' . ($mod ? 'true' : 'false');
1394        echo '//--><!]]></script>' . NL;
1395    } ?>
1396    <div style="width:99%;">
1397
1398    <div class="toolbar">
1399    <div id="draft__status"><?php if(!empty($INFO['draft'])) echo $lang['draftdate'].' '.dformat();?></div>
1400    <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']?>"
1401        target="_blank"><?php echo $lang['mediaselect'] ?></a><?php }?></div>
1402
1403    </div>
1404    <?php
1405
1406    html_form('edit', $form);
1407    print '</div>'.NL;
1408}
1409
1410/**
1411 * Display the default edit form
1412 *
1413 * Is the default action for HTML_EDIT_FORMSELECTION.
1414 */
1415function html_edit_form($param) {
1416    global $TEXT;
1417
1418    if ($param['target'] !== 'section') {
1419        msg('No editor for edit target ' . $param['target'] . ' found.', -1);
1420    }
1421
1422    $attr = array('tabindex'=>'1');
1423    if (!$param['wr']) $attr['readonly'] = 'readonly';
1424
1425    $param['form']->addElement(form_makeWikiText($TEXT, $attr));
1426}
1427
1428/**
1429 * Adds a checkbox for minor edits for logged in users
1430 *
1431 * @author Andreas Gohr <andi@splitbrain.org>
1432 */
1433function html_minoredit(){
1434    global $conf;
1435    global $lang;
1436    // minor edits are for logged in users only
1437    if(!$conf['useacl'] || !$_SERVER['REMOTE_USER']){
1438        return false;
1439    }
1440
1441    $p = array();
1442    $p['tabindex'] = 3;
1443    if(!empty($_REQUEST['minor'])) $p['checked']='checked';
1444    return form_makeCheckboxField('minor', '1', $lang['minoredit'], 'minoredit', 'nowrap', $p);
1445}
1446
1447/**
1448 * prints some debug info
1449 *
1450 * @author Andreas Gohr <andi@splitbrain.org>
1451 */
1452function html_debug(){
1453    global $conf;
1454    global $lang;
1455    global $auth;
1456    global $INFO;
1457
1458    //remove sensitive data
1459    $cnf = $conf;
1460    debug_guard($cnf);
1461    $nfo = $INFO;
1462    debug_guard($nfo);
1463    $ses = $_SESSION;
1464    debug_guard($ses);
1465
1466    print '<html><body>';
1467
1468    print '<p>When reporting bugs please send all the following ';
1469    print 'output as a mail to andi@splitbrain.org ';
1470    print 'The best way to do this is to save this page in your browser</p>';
1471
1472    print '<b>$INFO:</b><pre>';
1473    print_r($nfo);
1474    print '</pre>';
1475
1476    print '<b>$_SERVER:</b><pre>';
1477    print_r($_SERVER);
1478    print '</pre>';
1479
1480    print '<b>$conf:</b><pre>';
1481    print_r($cnf);
1482    print '</pre>';
1483
1484    print '<b>DOKU_BASE:</b><pre>';
1485    print DOKU_BASE;
1486    print '</pre>';
1487
1488    print '<b>abs DOKU_BASE:</b><pre>';
1489    print DOKU_URL;
1490    print '</pre>';
1491
1492    print '<b>rel DOKU_BASE:</b><pre>';
1493    print dirname($_SERVER['PHP_SELF']).'/';
1494    print '</pre>';
1495
1496    print '<b>PHP Version:</b><pre>';
1497    print phpversion();
1498    print '</pre>';
1499
1500    print '<b>locale:</b><pre>';
1501    print setlocale(LC_ALL,0);
1502    print '</pre>';
1503
1504    print '<b>encoding:</b><pre>';
1505    print $lang['encoding'];
1506    print '</pre>';
1507
1508    if($auth){
1509        print '<b>Auth backend capabilities:</b><pre>';
1510        print_r($auth->cando);
1511        print '</pre>';
1512    }
1513
1514    print '<b>$_SESSION:</b><pre>';
1515    print_r($ses);
1516    print '</pre>';
1517
1518    print '<b>Environment:</b><pre>';
1519    print_r($_ENV);
1520    print '</pre>';
1521
1522    print '<b>PHP settings:</b><pre>';
1523    $inis = ini_get_all();
1524    print_r($inis);
1525    print '</pre>';
1526
1527    print '</body></html>';
1528}
1529
1530/**
1531 * List available Administration Tasks
1532 *
1533 * @author Andreas Gohr <andi@splitbrain.org>
1534 * @author Håkan Sandell <hakan.sandell@home.se>
1535 */
1536function html_admin(){
1537    global $ID;
1538    global $INFO;
1539    global $lang;
1540    global $conf;
1541    global $auth;
1542
1543    // build menu of admin functions from the plugins that handle them
1544    $pluginlist = plugin_list('admin');
1545    $menu = array();
1546    foreach ($pluginlist as $p) {
1547        if($obj =& plugin_load('admin',$p) === null) continue;
1548
1549        // check permissions
1550        if($obj->forAdminOnly() && !$INFO['isadmin']) continue;
1551
1552        $menu[$p] = array('plugin' => $p,
1553                'prompt' => $obj->getMenuText($conf['lang']),
1554                'sort' => $obj->getMenuSort()
1555                );
1556    }
1557
1558    // data security check
1559    // @todo: could be checked and only displayed if $conf['savedir'] is under the web root
1560    echo '<a style="border:none; float:right;"
1561            href="http://www.dokuwiki.org/security#web_access_security">
1562            <img src="data/security.png" alt="Your data directory seems to be protected properly."
1563             onerror="this.parentNode.style.display=\'none\'" /></a>';
1564
1565    print p_locale_xhtml('admin');
1566
1567    // Admin Tasks
1568    if($INFO['isadmin']){
1569        ptln('<ul class="admin_tasks">');
1570
1571        if($menu['usermanager'] && $auth && $auth->canDo('getUsers')){
1572            ptln('  <li class="admin_usermanager"><div class="li">'.
1573                    '<a href="'.wl($ID, array('do' => 'admin','page' => 'usermanager')).'">'.
1574                    $menu['usermanager']['prompt'].'</a></div></li>');
1575        }
1576        unset($menu['usermanager']);
1577
1578        if($menu['acl']){
1579            ptln('  <li class="admin_acl"><div class="li">'.
1580                    '<a href="'.wl($ID, array('do' => 'admin','page' => 'acl')).'">'.
1581                    $menu['acl']['prompt'].'</a></div></li>');
1582        }
1583        unset($menu['acl']);
1584
1585        if($menu['plugin']){
1586            ptln('  <li class="admin_plugin"><div class="li">'.
1587                    '<a href="'.wl($ID, array('do' => 'admin','page' => 'plugin')).'">'.
1588                    $menu['plugin']['prompt'].'</a></div></li>');
1589        }
1590        unset($menu['plugin']);
1591
1592        if($menu['config']){
1593            ptln('  <li class="admin_config"><div class="li">'.
1594                    '<a href="'.wl($ID, array('do' => 'admin','page' => 'config')).'">'.
1595                    $menu['config']['prompt'].'</a></div></li>');
1596        }
1597        unset($menu['config']);
1598    }
1599    ptln('</ul>');
1600
1601    // Manager Tasks
1602    ptln('<ul class="admin_tasks">');
1603
1604    if($menu['revert']){
1605        ptln('  <li class="admin_revert"><div class="li">'.
1606                '<a href="'.wl($ID, array('do' => 'admin','page' => 'revert')).'">'.
1607                $menu['revert']['prompt'].'</a></div></li>');
1608    }
1609    unset($menu['revert']);
1610
1611    if($menu['popularity']){
1612        ptln('  <li class="admin_popularity"><div class="li">'.
1613                '<a href="'.wl($ID, array('do' => 'admin','page' => 'popularity')).'">'.
1614                $menu['popularity']['prompt'].'</a></div></li>');
1615    }
1616    unset($menu['popularity']);
1617
1618    // print DokuWiki version:
1619    ptln('</ul>');
1620    echo '<div id="admin__version">';
1621    echo getVersion();
1622    echo '</div>';
1623
1624    // print the rest as sorted list
1625    if(count($menu)){
1626        usort($menu, 'p_sort_modes');
1627        // output the menu
1628        ptln('<div class="clearer"></div>');
1629        print p_locale_xhtml('adminplugins');
1630        ptln('<ul>');
1631        foreach ($menu as $item) {
1632            if (!$item['prompt']) continue;
1633            ptln('  <li><div class="li"><a href="'.wl($ID, 'do=admin&amp;page='.$item['plugin']).'">'.$item['prompt'].'</a></div></li>');
1634        }
1635        ptln('</ul>');
1636    }
1637}
1638
1639/**
1640 * Form to request a new password for an existing account
1641 *
1642 * @author Benoit Chesneau <benoit@bchesneau.info>
1643 */
1644function html_resendpwd() {
1645    global $lang;
1646    global $conf;
1647    global $ID;
1648
1649    print p_locale_xhtml('resendpwd');
1650    print '<div class="centeralign">'.NL;
1651    $form = new Doku_Form(array('id' => 'dw__resendpwd'));
1652    $form->startFieldset($lang['resendpwd']);
1653    $form->addHidden('do', 'resendpwd');
1654    $form->addHidden('save', '1');
1655    $form->addElement(form_makeTag('br'));
1656    $form->addElement(form_makeTextField('login', $_POST['login'], $lang['user'], '', 'block'));
1657    $form->addElement(form_makeTag('br'));
1658    $form->addElement(form_makeTag('br'));
1659    $form->addElement(form_makeButton('submit', '', $lang['btn_resendpwd']));
1660    $form->endFieldset();
1661    html_form('resendpwd', $form);
1662    print '</div>'.NL;
1663}
1664
1665/**
1666 * Return the TOC rendered to XHTML
1667 *
1668 * @author Andreas Gohr <andi@splitbrain.org>
1669 */
1670function html_TOC($toc){
1671    if(!count($toc)) return '';
1672    global $lang;
1673    $out  = '<!-- TOC START -->'.DOKU_LF;
1674    $out .= '<div class="toc">'.DOKU_LF;
1675    $out .= '<div class="tocheader toctoggle" id="toc__header">';
1676    $out .= $lang['toc'];
1677    $out .= '</div>'.DOKU_LF;
1678    $out .= '<div id="toc__inside">'.DOKU_LF;
1679    $out .= html_buildlist($toc,'toc','html_list_toc');
1680    $out .= '</div>'.DOKU_LF.'</div>'.DOKU_LF;
1681    $out .= '<!-- TOC END -->'.DOKU_LF;
1682    return $out;
1683}
1684
1685/**
1686 * Callback for html_buildlist
1687 */
1688function html_list_toc($item){
1689    if(isset($item['hid'])){
1690        $link = '#'.$item['hid'];
1691    }else{
1692        $link = $item['link'];
1693    }
1694
1695    return '<span class="li"><a href="'.$link.'" class="toc">'.
1696        hsc($item['title']).'</a></span>';
1697}
1698
1699/**
1700 * Helper function to build TOC items
1701 *
1702 * Returns an array ready to be added to a TOC array
1703 *
1704 * @param string $link  - where to link (if $hash set to '#' it's a local anchor)
1705 * @param string $text  - what to display in the TOC
1706 * @param int    $level - nesting level
1707 * @param string $hash  - is prepended to the given $link, set blank if you want full links
1708 */
1709function html_mktocitem($link, $text, $level, $hash='#'){
1710    global $conf;
1711    return  array( 'link'  => $hash.$link,
1712            'title' => $text,
1713            'type'  => 'ul',
1714            'level' => $level);
1715}
1716
1717/**
1718 * Output a Doku_Form object.
1719 * Triggers an event with the form name: HTML_{$name}FORM_OUTPUT
1720 *
1721 * @author Tom N Harris <tnharris@whoopdedo.org>
1722 */
1723function html_form($name, &$form) {
1724    // Safety check in case the caller forgets.
1725    $form->endFieldset();
1726    trigger_event('HTML_'.strtoupper($name).'FORM_OUTPUT', $form, 'html_form_output', false);
1727}
1728
1729/**
1730 * Form print function.
1731 * Just calls printForm() on the data object.
1732 */
1733function html_form_output($data) {
1734    $data->printForm();
1735}
1736
1737/**
1738 * Embed a flash object in HTML
1739 *
1740 * This will create the needed HTML to embed a flash movie in a cross browser
1741 * compatble way using valid XHTML
1742 *
1743 * The parameters $params, $flashvars and $atts need to be associative arrays.
1744 * No escaping needs to be done for them. The alternative content *has* to be
1745 * escaped because it is used as is. If no alternative content is given
1746 * $lang['noflash'] is used.
1747 *
1748 * @author Andreas Gohr <andi@splitbrain.org>
1749 * @link   http://latrine.dgx.cz/how-to-correctly-insert-a-flash-into-xhtml
1750 *
1751 * @param string $swf      - the SWF movie to embed
1752 * @param int $width       - width of the flash movie in pixels
1753 * @param int $height      - height of the flash movie in pixels
1754 * @param array $params    - additional parameters (<param>)
1755 * @param array $flashvars - parameters to be passed in the flashvar parameter
1756 * @param array $atts      - additional attributes for the <object> tag
1757 * @param string $alt      - alternative content (is NOT automatically escaped!)
1758 * @returns string         - the XHTML markup
1759 */
1760function html_flashobject($swf,$width,$height,$params=null,$flashvars=null,$atts=null,$alt=''){
1761    global $lang;
1762
1763    $out = '';
1764
1765    // prepare the object attributes
1766    if(is_null($atts)) $atts = array();
1767    $atts['width']  = (int) $width;
1768    $atts['height'] = (int) $height;
1769    if(!$atts['width'])  $atts['width']  = 425;
1770    if(!$atts['height']) $atts['height'] = 350;
1771
1772    // add object attributes for standard compliant browsers
1773    $std = $atts;
1774    $std['type'] = 'application/x-shockwave-flash';
1775    $std['data'] = $swf;
1776
1777    // add object attributes for IE
1778    $ie  = $atts;
1779    $ie['classid'] = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
1780
1781    // open object (with conditional comments)
1782    $out .= '<!--[if !IE]> -->'.NL;
1783    $out .= '<object '.buildAttributes($std).'>'.NL;
1784    $out .= '<!-- <![endif]-->'.NL;
1785    $out .= '<!--[if IE]>'.NL;
1786    $out .= '<object '.buildAttributes($ie).'>'.NL;
1787    $out .= '    <param name="movie" value="'.hsc($swf).'" />'.NL;
1788    $out .= '<!--><!-- -->'.NL;
1789
1790    // print params
1791    if(is_array($params)) foreach($params as $key => $val){
1792        $out .= '  <param name="'.hsc($key).'" value="'.hsc($val).'" />'.NL;
1793    }
1794
1795    // add flashvars
1796    if(is_array($flashvars)){
1797        $out .= '  <param name="FlashVars" value="'.buildURLparams($flashvars).'" />'.NL;
1798    }
1799
1800    // alternative content
1801    if($alt){
1802        $out .= $alt.NL;
1803    }else{
1804        $out .= $lang['noflash'].NL;
1805    }
1806
1807    // finish
1808    $out .= '</object>'.NL;
1809    $out .= '<!-- <![endif]-->'.NL;
1810
1811    return $out;
1812}
1813
1814