xref: /dokuwiki/inc/html.php (revision 551be3f95591ab473d85e2d407a6339d9134a8e0)
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    $form = new Doku_Form(array('id' => 'dw__recent', 'method' => 'GET', 'class' => 'changes'));
795    $form->addHidden('sectok', null);
796    $form->addHidden('do', 'recent');
797    $form->addHidden('id', $ID);
798
799    if ($conf['mediarevisions']) {
800        $form->addElement('<div class="changeType">');
801        $form->addElement(form_makeListboxField(
802                    'show_changes',
803                    array(
804                        'pages'      => $lang['pages_changes'],
805                        'mediafiles' => $lang['media_changes'],
806                        'both'       => $lang['both_changes']),
807                    $show_changes,
808                    $lang['changes_type'],
809                    '','',
810                    array('class'=>'quickselect')));
811
812        $form->addElement(form_makeButton('submit', 'recent', $lang['btn_apply']));
813        $form->addElement('</div>');
814    }
815
816    $form->addElement(form_makeOpenTag('ul'));
817
818    foreach($recents as $recent){
819        $date = dformat($recent['date']);
820        if ($recent['type']===DOKU_CHANGE_TYPE_MINOR_EDIT)
821            $form->addElement(form_makeOpenTag('li', array('class' => 'minor')));
822        else
823            $form->addElement(form_makeOpenTag('li'));
824
825        $form->addElement(form_makeOpenTag('div', array('class' => 'li')));
826
827        if (!empty($recent['media'])) {
828            $form->addElement(media_printicon($recent['id']));
829        } else {
830            $icon = DOKU_BASE.'lib/images/fileicons/file.png';
831            $form->addElement('<img src="'.$icon.'" alt="'.$recent['id'].'" class="icon" />');
832        }
833
834        $form->addElement(form_makeOpenTag('span', array('class' => 'date')));
835        $form->addElement($date);
836        $form->addElement(form_makeCloseTag('span'));
837
838        $diff = false;
839        $href = '';
840
841        if (!empty($recent['media'])) {
842            $changelog = new MediaChangeLog($recent['id']);
843            $revs = $changelog->getRevisions(0, 1);
844            $diff = (count($revs) && file_exists(mediaFN($recent['id'])));
845            if ($diff) {
846                $href = media_managerURL(array(
847                                             'tab_details' => 'history',
848                                             'mediado' => 'diff',
849                                             'image' => $recent['id'],
850                                             'ns' => getNS($recent['id'])
851                                         ), '&');
852            }
853        } else {
854            $href = wl($recent['id'],"do=diff", false, '&');
855        }
856
857        if (!empty($recent['media']) && !$diff) {
858            $form->addElement('<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />');
859        } else {
860            $form->addElement(form_makeOpenTag('a', array('class' => 'diff_link', 'href' => $href)));
861            $form->addElement(form_makeTag('img', array(
862                            'src'   => DOKU_BASE.'lib/images/diff.png',
863                            'width' => 15,
864                            'height'=> 11,
865                            'title' => $lang['diff'],
866                            'alt'   => $lang['diff']
867                            )));
868            $form->addElement(form_makeCloseTag('a'));
869        }
870
871        if (!empty($recent['media'])) {
872            $href = media_managerURL(array('tab_details' => 'history',
873                'image' => $recent['id'], 'ns' => getNS($recent['id'])), '&');
874        } else {
875            $href = wl($recent['id'],"do=revisions",false,'&');
876        }
877        $form->addElement(form_makeOpenTag('a', array('class' => 'revisions_link', 'href' => $href)));
878        $form->addElement(form_makeTag('img', array(
879                        'src'   => DOKU_BASE.'lib/images/history.png',
880                        'width' => 12,
881                        'height'=> 14,
882                        'title' => $lang['btn_revs'],
883                        'alt'   => $lang['btn_revs']
884                        )));
885        $form->addElement(form_makeCloseTag('a'));
886
887        if (!empty($recent['media'])) {
888            $href = media_managerURL(array('tab_details' => 'view', 'image' => $recent['id'], 'ns' => getNS($recent['id'])), '&');
889            $class = (file_exists(mediaFN($recent['id']))) ? 'wikilink1' : $class = 'wikilink2';
890            $form->addElement(form_makeOpenTag('a', array('class' => $class, 'href' => $href)));
891            $form->addElement($recent['id']);
892            $form->addElement(form_makeCloseTag('a'));
893        } else {
894            $form->addElement(html_wikilink(':'.$recent['id'],useHeading('navigation')?null:$recent['id']));
895        }
896        $form->addElement(form_makeOpenTag('span', array('class' => 'sum')));
897        $form->addElement(' – '.htmlspecialchars($recent['sum']));
898        $form->addElement(form_makeCloseTag('span'));
899
900        $form->addElement(form_makeOpenTag('span', array('class' => 'user')));
901        if($recent['user']){
902            $form->addElement('<bdi>'.editorinfo($recent['user']).'</bdi>');
903            if(auth_ismanager()){
904                $form->addElement(' <bdo dir="ltr">('.$recent['ip'].')</bdo>');
905            }
906        }else{
907            $form->addElement('<bdo dir="ltr">'.$recent['ip'].'</bdo>');
908        }
909        $form->addElement(form_makeCloseTag('span'));
910
911        $form->addElement(form_makeCloseTag('div'));
912        $form->addElement(form_makeCloseTag('li'));
913    }
914    $form->addElement(form_makeCloseTag('ul'));
915
916    $form->addElement(form_makeOpenTag('div', array('class' => 'pagenav')));
917    $last = $first + $conf['recent'];
918    if ($first > 0) {
919        $first -= $conf['recent'];
920        if ($first < 0) $first = 0;
921        $form->addElement(form_makeOpenTag('div', array('class' => 'pagenav-prev')));
922        $form->addElement(form_makeOpenTag('button', array(
923                    'type'  => 'submit',
924                    'name'  => 'first['.$first.']',
925                    'accesskey' => 'n',
926                    'title' => $lang['btn_newer'].' [N]',
927                    'class' => 'button show'
928                    )));
929        $form->addElement($lang['btn_newer']);
930        $form->addElement(form_makeCloseTag('button'));
931        $form->addElement(form_makeCloseTag('div'));
932    }
933    if ($hasNext) {
934        $form->addElement(form_makeOpenTag('div', array('class' => 'pagenav-next')));
935        $form->addElement(form_makeOpenTag('button', array(
936                        'type'  => 'submit',
937                        'name'  => 'first['.$last.']',
938                        'accesskey' => 'p',
939                        'title' => $lang['btn_older'].' [P]',
940                        'class' => 'button show'
941                        )));
942        $form->addElement($lang['btn_older']);
943        $form->addElement(form_makeCloseTag('button'));
944        $form->addElement(form_makeCloseTag('div'));
945    }
946    $form->addElement(form_makeCloseTag('div'));
947    html_form('recent', $form);
948}
949
950/**
951 * Display page index
952 *
953 * @author Andreas Gohr <andi@splitbrain.org>
954 *
955 * @param string $ns
956 */
957function html_index($ns){
958    global $conf;
959    global $ID;
960    $ns  = cleanID($ns);
961    #fixme use appropriate function
962    if(empty($ns)){
963        $ns = dirname(str_replace(':','/',$ID));
964        if($ns == '.') $ns ='';
965    }
966    $ns  = utf8_encodeFN(str_replace(':','/',$ns));
967
968    echo p_locale_xhtml('index');
969    echo '<div id="index__tree">';
970
971    $data = array();
972    search($data,$conf['datadir'],'search_index',array('ns' => $ns));
973    echo html_buildlist($data,'idx','html_list_index','html_li_index');
974
975    echo '</div>';
976}
977
978/**
979 * Index item formatter
980 *
981 * User function for html_buildlist()
982 *
983 * @author Andreas Gohr <andi@splitbrain.org>
984 *
985 * @param array $item
986 * @return string
987 */
988function html_list_index($item){
989    global $ID, $conf;
990
991    // prevent searchbots needlessly following links
992    $nofollow = ($ID != $conf['start'] || $conf['sitemap']) ? ' rel="nofollow"' : '';
993
994    $ret = '';
995    $base = ':'.$item['id'];
996    $base = substr($base,strrpos($base,':')+1);
997    if($item['type']=='d'){
998        // FS#2766, no need for search bots to follow namespace links in the index
999        $ret .= '<a href="'.wl($ID,'idx='.rawurlencode($item['id'])).'" title="' . $item['id'] . '" class="idx_dir"' . $nofollow . '><strong>';
1000        $ret .= $base;
1001        $ret .= '</strong></a>';
1002    }else{
1003        // default is noNSorNS($id), but we want noNS($id) when useheading is off FS#2605
1004        $ret .= html_wikilink(':'.$item['id'], useHeading('navigation') ? null : noNS($item['id']));
1005    }
1006    return $ret;
1007}
1008
1009/**
1010 * Index List item
1011 *
1012 * This user function is used in html_buildlist to build the
1013 * <li> tags for namespaces when displaying the page index
1014 * it gives different classes to opened or closed "folders"
1015 *
1016 * @author Andreas Gohr <andi@splitbrain.org>
1017 *
1018 * @param array $item
1019 * @return string html
1020 */
1021function html_li_index($item){
1022    global $INFO;
1023
1024    $class = '';
1025    $id = '';
1026
1027    if($item['type'] == "f"){
1028        // scroll to the current item
1029        if($item['id'] == $INFO['id']) {
1030            $id = ' id="scroll__here"';
1031            $class = ' bounce';
1032        }
1033        return '<li class="level'.$item['level'].$class.'" '.$id.'>';
1034    }elseif($item['open']){
1035        return '<li class="open">';
1036    }else{
1037        return '<li class="closed">';
1038    }
1039}
1040
1041/**
1042 * Default List item
1043 *
1044 * @author Andreas Gohr <andi@splitbrain.org>
1045 *
1046 * @param array $item
1047 * @return string html
1048 */
1049function html_li_default($item){
1050    return '<li class="level'.$item['level'].'">';
1051}
1052
1053/**
1054 * Build an unordered list
1055 *
1056 * Build an unordered list from the given $data array
1057 * Each item in the array has to have a 'level' property
1058 * the item itself gets printed by the given $func user
1059 * function. The second and optional function is used to
1060 * print the <li> tag. Both user function need to accept
1061 * a single item.
1062 *
1063 * Both user functions can be given as array to point to
1064 * a member of an object.
1065 *
1066 * @author Andreas Gohr <andi@splitbrain.org>
1067 *
1068 * @param array    $data  array with item arrays
1069 * @param string   $class class of ul wrapper
1070 * @param callable $func  callback to print an list item
1071 * @param callable $lifunc callback to the opening li tag
1072 * @param bool     $forcewrapper Trigger building a wrapper ul if the first level is
1073 *                               0 (we have a root object) or 1 (just the root content)
1074 * @return string html of an unordered list
1075 */
1076function html_buildlist($data,$class,$func,$lifunc='html_li_default',$forcewrapper=false){
1077    if (count($data) === 0) {
1078        return '';
1079    }
1080
1081    $start_level = $data[0]['level'];
1082    $level = $start_level;
1083    $ret   = '';
1084    $open  = 0;
1085
1086    foreach ($data as $item){
1087
1088        if( $item['level'] > $level ){
1089            //open new list
1090            for($i=0; $i<($item['level'] - $level); $i++){
1091                if ($i) $ret .= "<li class=\"clear\">";
1092                $ret .= "\n<ul class=\"$class\">\n";
1093                $open++;
1094            }
1095            $level = $item['level'];
1096
1097        }elseif( $item['level'] < $level ){
1098            //close last item
1099            $ret .= "</li>\n";
1100            while( $level > $item['level'] && $open > 0 ){
1101                //close higher lists
1102                $ret .= "</ul>\n</li>\n";
1103                $level--;
1104                $open--;
1105            }
1106        } elseif ($ret !== '') {
1107            //close previous item
1108            $ret .= "</li>\n";
1109        }
1110
1111        //print item
1112        $ret .= call_user_func($lifunc,$item);
1113        $ret .= '<div class="li">';
1114
1115        $ret .= call_user_func($func,$item);
1116        $ret .= '</div>';
1117    }
1118
1119    //close remaining items and lists
1120    $ret .= "</li>\n";
1121    while($open-- > 0) {
1122        $ret .= "</ul></li>\n";
1123    }
1124
1125    if ($forcewrapper || $start_level < 2) {
1126        // Trigger building a wrapper ul if the first level is
1127        // 0 (we have a root object) or 1 (just the root content)
1128        $ret = "\n<ul class=\"$class\">\n".$ret."</ul>\n";
1129    }
1130
1131    return $ret;
1132}
1133
1134/**
1135 * display backlinks
1136 *
1137 * @author Andreas Gohr <andi@splitbrain.org>
1138 * @author Michael Klier <chi@chimeric.de>
1139 */
1140function html_backlinks(){
1141    global $ID;
1142    global $lang;
1143
1144    print p_locale_xhtml('backlinks');
1145
1146    $data = ft_backlinks($ID);
1147
1148    if(!empty($data)) {
1149        print '<ul class="idx">';
1150        foreach($data as $blink){
1151            print '<li><div class="li">';
1152            print html_wikilink(':'.$blink,useHeading('navigation')?null:$blink);
1153            print '</div></li>';
1154        }
1155        print '</ul>';
1156    } else {
1157        print '<div class="level1"><p>' . $lang['nothingfound'] . '</p></div>';
1158    }
1159}
1160
1161/**
1162 * Get header of diff HTML
1163 *
1164 * @param string $l_rev   Left revisions
1165 * @param string $r_rev   Right revision
1166 * @param string $id      Page id, if null $ID is used
1167 * @param bool   $media   If it is for media files
1168 * @param bool   $inline  Return the header on a single line
1169 * @return string[] HTML snippets for diff header
1170 */
1171function html_diff_head($l_rev, $r_rev, $id = null, $media = false, $inline = false) {
1172    global $lang;
1173    if ($id === null) {
1174        global $ID;
1175        $id = $ID;
1176    }
1177    $head_separator = $inline ? ' ' : '<br />';
1178    $media_or_wikiFN = $media ? 'mediaFN' : 'wikiFN';
1179    $ml_or_wl = $media ? 'ml' : 'wl';
1180    $l_minor = $r_minor = '';
1181
1182    if($media) {
1183        $changelog = new MediaChangeLog($id);
1184    } else {
1185        $changelog = new PageChangeLog($id);
1186    }
1187    if(!$l_rev){
1188        $l_head = '&mdash;';
1189    }else{
1190        $l_info   = $changelog->getRevisionInfo($l_rev);
1191        if($l_info['user']){
1192            $l_user = '<bdi>'.editorinfo($l_info['user']).'</bdi>';
1193            if(auth_ismanager()) $l_user .= ' <bdo dir="ltr">('.$l_info['ip'].')</bdo>';
1194        } else {
1195            $l_user = '<bdo dir="ltr">'.$l_info['ip'].'</bdo>';
1196        }
1197        $l_user  = '<span class="user">'.$l_user.'</span>';
1198        $l_sum   = ($l_info['sum']) ? '<span class="sum"><bdi>'.hsc($l_info['sum']).'</bdi></span>' : '';
1199        if ($l_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $l_minor = 'class="minor"';
1200
1201        $l_head_title = ($media) ? dformat($l_rev) : $id.' ['.dformat($l_rev).']';
1202        $l_head = '<bdi><a class="wikilink1" href="'.$ml_or_wl($id,"rev=$l_rev").'">'.
1203        $l_head_title.'</a></bdi>'.
1204        $head_separator.$l_user.' '.$l_sum;
1205    }
1206
1207    if($r_rev){
1208        $r_info   = $changelog->getRevisionInfo($r_rev);
1209        if($r_info['user']){
1210            $r_user = '<bdi>'.editorinfo($r_info['user']).'</bdi>';
1211            if(auth_ismanager()) $r_user .= ' <bdo dir="ltr">('.$r_info['ip'].')</bdo>';
1212        } else {
1213            $r_user = '<bdo dir="ltr">'.$r_info['ip'].'</bdo>';
1214        }
1215        $r_user = '<span class="user">'.$r_user.'</span>';
1216        $r_sum  = ($r_info['sum']) ? '<span class="sum"><bdi>'.hsc($r_info['sum']).'</bdi></span>' : '';
1217        if ($r_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $r_minor = 'class="minor"';
1218
1219        $r_head_title = ($media) ? dformat($r_rev) : $id.' ['.dformat($r_rev).']';
1220        $r_head = '<bdi><a class="wikilink1" href="'.$ml_or_wl($id,"rev=$r_rev").'">'.
1221        $r_head_title.'</a></bdi>'.
1222        $head_separator.$r_user.' '.$r_sum;
1223    }elseif($_rev = @filemtime($media_or_wikiFN($id))){
1224        $_info   = $changelog->getRevisionInfo($_rev);
1225        if($_info['user']){
1226            $_user = '<bdi>'.editorinfo($_info['user']).'</bdi>';
1227            if(auth_ismanager()) $_user .= ' <bdo dir="ltr">('.$_info['ip'].')</bdo>';
1228        } else {
1229            $_user = '<bdo dir="ltr">'.$_info['ip'].'</bdo>';
1230        }
1231        $_user = '<span class="user">'.$_user.'</span>';
1232        $_sum  = ($_info['sum']) ? '<span class="sum"><bdi>'.hsc($_info['sum']).'</span></bdi>' : '';
1233        if ($_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $r_minor = 'class="minor"';
1234
1235        $r_head_title = ($media) ? dformat($_rev) : $id.' ['.dformat($_rev).']';
1236        $r_head  = '<bdi><a class="wikilink1" href="'.$ml_or_wl($id).'">'.
1237        $r_head_title.'</a></bdi> '.
1238        '('.$lang['current'].')'.
1239        $head_separator.$_user.' '.$_sum;
1240    }else{
1241        $r_head = '&mdash; ('.$lang['current'].')';
1242    }
1243
1244    return array($l_head, $r_head, $l_minor, $r_minor);
1245}
1246
1247/**
1248 * Show diff
1249 * between current page version and provided $text
1250 * or between the revisions provided via GET or POST
1251 *
1252 * @author Andreas Gohr <andi@splitbrain.org>
1253 * @param  string $text  when non-empty: compare with this text with most current version
1254 * @param  bool   $intro display the intro text
1255 * @param  string $type  type of the diff (inline or sidebyside)
1256 */
1257function html_diff($text = '', $intro = true, $type = null) {
1258    global $ID;
1259    global $REV;
1260    global $lang;
1261    global $INPUT;
1262    global $INFO;
1263    $pagelog = new PageChangeLog($ID);
1264
1265    /*
1266     * Determine diff type
1267     */
1268    if(!$type) {
1269        $type = $INPUT->str('difftype');
1270        if(empty($type)) {
1271            $type = get_doku_pref('difftype', $type);
1272            if(empty($type) && $INFO['ismobile']) {
1273                $type = 'inline';
1274            }
1275        }
1276    }
1277    if($type != 'inline') $type = 'sidebyside';
1278
1279    /*
1280     * Determine requested revision(s)
1281     */
1282    // we're trying to be clever here, revisions to compare can be either
1283    // given as rev and rev2 parameters, with rev2 being optional. Or in an
1284    // array in rev2.
1285    $rev1 = $REV;
1286
1287    $rev2 = $INPUT->ref('rev2');
1288    if(is_array($rev2)) {
1289        $rev1 = (int) $rev2[0];
1290        $rev2 = (int) $rev2[1];
1291
1292        if(!$rev1) {
1293            $rev1 = $rev2;
1294            unset($rev2);
1295        }
1296    } else {
1297        $rev2 = $INPUT->int('rev2');
1298    }
1299
1300    /*
1301     * Determine left and right revision, its texts and the header
1302     */
1303    $r_minor = '';
1304    $l_minor = '';
1305
1306    if($text) { // compare text to the most current revision
1307        $l_rev = '';
1308        $l_text = rawWiki($ID, '');
1309        $l_head = '<a class="wikilink1" href="' . wl($ID) . '">' .
1310            $ID . ' ' . dformat((int) @filemtime(wikiFN($ID))) . '</a> ' .
1311            $lang['current'];
1312
1313        $r_rev = '';
1314        $r_text = cleanText($text);
1315        $r_head = $lang['yours'];
1316    } else {
1317        if($rev1 && isset($rev2) && $rev2) { // two specific revisions wanted
1318            // make sure order is correct (older on the left)
1319            if($rev1 < $rev2) {
1320                $l_rev = $rev1;
1321                $r_rev = $rev2;
1322            } else {
1323                $l_rev = $rev2;
1324                $r_rev = $rev1;
1325            }
1326        } elseif($rev1) { // single revision given, compare to current
1327            $r_rev = '';
1328            $l_rev = $rev1;
1329        } else { // no revision was given, compare previous to current
1330            $r_rev = '';
1331            $revs = $pagelog->getRevisions(0, 1);
1332            $l_rev = $revs[0];
1333            $REV = $l_rev; // store revision back in $REV
1334        }
1335
1336        // when both revisions are empty then the page was created just now
1337        if(!$l_rev && !$r_rev) {
1338            $l_text = '';
1339        } else {
1340            $l_text = rawWiki($ID, $l_rev);
1341        }
1342        $r_text = rawWiki($ID, $r_rev);
1343
1344        list($l_head, $r_head, $l_minor, $r_minor) = html_diff_head($l_rev, $r_rev, null, false, $type == 'inline');
1345    }
1346
1347    /*
1348     * Build navigation
1349     */
1350    $l_nav = '';
1351    $r_nav = '';
1352    if(!$text) {
1353        list($l_nav, $r_nav) = html_diff_navigation($pagelog, $type, $l_rev, $r_rev);
1354    }
1355    /*
1356     * Create diff object and the formatter
1357     */
1358    $diff = new Diff(explode("\n", $l_text), explode("\n", $r_text));
1359
1360    if($type == 'inline') {
1361        $diffformatter = new InlineDiffFormatter();
1362    } else {
1363        $diffformatter = new TableDiffFormatter();
1364    }
1365    /*
1366     * Display intro
1367     */
1368    if($intro) print p_locale_xhtml('diff');
1369
1370    /*
1371     * Display type and exact reference
1372     */
1373    if(!$text) {
1374        ptln('<div class="diffoptions group">');
1375
1376
1377        $form = new Doku_Form(array('action' => wl()));
1378        $form->addHidden('id', $ID);
1379        $form->addHidden('rev2[0]', $l_rev);
1380        $form->addHidden('rev2[1]', $r_rev);
1381        $form->addHidden('do', 'diff');
1382        $form->addElement(
1383             form_makeListboxField(
1384                 'difftype',
1385                 array(
1386                     'sidebyside' => $lang['diff_side'],
1387                     'inline' => $lang['diff_inline']
1388                 ),
1389                 $type,
1390                 $lang['diff_type'],
1391                 '', '',
1392                 array('class' => 'quickselect')
1393             )
1394        );
1395        $form->addElement(form_makeButton('submit', 'diff', 'Go'));
1396        $form->printForm();
1397
1398        ptln('<p>');
1399        // link to exactly this view FS#2835
1400        echo html_diff_navigationlink($type, 'difflink', $l_rev, $r_rev ? $r_rev : $INFO['currentrev']);
1401        ptln('</p>');
1402
1403        ptln('</div>'); // .diffoptions
1404    }
1405
1406    /*
1407     * Display diff view table
1408     */
1409    ?>
1410    <div class="table">
1411    <table class="diff diff_<?php echo $type ?>">
1412
1413        <?php
1414        //navigation and header
1415        if($type == 'inline') {
1416            if(!$text) { ?>
1417                <tr>
1418                    <td class="diff-lineheader">-</td>
1419                    <td class="diffnav"><?php echo $l_nav ?></td>
1420                </tr>
1421                <tr>
1422                    <th class="diff-lineheader">-</th>
1423                    <th <?php echo $l_minor ?>>
1424                        <?php echo $l_head ?>
1425                    </th>
1426                </tr>
1427            <?php } ?>
1428            <tr>
1429                <td class="diff-lineheader">+</td>
1430                <td class="diffnav"><?php echo $r_nav ?></td>
1431            </tr>
1432            <tr>
1433                <th class="diff-lineheader">+</th>
1434                <th <?php echo $r_minor ?>>
1435                    <?php echo $r_head ?>
1436                </th>
1437            </tr>
1438        <?php } else {
1439            if(!$text) { ?>
1440                <tr>
1441                    <td colspan="2" class="diffnav"><?php echo $l_nav ?></td>
1442                    <td colspan="2" class="diffnav"><?php echo $r_nav ?></td>
1443                </tr>
1444            <?php } ?>
1445            <tr>
1446                <th colspan="2" <?php echo $l_minor ?>>
1447                    <?php echo $l_head ?>
1448                </th>
1449                <th colspan="2" <?php echo $r_minor ?>>
1450                    <?php echo $r_head ?>
1451                </th>
1452            </tr>
1453        <?php }
1454
1455        //diff view
1456        echo html_insert_softbreaks($diffformatter->format($diff)); ?>
1457
1458    </table>
1459    </div>
1460<?php
1461}
1462
1463/**
1464 * Create html for revision navigation
1465 *
1466 * @param PageChangeLog $pagelog changelog object of current page
1467 * @param string        $type    inline vs sidebyside
1468 * @param int           $l_rev   left revision timestamp
1469 * @param int           $r_rev   right revision timestamp
1470 * @return string[] html of left and right navigation elements
1471 */
1472function html_diff_navigation($pagelog, $type, $l_rev, $r_rev) {
1473    global $INFO, $ID;
1474
1475    // last timestamp is not in changelog, retrieve timestamp from metadata
1476    // note: when page is removed, the metadata timestamp is zero
1477    if(!$r_rev) {
1478        if(isset($INFO['meta']['last_change']['date'])) {
1479            $r_rev = $INFO['meta']['last_change']['date'];
1480        } else {
1481            $r_rev = 0;
1482        }
1483    }
1484
1485    //retrieve revisions with additional info
1486    list($l_revs, $r_revs) = $pagelog->getRevisionsAround($l_rev, $r_rev);
1487    $l_revisions = array();
1488    if(!$l_rev) {
1489        $l_revisions[0] = array(0, "", false); //no left revision given, add dummy
1490    }
1491    foreach($l_revs as $rev) {
1492        $info = $pagelog->getRevisionInfo($rev);
1493        $l_revisions[$rev] = array(
1494            $rev,
1495            dformat($info['date']) . ' ' . editorinfo($info['user'], true) . ' ' . $info['sum'],
1496            $r_rev ? $rev >= $r_rev : false //disable?
1497        );
1498    }
1499    $r_revisions = array();
1500    if(!$r_rev) {
1501        $r_revisions[0] = array(0, "", false); //no right revision given, add dummy
1502    }
1503    foreach($r_revs as $rev) {
1504        $info = $pagelog->getRevisionInfo($rev);
1505        $r_revisions[$rev] = array(
1506            $rev,
1507            dformat($info['date']) . ' ' . editorinfo($info['user'], true) . ' ' . $info['sum'],
1508            $rev <= $l_rev //disable?
1509        );
1510    }
1511
1512    //determine previous/next revisions
1513    $l_index = array_search($l_rev, $l_revs);
1514    $l_prev = $l_revs[$l_index + 1];
1515    $l_next = $l_revs[$l_index - 1];
1516    if($r_rev) {
1517        $r_index = array_search($r_rev, $r_revs);
1518        $r_prev = $r_revs[$r_index + 1];
1519        $r_next = $r_revs[$r_index - 1];
1520    } else {
1521        //removed page
1522        if($l_next) {
1523            $r_prev = $r_revs[0];
1524        } else {
1525            $r_prev = null;
1526        }
1527        $r_next = null;
1528    }
1529
1530    /*
1531     * Left side:
1532     */
1533    $l_nav = '';
1534    //move back
1535    if($l_prev) {
1536        $l_nav .= html_diff_navigationlink($type, 'diffbothprevrev', $l_prev, $r_prev);
1537        $l_nav .= html_diff_navigationlink($type, 'diffprevrev', $l_prev, $r_rev);
1538    }
1539    //dropdown
1540    $form = new Doku_Form(array('action' => wl()));
1541    $form->addHidden('id', $ID);
1542    $form->addHidden('difftype', $type);
1543    $form->addHidden('rev2[1]', $r_rev);
1544    $form->addHidden('do', 'diff');
1545    $form->addElement(
1546         form_makeListboxField(
1547             'rev2[0]',
1548             $l_revisions,
1549             $l_rev,
1550             '', '', '',
1551             array('class' => 'quickselect')
1552         )
1553    );
1554    $form->addElement(form_makeButton('submit', 'diff', 'Go'));
1555    $l_nav .= $form->getForm();
1556    //move forward
1557    if($l_next && ($l_next < $r_rev || !$r_rev)) {
1558        $l_nav .= html_diff_navigationlink($type, 'diffnextrev', $l_next, $r_rev);
1559    }
1560
1561    /*
1562     * Right side:
1563     */
1564    $r_nav = '';
1565    //move back
1566    if($l_rev < $r_prev) {
1567        $r_nav .= html_diff_navigationlink($type, 'diffprevrev', $l_rev, $r_prev);
1568    }
1569    //dropdown
1570    $form = new Doku_Form(array('action' => wl()));
1571    $form->addHidden('id', $ID);
1572    $form->addHidden('rev2[0]', $l_rev);
1573    $form->addHidden('difftype', $type);
1574    $form->addHidden('do', 'diff');
1575    $form->addElement(
1576         form_makeListboxField(
1577             'rev2[1]',
1578             $r_revisions,
1579             $r_rev,
1580             '', '', '',
1581             array('class' => 'quickselect')
1582         )
1583    );
1584    $form->addElement(form_makeButton('submit', 'diff', 'Go'));
1585    $r_nav .= $form->getForm();
1586    //move forward
1587    if($r_next) {
1588        if($pagelog->isCurrentRevision($r_next)) {
1589            $r_nav .= html_diff_navigationlink($type, 'difflastrev', $l_rev); //last revision is diff with current page
1590        } else {
1591            $r_nav .= html_diff_navigationlink($type, 'diffnextrev', $l_rev, $r_next);
1592        }
1593        $r_nav .= html_diff_navigationlink($type, 'diffbothnextrev', $l_next, $r_next);
1594    }
1595    return array($l_nav, $r_nav);
1596}
1597
1598/**
1599 * Create html link to a diff defined by two revisions
1600 *
1601 * @param string $difftype display type
1602 * @param string $linktype
1603 * @param int $lrev oldest revision
1604 * @param int $rrev newest revision or null for diff with current revision
1605 * @return string html of link to a diff
1606 */
1607function html_diff_navigationlink($difftype, $linktype, $lrev, $rrev = null) {
1608    global $ID, $lang;
1609    if(!$rrev) {
1610        $urlparam = array(
1611            'do' => 'diff',
1612            'rev' => $lrev,
1613            'difftype' => $difftype,
1614        );
1615    } else {
1616        $urlparam = array(
1617            'do' => 'diff',
1618            'rev2[0]' => $lrev,
1619            'rev2[1]' => $rrev,
1620            'difftype' => $difftype,
1621        );
1622    }
1623    return  '<a class="' . $linktype . '" href="' . wl($ID, $urlparam) . '" title="' . $lang[$linktype] . '">' .
1624                '<span>' . $lang[$linktype] . '</span>' .
1625            '</a>' . "\n";
1626}
1627
1628/**
1629 * Insert soft breaks in diff html
1630 *
1631 * @param string $diffhtml
1632 * @return string
1633 */
1634function html_insert_softbreaks($diffhtml) {
1635    // search the diff html string for both:
1636    // - html tags, so these can be ignored
1637    // - long strings of characters without breaking characters
1638    return preg_replace_callback('/<[^>]*>|[^<> ]{12,}/','html_softbreak_callback',$diffhtml);
1639}
1640
1641/**
1642 * callback which adds softbreaks
1643 *
1644 * @param array $match array with first the complete match
1645 * @return string the replacement
1646 */
1647function html_softbreak_callback($match){
1648    // if match is an html tag, return it intact
1649    if ($match[0]{0} == '<') return $match[0];
1650
1651    // its a long string without a breaking character,
1652    // make certain characters into breaking characters by inserting a
1653    // breaking character (zero length space, U+200B / #8203) in front them.
1654    $regex = <<< REGEX
1655(?(?=                                 # start a conditional expression with a positive look ahead ...
1656&\#?\\w{1,6};)                        # ... for html entities - we don't want to split them (ok to catch some invalid combinations)
1657&\#?\\w{1,6};                         # yes pattern - a quicker match for the html entity, since we know we have one
1658|
1659[?/,&\#;:]                            # no pattern - any other group of 'special' characters to insert a breaking character after
1660)+                                    # end conditional expression
1661REGEX;
1662
1663    return preg_replace('<'.$regex.'>xu','\0&#8203;',$match[0]);
1664}
1665
1666/**
1667 * show warning on conflict detection
1668 *
1669 * @author Andreas Gohr <andi@splitbrain.org>
1670 *
1671 * @param string $text
1672 * @param string $summary
1673 */
1674function html_conflict($text,$summary){
1675    global $ID;
1676    global $lang;
1677
1678    print p_locale_xhtml('conflict');
1679    $form = new Doku_Form(array('id' => 'dw__editform'));
1680    $form->addHidden('id', $ID);
1681    $form->addHidden('wikitext', $text);
1682    $form->addHidden('summary', $summary);
1683    $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('accesskey'=>'s')));
1684    $form->addElement(form_makeButton('submit', 'cancel', $lang['btn_cancel']));
1685    html_form('conflict', $form);
1686    print '<br /><br /><br /><br />'.NL;
1687}
1688
1689/**
1690 * Prints the global message array
1691 *
1692 * @author Andreas Gohr <andi@splitbrain.org>
1693 */
1694function html_msgarea(){
1695    global $MSG, $MSG_shown;
1696    /** @var array $MSG */
1697    // store if the global $MSG has already been shown and thus HTML output has been started
1698    $MSG_shown = true;
1699
1700    if(!isset($MSG)) return;
1701
1702    $shown = array();
1703    foreach($MSG as $msg){
1704        $hash = md5($msg['msg']);
1705        if(isset($shown[$hash])) continue; // skip double messages
1706        if(info_msg_allowed($msg)){
1707            print '<div class="'.$msg['lvl'].'">';
1708            print $msg['msg'];
1709            print '</div>';
1710        }
1711        $shown[$hash] = 1;
1712    }
1713
1714    unset($GLOBALS['MSG']);
1715}
1716
1717/**
1718 * Prints the registration form
1719 *
1720 * @author Andreas Gohr <andi@splitbrain.org>
1721 */
1722function html_register(){
1723    global $lang;
1724    global $conf;
1725    global $INPUT;
1726
1727    $base_attrs = array('size'=>50,'required'=>'required');
1728    $email_attrs = $base_attrs + array('type'=>'email','class'=>'edit');
1729
1730    print p_locale_xhtml('register');
1731    print '<div class="centeralign">'.NL;
1732    $form = new Doku_Form(array('id' => 'dw__register'));
1733    $form->startFieldset($lang['btn_register']);
1734    $form->addHidden('do', 'register');
1735    $form->addHidden('save', '1');
1736    $form->addElement(form_makeTextField('login', $INPUT->post->str('login'), $lang['user'], '', 'block', $base_attrs));
1737    if (!$conf['autopasswd']) {
1738        $form->addElement(form_makePasswordField('pass', $lang['pass'], '', 'block', $base_attrs));
1739        $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', $base_attrs));
1740    }
1741    $form->addElement(form_makeTextField('fullname', $INPUT->post->str('fullname'), $lang['fullname'], '', 'block', $base_attrs));
1742    $form->addElement(form_makeField('email','email', $INPUT->post->str('email'), $lang['email'], '', 'block', $email_attrs));
1743    $form->addElement(form_makeButton('submit', '', $lang['btn_register']));
1744    $form->endFieldset();
1745    html_form('register', $form);
1746
1747    print '</div>'.NL;
1748}
1749
1750/**
1751 * Print the update profile form
1752 *
1753 * @author Christopher Smith <chris@jalakai.co.uk>
1754 * @author Andreas Gohr <andi@splitbrain.org>
1755 */
1756function html_updateprofile(){
1757    global $lang;
1758    global $conf;
1759    global $INPUT;
1760    global $INFO;
1761    /** @var DokuWiki_Auth_Plugin $auth */
1762    global $auth;
1763
1764    print p_locale_xhtml('updateprofile');
1765    print '<div class="centeralign">'.NL;
1766
1767    $fullname = $INPUT->post->str('fullname', $INFO['userinfo']['name'], true);
1768    $email = $INPUT->post->str('email', $INFO['userinfo']['mail'], true);
1769    $form = new Doku_Form(array('id' => 'dw__register'));
1770    $form->startFieldset($lang['profile']);
1771    $form->addHidden('do', 'profile');
1772    $form->addHidden('save', '1');
1773    $form->addElement(form_makeTextField('login', $_SERVER['REMOTE_USER'], $lang['user'], '', 'block', array('size'=>'50', 'disabled'=>'disabled')));
1774    $attr = array('size'=>'50');
1775    if (!$auth->canDo('modName')) $attr['disabled'] = 'disabled';
1776    $form->addElement(form_makeTextField('fullname', $fullname, $lang['fullname'], '', 'block', $attr));
1777    $attr = array('size'=>'50', 'class'=>'edit');
1778    if (!$auth->canDo('modMail')) $attr['disabled'] = 'disabled';
1779    $form->addElement(form_makeField('email','email', $email, $lang['email'], '', 'block', $attr));
1780    $form->addElement(form_makeTag('br'));
1781    if ($auth->canDo('modPass')) {
1782        $form->addElement(form_makePasswordField('newpass', $lang['newpass'], '', 'block', array('size'=>'50')));
1783        $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
1784    }
1785    if ($conf['profileconfirm']) {
1786        $form->addElement(form_makeTag('br'));
1787        $form->addElement(form_makePasswordField('oldpass', $lang['oldpass'], '', 'block', array('size'=>'50', 'required' => 'required')));
1788    }
1789    $form->addElement(form_makeButton('submit', '', $lang['btn_save']));
1790    $form->addElement(form_makeButton('reset', '', $lang['btn_reset']));
1791
1792    $form->endFieldset();
1793    html_form('updateprofile', $form);
1794
1795    if ($auth->canDo('delUser') && actionOK('profile_delete')) {
1796        $form_profiledelete = new Doku_Form(array('id' => 'dw__profiledelete'));
1797        $form_profiledelete->startFieldset($lang['profdeleteuser']);
1798        $form_profiledelete->addHidden('do', 'profile_delete');
1799        $form_profiledelete->addHidden('delete', '1');
1800        $form_profiledelete->addElement(form_makeCheckboxField('confirm_delete', '1', $lang['profconfdelete'],'dw__confirmdelete','', array('required' => 'required')));
1801        if ($conf['profileconfirm']) {
1802            $form_profiledelete->addElement(form_makeTag('br'));
1803            $form_profiledelete->addElement(form_makePasswordField('oldpass', $lang['oldpass'], '', 'block', array('size'=>'50', 'required' => 'required')));
1804        }
1805        $form_profiledelete->addElement(form_makeButton('submit', '', $lang['btn_deleteuser']));
1806        $form_profiledelete->endFieldset();
1807
1808        html_form('profiledelete', $form_profiledelete);
1809    }
1810
1811    print '</div>'.NL;
1812}
1813
1814/**
1815 * Preprocess edit form data
1816 *
1817 * @author   Andreas Gohr <andi@splitbrain.org>
1818 *
1819 * @triggers HTML_EDITFORM_OUTPUT
1820 */
1821function html_edit(){
1822    global $INPUT;
1823    global $ID;
1824    global $REV;
1825    global $DATE;
1826    global $PRE;
1827    global $SUF;
1828    global $INFO;
1829    global $SUM;
1830    global $lang;
1831    global $conf;
1832    global $TEXT;
1833
1834    if ($INPUT->has('changecheck')) {
1835        $check = $INPUT->str('changecheck');
1836    } elseif(!$INFO['exists']){
1837        // $TEXT has been loaded from page template
1838        $check = md5('');
1839    } else {
1840        $check = md5($TEXT);
1841    }
1842    $mod = md5($TEXT) !== $check;
1843
1844    $wr = $INFO['writable'] && !$INFO['locked'];
1845    $include = 'edit';
1846    if($wr){
1847        if ($REV) $include = 'editrev';
1848    }else{
1849        // check pseudo action 'source'
1850        if(!actionOK('source')){
1851            msg('Command disabled: source',-1);
1852            return;
1853        }
1854        $include = 'read';
1855    }
1856
1857    global $license;
1858
1859    $form = new Doku_Form(array('id' => 'dw__editform'));
1860    $form->addHidden('id', $ID);
1861    $form->addHidden('rev', $REV);
1862    $form->addHidden('date', $DATE);
1863    $form->addHidden('prefix', $PRE . '.');
1864    $form->addHidden('suffix', $SUF);
1865    $form->addHidden('changecheck', $check);
1866
1867    $data = array('form' => $form,
1868                  'wr'   => $wr,
1869                  'media_manager' => true,
1870                  'target' => ($INPUT->has('target') && $wr) ? $INPUT->str('target') : 'section',
1871                  'intro_locale' => $include);
1872
1873    if ($data['target'] !== 'section') {
1874        // Only emit event if page is writable, section edit data is valid and
1875        // edit target is not section.
1876        trigger_event('HTML_EDIT_FORMSELECTION', $data, 'html_edit_form', true);
1877    } else {
1878        html_edit_form($data);
1879    }
1880    if (isset($data['intro_locale'])) {
1881        echo p_locale_xhtml($data['intro_locale']);
1882    }
1883
1884    $form->addHidden('target', $data['target']);
1885    $form->addElement(form_makeOpenTag('div', array('id'=>'wiki__editbar', 'class'=>'editBar')));
1886    $form->addElement(form_makeOpenTag('div', array('id'=>'size__ctl')));
1887    $form->addElement(form_makeCloseTag('div'));
1888    if ($wr) {
1889        $form->addElement(form_makeOpenTag('div', array('class'=>'editButtons')));
1890        $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('id'=>'edbtn__save', 'accesskey'=>'s', 'tabindex'=>'4')));
1891        $form->addElement(form_makeButton('submit', 'preview', $lang['btn_preview'], array('id'=>'edbtn__preview', 'accesskey'=>'p', 'tabindex'=>'5')));
1892        $form->addElement(form_makeButton('submit', 'draftdel', $lang['btn_cancel'], array('tabindex'=>'6')));
1893        $form->addElement(form_makeCloseTag('div'));
1894        $form->addElement(form_makeOpenTag('div', array('class'=>'summary')));
1895        $form->addElement(form_makeTextField('summary', $SUM, $lang['summary'], 'edit__summary', 'nowrap', array('size'=>'50', 'tabindex'=>'2')));
1896        $elem = html_minoredit();
1897        if ($elem) $form->addElement($elem);
1898        $form->addElement(form_makeCloseTag('div'));
1899    }
1900    $form->addElement(form_makeCloseTag('div'));
1901    if($wr && $conf['license']){
1902        $form->addElement(form_makeOpenTag('div', array('class'=>'license')));
1903        $out  = $lang['licenseok'];
1904        $out .= ' <a href="'.$license[$conf['license']]['url'].'" rel="license" class="urlextern"';
1905        if($conf['target']['extern']) $out .= ' target="'.$conf['target']['extern'].'"';
1906        $out .= '>'.$license[$conf['license']]['name'].'</a>';
1907        $form->addElement($out);
1908        $form->addElement(form_makeCloseTag('div'));
1909    }
1910
1911    if ($wr) {
1912        // sets changed to true when previewed
1913        echo '<script type="text/javascript">/*<![CDATA[*/'. NL;
1914        echo 'textChanged = ' . ($mod ? 'true' : 'false');
1915        echo '/*!]]>*/</script>' . NL;
1916    } ?>
1917    <div class="editBox" role="application">
1918
1919    <div class="toolbar group">
1920        <div id="draft__status"><?php if(!empty($INFO['draft'])) echo $lang['draftdate'].' '.dformat();?></div>
1921        <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']?>"
1922            target="_blank"><?php echo $lang['mediaselect'] ?></a><?php }?></div>
1923    </div>
1924    <?php
1925
1926    html_form('edit', $form);
1927    print '</div>'.NL;
1928}
1929
1930/**
1931 * Display the default edit form
1932 *
1933 * Is the default action for HTML_EDIT_FORMSELECTION.
1934 *
1935 * @param mixed[] $param
1936 */
1937function html_edit_form($param) {
1938    global $TEXT;
1939
1940    if ($param['target'] !== 'section') {
1941        msg('No editor for edit target ' . hsc($param['target']) . ' found.', -1);
1942    }
1943
1944    $attr = array('tabindex'=>'1');
1945    if (!$param['wr']) $attr['readonly'] = 'readonly';
1946
1947    $param['form']->addElement(form_makeWikiText($TEXT, $attr));
1948}
1949
1950/**
1951 * Adds a checkbox for minor edits for logged in users
1952 *
1953 * @author Andreas Gohr <andi@splitbrain.org>
1954 *
1955 * @return array|bool
1956 */
1957function html_minoredit(){
1958    global $conf;
1959    global $lang;
1960    global $INPUT;
1961    // minor edits are for logged in users only
1962    if(!$conf['useacl'] || !$_SERVER['REMOTE_USER']){
1963        return false;
1964    }
1965
1966    $p = array();
1967    $p['tabindex'] = 3;
1968    if($INPUT->bool('minor')) $p['checked']='checked';
1969    return form_makeCheckboxField('minor', '1', $lang['minoredit'], 'minoredit', 'nowrap', $p);
1970}
1971
1972/**
1973 * prints some debug info
1974 *
1975 * @author Andreas Gohr <andi@splitbrain.org>
1976 */
1977function html_debug(){
1978    global $conf;
1979    global $lang;
1980    /** @var DokuWiki_Auth_Plugin $auth */
1981    global $auth;
1982    global $INFO;
1983
1984    //remove sensitive data
1985    $cnf = $conf;
1986    debug_guard($cnf);
1987    $nfo = $INFO;
1988    debug_guard($nfo);
1989    $ses = $_SESSION;
1990    debug_guard($ses);
1991
1992    print '<html><body>';
1993
1994    print '<p>When reporting bugs please send all the following ';
1995    print 'output as a mail to andi@splitbrain.org ';
1996    print 'The best way to do this is to save this page in your browser</p>';
1997
1998    print '<b>$INFO:</b><pre>';
1999    print_r($nfo);
2000    print '</pre>';
2001
2002    print '<b>$_SERVER:</b><pre>';
2003    print_r($_SERVER);
2004    print '</pre>';
2005
2006    print '<b>$conf:</b><pre>';
2007    print_r($cnf);
2008    print '</pre>';
2009
2010    print '<b>DOKU_BASE:</b><pre>';
2011    print DOKU_BASE;
2012    print '</pre>';
2013
2014    print '<b>abs DOKU_BASE:</b><pre>';
2015    print DOKU_URL;
2016    print '</pre>';
2017
2018    print '<b>rel DOKU_BASE:</b><pre>';
2019    print dirname($_SERVER['PHP_SELF']).'/';
2020    print '</pre>';
2021
2022    print '<b>PHP Version:</b><pre>';
2023    print phpversion();
2024    print '</pre>';
2025
2026    print '<b>locale:</b><pre>';
2027    print setlocale(LC_ALL,0);
2028    print '</pre>';
2029
2030    print '<b>encoding:</b><pre>';
2031    print $lang['encoding'];
2032    print '</pre>';
2033
2034    if($auth){
2035        print '<b>Auth backend capabilities:</b><pre>';
2036        foreach ($auth->getCapabilities() as $cando){
2037            print '   '.str_pad($cando,16) . ' => ' . (int)$auth->canDo($cando) . NL;
2038        }
2039        print '</pre>';
2040    }
2041
2042    print '<b>$_SESSION:</b><pre>';
2043    print_r($ses);
2044    print '</pre>';
2045
2046    print '<b>Environment:</b><pre>';
2047    print_r($_ENV);
2048    print '</pre>';
2049
2050    print '<b>PHP settings:</b><pre>';
2051    $inis = ini_get_all();
2052    print_r($inis);
2053    print '</pre>';
2054
2055    if (function_exists('apache_get_version')) {
2056        $apache = array();
2057        $apache['version'] = apache_get_version();
2058
2059        if (function_exists('apache_get_modules')) {
2060            $apache['modules'] = apache_get_modules();
2061        }
2062        print '<b>Apache</b><pre>';
2063        print_r($apache);
2064        print '</pre>';
2065    }
2066
2067    print '</body></html>';
2068}
2069
2070/**
2071 * List available Administration Tasks
2072 *
2073 * @author Andreas Gohr <andi@splitbrain.org>
2074 * @author Håkan Sandell <hakan.sandell@home.se>
2075 */
2076function html_admin(){
2077    global $ID;
2078    global $INFO;
2079    global $conf;
2080    /** @var DokuWiki_Auth_Plugin $auth */
2081    global $auth;
2082
2083    // build menu of admin functions from the plugins that handle them
2084    $pluginlist = plugin_list('admin');
2085    $menu = array();
2086    foreach ($pluginlist as $p) {
2087        /** @var DokuWiki_Admin_Plugin $obj */
2088        if(($obj = plugin_load('admin',$p)) === null) continue;
2089
2090        // check permissions
2091        if($obj->forAdminOnly() && !$INFO['isadmin']) continue;
2092
2093        $menu[$p] = array('plugin' => $p,
2094                'prompt' => $obj->getMenuText($conf['lang']),
2095                'sort' => $obj->getMenuSort()
2096                );
2097    }
2098
2099    // data security check
2100    // simple check if the 'savedir' is relative and accessible when appended to DOKU_URL
2101    // it verifies either:
2102    //   'savedir' has been moved elsewhere, or
2103    //   has protection to prevent the webserver serving files from it
2104    if (substr($conf['savedir'],0,2) == './'){
2105        echo '<a style="border:none; float:right;"
2106                href="http://www.dokuwiki.org/security#web_access_security">
2107                <img src="'.DOKU_URL.$conf['savedir'].'/security.png" alt="Your data directory seems to be protected properly."
2108                onerror="this.parentNode.style.display=\'none\'" /></a>';
2109    }
2110
2111    print p_locale_xhtml('admin');
2112
2113    // Admin Tasks
2114    if($INFO['isadmin']){
2115        ptln('<ul class="admin_tasks">');
2116
2117        if($menu['usermanager'] && $auth && $auth->canDo('getUsers')){
2118            ptln('  <li class="admin_usermanager"><div class="li">'.
2119                    '<a href="'.wl($ID, array('do' => 'admin','page' => 'usermanager')).'">'.
2120                    $menu['usermanager']['prompt'].'</a></div></li>');
2121        }
2122        unset($menu['usermanager']);
2123
2124        if($menu['acl']){
2125            ptln('  <li class="admin_acl"><div class="li">'.
2126                    '<a href="'.wl($ID, array('do' => 'admin','page' => 'acl')).'">'.
2127                    $menu['acl']['prompt'].'</a></div></li>');
2128        }
2129        unset($menu['acl']);
2130
2131        if($menu['extension']){
2132            ptln('  <li class="admin_plugin"><div class="li">'.
2133                    '<a href="'.wl($ID, array('do' => 'admin','page' => 'extension')).'">'.
2134                    $menu['extension']['prompt'].'</a></div></li>');
2135        }
2136        unset($menu['extension']);
2137
2138        if($menu['config']){
2139            ptln('  <li class="admin_config"><div class="li">'.
2140                    '<a href="'.wl($ID, array('do' => 'admin','page' => 'config')).'">'.
2141                    $menu['config']['prompt'].'</a></div></li>');
2142        }
2143        unset($menu['config']);
2144
2145        if($menu['styling']){
2146            ptln('  <li class="admin_styling"><div class="li">'.
2147                '<a href="'.wl($ID, array('do' => 'admin','page' => 'styling')).'">'.
2148                $menu['styling']['prompt'].'</a></div></li>');
2149        }
2150        unset($menu['styling']);
2151    }
2152    ptln('</ul>');
2153
2154    // Manager Tasks
2155    ptln('<ul class="admin_tasks">');
2156
2157    if($menu['revert']){
2158        ptln('  <li class="admin_revert"><div class="li">'.
2159                '<a href="'.wl($ID, array('do' => 'admin','page' => 'revert')).'">'.
2160                $menu['revert']['prompt'].'</a></div></li>');
2161    }
2162    unset($menu['revert']);
2163
2164    if($menu['popularity']){
2165        ptln('  <li class="admin_popularity"><div class="li">'.
2166                '<a href="'.wl($ID, array('do' => 'admin','page' => 'popularity')).'">'.
2167                $menu['popularity']['prompt'].'</a></div></li>');
2168    }
2169    unset($menu['popularity']);
2170
2171    // print DokuWiki version:
2172    ptln('</ul>');
2173    echo '<div id="admin__version">';
2174    echo getVersion();
2175    echo '</div>';
2176
2177    // print the rest as sorted list
2178    if(count($menu)){
2179        usort($menu, 'p_sort_modes');
2180        // output the menu
2181        ptln('<div class="clearer"></div>');
2182        print p_locale_xhtml('adminplugins');
2183        ptln('<ul>');
2184        foreach ($menu as $item) {
2185            if (!$item['prompt']) continue;
2186            ptln('  <li><div class="li"><a href="'.wl($ID, 'do=admin&amp;page='.$item['plugin']).'">'.$item['prompt'].'</a></div></li>');
2187        }
2188        ptln('</ul>');
2189    }
2190}
2191
2192/**
2193 * Form to request a new password for an existing account
2194 *
2195 * @author Benoit Chesneau <benoit@bchesneau.info>
2196 * @author Andreas Gohr <gohr@cosmocode.de>
2197 */
2198function html_resendpwd() {
2199    global $lang;
2200    global $conf;
2201    global $INPUT;
2202
2203    $token = preg_replace('/[^a-f0-9]+/','',$INPUT->str('pwauth'));
2204
2205    if(!$conf['autopasswd'] && $token){
2206        print p_locale_xhtml('resetpwd');
2207        print '<div class="centeralign">'.NL;
2208        $form = new Doku_Form(array('id' => 'dw__resendpwd'));
2209        $form->startFieldset($lang['btn_resendpwd']);
2210        $form->addHidden('token', $token);
2211        $form->addHidden('do', 'resendpwd');
2212
2213        $form->addElement(form_makePasswordField('pass', $lang['pass'], '', 'block', array('size'=>'50')));
2214        $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
2215
2216        $form->addElement(form_makeButton('submit', '', $lang['btn_resendpwd']));
2217        $form->endFieldset();
2218        html_form('resendpwd', $form);
2219        print '</div>'.NL;
2220    }else{
2221        print p_locale_xhtml('resendpwd');
2222        print '<div class="centeralign">'.NL;
2223        $form = new Doku_Form(array('id' => 'dw__resendpwd'));
2224        $form->startFieldset($lang['resendpwd']);
2225        $form->addHidden('do', 'resendpwd');
2226        $form->addHidden('save', '1');
2227        $form->addElement(form_makeTag('br'));
2228        $form->addElement(form_makeTextField('login', $INPUT->post->str('login'), $lang['user'], '', 'block'));
2229        $form->addElement(form_makeTag('br'));
2230        $form->addElement(form_makeTag('br'));
2231        $form->addElement(form_makeButton('submit', '', $lang['btn_resendpwd']));
2232        $form->endFieldset();
2233        html_form('resendpwd', $form);
2234        print '</div>'.NL;
2235    }
2236}
2237
2238/**
2239 * Return the TOC rendered to XHTML
2240 *
2241 * @author Andreas Gohr <andi@splitbrain.org>
2242 *
2243 * @param array $toc
2244 * @return string html
2245 */
2246function html_TOC($toc){
2247    if(!count($toc)) return '';
2248    global $lang;
2249    $out  = '<!-- TOC START -->'.DOKU_LF;
2250    $out .= '<div id="dw__toc">'.DOKU_LF;
2251    $out .= '<h3 class="toggle">';
2252    $out .= $lang['toc'];
2253    $out .= '</h3>'.DOKU_LF;
2254    $out .= '<div>'.DOKU_LF;
2255    $out .= html_buildlist($toc,'toc','html_list_toc','html_li_default',true);
2256    $out .= '</div>'.DOKU_LF.'</div>'.DOKU_LF;
2257    $out .= '<!-- TOC END -->'.DOKU_LF;
2258    return $out;
2259}
2260
2261/**
2262 * Callback for html_buildlist
2263 *
2264 * @param array $item
2265 * @return string html
2266 */
2267function html_list_toc($item){
2268    if(isset($item['hid'])){
2269        $link = '#'.$item['hid'];
2270    }else{
2271        $link = $item['link'];
2272    }
2273
2274    return '<a href="'.$link.'">'.hsc($item['title']).'</a>';
2275}
2276
2277/**
2278 * Helper function to build TOC items
2279 *
2280 * Returns an array ready to be added to a TOC array
2281 *
2282 * @param string $link  - where to link (if $hash set to '#' it's a local anchor)
2283 * @param string $text  - what to display in the TOC
2284 * @param int    $level - nesting level
2285 * @param string $hash  - is prepended to the given $link, set blank if you want full links
2286 * @return array the toc item
2287 */
2288function html_mktocitem($link, $text, $level, $hash='#'){
2289    return  array( 'link'  => $hash.$link,
2290            'title' => $text,
2291            'type'  => 'ul',
2292            'level' => $level);
2293}
2294
2295/**
2296 * Output a Doku_Form object.
2297 * Triggers an event with the form name: HTML_{$name}FORM_OUTPUT
2298 *
2299 * @author Tom N Harris <tnharris@whoopdedo.org>
2300 *
2301 * @param string     $name The name of the form
2302 * @param Doku_Form  $form The form
2303 */
2304function html_form($name, &$form) {
2305    // Safety check in case the caller forgets.
2306    $form->endFieldset();
2307    trigger_event('HTML_'.strtoupper($name).'FORM_OUTPUT', $form, 'html_form_output', false);
2308}
2309
2310/**
2311 * Form print function.
2312 * Just calls printForm() on the data object.
2313 *
2314 * @param Doku_Form $data The form
2315 */
2316function html_form_output($data) {
2317    $data->printForm();
2318}
2319
2320/**
2321 * Embed a flash object in HTML
2322 *
2323 * This will create the needed HTML to embed a flash movie in a cross browser
2324 * compatble way using valid XHTML
2325 *
2326 * The parameters $params, $flashvars and $atts need to be associative arrays.
2327 * No escaping needs to be done for them. The alternative content *has* to be
2328 * escaped because it is used as is. If no alternative content is given
2329 * $lang['noflash'] is used.
2330 *
2331 * @author Andreas Gohr <andi@splitbrain.org>
2332 * @link   http://latrine.dgx.cz/how-to-correctly-insert-a-flash-into-xhtml
2333 *
2334 * @param string $swf      - the SWF movie to embed
2335 * @param int $width       - width of the flash movie in pixels
2336 * @param int $height      - height of the flash movie in pixels
2337 * @param array $params    - additional parameters (<param>)
2338 * @param array $flashvars - parameters to be passed in the flashvar parameter
2339 * @param array $atts      - additional attributes for the <object> tag
2340 * @param string $alt      - alternative content (is NOT automatically escaped!)
2341 * @return string         - the XHTML markup
2342 */
2343function html_flashobject($swf,$width,$height,$params=null,$flashvars=null,$atts=null,$alt=''){
2344    global $lang;
2345
2346    $out = '';
2347
2348    // prepare the object attributes
2349    if(is_null($atts)) $atts = array();
2350    $atts['width']  = (int) $width;
2351    $atts['height'] = (int) $height;
2352    if(!$atts['width'])  $atts['width']  = 425;
2353    if(!$atts['height']) $atts['height'] = 350;
2354
2355    // add object attributes for standard compliant browsers
2356    $std = $atts;
2357    $std['type'] = 'application/x-shockwave-flash';
2358    $std['data'] = $swf;
2359
2360    // add object attributes for IE
2361    $ie  = $atts;
2362    $ie['classid'] = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
2363
2364    // open object (with conditional comments)
2365    $out .= '<!--[if !IE]> -->'.NL;
2366    $out .= '<object '.buildAttributes($std).'>'.NL;
2367    $out .= '<!-- <![endif]-->'.NL;
2368    $out .= '<!--[if IE]>'.NL;
2369    $out .= '<object '.buildAttributes($ie).'>'.NL;
2370    $out .= '    <param name="movie" value="'.hsc($swf).'" />'.NL;
2371    $out .= '<!--><!-- -->'.NL;
2372
2373    // print params
2374    if(is_array($params)) foreach($params as $key => $val){
2375        $out .= '  <param name="'.hsc($key).'" value="'.hsc($val).'" />'.NL;
2376    }
2377
2378    // add flashvars
2379    if(is_array($flashvars)){
2380        $out .= '  <param name="FlashVars" value="'.buildURLparams($flashvars).'" />'.NL;
2381    }
2382
2383    // alternative content
2384    if($alt){
2385        $out .= $alt.NL;
2386    }else{
2387        $out .= $lang['noflash'].NL;
2388    }
2389
2390    // finish
2391    $out .= '</object>'.NL;
2392    $out .= '<!-- <![endif]-->'.NL;
2393
2394    return $out;
2395}
2396
2397/**
2398 * Prints HTML code for the given tab structure
2399 *
2400 * @param array  $tabs        tab structure
2401 * @param string $current_tab the current tab id
2402 */
2403function html_tabs($tabs, $current_tab = null) {
2404    echo '<ul class="tabs">'.NL;
2405
2406    foreach($tabs as $id => $tab) {
2407        html_tab($tab['href'], $tab['caption'], $id === $current_tab);
2408    }
2409
2410    echo '</ul>'.NL;
2411}
2412/**
2413 * Prints a single tab
2414 *
2415 * @author Kate Arzamastseva <pshns@ukr.net>
2416 * @author Adrian Lang <mail@adrianlang.de>
2417 *
2418 * @param string $href - tab href
2419 * @param string $caption - tab caption
2420 * @param boolean $selected - is tab selected
2421 */
2422
2423function html_tab($href, $caption, $selected=false) {
2424    $tab = '<li>';
2425    if ($selected) {
2426        $tab .= '<strong>';
2427    } else {
2428        $tab .= '<a href="' . hsc($href) . '">';
2429    }
2430    $tab .= hsc($caption)
2431         .  '</' . ($selected ? 'strong' : 'a') . '>'
2432         .  '</li>'.NL;
2433    echo $tab;
2434}
2435
2436