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