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