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