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