xref: /dokuwiki/inc/html.php (revision a215faf2fc4cea7bbd92b8fd0b1034e976b87142)
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
9use dokuwiki\ChangeLog\MediaChangeLog;
10use dokuwiki\ChangeLog\PageChangeLog;
11use dokuwiki\Extension\AuthPlugin;
12use dokuwiki\Extension\Event;
13
14if (!defined('SEC_EDIT_PATTERN')) {
15    define('SEC_EDIT_PATTERN', '#<!-- EDIT({.*?}) -->#');
16}
17
18
19/**
20 * Convenience function to quickly build a wikilink
21 *
22 * @author Andreas Gohr <andi@splitbrain.org>
23 * @param string  $id      id of the target page
24 * @param string  $name    the name of the link, i.e. the text that is displayed
25 * @param string|array  $search  search string(s) that shall be highlighted in the target page
26 * @return string the HTML code of the link
27 */
28function html_wikilink($id, $name = null, $search = '') {
29    /** @var Doku_Renderer_xhtml $xhtml_renderer */
30    static $xhtml_renderer = null;
31    if (is_null($xhtml_renderer)) {
32        $xhtml_renderer = p_get_renderer('xhtml');
33    }
34
35    return $xhtml_renderer->internallink($id,$name,$search,true,'navigation');
36}
37
38/**
39 * The loginform
40 *
41 * @author   Andreas Gohr <andi@splitbrain.org>
42 *
43 * @param bool $svg Whether to show svg icons in the register and resendpwd links or not
44 * @deprecated 2020-07-18
45 */
46function html_login($svg = false) {
47    dbg_deprecated(\dokuwiki\Ui\Login::class .'::show()');
48    (new dokuwiki\Ui\Login($svg))->show();
49}
50
51
52/**
53 * Denied page content
54 *
55 * @return string html
56 * @deprecated 2020-07-18 not called anymore, see inc/Action/Denied::tplContent()
57 */
58function html_denied() {
59    dbg_deprecated(\dokuwiki\Action\Denied::class .'::showBanner()');
60    (new dokuwiki\Action\Denied())->showBanner();
61}
62
63/**
64 * inserts section edit buttons if wanted or removes the markers
65 *
66 * @author Andreas Gohr <andi@splitbrain.org>
67 *
68 * @param string $text
69 * @param bool   $show show section edit buttons?
70 * @return string
71 */
72function html_secedit($text, $show = true) {
73    global $INFO;
74
75    if ((isset($INFO) && !$INFO['writable']) || !$show || (isset($INFO) && $INFO['rev'])) {
76        return preg_replace(SEC_EDIT_PATTERN,'',$text);
77    }
78
79    return preg_replace_callback(SEC_EDIT_PATTERN,
80                'html_secedit_button', $text);
81}
82
83/**
84 * prepares section edit button data for event triggering
85 * used as a callback in html_secedit
86 *
87 * @author Andreas Gohr <andi@splitbrain.org>
88 *
89 * @param array $matches matches with regexp
90 * @return string
91 * @triggers HTML_SECEDIT_BUTTON
92 */
93function html_secedit_button($matches){
94    $json = htmlspecialchars_decode($matches[1], ENT_QUOTES);
95    $data = json_decode($json, true);
96    if ($data == NULL) {
97        return;
98    }
99    $data ['target'] = strtolower($data['target']);
100    $data ['hid'] = strtolower($data['hid']);
101
102    return Event::createAndTrigger('HTML_SECEDIT_BUTTON', $data,
103                         'html_secedit_get_button');
104}
105
106/**
107 * prints a section editing button
108 * used as default action form HTML_SECEDIT_BUTTON
109 *
110 * @author Adrian Lang <lang@cosmocode.de>
111 *
112 * @param array $data name, section id and target
113 * @return string html
114 */
115function html_secedit_get_button($data) {
116    global $ID;
117    global $INFO;
118
119    if (!isset($data['name']) || $data['name'] === '') return '';
120
121    $name = $data['name'];
122    unset($data['name']);
123
124    $secid = $data['secid'];
125    unset($data['secid']);
126
127    $params = array_merge(
128           array('do'  => 'edit', 'rev' => $INFO['lastmod'], 'summary' => '['.$name.'] '),
129           $data
130    );
131
132    $html = '<div class="secedit editbutton_'.$data['target'] .' editbutton_'.$secid .'">';
133    $html.= html_btn('secedit', $ID, '', $params, 'post', $name);
134    $html.= '</div>';
135    return $html;
136}
137
138/**
139 * Just the back to top button (in its own form)
140 *
141 * @author Andreas Gohr <andi@splitbrain.org>
142 *
143 * @return string html
144 */
145function html_topbtn() {
146    global $lang;
147
148    $html = '<a class="nolink" href="#dokuwiki__top">'
149        .'<button class="button" onclick="window.scrollTo(0, 0)" title="'. $lang['btn_top'] .'">'
150        . $lang['btn_top']
151        .'</button></a>';
152    return $html;
153}
154
155/**
156 * Displays a button (using its own form)
157 * If tooltip exists, the access key tooltip is replaced.
158 *
159 * @author Andreas Gohr <andi@splitbrain.org>
160 *
161 * @param string         $name
162 * @param string         $id
163 * @param string         $akey   access key
164 * @param string[] $params key-value pairs added as hidden inputs
165 * @param string         $method
166 * @param string         $tooltip
167 * @param bool|string    $label  label text, false: lookup btn_$name in localization
168 * @param string         $svg (optional) svg code, inserted into the button
169 * @return string
170 */
171function html_btn($name, $id, $akey, $params, $method = 'get', $tooltip = '', $label = false, $svg = null) {
172    global $conf;
173    global $lang;
174
175    if (!$label)
176        $label = $lang['btn_'.$name];
177
178    //filter id (without urlencoding)
179    $id = idfilter($id,false);
180
181    //make nice URLs even for buttons
182    if ($conf['userewrite'] == 2) {
183        $script = DOKU_BASE.DOKU_SCRIPT.'/'.$id;
184    } elseif ($conf['userewrite']) {
185        $script = DOKU_BASE.$id;
186    } else {
187        $script = DOKU_BASE.DOKU_SCRIPT;
188        $params['id'] = $id;
189    }
190
191    $html = '<form class="button btn_'.$name.'" method="'.$method.'" action="'.$script.'"><div class="no">';
192
193    if (is_array($params)) {
194        foreach ($params as $key => $val) {
195            $html .= '<input type="hidden" name="'.$key.'" value="'.hsc($val).'" />';
196        }
197    }
198
199    $tip = empty($tooltip) ? hsc($label) : hsc($tooltip);
200
201    $html .= '<button type="submit" ';
202    if ($akey) {
203        $tip  .= ' ['.strtoupper($akey).']';
204        $html .= 'accesskey="'.$akey.'" ';
205    }
206    $html .= 'title="'.$tip.'">';
207    if ($svg) {
208        $html .= '<span>'. hsc($label) .'</span>'. inlineSVG($svg);
209    } else {
210        $html .= hsc($label);
211    }
212    $html .= '</button>';
213    $html .= '</div></form>';
214
215    return $html;
216}
217/**
218 * show a revision warning
219 *
220 * @author Szymon Olewniczak <dokuwiki@imz.re>
221 * @deprecated 2020-07-18
222 */
223function html_showrev() {
224    dbg_deprecated(\dokuwiki\Ui\PageView::class .'::showrev()');
225}
226
227/**
228 * Show a wiki page
229 *
230 * @author Andreas Gohr <andi@splitbrain.org>
231 *
232 * @param null|string $txt wiki text or null for showing $ID
233 * @deprecated 2020-07-18
234 */
235function html_show($txt=null) {
236    dbg_deprecated(\dokuwiki\Ui\PageView::class .'::show()');
237    (new dokuwiki\Ui\PageView($txt))->show();
238}
239
240/**
241 * ask the user about how to handle an exisiting draft
242 *
243 * @author Andreas Gohr <andi@splitbrain.org>
244 * @deprecated 2020-07-18
245 */
246function html_draft() {
247    dbg_deprecated(\dokuwiki\Ui\Draft::class .'::show()');
248    (new dokuwiki\Ui\Draft)->show();
249}
250
251/**
252 * Highlights searchqueries in HTML code
253 *
254 * @author Andreas Gohr <andi@splitbrain.org>
255 * @author Harry Fuecks <hfuecks@gmail.com>
256 *
257 * @param string $html
258 * @param array|string $phrases
259 * @return string html
260 */
261function html_hilight($html, $phrases) {
262    $phrases = (array) $phrases;
263    $phrases = array_map('preg_quote_cb', $phrases);
264    $phrases = array_map('ft_snippet_re_preprocess', $phrases);
265    $phrases = array_filter($phrases);
266    $regex = join('|',$phrases);
267
268    if ($regex === '') return $html;
269    if (!\dokuwiki\Utf8\Clean::isUtf8($regex)) return $html;
270
271    $html = @preg_replace_callback("/((<[^>]*)|$regex)/ui", function ($match) {
272        $hlight = unslash($match[0]);
273        if (!isset($match[2])) {
274            $hlight = '<span class="search_hit">'.$hlight.'</span>';
275        }
276        return $hlight;
277    }, $html);
278    return $html;
279}
280
281/**
282 * Display error on locked pages
283 *
284 * @author Andreas Gohr <andi@splitbrain.org>
285 * @deprecated 2020-07-18 not called anymore, see inc/Action/Locked::tplContent()
286 */
287function html_locked() {
288    dbg_deprecated(\dokuwiki\Action\Locked::class .'::showBanner()');
289    (new dokuwiki\Action\Locked())->showBanner();
290}
291
292/**
293 * list old revisions
294 *
295 * @author Andreas Gohr <andi@splitbrain.org>
296 * @author Ben Coburn <btcoburn@silicodon.net>
297 * @author Kate Arzamastseva <pshns@ukr.net>
298 *
299 * @param int $first skip the first n changelog lines
300 * @param bool|string $media_id id of media, or false for current page
301 * @deprecated 2020-07-18
302 */
303function html_revisions($first=0, $media_id = false) {
304    dbg_deprecated(\dokuwiki\Ui\Revisions::class .'::show()');
305    (new dokuwiki\Ui\Revisions($first, $media_id))->show();
306}
307
308/**
309 * display recent changes
310 *
311 * @author Andreas Gohr <andi@splitbrain.org>
312 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
313 * @author Ben Coburn <btcoburn@silicodon.net>
314 * @author Kate Arzamastseva <pshns@ukr.net>
315 *
316 * @param int $first
317 * @param string $show_changes
318 * @deprecated 2020-07-18
319 */
320function html_recent($first = 0, $show_changes = 'both') {
321    dbg_deprecated(\dokuwiki\Ui\Recent::class .'::show()');
322    (new dokuwiki\Ui\Recent($first, $show_changes))->show();
323}
324
325/**
326 * Display page index
327 *
328 * @author Andreas Gohr <andi@splitbrain.org>
329 *
330 * @param string $ns
331 * @deprecated 2020-07-18
332 */
333function html_index($ns) {
334    dbg_deprecated(\dokuwiki\Ui\Index::class .'::show()');
335    (new dokuwiki\Ui\Index($ns))->show();
336}
337
338/**
339 * Index item formatter
340 *
341 * User function for html_buildlist()
342 *
343 * @author Andreas Gohr <andi@splitbrain.org>
344 *
345 * @param array $item
346 * @return string
347 * @deprecated 2020-07-18
348 */
349function html_list_index($item) {
350    dbg_deprecated(\dokuwiki\Ui\Index::class .'::formatListItem()');
351    return (new dokuwiki\Ui\Index)->formatListItem($item);
352}
353
354/**
355 * Index List item
356 *
357 * This user function is used in html_buildlist to build the
358 * <li> tags for namespaces when displaying the page index
359 * it gives different classes to opened or closed "folders"
360 *
361 * @author Andreas Gohr <andi@splitbrain.org>
362 *
363 * @param array $item
364 * @return string html
365 * @deprecated 2020-07-18
366 */
367function html_li_index($item) {
368    dbg_deprecated(\dokuwiki\Ui\Index::class .'::tagListItem()');
369    return (new dokuwiki\Ui\Index)->tagListItem($item);
370}
371
372/**
373 * Build an unordered list
374 *
375 * Build an unordered list from the given $data array
376 * Each item in the array has to have a 'level' property
377 * the item itself gets printed by the given $func user
378 * function. The second and optional function is used to
379 * print the <li> tag. Both user function need to accept
380 * a single item.
381 *
382 * Both user functions can be given as array to point to
383 * a member of an object.
384 *
385 * @author Andreas Gohr <andi@splitbrain.org>
386 *
387 * @param array    $data  array with item arrays
388 * @param string   $class class of ul wrapper
389 * @param callable $func  callback to print an list item
390 * @param callable $lifunc (optional) callback to the opening li tag
391 * @param bool     $forcewrapper (optional) Trigger building a wrapper ul if the first level is
392 *                               0 (we have a root object) or 1 (just the root content)
393 * @return string html of an unordered list
394 */
395function html_buildlist($data, $class, $func, $lifunc = null, $forcewrapper = false) {
396    if (count($data) === 0) {
397        return '';
398    }
399
400    $firstElement = reset($data);
401    $start_level = $firstElement['level'];
402    $level = $start_level;
403    $html  = '';
404    $open  = 0;
405
406    // set callback function to build the <li> tag, formerly defined as html_li_default()
407    if (!is_callable($lifunc)) {
408       $lifunc = function ($item) {
409           return '<li class="level'.$item['level'].'">';
410       };
411    }
412
413    foreach ($data as $item) {
414        if ($item['level'] > $level) {
415            //open new list
416            for ($i = 0; $i < ($item['level'] - $level); $i++) {
417                if ($i) $html .= '<li class="clear">';
418                $html .= "\n".'<ul class="'.$class.'">'."\n";
419                $open++;
420            }
421            $level = $item['level'];
422
423        } elseif ($item['level'] < $level) {
424            //close last item
425            $html .= '</li>'."\n";
426            while ($level > $item['level'] && $open > 0 ) {
427                //close higher lists
428                $html .= '</ul>'."\n".'</li>'."\n";
429                $level--;
430                $open--;
431            }
432        } elseif ($html !== '') {
433            //close previous item
434            $html .= '</li>'."\n";
435        }
436
437        //print item
438        $html .= call_user_func($lifunc, $item);
439        $html .= '<div class="li">';
440
441        $html .= call_user_func($func, $item);
442        $html .= '</div>';
443    }
444
445    //close remaining items and lists
446    $html .= '</li>'."\n";
447    while ($open-- > 0) {
448        $html .= '</ul></li>'."\n";
449    }
450
451    if ($forcewrapper || $start_level < 2) {
452        // Trigger building a wrapper ul if the first level is
453        // 0 (we have a root object) or 1 (just the root content)
454        $html = "\n".'<ul class="'.$class.'">'."\n".$html.'</ul>'."\n";
455    }
456
457    return $html;
458}
459
460/**
461 * display backlinks
462 *
463 * @author Andreas Gohr <andi@splitbrain.org>
464 * @author Michael Klier <chi@chimeric.de>
465 * @deprecated 2020-07-18
466 */
467function html_backlinks() {
468    dbg_deprecated(\dokuwiki\Ui\Backlinks::class .'::show()');
469    (new dokuwiki\Ui\Backlinks)->show();
470}
471
472/**
473 * Get header of diff HTML
474 *
475 * @param string $l_rev   Left revisions
476 * @param string $r_rev   Right revision
477 * @param string $id      Page id, if null $ID is used
478 * @param bool   $media   If it is for media files
479 * @param bool   $inline  Return the header on a single line
480 * @return string[] HTML snippets for diff header
481 * @deprecated 2020-07-18
482 */
483function html_diff_head($l_rev, $r_rev, $id = null, $media = false, $inline = false) {
484    dbg_deprecated('see '. \dokuwiki\Ui\Diff::class .'::diffHead()');
485}
486
487/**
488 * Show diff
489 * between current page version and provided $text
490 * or between the revisions provided via GET or POST
491 *
492 * @author Andreas Gohr <andi@splitbrain.org>
493 * @param  string $text  when non-empty: compare with this text with most current version
494 * @param  bool   $intro display the intro text
495 * @param  string $type  type of the diff (inline or sidebyside)
496 * @deprecated 2020-07-18
497 */
498function html_diff($text = '', $intro = true, $type = null) {
499    dbg_deprecated(\dokuwiki\Ui\Diff::class .'::show()');
500    (new dokuwiki\Ui\Diff($text, $intro, $type))->show();
501}
502
503/**
504 * Create html for revision navigation
505 *
506 * @param PageChangeLog $pagelog changelog object of current page
507 * @param string        $type    inline vs sidebyside
508 * @param int           $l_rev   left revision timestamp
509 * @param int           $r_rev   right revision timestamp
510 * @return string[] html of left and right navigation elements
511 * @deprecated 2020-07-18
512 */
513function html_diff_navigation($pagelog, $type, $l_rev, $r_rev) {
514    dbg_deprecated('see '. \dokuwiki\Ui\Diff::class .'::diffNavigation()');
515}
516
517/**
518 * Create html link to a diff defined by two revisions
519 *
520 * @param string $difftype display type
521 * @param string $linktype
522 * @param int $lrev oldest revision
523 * @param int $rrev newest revision or null for diff with current revision
524 * @return string html of link to a diff
525 * @deprecated 2020-07-18
526 */
527function html_diff_navigationlink($difftype, $linktype, $lrev, $rrev = null) {
528    dbg_deprecated('see '. \dokuwiki\Ui\Diff::class .'::diffViewlink()');
529}
530
531/**
532 * Insert soft breaks in diff html
533 *
534 * @param string $diffhtml
535 * @return string
536 * @deprecated 2020-07-18
537 */
538function html_insert_softbreaks($diffhtml) {
539    dbg_deprecated(\dokuwiki\Ui\Diff::class .'::insertSoftbreaks()');
540    (new dokuwiki\Ui\Diff())->insertSoftbreaks($diffhtml);
541}
542
543/**
544 * callback which adds softbreaks
545 *
546 * @param array $match array with first the complete match
547 * @return string the replacement
548 * @deprecated 2020-07-18
549 */
550function html_softbreak_callback($match){
551    dbg_deprecated('integrated into '. \dokuwiki\Ui\Diff::class .'::insertSoftbreaks()');
552}
553
554/**
555 * show warning on conflict detection
556 *
557 * @author Andreas Gohr <andi@splitbrain.org>
558 *
559 * @param string $text
560 * @param string $summary
561 * @deprecated 2020-07-18
562 */
563function html_conflict($text, $summary) {
564    dbg_deprecated(\dokuwiki\Ui\Conflict::class .'::show()');
565    (new dokuwiki\Ui\Conflict($text, $summary))->show();
566}
567
568/**
569 * Prints the global message array
570 *
571 * @author Andreas Gohr <andi@splitbrain.org>
572 */
573function html_msgarea() {
574    global $MSG, $MSG_shown;
575    /** @var array $MSG */
576    // store if the global $MSG has already been shown and thus HTML output has been started
577    $MSG_shown = true;
578
579    if (!isset($MSG)) return;
580
581    $shown = array();
582    foreach ($MSG as $msg) {
583        $hash = md5($msg['msg']);
584        if (isset($shown[$hash])) continue; // skip double messages
585        if (info_msg_allowed($msg)) {
586            print '<div class="'.$msg['lvl'].'">';
587            print $msg['msg'];
588            print '</div>';
589        }
590        $shown[$hash] = 1;
591    }
592
593    unset($GLOBALS['MSG']);
594}
595
596/**
597 * Prints the registration form
598 *
599 * @author Andreas Gohr <andi@splitbrain.org>
600 * @deprecated 2020-07-18
601 */
602function html_register() {
603    dbg_deprecated(\dokuwiki\Ui\UserRegister::class .'::show()');
604    (new dokuwiki\Ui\UserRegister)->show();
605}
606
607/**
608 * Print the update profile form
609 *
610 * @author Christopher Smith <chris@jalakai.co.uk>
611 * @author Andreas Gohr <andi@splitbrain.org>
612 * @deprecated 2020-07-18
613 */
614function html_updateprofile() {
615    dbg_deprecated(\dokuwiki\Ui\UserProfile::class .'::show()');
616    (new dokuwiki\Ui\UserProfile)->show();
617}
618
619/**
620 * Preprocess edit form data
621 *
622 * @author   Andreas Gohr <andi@splitbrain.org>
623 *
624 * @deprecated 2020-07-18
625 */
626function html_edit() {
627    dbg_deprecated(\dokuwiki\Ui\Editor::class .'::show()');
628    (new dokuwiki\Ui\Editor)->show();
629}
630
631/**
632 * prints some debug info
633 *
634 * @author Andreas Gohr <andi@splitbrain.org>
635 */
636function html_debug() {
637    global $conf;
638    global $lang;
639    /** @var AuthPlugin $auth */
640    global $auth;
641    global $INFO;
642
643    //remove sensitive data
644    $cnf = $conf;
645    debug_guard($cnf);
646    $nfo = $INFO;
647    debug_guard($nfo);
648    $ses = $_SESSION;
649    debug_guard($ses);
650
651    print '<html><body>';
652
653    print '<p>When reporting bugs please send all the following ';
654    print 'output as a mail to andi@splitbrain.org ';
655    print 'The best way to do this is to save this page in your browser</p>';
656
657    print '<b>$INFO:</b><pre>';
658    print_r($nfo);
659    print '</pre>';
660
661    print '<b>$_SERVER:</b><pre>';
662    print_r($_SERVER);
663    print '</pre>';
664
665    print '<b>$conf:</b><pre>';
666    print_r($cnf);
667    print '</pre>';
668
669    print '<b>DOKU_BASE:</b><pre>';
670    print DOKU_BASE;
671    print '</pre>';
672
673    print '<b>abs DOKU_BASE:</b><pre>';
674    print DOKU_URL;
675    print '</pre>';
676
677    print '<b>rel DOKU_BASE:</b><pre>';
678    print dirname($_SERVER['PHP_SELF']).'/';
679    print '</pre>';
680
681    print '<b>PHP Version:</b><pre>';
682    print phpversion();
683    print '</pre>';
684
685    print '<b>locale:</b><pre>';
686    print setlocale(LC_ALL,0);
687    print '</pre>';
688
689    print '<b>encoding:</b><pre>';
690    print $lang['encoding'];
691    print '</pre>';
692
693    if ($auth) {
694        print '<b>Auth backend capabilities:</b><pre>';
695        foreach ($auth->getCapabilities() as $cando) {
696            print '   '.str_pad($cando,16) .' => '. (int)$auth->canDo($cando) . DOKU_LF;
697        }
698        print '</pre>';
699    }
700
701    print '<b>$_SESSION:</b><pre>';
702    print_r($ses);
703    print '</pre>';
704
705    print '<b>Environment:</b><pre>';
706    print_r($_ENV);
707    print '</pre>';
708
709    print '<b>PHP settings:</b><pre>';
710    $inis = ini_get_all();
711    print_r($inis);
712    print '</pre>';
713
714    if (function_exists('apache_get_version')) {
715        $apache = array();
716        $apache['version'] = apache_get_version();
717
718        if (function_exists('apache_get_modules')) {
719            $apache['modules'] = apache_get_modules();
720        }
721        print '<b>Apache</b><pre>';
722        print_r($apache);
723        print '</pre>';
724    }
725
726    print '</body></html>';
727}
728
729/**
730 * Form to request a new password for an existing account
731 *
732 * @author Benoit Chesneau <benoit@bchesneau.info>
733 * @author Andreas Gohr <gohr@cosmocode.de>
734 * @deprecated 2020-07-18
735 */
736function html_resendpwd() {
737    dbg_deprecated(\dokuwiki\Ui\UserResendPwd::class .'::show()');
738    (new dokuwiki\Ui\UserResendPwd)->show();
739}
740
741/**
742 * Return the TOC rendered to XHTML
743 *
744 * @author Andreas Gohr <andi@splitbrain.org>
745 *
746 * @param array $toc
747 * @return string html
748 */
749function html_TOC($toc) {
750    if (!count($toc)) return '';
751    global $lang;
752    $out  = '<!-- TOC START -->'.DOKU_LF;
753    $out .= '<div id="dw__toc" class="dw__toc">'.DOKU_LF;
754    $out .= '<h3 class="toggle">';
755    $out .= $lang['toc'];
756    $out .= '</h3>'.DOKU_LF;
757    $out .= '<div>'.DOKU_LF;
758    $out .= html_buildlist($toc, 'toc', 'html_list_toc', null, true);
759    $out .= '</div>'.DOKU_LF.'</div>'.DOKU_LF;
760    $out .= '<!-- TOC END -->'.DOKU_LF;
761    return $out;
762}
763
764/**
765 * Callback for html_buildlist
766 *
767 * @param array $item
768 * @return string html
769 */
770function html_list_toc($item) {
771    if (isset($item['hid'])){
772        $link = '#'.$item['hid'];
773    } else {
774        $link = $item['link'];
775    }
776
777    return '<a href="'.$link.'">'.hsc($item['title']).'</a>';
778}
779
780/**
781 * Helper function to build TOC items
782 *
783 * Returns an array ready to be added to a TOC array
784 *
785 * @param string $link  - where to link (if $hash set to '#' it's a local anchor)
786 * @param string $text  - what to display in the TOC
787 * @param int    $level - nesting level
788 * @param string $hash  - is prepended to the given $link, set blank if you want full links
789 * @return array the toc item
790 */
791function html_mktocitem($link, $text, $level, $hash='#') {
792    return  array(
793            'link'  => $hash.$link,
794            'title' => $text,
795            'type'  => 'ul',
796            'level' => $level
797    );
798}
799
800/**
801 * Output a Doku_Form object.
802 * Triggers an event with the form name: HTML_{$name}FORM_OUTPUT
803 *
804 * @author Tom N Harris <tnharris@whoopdedo.org>
805 *
806 * @param string     $name The name of the form
807 * @param Doku_Form  $form The form
808 */
809function html_form($name, $form) {
810    // Safety check in case the caller forgets.
811    $form->endFieldset();
812    Event::createAndTrigger('HTML_'.strtoupper($name).'FORM_OUTPUT', $form, 'html_form_output', false);
813}
814
815/**
816 * Form print function.
817 * Just calls printForm() on the form object.
818 *
819 * @param Doku_Form $form The form
820 */
821function html_form_output($form) {
822    $form->printForm();
823}
824
825/**
826 * Embed a flash object in HTML
827 *
828 * This will create the needed HTML to embed a flash movie in a cross browser
829 * compatble way using valid XHTML
830 *
831 * The parameters $params, $flashvars and $atts need to be associative arrays.
832 * No escaping needs to be done for them. The alternative content *has* to be
833 * escaped because it is used as is. If no alternative content is given
834 * $lang['noflash'] is used.
835 *
836 * @author Andreas Gohr <andi@splitbrain.org>
837 * @link   http://latrine.dgx.cz/how-to-correctly-insert-a-flash-into-xhtml
838 *
839 * @param string $swf      - the SWF movie to embed
840 * @param int $width       - width of the flash movie in pixels
841 * @param int $height      - height of the flash movie in pixels
842 * @param array $params    - additional parameters (<param>)
843 * @param array $flashvars - parameters to be passed in the flashvar parameter
844 * @param array $atts      - additional attributes for the <object> tag
845 * @param string $alt      - alternative content (is NOT automatically escaped!)
846 * @return string         - the XHTML markup
847 */
848function html_flashobject($swf,$width,$height,$params=null,$flashvars=null,$atts=null,$alt=''){
849    global $lang;
850
851    $out = '';
852
853    // prepare the object attributes
854    if(is_null($atts)) $atts = array();
855    $atts['width']  = (int) $width;
856    $atts['height'] = (int) $height;
857    if(!$atts['width'])  $atts['width']  = 425;
858    if(!$atts['height']) $atts['height'] = 350;
859
860    // add object attributes for standard compliant browsers
861    $std = $atts;
862    $std['type'] = 'application/x-shockwave-flash';
863    $std['data'] = $swf;
864
865    // add object attributes for IE
866    $ie  = $atts;
867    $ie['classid'] = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
868
869    // open object (with conditional comments)
870    $out .= '<!--[if !IE]> -->'.NL;
871    $out .= '<object '.buildAttributes($std).'>'.NL;
872    $out .= '<!-- <![endif]-->'.NL;
873    $out .= '<!--[if IE]>'.NL;
874    $out .= '<object '.buildAttributes($ie).'>'.NL;
875    $out .= '    <param name="movie" value="'.hsc($swf).'" />'.NL;
876    $out .= '<!--><!-- -->'.NL;
877
878    // print params
879    if(is_array($params)) foreach($params as $key => $val){
880        $out .= '  <param name="'.hsc($key).'" value="'.hsc($val).'" />'.NL;
881    }
882
883    // add flashvars
884    if(is_array($flashvars)){
885        $out .= '  <param name="FlashVars" value="'.buildURLparams($flashvars).'" />'.NL;
886    }
887
888    // alternative content
889    if($alt){
890        $out .= $alt.NL;
891    }else{
892        $out .= $lang['noflash'].NL;
893    }
894
895    // finish
896    $out .= '</object>'.NL;
897    $out .= '<!-- <![endif]-->'.NL;
898
899    return $out;
900}
901
902/**
903 * Prints HTML code for the given tab structure
904 *
905 * @param array  $tabs        tab structure
906 * @param string $current_tab the current tab id
907 */
908function html_tabs($tabs, $current_tab = null) {
909    echo '<ul class="tabs">'.NL;
910
911    foreach ($tabs as $id => $tab) {
912        html_tab($tab['href'], $tab['caption'], $id === $current_tab);
913    }
914
915    echo '</ul>'.NL;
916}
917
918/**
919 * Prints a single tab
920 *
921 * @author Kate Arzamastseva <pshns@ukr.net>
922 * @author Adrian Lang <mail@adrianlang.de>
923 *
924 * @param string $href - tab href
925 * @param string $caption - tab caption
926 * @param boolean $selected - is tab selected
927 */
928
929function html_tab($href, $caption, $selected = false) {
930    $tab = '<li>';
931    if ($selected) {
932        $tab .= '<strong>';
933    } else {
934        $tab .= '<a href="' . hsc($href) . '">';
935    }
936    $tab .= hsc($caption)
937         .  '</' . ($selected ? 'strong' : 'a') . '>'
938         .  '</li>'.NL;
939    echo $tab;
940}
941
942/**
943 * Display size change
944 *
945 * @param int $sizechange - size of change in Bytes
946 * @param Doku_Form $form - (optional) form to add elements to
947 * @return void|string
948 */
949function html_sizechange($sizechange, $form = null) {
950    if (isset($sizechange)) {
951        $class = 'sizechange';
952        $value = filesize_h(abs($sizechange));
953        if ($sizechange > 0) {
954            $class .= ' positive';
955            $value = '+' . $value;
956        } elseif ($sizechange < 0) {
957            $class .= ' negative';
958            $value = '-' . $value;
959        } else {
960            $value = '±' . $value;
961        }
962        if (!isset($form)) {
963            return '<span class="'.$class.'">'.$value.'</span>';
964        } else { // Doku_Form
965            $form->addElement(form_makeOpenTag('span', array('class' => $class)));
966            $form->addElement($value);
967            $form->addElement(form_makeCloseTag('span'));
968        }
969    }
970}
971