xref: /dokuwiki/inc/html.php (revision a023425bd4a7e771a08a437a70034381d7ec0681)
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 * @param string  $id      id of the target page
17 * @param string  $name    the name of the link, i.e. the text that is displayed
18 * @param string|array  $search  search string(s) that shall be highlighted in the target page
19 * @return string the HTML code of the link
20 */
21function html_wikilink($id,$name=null,$search=''){
22    /** @var Doku_Renderer_xhtml $xhtml_renderer */
23    static $xhtml_renderer = null;
24    if(is_null($xhtml_renderer)){
25        $xhtml_renderer = p_get_renderer('xhtml');
26    }
27
28    return $xhtml_renderer->internallink($id,$name,$search,true,'navigation');
29}
30
31/**
32 * The loginform
33 *
34 * @author   Andreas Gohr <andi@splitbrain.org>
35 */
36function html_login(){
37    global $lang;
38    global $conf;
39    global $ID;
40    global $INPUT;
41
42    print p_locale_xhtml('login');
43    print '<div class="centeralign">'.NL;
44    $form = new Doku_Form(array('id' => 'dw__login'));
45    $form->startFieldset($lang['btn_login']);
46    $form->addHidden('id', $ID);
47    $form->addHidden('do', 'login');
48    $form->addElement(form_makeTextField('u', ((!$INPUT->bool('http_credentials')) ? $INPUT->str('u') : ''), $lang['user'], 'focus__this', 'block'));
49    $form->addElement(form_makePasswordField('p', $lang['pass'], '', 'block'));
50    if($conf['rememberme']) {
51        $form->addElement(form_makeCheckboxField('r', '1', $lang['remember'], 'remember__me', 'simple'));
52    }
53    $form->addElement(form_makeButton('submit', '', $lang['btn_login']));
54    $form->endFieldset();
55
56    if(actionOK('register')){
57        $form->addElement('<p>'.$lang['reghere'].': '.tpl_actionlink('register','','','',true).'</p>');
58    }
59
60    if (actionOK('resendpwd')) {
61        $form->addElement('<p>'.$lang['pwdforget'].': '.tpl_actionlink('resendpwd','','','',true).'</p>');
62    }
63
64    html_form('login', $form);
65    print '</div>'.NL;
66}
67
68
69/**
70 * Denied page content
71 *
72 * @return string html
73 */
74function html_denied() {
75    print p_locale_xhtml('denied');
76
77    if(!$_SERVER['REMOTE_USER']){
78        html_login();
79    }
80}
81
82/**
83 * inserts section edit buttons if wanted or removes the markers
84 *
85 * @author Andreas Gohr <andi@splitbrain.org>
86 *
87 * @param string $text
88 * @param bool   $show show section edit buttons?
89 * @return string
90 */
91function html_secedit($text,$show=true){
92    global $INFO;
93
94    $regexp = '#<!-- EDIT(\d+) ([A-Z_]+) (?:"([^"]*)" )?\[(\d+-\d*)\] -->#';
95
96    if(!$INFO['writable'] || !$show || $INFO['rev']){
97        return preg_replace($regexp,'',$text);
98    }
99
100    return preg_replace_callback($regexp,
101                'html_secedit_button', $text);
102}
103
104/**
105 * prepares section edit button data for event triggering
106 * used as a callback in html_secedit
107 *
108 * @author Andreas Gohr <andi@splitbrain.org>
109 *
110 * @param array $matches matches with regexp
111 * @return string
112 * @triggers HTML_SECEDIT_BUTTON
113 */
114function html_secedit_button($matches){
115    $data = array('secid'  => $matches[1],
116                  'target' => strtolower($matches[2]),
117                  'range'  => $matches[count($matches) - 1]);
118    if (count($matches) === 5) {
119        $data['name'] = $matches[3];
120    }
121
122    return trigger_event('HTML_SECEDIT_BUTTON', $data,
123                         'html_secedit_get_button');
124}
125
126/**
127 * prints a section editing button
128 * used as default action form HTML_SECEDIT_BUTTON
129 *
130 * @author Adrian Lang <lang@cosmocode.de>
131 *
132 * @param array $data name, section id and target
133 * @return string html
134 */
135function html_secedit_get_button($data) {
136    global $ID;
137    global $INFO;
138
139    if (!isset($data['name']) || $data['name'] === '') return '';
140
141    $name = $data['name'];
142    unset($data['name']);
143
144    $secid = $data['secid'];
145    unset($data['secid']);
146
147    return "<div class='secedit editbutton_" . $data['target'] .
148                       " editbutton_" . $secid . "'>" .
149           html_btn('secedit', $ID, '',
150                    array_merge(array('do'  => 'edit',
151                                      'rev' => $INFO['lastmod'],
152                                      'summary' => '['.$name.'] '), $data),
153                    'post', $name) . '</div>';
154}
155
156/**
157 * Just the back to top button (in its own form)
158 *
159 * @author Andreas Gohr <andi@splitbrain.org>
160 *
161 * @return string html
162 */
163function html_topbtn(){
164    global $lang;
165
166    $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>';
167
168    return $ret;
169}
170
171/**
172 * Displays a button (using its own form)
173 * If tooltip exists, the access key tooltip is replaced.
174 *
175 * @author Andreas Gohr <andi@splitbrain.org>
176 *
177 * @param string         $name
178 * @param string         $id
179 * @param string         $akey   access key
180 * @param string[] $params key-value pairs added as hidden inputs
181 * @param string         $method
182 * @param string         $tooltip
183 * @param bool|string    $label  label text, false: lookup btn_$name in localization
184 * @return string
185 */
186function html_btn($name, $id, $akey, $params, $method='get', $tooltip='', $label=false){
187    global $conf;
188    global $lang;
189
190    if (!$label)
191        $label = $lang['btn_'.$name];
192
193    $ret = '';
194
195    //filter id (without urlencoding)
196    $id = idfilter($id,false);
197
198    //make nice URLs even for buttons
199    if($conf['userewrite'] == 2){
200        $script = DOKU_BASE.DOKU_SCRIPT.'/'.$id;
201    }elseif($conf['userewrite']){
202        $script = DOKU_BASE.$id;
203    }else{
204        $script = DOKU_BASE.DOKU_SCRIPT;
205        $params['id'] = $id;
206    }
207
208    $ret .= '<form class="button btn_'.$name.'" method="'.$method.'" action="'.$script.'"><div class="no">';
209
210    if(is_array($params)){
211        reset($params);
212        while (list($key, $val) = each($params)) {
213            $ret .= '<input type="hidden" name="'.$key.'" ';
214            $ret .= 'value="'.htmlspecialchars($val).'" />';
215        }
216    }
217
218    if ($tooltip!='') {
219        $tip = htmlspecialchars($tooltip);
220    }else{
221        $tip = htmlspecialchars($label);
222    }
223
224    $ret .= '<button type="submit" ';
225    if($akey){
226        $tip .= ' ['.strtoupper($akey).']';
227        $ret .= 'accesskey="'.$akey.'" ';
228    }
229    $ret .= 'title="'.$tip.'" ';
230    $ret .= '/>';
231    $ret .= hsc($label);
232    $ret .= '</button>';
233    $ret .= '</div></form>';
234
235    return $ret;
236}
237/**
238 * show a revision warning
239 *
240 * @author Szymon Olewniczak <dokuwiki@imz.re>
241 */
242function html_showrev() {
243    print p_locale_xhtml('showrev');
244}
245
246/**
247 * Show a wiki page
248 *
249 * @author Andreas Gohr <andi@splitbrain.org>
250 *
251 * @param null|string $txt wiki text or null for showing $ID
252 */
253function html_show($txt=null){
254    global $ID;
255    global $REV;
256    global $HIGH;
257    global $INFO;
258    global $DATE_AT;
259    //disable section editing for old revisions or in preview
260    if($txt || $REV){
261        $secedit = false;
262    }else{
263        $secedit = true;
264    }
265
266    if (!is_null($txt)){
267        //PreviewHeader
268        echo '<br id="scroll__here" />';
269        echo p_locale_xhtml('preview');
270        echo '<div class="preview"><div class="pad">';
271        $html = html_secedit(p_render('xhtml',p_get_instructions($txt),$info),$secedit);
272        if($INFO['prependTOC']) $html = tpl_toc(true).$html;
273        echo $html;
274        echo '<div class="clearer"></div>';
275        echo '</div></div>';
276
277    }else{
278        if ($REV||$DATE_AT){
279            $data = array('rev' => &$REV, 'date_at' => &$DATE_AT);
280            trigger_event('HTML_SHOWREV_OUTPUT', $data, 'html_showrev');
281        }
282        $html = p_wiki_xhtml($ID,$REV,true,$DATE_AT);
283        $html = html_secedit($html,$secedit);
284        if($INFO['prependTOC']) $html = tpl_toc(true).$html;
285        $html = html_hilight($html,$HIGH);
286        echo $html;
287    }
288}
289
290/**
291 * ask the user about how to handle an exisiting draft
292 *
293 * @author Andreas Gohr <andi@splitbrain.org>
294 */
295function html_draft(){
296    global $INFO;
297    global $ID;
298    global $lang;
299    $draft = unserialize(io_readFile($INFO['draft'],false));
300    $text  = cleanText(con($draft['prefix'],$draft['text'],$draft['suffix'],true));
301
302    print p_locale_xhtml('draft');
303    $form = new Doku_Form(array('id' => 'dw__editform'));
304    $form->addHidden('id', $ID);
305    $form->addHidden('date', $draft['date']);
306    $form->addElement(form_makeWikiText($text, array('readonly'=>'readonly')));
307    $form->addElement(form_makeOpenTag('div', array('id'=>'draft__status')));
308    $form->addElement($lang['draftdate'].' '. dformat(filemtime($INFO['draft'])));
309    $form->addElement(form_makeCloseTag('div'));
310    $form->addElement(form_makeButton('submit', 'recover', $lang['btn_recover'], array('tabindex'=>'1')));
311    $form->addElement(form_makeButton('submit', 'draftdel', $lang['btn_draftdel'], array('tabindex'=>'2')));
312    $form->addElement(form_makeButton('submit', 'show', $lang['btn_cancel'], array('tabindex'=>'3')));
313    html_form('draft', $form);
314}
315
316/**
317 * Highlights searchqueries in HTML code
318 *
319 * @author Andreas Gohr <andi@splitbrain.org>
320 * @author Harry Fuecks <hfuecks@gmail.com>
321 *
322 * @param string $html
323 * @param array|string $phrases
324 * @return string html
325 */
326function html_hilight($html,$phrases){
327    $phrases = (array) $phrases;
328    $phrases = array_map('preg_quote_cb', $phrases);
329    $phrases = array_map('ft_snippet_re_preprocess', $phrases);
330    $phrases = array_filter($phrases);
331    $regex = join('|',$phrases);
332
333    if ($regex === '') return $html;
334    if (!utf8_check($regex)) return $html;
335    $html = @preg_replace_callback("/((<[^>]*)|$regex)/ui",'html_hilight_callback',$html);
336    return $html;
337}
338
339/**
340 * Callback used by html_hilight()
341 *
342 * @author Harry Fuecks <hfuecks@gmail.com>
343 *
344 * @param array $m matches
345 * @return string html
346 */
347function html_hilight_callback($m) {
348    $hlight = unslash($m[0]);
349    if ( !isset($m[2])) {
350        $hlight = '<span class="search_hit">'.$hlight.'</span>';
351    }
352    return $hlight;
353}
354
355/**
356 * Run a search and display the result
357 *
358 * @author Andreas Gohr <andi@splitbrain.org>
359 */
360function html_search(){
361    global $QUERY, $ID;
362    global $lang;
363
364    $intro = p_locale_xhtml('searchpage');
365    // allow use of placeholder in search intro
366    $pagecreateinfo = (auth_quickaclcheck($ID) >= AUTH_CREATE) ? $lang['searchcreatepage'] : '';
367    $intro = str_replace(
368        array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'),
369        array(hsc(rawurlencode($QUERY)), hsc($QUERY), $pagecreateinfo),
370        $intro
371    );
372    echo $intro;
373    flush();
374
375    //show progressbar
376    print '<div id="dw__loading">'.NL;
377    print '<script type="text/javascript">/*<![CDATA[*/'.NL;
378    print 'showLoadBar();'.NL;
379    print '/*!]]>*/</script>'.NL;
380    print '</div>'.NL;
381    flush();
382
383    //do quick pagesearch
384    $data = ft_pageLookup($QUERY,true,useHeading('navigation'));
385    if(count($data)){
386        print '<div class="search_quickresult">';
387        print '<h3>'.$lang['quickhits'].':</h3>';
388        print '<ul class="search_quickhits">';
389        foreach($data as $id => $title){
390            print '<li> ';
391            if (useHeading('navigation')) {
392                $name = $title;
393            }else{
394                $ns = getNS($id);
395                if($ns){
396                    $name = shorten(noNS($id), ' ('.$ns.')',30);
397                }else{
398                    $name = $id;
399                }
400            }
401            print html_wikilink(':'.$id,$name);
402            print '</li> ';
403        }
404        print '</ul> ';
405        //clear float (see http://www.complexspiral.com/publications/containing-floats/)
406        print '<div class="clearer"></div>';
407        print '</div>';
408    }
409    flush();
410
411    //do fulltext search
412    $data = ft_pageSearch($QUERY,$regex);
413    if(count($data)){
414        print '<dl class="search_results">';
415        $num = 1;
416        foreach($data as $id => $cnt){
417            print '<dt>';
418            print html_wikilink(':'.$id,useHeading('navigation')?null:$id,$regex);
419            if($cnt !== 0){
420                print ': '.$cnt.' '.$lang['hits'].'';
421            }
422            print '</dt>';
423            if($cnt !== 0){
424                if($num < FT_SNIPPET_NUMBER){ // create snippets for the first number of matches only
425                    print '<dd>'.ft_snippet($id,$regex).'</dd>';
426                }
427                $num++;
428            }
429            flush();
430        }
431        print '</dl>';
432    }else{
433        print '<div class="nothing">'.$lang['nothingfound'].'</div>';
434    }
435
436    //hide progressbar
437    print '<script type="text/javascript">/*<![CDATA[*/'.NL;
438    print 'hideLoadBar("dw__loading");'.NL;
439    print '/*!]]>*/</script>'.NL;
440    flush();
441}
442
443/**
444 * Display error on locked pages
445 *
446 * @author Andreas Gohr <andi@splitbrain.org>
447 */
448function html_locked(){
449    global $ID;
450    global $conf;
451    global $lang;
452    global $INFO;
453
454    $locktime = filemtime(wikiLockFN($ID));
455    $expire = dformat($locktime + $conf['locktime']);
456    $min    = round(($conf['locktime'] - (time() - $locktime) )/60);
457
458    print p_locale_xhtml('locked');
459    print '<ul>';
460    print '<li><div class="li"><strong>'.$lang['lockedby'].'</strong> '.editorinfo($INFO['locked']).'</div></li>';
461    print '<li><div class="li"><strong>'.$lang['lockexpire'].'</strong> '.$expire.' ('.$min.' min)</div></li>';
462    print '</ul>';
463}
464
465/**
466 * list old revisions
467 *
468 * @author Andreas Gohr <andi@splitbrain.org>
469 * @author Ben Coburn <btcoburn@silicodon.net>
470 * @author Kate Arzamastseva <pshns@ukr.net>
471 *
472 * @param int $first skip the first n changelog lines
473 * @param bool|string $media_id id of media, or false for current page
474 */
475function html_revisions($first=0, $media_id = false){
476    global $ID;
477    global $INFO;
478    global $conf;
479    global $lang;
480    $id = $ID;
481    if ($media_id) {
482        $id = $media_id;
483        $changelog = new MediaChangeLog($id);
484    } else {
485        $changelog = new PageChangeLog($id);
486    }
487
488    /* we need to get one additional log entry to be able to
489     * decide if this is the last page or is there another one.
490     * see html_recent()
491     */
492
493    $revisions = $changelog->getRevisions($first, $conf['recent']+1);
494
495    if(count($revisions)==0 && $first!=0){
496        $first=0;
497        $revisions = $changelog->getRevisions($first, $conf['recent']+1);
498    }
499    $hasNext = false;
500    if (count($revisions)>$conf['recent']) {
501        $hasNext = true;
502        array_pop($revisions); // remove extra log entry
503    }
504
505    if (!$media_id) print p_locale_xhtml('revisions');
506
507    $params = array('id' => 'page__revisions', 'class' => 'changes');
508    if($media_id) {
509        $params['action'] = media_managerURL(array('image' => $media_id), '&');
510    }
511
512    if(!$media_id) {
513        $exists = $INFO['exists'];
514        $display_name = useHeading('navigation') ? hsc(p_get_first_heading($id)) : $id;
515        if(!$display_name) {
516            $display_name = $id;
517        }
518    } else {
519        $exists = file_exists(mediaFN($id));
520        $display_name = $id;
521    }
522
523    $form = new Doku_Form($params);
524    $form->addElement(form_makeOpenTag('ul'));
525
526    if($exists && $first == 0) {
527        $minor = false;
528        if($media_id) {
529            $date = dformat(@filemtime(mediaFN($id)));
530            $href = media_managerURL(array('image' => $id, 'tab_details' => 'view'), '&');
531
532            $changelog->setChunkSize(1024);
533            $revinfo = $changelog->getRevisionInfo(@filemtime(fullpath(mediaFN($id))));
534
535            $summary = $revinfo['sum'];
536            if($revinfo['user']) {
537                $editor = $revinfo['user'];
538            } else {
539                $editor = $revinfo['ip'];
540            }
541            $sizechange = $revinfo['sizechange'];
542        } else {
543            $date = dformat($INFO['lastmod']);
544            if(isset($INFO['meta']) && isset($INFO['meta']['last_change'])) {
545                if($INFO['meta']['last_change']['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) {
546                    $minor = true;
547                }
548                if(isset($INFO['meta']['last_change']['sizechange'])) {
549                    $sizechange = $INFO['meta']['last_change']['sizechange'];
550                } else {
551                    $sizechange = null;
552                }
553            }
554            $href = wl($id);
555            $summary = $INFO['sum'];
556            $editor = $INFO['editor'];
557        }
558
559        $form->addElement(form_makeOpenTag('li', array('class' => ($minor ? 'minor' : ''))));
560        $form->addElement(form_makeOpenTag('div', array('class' => 'li')));
561        $form->addElement(form_makeTag('input', array(
562                        'type' => 'checkbox',
563                        'name' => 'rev2[]',
564                        'value' => 'current')));
565
566        $form->addElement(form_makeOpenTag('span', array('class' => 'date')));
567        $form->addElement($date);
568        $form->addElement(form_makeCloseTag('span'));
569
570        $form->addElement('<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />');
571
572        $form->addElement(form_makeOpenTag('a', array(
573                        'class' => 'wikilink1',
574                        'href'  => $href)));
575        $form->addElement($display_name);
576        $form->addElement(form_makeCloseTag('a'));
577
578        if ($media_id) $form->addElement(form_makeOpenTag('div'));
579
580        if($summary) {
581            $form->addElement(form_makeOpenTag('span', array('class' => 'sum')));
582            if(!$media_id) $form->addElement(' – ');
583            $form->addElement('<bdi>' . htmlspecialchars($summary) . '</bdi>');
584            $form->addElement(form_makeCloseTag('span'));
585        }
586
587        $form->addElement(form_makeOpenTag('span', array('class' => 'user')));
588        $form->addElement((empty($editor))?('('.$lang['external_edit'].')'):'<bdi>'.editorinfo($editor).'</bdi>');
589        $form->addElement(form_makeCloseTag('span'));
590
591        if(isset($sizechange)) {
592            $class = 'sizechange';
593            $value = filesize_h(abs($sizechange));
594            if($sizechange > 0) {
595                $class .= ' positive';
596                $value = '+' . $value;
597            } elseif($sizechange < 0) {
598                $class .= ' negative';
599                $value = '-' . $value;
600            }
601            $form->addElement(form_makeOpenTag('span', array('class' => $class)));
602            $form->addElement($value);
603            $form->addElement(form_makeCloseTag('span'));
604        }
605
606        $form->addElement('('.$lang['current'].')');
607
608        if ($media_id) $form->addElement(form_makeCloseTag('div'));
609
610        $form->addElement(form_makeCloseTag('div'));
611        $form->addElement(form_makeCloseTag('li'));
612    }
613
614    foreach($revisions as $rev) {
615        $date = dformat($rev);
616        $info = $changelog->getRevisionInfo($rev);
617        if($media_id) {
618            $exists = file_exists(mediaFN($id, $rev));
619        } else {
620            $exists = page_exists($id, $rev);
621        }
622
623        $class = '';
624        if($info['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) {
625            $class = 'minor';
626        }
627        $form->addElement(form_makeOpenTag('li', array('class' => $class)));
628        $form->addElement(form_makeOpenTag('div', array('class' => 'li')));
629        if($exists){
630            $form->addElement(form_makeTag('input', array(
631                            'type' => 'checkbox',
632                            'name' => 'rev2[]',
633                            'value' => $rev)));
634        }else{
635            $form->addElement('<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />');
636        }
637
638        $form->addElement(form_makeOpenTag('span', array('class' => 'date')));
639        $form->addElement($date);
640        $form->addElement(form_makeCloseTag('span'));
641
642        if($exists){
643            if (!$media_id) {
644                $href = wl($id,"rev=$rev,do=diff", false, '&');
645            } else {
646                $href = media_managerURL(array('image' => $id, 'rev' => $rev, 'mediado' => 'diff'), '&');
647            }
648            $form->addElement(form_makeOpenTag('a', array(
649                            'class' => 'diff_link',
650                            'href' => $href)));
651            $form->addElement(form_makeTag('img', array(
652                            'src'    => DOKU_BASE.'lib/images/diff.png',
653                            'width'  => 15,
654                            'height' => 11,
655                            'title'  => $lang['diff'],
656                            'alt'    => $lang['diff'])));
657            $form->addElement(form_makeCloseTag('a'));
658
659            if (!$media_id) {
660                $href = wl($id,"rev=$rev",false,'&');
661            } else {
662                $href = media_managerURL(array('image' => $id, 'tab_details' => 'view', 'rev' => $rev), '&');
663            }
664            $form->addElement(form_makeOpenTag('a', array(
665                            'class' => 'wikilink1',
666                            'href' => $href)));
667            $form->addElement($display_name);
668            $form->addElement(form_makeCloseTag('a'));
669        }else{
670            $form->addElement('<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />');
671            $form->addElement($display_name);
672        }
673
674        if ($media_id) $form->addElement(form_makeOpenTag('div'));
675
676        if ($info['sum']) {
677            $form->addElement(form_makeOpenTag('span', array('class' => 'sum')));
678            if(!$media_id) $form->addElement(' – ');
679            $form->addElement('<bdi>'.htmlspecialchars($info['sum']).'</bdi>');
680            $form->addElement(form_makeCloseTag('span'));
681        }
682
683        $form->addElement(form_makeOpenTag('span', array('class' => 'user')));
684        if($info['user']){
685            $form->addElement('<bdi>'.editorinfo($info['user']).'</bdi>');
686            if(auth_ismanager()){
687                $form->addElement(' <bdo dir="ltr">('.$info['ip'].')</bdo>');
688            }
689        }else{
690            $form->addElement('<bdo dir="ltr">'.$info['ip'].'</bdo>');
691        }
692        $form->addElement(form_makeCloseTag('span'));
693
694        if(isset($info['sizechange'])) {
695            $class = 'sizechange';
696            $value = filesize_h(abs($info['sizechange']));
697            if($info['sizechange'] > 0) {
698                $class .= ' positive';
699                $value = '+' . $value;
700            } elseif($info['sizechange'] < 0) {
701                $class .= ' negative';
702                $value = '-' . $value;
703            }
704            $form->addElement(form_makeOpenTag('span', array('class' => $class)));
705            $form->addElement($value);
706            $form->addElement(form_makeCloseTag('span'));
707        }
708
709        if ($media_id) $form->addElement(form_makeCloseTag('div'));
710
711        $form->addElement(form_makeCloseTag('div'));
712        $form->addElement(form_makeCloseTag('li'));
713    }
714    $form->addElement(form_makeCloseTag('ul'));
715    if (!$media_id) {
716        $form->addElement(form_makeButton('submit', 'diff', $lang['diff2']));
717    } else {
718        $form->addHidden('mediado', 'diff');
719        $form->addElement(form_makeButton('submit', '', $lang['diff2']));
720    }
721    html_form('revisions', $form);
722
723    print '<div class="pagenav">';
724    $last = $first + $conf['recent'];
725    if ($first > 0) {
726        $first -= $conf['recent'];
727        if ($first < 0) $first = 0;
728        print '<div class="pagenav-prev">';
729        if ($media_id) {
730            print html_btn('newer',$media_id,"p",media_managerURL(array('first' => $first), '&amp;', false, true));
731        } else {
732            print html_btn('newer',$id,"p",array('do' => 'revisions', 'first' => $first));
733        }
734        print '</div>';
735    }
736    if ($hasNext) {
737        print '<div class="pagenav-next">';
738        if ($media_id) {
739            print html_btn('older',$media_id,"n",media_managerURL(array('first' => $last), '&amp;', false, true));
740        } else {
741            print html_btn('older',$id,"n",array('do' => 'revisions', 'first' => $last));
742        }
743        print '</div>';
744    }
745    print '</div>';
746
747}
748
749/**
750 * display recent changes
751 *
752 * @author Andreas Gohr <andi@splitbrain.org>
753 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
754 * @author Ben Coburn <btcoburn@silicodon.net>
755 * @author Kate Arzamastseva <pshns@ukr.net>
756 *
757 * @param int $first
758 * @param string $show_changes
759 */
760function html_recent($first=0, $show_changes='both'){
761    global $conf;
762    global $lang;
763    global $ID;
764    /* we need to get one additionally log entry to be able to
765     * decide if this is the last page or is there another one.
766     * This is the cheapest solution to get this information.
767     */
768    $flags = 0;
769    if ($show_changes == 'mediafiles' && $conf['mediarevisions']) {
770        $flags = RECENTS_MEDIA_CHANGES;
771    } elseif ($show_changes == 'pages') {
772        $flags = 0;
773    } elseif ($conf['mediarevisions']) {
774        $show_changes = 'both';
775        $flags = RECENTS_MEDIA_PAGES_MIXED;
776    }
777
778    $recents = getRecents($first,$conf['recent'] + 1,getNS($ID),$flags);
779    if(count($recents) == 0 && $first != 0){
780        $first=0;
781        $recents = getRecents($first,$conf['recent'] + 1,getNS($ID),$flags);
782    }
783    $hasNext = false;
784    if (count($recents)>$conf['recent']) {
785        $hasNext = true;
786        array_pop($recents); // remove extra log entry
787    }
788
789    print p_locale_xhtml('recent');
790
791    if (getNS($ID) != '') {
792        print '<div class="level1"><p>' . sprintf($lang['recent_global'], getNS($ID), wl('', 'do=recent')) . '</p></div>';
793    }
794
795    $form = new Doku_Form(array('id' => 'dw__recent', 'method' => 'GET', 'class' => 'changes'));
796    $form->addHidden('sectok', null);
797    $form->addHidden('do', 'recent');
798    $form->addHidden('id', $ID);
799
800    if ($conf['mediarevisions']) {
801        $form->addElement('<div class="changeType">');
802        $form->addElement(form_makeListboxField(
803                    'show_changes',
804                    array(
805                        'pages'      => $lang['pages_changes'],
806                        'mediafiles' => $lang['media_changes'],
807                        'both'       => $lang['both_changes']),
808                    $show_changes,
809                    $lang['changes_type'],
810                    '','',
811                    array('class'=>'quickselect')));
812
813        $form->addElement(form_makeButton('submit', 'recent', $lang['btn_apply']));
814        $form->addElement('</div>');
815    }
816
817    $form->addElement(form_makeOpenTag('ul'));
818
819    foreach($recents as $recent) {
820        $date = dformat($recent['date']);
821
822        $class = '';
823        if($recent['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) {
824            $class = 'minor';
825        }
826        $form->addElement(form_makeOpenTag('li', array('class' => $class)));
827        $form->addElement(form_makeOpenTag('div', array('class' => 'li')));
828
829        if (!empty($recent['media'])) {
830            $form->addElement(media_printicon($recent['id']));
831        } else {
832            $icon = DOKU_BASE.'lib/images/fileicons/file.png';
833            $form->addElement('<img src="'.$icon.'" alt="'.$recent['id'].'" class="icon" />');
834        }
835
836        $form->addElement(form_makeOpenTag('span', array('class' => 'date')));
837        $form->addElement($date);
838        $form->addElement(form_makeCloseTag('span'));
839
840        $diff = false;
841        $href = '';
842
843        if (!empty($recent['media'])) {
844            $changelog = new MediaChangeLog($recent['id']);
845            $revs = $changelog->getRevisions(0, 1);
846            $diff = (count($revs) && file_exists(mediaFN($recent['id'])));
847            if ($diff) {
848                $href = media_managerURL(array(
849                                             'tab_details' => 'history',
850                                             'mediado' => 'diff',
851                                             'image' => $recent['id'],
852                                             'ns' => getNS($recent['id'])
853                                         ), '&');
854            }
855        } else {
856            $href = wl($recent['id'],"do=diff", false, '&');
857        }
858
859        if (!empty($recent['media']) && !$diff) {
860            $form->addElement('<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />');
861        } else {
862            $form->addElement(form_makeOpenTag('a', array('class' => 'diff_link', 'href' => $href)));
863            $form->addElement(form_makeTag('img', array(
864                            'src'   => DOKU_BASE.'lib/images/diff.png',
865                            'width' => 15,
866                            'height'=> 11,
867                            'title' => $lang['diff'],
868                            'alt'   => $lang['diff']
869                            )));
870            $form->addElement(form_makeCloseTag('a'));
871        }
872
873        if (!empty($recent['media'])) {
874            $href = media_managerURL(array('tab_details' => 'history', 'image' => $recent['id'], 'ns' => getNS($recent['id'])), '&');
875        } else {
876            $href = wl($recent['id'],"do=revisions",false,'&');
877        }
878        $form->addElement(form_makeOpenTag('a', array(
879                        'class' => 'revisions_link',
880                        'href' => $href)));
881        $form->addElement(form_makeTag('img', array(
882                        'src'   => DOKU_BASE.'lib/images/history.png',
883                        'width' => 12,
884                        'height'=> 14,
885                        'title' => $lang['btn_revs'],
886                        'alt'   => $lang['btn_revs']
887                        )));
888        $form->addElement(form_makeCloseTag('a'));
889
890        if (!empty($recent['media'])) {
891            $href = media_managerURL(array('tab_details' => 'view', 'image' => $recent['id'], 'ns' => getNS($recent['id'])), '&');
892            $class = file_exists(mediaFN($recent['id'])) ? 'wikilink1' : 'wikilink2';
893            $form->addElement(form_makeOpenTag('a', array(
894                        'class' => $class,
895                        'href' => $href)));
896            $form->addElement($recent['id']);
897            $form->addElement(form_makeCloseTag('a'));
898        } else {
899            $form->addElement(html_wikilink(':' . $recent['id'], useHeading('navigation') ? null : $recent['id']));
900        }
901        $form->addElement(form_makeOpenTag('span', array('class' => 'sum')));
902        $form->addElement(' – '.htmlspecialchars($recent['sum']));
903        $form->addElement(form_makeCloseTag('span'));
904
905        $form->addElement(form_makeOpenTag('span', array('class' => 'user')));
906        if($recent['user']){
907            $form->addElement('<bdi>'.editorinfo($recent['user']).'</bdi>');
908            if(auth_ismanager()){
909                $form->addElement(' <bdo dir="ltr">('.$recent['ip'].')</bdo>');
910            }
911        }else{
912            $form->addElement('<bdo dir="ltr">'.$recent['ip'].'</bdo>');
913        }
914        $form->addElement(form_makeCloseTag('span'));
915
916        $form->addElement(form_makeCloseTag('div'));
917        $form->addElement(form_makeCloseTag('li'));
918    }
919    $form->addElement(form_makeCloseTag('ul'));
920
921    $form->addElement(form_makeOpenTag('div', array('class' => 'pagenav')));
922    $last = $first + $conf['recent'];
923    if ($first > 0) {
924        $first -= $conf['recent'];
925        if ($first < 0) $first = 0;
926        $form->addElement(form_makeOpenTag('div', array('class' => 'pagenav-prev')));
927        $form->addElement(form_makeOpenTag('button', array(
928                    'type'  => 'submit',
929                    'name'  => 'first['.$first.']',
930                    'accesskey' => 'n',
931                    'title' => $lang['btn_newer'].' [N]',
932                    'class' => 'button show'
933                    )));
934        $form->addElement($lang['btn_newer']);
935        $form->addElement(form_makeCloseTag('button'));
936        $form->addElement(form_makeCloseTag('div'));
937    }
938    if ($hasNext) {
939        $form->addElement(form_makeOpenTag('div', array('class' => 'pagenav-next')));
940        $form->addElement(form_makeOpenTag('button', array(
941                        'type'  => 'submit',
942                        'name'  => 'first['.$last.']',
943                        'accesskey' => 'p',
944                        'title' => $lang['btn_older'].' [P]',
945                        'class' => 'button show'
946                        )));
947        $form->addElement($lang['btn_older']);
948        $form->addElement(form_makeCloseTag('button'));
949        $form->addElement(form_makeCloseTag('div'));
950    }
951    $form->addElement(form_makeCloseTag('div'));
952    html_form('recent', $form);
953}
954
955/**
956 * Display page index
957 *
958 * @author Andreas Gohr <andi@splitbrain.org>
959 *
960 * @param string $ns
961 */
962function html_index($ns){
963    global $conf;
964    global $ID;
965    $ns  = cleanID($ns);
966    #fixme use appropriate function
967    if(empty($ns)){
968        $ns = dirname(str_replace(':','/',$ID));
969        if($ns == '.') $ns ='';
970    }
971    $ns  = utf8_encodeFN(str_replace(':','/',$ns));
972
973    echo p_locale_xhtml('index');
974    echo '<div id="index__tree">';
975
976    $data = array();
977    search($data,$conf['datadir'],'search_index',array('ns' => $ns));
978    echo html_buildlist($data,'idx','html_list_index','html_li_index');
979
980    echo '</div>';
981}
982
983/**
984 * Index item formatter
985 *
986 * User function for html_buildlist()
987 *
988 * @author Andreas Gohr <andi@splitbrain.org>
989 *
990 * @param array $item
991 * @return string
992 */
993function html_list_index($item){
994    global $ID, $conf;
995
996    // prevent searchbots needlessly following links
997    $nofollow = ($ID != $conf['start'] || $conf['sitemap']) ? ' rel="nofollow"' : '';
998
999    $ret = '';
1000    $base = ':'.$item['id'];
1001    $base = substr($base,strrpos($base,':')+1);
1002    if($item['type']=='d'){
1003        // FS#2766, no need for search bots to follow namespace links in the index
1004        $ret .= '<a href="'.wl($ID,'idx='.rawurlencode($item['id'])).'" title="' . $item['id'] . '" class="idx_dir"' . $nofollow . '><strong>';
1005        $ret .= $base;
1006        $ret .= '</strong></a>';
1007    }else{
1008        // default is noNSorNS($id), but we want noNS($id) when useheading is off FS#2605
1009        $ret .= html_wikilink(':'.$item['id'], useHeading('navigation') ? null : noNS($item['id']));
1010    }
1011    return $ret;
1012}
1013
1014/**
1015 * Index List item
1016 *
1017 * This user function is used in html_buildlist to build the
1018 * <li> tags for namespaces when displaying the page index
1019 * it gives different classes to opened or closed "folders"
1020 *
1021 * @author Andreas Gohr <andi@splitbrain.org>
1022 *
1023 * @param array $item
1024 * @return string html
1025 */
1026function html_li_index($item){
1027    global $INFO;
1028
1029    $class = '';
1030    $id = '';
1031
1032    if($item['type'] == "f"){
1033        // scroll to the current item
1034        if($item['id'] == $INFO['id']) {
1035            $id = ' id="scroll__here"';
1036            $class = ' bounce';
1037        }
1038        return '<li class="level'.$item['level'].$class.'" '.$id.'>';
1039    }elseif($item['open']){
1040        return '<li class="open">';
1041    }else{
1042        return '<li class="closed">';
1043    }
1044}
1045
1046/**
1047 * Default List item
1048 *
1049 * @author Andreas Gohr <andi@splitbrain.org>
1050 *
1051 * @param array $item
1052 * @return string html
1053 */
1054function html_li_default($item){
1055    return '<li class="level'.$item['level'].'">';
1056}
1057
1058/**
1059 * Build an unordered list
1060 *
1061 * Build an unordered list from the given $data array
1062 * Each item in the array has to have a 'level' property
1063 * the item itself gets printed by the given $func user
1064 * function. The second and optional function is used to
1065 * print the <li> tag. Both user function need to accept
1066 * a single item.
1067 *
1068 * Both user functions can be given as array to point to
1069 * a member of an object.
1070 *
1071 * @author Andreas Gohr <andi@splitbrain.org>
1072 *
1073 * @param array    $data  array with item arrays
1074 * @param string   $class class of ul wrapper
1075 * @param callable $func  callback to print an list item
1076 * @param callable $lifunc callback to the opening li tag
1077 * @param bool     $forcewrapper Trigger building a wrapper ul if the first level is
1078 *                               0 (we have a root object) or 1 (just the root content)
1079 * @return string html of an unordered list
1080 */
1081function html_buildlist($data,$class,$func,$lifunc='html_li_default',$forcewrapper=false){
1082    if (count($data) === 0) {
1083        return '';
1084    }
1085
1086    $start_level = $data[0]['level'];
1087    $level = $start_level;
1088    $ret   = '';
1089    $open  = 0;
1090
1091    foreach ($data as $item){
1092
1093        if( $item['level'] > $level ){
1094            //open new list
1095            for($i=0; $i<($item['level'] - $level); $i++){
1096                if ($i) $ret .= "<li class=\"clear\">";
1097                $ret .= "\n<ul class=\"$class\">\n";
1098                $open++;
1099            }
1100            $level = $item['level'];
1101
1102        }elseif( $item['level'] < $level ){
1103            //close last item
1104            $ret .= "</li>\n";
1105            while( $level > $item['level'] && $open > 0 ){
1106                //close higher lists
1107                $ret .= "</ul>\n</li>\n";
1108                $level--;
1109                $open--;
1110            }
1111        } elseif ($ret !== '') {
1112            //close previous item
1113            $ret .= "</li>\n";
1114        }
1115
1116        //print item
1117        $ret .= call_user_func($lifunc,$item);
1118        $ret .= '<div class="li">';
1119
1120        $ret .= call_user_func($func,$item);
1121        $ret .= '</div>';
1122    }
1123
1124    //close remaining items and lists
1125    $ret .= "</li>\n";
1126    while($open-- > 0) {
1127        $ret .= "</ul></li>\n";
1128    }
1129
1130    if ($forcewrapper || $start_level < 2) {
1131        // Trigger building a wrapper ul if the first level is
1132        // 0 (we have a root object) or 1 (just the root content)
1133        $ret = "\n<ul class=\"$class\">\n".$ret."</ul>\n";
1134    }
1135
1136    return $ret;
1137}
1138
1139/**
1140 * display backlinks
1141 *
1142 * @author Andreas Gohr <andi@splitbrain.org>
1143 * @author Michael Klier <chi@chimeric.de>
1144 */
1145function html_backlinks(){
1146    global $ID;
1147    global $lang;
1148
1149    print p_locale_xhtml('backlinks');
1150
1151    $data = ft_backlinks($ID);
1152
1153    if(!empty($data)) {
1154        print '<ul class="idx">';
1155        foreach($data as $blink){
1156            print '<li><div class="li">';
1157            print html_wikilink(':'.$blink,useHeading('navigation')?null:$blink);
1158            print '</div></li>';
1159        }
1160        print '</ul>';
1161    } else {
1162        print '<div class="level1"><p>' . $lang['nothingfound'] . '</p></div>';
1163    }
1164}
1165
1166/**
1167 * Get header of diff HTML
1168 *
1169 * @param string $l_rev   Left revisions
1170 * @param string $r_rev   Right revision
1171 * @param string $id      Page id, if null $ID is used
1172 * @param bool   $media   If it is for media files
1173 * @param bool   $inline  Return the header on a single line
1174 * @return string[] HTML snippets for diff header
1175 */
1176function html_diff_head($l_rev, $r_rev, $id = null, $media = false, $inline = false) {
1177    global $lang;
1178    if ($id === null) {
1179        global $ID;
1180        $id = $ID;
1181    }
1182    $head_separator = $inline ? ' ' : '<br />';
1183    $media_or_wikiFN = $media ? 'mediaFN' : 'wikiFN';
1184    $ml_or_wl = $media ? 'ml' : 'wl';
1185    $l_minor = $r_minor = '';
1186
1187    if($media) {
1188        $changelog = new MediaChangeLog($id);
1189    } else {
1190        $changelog = new PageChangeLog($id);
1191    }
1192    if(!$l_rev){
1193        $l_head = '&mdash;';
1194    }else{
1195        $l_info   = $changelog->getRevisionInfo($l_rev);
1196        if($l_info['user']){
1197            $l_user = '<bdi>'.editorinfo($l_info['user']).'</bdi>';
1198            if(auth_ismanager()) $l_user .= ' <bdo dir="ltr">('.$l_info['ip'].')</bdo>';
1199        } else {
1200            $l_user = '<bdo dir="ltr">'.$l_info['ip'].'</bdo>';
1201        }
1202        $l_user  = '<span class="user">'.$l_user.'</span>';
1203        $l_sum   = ($l_info['sum']) ? '<span class="sum"><bdi>'.hsc($l_info['sum']).'</bdi></span>' : '';
1204        if ($l_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $l_minor = 'class="minor"';
1205
1206        $l_head_title = ($media) ? dformat($l_rev) : $id.' ['.dformat($l_rev).']';
1207        $l_head = '<bdi><a class="wikilink1" href="'.$ml_or_wl($id,"rev=$l_rev").'">'.
1208        $l_head_title.'</a></bdi>'.
1209        $head_separator.$l_user.' '.$l_sum;
1210    }
1211
1212    if($r_rev){
1213        $r_info   = $changelog->getRevisionInfo($r_rev);
1214        if($r_info['user']){
1215            $r_user = '<bdi>'.editorinfo($r_info['user']).'</bdi>';
1216            if(auth_ismanager()) $r_user .= ' <bdo dir="ltr">('.$r_info['ip'].')</bdo>';
1217        } else {
1218            $r_user = '<bdo dir="ltr">'.$r_info['ip'].'</bdo>';
1219        }
1220        $r_user = '<span class="user">'.$r_user.'</span>';
1221        $r_sum  = ($r_info['sum']) ? '<span class="sum"><bdi>'.hsc($r_info['sum']).'</bdi></span>' : '';
1222        if ($r_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $r_minor = 'class="minor"';
1223
1224        $r_head_title = ($media) ? dformat($r_rev) : $id.' ['.dformat($r_rev).']';
1225        $r_head = '<bdi><a class="wikilink1" href="'.$ml_or_wl($id,"rev=$r_rev").'">'.
1226        $r_head_title.'</a></bdi>'.
1227        $head_separator.$r_user.' '.$r_sum;
1228    }elseif($_rev = @filemtime($media_or_wikiFN($id))){
1229        $_info   = $changelog->getRevisionInfo($_rev);
1230        if($_info['user']){
1231            $_user = '<bdi>'.editorinfo($_info['user']).'</bdi>';
1232            if(auth_ismanager()) $_user .= ' <bdo dir="ltr">('.$_info['ip'].')</bdo>';
1233        } else {
1234            $_user = '<bdo dir="ltr">'.$_info['ip'].'</bdo>';
1235        }
1236        $_user = '<span class="user">'.$_user.'</span>';
1237        $_sum  = ($_info['sum']) ? '<span class="sum"><bdi>'.hsc($_info['sum']).'</span></bdi>' : '';
1238        if ($_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $r_minor = 'class="minor"';
1239
1240        $r_head_title = ($media) ? dformat($_rev) : $id.' ['.dformat($_rev).']';
1241        $r_head  = '<bdi><a class="wikilink1" href="'.$ml_or_wl($id).'">'.
1242        $r_head_title.'</a></bdi> '.
1243        '('.$lang['current'].')'.
1244        $head_separator.$_user.' '.$_sum;
1245    }else{
1246        $r_head = '&mdash; ('.$lang['current'].')';
1247    }
1248
1249    return array($l_head, $r_head, $l_minor, $r_minor);
1250}
1251
1252/**
1253 * Show diff
1254 * between current page version and provided $text
1255 * or between the revisions provided via GET or POST
1256 *
1257 * @author Andreas Gohr <andi@splitbrain.org>
1258 * @param  string $text  when non-empty: compare with this text with most current version
1259 * @param  bool   $intro display the intro text
1260 * @param  string $type  type of the diff (inline or sidebyside)
1261 */
1262function html_diff($text = '', $intro = true, $type = null) {
1263    global $ID;
1264    global $REV;
1265    global $lang;
1266    global $INPUT;
1267    global $INFO;
1268    $pagelog = new PageChangeLog($ID);
1269
1270    /*
1271     * Determine diff type
1272     */
1273    if(!$type) {
1274        $type = $INPUT->str('difftype');
1275        if(empty($type)) {
1276            $type = get_doku_pref('difftype', $type);
1277            if(empty($type) && $INFO['ismobile']) {
1278                $type = 'inline';
1279            }
1280        }
1281    }
1282    if($type != 'inline') $type = 'sidebyside';
1283
1284    /*
1285     * Determine requested revision(s)
1286     */
1287    // we're trying to be clever here, revisions to compare can be either
1288    // given as rev and rev2 parameters, with rev2 being optional. Or in an
1289    // array in rev2.
1290    $rev1 = $REV;
1291
1292    $rev2 = $INPUT->ref('rev2');
1293    if(is_array($rev2)) {
1294        $rev1 = (int) $rev2[0];
1295        $rev2 = (int) $rev2[1];
1296
1297        if(!$rev1) {
1298            $rev1 = $rev2;
1299            unset($rev2);
1300        }
1301    } else {
1302        $rev2 = $INPUT->int('rev2');
1303    }
1304
1305    /*
1306     * Determine left and right revision, its texts and the header
1307     */
1308    $r_minor = '';
1309    $l_minor = '';
1310
1311    if($text) { // compare text to the most current revision
1312        $l_rev = '';
1313        $l_text = rawWiki($ID, '');
1314        $l_head = '<a class="wikilink1" href="' . wl($ID) . '">' .
1315            $ID . ' ' . dformat((int) @filemtime(wikiFN($ID))) . '</a> ' .
1316            $lang['current'];
1317
1318        $r_rev = '';
1319        $r_text = cleanText($text);
1320        $r_head = $lang['yours'];
1321    } else {
1322        if($rev1 && isset($rev2) && $rev2) { // two specific revisions wanted
1323            // make sure order is correct (older on the left)
1324            if($rev1 < $rev2) {
1325                $l_rev = $rev1;
1326                $r_rev = $rev2;
1327            } else {
1328                $l_rev = $rev2;
1329                $r_rev = $rev1;
1330            }
1331        } elseif($rev1) { // single revision given, compare to current
1332            $r_rev = '';
1333            $l_rev = $rev1;
1334        } else { // no revision was given, compare previous to current
1335            $r_rev = '';
1336            $revs = $pagelog->getRevisions(0, 1);
1337            $l_rev = $revs[0];
1338            $REV = $l_rev; // store revision back in $REV
1339        }
1340
1341        // when both revisions are empty then the page was created just now
1342        if(!$l_rev && !$r_rev) {
1343            $l_text = '';
1344        } else {
1345            $l_text = rawWiki($ID, $l_rev);
1346        }
1347        $r_text = rawWiki($ID, $r_rev);
1348
1349        list($l_head, $r_head, $l_minor, $r_minor) = html_diff_head($l_rev, $r_rev, null, false, $type == 'inline');
1350    }
1351
1352    /*
1353     * Build navigation
1354     */
1355    $l_nav = '';
1356    $r_nav = '';
1357    if(!$text) {
1358        list($l_nav, $r_nav) = html_diff_navigation($pagelog, $type, $l_rev, $r_rev);
1359    }
1360    /*
1361     * Create diff object and the formatter
1362     */
1363    $diff = new Diff(explode("\n", $l_text), explode("\n", $r_text));
1364
1365    if($type == 'inline') {
1366        $diffformatter = new InlineDiffFormatter();
1367    } else {
1368        $diffformatter = new TableDiffFormatter();
1369    }
1370    /*
1371     * Display intro
1372     */
1373    if($intro) print p_locale_xhtml('diff');
1374
1375    /*
1376     * Display type and exact reference
1377     */
1378    if(!$text) {
1379        ptln('<div class="diffoptions group">');
1380
1381
1382        $form = new Doku_Form(array('action' => wl()));
1383        $form->addHidden('id', $ID);
1384        $form->addHidden('rev2[0]', $l_rev);
1385        $form->addHidden('rev2[1]', $r_rev);
1386        $form->addHidden('do', 'diff');
1387        $form->addElement(
1388             form_makeListboxField(
1389                 'difftype',
1390                 array(
1391                     'sidebyside' => $lang['diff_side'],
1392                     'inline' => $lang['diff_inline']
1393                 ),
1394                 $type,
1395                 $lang['diff_type'],
1396                 '', '',
1397                 array('class' => 'quickselect')
1398             )
1399        );
1400        $form->addElement(form_makeButton('submit', 'diff', 'Go'));
1401        $form->printForm();
1402
1403        ptln('<p>');
1404        // link to exactly this view FS#2835
1405        echo html_diff_navigationlink($type, 'difflink', $l_rev, $r_rev ? $r_rev : $INFO['currentrev']);
1406        ptln('</p>');
1407
1408        ptln('</div>'); // .diffoptions
1409    }
1410
1411    /*
1412     * Display diff view table
1413     */
1414    ?>
1415    <div class="table">
1416    <table class="diff diff_<?php echo $type ?>">
1417
1418        <?php
1419        //navigation and header
1420        if($type == 'inline') {
1421            if(!$text) { ?>
1422                <tr>
1423                    <td class="diff-lineheader">-</td>
1424                    <td class="diffnav"><?php echo $l_nav ?></td>
1425                </tr>
1426                <tr>
1427                    <th class="diff-lineheader">-</th>
1428                    <th <?php echo $l_minor ?>>
1429                        <?php echo $l_head ?>
1430                    </th>
1431                </tr>
1432            <?php } ?>
1433            <tr>
1434                <td class="diff-lineheader">+</td>
1435                <td class="diffnav"><?php echo $r_nav ?></td>
1436            </tr>
1437            <tr>
1438                <th class="diff-lineheader">+</th>
1439                <th <?php echo $r_minor ?>>
1440                    <?php echo $r_head ?>
1441                </th>
1442            </tr>
1443        <?php } else {
1444            if(!$text) { ?>
1445                <tr>
1446                    <td colspan="2" class="diffnav"><?php echo $l_nav ?></td>
1447                    <td colspan="2" class="diffnav"><?php echo $r_nav ?></td>
1448                </tr>
1449            <?php } ?>
1450            <tr>
1451                <th colspan="2" <?php echo $l_minor ?>>
1452                    <?php echo $l_head ?>
1453                </th>
1454                <th colspan="2" <?php echo $r_minor ?>>
1455                    <?php echo $r_head ?>
1456                </th>
1457            </tr>
1458        <?php }
1459
1460        //diff view
1461        echo html_insert_softbreaks($diffformatter->format($diff)); ?>
1462
1463    </table>
1464    </div>
1465<?php
1466}
1467
1468/**
1469 * Create html for revision navigation
1470 *
1471 * @param PageChangeLog $pagelog changelog object of current page
1472 * @param string        $type    inline vs sidebyside
1473 * @param int           $l_rev   left revision timestamp
1474 * @param int           $r_rev   right revision timestamp
1475 * @return string[] html of left and right navigation elements
1476 */
1477function html_diff_navigation($pagelog, $type, $l_rev, $r_rev) {
1478    global $INFO, $ID;
1479
1480    // last timestamp is not in changelog, retrieve timestamp from metadata
1481    // note: when page is removed, the metadata timestamp is zero
1482    if(!$r_rev) {
1483        if(isset($INFO['meta']['last_change']['date'])) {
1484            $r_rev = $INFO['meta']['last_change']['date'];
1485        } else {
1486            $r_rev = 0;
1487        }
1488    }
1489
1490    //retrieve revisions with additional info
1491    list($l_revs, $r_revs) = $pagelog->getRevisionsAround($l_rev, $r_rev);
1492    $l_revisions = array();
1493    if(!$l_rev) {
1494        $l_revisions[0] = array(0, "", false); //no left revision given, add dummy
1495    }
1496    foreach($l_revs as $rev) {
1497        $info = $pagelog->getRevisionInfo($rev);
1498        $l_revisions[$rev] = array(
1499            $rev,
1500            dformat($info['date']) . ' ' . editorinfo($info['user'], true) . ' ' . $info['sum'],
1501            $r_rev ? $rev >= $r_rev : false //disable?
1502        );
1503    }
1504    $r_revisions = array();
1505    if(!$r_rev) {
1506        $r_revisions[0] = array(0, "", false); //no right revision given, add dummy
1507    }
1508    foreach($r_revs as $rev) {
1509        $info = $pagelog->getRevisionInfo($rev);
1510        $r_revisions[$rev] = array(
1511            $rev,
1512            dformat($info['date']) . ' ' . editorinfo($info['user'], true) . ' ' . $info['sum'],
1513            $rev <= $l_rev //disable?
1514        );
1515    }
1516
1517    //determine previous/next revisions
1518    $l_index = array_search($l_rev, $l_revs);
1519    $l_prev = $l_revs[$l_index + 1];
1520    $l_next = $l_revs[$l_index - 1];
1521    if($r_rev) {
1522        $r_index = array_search($r_rev, $r_revs);
1523        $r_prev = $r_revs[$r_index + 1];
1524        $r_next = $r_revs[$r_index - 1];
1525    } else {
1526        //removed page
1527        if($l_next) {
1528            $r_prev = $r_revs[0];
1529        } else {
1530            $r_prev = null;
1531        }
1532        $r_next = null;
1533    }
1534
1535    /*
1536     * Left side:
1537     */
1538    $l_nav = '';
1539    //move back
1540    if($l_prev) {
1541        $l_nav .= html_diff_navigationlink($type, 'diffbothprevrev', $l_prev, $r_prev);
1542        $l_nav .= html_diff_navigationlink($type, 'diffprevrev', $l_prev, $r_rev);
1543    }
1544    //dropdown
1545    $form = new Doku_Form(array('action' => wl()));
1546    $form->addHidden('id', $ID);
1547    $form->addHidden('difftype', $type);
1548    $form->addHidden('rev2[1]', $r_rev);
1549    $form->addHidden('do', 'diff');
1550    $form->addElement(
1551         form_makeListboxField(
1552             'rev2[0]',
1553             $l_revisions,
1554             $l_rev,
1555             '', '', '',
1556             array('class' => 'quickselect')
1557         )
1558    );
1559    $form->addElement(form_makeButton('submit', 'diff', 'Go'));
1560    $l_nav .= $form->getForm();
1561    //move forward
1562    if($l_next && ($l_next < $r_rev || !$r_rev)) {
1563        $l_nav .= html_diff_navigationlink($type, 'diffnextrev', $l_next, $r_rev);
1564    }
1565
1566    /*
1567     * Right side:
1568     */
1569    $r_nav = '';
1570    //move back
1571    if($l_rev < $r_prev) {
1572        $r_nav .= html_diff_navigationlink($type, 'diffprevrev', $l_rev, $r_prev);
1573    }
1574    //dropdown
1575    $form = new Doku_Form(array('action' => wl()));
1576    $form->addHidden('id', $ID);
1577    $form->addHidden('rev2[0]', $l_rev);
1578    $form->addHidden('difftype', $type);
1579    $form->addHidden('do', 'diff');
1580    $form->addElement(
1581         form_makeListboxField(
1582             'rev2[1]',
1583             $r_revisions,
1584             $r_rev,
1585             '', '', '',
1586             array('class' => 'quickselect')
1587         )
1588    );
1589    $form->addElement(form_makeButton('submit', 'diff', 'Go'));
1590    $r_nav .= $form->getForm();
1591    //move forward
1592    if($r_next) {
1593        if($pagelog->isCurrentRevision($r_next)) {
1594            $r_nav .= html_diff_navigationlink($type, 'difflastrev', $l_rev); //last revision is diff with current page
1595        } else {
1596            $r_nav .= html_diff_navigationlink($type, 'diffnextrev', $l_rev, $r_next);
1597        }
1598        $r_nav .= html_diff_navigationlink($type, 'diffbothnextrev', $l_next, $r_next);
1599    }
1600    return array($l_nav, $r_nav);
1601}
1602
1603/**
1604 * Create html link to a diff defined by two revisions
1605 *
1606 * @param string $difftype display type
1607 * @param string $linktype
1608 * @param int $lrev oldest revision
1609 * @param int $rrev newest revision or null for diff with current revision
1610 * @return string html of link to a diff
1611 */
1612function html_diff_navigationlink($difftype, $linktype, $lrev, $rrev = null) {
1613    global $ID, $lang;
1614    if(!$rrev) {
1615        $urlparam = array(
1616            'do' => 'diff',
1617            'rev' => $lrev,
1618            'difftype' => $difftype,
1619        );
1620    } else {
1621        $urlparam = array(
1622            'do' => 'diff',
1623            'rev2[0]' => $lrev,
1624            'rev2[1]' => $rrev,
1625            'difftype' => $difftype,
1626        );
1627    }
1628    return  '<a class="' . $linktype . '" href="' . wl($ID, $urlparam) . '" title="' . $lang[$linktype] . '">' .
1629                '<span>' . $lang[$linktype] . '</span>' .
1630            '</a>' . "\n";
1631}
1632
1633/**
1634 * Insert soft breaks in diff html
1635 *
1636 * @param string $diffhtml
1637 * @return string
1638 */
1639function html_insert_softbreaks($diffhtml) {
1640    // search the diff html string for both:
1641    // - html tags, so these can be ignored
1642    // - long strings of characters without breaking characters
1643    return preg_replace_callback('/<[^>]*>|[^<> ]{12,}/','html_softbreak_callback',$diffhtml);
1644}
1645
1646/**
1647 * callback which adds softbreaks
1648 *
1649 * @param array $match array with first the complete match
1650 * @return string the replacement
1651 */
1652function html_softbreak_callback($match){
1653    // if match is an html tag, return it intact
1654    if ($match[0]{0} == '<') return $match[0];
1655
1656    // its a long string without a breaking character,
1657    // make certain characters into breaking characters by inserting a
1658    // breaking character (zero length space, U+200B / #8203) in front them.
1659    $regex = <<< REGEX
1660(?(?=                                 # start a conditional expression with a positive look ahead ...
1661&\#?\\w{1,6};)                        # ... for html entities - we don't want to split them (ok to catch some invalid combinations)
1662&\#?\\w{1,6};                         # yes pattern - a quicker match for the html entity, since we know we have one
1663|
1664[?/,&\#;:]                            # no pattern - any other group of 'special' characters to insert a breaking character after
1665)+                                    # end conditional expression
1666REGEX;
1667
1668    return preg_replace('<'.$regex.'>xu','\0&#8203;',$match[0]);
1669}
1670
1671/**
1672 * show warning on conflict detection
1673 *
1674 * @author Andreas Gohr <andi@splitbrain.org>
1675 *
1676 * @param string $text
1677 * @param string $summary
1678 */
1679function html_conflict($text,$summary){
1680    global $ID;
1681    global $lang;
1682
1683    print p_locale_xhtml('conflict');
1684    $form = new Doku_Form(array('id' => 'dw__editform'));
1685    $form->addHidden('id', $ID);
1686    $form->addHidden('wikitext', $text);
1687    $form->addHidden('summary', $summary);
1688    $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('accesskey'=>'s')));
1689    $form->addElement(form_makeButton('submit', 'cancel', $lang['btn_cancel']));
1690    html_form('conflict', $form);
1691    print '<br /><br /><br /><br />'.NL;
1692}
1693
1694/**
1695 * Prints the global message array
1696 *
1697 * @author Andreas Gohr <andi@splitbrain.org>
1698 */
1699function html_msgarea(){
1700    global $MSG, $MSG_shown;
1701    /** @var array $MSG */
1702    // store if the global $MSG has already been shown and thus HTML output has been started
1703    $MSG_shown = true;
1704
1705    if(!isset($MSG)) return;
1706
1707    $shown = array();
1708    foreach($MSG as $msg){
1709        $hash = md5($msg['msg']);
1710        if(isset($shown[$hash])) continue; // skip double messages
1711        if(info_msg_allowed($msg)){
1712            print '<div class="'.$msg['lvl'].'">';
1713            print $msg['msg'];
1714            print '</div>';
1715        }
1716        $shown[$hash] = 1;
1717    }
1718
1719    unset($GLOBALS['MSG']);
1720}
1721
1722/**
1723 * Prints the registration form
1724 *
1725 * @author Andreas Gohr <andi@splitbrain.org>
1726 */
1727function html_register(){
1728    global $lang;
1729    global $conf;
1730    global $INPUT;
1731
1732    $base_attrs = array('size'=>50,'required'=>'required');
1733    $email_attrs = $base_attrs + array('type'=>'email','class'=>'edit');
1734
1735    print p_locale_xhtml('register');
1736    print '<div class="centeralign">'.NL;
1737    $form = new Doku_Form(array('id' => 'dw__register'));
1738    $form->startFieldset($lang['btn_register']);
1739    $form->addHidden('do', 'register');
1740    $form->addHidden('save', '1');
1741    $form->addElement(form_makeTextField('login', $INPUT->post->str('login'), $lang['user'], '', 'block', $base_attrs));
1742    if (!$conf['autopasswd']) {
1743        $form->addElement(form_makePasswordField('pass', $lang['pass'], '', 'block', $base_attrs));
1744        $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', $base_attrs));
1745    }
1746    $form->addElement(form_makeTextField('fullname', $INPUT->post->str('fullname'), $lang['fullname'], '', 'block', $base_attrs));
1747    $form->addElement(form_makeField('email','email', $INPUT->post->str('email'), $lang['email'], '', 'block', $email_attrs));
1748    $form->addElement(form_makeButton('submit', '', $lang['btn_register']));
1749    $form->endFieldset();
1750    html_form('register', $form);
1751
1752    print '</div>'.NL;
1753}
1754
1755/**
1756 * Print the update profile form
1757 *
1758 * @author Christopher Smith <chris@jalakai.co.uk>
1759 * @author Andreas Gohr <andi@splitbrain.org>
1760 */
1761function html_updateprofile(){
1762    global $lang;
1763    global $conf;
1764    global $INPUT;
1765    global $INFO;
1766    /** @var DokuWiki_Auth_Plugin $auth */
1767    global $auth;
1768
1769    print p_locale_xhtml('updateprofile');
1770    print '<div class="centeralign">'.NL;
1771
1772    $fullname = $INPUT->post->str('fullname', $INFO['userinfo']['name'], true);
1773    $email = $INPUT->post->str('email', $INFO['userinfo']['mail'], true);
1774    $form = new Doku_Form(array('id' => 'dw__register'));
1775    $form->startFieldset($lang['profile']);
1776    $form->addHidden('do', 'profile');
1777    $form->addHidden('save', '1');
1778    $form->addElement(form_makeTextField('login', $_SERVER['REMOTE_USER'], $lang['user'], '', 'block', array('size'=>'50', 'disabled'=>'disabled')));
1779    $attr = array('size'=>'50');
1780    if (!$auth->canDo('modName')) $attr['disabled'] = 'disabled';
1781    $form->addElement(form_makeTextField('fullname', $fullname, $lang['fullname'], '', 'block', $attr));
1782    $attr = array('size'=>'50', 'class'=>'edit');
1783    if (!$auth->canDo('modMail')) $attr['disabled'] = 'disabled';
1784    $form->addElement(form_makeField('email','email', $email, $lang['email'], '', 'block', $attr));
1785    $form->addElement(form_makeTag('br'));
1786    if ($auth->canDo('modPass')) {
1787        $form->addElement(form_makePasswordField('newpass', $lang['newpass'], '', 'block', array('size'=>'50')));
1788        $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
1789    }
1790    if ($conf['profileconfirm']) {
1791        $form->addElement(form_makeTag('br'));
1792        $form->addElement(form_makePasswordField('oldpass', $lang['oldpass'], '', 'block', array('size'=>'50', 'required' => 'required')));
1793    }
1794    $form->addElement(form_makeButton('submit', '', $lang['btn_save']));
1795    $form->addElement(form_makeButton('reset', '', $lang['btn_reset']));
1796
1797    $form->endFieldset();
1798    html_form('updateprofile', $form);
1799
1800    if ($auth->canDo('delUser') && actionOK('profile_delete')) {
1801        $form_profiledelete = new Doku_Form(array('id' => 'dw__profiledelete'));
1802        $form_profiledelete->startFieldset($lang['profdeleteuser']);
1803        $form_profiledelete->addHidden('do', 'profile_delete');
1804        $form_profiledelete->addHidden('delete', '1');
1805        $form_profiledelete->addElement(form_makeCheckboxField('confirm_delete', '1', $lang['profconfdelete'],'dw__confirmdelete','', array('required' => 'required')));
1806        if ($conf['profileconfirm']) {
1807            $form_profiledelete->addElement(form_makeTag('br'));
1808            $form_profiledelete->addElement(form_makePasswordField('oldpass', $lang['oldpass'], '', 'block', array('size'=>'50', 'required' => 'required')));
1809        }
1810        $form_profiledelete->addElement(form_makeButton('submit', '', $lang['btn_deleteuser']));
1811        $form_profiledelete->endFieldset();
1812
1813        html_form('profiledelete', $form_profiledelete);
1814    }
1815
1816    print '</div>'.NL;
1817}
1818
1819/**
1820 * Preprocess edit form data
1821 *
1822 * @author   Andreas Gohr <andi@splitbrain.org>
1823 *
1824 * @triggers HTML_EDITFORM_OUTPUT
1825 */
1826function html_edit(){
1827    global $INPUT;
1828    global $ID;
1829    global $REV;
1830    global $DATE;
1831    global $PRE;
1832    global $SUF;
1833    global $INFO;
1834    global $SUM;
1835    global $lang;
1836    global $conf;
1837    global $TEXT;
1838
1839    if ($INPUT->has('changecheck')) {
1840        $check = $INPUT->str('changecheck');
1841    } elseif(!$INFO['exists']){
1842        // $TEXT has been loaded from page template
1843        $check = md5('');
1844    } else {
1845        $check = md5($TEXT);
1846    }
1847    $mod = md5($TEXT) !== $check;
1848
1849    $wr = $INFO['writable'] && !$INFO['locked'];
1850    $include = 'edit';
1851    if($wr){
1852        if ($REV) $include = 'editrev';
1853    }else{
1854        // check pseudo action 'source'
1855        if(!actionOK('source')){
1856            msg('Command disabled: source',-1);
1857            return;
1858        }
1859        $include = 'read';
1860    }
1861
1862    global $license;
1863
1864    $form = new Doku_Form(array('id' => 'dw__editform'));
1865    $form->addHidden('id', $ID);
1866    $form->addHidden('rev', $REV);
1867    $form->addHidden('date', $DATE);
1868    $form->addHidden('prefix', $PRE . '.');
1869    $form->addHidden('suffix', $SUF);
1870    $form->addHidden('changecheck', $check);
1871
1872    $data = array('form' => $form,
1873                  'wr'   => $wr,
1874                  'media_manager' => true,
1875                  'target' => ($INPUT->has('target') && $wr) ? $INPUT->str('target') : 'section',
1876                  'intro_locale' => $include);
1877
1878    if ($data['target'] !== 'section') {
1879        // Only emit event if page is writable, section edit data is valid and
1880        // edit target is not section.
1881        trigger_event('HTML_EDIT_FORMSELECTION', $data, 'html_edit_form', true);
1882    } else {
1883        html_edit_form($data);
1884    }
1885    if (isset($data['intro_locale'])) {
1886        echo p_locale_xhtml($data['intro_locale']);
1887    }
1888
1889    $form->addHidden('target', $data['target']);
1890    $form->addElement(form_makeOpenTag('div', array('id'=>'wiki__editbar', 'class'=>'editBar')));
1891    $form->addElement(form_makeOpenTag('div', array('id'=>'size__ctl')));
1892    $form->addElement(form_makeCloseTag('div'));
1893    if ($wr) {
1894        $form->addElement(form_makeOpenTag('div', array('class'=>'editButtons')));
1895        $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('id'=>'edbtn__save', 'accesskey'=>'s', 'tabindex'=>'4')));
1896        $form->addElement(form_makeButton('submit', 'preview', $lang['btn_preview'], array('id'=>'edbtn__preview', 'accesskey'=>'p', 'tabindex'=>'5')));
1897        $form->addElement(form_makeButton('submit', 'draftdel', $lang['btn_cancel'], array('tabindex'=>'6')));
1898        $form->addElement(form_makeCloseTag('div'));
1899        $form->addElement(form_makeOpenTag('div', array('class'=>'summary')));
1900        $form->addElement(form_makeTextField('summary', $SUM, $lang['summary'], 'edit__summary', 'nowrap', array('size'=>'50', 'tabindex'=>'2')));
1901        $elem = html_minoredit();
1902        if ($elem) $form->addElement($elem);
1903        $form->addElement(form_makeCloseTag('div'));
1904    }
1905    $form->addElement(form_makeCloseTag('div'));
1906    if($wr && $conf['license']){
1907        $form->addElement(form_makeOpenTag('div', array('class'=>'license')));
1908        $out  = $lang['licenseok'];
1909        $out .= ' <a href="'.$license[$conf['license']]['url'].'" rel="license" class="urlextern"';
1910        if($conf['target']['extern']) $out .= ' target="'.$conf['target']['extern'].'"';
1911        $out .= '>'.$license[$conf['license']]['name'].'</a>';
1912        $form->addElement($out);
1913        $form->addElement(form_makeCloseTag('div'));
1914    }
1915
1916    if ($wr) {
1917        // sets changed to true when previewed
1918        echo '<script type="text/javascript">/*<![CDATA[*/'. NL;
1919        echo 'textChanged = ' . ($mod ? 'true' : 'false');
1920        echo '/*!]]>*/</script>' . NL;
1921    } ?>
1922    <div class="editBox" role="application">
1923
1924    <div class="toolbar group">
1925        <div id="draft__status"><?php if(!empty($INFO['draft'])) echo $lang['draftdate'].' '.dformat();?></div>
1926        <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']?>"
1927            target="_blank"><?php echo $lang['mediaselect'] ?></a><?php }?></div>
1928    </div>
1929    <?php
1930
1931    html_form('edit', $form);
1932    print '</div>'.NL;
1933}
1934
1935/**
1936 * Display the default edit form
1937 *
1938 * Is the default action for HTML_EDIT_FORMSELECTION.
1939 *
1940 * @param mixed[] $param
1941 */
1942function html_edit_form($param) {
1943    global $TEXT;
1944
1945    if ($param['target'] !== 'section') {
1946        msg('No editor for edit target ' . hsc($param['target']) . ' found.', -1);
1947    }
1948
1949    $attr = array('tabindex'=>'1');
1950    if (!$param['wr']) $attr['readonly'] = 'readonly';
1951
1952    $param['form']->addElement(form_makeWikiText($TEXT, $attr));
1953}
1954
1955/**
1956 * Adds a checkbox for minor edits for logged in users
1957 *
1958 * @author Andreas Gohr <andi@splitbrain.org>
1959 *
1960 * @return array|bool
1961 */
1962function html_minoredit(){
1963    global $conf;
1964    global $lang;
1965    global $INPUT;
1966    // minor edits are for logged in users only
1967    if(!$conf['useacl'] || !$_SERVER['REMOTE_USER']){
1968        return false;
1969    }
1970
1971    $p = array();
1972    $p['tabindex'] = 3;
1973    if($INPUT->bool('minor')) $p['checked']='checked';
1974    return form_makeCheckboxField('minor', '1', $lang['minoredit'], 'minoredit', 'nowrap', $p);
1975}
1976
1977/**
1978 * prints some debug info
1979 *
1980 * @author Andreas Gohr <andi@splitbrain.org>
1981 */
1982function html_debug(){
1983    global $conf;
1984    global $lang;
1985    /** @var DokuWiki_Auth_Plugin $auth */
1986    global $auth;
1987    global $INFO;
1988
1989    //remove sensitive data
1990    $cnf = $conf;
1991    debug_guard($cnf);
1992    $nfo = $INFO;
1993    debug_guard($nfo);
1994    $ses = $_SESSION;
1995    debug_guard($ses);
1996
1997    print '<html><body>';
1998
1999    print '<p>When reporting bugs please send all the following ';
2000    print 'output as a mail to andi@splitbrain.org ';
2001    print 'The best way to do this is to save this page in your browser</p>';
2002
2003    print '<b>$INFO:</b><pre>';
2004    print_r($nfo);
2005    print '</pre>';
2006
2007    print '<b>$_SERVER:</b><pre>';
2008    print_r($_SERVER);
2009    print '</pre>';
2010
2011    print '<b>$conf:</b><pre>';
2012    print_r($cnf);
2013    print '</pre>';
2014
2015    print '<b>DOKU_BASE:</b><pre>';
2016    print DOKU_BASE;
2017    print '</pre>';
2018
2019    print '<b>abs DOKU_BASE:</b><pre>';
2020    print DOKU_URL;
2021    print '</pre>';
2022
2023    print '<b>rel DOKU_BASE:</b><pre>';
2024    print dirname($_SERVER['PHP_SELF']).'/';
2025    print '</pre>';
2026
2027    print '<b>PHP Version:</b><pre>';
2028    print phpversion();
2029    print '</pre>';
2030
2031    print '<b>locale:</b><pre>';
2032    print setlocale(LC_ALL,0);
2033    print '</pre>';
2034
2035    print '<b>encoding:</b><pre>';
2036    print $lang['encoding'];
2037    print '</pre>';
2038
2039    if($auth){
2040        print '<b>Auth backend capabilities:</b><pre>';
2041        foreach ($auth->getCapabilities() as $cando){
2042            print '   '.str_pad($cando,16) . ' => ' . (int)$auth->canDo($cando) . NL;
2043        }
2044        print '</pre>';
2045    }
2046
2047    print '<b>$_SESSION:</b><pre>';
2048    print_r($ses);
2049    print '</pre>';
2050
2051    print '<b>Environment:</b><pre>';
2052    print_r($_ENV);
2053    print '</pre>';
2054
2055    print '<b>PHP settings:</b><pre>';
2056    $inis = ini_get_all();
2057    print_r($inis);
2058    print '</pre>';
2059
2060    if (function_exists('apache_get_version')) {
2061        $apache = array();
2062        $apache['version'] = apache_get_version();
2063
2064        if (function_exists('apache_get_modules')) {
2065            $apache['modules'] = apache_get_modules();
2066        }
2067        print '<b>Apache</b><pre>';
2068        print_r($apache);
2069        print '</pre>';
2070    }
2071
2072    print '</body></html>';
2073}
2074
2075/**
2076 * List available Administration Tasks
2077 *
2078 * @author Andreas Gohr <andi@splitbrain.org>
2079 * @author Håkan Sandell <hakan.sandell@home.se>
2080 */
2081function html_admin(){
2082    global $ID;
2083    global $INFO;
2084    global $conf;
2085    /** @var DokuWiki_Auth_Plugin $auth */
2086    global $auth;
2087
2088    // build menu of admin functions from the plugins that handle them
2089    $pluginlist = plugin_list('admin');
2090    $menu = array();
2091    foreach ($pluginlist as $p) {
2092        /** @var DokuWiki_Admin_Plugin $obj */
2093        if(($obj = plugin_load('admin',$p)) === null) continue;
2094
2095        // check permissions
2096        if($obj->forAdminOnly() && !$INFO['isadmin']) continue;
2097
2098        $menu[$p] = array('plugin' => $p,
2099                'prompt' => $obj->getMenuText($conf['lang']),
2100                'sort' => $obj->getMenuSort()
2101                );
2102    }
2103
2104    // data security check
2105    // simple check if the 'savedir' is relative and accessible when appended to DOKU_URL
2106    // it verifies either:
2107    //   'savedir' has been moved elsewhere, or
2108    //   has protection to prevent the webserver serving files from it
2109    if (substr($conf['savedir'],0,2) == './'){
2110        echo '<a style="border:none; float:right;"
2111                href="http://www.dokuwiki.org/security#web_access_security">
2112                <img src="'.DOKU_URL.$conf['savedir'].'/security.png" alt="Your data directory seems to be protected properly."
2113                onerror="this.parentNode.style.display=\'none\'" /></a>';
2114    }
2115
2116    print p_locale_xhtml('admin');
2117
2118    // Admin Tasks
2119    if($INFO['isadmin']){
2120        ptln('<ul class="admin_tasks">');
2121
2122        if($menu['usermanager'] && $auth && $auth->canDo('getUsers')){
2123            ptln('  <li class="admin_usermanager"><div class="li">'.
2124                    '<a href="'.wl($ID, array('do' => 'admin','page' => 'usermanager')).'">'.
2125                    $menu['usermanager']['prompt'].'</a></div></li>');
2126        }
2127        unset($menu['usermanager']);
2128
2129        if($menu['acl']){
2130            ptln('  <li class="admin_acl"><div class="li">'.
2131                    '<a href="'.wl($ID, array('do' => 'admin','page' => 'acl')).'">'.
2132                    $menu['acl']['prompt'].'</a></div></li>');
2133        }
2134        unset($menu['acl']);
2135
2136        if($menu['extension']){
2137            ptln('  <li class="admin_plugin"><div class="li">'.
2138                    '<a href="'.wl($ID, array('do' => 'admin','page' => 'extension')).'">'.
2139                    $menu['extension']['prompt'].'</a></div></li>');
2140        }
2141        unset($menu['extension']);
2142
2143        if($menu['config']){
2144            ptln('  <li class="admin_config"><div class="li">'.
2145                    '<a href="'.wl($ID, array('do' => 'admin','page' => 'config')).'">'.
2146                    $menu['config']['prompt'].'</a></div></li>');
2147        }
2148        unset($menu['config']);
2149
2150        if($menu['styling']){
2151            ptln('  <li class="admin_styling"><div class="li">'.
2152                '<a href="'.wl($ID, array('do' => 'admin','page' => 'styling')).'">'.
2153                $menu['styling']['prompt'].'</a></div></li>');
2154        }
2155        unset($menu['styling']);
2156    }
2157    ptln('</ul>');
2158
2159    // Manager Tasks
2160    ptln('<ul class="admin_tasks">');
2161
2162    if($menu['revert']){
2163        ptln('  <li class="admin_revert"><div class="li">'.
2164                '<a href="'.wl($ID, array('do' => 'admin','page' => 'revert')).'">'.
2165                $menu['revert']['prompt'].'</a></div></li>');
2166    }
2167    unset($menu['revert']);
2168
2169    if($menu['popularity']){
2170        ptln('  <li class="admin_popularity"><div class="li">'.
2171                '<a href="'.wl($ID, array('do' => 'admin','page' => 'popularity')).'">'.
2172                $menu['popularity']['prompt'].'</a></div></li>');
2173    }
2174    unset($menu['popularity']);
2175
2176    // print DokuWiki version:
2177    ptln('</ul>');
2178    echo '<div id="admin__version">';
2179    echo getVersion();
2180    echo '</div>';
2181
2182    // print the rest as sorted list
2183    if(count($menu)){
2184        usort($menu, 'p_sort_modes');
2185        // output the menu
2186        ptln('<div class="clearer"></div>');
2187        print p_locale_xhtml('adminplugins');
2188        ptln('<ul>');
2189        foreach ($menu as $item) {
2190            if (!$item['prompt']) continue;
2191            ptln('  <li><div class="li"><a href="'.wl($ID, 'do=admin&amp;page='.$item['plugin']).'">'.$item['prompt'].'</a></div></li>');
2192        }
2193        ptln('</ul>');
2194    }
2195}
2196
2197/**
2198 * Form to request a new password for an existing account
2199 *
2200 * @author Benoit Chesneau <benoit@bchesneau.info>
2201 * @author Andreas Gohr <gohr@cosmocode.de>
2202 */
2203function html_resendpwd() {
2204    global $lang;
2205    global $conf;
2206    global $INPUT;
2207
2208    $token = preg_replace('/[^a-f0-9]+/','',$INPUT->str('pwauth'));
2209
2210    if(!$conf['autopasswd'] && $token){
2211        print p_locale_xhtml('resetpwd');
2212        print '<div class="centeralign">'.NL;
2213        $form = new Doku_Form(array('id' => 'dw__resendpwd'));
2214        $form->startFieldset($lang['btn_resendpwd']);
2215        $form->addHidden('token', $token);
2216        $form->addHidden('do', 'resendpwd');
2217
2218        $form->addElement(form_makePasswordField('pass', $lang['pass'], '', 'block', array('size'=>'50')));
2219        $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
2220
2221        $form->addElement(form_makeButton('submit', '', $lang['btn_resendpwd']));
2222        $form->endFieldset();
2223        html_form('resendpwd', $form);
2224        print '</div>'.NL;
2225    }else{
2226        print p_locale_xhtml('resendpwd');
2227        print '<div class="centeralign">'.NL;
2228        $form = new Doku_Form(array('id' => 'dw__resendpwd'));
2229        $form->startFieldset($lang['resendpwd']);
2230        $form->addHidden('do', 'resendpwd');
2231        $form->addHidden('save', '1');
2232        $form->addElement(form_makeTag('br'));
2233        $form->addElement(form_makeTextField('login', $INPUT->post->str('login'), $lang['user'], '', 'block'));
2234        $form->addElement(form_makeTag('br'));
2235        $form->addElement(form_makeTag('br'));
2236        $form->addElement(form_makeButton('submit', '', $lang['btn_resendpwd']));
2237        $form->endFieldset();
2238        html_form('resendpwd', $form);
2239        print '</div>'.NL;
2240    }
2241}
2242
2243/**
2244 * Return the TOC rendered to XHTML
2245 *
2246 * @author Andreas Gohr <andi@splitbrain.org>
2247 *
2248 * @param array $toc
2249 * @return string html
2250 */
2251function html_TOC($toc){
2252    if(!count($toc)) return '';
2253    global $lang;
2254    $out  = '<!-- TOC START -->'.DOKU_LF;
2255    $out .= '<div id="dw__toc">'.DOKU_LF;
2256    $out .= '<h3 class="toggle">';
2257    $out .= $lang['toc'];
2258    $out .= '</h3>'.DOKU_LF;
2259    $out .= '<div>'.DOKU_LF;
2260    $out .= html_buildlist($toc,'toc','html_list_toc','html_li_default',true);
2261    $out .= '</div>'.DOKU_LF.'</div>'.DOKU_LF;
2262    $out .= '<!-- TOC END -->'.DOKU_LF;
2263    return $out;
2264}
2265
2266/**
2267 * Callback for html_buildlist
2268 *
2269 * @param array $item
2270 * @return string html
2271 */
2272function html_list_toc($item){
2273    if(isset($item['hid'])){
2274        $link = '#'.$item['hid'];
2275    }else{
2276        $link = $item['link'];
2277    }
2278
2279    return '<a href="'.$link.'">'.hsc($item['title']).'</a>';
2280}
2281
2282/**
2283 * Helper function to build TOC items
2284 *
2285 * Returns an array ready to be added to a TOC array
2286 *
2287 * @param string $link  - where to link (if $hash set to '#' it's a local anchor)
2288 * @param string $text  - what to display in the TOC
2289 * @param int    $level - nesting level
2290 * @param string $hash  - is prepended to the given $link, set blank if you want full links
2291 * @return array the toc item
2292 */
2293function html_mktocitem($link, $text, $level, $hash='#'){
2294    return  array( 'link'  => $hash.$link,
2295            'title' => $text,
2296            'type'  => 'ul',
2297            'level' => $level);
2298}
2299
2300/**
2301 * Output a Doku_Form object.
2302 * Triggers an event with the form name: HTML_{$name}FORM_OUTPUT
2303 *
2304 * @author Tom N Harris <tnharris@whoopdedo.org>
2305 *
2306 * @param string     $name The name of the form
2307 * @param Doku_Form  $form The form
2308 */
2309function html_form($name, &$form) {
2310    // Safety check in case the caller forgets.
2311    $form->endFieldset();
2312    trigger_event('HTML_'.strtoupper($name).'FORM_OUTPUT', $form, 'html_form_output', false);
2313}
2314
2315/**
2316 * Form print function.
2317 * Just calls printForm() on the data object.
2318 *
2319 * @param Doku_Form $data The form
2320 */
2321function html_form_output($data) {
2322    $data->printForm();
2323}
2324
2325/**
2326 * Embed a flash object in HTML
2327 *
2328 * This will create the needed HTML to embed a flash movie in a cross browser
2329 * compatble way using valid XHTML
2330 *
2331 * The parameters $params, $flashvars and $atts need to be associative arrays.
2332 * No escaping needs to be done for them. The alternative content *has* to be
2333 * escaped because it is used as is. If no alternative content is given
2334 * $lang['noflash'] is used.
2335 *
2336 * @author Andreas Gohr <andi@splitbrain.org>
2337 * @link   http://latrine.dgx.cz/how-to-correctly-insert-a-flash-into-xhtml
2338 *
2339 * @param string $swf      - the SWF movie to embed
2340 * @param int $width       - width of the flash movie in pixels
2341 * @param int $height      - height of the flash movie in pixels
2342 * @param array $params    - additional parameters (<param>)
2343 * @param array $flashvars - parameters to be passed in the flashvar parameter
2344 * @param array $atts      - additional attributes for the <object> tag
2345 * @param string $alt      - alternative content (is NOT automatically escaped!)
2346 * @return string         - the XHTML markup
2347 */
2348function html_flashobject($swf,$width,$height,$params=null,$flashvars=null,$atts=null,$alt=''){
2349    global $lang;
2350
2351    $out = '';
2352
2353    // prepare the object attributes
2354    if(is_null($atts)) $atts = array();
2355    $atts['width']  = (int) $width;
2356    $atts['height'] = (int) $height;
2357    if(!$atts['width'])  $atts['width']  = 425;
2358    if(!$atts['height']) $atts['height'] = 350;
2359
2360    // add object attributes for standard compliant browsers
2361    $std = $atts;
2362    $std['type'] = 'application/x-shockwave-flash';
2363    $std['data'] = $swf;
2364
2365    // add object attributes for IE
2366    $ie  = $atts;
2367    $ie['classid'] = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
2368
2369    // open object (with conditional comments)
2370    $out .= '<!--[if !IE]> -->'.NL;
2371    $out .= '<object '.buildAttributes($std).'>'.NL;
2372    $out .= '<!-- <![endif]-->'.NL;
2373    $out .= '<!--[if IE]>'.NL;
2374    $out .= '<object '.buildAttributes($ie).'>'.NL;
2375    $out .= '    <param name="movie" value="'.hsc($swf).'" />'.NL;
2376    $out .= '<!--><!-- -->'.NL;
2377
2378    // print params
2379    if(is_array($params)) foreach($params as $key => $val){
2380        $out .= '  <param name="'.hsc($key).'" value="'.hsc($val).'" />'.NL;
2381    }
2382
2383    // add flashvars
2384    if(is_array($flashvars)){
2385        $out .= '  <param name="FlashVars" value="'.buildURLparams($flashvars).'" />'.NL;
2386    }
2387
2388    // alternative content
2389    if($alt){
2390        $out .= $alt.NL;
2391    }else{
2392        $out .= $lang['noflash'].NL;
2393    }
2394
2395    // finish
2396    $out .= '</object>'.NL;
2397    $out .= '<!-- <![endif]-->'.NL;
2398
2399    return $out;
2400}
2401
2402/**
2403 * Prints HTML code for the given tab structure
2404 *
2405 * @param array  $tabs        tab structure
2406 * @param string $current_tab the current tab id
2407 */
2408function html_tabs($tabs, $current_tab = null) {
2409    echo '<ul class="tabs">'.NL;
2410
2411    foreach($tabs as $id => $tab) {
2412        html_tab($tab['href'], $tab['caption'], $id === $current_tab);
2413    }
2414
2415    echo '</ul>'.NL;
2416}
2417/**
2418 * Prints a single tab
2419 *
2420 * @author Kate Arzamastseva <pshns@ukr.net>
2421 * @author Adrian Lang <mail@adrianlang.de>
2422 *
2423 * @param string $href - tab href
2424 * @param string $caption - tab caption
2425 * @param boolean $selected - is tab selected
2426 */
2427
2428function html_tab($href, $caption, $selected=false) {
2429    $tab = '<li>';
2430    if ($selected) {
2431        $tab .= '<strong>';
2432    } else {
2433        $tab .= '<a href="' . hsc($href) . '">';
2434    }
2435    $tab .= hsc($caption)
2436         .  '</' . ($selected ? 'strong' : 'a') . '>'
2437         .  '</li>'.NL;
2438    echo $tab;
2439}
2440
2441