1<?php
2
3/**
4 * DokuWiki template functions
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Andreas Gohr <andi@splitbrain.org>
8 */
9
10use dokuwiki\ActionRouter;
11use dokuwiki\Action\Exception\FatalException;
12use dokuwiki\Extension\PluginInterface;
13use dokuwiki\Ui\Admin;
14use dokuwiki\StyleUtils;
15use dokuwiki\Menu\Item\AbstractItem;
16use dokuwiki\Form\Form;
17use dokuwiki\Menu\MobileMenu;
18use dokuwiki\Ui\Subscribe;
19use dokuwiki\Extension\AdminPlugin;
20use dokuwiki\Extension\Event;
21use dokuwiki\File\PageResolver;
22
23/**
24 * Access a template file
25 *
26 * Returns the path to the given file inside the current template, uses
27 * default template if the custom version doesn't exist.
28 *
29 * @param string $file
30 * @return string
31 *
32 * @author Andreas Gohr <andi@splitbrain.org>
33 */
34function template($file)
35{
36    global $conf;
37
38    if (@is_readable(DOKU_INC . 'lib/tpl/' . $conf['template'] . '/' . $file))
39        return DOKU_INC . 'lib/tpl/' . $conf['template'] . '/' . $file;
40
41    return DOKU_INC . 'lib/tpl/dokuwiki/' . $file;
42}
43
44/**
45 * Convenience function to access template dir from local FS
46 *
47 * This replaces the deprecated DOKU_TPLINC constant
48 *
49 * @param string $tpl The template to use, default to current one
50 * @return string
51 *
52 * @author Andreas Gohr <andi@splitbrain.org>
53 */
54function tpl_incdir($tpl = '')
55{
56    global $conf;
57    if (!$tpl) $tpl = $conf['template'];
58    return DOKU_INC . 'lib/tpl/' . $tpl . '/';
59}
60
61/**
62 * Convenience function to access template dir from web
63 *
64 * This replaces the deprecated DOKU_TPL constant
65 *
66 * @param string $tpl The template to use, default to current one
67 * @return string
68 *
69 * @author Andreas Gohr <andi@splitbrain.org>
70 */
71function tpl_basedir($tpl = '')
72{
73    global $conf;
74    if (!$tpl) $tpl = $conf['template'];
75    return DOKU_BASE . 'lib/tpl/' . $tpl . '/';
76}
77
78/**
79 * Print the content
80 *
81 * This function is used for printing all the usual content
82 * (defined by the global $ACT var) by calling the appropriate
83 * outputfunction(s) from html.php
84 *
85 * Everything that doesn't use the main template file isn't
86 * handled by this function. ACL stuff is not done here either.
87 *
88 * @param bool $prependTOC should the TOC be displayed here?
89 * @return bool true if any output
90 *
91 * @triggers TPL_ACT_RENDER
92 * @triggers TPL_CONTENT_DISPLAY
93 * @author Andreas Gohr <andi@splitbrain.org>
94 */
95function tpl_content($prependTOC = true)
96{
97    global $ACT;
98    global $INFO;
99    $INFO['prependTOC'] = $prependTOC;
100
101    ob_start();
102    Event::createAndTrigger('TPL_ACT_RENDER', $ACT, 'tpl_content_core');
103    $html_output = ob_get_clean();
104    Event::createAndTrigger('TPL_CONTENT_DISPLAY', $html_output, function ($html_output) {
105        echo $html_output;
106    });
107
108    return !empty($html_output);
109}
110
111/**
112 * Default Action of TPL_ACT_RENDER
113 *
114 * @return bool
115 */
116function tpl_content_core()
117{
118    $router = ActionRouter::getInstance();
119    try {
120        $router->getAction()->tplContent();
121    } catch (FatalException $e) {
122        // there was no content for the action
123        msg(hsc($e->getMessage()), -1);
124        return false;
125    }
126    return true;
127}
128
129/**
130 * Places the TOC where the function is called
131 *
132 * If you use this you most probably want to call tpl_content with
133 * a false argument
134 *
135 * @param bool $return Should the TOC be returned instead to be printed?
136 * @return string
137 *
138 * @author Andreas Gohr <andi@splitbrain.org>
139 */
140function tpl_toc($return = false)
141{
142    global $TOC;
143    global $ACT;
144    global $ID;
145    global $REV;
146    global $INFO;
147    global $conf;
148    $toc = [];
149
150    if (is_array($TOC)) {
151        // if a TOC was prepared in global scope, always use it
152        $toc = $TOC;
153    } elseif (($ACT == 'show' || str_starts_with($ACT, 'export')) && !$REV && $INFO['exists']) {
154        // get TOC from metadata, render if neccessary
155        $meta = p_get_metadata($ID, '', METADATA_RENDER_USING_CACHE);
156        $tocok = $meta['internal']['toc'] ?? true;
157        $toc = $meta['description']['tableofcontents'] ?? null;
158        if (!$tocok || !is_array($toc) || !$conf['tocminheads'] || count($toc) < $conf['tocminheads']) {
159            $toc = [];
160        }
161    } elseif ($ACT == 'admin') {
162        // try to load admin plugin TOC
163        /** @var AdminPlugin $plugin */
164        if ($plugin = plugin_getRequestAdminPlugin()) {
165            $toc = $plugin->getTOC();
166            $TOC = $toc; // avoid later rebuild
167        }
168    }
169
170    Event::createAndTrigger('TPL_TOC_RENDER', $toc, null, false);
171    $html = html_TOC($toc);
172    if ($return) return $html;
173    echo $html;
174    return '';
175}
176
177/**
178 * Handle the admin page contents
179 *
180 * @return bool
181 *
182 * @author Andreas Gohr <andi@splitbrain.org>
183 */
184function tpl_admin()
185{
186    global $INFO;
187    global $TOC;
188    global $INPUT;
189
190    $plugin = null;
191    $class = $INPUT->str('page');
192    if (!empty($class)) {
193        $pluginlist = plugin_list('admin');
194
195        if (in_array($class, $pluginlist)) {
196            // attempt to load the plugin
197            /** @var AdminPlugin $plugin */
198            $plugin = plugin_load('admin', $class);
199        }
200    }
201
202    if ($plugin instanceof PluginInterface) {
203        if (!is_array($TOC)) $TOC = $plugin->getTOC(); //if TOC wasn't requested yet
204        if ($INFO['prependTOC']) tpl_toc();
205        $plugin->html();
206    } else {
207        $admin = new Admin();
208        $admin->show();
209    }
210    return true;
211}
212
213/**
214 * Print the correct HTML meta headers
215 *
216 * This has to go into the head section of your template.
217 *
218 * @param bool $alt Should feeds and alternative format links be added?
219 * @return bool
220 * @throws JsonException
221 *
222 * @author Andreas Gohr <andi@splitbrain.org>
223 * @triggers TPL_METAHEADER_OUTPUT
224 */
225function tpl_metaheaders($alt = true)
226{
227    global $ID;
228    global $REV;
229    global $INFO;
230    global $JSINFO;
231    global $ACT;
232    global $QUERY;
233    global $lang;
234    global $conf;
235    global $updateVersion;
236    /** @var Input $INPUT */
237    global $INPUT;
238
239    // prepare the head array
240    $head = [];
241
242    // prepare seed for js and css
243    $tseed = $updateVersion;
244    $depends = getConfigFiles('main');
245    $depends[] = DOKU_CONF . "tpl/" . $conf['template'] . "/style.ini";
246    foreach ($depends as $f) $tseed .= @filemtime($f);
247    $tseed = md5($tseed);
248
249    // the usual stuff
250    $head['meta'][] = ['name' => 'generator', 'content' => 'DokuWiki'];
251    if (actionOK('search')) {
252        $head['link'][] = [
253            'rel' => 'search',
254            'type' => 'application/opensearchdescription+xml',
255            'href' => DOKU_BASE . 'lib/exe/opensearch.php',
256            'title' => $conf['title']
257        ];
258    }
259
260    $head['link'][] = ['rel' => 'start', 'href' => DOKU_BASE];
261    if (actionOK('index')) {
262        $head['link'][] = [
263            'rel' => 'contents',
264            'href' => wl($ID, 'do=index', false, '&'),
265            'title' => $lang['btn_index']
266        ];
267    }
268
269    if (actionOK('manifest')) {
270        $head['link'][] = [
271            'rel' => 'manifest',
272            'href' => DOKU_BASE . 'lib/exe/manifest.php',
273            'crossorigin' => 'use-credentials' // See issue #4322
274        ];
275    }
276
277    $styleUtil = new StyleUtils();
278    $styleIni = $styleUtil->cssStyleini();
279    $replacements = $styleIni['replacements'];
280    if (!empty($replacements['__theme_color__'])) {
281        $head['meta'][] = [
282            'name' => 'theme-color',
283            'content' => $replacements['__theme_color__']
284        ];
285    }
286
287    if ($alt) {
288        if (actionOK('rss')) {
289            $head['link'][] = [
290                'rel' => 'alternate',
291                'type' => 'application/rss+xml',
292                'title' => $lang['btn_recent'],
293                'href' => DOKU_BASE . 'feed.php'
294            ];
295            $head['link'][] = [
296                'rel' => 'alternate',
297                'type' => 'application/rss+xml',
298                'title' => $lang['currentns'],
299                'href' => DOKU_BASE . 'feed.php?mode=list&ns=' . (isset($INFO) ? $INFO['namespace'] : '')
300            ];
301        }
302        if (($ACT == 'show' || $ACT == 'search') && $INFO['writable']) {
303            $head['link'][] = [
304                'rel' => 'edit',
305                'title' => $lang['btn_edit'],
306                'href' => wl($ID, 'do=edit', false, '&')
307            ];
308        }
309
310        if (actionOK('rss') && $ACT == 'search') {
311            $head['link'][] = [
312                'rel' => 'alternate',
313                'type' => 'application/rss+xml',
314                'title' => $lang['searchresult'],
315                'href' => DOKU_BASE . 'feed.php?mode=search&q=' . $QUERY
316            ];
317        }
318
319        if (actionOK('export_xhtml')) {
320            $head['link'][] = [
321                'rel' => 'alternate',
322                'type' => 'text/html',
323                'title' => $lang['plainhtml'],
324                'href' => exportlink($ID, 'xhtml', '', false, '&')
325            ];
326        }
327
328        if (actionOK('export_raw')) {
329            $head['link'][] = [
330                'rel' => 'alternate',
331                'type' => 'text/plain',
332                'title' => $lang['wikimarkup'],
333                'href' => exportlink($ID, 'raw', '', false, '&')
334            ];
335        }
336    }
337
338    // setup robot tags appropriate for different modes
339    if (($ACT == 'show' || $ACT == 'export_xhtml') && !$REV) {
340        if ($INFO['exists']) {
341            //delay indexing:
342            if ((time() - $INFO['lastmod']) >= $conf['indexdelay'] && !isHiddenPage($ID)) {
343                $head['meta'][] = ['name' => 'robots', 'content' => 'index,follow'];
344            } else {
345                $head['meta'][] = ['name' => 'robots', 'content' => 'noindex,nofollow'];
346            }
347            $canonicalUrl = wl($ID, '', true, '&');
348            if ($ID == $conf['start']) {
349                $canonicalUrl = DOKU_URL;
350            }
351            $head['link'][] = ['rel' => 'canonical', 'href' => $canonicalUrl];
352        } else {
353            $head['meta'][] = ['name' => 'robots', 'content' => 'noindex,follow'];
354        }
355    } elseif (defined('DOKU_MEDIADETAIL')) {
356        $head['meta'][] = ['name' => 'robots', 'content' => 'index,follow'];
357    } else {
358        $head['meta'][] = ['name' => 'robots', 'content' => 'noindex,nofollow'];
359    }
360
361    // set metadata
362    if ($ACT == 'show' || $ACT == 'export_xhtml') {
363        // keywords (explicit or implicit)
364        if (!empty($INFO['meta']['subject'])) {
365            $head['meta'][] = ['name' => 'keywords', 'content' => implode(',', $INFO['meta']['subject'])];
366        } else {
367            $head['meta'][] = ['name' => 'keywords', 'content' => str_replace(':', ',', $ID)];
368        }
369    }
370
371    // load stylesheets
372    $head['link'][] = [
373        'rel' => 'stylesheet',
374        'href' => DOKU_BASE . 'lib/exe/css.php?t=' . rawurlencode($conf['template']) . '&tseed=' . $tseed
375    ];
376
377    $script = "var NS='" . (isset($INFO) ? $INFO['namespace'] : '') . "';";
378    if ($conf['useacl'] && $INPUT->server->str('REMOTE_USER')) {
379        $script .= "var SIG=" . toolbar_signature() . ";";
380    }
381    jsinfo();
382    $script .= 'var JSINFO = ' . json_encode($JSINFO, JSON_THROW_ON_ERROR) . ';';
383    $script .= '(function(H){H.className=H.className.replace(/\bno-js\b/,\'js\')})(document.documentElement);';
384    $head['script'][] = ['_data' => $script];
385
386    // load jquery
387    $jquery = getCdnUrls();
388    foreach ($jquery as $src) {
389        $head['script'][] = [
390                '_data' => '',
391                'src' => $src
392            ] + ($conf['defer_js'] ? ['defer' => 'defer'] : []);
393    }
394
395    // load our javascript dispatcher
396    $head['script'][] = [
397            '_data' => '',
398            'src' => DOKU_BASE . 'lib/exe/js.php' . '?t=' . rawurlencode($conf['template']) . '&tseed=' . $tseed
399        ] + ($conf['defer_js'] ? ['defer' => 'defer'] : []);
400
401    // trigger event here
402    Event::createAndTrigger('TPL_METAHEADER_OUTPUT', $head, '_tpl_metaheaders_action', true);
403    return true;
404}
405
406/**
407 * prints the array build by tpl_metaheaders
408 *
409 * $data is an array of different header tags. Each tag can have multiple
410 * instances. Attributes are given as key value pairs. Values will be HTML
411 * encoded automatically so they should be provided as is in the $data array.
412 *
413 * For tags having a body attribute specify the body data in the special
414 * attribute '_data'. This field will NOT BE ESCAPED automatically.
415 *
416 * Inline scripts will use any nonce provided in the environment variable 'NONCE'.
417 *
418 * @param array $data
419 *
420 * @author Andreas Gohr <andi@splitbrain.org>
421 */
422function _tpl_metaheaders_action($data)
423{
424    $nonce = getenv('NONCE');
425    foreach ($data as $tag => $inst) {
426        foreach ($inst as $attr) {
427            if (empty($attr)) {
428                continue;
429            }
430            if ($nonce && $tag == 'script' && !empty($attr['_data'])) {
431                $attr['nonce'] = $nonce; // add nonce to inline script tags
432            }
433            echo '<', $tag, ' ', buildAttributes($attr);
434            if (isset($attr['_data']) || $tag == 'script') {
435                echo '>', $attr['_data'] ?? '', '</', $tag, '>';
436            } else {
437                echo '/>';
438            }
439            echo "\n";
440        }
441    }
442}
443
444/**
445 * Output the given script as inline script tag
446 *
447 * This function will add the nonce attribute if a nonce is available.
448 *
449 * The script is NOT automatically escaped!
450 *
451 * @param string $script
452 * @param bool $return Return or print directly?
453 * @return string|void
454 */
455function tpl_inlineScript($script, $return = false)
456{
457    $nonce = getenv('NONCE');
458    if ($nonce) {
459        $script = '<script nonce="' . $nonce . '">' . $script . '</script>';
460    } else {
461        $script = '<script>' . $script . '</script>';
462    }
463
464    if ($return) return $script;
465    echo $script;
466}
467
468/**
469 * Print a link
470 *
471 * Just builds a link.
472 *
473 * @param string $url
474 * @param string $name
475 * @param string $more
476 * @param bool $return if true return the link html, otherwise print
477 * @return bool|string html of the link, or true if printed
478 *
479 * @author Andreas Gohr <andi@splitbrain.org>
480 */
481function tpl_link($url, $name, $more = '', $return = false)
482{
483    $out = '<a href="' . $url . '" ';
484    if ($more) $out .= ' ' . $more;
485    $out .= ">$name</a>";
486    if ($return) return $out;
487    echo $out;
488    return true;
489}
490
491/**
492 * Prints a link to a WikiPage
493 *
494 * Wrapper around html_wikilink
495 *
496 * @param string $id page id
497 * @param string|null $name the name of the link
498 * @param bool $return
499 * @return true|string
500 *
501 * @author Andreas Gohr <andi@splitbrain.org>
502 */
503function tpl_pagelink($id, $name = null, $return = false)
504{
505    $out = '<bdi>' . html_wikilink($id, $name) . '</bdi>';
506    if ($return) return $out;
507    echo $out;
508    return true;
509}
510
511/**
512 * get the parent page
513 *
514 * Tries to find out which page is parent.
515 * returns false if none is available
516 *
517 * @param string $id page id
518 * @return false|string
519 *
520 * @author Andreas Gohr <andi@splitbrain.org>
521 */
522function tpl_getparent($id)
523{
524    $resolver = new PageResolver('root');
525
526    $parent = getNS($id) . ':';
527    $parent = $resolver->resolveId($parent);
528    if ($parent == $id) {
529        $pos = strrpos(getNS($id), ':');
530        $parent = substr($parent, 0, $pos) . ':';
531        $parent = $resolver->resolveId($parent);
532        if ($parent == $id) return false;
533    }
534    return $parent;
535}
536
537/**
538 * Print one of the buttons
539 *
540 * @param string $type
541 * @param bool $return
542 * @return bool|string html, or false if no data, true if printed
543 * @see    tpl_get_action
544 *
545 * @author Adrian Lang <mail@adrianlang.de>
546 * @deprecated 2017-09-01 see devel:menus
547 */
548function tpl_button($type, $return = false)
549{
550    dbg_deprecated('see devel:menus');
551    $data = tpl_get_action($type);
552    if ($data === false) {
553        return false;
554    } elseif (!is_array($data)) {
555        $out = sprintf($data, 'button');
556    } else {
557        /**
558         * @var string $accesskey
559         * @var string $id
560         * @var string $method
561         * @var array $params
562         */
563        extract($data);
564        if ($id === '#dokuwiki__top') {
565            $out = html_topbtn();
566        } else {
567            $out = html_btn($type, $id, $accesskey, $params, $method);
568        }
569    }
570    if ($return) return $out;
571    echo $out;
572    return true;
573}
574
575/**
576 * Like the action buttons but links
577 *
578 * @param string $type action command
579 * @param string $pre prefix of link
580 * @param string $suf suffix of link
581 * @param string $inner innerHML of link
582 * @param bool $return if true it returns html, otherwise prints
583 * @return bool|string html or false if no data, true if printed
584 *
585 * @see    tpl_get_action
586 * @author Adrian Lang <mail@adrianlang.de>
587 * @deprecated 2017-09-01 see devel:menus
588 */
589function tpl_actionlink($type, $pre = '', $suf = '', $inner = '', $return = false)
590{
591    dbg_deprecated('see devel:menus');
592    global $lang;
593    $data = tpl_get_action($type);
594    if ($data === false) {
595        return false;
596    } elseif (!is_array($data)) {
597        $out = sprintf($data, 'link');
598    } else {
599        /**
600         * @var string $accesskey
601         * @var string $id
602         * @var string $method
603         * @var bool $nofollow
604         * @var array $params
605         * @var string $replacement
606         */
607        extract($data);
608        if (strpos($id, '#') === 0) {
609            $linktarget = $id;
610        } else {
611            $linktarget = wl($id, $params);
612        }
613        $caption = $lang['btn_' . $type];
614        if (strpos($caption, '%s')) {
615            $caption = sprintf($caption, $replacement);
616        }
617        $akey = '';
618        $addTitle = '';
619        if ($accesskey) {
620            $akey = 'accesskey="' . $accesskey . '" ';
621            $addTitle = ' [' . strtoupper($accesskey) . ']';
622        }
623        $rel = $nofollow ? 'rel="nofollow" ' : '';
624        $out = tpl_link(
625            $linktarget,
626            $pre . ($inner ?: $caption) . $suf,
627            'class="action ' . $type . '" ' .
628            $akey . $rel .
629            'title="' . hsc($caption) . $addTitle . '"',
630            true
631        );
632    }
633    if ($return) return $out;
634    echo $out;
635    return true;
636}
637
638/**
639 * Check the actions and get data for buttons and links
640 *
641 * @param string $type
642 * @return array|bool|string
643 *
644 * @author Adrian Lang <mail@adrianlang.de>
645 * @author Andreas Gohr <andi@splitbrain.org>
646 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
647 * @deprecated 2017-09-01 see devel:menus
648 */
649function tpl_get_action($type)
650{
651    dbg_deprecated('see devel:menus');
652    if ($type == 'history') $type = 'revisions';
653    if ($type == 'subscription') $type = 'subscribe';
654    if ($type == 'img_backto') $type = 'imgBackto';
655
656    $class = '\\dokuwiki\\Menu\\Item\\' . ucfirst($type);
657    if (class_exists($class)) {
658        try {
659            /** @var AbstractItem $item */
660            $item = new $class();
661            $data = $item->getLegacyData();
662            $unknown = false;
663        } catch (RuntimeException $ignored) {
664            return false;
665        }
666    } else {
667        global $ID;
668        $data = [
669            'accesskey' => null,
670            'type' => $type,
671            'id' => $ID,
672            'method' => 'get',
673            'params' => ['do' => $type],
674            'nofollow' => true,
675            'replacement' => ''
676        ];
677        $unknown = true;
678    }
679
680    $evt = new Event('TPL_ACTION_GET', $data);
681    if ($evt->advise_before()) {
682        //handle unknown types
683        if ($unknown) {
684            $data = '[unknown %s type]';
685        }
686    }
687    $evt->advise_after();
688    unset($evt);
689
690    return $data;
691}
692
693/**
694 * Wrapper around tpl_button() and tpl_actionlink()
695 *
696 * @param string $type action command
697 * @param bool $link link or form button?
698 * @param string|bool $wrapper HTML element wrapper
699 * @param bool $return return or print
700 * @param string $pre prefix for links
701 * @param string $suf suffix for links
702 * @param string $inner inner HTML for links
703 * @return bool|string
704 *
705 * @author Anika Henke <anika@selfthinker.org>
706 * @deprecated 2017-09-01 see devel:menus
707 */
708function tpl_action($type, $link = false, $wrapper = false, $return = false, $pre = '', $suf = '', $inner = '')
709{
710    dbg_deprecated('see devel:menus');
711    $out = '';
712    if ($link) {
713        $out .= tpl_actionlink($type, $pre, $suf, $inner, true);
714    } else {
715        $out .= tpl_button($type, true);
716    }
717    if ($out && $wrapper) $out = "<$wrapper>$out</$wrapper>";
718
719    if ($return) return $out;
720    echo $out;
721    return (bool)$out;
722}
723
724/**
725 * Print the search form
726 *
727 * If the first parameter is given a div with the ID 'qsearch_out' will
728 * be added which instructs the ajax pagequicksearch to kick in and place
729 * its output into this div. The second parameter controls the propritary
730 * attribute autocomplete. If set to false this attribute will be set with an
731 * value of "off" to instruct the browser to disable it's own built in
732 * autocompletion feature (MSIE and Firefox)
733 *
734 * @param bool $ajax
735 * @param bool $autocomplete
736 * @return bool
737 *
738 * @author Andreas Gohr <andi@splitbrain.org>
739 */
740function tpl_searchform($ajax = true, $autocomplete = true)
741{
742    global $lang;
743    global $ACT;
744    global $QUERY;
745    global $ID;
746
747    // don't print the search form if search action has been disabled
748    if (!actionOK('search')) return false;
749
750    $searchForm = new Form([
751        'action' => wl(),
752        'method' => 'get',
753        'role' => 'search',
754        'class' => 'search',
755        'id' => 'dw__search',
756    ], true);
757    $searchForm->addTagOpen('div')->addClass('no');
758    $searchForm->setHiddenField('do', 'search');
759    $searchForm->setHiddenField('id', $ID);
760    $searchForm->addTextInput('q')
761        ->addClass('edit')
762        ->attrs([
763            'title' => '[F]',
764            'accesskey' => 'f',
765            'placeholder' => $lang['btn_search'],
766            'autocomplete' => $autocomplete ? 'on' : 'off',
767        ])
768        ->id('qsearch__in')
769        ->val($ACT === 'search' ? $QUERY : '')
770        ->useInput(false);
771    $searchForm->addButton('', $lang['btn_search'])->attrs([
772        'type' => 'submit',
773        'title' => $lang['btn_search'],
774    ]);
775    if ($ajax) {
776        $searchForm->addTagOpen('div')->id('qsearch__out')->addClass('ajax_qsearch JSpopup');
777        $searchForm->addTagClose('div');
778    }
779    $searchForm->addTagClose('div');
780
781    echo $searchForm->toHTML('QuickSearch');
782
783    return true;
784}
785
786/**
787 * Print the breadcrumbs trace
788 *
789 * @param string $sep Separator between entries
790 * @param bool $return return or print
791 * @return bool|string
792 *
793 * @author Andreas Gohr <andi@splitbrain.org>
794 */
795function tpl_breadcrumbs($sep = null, $return = false)
796{
797    global $lang;
798    global $conf;
799
800    //check if enabled
801    if (!$conf['breadcrumbs']) return false;
802
803    //set default
804    if (is_null($sep)) $sep = '•';
805
806    $out = '';
807
808    $crumbs = breadcrumbs(); //setup crumb trace
809
810    $crumbs_sep = ' <span class="bcsep">' . $sep . '</span> ';
811
812    //render crumbs, highlight the last one
813    $out .= '<span class="bchead">' . $lang['breadcrumb'] . '</span>';
814    $last = count($crumbs);
815    $i = 0;
816    foreach ($crumbs as $id => $name) {
817        $i++;
818        $out .= $crumbs_sep;
819        if ($i == $last) $out .= '<span class="curid">';
820        $out .= '<bdi>' . tpl_link(wl($id), hsc($name), 'class="breadcrumbs" title="' . $id . '"', true) . '</bdi>';
821        if ($i == $last) $out .= '</span>';
822    }
823    if ($return) return $out;
824    echo $out;
825    return (bool)$out;
826}
827
828/**
829 * Hierarchical breadcrumbs
830 *
831 * This code was suggested as replacement for the usual breadcrumbs.
832 * It only makes sense with a deep site structure.
833 *
834 * @param string $sep Separator between entries
835 * @param bool $return return or print
836 * @return bool|string
837 *
838 * @todo   May behave strangely in RTL languages
839 * @author <fredrik@averpil.com>
840 * @author Andreas Gohr <andi@splitbrain.org>
841 * @author Nigel McNie <oracle.shinoda@gmail.com>
842 * @author Sean Coates <sean@caedmon.net>
843 */
844function tpl_youarehere($sep = null, $return = false)
845{
846    global $conf;
847    global $ID;
848    global $lang;
849
850    // check if enabled
851    if (!$conf['youarehere']) return false;
852
853    //set default
854    if (is_null($sep)) $sep = ' » ';
855
856    $out = '';
857
858    $parts = explode(':', $ID);
859    $count = count($parts);
860
861    $out .= '<span class="bchead">' . $lang['youarehere'] . ' </span>';
862
863    // always print the startpage
864    $out .= '<span class="home">' . tpl_pagelink(':' . $conf['start'], null, true) . '</span>';
865
866    // print intermediate namespace links
867    $part = '';
868    for ($i = 0; $i < $count - 1; $i++) {
869        $part .= $parts[$i] . ':';
870        $page = $part;
871        if ($page == $conf['start']) continue; // Skip startpage
872
873        // output
874        $out .= $sep . tpl_pagelink($page, null, true);
875    }
876
877    // print current page, skipping start page, skipping for namespace index
878    if (isset($page)) {
879        $page = (new PageResolver('root'))->resolveId($page);
880        if ($page == $part . $parts[$i]) {
881            if ($return) return $out;
882            echo $out;
883            return true;
884        }
885    }
886    $page = $part . $parts[$i];
887    if ($page == $conf['start']) {
888        if ($return) return $out;
889        echo $out;
890        return true;
891    }
892    $out .= $sep;
893    $out .= tpl_pagelink($page, null, true);
894    if ($return) return $out;
895    echo $out;
896    return (bool)$out;
897}
898
899/**
900 * Print info if the user is logged in
901 * and show full name in that case
902 *
903 * Could be enhanced with a profile link in future?
904 *
905 * @return bool
906 *
907 * @author Andreas Gohr <andi@splitbrain.org>
908 */
909function tpl_userinfo()
910{
911    global $lang;
912    /** @var Input $INPUT */
913    global $INPUT;
914
915    if ($INPUT->server->str('REMOTE_USER')) {
916        echo $lang['loggedinas'] . ' ' . userlink();
917        return true;
918    }
919    return false;
920}
921
922/**
923 * Print some info about the current page
924 *
925 * @param bool $ret return content instead of printing it
926 * @return bool|string
927 *
928 * @author Andreas Gohr <andi@splitbrain.org>
929 */
930function tpl_pageinfo($ret = false)
931{
932    global $conf;
933    global $lang;
934    global $INFO;
935    global $ID;
936
937    // return if we are not allowed to view the page
938    if (!auth_quickaclcheck($ID)) {
939        return false;
940    }
941
942    // prepare date and path
943    $fn = $INFO['filepath'];
944    if (!$conf['fullpath']) {
945        if ($INFO['rev']) {
946            $fn = str_replace($conf['olddir'] . '/', '', $fn);
947        } else {
948            $fn = str_replace($conf['datadir'] . '/', '', $fn);
949        }
950    }
951    $fn = utf8_decodeFN($fn);
952    $dateLocal = dformat($INFO['lastmod']);
953    $dateIso = date(DATE_ISO8601, $INFO['lastmod']);
954
955    // print it
956    if ($INFO['exists']) {
957        $out = '<bdi>' . $fn . '</bdi>';
958        $out .= ' · ';
959        $out .= $lang['lastmod'];
960        $out .= ' ';
961        $out .= '<time datetime="' . $dateIso . '">' . $dateLocal . '</time>';
962        if ($INFO['editor']) {
963            $out .= ' ' . $lang['by'] . ' ';
964            $out .= '<bdi>' . editorinfo($INFO['editor']) . '</bdi>';
965        } else {
966            $out .= ' (' . $lang['external_edit'] . ')';
967        }
968        if ($INFO['locked']) {
969            $out .= ' · ';
970            $out .= $lang['lockedby'];
971            $out .= ' ';
972            $out .= '<bdi>' . editorinfo($INFO['locked']) . '</bdi>';
973        }
974        if ($ret) {
975            return $out;
976        } else {
977            echo $out;
978            return true;
979        }
980    }
981    return false;
982}
983
984/**
985 * Prints or returns the name of the given page (current one if none given).
986 *
987 * If useheading is enabled this will use the first headline else
988 * the given ID is used.
989 *
990 * @param string $id page id
991 * @param bool $ret return content instead of printing
992 * @return bool|string
993 *
994 * @author Andreas Gohr <andi@splitbrain.org>
995 */
996function tpl_pagetitle($id = null, $ret = false)
997{
998    global $ACT, $conf, $lang;
999
1000    if (is_null($id)) {
1001        global $ID;
1002        $id = $ID;
1003    }
1004
1005    $name = $id;
1006    if (useHeading('navigation')) {
1007        $first_heading = p_get_first_heading($id);
1008        if ($first_heading) $name = $first_heading;
1009    }
1010
1011    // default page title is the page name, modify with the current action
1012    switch ($ACT) {
1013        // admin functions
1014        case 'admin':
1015            $page_title = $lang['btn_admin'];
1016            // try to get the plugin name
1017            /** @var AdminPlugin $plugin */
1018            if ($plugin = plugin_getRequestAdminPlugin()) {
1019                $plugin_title = $plugin->getMenuText($conf['lang']);
1020                $page_title = $plugin_title ?: $plugin->getPluginName();
1021            }
1022            break;
1023
1024        // show action as title
1025        case 'login':
1026        case 'profile':
1027        case 'register':
1028        case 'resendpwd':
1029        case 'index':
1030        case 'search':
1031            $page_title = $lang['btn_' . $ACT];
1032            break;
1033
1034        // add pen during editing
1035        case 'edit':
1036        case 'preview':
1037            $page_title = "✎ " . $name;
1038            break;
1039
1040        // add action to page name
1041        case 'revisions':
1042            $page_title = $name . ' - ' . $lang['btn_revs'];
1043            break;
1044
1045        // add action to page name
1046        case 'backlink':
1047        case 'recent':
1048        case 'subscribe':
1049            $page_title = $name . ' - ' . $lang['btn_' . $ACT];
1050            break;
1051
1052        default: // SHOW and anything else not included
1053            $page_title = $name;
1054    }
1055
1056    if ($ret) {
1057        return hsc($page_title);
1058    } else {
1059        echo hsc($page_title);
1060        return true;
1061    }
1062}
1063
1064/**
1065 * Returns the requested EXIF/IPTC tag from the current image
1066 *
1067 * If $tags is an array all given tags are tried until a
1068 * value is found. If no value is found $alt is returned.
1069 *
1070 * Which texts are known is defined in the functions _exifTagNames
1071 * and _iptcTagNames() in inc/jpeg.php (You need to prepend IPTC
1072 * to the names of the latter one)
1073 *
1074 * Only allowed in: detail.php
1075 *
1076 * @param array|string $tags tag or array of tags to try
1077 * @param string $alt alternative output if no data was found
1078 * @param null|string $src the image src, uses global $SRC if not given
1079 * @return string
1080 *
1081 * @author Andreas Gohr <andi@splitbrain.org>
1082 */
1083function tpl_img_getTag($tags, $alt = '', $src = null)
1084{
1085    // Init Exif Reader
1086    global $SRC, $imgMeta;
1087
1088    if (is_null($src)) $src = $SRC;
1089    if (is_null($src)) return $alt;
1090
1091    if (!isset($imgMeta)) {
1092        $imgMeta = new JpegMeta($src);
1093    }
1094    if ($imgMeta === false) return $alt;
1095    $info = cleanText($imgMeta->getField($tags));
1096    if (!$info) return $alt;
1097    return $info;
1098}
1099
1100
1101/**
1102 * Garbage collects up the open JpegMeta object.
1103 */
1104function tpl_img_close()
1105{
1106    global $imgMeta;
1107    $imgMeta = null;
1108}
1109
1110/**
1111 * Prints a html description list of the metatags of the current image
1112 */
1113function tpl_img_meta()
1114{
1115    global $lang;
1116
1117    $tags = tpl_get_img_meta();
1118
1119    echo '<dl>';
1120    foreach ($tags as $tag) {
1121        $label = $lang[$tag['langkey']];
1122        if (!$label) $label = $tag['langkey'] . ':';
1123
1124        echo '<dt>' . $label . '</dt><dd>';
1125        if ($tag['type'] == 'date') {
1126            echo dformat($tag['value']);
1127        } else {
1128            echo hsc($tag['value']);
1129        }
1130        echo '</dd>';
1131    }
1132    echo '</dl>';
1133}
1134
1135/**
1136 * Returns metadata as configured in mediameta config file, ready for creating html
1137 *
1138 * @return array with arrays containing the entries:
1139 *   - string langkey  key to lookup in the $lang var, if not found printed as is
1140 *   - string type     type of value
1141 *   - string value    tag value (unescaped)
1142 */
1143function tpl_get_img_meta()
1144{
1145
1146    $config_files = getConfigFiles('mediameta');
1147    foreach ($config_files as $config_file) {
1148        if (file_exists($config_file)) {
1149            include($config_file);
1150        }
1151    }
1152    $tags = [];
1153    foreach ($fields as $tag) {
1154        $t = [];
1155        if (!empty($tag[0])) {
1156            $t = [$tag[0]];
1157        }
1158        if (isset($tag[3]) && is_array($tag[3])) {
1159            $t = array_merge($t, $tag[3]);
1160        }
1161        $value = tpl_img_getTag($t);
1162        if ($value) {
1163            $tags[] = ['langkey' => $tag[1], 'type' => $tag[2], 'value' => $value];
1164        }
1165    }
1166    return $tags;
1167}
1168
1169/**
1170 * Prints the image with a link to the full sized version
1171 *
1172 * Only allowed in: detail.php
1173 *
1174 * @triggers TPL_IMG_DISPLAY
1175 * @param int $maxwidth - maximal width of the image
1176 * @param int $maxheight - maximal height of the image
1177 * @param bool $link - link to the orginal size?
1178 * @param array $params - additional image attributes
1179 * @return bool Result of TPL_IMG_DISPLAY
1180 */
1181function tpl_img($maxwidth = 0, $maxheight = 0, $link = true, $params = null)
1182{
1183    global $IMG;
1184    /** @var Input $INPUT */
1185    global $INPUT;
1186    global $REV;
1187    $w = (int)tpl_img_getTag('File.Width');
1188    $h = (int)tpl_img_getTag('File.Height');
1189
1190    //resize to given max values
1191    $ratio = 1;
1192    if ($w >= $h) {
1193        if ($maxwidth && $w >= $maxwidth) {
1194            $ratio = $maxwidth / $w;
1195        } elseif ($maxheight && $h > $maxheight) {
1196            $ratio = $maxheight / $h;
1197        }
1198    } elseif ($maxheight && $h >= $maxheight) {
1199        $ratio = $maxheight / $h;
1200    } elseif ($maxwidth && $w > $maxwidth) {
1201        $ratio = $maxwidth / $w;
1202    }
1203    if ($ratio) {
1204        $w = floor($ratio * $w);
1205        $h = floor($ratio * $h);
1206    }
1207
1208    //prepare URLs
1209    $url = ml($IMG, ['cache' => $INPUT->str('cache'), 'rev' => $REV], true, '&');
1210    $src = ml($IMG, ['cache' => $INPUT->str('cache'), 'rev' => $REV, 'w' => $w, 'h' => $h], true, '&');
1211
1212    //prepare attributes
1213    $alt = tpl_img_getTag('Simple.Title');
1214    if (is_null($params)) {
1215        $p = [];
1216    } else {
1217        $p = $params;
1218    }
1219    if ($w) $p['width'] = $w;
1220    if ($h) $p['height'] = $h;
1221    $p['class'] = 'img_detail';
1222    if ($alt) {
1223        $p['alt'] = $alt;
1224        $p['title'] = $alt;
1225    } else {
1226        $p['alt'] = '';
1227    }
1228    $p['src'] = $src;
1229
1230    $data = ['url' => ($link ? $url : null), 'params' => $p];
1231    return Event::createAndTrigger('TPL_IMG_DISPLAY', $data, '_tpl_img_action', true);
1232}
1233
1234/**
1235 * Default action for TPL_IMG_DISPLAY
1236 *
1237 * @param array $data
1238 * @return bool
1239 */
1240function _tpl_img_action($data)
1241{
1242    global $lang;
1243    $p = buildAttributes($data['params']);
1244
1245    if ($data['url']) echo '<a href="' . hsc($data['url']) . '" title="' . $lang['mediaview'] . '">';
1246    echo '<img ' . $p . '/>';
1247    if ($data['url']) echo '</a>';
1248    return true;
1249}
1250
1251/**
1252 * This function inserts a small gif which in reality is the indexer function.
1253 *
1254 * Should be called somewhere at the very end of the main.php template
1255 *
1256 * @return bool
1257 */
1258function tpl_indexerWebBug()
1259{
1260    global $ID;
1261
1262    $p = [];
1263    $p['src'] = DOKU_BASE . 'lib/exe/taskrunner.php?id=' . rawurlencode($ID) .
1264        '&' . time();
1265    $p['width'] = 2; //no more 1x1 px image because we live in times of ad blockers...
1266    $p['height'] = 1;
1267    $p['alt'] = '';
1268    $att = buildAttributes($p);
1269    echo "<img $att />";
1270    return true;
1271}
1272
1273/**
1274 * tpl_getConf($id)
1275 *
1276 * use this function to access template configuration variables
1277 *
1278 * @param string $id name of the value to access
1279 * @param mixed $notset what to return if the setting is not available
1280 * @return mixed
1281 */
1282function tpl_getConf($id, $notset = false)
1283{
1284    global $conf;
1285    static $tpl_configloaded = false;
1286
1287    $tpl = $conf['template'];
1288
1289    if (!$tpl_configloaded) {
1290        $tconf = tpl_loadConfig();
1291        if ($tconf !== false) {
1292            foreach ($tconf as $key => $value) {
1293                if (isset($conf['tpl'][$tpl][$key])) continue;
1294                $conf['tpl'][$tpl][$key] = $value;
1295            }
1296            $tpl_configloaded = true;
1297        }
1298    }
1299
1300    return $conf['tpl'][$tpl][$id] ?? $notset;
1301}
1302
1303/**
1304 * tpl_loadConfig()
1305 *
1306 * reads all template configuration variables
1307 * this function is automatically called by tpl_getConf()
1308 *
1309 * @return false|array
1310 */
1311function tpl_loadConfig()
1312{
1313
1314    $file = tpl_incdir() . '/conf/default.php';
1315    $conf = [];
1316
1317    if (!file_exists($file)) return false;
1318
1319    // load default config file
1320    include($file);
1321
1322    return $conf;
1323}
1324
1325// language methods
1326
1327/**
1328 * tpl_getLang($id)
1329 *
1330 * use this function to access template language variables
1331 *
1332 * @param string $id key of language string
1333 * @return string
1334 */
1335function tpl_getLang($id)
1336{
1337    static $lang = [];
1338
1339    if (count($lang) === 0) {
1340        global $conf, $config_cascade; // definitely don't invoke "global $lang"
1341
1342        $path = tpl_incdir() . 'lang/';
1343
1344        $lang = [];
1345
1346        // don't include once
1347        @include($path . 'en/lang.php');
1348        foreach ($config_cascade['lang']['template'] as $config_file) {
1349            if (file_exists($config_file . $conf['template'] . '/en/lang.php')) {
1350                include($config_file . $conf['template'] . '/en/lang.php');
1351            }
1352        }
1353
1354        if ($conf['lang'] != 'en') {
1355            @include($path . $conf['lang'] . '/lang.php');
1356            foreach ($config_cascade['lang']['template'] as $config_file) {
1357                if (file_exists($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php')) {
1358                    include($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php');
1359                }
1360            }
1361        }
1362    }
1363    return $lang[$id] ?? '';
1364}
1365
1366/**
1367 * Retrieve a language dependent file and pass to xhtml renderer for display
1368 * template equivalent of p_locale_xhtml()
1369 *
1370 * @param string $id id of language dependent wiki page
1371 * @return  string     parsed contents of the wiki page in xhtml format
1372 */
1373function tpl_locale_xhtml($id)
1374{
1375    return p_cached_output(tpl_localeFN($id));
1376}
1377
1378/**
1379 * Prepends appropriate path for a language dependent filename
1380 *
1381 * @param string $id id of localized text
1382 * @return string wiki text
1383 */
1384function tpl_localeFN($id)
1385{
1386    $path = tpl_incdir() . 'lang/';
1387    global $conf;
1388    $file = DOKU_CONF . 'template_lang/' . $conf['template'] . '/' . $conf['lang'] . '/' . $id . '.txt';
1389    if (!file_exists($file)) {
1390        $file = $path . $conf['lang'] . '/' . $id . '.txt';
1391        if (!file_exists($file)) {
1392            //fall back to english
1393            $file = $path . 'en/' . $id . '.txt';
1394        }
1395    }
1396    return $file;
1397}
1398
1399/**
1400 * prints the "main content" in the mediamanager popup
1401 *
1402 * Depending on the user's actions this may be a list of
1403 * files in a namespace, the meta editing dialog or
1404 * a message of referencing pages
1405 *
1406 * Only allowed in mediamanager.php
1407 *
1408 * @triggers MEDIAMANAGER_CONTENT_OUTPUT
1409 * @param bool $fromajax - set true when calling this function via ajax
1410 * @param string $sort
1411 *
1412 * @author Andreas Gohr <andi@splitbrain.org>
1413 */
1414function tpl_mediaContent($fromajax = false, $sort = 'natural')
1415{
1416    global $IMG;
1417    global $AUTH;
1418    global $INUSE;
1419    global $NS;
1420    global $JUMPTO;
1421    /** @var Input $INPUT */
1422    global $INPUT;
1423
1424    $do = $INPUT->extract('do')->str('do');
1425    if (in_array($do, ['save', 'cancel'])) $do = '';
1426
1427    if (!$do) {
1428        if ($INPUT->bool('edit')) {
1429            $do = 'metaform';
1430        } elseif (is_array($INUSE)) {
1431            $do = 'filesinuse';
1432        } else {
1433            $do = 'filelist';
1434        }
1435    }
1436
1437    // output the content pane, wrapped in an event.
1438    if (!$fromajax) echo '<div id="media__content">';
1439    $data = ['do' => $do];
1440    $evt = new Event('MEDIAMANAGER_CONTENT_OUTPUT', $data);
1441    if ($evt->advise_before()) {
1442        $do = $data['do'];
1443        if ($do == 'filesinuse') {
1444            media_filesinuse($INUSE, $IMG);
1445        } elseif ($do == 'filelist') {
1446            media_filelist($NS, $AUTH, $JUMPTO, false, $sort);
1447        } elseif ($do == 'searchlist') {
1448            media_searchlist($INPUT->str('q'), $NS, $AUTH);
1449        } else {
1450            msg('Unknown action ' . hsc($do), -1);
1451        }
1452    }
1453    $evt->advise_after();
1454    unset($evt);
1455    if (!$fromajax) echo '</div>';
1456}
1457
1458/**
1459 * Prints the central column in full-screen media manager
1460 * Depending on the opened tab this may be a list of
1461 * files in a namespace, upload form or search form
1462 *
1463 * @author Kate Arzamastseva <pshns@ukr.net>
1464 */
1465function tpl_mediaFileList()
1466{
1467    global $AUTH;
1468    global $NS;
1469    global $JUMPTO;
1470    global $lang;
1471    /** @var Input $INPUT */
1472    global $INPUT;
1473
1474    $opened_tab = $INPUT->str('tab_files');
1475    if (!$opened_tab || !in_array($opened_tab, ['files', 'upload', 'search'])) $opened_tab = 'files';
1476    if ($INPUT->str('mediado') == 'update') $opened_tab = 'upload';
1477
1478    echo '<h2 class="a11y">' . $lang['mediaselect'] . '</h2>' . NL;
1479
1480    media_tabs_files($opened_tab);
1481
1482    echo '<div class="panelHeader">' . NL;
1483    echo '<h3>';
1484    $tabTitle = $NS ?: '[' . $lang['mediaroot'] . ']';
1485    printf($lang['media_' . $opened_tab], '<strong>' . hsc($tabTitle) . '</strong>');
1486    echo '</h3>' . NL;
1487    if ($opened_tab === 'search' || $opened_tab === 'files') {
1488        media_tab_files_options();
1489    }
1490    echo '</div>' . NL;
1491
1492    echo '<div class="panelContent">' . NL;
1493    if ($opened_tab == 'files') {
1494        media_tab_files($NS, $AUTH, $JUMPTO);
1495    } elseif ($opened_tab == 'upload') {
1496        media_tab_upload($NS, $AUTH, $JUMPTO);
1497    } elseif ($opened_tab == 'search') {
1498        media_tab_search($NS, $AUTH);
1499    }
1500    echo '</div>' . NL;
1501}
1502
1503/**
1504 * Prints the third column in full-screen media manager
1505 * Depending on the opened tab this may be details of the
1506 * selected file, the meta editing dialog or
1507 * list of file revisions
1508 *
1509 * @param string $image
1510 * @param boolean $rev
1511 *
1512 * @author Kate Arzamastseva <pshns@ukr.net>
1513 */
1514function tpl_mediaFileDetails($image, $rev)
1515{
1516    global $conf, $DEL, $lang;
1517    /** @var Input $INPUT */
1518    global $INPUT;
1519
1520    $removed = (
1521        !file_exists(mediaFN($image)) &&
1522        file_exists(mediaMetaFN($image, '.changes')) &&
1523        $conf['mediarevisions']
1524    );
1525    if (!$image || (!file_exists(mediaFN($image)) && !$removed) || $DEL) return;
1526    if ($rev && !file_exists(mediaFN($image, $rev))) $rev = false;
1527    $ns = getNS($image);
1528    $do = $INPUT->str('mediado');
1529
1530    $opened_tab = $INPUT->str('tab_details');
1531
1532    $tab_array = ['view'];
1533    [, $mime] = mimetype($image);
1534    if ($mime == 'image/jpeg') {
1535        $tab_array[] = 'edit';
1536    }
1537    if ($conf['mediarevisions']) {
1538        $tab_array[] = 'history';
1539    }
1540
1541    if (!$opened_tab || !in_array($opened_tab, $tab_array)) $opened_tab = 'view';
1542    if ($INPUT->bool('edit')) $opened_tab = 'edit';
1543    if ($do == 'restore') $opened_tab = 'view';
1544
1545    media_tabs_details($image, $opened_tab);
1546
1547    echo '<div class="panelHeader"><h3>';
1548    [$ext] = mimetype($image, false);
1549    $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
1550    $class = 'select mediafile mf_' . $class;
1551
1552    $attributes = $rev ? ['rev' => $rev] : [];
1553    $tabTitle = sprintf(
1554        '<strong><a href="%s" class="%s" title="%s">%s</a></strong>',
1555        ml($image, $attributes),
1556        $class,
1557        $lang['mediaview'],
1558        $image
1559    );
1560    if ($opened_tab === 'view' && $rev) {
1561        printf($lang['media_viewold'], $tabTitle, dformat($rev));
1562    } else {
1563        printf($lang['media_' . $opened_tab], $tabTitle);
1564    }
1565
1566    echo '</h3></div>' . NL;
1567
1568    echo '<div class="panelContent">' . NL;
1569
1570    if ($opened_tab == 'view') {
1571        media_tab_view($image, $ns, null, $rev);
1572    } elseif ($opened_tab == 'edit' && !$removed) {
1573        media_tab_edit($image, $ns);
1574    } elseif ($opened_tab == 'history' && $conf['mediarevisions']) {
1575        media_tab_history($image, $ns);
1576    }
1577
1578    echo '</div>' . NL;
1579}
1580
1581/**
1582 * prints the namespace tree in the mediamanager popup
1583 *
1584 * Only allowed in mediamanager.php
1585 *
1586 * @author Andreas Gohr <andi@splitbrain.org>
1587 */
1588function tpl_mediaTree()
1589{
1590    global $NS;
1591    echo '<div id="media__tree">';
1592    media_nstree($NS);
1593    echo '</div>';
1594}
1595
1596/**
1597 * Print a dropdown menu with all DokuWiki actions
1598 *
1599 * Note: this will not use any pretty URLs
1600 *
1601 * @param string $empty empty option label
1602 * @param string $button submit button label
1603 *
1604 * @author Andreas Gohr <andi@splitbrain.org>
1605 * @deprecated 2017-09-01 see devel:menus
1606 */
1607function tpl_actiondropdown($empty = '', $button = '&gt;')
1608{
1609    dbg_deprecated('see devel:menus');
1610    $menu = new MobileMenu();
1611    echo $menu->getDropdown($empty, $button);
1612}
1613
1614/**
1615 * Print a informational line about the used license
1616 *
1617 * @param string $img print image? (|button|badge)
1618 * @param bool $imgonly skip the textual description?
1619 * @param bool $return when true don't print, but return HTML
1620 * @param bool $wrap wrap in div with class="license"?
1621 * @return string
1622 *
1623 * @author Andreas Gohr <andi@splitbrain.org>
1624 */
1625function tpl_license($img = 'badge', $imgonly = false, $return = false, $wrap = true)
1626{
1627    global $license;
1628    global $conf;
1629    global $lang;
1630    if (!$conf['license']) return '';
1631    if (!is_array($license[$conf['license']])) return '';
1632    $lic = $license[$conf['license']];
1633    $target = ($conf['target']['extern']) ? ' target="' . $conf['target']['extern'] . '"' : '';
1634
1635    $out = '';
1636    if ($wrap) $out .= '<div class="license">';
1637    if ($img) {
1638        $src = license_img($img);
1639        if ($src) {
1640            $out .= '<a href="' . $lic['url'] . '" rel="license"' . $target;
1641            $out .= '><img src="' . DOKU_BASE . $src . '" alt="' . $lic['name'] . '" /></a>';
1642            if (!$imgonly) $out .= ' ';
1643        }
1644    }
1645    if (!$imgonly) {
1646        $out .= $lang['license'] . ' ';
1647        $out .= '<bdi><a href="' . $lic['url'] . '" rel="license" class="urlextern"' . $target;
1648        $out .= '>' . $lic['name'] . '</a></bdi>';
1649    }
1650    if ($wrap) $out .= '</div>';
1651
1652    if ($return) return $out;
1653    echo $out;
1654    return '';
1655}
1656
1657/**
1658 * Includes the rendered HTML of a given page
1659 *
1660 * This function is useful to populate sidebars or similar features in a
1661 * template
1662 *
1663 * @param string $pageid The page name you want to include
1664 * @param bool $print Should the content be printed or returned only
1665 * @param bool $propagate Search higher namespaces, too?
1666 * @param bool $useacl Include the page only if the ACLs check out?
1667 * @return bool|null|string
1668 */
1669function tpl_include_page($pageid, $print = true, $propagate = false, $useacl = true)
1670{
1671    if ($propagate) {
1672        $pageid = page_findnearest($pageid, $useacl);
1673    } elseif ($useacl && auth_quickaclcheck($pageid) == AUTH_NONE) {
1674        return false;
1675    }
1676    if (!$pageid) return false;
1677
1678    global $TOC;
1679    $oldtoc = $TOC;
1680    $html = p_wiki_xhtml($pageid, '', false);
1681    $TOC = $oldtoc;
1682
1683    if ($print) echo $html;
1684    return $html;
1685}
1686
1687/**
1688 * Display the subscribe form
1689 *
1690 * @author Adrian Lang <lang@cosmocode.de>
1691 * @deprecated 2020-07-23
1692 */
1693function tpl_subscribe()
1694{
1695    dbg_deprecated(Subscribe::class . '::show()');
1696    (new Subscribe())->show();
1697}
1698
1699/**
1700 * Tries to send already created content right to the browser
1701 *
1702 * Wraps around ob_flush() and flush()
1703 *
1704 * @author Andreas Gohr <andi@splitbrain.org>
1705 */
1706function tpl_flush()
1707{
1708    if (ob_get_level() > 0) ob_flush();
1709    flush();
1710}
1711
1712/**
1713 * Tries to find a ressource file in the given locations.
1714 *
1715 * If a given location starts with a colon it is assumed to be a media
1716 * file, otherwise it is assumed to be relative to the current template
1717 *
1718 * @param string[] $search locations to look at
1719 * @param bool $abs if to use absolute URL
1720 * @param array    &$imginfo filled with getimagesize()
1721 * @param bool $fallback use fallback image if target isn't found or return 'false' if potential
1722 *                                false result is required
1723 * @return string
1724 *
1725 * @author Andreas  Gohr <andi@splitbrain.org>
1726 */
1727function tpl_getMediaFile($search, $abs = false, &$imginfo = null, $fallback = true)
1728{
1729    $img = '';
1730    $file = '';
1731    $ismedia = false;
1732    // loop through candidates until a match was found:
1733    foreach ($search as $img) {
1734        if (str_starts_with($img, ':')) {
1735            $file = mediaFN($img);
1736            $ismedia = true;
1737        } else {
1738            $file = tpl_incdir() . $img;
1739            $ismedia = false;
1740        }
1741
1742        if (file_exists($file)) break;
1743    }
1744
1745    // manage non existing target
1746    if (!file_exists($file)) {
1747        // give result for fallback image
1748        if ($fallback) {
1749            $file = DOKU_INC . 'lib/images/blank.gif';
1750            // stop process if false result is required (if $fallback is false)
1751        } else {
1752            return false;
1753        }
1754    }
1755
1756    // fetch image data if requested
1757    if (!is_null($imginfo)) {
1758        $imginfo = getimagesize($file);
1759    }
1760
1761    // build URL
1762    if ($ismedia) {
1763        $url = ml($img, '', true, '', $abs);
1764    } else {
1765        $url = tpl_basedir() . $img;
1766        if ($abs) $url = DOKU_URL . substr($url, strlen(DOKU_REL));
1767    }
1768
1769    return $url;
1770}
1771
1772/**
1773 * PHP include a file
1774 *
1775 * either from the conf directory if it exists, otherwise use
1776 * file in the template's root directory.
1777 *
1778 * The function honours config cascade settings and looks for the given
1779 * file next to the ´main´ config files, in the order protected, local,
1780 * default.
1781 *
1782 * Note: no escaping or sanity checking is done here. Never pass user input
1783 * to this function!
1784 *
1785 * @param string $file
1786 *
1787 * @author Andreas Gohr <andi@splitbrain.org>
1788 * @author Anika Henke <anika@selfthinker.org>
1789 */
1790function tpl_includeFile($file)
1791{
1792    global $config_cascade;
1793    foreach (['protected', 'local', 'default'] as $config_group) {
1794        if (empty($config_cascade['main'][$config_group])) continue;
1795        foreach ($config_cascade['main'][$config_group] as $conf_file) {
1796            $dir = dirname($conf_file);
1797            if (file_exists("$dir/$file")) {
1798                include("$dir/$file");
1799                return;
1800            }
1801        }
1802    }
1803
1804    // still here? try the template dir
1805    $file = tpl_incdir() . $file;
1806    if (file_exists($file)) {
1807        include($file);
1808    }
1809}
1810
1811/**
1812 * Returns <link> tag for various icon types (favicon|mobile|generic)
1813 *
1814 * @param array $types - list of icon types to display (favicon|mobile|generic)
1815 * @return string
1816 *
1817 * @author Anika Henke <anika@selfthinker.org>
1818 */
1819function tpl_favicon($types = ['favicon'])
1820{
1821
1822    $return = '';
1823
1824    foreach ($types as $type) {
1825        switch ($type) {
1826            case 'favicon':
1827                $look = [':wiki:favicon.ico', ':favicon.ico', 'images/favicon.ico'];
1828                $return .= '<link rel="shortcut icon" href="' . tpl_getMediaFile($look) . '" />' . NL;
1829                break;
1830            case 'mobile':
1831                $look = [':wiki:apple-touch-icon.png', ':apple-touch-icon.png', 'images/apple-touch-icon.png'];
1832                $return .= '<link rel="apple-touch-icon" href="' . tpl_getMediaFile($look) . '" />' . NL;
1833                break;
1834            case 'generic':
1835                // ideal world solution, which doesn't work in any browser yet
1836                $look = [':wiki:favicon.svg', ':favicon.svg', 'images/favicon.svg'];
1837                $return .= '<link rel="icon" href="' . tpl_getMediaFile($look) . '" type="image/svg+xml" />' . NL;
1838                break;
1839        }
1840    }
1841
1842    return $return;
1843}
1844
1845/**
1846 * Prints full-screen media manager
1847 *
1848 * @author Kate Arzamastseva <pshns@ukr.net>
1849 */
1850function tpl_media()
1851{
1852    global $NS, $IMG, $JUMPTO, $REV, $lang, $fullscreen, $INPUT;
1853    $fullscreen = true;
1854    require_once DOKU_INC . 'lib/exe/mediamanager.php';
1855
1856    $rev = '';
1857    $image = cleanID($INPUT->str('image'));
1858    if (isset($IMG)) $image = $IMG;
1859    if (isset($JUMPTO)) $image = $JUMPTO;
1860    if (isset($REV) && !$JUMPTO) $rev = $REV;
1861
1862    echo '<div id="mediamanager__page">' . NL;
1863    echo '<h1>' . $lang['btn_media'] . '</h1>' . NL;
1864    html_msgarea();
1865
1866    echo '<div class="panel namespaces">' . NL;
1867    echo '<h2>' . $lang['namespaces'] . '</h2>' . NL;
1868    echo '<div class="panelHeader">';
1869    echo $lang['media_namespaces'];
1870    echo '</div>' . NL;
1871
1872    echo '<div class="panelContent" id="media__tree">' . NL;
1873    media_nstree($NS);
1874    echo '</div>' . NL;
1875    echo '</div>' . NL;
1876
1877    echo '<div class="panel filelist">' . NL;
1878    tpl_mediaFileList();
1879    echo '</div>' . NL;
1880
1881    echo '<div class="panel file">' . NL;
1882    echo '<h2 class="a11y">' . $lang['media_file'] . '</h2>' . NL;
1883    tpl_mediaFileDetails($image, $rev);
1884    echo '</div>' . NL;
1885
1886    echo '</div>' . NL;
1887}
1888
1889/**
1890 * Return useful layout classes
1891 *
1892 * @return string
1893 *
1894 * @author Anika Henke <anika@selfthinker.org>
1895 */
1896function tpl_classes()
1897{
1898    global $ACT, $conf, $ID, $INFO;
1899    /** @var Input $INPUT */
1900    global $INPUT;
1901
1902    $classes = [
1903        'dokuwiki',
1904        'mode_' . $ACT,
1905        'tpl_' . $conf['template'],
1906        $INPUT->server->bool('REMOTE_USER') ? 'loggedIn' : '',
1907        (isset($INFO['exists']) && $INFO['exists']) ? '' : 'notFound',
1908        ($ID == $conf['start']) ? 'home' : ''
1909    ];
1910    return implode(' ', $classes);
1911}
1912
1913/**
1914 * Create event for tools menues
1915 *
1916 * @param string $toolsname name of menu
1917 * @param array $items
1918 * @param string $view e.g. 'main', 'detail', ...
1919 *
1920 * @author Anika Henke <anika@selfthinker.org>
1921 * @deprecated 2017-09-01 see devel:menus
1922 */
1923function tpl_toolsevent($toolsname, $items, $view = 'main')
1924{
1925    dbg_deprecated('see devel:menus');
1926    $data = ['view' => $view, 'items' => $items];
1927
1928    $hook = 'TEMPLATE_' . strtoupper($toolsname) . '_DISPLAY';
1929    $evt = new Event($hook, $data);
1930    if ($evt->advise_before()) {
1931        foreach ($evt->data['items'] as $html) echo $html;
1932    }
1933    $evt->advise_after();
1934}
1935