xref: /dokuwiki/inc/html.php (revision bf69f8cb6ac73798213ed30b1003ff973516d2bf)
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 tree item formatter for html_buildlist()
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 formatter for html_buildlist()
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 * Default list item formatter for html_buildlist()
374 *
375 * @author Andreas Gohr <andi@splitbrain.org>
376 *
377 * @param array $item
378 * @return string html
379 * @deprecated 2020-07-18
380 */
381function html_li_default($item){
382    return '<li class="level'.$item['level'].'">';
383}
384
385/**
386 * Build an unordered list
387 *
388 * Build an unordered list from the given $data array
389 * Each item in the array has to have a 'level' property
390 * the item itself gets printed by the given $func user
391 * function. The second and optional function is used to
392 * print the <li> tag. Both user function need to accept
393 * a single item.
394 *
395 * Both user functions can be given as array to point to
396 * a member of an object.
397 *
398 * @author Andreas Gohr <andi@splitbrain.org>
399 *
400 * @param array    $data  array with item arrays
401 * @param string   $class class of ul wrapper
402 * @param callable $func  callback to print an list item
403 * @param callable $lifunc (optional) callback to the opening li tag
404 * @param bool     $forcewrapper (optional) Trigger building a wrapper ul if the first level is
405 *                               0 (we have a root object) or 1 (just the root content)
406 * @return string html of an unordered list
407 */
408function html_buildlist($data, $class, $func, $lifunc = null, $forcewrapper = false) {
409    if (count($data) === 0) {
410        return '';
411    }
412
413    $firstElement = reset($data);
414    $start_level = $firstElement['level'];
415    $level = $start_level;
416    $html  = '';
417    $open  = 0;
418
419    // set callback function to build the <li> tag, formerly defined as html_li_default()
420    if (!is_callable($lifunc)) {
421       $lifunc = function ($item) {
422           return '<li class="level'.$item['level'].'">';
423       };
424    }
425
426    foreach ($data as $item) {
427        if ($item['level'] > $level) {
428            //open new list
429            for ($i = 0; $i < ($item['level'] - $level); $i++) {
430                if ($i) $html .= '<li class="clear">';
431                $html .= "\n".'<ul class="'.$class.'">'."\n";
432                $open++;
433            }
434            $level = $item['level'];
435
436        } elseif ($item['level'] < $level) {
437            //close last item
438            $html .= '</li>'."\n";
439            while ($level > $item['level'] && $open > 0 ) {
440                //close higher lists
441                $html .= '</ul>'."\n".'</li>'."\n";
442                $level--;
443                $open--;
444            }
445        } elseif ($html !== '') {
446            //close previous item
447            $html .= '</li>'."\n";
448        }
449
450        //print item
451        $html .= call_user_func($lifunc, $item);
452        $html .= '<div class="li">';
453
454        $html .= call_user_func($func, $item);
455        $html .= '</div>';
456    }
457
458    //close remaining items and lists
459    $html .= '</li>'."\n";
460    while ($open-- > 0) {
461        $html .= '</ul></li>'."\n";
462    }
463
464    if ($forcewrapper || $start_level < 2) {
465        // Trigger building a wrapper ul if the first level is
466        // 0 (we have a root object) or 1 (just the root content)
467        $html = "\n".'<ul class="'.$class.'">'."\n".$html.'</ul>'."\n";
468    }
469
470    return $html;
471}
472
473/**
474 * display backlinks
475 *
476 * @author Andreas Gohr <andi@splitbrain.org>
477 * @author Michael Klier <chi@chimeric.de>
478 * @deprecated 2020-07-18
479 */
480function html_backlinks() {
481    dbg_deprecated(\dokuwiki\Ui\Backlinks::class .'::show()');
482    (new dokuwiki\Ui\Backlinks)->show();
483}
484
485/**
486 * Get header of diff HTML
487 *
488 * @param string $l_rev   Left revisions
489 * @param string $r_rev   Right revision
490 * @param string $id      Page id, if null $ID is used
491 * @param bool   $media   If it is for media files
492 * @param bool   $inline  Return the header on a single line
493 * @return string[] HTML snippets for diff header
494 * @deprecated 2020-07-18
495 */
496function html_diff_head($l_rev, $r_rev, $id = null, $media = false, $inline = false) {
497    dbg_deprecated('see '. \dokuwiki\Ui\Diff::class .'::diffHead()');
498}
499
500/**
501 * Show diff
502 * between current page version and provided $text
503 * or between the revisions provided via GET or POST
504 *
505 * @author Andreas Gohr <andi@splitbrain.org>
506 * @param  string $text  when non-empty: compare with this text with most current version
507 * @param  bool   $intro display the intro text
508 * @param  string $type  type of the diff (inline or sidebyside)
509 * @deprecated 2020-07-18
510 */
511function html_diff($text = '', $intro = true, $type = null) {
512    dbg_deprecated(\dokuwiki\Ui\Diff::class .'::show()');
513    (new dokuwiki\Ui\Diff($text, $intro, $type))->show();
514}
515
516/**
517 * Create html for revision navigation
518 *
519 * @param PageChangeLog $pagelog changelog object of current page
520 * @param string        $type    inline vs sidebyside
521 * @param int           $l_rev   left revision timestamp
522 * @param int           $r_rev   right revision timestamp
523 * @return string[] html of left and right navigation elements
524 * @deprecated 2020-07-18
525 */
526function html_diff_navigation($pagelog, $type, $l_rev, $r_rev) {
527    dbg_deprecated('see '. \dokuwiki\Ui\Diff::class .'::diffNavigation()');
528}
529
530/**
531 * Create html link to a diff defined by two revisions
532 *
533 * @param string $difftype display type
534 * @param string $linktype
535 * @param int $lrev oldest revision
536 * @param int $rrev newest revision or null for diff with current revision
537 * @return string html of link to a diff
538 * @deprecated 2020-07-18
539 */
540function html_diff_navigationlink($difftype, $linktype, $lrev, $rrev = null) {
541    dbg_deprecated('see '. \dokuwiki\Ui\Diff::class .'::diffViewlink()');
542}
543
544/**
545 * Insert soft breaks in diff html
546 *
547 * @param string $diffhtml
548 * @return string
549 * @deprecated 2020-07-18
550 */
551function html_insert_softbreaks($diffhtml) {
552    dbg_deprecated(\dokuwiki\Ui\Diff::class .'::insertSoftbreaks()');
553    return (new dokuwiki\Ui\Diff())->insertSoftbreaks($diffhtml);
554}
555
556/**
557 * callback which adds softbreaks
558 *
559 * @param array $match array with first the complete match
560 * @return string the replacement
561 * @deprecated 2020-07-18
562 */
563function html_softbreak_callback($match){
564    dbg_deprecated('integrated into '. \dokuwiki\Ui\Diff::class .'::insertSoftbreaks()');
565}
566
567/**
568 * show warning on conflict detection
569 *
570 * @author Andreas Gohr <andi@splitbrain.org>
571 *
572 * @param string $text
573 * @param string $summary
574 * @deprecated 2020-07-18
575 */
576function html_conflict($text, $summary) {
577    dbg_deprecated(\dokuwiki\Ui\Conflict::class .'::show()');
578    (new dokuwiki\Ui\Conflict($text, $summary))->show();
579}
580
581/**
582 * Prints the global message array
583 *
584 * @author Andreas Gohr <andi@splitbrain.org>
585 */
586function html_msgarea() {
587    global $MSG, $MSG_shown;
588    /** @var array $MSG */
589    // store if the global $MSG has already been shown and thus HTML output has been started
590    $MSG_shown = true;
591
592    if (!isset($MSG)) return;
593
594    $shown = array();
595    foreach ($MSG as $msg) {
596        $hash = md5($msg['msg']);
597        if (isset($shown[$hash])) continue; // skip double messages
598        if (info_msg_allowed($msg)) {
599            print '<div class="'.$msg['lvl'].'">';
600            print $msg['msg'];
601            print '</div>';
602        }
603        $shown[$hash] = 1;
604    }
605
606    unset($GLOBALS['MSG']);
607}
608
609/**
610 * Prints the registration form
611 *
612 * @author Andreas Gohr <andi@splitbrain.org>
613 * @deprecated 2020-07-18
614 */
615function html_register() {
616    dbg_deprecated(\dokuwiki\Ui\UserRegister::class .'::show()');
617    (new dokuwiki\Ui\UserRegister)->show();
618}
619
620/**
621 * Print the update profile form
622 *
623 * @author Christopher Smith <chris@jalakai.co.uk>
624 * @author Andreas Gohr <andi@splitbrain.org>
625 * @deprecated 2020-07-18
626 */
627function html_updateprofile() {
628    dbg_deprecated(\dokuwiki\Ui\UserProfile::class .'::show()');
629    (new dokuwiki\Ui\UserProfile)->show();
630}
631
632/**
633 * Preprocess edit form data
634 *
635 * @author   Andreas Gohr <andi@splitbrain.org>
636 *
637 * @deprecated 2020-07-18
638 */
639function html_edit() {
640    dbg_deprecated(\dokuwiki\Ui\Editor::class .'::show()');
641    (new dokuwiki\Ui\Editor)->show();
642}
643
644/**
645 * Display the default edit form
646 *
647 * Is the default action for HTML_EDIT_FORMSELECTION.
648 *
649 * @param mixed[] $param
650 * @deprecated 2020-07-18
651 */
652function html_edit_form($param) {
653    dbg_deprecated(\dokuwiki\Ui\Editor::class .'::addTextarea()');
654    return (new dokuwiki\Ui\Editor)->addTextarea($param);
655}
656
657/**
658 * prints some debug info
659 *
660 * @author Andreas Gohr <andi@splitbrain.org>
661 */
662function html_debug() {
663    global $conf;
664    global $lang;
665    /** @var AuthPlugin $auth */
666    global $auth;
667    global $INFO;
668
669    //remove sensitive data
670    $cnf = $conf;
671    debug_guard($cnf);
672    $nfo = $INFO;
673    debug_guard($nfo);
674    $ses = $_SESSION;
675    debug_guard($ses);
676
677    print '<html><body>';
678
679    print '<p>When reporting bugs please send all the following ';
680    print 'output as a mail to andi@splitbrain.org ';
681    print 'The best way to do this is to save this page in your browser</p>';
682
683    print '<b>$INFO:</b><pre>';
684    print_r($nfo);
685    print '</pre>';
686
687    print '<b>$_SERVER:</b><pre>';
688    print_r($_SERVER);
689    print '</pre>';
690
691    print '<b>$conf:</b><pre>';
692    print_r($cnf);
693    print '</pre>';
694
695    print '<b>DOKU_BASE:</b><pre>';
696    print DOKU_BASE;
697    print '</pre>';
698
699    print '<b>abs DOKU_BASE:</b><pre>';
700    print DOKU_URL;
701    print '</pre>';
702
703    print '<b>rel DOKU_BASE:</b><pre>';
704    print dirname($_SERVER['PHP_SELF']).'/';
705    print '</pre>';
706
707    print '<b>PHP Version:</b><pre>';
708    print phpversion();
709    print '</pre>';
710
711    print '<b>locale:</b><pre>';
712    print setlocale(LC_ALL,0);
713    print '</pre>';
714
715    print '<b>encoding:</b><pre>';
716    print $lang['encoding'];
717    print '</pre>';
718
719    if ($auth) {
720        print '<b>Auth backend capabilities:</b><pre>';
721        foreach ($auth->getCapabilities() as $cando) {
722            print '   '.str_pad($cando,16) .' => '. (int)$auth->canDo($cando) . DOKU_LF;
723        }
724        print '</pre>';
725    }
726
727    print '<b>$_SESSION:</b><pre>';
728    print_r($ses);
729    print '</pre>';
730
731    print '<b>Environment:</b><pre>';
732    print_r($_ENV);
733    print '</pre>';
734
735    print '<b>PHP settings:</b><pre>';
736    $inis = ini_get_all();
737    print_r($inis);
738    print '</pre>';
739
740    if (function_exists('apache_get_version')) {
741        $apache = array();
742        $apache['version'] = apache_get_version();
743
744        if (function_exists('apache_get_modules')) {
745            $apache['modules'] = apache_get_modules();
746        }
747        print '<b>Apache</b><pre>';
748        print_r($apache);
749        print '</pre>';
750    }
751
752    print '</body></html>';
753}
754
755/**
756 * Form to request a new password for an existing account
757 *
758 * @author Benoit Chesneau <benoit@bchesneau.info>
759 * @author Andreas Gohr <gohr@cosmocode.de>
760 * @deprecated 2020-07-18
761 */
762function html_resendpwd() {
763    dbg_deprecated(\dokuwiki\Ui\UserResendPwd::class .'::show()');
764    (new dokuwiki\Ui\UserResendPwd)->show();
765}
766
767/**
768 * Return the TOC rendered to XHTML
769 *
770 * @author Andreas Gohr <andi@splitbrain.org>
771 *
772 * @param array $toc
773 * @return string html
774 */
775function html_TOC($toc) {
776    if (!count($toc)) return '';
777    global $lang;
778    $out  = '<!-- TOC START -->'.DOKU_LF;
779    $out .= '<div id="dw__toc" class="dw__toc">'.DOKU_LF;
780    $out .= '<h3 class="toggle">';
781    $out .= $lang['toc'];
782    $out .= '</h3>'.DOKU_LF;
783    $out .= '<div>'.DOKU_LF;
784    $out .= html_buildlist($toc, 'toc', 'html_list_toc', null, true);
785    $out .= '</div>'.DOKU_LF.'</div>'.DOKU_LF;
786    $out .= '<!-- TOC END -->'.DOKU_LF;
787    return $out;
788}
789
790/**
791 * Callback for html_buildlist
792 *
793 * @param array $item
794 * @return string html
795 */
796function html_list_toc($item) {
797    if (isset($item['hid'])){
798        $link = '#'.$item['hid'];
799    } else {
800        $link = $item['link'];
801    }
802
803    return '<a href="'.$link.'">'.hsc($item['title']).'</a>';
804}
805
806/**
807 * Helper function to build TOC items
808 *
809 * Returns an array ready to be added to a TOC array
810 *
811 * @param string $link  - where to link (if $hash set to '#' it's a local anchor)
812 * @param string $text  - what to display in the TOC
813 * @param int    $level - nesting level
814 * @param string $hash  - is prepended to the given $link, set blank if you want full links
815 * @return array the toc item
816 */
817function html_mktocitem($link, $text, $level, $hash='#') {
818    return  array(
819            'link'  => $hash.$link,
820            'title' => $text,
821            'type'  => 'ul',
822            'level' => $level
823    );
824}
825
826/**
827 * Output a Doku_Form object.
828 * Triggers an event with the form name: HTML_{$name}FORM_OUTPUT
829 *
830 * @author Tom N Harris <tnharris@whoopdedo.org>
831 *
832 * @param string     $name The name of the form
833 * @param Doku_Form  $form The form
834 */
835function html_form($name, $form) {
836    // Safety check in case the caller forgets.
837    $form->endFieldset();
838    Event::createAndTrigger('HTML_'.strtoupper($name).'FORM_OUTPUT', $form, 'html_form_output', false);
839}
840
841/**
842 * Form print function.
843 * Just calls printForm() on the form object.
844 *
845 * @param Doku_Form $form The form
846 */
847function html_form_output($form) {
848    $form->printForm();
849}
850
851/**
852 * Embed a flash object in HTML
853 *
854 * This will create the needed HTML to embed a flash movie in a cross browser
855 * compatble way using valid XHTML
856 *
857 * The parameters $params, $flashvars and $atts need to be associative arrays.
858 * No escaping needs to be done for them. The alternative content *has* to be
859 * escaped because it is used as is. If no alternative content is given
860 * $lang['noflash'] is used.
861 *
862 * @author Andreas Gohr <andi@splitbrain.org>
863 * @link   http://latrine.dgx.cz/how-to-correctly-insert-a-flash-into-xhtml
864 *
865 * @param string $swf      - the SWF movie to embed
866 * @param int $width       - width of the flash movie in pixels
867 * @param int $height      - height of the flash movie in pixels
868 * @param array $params    - additional parameters (<param>)
869 * @param array $flashvars - parameters to be passed in the flashvar parameter
870 * @param array $atts      - additional attributes for the <object> tag
871 * @param string $alt      - alternative content (is NOT automatically escaped!)
872 * @return string         - the XHTML markup
873 */
874function html_flashobject($swf,$width,$height,$params=null,$flashvars=null,$atts=null,$alt=''){
875    global $lang;
876
877    $out = '';
878
879    // prepare the object attributes
880    if(is_null($atts)) $atts = array();
881    $atts['width']  = (int) $width;
882    $atts['height'] = (int) $height;
883    if(!$atts['width'])  $atts['width']  = 425;
884    if(!$atts['height']) $atts['height'] = 350;
885
886    // add object attributes for standard compliant browsers
887    $std = $atts;
888    $std['type'] = 'application/x-shockwave-flash';
889    $std['data'] = $swf;
890
891    // add object attributes for IE
892    $ie  = $atts;
893    $ie['classid'] = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
894
895    // open object (with conditional comments)
896    $out .= '<!--[if !IE]> -->'.NL;
897    $out .= '<object '.buildAttributes($std).'>'.NL;
898    $out .= '<!-- <![endif]-->'.NL;
899    $out .= '<!--[if IE]>'.NL;
900    $out .= '<object '.buildAttributes($ie).'>'.NL;
901    $out .= '    <param name="movie" value="'.hsc($swf).'" />'.NL;
902    $out .= '<!--><!-- -->'.NL;
903
904    // print params
905    if(is_array($params)) foreach($params as $key => $val){
906        $out .= '  <param name="'.hsc($key).'" value="'.hsc($val).'" />'.NL;
907    }
908
909    // add flashvars
910    if(is_array($flashvars)){
911        $out .= '  <param name="FlashVars" value="'.buildURLparams($flashvars).'" />'.NL;
912    }
913
914    // alternative content
915    if($alt){
916        $out .= $alt.NL;
917    }else{
918        $out .= $lang['noflash'].NL;
919    }
920
921    // finish
922    $out .= '</object>'.NL;
923    $out .= '<!-- <![endif]-->'.NL;
924
925    return $out;
926}
927
928/**
929 * Prints HTML code for the given tab structure
930 *
931 * @param array  $tabs        tab structure
932 * @param string $current_tab the current tab id
933 */
934function html_tabs($tabs, $current_tab = null) {
935    echo '<ul class="tabs">'.NL;
936
937    foreach ($tabs as $id => $tab) {
938        html_tab($tab['href'], $tab['caption'], $id === $current_tab);
939    }
940
941    echo '</ul>'.NL;
942}
943
944/**
945 * Prints a single tab
946 *
947 * @author Kate Arzamastseva <pshns@ukr.net>
948 * @author Adrian Lang <mail@adrianlang.de>
949 *
950 * @param string $href - tab href
951 * @param string $caption - tab caption
952 * @param boolean $selected - is tab selected
953 */
954
955function html_tab($href, $caption, $selected = false) {
956    $tab = '<li>';
957    if ($selected) {
958        $tab .= '<strong>';
959    } else {
960        $tab .= '<a href="' . hsc($href) . '">';
961    }
962    $tab .= hsc($caption)
963         .  '</' . ($selected ? 'strong' : 'a') . '>'
964         .  '</li>'.NL;
965    echo $tab;
966}
967
968/**
969 * Display size change
970 *
971 * @param int $sizechange - size of change in Bytes
972 * @param Doku_Form $form - (optional) form to add elements to
973 * @return void|string
974 */
975function html_sizechange($sizechange, $form = null) {
976    if (isset($sizechange)) {
977        $class = 'sizechange';
978        $value = filesize_h(abs($sizechange));
979        if ($sizechange > 0) {
980            $class .= ' positive';
981            $value = '+' . $value;
982        } elseif ($sizechange < 0) {
983            $class .= ' negative';
984            $value = '-' . $value;
985        } else {
986            $value = '±' . $value;
987        }
988        if (!isset($form)) {
989            return '<span class="'.$class.'">'.$value.'</span>';
990        } else { // Doku_Form
991            $form->addElement(form_makeOpenTag('span', array('class' => $class)));
992            $form->addElement($value);
993            $form->addElement(form_makeCloseTag('span'));
994        }
995    }
996}
997