xref: /dokuwiki/inc/template.php (revision 6c16a3a9aa602bb7e269fb6d5d18e1353e17f97f)
16b13307fSandi<?php
2d4f83172SAndreas Gohr
36b13307fSandi/**
46b13307fSandi * DokuWiki template functions
56b13307fSandi *
66b13307fSandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
76b13307fSandi * @author     Andreas Gohr <andi@splitbrain.org>
86b13307fSandi */
9d4f83172SAndreas Gohr
1024870174SAndreas Gohruse dokuwiki\ActionRouter;
1124870174SAndreas Gohruse dokuwiki\Action\Exception\FatalException;
1224870174SAndreas Gohruse dokuwiki\Extension\PluginInterface;
1324870174SAndreas Gohruse dokuwiki\Ui\Admin;
1424870174SAndreas Gohruse dokuwiki\StyleUtils;
1524870174SAndreas Gohruse dokuwiki\Menu\Item\AbstractItem;
1624870174SAndreas Gohruse dokuwiki\Form\Form;
1724870174SAndreas Gohruse dokuwiki\Menu\MobileMenu;
1824870174SAndreas Gohruse dokuwiki\Ui\Subscribe;
19e1d9dcc8SAndreas Gohruse dokuwiki\Extension\AdminPlugin;
20e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event;
212cd6cc0aSAndreas Gohruse dokuwiki\File\PageResolver;
22e1d9dcc8SAndreas Gohr
236b13307fSandi/**
24ac7a515fSAndreas Gohr * Access a template file
25ac7a515fSAndreas Gohr *
26ac7a515fSAndreas Gohr * Returns the path to the given file inside the current template, uses
27ac7a515fSAndreas Gohr * default template if the custom version doesn't exist.
285a892029SAndreas Gohr *
29ac7a515fSAndreas Gohr * @param string $file
30ac7a515fSAndreas Gohr * @return string
314dc42f7fSGerrit Uitslag *
324dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
335a892029SAndreas Gohr */
34d868eb89SAndreas Gohrfunction template($file)
35d868eb89SAndreas Gohr{
365a892029SAndreas Gohr    global $conf;
375a892029SAndreas Gohr
38ac7a515fSAndreas Gohr    if (@is_readable(DOKU_INC . 'lib/tpl/' . $conf['template'] . '/' . $file))
39ac7a515fSAndreas Gohr        return DOKU_INC . 'lib/tpl/' . $conf['template'] . '/' . $file;
405a892029SAndreas Gohr
41ac7a515fSAndreas Gohr    return DOKU_INC . 'lib/tpl/dokuwiki/' . $file;
425a892029SAndreas Gohr}
435a892029SAndreas Gohr
44c4766956SAndreas Gohr/**
45c4766956SAndreas Gohr * Convenience function to access template dir from local FS
46c4766956SAndreas Gohr *
47c4766956SAndreas Gohr * This replaces the deprecated DOKU_TPLINC constant
48c4766956SAndreas Gohr *
49afb2c082SAndreas Gohr * @param string $tpl The template to use, default to current one
50ac7a515fSAndreas Gohr * @return string
514dc42f7fSGerrit Uitslag *
524dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
53c4766956SAndreas Gohr */
54d868eb89SAndreas Gohrfunction tpl_incdir($tpl = '')
55d868eb89SAndreas Gohr{
5675b14482SAndreas Gohr    global $conf;
57afb2c082SAndreas Gohr    if (!$tpl) $tpl = $conf['template'];
58afb2c082SAndreas Gohr    return DOKU_INC . 'lib/tpl/' . $tpl . '/';
59c4766956SAndreas Gohr}
60c4766956SAndreas Gohr
61c4766956SAndreas Gohr/**
62c4766956SAndreas Gohr * Convenience function to access template dir from web
63c4766956SAndreas Gohr *
64c4766956SAndreas Gohr * This replaces the deprecated DOKU_TPL constant
65c4766956SAndreas Gohr *
66afb2c082SAndreas Gohr * @param string $tpl The template to use, default to current one
67ac7a515fSAndreas Gohr * @return string
684dc42f7fSGerrit Uitslag *
694dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
70c4766956SAndreas Gohr */
71d868eb89SAndreas Gohrfunction tpl_basedir($tpl = '')
72d868eb89SAndreas Gohr{
7375b14482SAndreas Gohr    global $conf;
74afb2c082SAndreas Gohr    if (!$tpl) $tpl = $conf['template'];
75dcd4911eSMichael Hamann    return DOKU_BASE . 'lib/tpl/' . $tpl . '/';
76c4766956SAndreas Gohr}
77c4766956SAndreas Gohr
785a892029SAndreas Gohr/**
796b13307fSandi * Print the content
806b13307fSandi *
816b13307fSandi * This function is used for printing all the usual content
826b13307fSandi * (defined by the global $ACT var) by calling the appropriate
836b13307fSandi * outputfunction(s) from html.php
846b13307fSandi *
85ee4c4a1bSAndreas Gohr * Everything that doesn't use the main template file isn't
86ee4c4a1bSAndreas Gohr * handled by this function. ACL stuff is not done here either.
876b13307fSandi *
884dc42f7fSGerrit Uitslag * @param bool $prependTOC should the TOC be displayed here?
894dc42f7fSGerrit Uitslag * @return bool true if any output
9042ea7f44SGerrit Uitslag *
91ac7a515fSAndreas Gohr * @triggers TPL_ACT_RENDER
92ac7a515fSAndreas Gohr * @triggers TPL_CONTENT_DISPLAY
934dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
946b13307fSandi */
95d868eb89SAndreas Gohrfunction tpl_content($prependTOC = true)
96d868eb89SAndreas Gohr{
977ea0913cSchris    global $ACT;
98b8595a66SAndreas Gohr    global $INFO;
99b8595a66SAndreas Gohr    $INFO['prependTOC'] = $prependTOC;
1007ea0913cSchris
1017ea0913cSchris    ob_start();
102cbb44eabSAndreas Gohr    Event::createAndTrigger('TPL_ACT_RENDER', $ACT, 'tpl_content_core');
1037ea0913cSchris    $html_output = ob_get_clean();
10434c27e09SAndreas Gohr    Event::createAndTrigger('TPL_CONTENT_DISPLAY', $html_output, function ($html_output) {
10534c27e09SAndreas Gohr        echo $html_output;
10634c27e09SAndreas Gohr    });
10754e95700STom N Harris
10854e95700STom N Harris    return !empty($html_output);
1097ea0913cSchris}
1107ea0913cSchris
111ac7a515fSAndreas Gohr/**
112ac7a515fSAndreas Gohr * Default Action of TPL_ACT_RENDER
113ac7a515fSAndreas Gohr *
114ac7a515fSAndreas Gohr * @return bool
115ac7a515fSAndreas Gohr */
116d868eb89SAndreas Gohrfunction tpl_content_core()
117d868eb89SAndreas Gohr{
11824870174SAndreas Gohr    $router = ActionRouter::getInstance();
119952acff9SAndreas Gohr    try {
120952acff9SAndreas Gohr        $router->getAction()->tplContent();
12124870174SAndreas Gohr    } catch (FatalException $e) {
122952acff9SAndreas Gohr        // there was no content for the action
123952acff9SAndreas Gohr        msg(hsc($e->getMessage()), -1);
12454e95700STom N Harris        return false;
1256b13307fSandi    }
12654e95700STom N Harris    return true;
1276b13307fSandi}
1286b13307fSandi
129c19fe9c0Sandi/**
130b8595a66SAndreas Gohr * Places the TOC where the function is called
131b8595a66SAndreas Gohr *
132b8595a66SAndreas Gohr * If you use this you most probably want to call tpl_content with
133b8595a66SAndreas Gohr * a false argument
134b8595a66SAndreas Gohr *
135ac7a515fSAndreas Gohr * @param bool $return Should the TOC be returned instead to be printed?
136ac7a515fSAndreas Gohr * @return string
1374dc42f7fSGerrit Uitslag *
1384dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
139b8595a66SAndreas Gohr */
140d868eb89SAndreas Gohrfunction tpl_toc($return = false)
141d868eb89SAndreas Gohr{
142b8595a66SAndreas Gohr    global $TOC;
143b8595a66SAndreas Gohr    global $ACT;
144b8595a66SAndreas Gohr    global $ID;
145b8595a66SAndreas Gohr    global $REV;
146b8595a66SAndreas Gohr    global $INFO;
147851f2e89SAnika Henke    global $conf;
14824870174SAndreas Gohr    $toc = [];
149b8595a66SAndreas Gohr
150b8595a66SAndreas Gohr    if (is_array($TOC)) {
151b8595a66SAndreas Gohr        // if a TOC was prepared in global scope, always use it
152b8595a66SAndreas Gohr        $toc = $TOC;
153*6c16a3a9Sfiwswe    } elseif (($ACT == 'show' || str_starts_with($ACT, 'export')) && !$REV && $INFO['exists']) {
154b8595a66SAndreas Gohr        // get TOC from metadata, render if neccessary
155e0c26282SGerrit Uitslag        $meta = p_get_metadata($ID, '', METADATA_RENDER_USING_CACHE);
1564dc42f7fSGerrit Uitslag        $tocok = $meta['internal']['toc'] ?? true;
15724870174SAndreas Gohr        $toc = $meta['description']['tableofcontents'] ?? null;
158851f2e89SAnika Henke        if (!$tocok || !is_array($toc) || !$conf['tocminheads'] || count($toc) < $conf['tocminheads']) {
15924870174SAndreas Gohr            $toc = [];
160b8595a66SAndreas Gohr        }
161b8595a66SAndreas Gohr    } elseif ($ACT == 'admin') {
162a61966c5SChristopher Smith        // try to load admin plugin TOC
1634dc42f7fSGerrit Uitslag        /** @var AdminPlugin $plugin */
164a61966c5SChristopher Smith        if ($plugin = plugin_getRequestAdminPlugin()) {
165b8595a66SAndreas Gohr            $toc = $plugin->getTOC();
166b8595a66SAndreas Gohr            $TOC = $toc; // avoid later rebuild
167b8595a66SAndreas Gohr        }
168b8595a66SAndreas Gohr    }
169b8595a66SAndreas Gohr
170cbb44eabSAndreas Gohr    Event::createAndTrigger('TPL_TOC_RENDER', $toc, null, false);
171b8595a66SAndreas Gohr    $html = html_TOC($toc);
172b8595a66SAndreas Gohr    if ($return) return $html;
173b8595a66SAndreas Gohr    echo $html;
174ac7a515fSAndreas Gohr    return '';
175b8595a66SAndreas Gohr}
176b8595a66SAndreas Gohr
177b8595a66SAndreas Gohr/**
178c19fe9c0Sandi * Handle the admin page contents
179c19fe9c0Sandi *
18042ea7f44SGerrit Uitslag * @return bool
1814dc42f7fSGerrit Uitslag *
1824dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
183c19fe9c0Sandi */
184d868eb89SAndreas Gohrfunction tpl_admin()
185d868eb89SAndreas Gohr{
186f8cc712eSAndreas Gohr    global $INFO;
187b8595a66SAndreas Gohr    global $TOC;
188ac7a515fSAndreas Gohr    global $INPUT;
18911e2ce22Schris
190b8595a66SAndreas Gohr    $plugin = null;
191ac7a515fSAndreas Gohr    $class = $INPUT->str('page');
192ac7a515fSAndreas Gohr    if (!empty($class)) {
19311e2ce22Schris        $pluginlist = plugin_list('admin');
19411e2ce22Schris
195ac7a515fSAndreas Gohr        if (in_array($class, $pluginlist)) {
19611e2ce22Schris            // attempt to load the plugin
1974dc42f7fSGerrit Uitslag            /** @var AdminPlugin $plugin */
198a04f2bd5SGerrit Uitslag            $plugin = plugin_load('admin', $class);
19911e2ce22Schris        }
20011e2ce22Schris    }
20111e2ce22Schris
20224870174SAndreas Gohr    if ($plugin instanceof PluginInterface) {
203b8595a66SAndreas Gohr        if (!is_array($TOC)) $TOC = $plugin->getTOC(); //if TOC wasn't requested yet
204b8595a66SAndreas Gohr        if ($INFO['prependTOC']) tpl_toc();
205f8cc712eSAndreas Gohr        $plugin->html();
206f8cc712eSAndreas Gohr    } else {
20724870174SAndreas Gohr        $admin = new Admin();
2080470c28fSAndreas Gohr        $admin->show();
209f8cc712eSAndreas Gohr    }
21054e95700STom N Harris    return true;
211c19fe9c0Sandi}
2126b13307fSandi
2136b13307fSandi/**
2146b13307fSandi * Print the correct HTML meta headers
2156b13307fSandi *
2166b13307fSandi * This has to go into the head section of your template.
2176b13307fSandi *
218ac7a515fSAndreas Gohr * @param bool $alt Should feeds and alternative format links be added?
219ac7a515fSAndreas Gohr * @return bool
2204dc42f7fSGerrit Uitslag * @throws JsonException
2214dc42f7fSGerrit Uitslag *
2224dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
2234dc42f7fSGerrit Uitslag * @triggers TPL_METAHEADER_OUTPUT
2246b13307fSandi */
225d868eb89SAndreas Gohrfunction tpl_metaheaders($alt = true)
226d868eb89SAndreas Gohr{
2276b13307fSandi    global $ID;
228d98d4540SBen Coburn    global $REV;
2296b13307fSandi    global $INFO;
23072e0dc37SAndreas Gohr    global $JSINFO;
2316b13307fSandi    global $ACT;
2324bb1b5aeSAndreas Gohr    global $QUERY;
2336b13307fSandi    global $lang;
234dc57ef04Sandi    global $conf;
2359c438d6cSMichael Hamann    global $updateVersion;
236585bf44eSChristopher Smith    /** @var Input $INPUT */
237585bf44eSChristopher Smith    global $INPUT;
2386b13307fSandi
2397bff22c0SAndreas Gohr    // prepare the head array
24024870174SAndreas Gohr    $head = [];
2417bff22c0SAndreas Gohr
242202ac28bSMichael Klier    // prepare seed for js and css
243cd997f93SAndreas Gohr    $tseed = $updateVersion;
244202ac28bSMichael Klier    $depends = getConfigFiles('main');
24584e76a7eSAndreas Gohr    $depends[] = DOKU_CONF . "tpl/" . $conf['template'] . "/style.ini";
246cd997f93SAndreas Gohr    foreach ($depends as $f) $tseed .= @filemtime($f);
247cd997f93SAndreas Gohr    $tseed = md5($tseed);
2487bff22c0SAndreas Gohr
2496b13307fSandi    // the usual stuff
25024870174SAndreas Gohr    $head['meta'][] = ['name' => 'generator', 'content' => 'DokuWiki'];
25163cf4192Ssarehag    if (actionOK('search')) {
25224870174SAndreas Gohr        $head['link'][] = [
25324870174SAndreas Gohr            'rel' => 'search',
25424870174SAndreas Gohr            'type' => 'application/opensearchdescription+xml',
25524870174SAndreas Gohr            'href' => DOKU_BASE . 'lib/exe/opensearch.php',
25624870174SAndreas Gohr            'title' => $conf['title']
25724870174SAndreas Gohr        ];
25863cf4192Ssarehag    }
25963cf4192Ssarehag
26024870174SAndreas Gohr    $head['link'][] = ['rel' => 'start', 'href' => DOKU_BASE];
2617aedde2eSGina Haeussge    if (actionOK('index')) {
26224870174SAndreas Gohr        $head['link'][] = [
26324870174SAndreas Gohr            'rel' => 'contents',
26424870174SAndreas Gohr            'href' => wl($ID, 'do=index', false, '&'),
265ac7a515fSAndreas Gohr            'title' => $lang['btn_index']
26624870174SAndreas Gohr        ];
2677aedde2eSGina Haeussge    }
268f96fa415SAndreas Gohr
2695e0255e3SMichael Große    if (actionOK('manifest')) {
27024870174SAndreas Gohr        $head['link'][] = [
27124870174SAndreas Gohr            'rel' => 'manifest',
27224870174SAndreas Gohr            'href' => DOKU_BASE . 'lib/exe/manifest.php'
27324870174SAndreas Gohr        ];
2745e0255e3SMichael Große    }
2755e0255e3SMichael Große
27624870174SAndreas Gohr    $styleUtil = new StyleUtils();
2774593dbd2SAnna Dabrowska    $styleIni = $styleUtil->cssStyleini();
27840ca8540SMichael Große    $replacements = $styleIni['replacements'];
27940ca8540SMichael Große    if (!empty($replacements['__theme_color__'])) {
28024870174SAndreas Gohr        $head['meta'][] = [
28124870174SAndreas Gohr            'name' => 'theme-color',
28224870174SAndreas Gohr            'content' => $replacements['__theme_color__']
28324870174SAndreas Gohr        ];
28440ca8540SMichael Große    }
28540ca8540SMichael Große
286f96fa415SAndreas Gohr    if ($alt) {
28754be1338SGerrit Uitslag        if (actionOK('rss')) {
28824870174SAndreas Gohr            $head['link'][] = [
28924870174SAndreas Gohr                'rel' => 'alternate',
29024870174SAndreas Gohr                'type' => 'application/rss+xml',
29124870174SAndreas Gohr                'title' => $lang['btn_recent'],
29224870174SAndreas Gohr                'href' => DOKU_BASE . 'feed.php'
29324870174SAndreas Gohr            ];
29424870174SAndreas Gohr            $head['link'][] = [
29524870174SAndreas Gohr                'rel' => 'alternate',
29624870174SAndreas Gohr                'type' => 'application/rss+xml',
297a1288caeSGerrit Uitslag                'title' => $lang['currentns'],
298aac83cd4SPhy                'href' => DOKU_BASE . 'feed.php?mode=list&ns=' . (isset($INFO) ? $INFO['namespace'] : '')
29924870174SAndreas Gohr            ];
30054be1338SGerrit Uitslag        }
301c35f3875SAndreas Gohr        if (($ACT == 'show' || $ACT == 'search') && $INFO['writable']) {
30224870174SAndreas Gohr            $head['link'][] = [
303ac7a515fSAndreas Gohr                'rel' => 'edit',
304715bdf1fSAndreas Gohr                'title' => $lang['btn_edit'],
305ac7a515fSAndreas Gohr                'href' => wl($ID, 'do=edit', false, '&')
30624870174SAndreas Gohr            ];
307c35f3875SAndreas Gohr        }
308c35f3875SAndreas Gohr
30954be1338SGerrit Uitslag        if (actionOK('rss') && $ACT == 'search') {
31024870174SAndreas Gohr            $head['link'][] = [
31124870174SAndreas Gohr                'rel' => 'alternate',
31224870174SAndreas Gohr                'type' => 'application/rss+xml',
313a1288caeSGerrit Uitslag                'title' => $lang['searchresult'],
314ac7a515fSAndreas Gohr                'href' => DOKU_BASE . 'feed.php?mode=search&q=' . $QUERY
31524870174SAndreas Gohr            ];
3164bb1b5aeSAndreas Gohr        }
317bae36d94SAndreas Gohr
318bae36d94SAndreas Gohr        if (actionOK('export_xhtml')) {
31924870174SAndreas Gohr            $head['link'][] = [
32024870174SAndreas Gohr                'rel' => 'alternate',
32124870174SAndreas Gohr                'type' => 'text/html',
32224870174SAndreas Gohr                'title' => $lang['plainhtml'],
323ac7a515fSAndreas Gohr                'href' => exportlink($ID, 'xhtml', '', false, '&')
32424870174SAndreas Gohr            ];
325bae36d94SAndreas Gohr        }
326bae36d94SAndreas Gohr
327bae36d94SAndreas Gohr        if (actionOK('export_raw')) {
32824870174SAndreas Gohr            $head['link'][] = [
32924870174SAndreas Gohr                'rel' => 'alternate',
33024870174SAndreas Gohr                'type' => 'text/plain',
33124870174SAndreas Gohr                'title' => $lang['wikimarkup'],
332ac7a515fSAndreas Gohr                'href' => exportlink($ID, 'raw', '', false, '&')
33324870174SAndreas Gohr            ];
334f96fa415SAndreas Gohr        }
335bae36d94SAndreas Gohr    }
3366b13307fSandi
33763f13cadSDamien Regad    // setup robot tags appropriate for different modes
3384f2e0004STim Weber    if (($ACT == 'show' || $ACT == 'export_xhtml') && !$REV) {
3396b13307fSandi        if ($INFO['exists']) {
3406b13307fSandi            //delay indexing:
341fb9fa88bSAndreas Gohr            if ((time() - $INFO['lastmod']) >= $conf['indexdelay'] && !isHiddenPage($ID)) {
34224870174SAndreas Gohr                $head['meta'][] = ['name' => 'robots', 'content' => 'index,follow'];
3436b13307fSandi            } else {
34424870174SAndreas Gohr                $head['meta'][] = ['name' => 'robots', 'content' => 'noindex,nofollow'];
3456b13307fSandi            }
34601f9be51SAnika Henke            $canonicalUrl = wl($ID, '', true, '&');
34701f9be51SAnika Henke            if ($ID == $conf['start']) {
34801f9be51SAnika Henke                $canonicalUrl = DOKU_URL;
34901f9be51SAnika Henke            }
35024870174SAndreas Gohr            $head['link'][] = ['rel' => 'canonical', 'href' => $canonicalUrl];
3516b13307fSandi        } else {
35224870174SAndreas Gohr            $head['meta'][] = ['name' => 'robots', 'content' => 'noindex,follow'];
3536b13307fSandi        }
3547a24876fSAndreas Gohr    } elseif (defined('DOKU_MEDIADETAIL')) {
35524870174SAndreas Gohr        $head['meta'][] = ['name' => 'robots', 'content' => 'index,follow'];
3566b13307fSandi    } else {
35724870174SAndreas Gohr        $head['meta'][] = ['name' => 'robots', 'content' => 'noindex,nofollow'];
3586b13307fSandi    }
3596b13307fSandi
360831800b8SAndreas Gohr    // set metadata
361831800b8SAndreas Gohr    if ($ACT == 'show' || $ACT == 'export_xhtml') {
362831800b8SAndreas Gohr        // keywords (explicit or implicit)
363bb4866bdSchris        if (!empty($INFO['meta']['subject'])) {
36424870174SAndreas Gohr            $head['meta'][] = ['name' => 'keywords', 'content' => implode(',', $INFO['meta']['subject'])];
365831800b8SAndreas Gohr        } else {
36624870174SAndreas Gohr            $head['meta'][] = ['name' => 'keywords', 'content' => str_replace(':', ',', $ID)];
367831800b8SAndreas Gohr        }
368831800b8SAndreas Gohr    }
369831800b8SAndreas Gohr
37078a6aeb1SAndreas Gohr    // load stylesheets
37124870174SAndreas Gohr    $head['link'][] = [
37259305168SPhy        'rel' => 'stylesheet',
373e283bd6cSAnika Henke        'href' => DOKU_BASE . 'lib/exe/css.php?t=' . rawurlencode($conf['template']) . '&tseed=' . $tseed
37424870174SAndreas Gohr    ];
375bad31ae9SAndreas Gohr
376aac83cd4SPhy    $script = "var NS='" . (isset($INFO) ? $INFO['namespace'] : '') . "';";
377585bf44eSChristopher Smith    if ($conf['useacl'] && $INPUT->server->str('REMOTE_USER')) {
37898169a0fSAndreas Gohr        $script .= "var SIG=" . toolbar_signature() . ";";
379c591aabeSAndreas Gohr    }
3800c39d46cSMichael Große    jsinfo();
38124870174SAndreas Gohr    $script .= 'var JSINFO = ' . json_encode($JSINFO, JSON_THROW_ON_ERROR) . ';';
38224870174SAndreas Gohr    $head['script'][] = ['_data' => $script];
3838bbcb611SAndreas Gohr
38461537d47SAndreas Gohr    // load jquery
385fa078663SAndreas Gohr    $jquery = getCdnUrls();
386fa078663SAndreas Gohr    foreach ($jquery as $src) {
38724870174SAndreas Gohr        $head['script'][] = [
388fc6b11d2SMichael Große                '_data' => '',
38924870174SAndreas Gohr                'src' => $src
39024870174SAndreas Gohr            ] + ($conf['defer_js'] ? ['defer' => 'defer'] : []);
39161537d47SAndreas Gohr    }
39261537d47SAndreas Gohr
39361537d47SAndreas Gohr    // load our javascript dispatcher
39424870174SAndreas Gohr    $head['script'][] = [
395de1dc35bSNicolas Friedli            '_data' => '',
39624870174SAndreas Gohr            'src' => DOKU_BASE . 'lib/exe/js.php' . '?t=' . rawurlencode($conf['template']) . '&tseed=' . $tseed
39724870174SAndreas Gohr        ] + ($conf['defer_js'] ? ['defer' => 'defer'] : []);
3987bff22c0SAndreas Gohr
3997bff22c0SAndreas Gohr    // trigger event here
400cbb44eabSAndreas Gohr    Event::createAndTrigger('TPL_METAHEADER_OUTPUT', $head, '_tpl_metaheaders_action', true);
40154e95700STom N Harris    return true;
4027bff22c0SAndreas Gohr}
4037bff22c0SAndreas Gohr
4047bff22c0SAndreas Gohr/**
4057bff22c0SAndreas Gohr * prints the array build by tpl_metaheaders
4067bff22c0SAndreas Gohr *
4077bff22c0SAndreas Gohr * $data is an array of different header tags. Each tag can have multiple
4087bff22c0SAndreas Gohr * instances. Attributes are given as key value pairs. Values will be HTML
4097bff22c0SAndreas Gohr * encoded automatically so they should be provided as is in the $data array.
4107bff22c0SAndreas Gohr *
41142ea7f44SGerrit Uitslag * For tags having a body attribute specify the body data in the special
4121304d1dbSAndreas Gohr * attribute '_data'. This field will NOT BE ESCAPED automatically.
4137bff22c0SAndreas Gohr *
41442ea7f44SGerrit Uitslag * @param array $data
4154dc42f7fSGerrit Uitslag *
4164dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
4177bff22c0SAndreas Gohr */
418d868eb89SAndreas Gohrfunction _tpl_metaheaders_action($data)
419d868eb89SAndreas Gohr{
4207bff22c0SAndreas Gohr    foreach ($data as $tag => $inst) {
421427bf9a2SAndreas Gohr        if ($tag == 'script') {
422427bf9a2SAndreas Gohr            echo "<!--[if gte IE 9]><!-->\n"; // no scripts for old IE
423427bf9a2SAndreas Gohr        }
4247bff22c0SAndreas Gohr        foreach ($inst as $attr) {
425177d6836SAndreas Gohr            if (empty($attr)) {
426d4f83172SAndreas Gohr                continue;
427d4f83172SAndreas Gohr            }
4287bff22c0SAndreas Gohr            echo '<', $tag, ' ', buildAttributes($attr);
42926afa874SMikhail I. Izmestev            if (isset($attr['_data']) || $tag == 'script') {
430740897dcSasivery                if ($tag == 'script' && isset($attr['_data']))
43109f791c4SDominik Eckelmann                    $attr['_data'] = "/*<![CDATA[*/" .
432e226efe1SAndreas Gohr                        $attr['_data'] .
43309f791c4SDominik Eckelmann                        "\n/*!]]>*/";
434e226efe1SAndreas Gohr
43524870174SAndreas Gohr                echo '>', $attr['_data'] ?? '', '</', $tag, '>';
4367bff22c0SAndreas Gohr            } else {
4377bff22c0SAndreas Gohr                echo '/>';
4387bff22c0SAndreas Gohr            }
4397bff22c0SAndreas Gohr            echo "\n";
4407bff22c0SAndreas Gohr        }
441427bf9a2SAndreas Gohr        if ($tag == 'script') {
442427bf9a2SAndreas Gohr            echo "<!--<![endif]-->\n";
443427bf9a2SAndreas Gohr        }
4447bff22c0SAndreas Gohr    }
4456b13307fSandi}
4466b13307fSandi
4476b13307fSandi/**
4486b13307fSandi * Print a link
4496b13307fSandi *
4505e163278SAndreas Gohr * Just builds a link.
4516b13307fSandi *
45242ea7f44SGerrit Uitslag * @param string $url
45342ea7f44SGerrit Uitslag * @param string $name
45442ea7f44SGerrit Uitslag * @param string $more
45521d806cdSGerrit Uitslag * @param bool $return if true return the link html, otherwise print
45621d806cdSGerrit Uitslag * @return bool|string html of the link, or true if printed
4574dc42f7fSGerrit Uitslag *
4584dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
4596b13307fSandi */
460d868eb89SAndreas Gohrfunction tpl_link($url, $name, $more = '', $return = false)
461d868eb89SAndreas Gohr{
46201f17825SAnika Henke    $out = '<a href="' . $url . '" ';
4631af98a77SAnika Henke    if ($more) $out .= ' ' . $more;
4641af98a77SAnika Henke    $out .= ">$name</a>";
4651af98a77SAnika Henke    if ($return) return $out;
46626dfc232SAndreas Gohr    echo $out;
46754e95700STom N Harris    return true;
4686b13307fSandi}
4696b13307fSandi
4706b13307fSandi/**
47155efc227SAndreas Gohr * Prints a link to a WikiPage
47255efc227SAndreas Gohr *
47355efc227SAndreas Gohr * Wrapper around html_wikilink
47455efc227SAndreas Gohr *
47542ea7f44SGerrit Uitslag * @param string $id page id
47642ea7f44SGerrit Uitslag * @param string|null $name the name of the link
477fb008d31SIain Hallam * @param bool $return
478fb008d31SIain Hallam * @return true|string
4794dc42f7fSGerrit Uitslag *
4804dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
48155efc227SAndreas Gohr */
482d868eb89SAndreas Gohrfunction tpl_pagelink($id, $name = null, $return = false)
483d868eb89SAndreas Gohr{
484c4a386f1SIain Hallam    $out = '<bdi>' . html_wikilink($id, $name) . '</bdi>';
485c4a386f1SIain Hallam    if ($return) return $out;
48626dfc232SAndreas Gohr    echo $out;
48754e95700STom N Harris    return true;
48855efc227SAndreas Gohr}
48955efc227SAndreas Gohr
49055efc227SAndreas Gohr/**
491a3ec5f4aSmatthiasgrimm * get the parent page
492a3ec5f4aSmatthiasgrimm *
493a3ec5f4aSmatthiasgrimm * Tries to find out which page is parent.
494a3ec5f4aSmatthiasgrimm * returns false if none is available
495a3ec5f4aSmatthiasgrimm *
49642ea7f44SGerrit Uitslag * @param string $id page id
49742ea7f44SGerrit Uitslag * @return false|string
4984dc42f7fSGerrit Uitslag *
4994dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
500a3ec5f4aSmatthiasgrimm */
501d868eb89SAndreas Gohrfunction tpl_getparent($id)
502d868eb89SAndreas Gohr{
5038c6be208SAndreas Gohr    $resolver = new PageResolver('root');
5048c6be208SAndreas Gohr
505377f9e97SAndreas Gohr    $parent = getNS($id) . ':';
5068c6be208SAndreas Gohr    $parent = $resolver->resolveId($parent);
507a197105eSmatthiasgrimm    if ($parent == $id) {
508a197105eSmatthiasgrimm        $pos = strrpos(getNS($id), ':');
509a197105eSmatthiasgrimm        $parent = substr($parent, 0, $pos) . ':';
5108c6be208SAndreas Gohr        $parent = $resolver->resolveId($parent);
511377f9e97SAndreas Gohr        if ($parent == $id) return false;
512a197105eSmatthiasgrimm    }
513377f9e97SAndreas Gohr    return $parent;
514a3ec5f4aSmatthiasgrimm}
515a3ec5f4aSmatthiasgrimm
516a3ec5f4aSmatthiasgrimm/**
5176b13307fSandi * Print one of the buttons
5186b13307fSandi *
519e0c26282SGerrit Uitslag * @param string $type
520e0c26282SGerrit Uitslag * @param bool $return
521e0c26282SGerrit Uitslag * @return bool|string html, or false if no data, true if printed
5224dc42f7fSGerrit Uitslag * @see    tpl_get_action
5234dc42f7fSGerrit Uitslag *
5244dc42f7fSGerrit Uitslag * @author Adrian Lang <mail@adrianlang.de>
525affc7ddfSAndreas Gohr * @deprecated 2017-09-01 see devel:menus
5266b13307fSandi */
527d868eb89SAndreas Gohrfunction tpl_button($type, $return = false)
528d868eb89SAndreas Gohr{
529affc7ddfSAndreas Gohr    dbg_deprecated('see devel:menus');
530a453d131SAdrian Lang    $data = tpl_get_action($type);
531a453d131SAdrian Lang    if ($data === false) {
532a453d131SAdrian Lang        return false;
533a453d131SAdrian Lang    } elseif (!is_array($data)) {
534a453d131SAdrian Lang        $out = sprintf($data, 'button');
535409d7af7SAndreas Gohr    } else {
536ac7a515fSAndreas Gohr        /**
537ac7a515fSAndreas Gohr         * @var string $accesskey
538ac7a515fSAndreas Gohr         * @var string $id
539ac7a515fSAndreas Gohr         * @var string $method
540ac7a515fSAndreas Gohr         * @var array $params
541ac7a515fSAndreas Gohr         */
542a453d131SAdrian Lang        extract($data);
543a453d131SAdrian Lang        if ($id === '#dokuwiki__top') {
544a453d131SAdrian Lang            $out = html_topbtn();
545409d7af7SAndreas Gohr        } else {
546a453d131SAdrian Lang            $out = html_btn($type, $id, $accesskey, $params, $method);
547409d7af7SAndreas Gohr        }
548409d7af7SAndreas Gohr    }
5491af98a77SAnika Henke    if ($return) return $out;
550a453d131SAdrian Lang    echo $out;
551a453d131SAdrian Lang    return true;
5526b13307fSandi}
5536b13307fSandi
5546b13307fSandi/**
555ed630903Sandi * Like the action buttons but links
556ed630903Sandi *
55742ea7f44SGerrit Uitslag * @param string $type action command
558e0c26282SGerrit Uitslag * @param string $pre prefix of link
559e0c26282SGerrit Uitslag * @param string $suf suffix of link
560e0c26282SGerrit Uitslag * @param string $inner innerHML of link
56121d806cdSGerrit Uitslag * @param bool $return if true it returns html, otherwise prints
562e0c26282SGerrit Uitslag * @return bool|string html or false if no data, true if printed
5634dc42f7fSGerrit Uitslag *
5644dc42f7fSGerrit Uitslag * @see    tpl_get_action
5654dc42f7fSGerrit Uitslag * @author Adrian Lang <mail@adrianlang.de>
566affc7ddfSAndreas Gohr * @deprecated 2017-09-01 see devel:menus
567a453d131SAdrian Lang */
568d868eb89SAndreas Gohrfunction tpl_actionlink($type, $pre = '', $suf = '', $inner = '', $return = false)
569d868eb89SAndreas Gohr{
570affc7ddfSAndreas Gohr    dbg_deprecated('see devel:menus');
571a453d131SAdrian Lang    global $lang;
572a453d131SAdrian Lang    $data = tpl_get_action($type);
573a453d131SAdrian Lang    if ($data === false) {
574a453d131SAdrian Lang        return false;
575a453d131SAdrian Lang    } elseif (!is_array($data)) {
576a453d131SAdrian Lang        $out = sprintf($data, 'link');
577a453d131SAdrian Lang    } else {
578ac7a515fSAndreas Gohr        /**
579ac7a515fSAndreas Gohr         * @var string $accesskey
580ac7a515fSAndreas Gohr         * @var string $id
581ac7a515fSAndreas Gohr         * @var string $method
582b1af9014SChristopher Smith         * @var bool $nofollow
583ac7a515fSAndreas Gohr         * @var array $params
584becfa414SGerrit Uitslag         * @var string $replacement
585ac7a515fSAndreas Gohr         */
586a453d131SAdrian Lang        extract($data);
587a453d131SAdrian Lang        if (strpos($id, '#') === 0) {
588a453d131SAdrian Lang            $linktarget = $id;
589a453d131SAdrian Lang        } else {
590a453d131SAdrian Lang            $linktarget = wl($id, $params);
591a453d131SAdrian Lang        }
592a453d131SAdrian Lang        $caption = $lang['btn_' . $type];
593becfa414SGerrit Uitslag        if (strpos($caption, '%s')) {
594becfa414SGerrit Uitslag            $caption = sprintf($caption, $replacement);
595becfa414SGerrit Uitslag        }
59624870174SAndreas Gohr        $akey = '';
59724870174SAndreas Gohr        $addTitle = '';
598c7e90e3fSAnika Henke        if ($accesskey) {
599c7e90e3fSAnika Henke            $akey = 'accesskey="' . $accesskey . '" ';
600c7e90e3fSAnika Henke            $addTitle = ' [' . strtoupper($accesskey) . ']';
601c7e90e3fSAnika Henke        }
602b1af9014SChristopher Smith        $rel = $nofollow ? 'rel="nofollow" ' : '';
603ac7a515fSAndreas Gohr        $out = tpl_link(
604dccd6b2bSAndreas Gohr            $linktarget,
605dccd6b2bSAndreas Gohr            $pre . ($inner ?: $caption) . $suf,
606a453d131SAdrian Lang            'class="action ' . $type . '" ' .
607b1af9014SChristopher Smith            $akey . $rel .
608dccd6b2bSAndreas Gohr            'title="' . hsc($caption) . $addTitle . '"',
609dccd6b2bSAndreas Gohr            true
610ac7a515fSAndreas Gohr        );
611a453d131SAdrian Lang    }
612a453d131SAdrian Lang    if ($return) return $out;
613a453d131SAdrian Lang    echo $out;
614a453d131SAdrian Lang    return true;
615a453d131SAdrian Lang}
616a453d131SAdrian Lang
617a453d131SAdrian Lang/**
618a453d131SAdrian Lang * Check the actions and get data for buttons and links
619ed630903Sandi *
620ac7a515fSAndreas Gohr * @param string $type
621ac7a515fSAndreas Gohr * @return array|bool|string
6224dc42f7fSGerrit Uitslag *
6234dc42f7fSGerrit Uitslag * @author Adrian Lang <mail@adrianlang.de>
6244dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
6254dc42f7fSGerrit Uitslag * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
626affc7ddfSAndreas Gohr * @deprecated 2017-09-01 see devel:menus
627ed630903Sandi */
628d868eb89SAndreas Gohrfunction tpl_get_action($type)
629d868eb89SAndreas Gohr{
630affc7ddfSAndreas Gohr    dbg_deprecated('see devel:menus');
631a453d131SAdrian Lang    if ($type == 'history') $type = 'revisions';
6324c4b65c8SMichael Hamann    if ($type == 'subscription') $type = 'subscribe';
6334887c154SAndreas Gohr    if ($type == 'img_backto') $type = 'imgBackto';
634409d7af7SAndreas Gohr
6354887c154SAndreas Gohr    $class = '\\dokuwiki\\Menu\\Item\\' . ucfirst($type);
6364887c154SAndreas Gohr    if (class_exists($class)) {
6374887c154SAndreas Gohr        try {
63824870174SAndreas Gohr            /** @var AbstractItem $item */
63973022918SAndreas Gohr            $item = new $class();
6404887c154SAndreas Gohr            $data = $item->getLegacyData();
6417b4365a7SGerrit Uitslag            $unknown = false;
6424dc42f7fSGerrit Uitslag        } catch (RuntimeException $ignored) {
6434887c154SAndreas Gohr            return false;
644b8a111f5SMichael Klier        }
645ed630903Sandi    } else {
6464887c154SAndreas Gohr        global $ID;
64724870174SAndreas Gohr        $data = [
6484887c154SAndreas Gohr            'accesskey' => null,
6494887c154SAndreas Gohr            'type' => $type,
6504887c154SAndreas Gohr            'id' => $ID,
6514887c154SAndreas Gohr            'method' => 'get',
65224870174SAndreas Gohr            'params' => ['do' => $type],
6534887c154SAndreas Gohr            'nofollow' => true,
65424870174SAndreas Gohr            'replacement' => ''
65524870174SAndreas Gohr        ];
6567b4365a7SGerrit Uitslag        $unknown = true;
657ed630903Sandi    }
6587b4365a7SGerrit Uitslag
659e1d9dcc8SAndreas Gohr    $evt = new Event('TPL_ACTION_GET', $data);
6607b4365a7SGerrit Uitslag    if ($evt->advise_before()) {
6617b4365a7SGerrit Uitslag        //handle unknown types
6627b4365a7SGerrit Uitslag        if ($unknown) {
66338d2ca46SGerrit Uitslag            $data = '[unknown %s type]';
6647b4365a7SGerrit Uitslag        }
6657b4365a7SGerrit Uitslag    }
6667b4365a7SGerrit Uitslag    $evt->advise_after();
6677b4365a7SGerrit Uitslag    unset($evt);
6687b4365a7SGerrit Uitslag
6697b4365a7SGerrit Uitslag    return $data;
670ed630903Sandi}
671ed630903Sandi
672ed630903Sandi/**
67301f17825SAnika Henke * Wrapper around tpl_button() and tpl_actionlink()
67401f17825SAnika Henke *
67542ea7f44SGerrit Uitslag * @param string $type action command
676ac7a515fSAndreas Gohr * @param bool $link link or form button?
677e0c26282SGerrit Uitslag * @param string|bool $wrapper HTML element wrapper
678ac7a515fSAndreas Gohr * @param bool $return return or print
679ac7a515fSAndreas Gohr * @param string $pre prefix for links
680ac7a515fSAndreas Gohr * @param string $suf suffix for links
681ac7a515fSAndreas Gohr * @param string $inner inner HTML for links
682ac7a515fSAndreas Gohr * @return bool|string
6834dc42f7fSGerrit Uitslag *
6844dc42f7fSGerrit Uitslag * @author Anika Henke <anika@selfthinker.org>
685affc7ddfSAndreas Gohr * @deprecated 2017-09-01 see devel:menus
68601f17825SAnika Henke */
687d868eb89SAndreas Gohrfunction tpl_action($type, $link = false, $wrapper = false, $return = false, $pre = '', $suf = '', $inner = '')
688d868eb89SAndreas Gohr{
689affc7ddfSAndreas Gohr    dbg_deprecated('see devel:menus');
69001f17825SAnika Henke    $out = '';
691ac7a515fSAndreas Gohr    if ($link) {
692e0c26282SGerrit Uitslag        $out .= tpl_actionlink($type, $pre, $suf, $inner, true);
693ac7a515fSAndreas Gohr    } else {
694e0c26282SGerrit Uitslag        $out .= tpl_button($type, true);
695ac7a515fSAndreas Gohr    }
69601f17825SAnika Henke    if ($out && $wrapper) $out = "<$wrapper>$out</$wrapper>";
69701f17825SAnika Henke
69801f17825SAnika Henke    if ($return) return $out;
69926dfc232SAndreas Gohr    echo $out;
70024870174SAndreas Gohr    return (bool)$out;
70101f17825SAnika Henke}
70201f17825SAnika Henke
70301f17825SAnika Henke/**
7046b13307fSandi * Print the search form
7056b13307fSandi *
70672645b75SAndreas Gohr * If the first parameter is given a div with the ID 'qsearch_out' will
70772645b75SAndreas Gohr * be added which instructs the ajax pagequicksearch to kick in and place
70872645b75SAndreas Gohr * its output into this div. The second parameter controls the propritary
70972645b75SAndreas Gohr * attribute autocomplete. If set to false this attribute will be set with an
71072645b75SAndreas Gohr * value of "off" to instruct the browser to disable it's own built in
71172645b75SAndreas Gohr * autocompletion feature (MSIE and Firefox)
71272645b75SAndreas Gohr *
713ac7a515fSAndreas Gohr * @param bool $ajax
714ac7a515fSAndreas Gohr * @param bool $autocomplete
715ac7a515fSAndreas Gohr * @return bool
7164dc42f7fSGerrit Uitslag *
7174dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
7186b13307fSandi */
719d868eb89SAndreas Gohrfunction tpl_searchform($ajax = true, $autocomplete = true)
720d868eb89SAndreas Gohr{
7216b13307fSandi    global $lang;
722c1e3b7d9Smatthiasgrimm    global $ACT;
723ad4aaef7SAndreas Gohr    global $QUERY;
724cbcc2fa5SMichael Große    global $ID;
725c1e3b7d9Smatthiasgrimm
726670ff54eSchris    // don't print the search form if search action has been disabled
72764276bbcSarbrk1    if (!actionOK('search')) return false;
728670ff54eSchris
72924870174SAndreas Gohr    $searchForm = new Form([
7303c7a3327SMichael Große        'action' => wl(),
7313c7a3327SMichael Große        'method' => 'get',
7323c7a3327SMichael Große        'role' => 'search',
7333c7a3327SMichael Große        'class' => 'search',
7343c7a3327SMichael Große        'id' => 'dw__search',
7357fa270bcSMichael Große    ], true);
7363eb2b869SMichael Große    $searchForm->addTagOpen('div')->addClass('no');
7373c7a3327SMichael Große    $searchForm->setHiddenField('do', 'search');
738d22b78c8SMichael Große    $searchForm->setHiddenField('id', $ID);
739d22b78c8SMichael Große    $searchForm->addTextInput('q')
7403c7a3327SMichael Große        ->addClass('edit')
7413c7a3327SMichael Große        ->attrs([
7423c7a3327SMichael Große            'title' => '[F]',
7433c7a3327SMichael Große            'accesskey' => 'f',
7443c7a3327SMichael Große            'placeholder' => $lang['btn_search'],
7453c7a3327SMichael Große            'autocomplete' => $autocomplete ? 'on' : 'off',
7463c7a3327SMichael Große        ])
7473c7a3327SMichael Große        ->id('qsearch__in')
7483c7a3327SMichael Große        ->val($ACT === 'search' ? $QUERY : '')
7494dc42f7fSGerrit Uitslag        ->useInput(false);
7503c7a3327SMichael Große    $searchForm->addButton('', $lang['btn_search'])->attrs([
7513c7a3327SMichael Große        'type' => 'submit',
7523c7a3327SMichael Große        'title' => $lang['btn_search'],
7533c7a3327SMichael Große    ]);
7543c7a3327SMichael Große    if ($ajax) {
7553c7a3327SMichael Große        $searchForm->addTagOpen('div')->id('qsearch__out')->addClass('ajax_qsearch JSpopup');
7563c7a3327SMichael Große        $searchForm->addTagClose('div');
7573c7a3327SMichael Große    }
7583eb2b869SMichael Große    $searchForm->addTagClose('div');
7593c7a3327SMichael Große
760c6977b3aSSatoshi Sahara    echo $searchForm->toHTML('QuickSearch');
7613c7a3327SMichael Große
76254e95700STom N Harris    return true;
7636b13307fSandi}
7646b13307fSandi
7656b13307fSandi/**
7666b13307fSandi * Print the breadcrumbs trace
7676b13307fSandi *
768ac7a515fSAndreas Gohr * @param string $sep Separator between entries
769c4a386f1SIain Hallam * @param bool $return return or print
770c4a386f1SIain Hallam * @return bool|string
7714dc42f7fSGerrit Uitslag *
7724dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
7736b13307fSandi */
774d868eb89SAndreas Gohrfunction tpl_breadcrumbs($sep = null, $return = false)
775d868eb89SAndreas Gohr{
7766b13307fSandi    global $lang;
7776b13307fSandi    global $conf;
7786b13307fSandi
7796b13307fSandi    //check if enabled
780359fab8bSMichael Hamann    if (!$conf['breadcrumbs']) return false;
7816b13307fSandi
782c4a386f1SIain Hallam    //set default
783c4a386f1SIain Hallam    if (is_null($sep)) $sep = '•';
784c4a386f1SIain Hallam
785c4a386f1SIain Hallam    $out = '';
786c4a386f1SIain Hallam
7876b13307fSandi    $crumbs = breadcrumbs(); //setup crumb trace
788265e3787Sandi
7892979a10bSKatriel Traum    $crumbs_sep = ' <span class="bcsep">' . $sep . '</span> ';
790265e3787Sandi
79140eb54bbSjan    //render crumbs, highlight the last one
792c4a386f1SIain Hallam    $out .= '<span class="bchead">' . $lang['breadcrumb'] . '</span>';
79340eb54bbSjan    $last = count($crumbs);
79440eb54bbSjan    $i = 0;
795a77f5846Sjan    foreach ($crumbs as $id => $name) {
79640eb54bbSjan        $i++;
797c4a386f1SIain Hallam        $out .= $crumbs_sep;
798c4a386f1SIain Hallam        if ($i == $last) $out .= '<span class="curid">';
799c4a386f1SIain Hallam        $out .= '<bdi>' . tpl_link(wl($id), hsc($name), 'class="breadcrumbs" title="' . $id . '"', true) . '</bdi>';
800c4a386f1SIain Hallam        if ($i == $last) $out .= '</span>';
8016b13307fSandi    }
802c4a386f1SIain Hallam    if ($return) return $out;
80326dfc232SAndreas Gohr    echo $out;
80424870174SAndreas Gohr    return (bool)$out;
8056b13307fSandi}
8066b13307fSandi
8076b13307fSandi/**
8081734437eSandi * Hierarchical breadcrumbs
8091734437eSandi *
81031e187f8SSean Coates * This code was suggested as replacement for the usual breadcrumbs.
8111734437eSandi * It only makes sense with a deep site structure.
8121734437eSandi *
813ac7a515fSAndreas Gohr * @param string $sep Separator between entries
814c4a386f1SIain Hallam * @param bool $return return or print
815c4a386f1SIain Hallam * @return bool|string
8164dc42f7fSGerrit Uitslag *
8174dc42f7fSGerrit Uitslag * @todo   May behave strangely in RTL languages
8184dc42f7fSGerrit Uitslag * @author <fredrik@averpil.com>
8194dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
8204dc42f7fSGerrit Uitslag * @author Nigel McNie <oracle.shinoda@gmail.com>
8214dc42f7fSGerrit Uitslag * @author Sean Coates <sean@caedmon.net>
8221734437eSandi */
823d868eb89SAndreas Gohrfunction tpl_youarehere($sep = null, $return = false)
824d868eb89SAndreas Gohr{
8251734437eSandi    global $conf;
8261734437eSandi    global $ID;
8271734437eSandi    global $lang;
8281734437eSandi
82931e187f8SSean Coates    // check if enabled
83054e95700STom N Harris    if (!$conf['youarehere']) return false;
8311734437eSandi
832c4a386f1SIain Hallam    //set default
833c4a386f1SIain Hallam    if (is_null($sep)) $sep = ' » ';
834c4a386f1SIain Hallam
835c4a386f1SIain Hallam    $out = '';
836c4a386f1SIain Hallam
8371734437eSandi    $parts = explode(':', $ID);
838796bafb3SAndreas Gohr    $count = count($parts);
8391734437eSandi
840c4a386f1SIain Hallam    $out .= '<span class="bchead">' . $lang['youarehere'] . ' </span>';
8413940c519SMark
84208d7a575SAndreas Gohr    // always print the startpage
843c4a386f1SIain Hallam    $out .= '<span class="home">' . tpl_pagelink(':' . $conf['start'], null, true) . '</span>';
844796bafb3SAndreas Gohr
845796bafb3SAndreas Gohr    // print intermediate namespace links
846796bafb3SAndreas Gohr    $part = '';
847796bafb3SAndreas Gohr    for ($i = 0; $i < $count - 1; $i++) {
848796bafb3SAndreas Gohr        $part .= $parts[$i] . ':';
849796bafb3SAndreas Gohr        $page = $part;
850796bafb3SAndreas Gohr        if ($page == $conf['start']) continue; // Skip startpage
851796bafb3SAndreas Gohr
85208d7a575SAndreas Gohr        // output
853c4a386f1SIain Hallam        $out .= $sep . tpl_pagelink($page, null, true);
85431e187f8SSean Coates    }
8551734437eSandi
856796bafb3SAndreas Gohr    // print current page, skipping start page, skipping for namespace index
8578c6be208SAndreas Gohr    if (isset($page)) {
8588c6be208SAndreas Gohr        $page = (new PageResolver('root'))->resolveId($page);
8598c6be208SAndreas Gohr        if ($page == $part . $parts[$i]) {
860a8c33dedSMichael Große            if ($return) return $out;
86126dfc232SAndreas Gohr            echo $out;
862a8c33dedSMichael Große            return true;
863a8c33dedSMichael Große        }
8648c6be208SAndreas Gohr    }
865796bafb3SAndreas Gohr    $page = $part . $parts[$i];
866a8c33dedSMichael Große    if ($page == $conf['start']) {
867a8c33dedSMichael Große        if ($return) return $out;
86826dfc232SAndreas Gohr        echo $out;
869a8c33dedSMichael Große        return true;
870a8c33dedSMichael Große    }
871c4a386f1SIain Hallam    $out .= $sep;
872c4a386f1SIain Hallam    $out .= tpl_pagelink($page, null, true);
873c4a386f1SIain Hallam    if ($return) return $out;
87426dfc232SAndreas Gohr    echo $out;
87524870174SAndreas Gohr    return (bool)$out;
8761734437eSandi}
8771734437eSandi
8781734437eSandi/**
8796b13307fSandi * Print info if the user is logged in
880a2488c3cSMatthias Grimm * and show full name in that case
8816b13307fSandi *
8826b13307fSandi * Could be enhanced with a profile link in future?
8836b13307fSandi *
884ac7a515fSAndreas Gohr * @return bool
8854dc42f7fSGerrit Uitslag *
8864dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
8876b13307fSandi */
888d868eb89SAndreas Gohrfunction tpl_userinfo()
889d868eb89SAndreas Gohr{
8906b13307fSandi    global $lang;
891585bf44eSChristopher Smith    /** @var Input $INPUT */
892585bf44eSChristopher Smith    global $INPUT;
893585bf44eSChristopher Smith
894585bf44eSChristopher Smith    if ($INPUT->server->str('REMOTE_USER')) {
89526dfc232SAndreas Gohr        echo $lang['loggedinas'] . ' ' . userlink();
89654e95700STom N Harris        return true;
89754e95700STom N Harris    }
89854e95700STom N Harris    return false;
8996b13307fSandi}
9006b13307fSandi
9016b13307fSandi/**
9026b13307fSandi * Print some info about the current page
9036b13307fSandi *
904ac7a515fSAndreas Gohr * @param bool $ret return content instead of printing it
905ac7a515fSAndreas Gohr * @return bool|string
9064dc42f7fSGerrit Uitslag *
9074dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
9086b13307fSandi */
909d868eb89SAndreas Gohrfunction tpl_pageinfo($ret = false)
910d868eb89SAndreas Gohr{
9116b13307fSandi    global $conf;
9126b13307fSandi    global $lang;
9136b13307fSandi    global $INFO;
914c6e92a3cSDavid Lorentsen    global $ID;
915c6e92a3cSDavid Lorentsen
916c6e92a3cSDavid Lorentsen    // return if we are not allowed to view the page
917ac7a515fSAndreas Gohr    if (!auth_quickaclcheck($ID)) {
918ac7a515fSAndreas Gohr        return false;
919ac7a515fSAndreas Gohr    }
9206b13307fSandi
9216b13307fSandi    // prepare date and path
9226b13307fSandi    $fn = $INFO['filepath'];
9236b13307fSandi    if (!$conf['fullpath']) {
924613bca54SAndreas Gohr        if ($INFO['rev']) {
925c83f69baSSatoshi Sahara            $fn = str_replace($conf['olddir'] . '/', '', $fn);
9266b13307fSandi        } else {
927c83f69baSSatoshi Sahara            $fn = str_replace($conf['datadir'] . '/', '', $fn);
9286b13307fSandi        }
9296b13307fSandi    }
930bee6dc82Sandi    $fn = utf8_decodeFN($fn);
931f2263577SAndreas Gohr    $date = dformat($INFO['lastmod']);
9326b13307fSandi
933faecdfdfSAndreas Gohr    // print it
934faecdfdfSAndreas Gohr    if ($INFO['exists']) {
9354dc42f7fSGerrit Uitslag        $out = '<bdi>' . $fn . '</bdi>';
936e260f93bSAnika Henke        $out .= ' · ';
9374b0d3916SAndreas Gohr        $out .= $lang['lastmod'];
938fde860beSGerrit Uitslag        $out .= ' ';
9394b0d3916SAndreas Gohr        $out .= $date;
9406b13307fSandi        if ($INFO['editor']) {
9414b0d3916SAndreas Gohr            $out .= ' ' . $lang['by'] . ' ';
942d317fb5dSAnika Henke            $out .= '<bdi>' . editorinfo($INFO['editor']) . '</bdi>';
9435aa52fafSBen Coburn        } else {
9444b0d3916SAndreas Gohr            $out .= ' (' . $lang['external_edit'] . ')';
9456b13307fSandi        }
9466b13307fSandi        if ($INFO['locked']) {
947e260f93bSAnika Henke            $out .= ' · ';
9484b0d3916SAndreas Gohr            $out .= $lang['lockedby'];
949fde860beSGerrit Uitslag            $out .= ' ';
950d317fb5dSAnika Henke            $out .= '<bdi>' . editorinfo($INFO['locked']) . '</bdi>';
9516b13307fSandi        }
9524b0d3916SAndreas Gohr        if ($ret) {
9534b0d3916SAndreas Gohr            return $out;
9544b0d3916SAndreas Gohr        } else {
9554b0d3916SAndreas Gohr            echo $out;
95654e95700STom N Harris            return true;
9576b13307fSandi        }
9584b0d3916SAndreas Gohr    }
95954e95700STom N Harris    return false;
9606b13307fSandi}
9616b13307fSandi
962820fa24bSandi/**
963a6598f23SBen Coburn * Prints or returns the name of the given page (current one if none given).
96487c434ceSAndreas Gohr *
96587c434ceSAndreas Gohr * If useheading is enabled this will use the first headline else
966a6598f23SBen Coburn * the given ID is used.
96787c434ceSAndreas Gohr *
968ac7a515fSAndreas Gohr * @param string $id page id
969ac7a515fSAndreas Gohr * @param bool $ret return content instead of printing
970ac7a515fSAndreas Gohr * @return bool|string
9714dc42f7fSGerrit Uitslag *
9724dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
97387c434ceSAndreas Gohr */
974d868eb89SAndreas Gohrfunction tpl_pagetitle($id = null, $ret = false)
975d868eb89SAndreas Gohr{
9764dc42f7fSGerrit Uitslag    global $ACT, $conf, $lang;
977fffeeafeSChristopher Smith
97887c434ceSAndreas Gohr    if (is_null($id)) {
97987c434ceSAndreas Gohr        global $ID;
98087c434ceSAndreas Gohr        $id = $ID;
98187c434ceSAndreas Gohr    }
98287c434ceSAndreas Gohr
98387c434ceSAndreas Gohr    $name = $id;
984fe9ec250SChris Smith    if (useHeading('navigation')) {
985fffeeafeSChristopher Smith        $first_heading = p_get_first_heading($id);
986fffeeafeSChristopher Smith        if ($first_heading) $name = $first_heading;
987fffeeafeSChristopher Smith    }
988fffeeafeSChristopher Smith
989fffeeafeSChristopher Smith    // default page title is the page name, modify with the current action
990fffeeafeSChristopher Smith    switch ($ACT) {
991fffeeafeSChristopher Smith        // admin functions
992fffeeafeSChristopher Smith        case 'admin':
993fffeeafeSChristopher Smith            $page_title = $lang['btn_admin'];
994fffeeafeSChristopher Smith            // try to get the plugin name
9954dc42f7fSGerrit Uitslag            /** @var AdminPlugin $plugin */
996a61966c5SChristopher Smith            if ($plugin = plugin_getRequestAdminPlugin()) {
997c248bda1SChristopher Smith                $plugin_title = $plugin->getMenuText($conf['lang']);
99824870174SAndreas Gohr                $page_title = $plugin_title ?: $plugin->getPluginName();
999fffeeafeSChristopher Smith            }
1000fffeeafeSChristopher Smith            break;
1001fffeeafeSChristopher Smith
1002fffeeafeSChristopher Smith        // user functions
1003fffeeafeSChristopher Smith        case 'login':
1004fffeeafeSChristopher Smith        case 'profile':
1005fffeeafeSChristopher Smith        case 'register':
1006fffeeafeSChristopher Smith        case 'resendpwd':
1007fffeeafeSChristopher Smith            $page_title = $lang['btn_' . $ACT];
1008fffeeafeSChristopher Smith            break;
1009fffeeafeSChristopher Smith
1010fffeeafeSChristopher Smith        // wiki functions
1011fffeeafeSChristopher Smith        case 'search':
1012fffeeafeSChristopher Smith        case 'index':
1013fffeeafeSChristopher Smith            $page_title = $lang['btn_' . $ACT];
1014fffeeafeSChristopher Smith            break;
1015fffeeafeSChristopher Smith
1016fffeeafeSChristopher Smith        // page functions
1017fffeeafeSChristopher Smith        case 'edit':
10182f19acc2Sbleistivt        case 'preview':
1019fffeeafeSChristopher Smith            $page_title = "✎ " . $name;
1020fffeeafeSChristopher Smith            break;
1021fffeeafeSChristopher Smith
1022fffeeafeSChristopher Smith        case 'revisions':
1023fffeeafeSChristopher Smith            $page_title = $name . ' - ' . $lang['btn_revs'];
1024fffeeafeSChristopher Smith            break;
1025fffeeafeSChristopher Smith
1026fffeeafeSChristopher Smith        case 'backlink':
1027fffeeafeSChristopher Smith        case 'recent':
1028fffeeafeSChristopher Smith        case 'subscribe':
1029fffeeafeSChristopher Smith            $page_title = $name . ' - ' . $lang['btn_' . $ACT];
1030fffeeafeSChristopher Smith            break;
1031fffeeafeSChristopher Smith
1032fffeeafeSChristopher Smith        default: // SHOW and anything else not included
1033fffeeafeSChristopher Smith            $page_title = $name;
103487c434ceSAndreas Gohr    }
1035a6598f23SBen Coburn
1036a6598f23SBen Coburn    if ($ret) {
1037fffeeafeSChristopher Smith        return hsc($page_title);
1038a6598f23SBen Coburn    } else {
103926dfc232SAndreas Gohr        echo hsc($page_title);
104054e95700STom N Harris        return true;
104187c434ceSAndreas Gohr    }
1042a6598f23SBen Coburn}
1043340756e4Sandi
104455efc227SAndreas Gohr/**
104555efc227SAndreas Gohr * Returns the requested EXIF/IPTC tag from the current image
104655efc227SAndreas Gohr *
104755efc227SAndreas Gohr * If $tags is an array all given tags are tried until a
104855efc227SAndreas Gohr * value is found. If no value is found $alt is returned.
104955efc227SAndreas Gohr *
105055efc227SAndreas Gohr * Which texts are known is defined in the functions _exifTagNames
105155efc227SAndreas Gohr * and _iptcTagNames() in inc/jpeg.php (You need to prepend IPTC
105255efc227SAndreas Gohr * to the names of the latter one)
105355efc227SAndreas Gohr *
10543df72098SAndreas Gohr * Only allowed in: detail.php
105555efc227SAndreas Gohr *
105621d806cdSGerrit Uitslag * @param array|string $tags tag or array of tags to try
1057ac7a515fSAndreas Gohr * @param string $alt alternative output if no data was found
1058e0c26282SGerrit Uitslag * @param null|string $src the image src, uses global $SRC if not given
1059ac7a515fSAndreas Gohr * @return string
10604dc42f7fSGerrit Uitslag *
10614dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
106255efc227SAndreas Gohr */
1063d868eb89SAndreas Gohrfunction tpl_img_getTag($tags, $alt = '', $src = null)
1064d868eb89SAndreas Gohr{
106555efc227SAndreas Gohr    // Init Exif Reader
1066a46a7ce3Sasivery    global $SRC, $imgMeta;
10673df72098SAndreas Gohr
10683df72098SAndreas Gohr    if (is_null($src)) $src = $SRC;
10691eadd9e8SAndreas Gohr    if (is_null($src)) return $alt;
10703df72098SAndreas Gohr
10714dc42f7fSGerrit Uitslag    if (!isset($imgMeta)) {
10724dc42f7fSGerrit Uitslag        $imgMeta = new JpegMeta($src);
10734dc42f7fSGerrit Uitslag    }
1074a46a7ce3Sasivery    if ($imgMeta === false) return $alt;
1075a46a7ce3Sasivery    $info = cleanText($imgMeta->getField($tags));
10764dc42f7fSGerrit Uitslag    if (!$info) return $alt;
107755efc227SAndreas Gohr    return $info;
107855efc227SAndreas Gohr}
107955efc227SAndreas Gohr
1080a46a7ce3Sasivery
1081a46a7ce3Sasivery/**
1082a46a7ce3Sasivery * Garbage collects up the open JpegMeta object.
1083a46a7ce3Sasivery */
1084d868eb89SAndreas Gohrfunction tpl_img_close()
1085d868eb89SAndreas Gohr{
1086a46a7ce3Sasivery    global $imgMeta;
1087a46a7ce3Sasivery    $imgMeta = null;
1088a46a7ce3Sasivery}
1089a46a7ce3Sasivery
109055efc227SAndreas Gohr/**
10914dc42f7fSGerrit Uitslag * Prints a html description list of the metatags of the current image
1092becfa414SGerrit Uitslag */
1093d868eb89SAndreas Gohrfunction tpl_img_meta()
1094d868eb89SAndreas Gohr{
1095becfa414SGerrit Uitslag    global $lang;
1096becfa414SGerrit Uitslag
1097becfa414SGerrit Uitslag    $tags = tpl_get_img_meta();
1098becfa414SGerrit Uitslag
1099becfa414SGerrit Uitslag    echo '<dl>';
1100becfa414SGerrit Uitslag    foreach ($tags as $tag) {
1101becfa414SGerrit Uitslag        $label = $lang[$tag['langkey']];
1102fde860beSGerrit Uitslag        if (!$label) $label = $tag['langkey'] . ':';
1103becfa414SGerrit Uitslag
1104fde860beSGerrit Uitslag        echo '<dt>' . $label . '</dt><dd>';
1105becfa414SGerrit Uitslag        if ($tag['type'] == 'date') {
1106becfa414SGerrit Uitslag            echo dformat($tag['value']);
1107becfa414SGerrit Uitslag        } else {
1108becfa414SGerrit Uitslag            echo hsc($tag['value']);
1109becfa414SGerrit Uitslag        }
1110becfa414SGerrit Uitslag        echo '</dd>';
1111becfa414SGerrit Uitslag    }
1112becfa414SGerrit Uitslag    echo '</dl>';
1113becfa414SGerrit Uitslag}
1114becfa414SGerrit Uitslag
1115becfa414SGerrit Uitslag/**
1116becfa414SGerrit Uitslag * Returns metadata as configured in mediameta config file, ready for creating html
1117becfa414SGerrit Uitslag *
1118becfa414SGerrit Uitslag * @return array with arrays containing the entries:
1119becfa414SGerrit Uitslag *   - string langkey  key to lookup in the $lang var, if not found printed as is
1120becfa414SGerrit Uitslag *   - string type     type of value
1121becfa414SGerrit Uitslag *   - string value    tag value (unescaped)
1122becfa414SGerrit Uitslag */
1123d868eb89SAndreas Gohrfunction tpl_get_img_meta()
1124d868eb89SAndreas Gohr{
1125becfa414SGerrit Uitslag
1126becfa414SGerrit Uitslag    $config_files = getConfigFiles('mediameta');
1127becfa414SGerrit Uitslag    foreach ($config_files as $config_file) {
112879e79377SAndreas Gohr        if (file_exists($config_file)) {
1129becfa414SGerrit Uitslag            include($config_file);
1130becfa414SGerrit Uitslag        }
1131becfa414SGerrit Uitslag    }
113224870174SAndreas Gohr    $tags = [];
1133becfa414SGerrit Uitslag    foreach ($fields as $tag) {
113424870174SAndreas Gohr        $t = [];
1135becfa414SGerrit Uitslag        if (!empty($tag[0])) {
113624870174SAndreas Gohr            $t = [$tag[0]];
1137becfa414SGerrit Uitslag        }
1138056bf31fSDamien Regad        if (isset($tag[3]) && is_array($tag[3])) {
1139becfa414SGerrit Uitslag            $t = array_merge($t, $tag[3]);
1140becfa414SGerrit Uitslag        }
1141becfa414SGerrit Uitslag        $value = tpl_img_getTag($t);
1142becfa414SGerrit Uitslag        if ($value) {
114324870174SAndreas Gohr            $tags[] = ['langkey' => $tag[1], 'type' => $tag[2], 'value' => $value];
1144becfa414SGerrit Uitslag        }
1145becfa414SGerrit Uitslag    }
1146becfa414SGerrit Uitslag    return $tags;
1147becfa414SGerrit Uitslag}
1148becfa414SGerrit Uitslag
1149becfa414SGerrit Uitslag/**
115055efc227SAndreas Gohr * Prints the image with a link to the full sized version
115155efc227SAndreas Gohr *
115255efc227SAndreas Gohr * Only allowed in: detail.php
1153a02d2933SAndreas Gohr *
1154ac7a515fSAndreas Gohr * @triggers TPL_IMG_DISPLAY
11554dc42f7fSGerrit Uitslag * @param int $maxwidth - maximal width of the image
11564dc42f7fSGerrit Uitslag * @param int $maxheight - maximal height of the image
11574dc42f7fSGerrit Uitslag * @param bool $link - link to the orginal size?
11584dc42f7fSGerrit Uitslag * @param array $params - additional image attributes
115942ea7f44SGerrit Uitslag * @return bool Result of TPL_IMG_DISPLAY
116055efc227SAndreas Gohr */
1161d868eb89SAndreas Gohrfunction tpl_img($maxwidth = 0, $maxheight = 0, $link = true, $params = null)
1162d868eb89SAndreas Gohr{
116355efc227SAndreas Gohr    global $IMG;
1164585bf44eSChristopher Smith    /** @var Input $INPUT */
1165ac7a515fSAndreas Gohr    global $INPUT;
11665c2eed9aSlisps    global $REV;
116765d3a5dbSAndreas Gohr    $w = (int)tpl_img_getTag('File.Width');
116865d3a5dbSAndreas Gohr    $h = (int)tpl_img_getTag('File.Height');
116955efc227SAndreas Gohr
117055efc227SAndreas Gohr    //resize to given max values
117123a34783SAndreas Gohr    $ratio = 1;
117223a34783SAndreas Gohr    if ($w >= $h) {
1173f8925855Sjoe.lapp        if ($maxwidth && $w >= $maxwidth) {
117455efc227SAndreas Gohr            $ratio = $maxwidth / $w;
1175f8925855Sjoe.lapp        } elseif ($maxheight && $h > $maxheight) {
117655efc227SAndreas Gohr            $ratio = $maxheight / $h;
117755efc227SAndreas Gohr        }
117824870174SAndreas Gohr    } elseif ($maxheight && $h >= $maxheight) {
117955efc227SAndreas Gohr        $ratio = $maxheight / $h;
1180f8925855Sjoe.lapp    } elseif ($maxwidth && $w > $maxwidth) {
118155efc227SAndreas Gohr        $ratio = $maxwidth / $w;
118255efc227SAndreas Gohr    }
118355efc227SAndreas Gohr    if ($ratio) {
118455efc227SAndreas Gohr        $w = floor($ratio * $w);
118555efc227SAndreas Gohr        $h = floor($ratio * $h);
118655efc227SAndreas Gohr    }
118755efc227SAndreas Gohr
11886de3759aSAndreas Gohr    //prepare URLs
118924870174SAndreas Gohr    $url = ml($IMG, ['cache' => $INPUT->str('cache'), 'rev' => $REV], true, '&');
119024870174SAndreas Gohr    $src = ml($IMG, ['cache' => $INPUT->str('cache'), 'rev' => $REV, 'w' => $w, 'h' => $h], true, '&');
119155efc227SAndreas Gohr
11922684e50aSAndreas Gohr    //prepare attributes
119355efc227SAndreas Gohr    $alt = tpl_img_getTag('Simple.Title');
1194a02d2933SAndreas Gohr    if (is_null($params)) {
119524870174SAndreas Gohr        $p = [];
1196a02d2933SAndreas Gohr    } else {
1197a02d2933SAndreas Gohr        $p = $params;
1198a02d2933SAndreas Gohr    }
11992684e50aSAndreas Gohr    if ($w) $p['width'] = $w;
12002684e50aSAndreas Gohr    if ($h) $p['height'] = $h;
12012684e50aSAndreas Gohr    $p['class'] = 'img_detail';
12022684e50aSAndreas Gohr    if ($alt) {
12032684e50aSAndreas Gohr        $p['alt'] = $alt;
12042684e50aSAndreas Gohr        $p['title'] = $alt;
12052684e50aSAndreas Gohr    } else {
12062684e50aSAndreas Gohr        $p['alt'] = '';
12072684e50aSAndreas Gohr    }
1208a02d2933SAndreas Gohr    $p['src'] = $src;
120955efc227SAndreas Gohr
121024870174SAndreas Gohr    $data = ['url' => ($link ? $url : null), 'params' => $p];
1211cbb44eabSAndreas Gohr    return Event::createAndTrigger('TPL_IMG_DISPLAY', $data, '_tpl_img_action', true);
1212a02d2933SAndreas Gohr}
1213a02d2933SAndreas Gohr
1214a02d2933SAndreas Gohr/**
1215a02d2933SAndreas Gohr * Default action for TPL_IMG_DISPLAY
1216ac7a515fSAndreas Gohr *
1217ac7a515fSAndreas Gohr * @param array $data
1218ac7a515fSAndreas Gohr * @return bool
1219a02d2933SAndreas Gohr */
1220d868eb89SAndreas Gohrfunction _tpl_img_action($data)
1221d868eb89SAndreas Gohr{
122259f3611bSAnika Henke    global $lang;
1223a02d2933SAndreas Gohr    $p = buildAttributes($data['params']);
1224a02d2933SAndreas Gohr
122526dfc232SAndreas Gohr    if ($data['url']) echo '<a href="' . hsc($data['url']) . '" title="' . $lang['mediaview'] . '">';
122626dfc232SAndreas Gohr    echo '<img ' . $p . '/>';
122726dfc232SAndreas Gohr    if ($data['url']) echo '</a>';
122854e95700STom N Harris    return true;
122955efc227SAndreas Gohr}
123055efc227SAndreas Gohr
12317367b368SAndreas Gohr/**
1232881f2ee2SAndreas Haerter * This function inserts a small gif which in reality is the indexer function.
12337367b368SAndreas Gohr *
12344dc42f7fSGerrit Uitslag * Should be called somewhere at the very end of the main.php template
1235ac7a515fSAndreas Gohr *
1236ac7a515fSAndreas Gohr * @return bool
12377367b368SAndreas Gohr */
1238d868eb89SAndreas Gohrfunction tpl_indexerWebBug()
1239d868eb89SAndreas Gohr{
12407367b368SAndreas Gohr    global $ID;
12411dad36f5SAndreas Gohr
124224870174SAndreas Gohr    $p = [];
12434af80fe8SMichael Große    $p['src'] = DOKU_BASE . 'lib/exe/taskrunner.php?id=' . rawurlencode($ID) .
1244e68c51baSAndreas Gohr        '&' . time();
1245881f2ee2SAndreas Haerter    $p['width'] = 2; //no more 1x1 px image because we live in times of ad blockers...
12467367b368SAndreas Gohr    $p['height'] = 1;
12477367b368SAndreas Gohr    $p['alt'] = '';
12487367b368SAndreas Gohr    $att = buildAttributes($p);
124926dfc232SAndreas Gohr    echo "<img $att />";
125054e95700STom N Harris    return true;
12517367b368SAndreas Gohr}
12527367b368SAndreas Gohr
125378d4e784SEsther Brunner/**
125478d4e784SEsther Brunner * tpl_getConf($id)
125578d4e784SEsther Brunner *
125678d4e784SEsther Brunner * use this function to access template configuration variables
1257ac7a515fSAndreas Gohr *
125817448fb8SChristopher Smith * @param string $id name of the value to access
125917448fb8SChristopher Smith * @param mixed $notset what to return if the setting is not available
126017448fb8SChristopher Smith * @return mixed
126178d4e784SEsther Brunner */
1262d868eb89SAndreas Gohrfunction tpl_getConf($id, $notset = false)
1263d868eb89SAndreas Gohr{
126478d4e784SEsther Brunner    global $conf;
126517566ac6SAdrian Lang    static $tpl_configloaded = false;
126678d4e784SEsther Brunner
126778d4e784SEsther Brunner    $tpl = $conf['template'];
126878d4e784SEsther Brunner
126978d4e784SEsther Brunner    if (!$tpl_configloaded) {
127078d4e784SEsther Brunner        $tconf = tpl_loadConfig();
127178d4e784SEsther Brunner        if ($tconf !== false) {
127278d4e784SEsther Brunner            foreach ($tconf as $key => $value) {
127378d4e784SEsther Brunner                if (isset($conf['tpl'][$tpl][$key])) continue;
127478d4e784SEsther Brunner                $conf['tpl'][$tpl][$key] = $value;
127578d4e784SEsther Brunner            }
127678d4e784SEsther Brunner            $tpl_configloaded = true;
127778d4e784SEsther Brunner        }
127878d4e784SEsther Brunner    }
127978d4e784SEsther Brunner
128024870174SAndreas Gohr    return $conf['tpl'][$tpl][$id] ?? $notset;
128117448fb8SChristopher Smith}
128217448fb8SChristopher Smith
128378d4e784SEsther Brunner/**
128478d4e784SEsther Brunner * tpl_loadConfig()
1285ac7a515fSAndreas Gohr *
128678d4e784SEsther Brunner * reads all template configuration variables
128778d4e784SEsther Brunner * this function is automatically called by tpl_getConf()
1288ac7a515fSAndreas Gohr *
12894dc42f7fSGerrit Uitslag * @return false|array
129078d4e784SEsther Brunner */
1291d868eb89SAndreas Gohrfunction tpl_loadConfig()
1292d868eb89SAndreas Gohr{
129378d4e784SEsther Brunner
1294c4766956SAndreas Gohr    $file = tpl_incdir() . '/conf/default.php';
129524870174SAndreas Gohr    $conf = [];
129678d4e784SEsther Brunner
129779e79377SAndreas Gohr    if (!file_exists($file)) return false;
129878d4e784SEsther Brunner
129978d4e784SEsther Brunner    // load default config file
130078d4e784SEsther Brunner    include($file);
130178d4e784SEsther Brunner
130278d4e784SEsther Brunner    return $conf;
130378d4e784SEsther Brunner}
130478d4e784SEsther Brunner
130517566ac6SAdrian Lang// language methods
13064dc42f7fSGerrit Uitslag
130717566ac6SAdrian Lang/**
130817566ac6SAdrian Lang * tpl_getLang($id)
130917566ac6SAdrian Lang *
131017566ac6SAdrian Lang * use this function to access template language variables
131142ea7f44SGerrit Uitslag *
131242ea7f44SGerrit Uitslag * @param string $id key of language string
131342ea7f44SGerrit Uitslag * @return string
131417566ac6SAdrian Lang */
1315d868eb89SAndreas Gohrfunction tpl_getLang($id)
1316d868eb89SAndreas Gohr{
131724870174SAndreas Gohr    static $lang = [];
131817566ac6SAdrian Lang
131917566ac6SAdrian Lang    if (count($lang) === 0) {
1320dd7a6159SGerrit Uitslag        global $conf, $config_cascade; // definitely don't invoke "global $lang"
1321dd7a6159SGerrit Uitslag
1322c4766956SAndreas Gohr        $path = tpl_incdir() . 'lang/';
132317566ac6SAdrian Lang
132424870174SAndreas Gohr        $lang = [];
132517566ac6SAdrian Lang
132617566ac6SAdrian Lang        // don't include once
132717566ac6SAdrian Lang        @include($path . 'en/lang.php');
1328dd7a6159SGerrit Uitslag        foreach ($config_cascade['lang']['template'] as $config_file) {
132979e79377SAndreas Gohr            if (file_exists($config_file . $conf['template'] . '/en/lang.php')) {
1330dd7a6159SGerrit Uitslag                include($config_file . $conf['template'] . '/en/lang.php');
1331dd7a6159SGerrit Uitslag            }
133217566ac6SAdrian Lang        }
133317566ac6SAdrian Lang
1334dd7a6159SGerrit Uitslag        if ($conf['lang'] != 'en') {
1335dd7a6159SGerrit Uitslag            @include($path . $conf['lang'] . '/lang.php');
1336dd7a6159SGerrit Uitslag            foreach ($config_cascade['lang']['template'] as $config_file) {
133779e79377SAndreas Gohr                if (file_exists($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php')) {
1338dd7a6159SGerrit Uitslag                    include($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php');
1339dd7a6159SGerrit Uitslag                }
1340dd7a6159SGerrit Uitslag            }
1341dd7a6159SGerrit Uitslag        }
134217566ac6SAdrian Lang    }
134324870174SAndreas Gohr    return $lang[$id] ?? '';
134417566ac6SAdrian Lang}
134517566ac6SAdrian Lang
13463df72098SAndreas Gohr/**
1347c5c17fdaSKlap-in * Retrieve a language dependent file and pass to xhtml renderer for display
1348e8ec13b9SKlap-in * template equivalent of p_locale_xhtml()
1349e8ec13b9SKlap-in *
1350e8ec13b9SKlap-in * @param string $id id of language dependent wiki page
1351e8ec13b9SKlap-in * @return  string     parsed contents of the wiki page in xhtml format
1352e8ec13b9SKlap-in */
1353d868eb89SAndreas Gohrfunction tpl_locale_xhtml($id)
1354d868eb89SAndreas Gohr{
1355c5c17fdaSKlap-in    return p_cached_output(tpl_localeFN($id));
1356e8ec13b9SKlap-in}
1357e8ec13b9SKlap-in
1358e8ec13b9SKlap-in/**
1359c5c17fdaSKlap-in * Prepends appropriate path for a language dependent filename
136042ea7f44SGerrit Uitslag *
136142ea7f44SGerrit Uitslag * @param string $id id of localized text
136242ea7f44SGerrit Uitslag * @return string wiki text
1363e8ec13b9SKlap-in */
1364d868eb89SAndreas Gohrfunction tpl_localeFN($id)
1365d868eb89SAndreas Gohr{
1366e8ec13b9SKlap-in    $path = tpl_incdir() . 'lang/';
1367e8ec13b9SKlap-in    global $conf;
136838fb1fc7SGerrit Uitslag    $file = DOKU_CONF . 'template_lang/' . $conf['template'] . '/' . $conf['lang'] . '/' . $id . '.txt';
136979e79377SAndreas Gohr    if (!file_exists($file)) {
1370e8ec13b9SKlap-in        $file = $path . $conf['lang'] . '/' . $id . '.txt';
137179e79377SAndreas Gohr        if (!file_exists($file)) {
1372e8ec13b9SKlap-in            //fall back to english
1373e8ec13b9SKlap-in            $file = $path . 'en/' . $id . '.txt';
1374e8ec13b9SKlap-in        }
1375e8ec13b9SKlap-in    }
1376e8ec13b9SKlap-in    return $file;
1377e8ec13b9SKlap-in}
1378e8ec13b9SKlap-in
1379e8ec13b9SKlap-in/**
13807abc270fSGerrit Uitslag * prints the "main content" in the mediamanager popup
13813df72098SAndreas Gohr *
13823df72098SAndreas Gohr * Depending on the user's actions this may be a list of
13833df72098SAndreas Gohr * files in a namespace, the meta editing dialog or
13843df72098SAndreas Gohr * a message of referencing pages
13853df72098SAndreas Gohr *
13863df72098SAndreas Gohr * Only allowed in mediamanager.php
13873df72098SAndreas Gohr *
1388c182313eSAndreas Gohr * @triggers MEDIAMANAGER_CONTENT_OUTPUT
1389c182313eSAndreas Gohr * @param bool $fromajax - set true when calling this function via ajax
139042ea7f44SGerrit Uitslag * @param string $sort
13918702de7fSGerrit Uitslag *
13923df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
13933df72098SAndreas Gohr */
1394d868eb89SAndreas Gohrfunction tpl_mediaContent($fromajax = false, $sort = 'natural')
1395d868eb89SAndreas Gohr{
13963df72098SAndreas Gohr    global $IMG;
13973df72098SAndreas Gohr    global $AUTH;
13983df72098SAndreas Gohr    global $INUSE;
13993df72098SAndreas Gohr    global $NS;
14003df72098SAndreas Gohr    global $JUMPTO;
1401585bf44eSChristopher Smith    /** @var Input $INPUT */
1402ac7a515fSAndreas Gohr    global $INPUT;
14033df72098SAndreas Gohr
1404ac7a515fSAndreas Gohr    $do = $INPUT->extract('do')->str('do');
140524870174SAndreas Gohr    if (in_array($do, ['save', 'cancel'])) $do = '';
1406c182313eSAndreas Gohr
1407c182313eSAndreas Gohr    if (!$do) {
1408ac7a515fSAndreas Gohr        if ($INPUT->bool('edit')) {
1409c182313eSAndreas Gohr            $do = 'metaform';
1410c182313eSAndreas Gohr        } elseif (is_array($INUSE)) {
1411c182313eSAndreas Gohr            $do = 'filesinuse';
1412c182313eSAndreas Gohr        } else {
1413c182313eSAndreas Gohr            $do = 'filelist';
1414c182313eSAndreas Gohr        }
1415c182313eSAndreas Gohr    }
1416c182313eSAndreas Gohr
1417c182313eSAndreas Gohr    // output the content pane, wrapped in an event.
1418a664aabaSAndreas Gohr    if (!$fromajax) echo '<div id="media__content">';
141924870174SAndreas Gohr    $data = ['do' => $do];
1420e1d9dcc8SAndreas Gohr    $evt = new Event('MEDIAMANAGER_CONTENT_OUTPUT', $data);
1421c182313eSAndreas Gohr    if ($evt->advise_before()) {
1422c182313eSAndreas Gohr        $do = $data['do'];
142330fd72fbSKate Arzamastseva        if ($do == 'filesinuse') {
1424c182313eSAndreas Gohr            media_filesinuse($INUSE, $IMG);
1425c182313eSAndreas Gohr        } elseif ($do == 'filelist') {
142600e3e394SChristopher Smith            media_filelist($NS, $AUTH, $JUMPTO, false, $sort);
1427c9f56829SAndreas Gohr        } elseif ($do == 'searchlist') {
1428ac7a515fSAndreas Gohr            media_searchlist($INPUT->str('q'), $NS, $AUTH);
1429c182313eSAndreas Gohr        } else {
1430c182313eSAndreas Gohr            msg('Unknown action ' . hsc($do), -1);
1431c182313eSAndreas Gohr        }
1432c182313eSAndreas Gohr    }
1433c182313eSAndreas Gohr    $evt->advise_after();
1434c182313eSAndreas Gohr    unset($evt);
1435a664aabaSAndreas Gohr    if (!$fromajax) echo '</div>';
14363df72098SAndreas Gohr}
14373df72098SAndreas Gohr
14383df72098SAndreas Gohr/**
1439d9162c6cSKate Arzamastseva * Prints the central column in full-screen media manager
1440d9162c6cSKate Arzamastseva * Depending on the opened tab this may be a list of
1441d9162c6cSKate Arzamastseva * files in a namespace, upload form or search form
1442d9162c6cSKate Arzamastseva *
1443d9162c6cSKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net>
1444d9162c6cSKate Arzamastseva */
1445d868eb89SAndreas Gohrfunction tpl_mediaFileList()
1446d868eb89SAndreas Gohr{
1447d9162c6cSKate Arzamastseva    global $AUTH;
1448d9162c6cSKate Arzamastseva    global $NS;
1449d9162c6cSKate Arzamastseva    global $JUMPTO;
145095b451bcSAdrian Lang    global $lang;
1451585bf44eSChristopher Smith    /** @var Input $INPUT */
1452ac7a515fSAndreas Gohr    global $INPUT;
1453d9162c6cSKate Arzamastseva
1454ac7a515fSAndreas Gohr    $opened_tab = $INPUT->str('tab_files');
145524870174SAndreas Gohr    if (!$opened_tab || !in_array($opened_tab, ['files', 'upload', 'search'])) $opened_tab = 'files';
1456ac7a515fSAndreas Gohr    if ($INPUT->str('mediado') == 'update') $opened_tab = 'upload';
1457d9162c6cSKate Arzamastseva
145894add303SAnika Henke    echo '<h2 class="a11y">' . $lang['mediaselect'] . '</h2>' . NL;
145995b451bcSAdrian Lang
1460ed69a2aeSKate Arzamastseva    media_tabs_files($opened_tab);
146123846a98SKate Arzamastseva
146294add303SAnika Henke    echo '<div class="panelHeader">' . NL;
146395b451bcSAdrian Lang    echo '<h3>';
146424870174SAndreas Gohr    $tabTitle = $NS ?: '[' . $lang['mediaroot'] . ']';
1465c98f205eSAdrian Lang    printf($lang['media_' . $opened_tab], '<strong>' . hsc($tabTitle) . '</strong>');
146694add303SAnika Henke    echo '</h3>' . NL;
146795b451bcSAdrian Lang    if ($opened_tab === 'search' || $opened_tab === 'files') {
146895b451bcSAdrian Lang        media_tab_files_options();
146923846a98SKate Arzamastseva    }
147094add303SAnika Henke    echo '</div>' . NL;
1471d9162c6cSKate Arzamastseva
147294add303SAnika Henke    echo '<div class="panelContent">' . NL;
147395b451bcSAdrian Lang    if ($opened_tab == 'files') {
147495b451bcSAdrian Lang        media_tab_files($NS, $AUTH, $JUMPTO);
147595b451bcSAdrian Lang    } elseif ($opened_tab == 'upload') {
147695b451bcSAdrian Lang        media_tab_upload($NS, $AUTH, $JUMPTO);
147795b451bcSAdrian Lang    } elseif ($opened_tab == 'search') {
147895b451bcSAdrian Lang        media_tab_search($NS, $AUTH);
147995b451bcSAdrian Lang    }
148094add303SAnika Henke    echo '</div>' . NL;
1481d9162c6cSKate Arzamastseva}
1482d9162c6cSKate Arzamastseva
1483d9162c6cSKate Arzamastseva/**
1484d9162c6cSKate Arzamastseva * Prints the third column in full-screen media manager
1485d9162c6cSKate Arzamastseva * Depending on the opened tab this may be details of the
1486d9162c6cSKate Arzamastseva * selected file, the meta editing dialog or
1487d9162c6cSKate Arzamastseva * list of file revisions
1488d9162c6cSKate Arzamastseva *
1489f50a239bSTakamura * @param string $image
1490f50a239bSTakamura * @param boolean $rev
14914dc42f7fSGerrit Uitslag *
14924dc42f7fSGerrit Uitslag * @author Kate Arzamastseva <pshns@ukr.net>
1493d9162c6cSKate Arzamastseva */
1494d868eb89SAndreas Gohrfunction tpl_mediaFileDetails($image, $rev)
1495d868eb89SAndreas Gohr{
1496e8a2a143SMichael Hamann    global $conf, $DEL, $lang;
1497585bf44eSChristopher Smith    /** @var Input $INPUT */
1498585bf44eSChristopher Smith    global $INPUT;
1499d9162c6cSKate Arzamastseva
150064159a61SAndreas Gohr    $removed = (
150164159a61SAndreas Gohr        !file_exists(mediaFN($image)) &&
150264159a61SAndreas Gohr        file_exists(mediaMetaFN($image, '.changes')) &&
150364159a61SAndreas Gohr        $conf['mediarevisions']
150464159a61SAndreas Gohr    );
1505ac7a515fSAndreas Gohr    if (!$image || (!file_exists(mediaFN($image)) && !$removed) || $DEL) return;
15066dd095f5SKate Arzamastseva    if ($rev && !file_exists(mediaFN($image, $rev))) $rev = false;
1507e8a2a143SMichael Hamann    $ns = getNS($image);
1508ac7a515fSAndreas Gohr    $do = $INPUT->str('mediado');
15091eeeced2SKate Arzamastseva
1510ac7a515fSAndreas Gohr    $opened_tab = $INPUT->str('tab_details');
1511e5d185e1SKate Arzamastseva
151224870174SAndreas Gohr    $tab_array = ['view'];
151324870174SAndreas Gohr    [, $mime] = mimetype($image);
1514e5d185e1SKate Arzamastseva    if ($mime == 'image/jpeg') {
1515e5d185e1SKate Arzamastseva        $tab_array[] = 'edit';
1516e5d185e1SKate Arzamastseva    }
1517e5d185e1SKate Arzamastseva    if ($conf['mediarevisions']) {
1518e5d185e1SKate Arzamastseva        $tab_array[] = 'history';
1519e5d185e1SKate Arzamastseva    }
1520e5d185e1SKate Arzamastseva
1521e5d185e1SKate Arzamastseva    if (!$opened_tab || !in_array($opened_tab, $tab_array)) $opened_tab = 'view';
1522ac7a515fSAndreas Gohr    if ($INPUT->bool('edit')) $opened_tab = 'edit';
152323846a98SKate Arzamastseva    if ($do == 'restore') $opened_tab = 'view';
1524d9162c6cSKate Arzamastseva
1525ed69a2aeSKate Arzamastseva    media_tabs_details($image, $opened_tab);
152623846a98SKate Arzamastseva
152759f3611bSAnika Henke    echo '<div class="panelHeader"><h3>';
152824870174SAndreas Gohr    [$ext] = mimetype($image, false);
152995b451bcSAdrian Lang    $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
153095b451bcSAdrian Lang    $class = 'select mediafile mf_' . $class;
153124870174SAndreas Gohr
1532750a0b51SMichael Große    $attributes = $rev ? ['rev' => $rev] : [];
153390fb952cSAndreas Gohr    $tabTitle = sprintf(
153490fb952cSAndreas Gohr        '<strong><a href="%s" class="%s" title="%s">%s</a></strong>',
153590fb952cSAndreas Gohr        ml($image, $attributes),
153690fb952cSAndreas Gohr        $class,
153790fb952cSAndreas Gohr        $lang['mediaview'],
153890fb952cSAndreas Gohr        $image
153990fb952cSAndreas Gohr    );
154008317413SAdrian Lang    if ($opened_tab === 'view' && $rev) {
154108317413SAdrian Lang        printf($lang['media_viewold'], $tabTitle, dformat($rev));
154208317413SAdrian Lang    } else {
1543026d14a9SAnika Henke        printf($lang['media_' . $opened_tab], $tabTitle);
154408317413SAdrian Lang    }
1545b8a84c03SAndreas Gohr
154694add303SAnika Henke    echo '</h3></div>' . NL;
154795b451bcSAdrian Lang
154894add303SAnika Henke    echo '<div class="panelContent">' . NL;
154995b451bcSAdrian Lang
155023846a98SKate Arzamastseva    if ($opened_tab == 'view') {
1551e8a2a143SMichael Hamann        media_tab_view($image, $ns, null, $rev);
155292cac9a9SKate Arzamastseva    } elseif ($opened_tab == 'edit' && !$removed) {
1553e8a2a143SMichael Hamann        media_tab_edit($image, $ns);
1554e5d185e1SKate Arzamastseva    } elseif ($opened_tab == 'history' && $conf['mediarevisions']) {
1555e8a2a143SMichael Hamann        media_tab_history($image, $ns);
155623846a98SKate Arzamastseva    }
155795b451bcSAdrian Lang
155894add303SAnika Henke    echo '</div>' . NL;
1559d9162c6cSKate Arzamastseva}
1560d9162c6cSKate Arzamastseva
1561d9162c6cSKate Arzamastseva/**
15627abc270fSGerrit Uitslag * prints the namespace tree in the mediamanager popup
15633df72098SAndreas Gohr *
15643df72098SAndreas Gohr * Only allowed in mediamanager.php
15653df72098SAndreas Gohr *
15663df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
15673df72098SAndreas Gohr */
1568d868eb89SAndreas Gohrfunction tpl_mediaTree()
1569d868eb89SAndreas Gohr{
15703df72098SAndreas Gohr    global $NS;
1571a664aabaSAndreas Gohr    echo '<div id="media__tree">';
15723df72098SAndreas Gohr    media_nstree($NS);
1573a664aabaSAndreas Gohr    echo '</div>';
15743df72098SAndreas Gohr}
15753df72098SAndreas Gohr
1576a00de5b5SAndreas Gohr/**
1577a00de5b5SAndreas Gohr * Print a dropdown menu with all DokuWiki actions
1578a00de5b5SAndreas Gohr *
1579a00de5b5SAndreas Gohr * Note: this will not use any pretty URLs
1580a00de5b5SAndreas Gohr *
158142ea7f44SGerrit Uitslag * @param string $empty empty option label
158242ea7f44SGerrit Uitslag * @param string $button submit button label
15834dc42f7fSGerrit Uitslag *
15844dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
1585affc7ddfSAndreas Gohr * @deprecated 2017-09-01 see devel:menus
1586a00de5b5SAndreas Gohr */
1587d868eb89SAndreas Gohrfunction tpl_actiondropdown($empty = '', $button = '&gt;')
1588d868eb89SAndreas Gohr{
1589affc7ddfSAndreas Gohr    dbg_deprecated('see devel:menus');
159024870174SAndreas Gohr    $menu = new MobileMenu();
15911e875dcdSAndreas Gohr    echo $menu->getDropdown($empty, $button);
1592a00de5b5SAndreas Gohr}
1593a00de5b5SAndreas Gohr
1594066fee30SAndreas Gohr/**
1595066fee30SAndreas Gohr * Print a informational line about the used license
1596066fee30SAndreas Gohr *
1597ac7a515fSAndreas Gohr * @param string $img print image? (|button|badge)
1598ac7a515fSAndreas Gohr * @param bool $imgonly skip the textual description?
1599ac7a515fSAndreas Gohr * @param bool $return when true don't print, but return HTML
1600ac7a515fSAndreas Gohr * @param bool $wrap wrap in div with class="license"?
1601ac7a515fSAndreas Gohr * @return string
16024dc42f7fSGerrit Uitslag *
16034dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
1604066fee30SAndreas Gohr */
1605d868eb89SAndreas Gohrfunction tpl_license($img = 'badge', $imgonly = false, $return = false, $wrap = true)
1606d868eb89SAndreas Gohr{
1607066fee30SAndreas Gohr    global $license;
1608066fee30SAndreas Gohr    global $conf;
1609066fee30SAndreas Gohr    global $lang;
1610066fee30SAndreas Gohr    if (!$conf['license']) return '';
1611066fee30SAndreas Gohr    if (!is_array($license[$conf['license']])) return '';
1612066fee30SAndreas Gohr    $lic = $license[$conf['license']];
161353e15c8bSAnika Henke    $target = ($conf['target']['extern']) ? ' target="' . $conf['target']['extern'] . '"' : '';
1614066fee30SAndreas Gohr
161580083a41SAndreas Gohr    $out = '';
161680083a41SAndreas Gohr    if ($wrap) $out .= '<div class="license">';
1617066fee30SAndreas Gohr    if ($img) {
1618066fee30SAndreas Gohr        $src = license_img($img);
1619066fee30SAndreas Gohr        if ($src) {
162053e15c8bSAnika Henke            $out .= '<a href="' . $lic['url'] . '" rel="license"' . $target;
162153e15c8bSAnika Henke            $out .= '><img src="' . DOKU_BASE . $src . '" alt="' . $lic['name'] . '" /></a>';
162253e15c8bSAnika Henke            if (!$imgonly) $out .= ' ';
1623066fee30SAndreas Gohr        }
1624066fee30SAndreas Gohr    }
16254cefd216SMichael Klier    if (!$imgonly) {
162653e15c8bSAnika Henke        $out .= $lang['license'] . ' ';
1627d317fb5dSAnika Henke        $out .= '<bdi><a href="' . $lic['url'] . '" rel="license" class="urlextern"' . $target;
1628d317fb5dSAnika Henke        $out .= '>' . $lic['name'] . '</a></bdi>';
16294cefd216SMichael Klier    }
163080083a41SAndreas Gohr    if ($wrap) $out .= '</div>';
1631066fee30SAndreas Gohr
1632066fee30SAndreas Gohr    if ($return) return $out;
1633066fee30SAndreas Gohr    echo $out;
1634ac7a515fSAndreas Gohr    return '';
1635066fee30SAndreas Gohr}
1636066fee30SAndreas Gohr
1637a81910eeSAndreas Gohr/**
1638835dfcaeSAnika Henke * Includes the rendered HTML of a given page
1639a81910eeSAndreas Gohr *
1640a81910eeSAndreas Gohr * This function is useful to populate sidebars or similar features in a
1641a81910eeSAndreas Gohr * template
1642e0c26282SGerrit Uitslag *
16437a112df5SAndreas Gohr * @param string $pageid The page name you want to include
16447a112df5SAndreas Gohr * @param bool $print Should the content be printed or returned only
16457a112df5SAndreas Gohr * @param bool $propagate Search higher namespaces, too?
16467c3e4a67SAndreas Gohr * @param bool $useacl Include the page only if the ACLs check out?
1647e0c26282SGerrit Uitslag * @return bool|null|string
1648a81910eeSAndreas Gohr */
1649d868eb89SAndreas Gohrfunction tpl_include_page($pageid, $print = true, $propagate = false, $useacl = true)
1650d868eb89SAndreas Gohr{
16517a112df5SAndreas Gohr    if ($propagate) {
16527c3e4a67SAndreas Gohr        $pageid = page_findnearest($pageid, $useacl);
16537c3e4a67SAndreas Gohr    } elseif ($useacl && auth_quickaclcheck($pageid) == AUTH_NONE) {
16547a112df5SAndreas Gohr        return false;
16557a112df5SAndreas Gohr    }
1656c786a1b6SAnika Henke    if (!$pageid) return false;
1657835dfcaeSAnika Henke
1658c786a1b6SAnika Henke    global $TOC;
16599a2e250aSAndreas Gohr    $oldtoc = $TOC;
1660a81910eeSAndreas Gohr    $html = p_wiki_xhtml($pageid, '', false);
16619a2e250aSAndreas Gohr    $TOC = $oldtoc;
1662a81910eeSAndreas Gohr
1663a2e03c82SAndreas Gohr    if ($print) echo $html;
1664e66d3e6dSAndreas Gohr    return $html;
1665e66d3e6dSAndreas Gohr}
1666e66d3e6dSAndreas Gohr
1667e66d3e6dSAndreas Gohr/**
16685b75cd1fSAdrian Lang * Display the subscribe form
16695b75cd1fSAdrian Lang *
16705b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
1671848cb786SSatoshi Sahara * @deprecated 2020-07-23
16725b75cd1fSAdrian Lang */
1673d868eb89SAndreas Gohrfunction tpl_subscribe()
1674d868eb89SAndreas Gohr{
167524870174SAndreas Gohr    dbg_deprecated(Subscribe::class . '::show()');
167673022918SAndreas Gohr    (new Subscribe())->show();
16775b75cd1fSAdrian Lang}
16785b75cd1fSAdrian Lang
1679d059ba9bSAndreas Gohr/**
1680d059ba9bSAndreas Gohr * Tries to send already created content right to the browser
1681d059ba9bSAndreas Gohr *
1682d059ba9bSAndreas Gohr * Wraps around ob_flush() and flush()
1683d059ba9bSAndreas Gohr *
1684d059ba9bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1685d059ba9bSAndreas Gohr */
1686d868eb89SAndreas Gohrfunction tpl_flush()
1687d868eb89SAndreas Gohr{
1688c2009796SZemoj    if (ob_get_level() > 0) ob_flush();
1689d059ba9bSAndreas Gohr    flush();
1690d059ba9bSAndreas Gohr}
1691d059ba9bSAndreas Gohr
1692afca7e7eSAnika Henke/**
1693378325f9SAndreas Gohr * Tries to find a ressource file in the given locations.
1694afca7e7eSAnika Henke *
1695378325f9SAndreas Gohr * If a given location starts with a colon it is assumed to be a media
1696378325f9SAndreas Gohr * file, otherwise it is assumed to be relative to the current template
1697378325f9SAndreas Gohr *
169842ea7f44SGerrit Uitslag * @param string[] $search locations to look at
1699378325f9SAndreas Gohr * @param bool $abs if to use absolute URL
1700ac7a515fSAndreas Gohr * @param array    &$imginfo filled with getimagesize()
17016dc405e1SAndreas Gohr * @param bool $fallback use fallback image if target isn't found or return 'false' if potential
17026dc405e1SAndreas Gohr *                                false result is required
1703ac7a515fSAndreas Gohr * @return string
170442ea7f44SGerrit Uitslag *
1705378325f9SAndreas Gohr * @author Andreas  Gohr <andi@splitbrain.org>
1706afca7e7eSAnika Henke */
1707d868eb89SAndreas Gohrfunction tpl_getMediaFile($search, $abs = false, &$imginfo = null, $fallback = true)
1708d868eb89SAndreas Gohr{
1709ac7a515fSAndreas Gohr    $img = '';
1710ac7a515fSAndreas Gohr    $file = '';
1711ac7a515fSAndreas Gohr    $ismedia = false;
1712378325f9SAndreas Gohr    // loop through candidates until a match was found:
1713378325f9SAndreas Gohr    foreach ($search as $img) {
1714*6c16a3a9Sfiwswe        if (str_starts_with($img, ':')) {
1715378325f9SAndreas Gohr            $file = mediaFN($img);
1716378325f9SAndreas Gohr            $ismedia = true;
1717378325f9SAndreas Gohr        } else {
1718c4766956SAndreas Gohr            $file = tpl_incdir() . $img;
1719378325f9SAndreas Gohr            $ismedia = false;
17201f13e33dSAnika Henke        }
17210f747863Slupo49
1722378325f9SAndreas Gohr        if (file_exists($file)) break;
1723872a6d29SAnika Henke    }
1724378325f9SAndreas Gohr
172508a13262SSimon DELAGE    // manage non existing target
172608a13262SSimon DELAGE    if (!file_exists($file)) {
172708a13262SSimon DELAGE        // give result for fallback image
172824870174SAndreas Gohr        if ($fallback) {
1729c238d757SSimon DELAGE            $file = DOKU_INC . 'lib/images/blank.gif';
173008a13262SSimon DELAGE            // stop process if false result is required (if $fallback is false)
173108a13262SSimon DELAGE        } else {
173208a13262SSimon DELAGE            return false;
173308a13262SSimon DELAGE        }
173408a13262SSimon DELAGE    }
1735ca5b6a64SSimon DELAGE
1736378325f9SAndreas Gohr    // fetch image data if requested
1737378325f9SAndreas Gohr    if (!is_null($imginfo)) {
1738378325f9SAndreas Gohr        $imginfo = getimagesize($file);
1739378325f9SAndreas Gohr    }
1740378325f9SAndreas Gohr
1741378325f9SAndreas Gohr    // build URL
1742378325f9SAndreas Gohr    if ($ismedia) {
1743378325f9SAndreas Gohr        $url = ml($img, '', true, '', $abs);
1744378325f9SAndreas Gohr    } else {
1745c4766956SAndreas Gohr        $url = tpl_basedir() . $img;
1746378325f9SAndreas Gohr        if ($abs) $url = DOKU_URL . substr($url, strlen(DOKU_REL));
1747378325f9SAndreas Gohr    }
1748378325f9SAndreas Gohr
1749378325f9SAndreas Gohr    return $url;
1750a7e5f74cSlupo49}
17511f13e33dSAnika Henke
1752872a6d29SAnika Henke/**
1753e5d4768dSAndreas Gohr * PHP include a file
1754e5d4768dSAndreas Gohr *
1755e5d4768dSAndreas Gohr * either from the conf directory if it exists, otherwise use
1756e5d4768dSAndreas Gohr * file in the template's root directory.
1757e5d4768dSAndreas Gohr *
1758e5d4768dSAndreas Gohr * The function honours config cascade settings and looks for the given
1759e5d4768dSAndreas Gohr * file next to the ´main´ config files, in the order protected, local,
1760e5d4768dSAndreas Gohr * default.
1761e5d4768dSAndreas Gohr *
1762e5d4768dSAndreas Gohr * Note: no escaping or sanity checking is done here. Never pass user input
1763e5d4768dSAndreas Gohr * to this function!
1764e5d4768dSAndreas Gohr *
176542ea7f44SGerrit Uitslag * @param string $file
17664dc42f7fSGerrit Uitslag *
17674dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
17684dc42f7fSGerrit Uitslag * @author Anika Henke <anika@selfthinker.org>
1769e5d4768dSAndreas Gohr */
1770d868eb89SAndreas Gohrfunction tpl_includeFile($file)
1771d868eb89SAndreas Gohr{
1772e5d4768dSAndreas Gohr    global $config_cascade;
177324870174SAndreas Gohr    foreach (['protected', 'local', 'default'] as $config_group) {
1774e5d4768dSAndreas Gohr        if (empty($config_cascade['main'][$config_group])) continue;
1775e5d4768dSAndreas Gohr        foreach ($config_cascade['main'][$config_group] as $conf_file) {
1776e5d4768dSAndreas Gohr            $dir = dirname($conf_file);
1777e5d4768dSAndreas Gohr            if (file_exists("$dir/$file")) {
1778f3a1225fSAnika Henke                include("$dir/$file");
1779e5d4768dSAndreas Gohr                return;
1780e5d4768dSAndreas Gohr            }
1781e5d4768dSAndreas Gohr        }
1782e5d4768dSAndreas Gohr    }
1783e5d4768dSAndreas Gohr
1784e5d4768dSAndreas Gohr    // still here? try the template dir
1785e5d4768dSAndreas Gohr    $file = tpl_incdir() . $file;
1786e5d4768dSAndreas Gohr    if (file_exists($file)) {
1787f3a1225fSAnika Henke        include($file);
1788e5d4768dSAndreas Gohr    }
1789e5d4768dSAndreas Gohr}
1790e5d4768dSAndreas Gohr
1791e5d4768dSAndreas Gohr/**
1792872a6d29SAnika Henke * Returns <link> tag for various icon types (favicon|mobile|generic)
1793872a6d29SAnika Henke *
1794ac7a515fSAndreas Gohr * @param array $types - list of icon types to display (favicon|mobile|generic)
1795ac7a515fSAndreas Gohr * @return string
17964dc42f7fSGerrit Uitslag *
17974dc42f7fSGerrit Uitslag * @author Anika Henke <anika@selfthinker.org>
1798872a6d29SAnika Henke */
1799d868eb89SAndreas Gohrfunction tpl_favicon($types = ['favicon'])
1800d868eb89SAndreas Gohr{
1801872a6d29SAnika Henke
1802872a6d29SAnika Henke    $return = '';
1803872a6d29SAnika Henke
1804872a6d29SAnika Henke    foreach ($types as $type) {
1805872a6d29SAnika Henke        switch ($type) {
1806872a6d29SAnika Henke            case 'favicon':
180724870174SAndreas Gohr                $look = [':wiki:favicon.ico', ':favicon.ico', 'images/favicon.ico'];
1808378325f9SAndreas Gohr                $return .= '<link rel="shortcut icon" href="' . tpl_getMediaFile($look) . '" />' . NL;
1809872a6d29SAnika Henke                break;
1810872a6d29SAnika Henke            case 'mobile':
181124870174SAndreas Gohr                $look = [':wiki:apple-touch-icon.png', ':apple-touch-icon.png', 'images/apple-touch-icon.png'];
1812378325f9SAndreas Gohr                $return .= '<link rel="apple-touch-icon" href="' . tpl_getMediaFile($look) . '" />' . NL;
1813872a6d29SAnika Henke                break;
1814872a6d29SAnika Henke            case 'generic':
1815872a6d29SAnika Henke                // ideal world solution, which doesn't work in any browser yet
181624870174SAndreas Gohr                $look = [':wiki:favicon.svg', ':favicon.svg', 'images/favicon.svg'];
1817378325f9SAndreas Gohr                $return .= '<link rel="icon" href="' . tpl_getMediaFile($look) . '" type="image/svg+xml" />' . NL;
1818872a6d29SAnika Henke                break;
1819872a6d29SAnika Henke        }
1820872a6d29SAnika Henke    }
1821872a6d29SAnika Henke
1822872a6d29SAnika Henke    return $return;
1823afca7e7eSAnika Henke}
1824afca7e7eSAnika Henke
1825d9162c6cSKate Arzamastseva/**
1826d9162c6cSKate Arzamastseva * Prints full-screen media manager
1827d9162c6cSKate Arzamastseva *
1828d9162c6cSKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net>
1829d9162c6cSKate Arzamastseva */
1830d868eb89SAndreas Gohrfunction tpl_media()
1831d868eb89SAndreas Gohr{
1832ac7a515fSAndreas Gohr    global $NS, $IMG, $JUMPTO, $REV, $lang, $fullscreen, $INPUT;
183388a71175SKate Arzamastseva    $fullscreen = true;
183495b451bcSAdrian Lang    require_once DOKU_INC . 'lib/exe/mediamanager.php';
1835d9162c6cSKate Arzamastseva
1836ac7a515fSAndreas Gohr    $rev = '';
1837ac7a515fSAndreas Gohr    $image = cleanID($INPUT->str('image'));
183898f03b57SKate Arzamastseva    if (isset($IMG)) $image = $IMG;
183998f03b57SKate Arzamastseva    if (isset($JUMPTO)) $image = $JUMPTO;
18409c1bd4bcSKate Arzamastseva    if (isset($REV) && !$JUMPTO) $rev = $REV;
184198f03b57SKate Arzamastseva
184294add303SAnika Henke    echo '<div id="mediamanager__page">' . NL;
1843bc314c58SAnika Henke    echo '<h1>' . $lang['btn_media'] . '</h1>' . NL;
1844d9162c6cSKate Arzamastseva    html_msgarea();
184594add303SAnika Henke
184694add303SAnika Henke    echo '<div class="panel namespaces">' . NL;
184794add303SAnika Henke    echo '<h2>' . $lang['namespaces'] . '</h2>' . NL;
184895b451bcSAdrian Lang    echo '<div class="panelHeader">';
1849ba340a70SAnika Henke    echo $lang['media_namespaces'];
185094add303SAnika Henke    echo '</div>' . NL;
185195b451bcSAdrian Lang
185294add303SAnika Henke    echo '<div class="panelContent" id="media__tree">' . NL;
185395b451bcSAdrian Lang    media_nstree($NS);
185494add303SAnika Henke    echo '</div>' . NL;
185594add303SAnika Henke    echo '</div>' . NL;
1856fa8e5c77SKate Arzamastseva
185794add303SAnika Henke    echo '<div class="panel filelist">' . NL;
1858035e07f1SKate Arzamastseva    tpl_mediaFileList();
185994add303SAnika Henke    echo '</div>' . NL;
1860fa8e5c77SKate Arzamastseva
186194add303SAnika Henke    echo '<div class="panel file">' . NL;
186294add303SAnika Henke    echo '<h2 class="a11y">' . $lang['media_file'] . '</h2>' . NL;
1863035e07f1SKate Arzamastseva    tpl_mediaFileDetails($image, $rev);
186494add303SAnika Henke    echo '</div>' . NL;
1865ba340a70SAnika Henke
186694add303SAnika Henke    echo '</div>' . NL;
1867d9162c6cSKate Arzamastseva}
1868afca7e7eSAnika Henke
1869c71db656SAnika Henke/**
1870c71db656SAnika Henke * Return useful layout classes
1871c71db656SAnika Henke *
187242ea7f44SGerrit Uitslag * @return string
18734dc42f7fSGerrit Uitslag *
18744dc42f7fSGerrit Uitslag * @author Anika Henke <anika@selfthinker.org>
1875c71db656SAnika Henke */
1876d868eb89SAndreas Gohrfunction tpl_classes()
1877d868eb89SAndreas Gohr{
1878c71db656SAnika Henke    global $ACT, $conf, $ID, $INFO;
1879585bf44eSChristopher Smith    /** @var Input $INPUT */
1880585bf44eSChristopher Smith    global $INPUT;
1881585bf44eSChristopher Smith
188224870174SAndreas Gohr    $classes = [
1883c71db656SAnika Henke        'dokuwiki',
1884c71db656SAnika Henke        'mode_' . $ACT,
1885c71db656SAnika Henke        'tpl_' . $conf['template'],
1886585bf44eSChristopher Smith        $INPUT->server->bool('REMOTE_USER') ? 'loggedIn' : '',
188790eb1b7bSEduardo Mozart de Oliveira        (isset($INFO['exists']) && $INFO['exists']) ? '' : 'notFound',
188824870174SAndreas Gohr        ($ID == $conf['start']) ? 'home' : ''
188924870174SAndreas Gohr    ];
189024870174SAndreas Gohr    return implode(' ', $classes);
1891c71db656SAnika Henke}
1892c71db656SAnika Henke
189384dd2b1aSGerrit Uitslag/**
189484dd2b1aSGerrit Uitslag * Create event for tools menues
189584dd2b1aSGerrit Uitslag *
189684dd2b1aSGerrit Uitslag * @param string $toolsname name of menu
189784dd2b1aSGerrit Uitslag * @param array $items
189884dd2b1aSGerrit Uitslag * @param string $view e.g. 'main', 'detail', ...
18994dc42f7fSGerrit Uitslag *
19004dc42f7fSGerrit Uitslag * @author Anika Henke <anika@selfthinker.org>
1901affc7ddfSAndreas Gohr * @deprecated 2017-09-01 see devel:menus
190284dd2b1aSGerrit Uitslag */
1903d868eb89SAndreas Gohrfunction tpl_toolsevent($toolsname, $items, $view = 'main')
1904d868eb89SAndreas Gohr{
1905affc7ddfSAndreas Gohr    dbg_deprecated('see devel:menus');
190624870174SAndreas Gohr    $data = ['view' => $view, 'items' => $items];
190784dd2b1aSGerrit Uitslag
190884dd2b1aSGerrit Uitslag    $hook = 'TEMPLATE_' . strtoupper($toolsname) . '_DISPLAY';
1909e1d9dcc8SAndreas Gohr    $evt = new Event($hook, $data);
191084dd2b1aSGerrit Uitslag    if ($evt->advise_before()) {
191124870174SAndreas Gohr        foreach ($evt->data['items'] as $html) echo $html;
191284dd2b1aSGerrit Uitslag    }
191384dd2b1aSGerrit Uitslag    $evt->advise_after();
191484dd2b1aSGerrit Uitslag}
1915