xref: /dokuwiki/inc/template.php (revision 5ed7bacec486014377d5973347d50fb7953c84ad)
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;
1536c16a3a9Sfiwswe    } 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',
272*5ed7baceSAndreas Gohr            'href' => DOKU_BASE . 'lib/exe/manifest.php',
273*5ed7baceSAndreas Gohr            'crossorigin' => 'use-credentials' // See issue #4322
27424870174SAndreas Gohr        ];
2755e0255e3SMichael Große    }
2765e0255e3SMichael Große
27724870174SAndreas Gohr    $styleUtil = new StyleUtils();
2784593dbd2SAnna Dabrowska    $styleIni = $styleUtil->cssStyleini();
27940ca8540SMichael Große    $replacements = $styleIni['replacements'];
28040ca8540SMichael Große    if (!empty($replacements['__theme_color__'])) {
28124870174SAndreas Gohr        $head['meta'][] = [
28224870174SAndreas Gohr            'name' => 'theme-color',
28324870174SAndreas Gohr            'content' => $replacements['__theme_color__']
28424870174SAndreas Gohr        ];
28540ca8540SMichael Große    }
28640ca8540SMichael Große
287f96fa415SAndreas Gohr    if ($alt) {
28854be1338SGerrit Uitslag        if (actionOK('rss')) {
28924870174SAndreas Gohr            $head['link'][] = [
29024870174SAndreas Gohr                'rel' => 'alternate',
29124870174SAndreas Gohr                'type' => 'application/rss+xml',
29224870174SAndreas Gohr                'title' => $lang['btn_recent'],
29324870174SAndreas Gohr                'href' => DOKU_BASE . 'feed.php'
29424870174SAndreas Gohr            ];
29524870174SAndreas Gohr            $head['link'][] = [
29624870174SAndreas Gohr                'rel' => 'alternate',
29724870174SAndreas Gohr                'type' => 'application/rss+xml',
298a1288caeSGerrit Uitslag                'title' => $lang['currentns'],
299aac83cd4SPhy                'href' => DOKU_BASE . 'feed.php?mode=list&ns=' . (isset($INFO) ? $INFO['namespace'] : '')
30024870174SAndreas Gohr            ];
30154be1338SGerrit Uitslag        }
302c35f3875SAndreas Gohr        if (($ACT == 'show' || $ACT == 'search') && $INFO['writable']) {
30324870174SAndreas Gohr            $head['link'][] = [
304ac7a515fSAndreas Gohr                'rel' => 'edit',
305715bdf1fSAndreas Gohr                'title' => $lang['btn_edit'],
306ac7a515fSAndreas Gohr                'href' => wl($ID, 'do=edit', false, '&')
30724870174SAndreas Gohr            ];
308c35f3875SAndreas Gohr        }
309c35f3875SAndreas Gohr
31054be1338SGerrit Uitslag        if (actionOK('rss') && $ACT == 'search') {
31124870174SAndreas Gohr            $head['link'][] = [
31224870174SAndreas Gohr                'rel' => 'alternate',
31324870174SAndreas Gohr                'type' => 'application/rss+xml',
314a1288caeSGerrit Uitslag                'title' => $lang['searchresult'],
315ac7a515fSAndreas Gohr                'href' => DOKU_BASE . 'feed.php?mode=search&q=' . $QUERY
31624870174SAndreas Gohr            ];
3174bb1b5aeSAndreas Gohr        }
318bae36d94SAndreas Gohr
319bae36d94SAndreas Gohr        if (actionOK('export_xhtml')) {
32024870174SAndreas Gohr            $head['link'][] = [
32124870174SAndreas Gohr                'rel' => 'alternate',
32224870174SAndreas Gohr                'type' => 'text/html',
32324870174SAndreas Gohr                'title' => $lang['plainhtml'],
324ac7a515fSAndreas Gohr                'href' => exportlink($ID, 'xhtml', '', false, '&')
32524870174SAndreas Gohr            ];
326bae36d94SAndreas Gohr        }
327bae36d94SAndreas Gohr
328bae36d94SAndreas Gohr        if (actionOK('export_raw')) {
32924870174SAndreas Gohr            $head['link'][] = [
33024870174SAndreas Gohr                'rel' => 'alternate',
33124870174SAndreas Gohr                'type' => 'text/plain',
33224870174SAndreas Gohr                'title' => $lang['wikimarkup'],
333ac7a515fSAndreas Gohr                'href' => exportlink($ID, 'raw', '', false, '&')
33424870174SAndreas Gohr            ];
335f96fa415SAndreas Gohr        }
336bae36d94SAndreas Gohr    }
3376b13307fSandi
33863f13cadSDamien Regad    // setup robot tags appropriate for different modes
3394f2e0004STim Weber    if (($ACT == 'show' || $ACT == 'export_xhtml') && !$REV) {
3406b13307fSandi        if ($INFO['exists']) {
3416b13307fSandi            //delay indexing:
342fb9fa88bSAndreas Gohr            if ((time() - $INFO['lastmod']) >= $conf['indexdelay'] && !isHiddenPage($ID)) {
34324870174SAndreas Gohr                $head['meta'][] = ['name' => 'robots', 'content' => 'index,follow'];
3446b13307fSandi            } else {
34524870174SAndreas Gohr                $head['meta'][] = ['name' => 'robots', 'content' => 'noindex,nofollow'];
3466b13307fSandi            }
34701f9be51SAnika Henke            $canonicalUrl = wl($ID, '', true, '&');
34801f9be51SAnika Henke            if ($ID == $conf['start']) {
34901f9be51SAnika Henke                $canonicalUrl = DOKU_URL;
35001f9be51SAnika Henke            }
35124870174SAndreas Gohr            $head['link'][] = ['rel' => 'canonical', 'href' => $canonicalUrl];
3526b13307fSandi        } else {
35324870174SAndreas Gohr            $head['meta'][] = ['name' => 'robots', 'content' => 'noindex,follow'];
3546b13307fSandi        }
3557a24876fSAndreas Gohr    } elseif (defined('DOKU_MEDIADETAIL')) {
35624870174SAndreas Gohr        $head['meta'][] = ['name' => 'robots', 'content' => 'index,follow'];
3576b13307fSandi    } else {
35824870174SAndreas Gohr        $head['meta'][] = ['name' => 'robots', 'content' => 'noindex,nofollow'];
3596b13307fSandi    }
3606b13307fSandi
361831800b8SAndreas Gohr    // set metadata
362831800b8SAndreas Gohr    if ($ACT == 'show' || $ACT == 'export_xhtml') {
363831800b8SAndreas Gohr        // keywords (explicit or implicit)
364bb4866bdSchris        if (!empty($INFO['meta']['subject'])) {
36524870174SAndreas Gohr            $head['meta'][] = ['name' => 'keywords', 'content' => implode(',', $INFO['meta']['subject'])];
366831800b8SAndreas Gohr        } else {
36724870174SAndreas Gohr            $head['meta'][] = ['name' => 'keywords', 'content' => str_replace(':', ',', $ID)];
368831800b8SAndreas Gohr        }
369831800b8SAndreas Gohr    }
370831800b8SAndreas Gohr
37178a6aeb1SAndreas Gohr    // load stylesheets
37224870174SAndreas Gohr    $head['link'][] = [
37359305168SPhy        'rel' => 'stylesheet',
374e283bd6cSAnika Henke        'href' => DOKU_BASE . 'lib/exe/css.php?t=' . rawurlencode($conf['template']) . '&tseed=' . $tseed
37524870174SAndreas Gohr    ];
376bad31ae9SAndreas Gohr
377aac83cd4SPhy    $script = "var NS='" . (isset($INFO) ? $INFO['namespace'] : '') . "';";
378585bf44eSChristopher Smith    if ($conf['useacl'] && $INPUT->server->str('REMOTE_USER')) {
37998169a0fSAndreas Gohr        $script .= "var SIG=" . toolbar_signature() . ";";
380c591aabeSAndreas Gohr    }
3810c39d46cSMichael Große    jsinfo();
38224870174SAndreas Gohr    $script .= 'var JSINFO = ' . json_encode($JSINFO, JSON_THROW_ON_ERROR) . ';';
383e0aa6775SAndreas Gohr    $script .= '(function(H){H.className=H.className.replace(/\bno-js\b/,\'js\')})(document.documentElement);';
38424870174SAndreas Gohr    $head['script'][] = ['_data' => $script];
3858bbcb611SAndreas Gohr
38661537d47SAndreas Gohr    // load jquery
387fa078663SAndreas Gohr    $jquery = getCdnUrls();
388fa078663SAndreas Gohr    foreach ($jquery as $src) {
38924870174SAndreas Gohr        $head['script'][] = [
390fc6b11d2SMichael Große                '_data' => '',
39124870174SAndreas Gohr                'src' => $src
39224870174SAndreas Gohr            ] + ($conf['defer_js'] ? ['defer' => 'defer'] : []);
39361537d47SAndreas Gohr    }
39461537d47SAndreas Gohr
39561537d47SAndreas Gohr    // load our javascript dispatcher
39624870174SAndreas Gohr    $head['script'][] = [
397de1dc35bSNicolas Friedli            '_data' => '',
39824870174SAndreas Gohr            'src' => DOKU_BASE . 'lib/exe/js.php' . '?t=' . rawurlencode($conf['template']) . '&tseed=' . $tseed
39924870174SAndreas Gohr        ] + ($conf['defer_js'] ? ['defer' => 'defer'] : []);
4007bff22c0SAndreas Gohr
4017bff22c0SAndreas Gohr    // trigger event here
402cbb44eabSAndreas Gohr    Event::createAndTrigger('TPL_METAHEADER_OUTPUT', $head, '_tpl_metaheaders_action', true);
40354e95700STom N Harris    return true;
4047bff22c0SAndreas Gohr}
4057bff22c0SAndreas Gohr
4067bff22c0SAndreas Gohr/**
4077bff22c0SAndreas Gohr * prints the array build by tpl_metaheaders
4087bff22c0SAndreas Gohr *
4097bff22c0SAndreas Gohr * $data is an array of different header tags. Each tag can have multiple
4107bff22c0SAndreas Gohr * instances. Attributes are given as key value pairs. Values will be HTML
4117bff22c0SAndreas Gohr * encoded automatically so they should be provided as is in the $data array.
4127bff22c0SAndreas Gohr *
41342ea7f44SGerrit Uitslag * For tags having a body attribute specify the body data in the special
4141304d1dbSAndreas Gohr * attribute '_data'. This field will NOT BE ESCAPED automatically.
4157bff22c0SAndreas Gohr *
416e5d413b0SAndreas Gohr * Inline scripts will use any nonce provided in the environment variable 'NONCE'.
417e5d413b0SAndreas Gohr *
41842ea7f44SGerrit Uitslag * @param array $data
4194dc42f7fSGerrit Uitslag *
4204dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
4217bff22c0SAndreas Gohr */
422d868eb89SAndreas Gohrfunction _tpl_metaheaders_action($data)
423d868eb89SAndreas Gohr{
424e5d413b0SAndreas Gohr    $nonce = getenv('NONCE');
4257bff22c0SAndreas Gohr    foreach ($data as $tag => $inst) {
4267bff22c0SAndreas Gohr        foreach ($inst as $attr) {
427177d6836SAndreas Gohr            if (empty($attr)) {
428d4f83172SAndreas Gohr                continue;
429d4f83172SAndreas Gohr            }
430e5d413b0SAndreas Gohr            if ($nonce && $tag == 'script' && !empty($attr['_data'])) {
431e5d413b0SAndreas Gohr                $attr['nonce'] = $nonce; // add nonce to inline script tags
432e5d413b0SAndreas Gohr            }
4337bff22c0SAndreas Gohr            echo '<', $tag, ' ', buildAttributes($attr);
43426afa874SMikhail I. Izmestev            if (isset($attr['_data']) || $tag == 'script') {
43524870174SAndreas Gohr                echo '>', $attr['_data'] ?? '', '</', $tag, '>';
4367bff22c0SAndreas Gohr            } else {
4377bff22c0SAndreas Gohr                echo '/>';
4387bff22c0SAndreas Gohr            }
4397bff22c0SAndreas Gohr            echo "\n";
4407bff22c0SAndreas Gohr        }
4417bff22c0SAndreas Gohr    }
4426b13307fSandi}
4436b13307fSandi
4446b13307fSandi/**
445a77ab274SAndreas Gohr * Output the given script as inline script tag
446a77ab274SAndreas Gohr *
447a77ab274SAndreas Gohr * This function will add the nonce attribute if a nonce is available.
448a77ab274SAndreas Gohr *
449a77ab274SAndreas Gohr * The script is NOT automatically escaped!
450a77ab274SAndreas Gohr *
451a77ab274SAndreas Gohr * @param string $script
452a77ab274SAndreas Gohr * @param bool $return Return or print directly?
453a77ab274SAndreas Gohr * @return string|void
454a77ab274SAndreas Gohr */
455a77ab274SAndreas Gohrfunction tpl_inlineScript($script, $return = false)
456a77ab274SAndreas Gohr{
457a77ab274SAndreas Gohr    $nonce = getenv('NONCE');
458a77ab274SAndreas Gohr    if ($nonce) {
459a77ab274SAndreas Gohr        $script = '<script nonce="' . $nonce . '">' . $script . '</script>';
460a77ab274SAndreas Gohr    } else {
461a77ab274SAndreas Gohr        $script = '<script>' . $script . '</script>';
462a77ab274SAndreas Gohr    }
463a77ab274SAndreas Gohr
464a77ab274SAndreas Gohr    if ($return) return $script;
465a77ab274SAndreas Gohr    echo $script;
466a77ab274SAndreas Gohr}
467a77ab274SAndreas Gohr
468a77ab274SAndreas Gohr/**
4696b13307fSandi * Print a link
4706b13307fSandi *
4715e163278SAndreas Gohr * Just builds a link.
4726b13307fSandi *
47342ea7f44SGerrit Uitslag * @param string $url
47442ea7f44SGerrit Uitslag * @param string $name
47542ea7f44SGerrit Uitslag * @param string $more
47621d806cdSGerrit Uitslag * @param bool $return if true return the link html, otherwise print
47721d806cdSGerrit Uitslag * @return bool|string html of the link, or true if printed
4784dc42f7fSGerrit Uitslag *
4794dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
4806b13307fSandi */
481d868eb89SAndreas Gohrfunction tpl_link($url, $name, $more = '', $return = false)
482d868eb89SAndreas Gohr{
48301f17825SAnika Henke    $out = '<a href="' . $url . '" ';
4841af98a77SAnika Henke    if ($more) $out .= ' ' . $more;
4851af98a77SAnika Henke    $out .= ">$name</a>";
4861af98a77SAnika Henke    if ($return) return $out;
48726dfc232SAndreas Gohr    echo $out;
48854e95700STom N Harris    return true;
4896b13307fSandi}
4906b13307fSandi
4916b13307fSandi/**
49255efc227SAndreas Gohr * Prints a link to a WikiPage
49355efc227SAndreas Gohr *
49455efc227SAndreas Gohr * Wrapper around html_wikilink
49555efc227SAndreas Gohr *
49642ea7f44SGerrit Uitslag * @param string $id page id
49742ea7f44SGerrit Uitslag * @param string|null $name the name of the link
498fb008d31SIain Hallam * @param bool $return
499fb008d31SIain Hallam * @return true|string
5004dc42f7fSGerrit Uitslag *
5014dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
50255efc227SAndreas Gohr */
503d868eb89SAndreas Gohrfunction tpl_pagelink($id, $name = null, $return = false)
504d868eb89SAndreas Gohr{
505c4a386f1SIain Hallam    $out = '<bdi>' . html_wikilink($id, $name) . '</bdi>';
506c4a386f1SIain Hallam    if ($return) return $out;
50726dfc232SAndreas Gohr    echo $out;
50854e95700STom N Harris    return true;
50955efc227SAndreas Gohr}
51055efc227SAndreas Gohr
51155efc227SAndreas Gohr/**
512a3ec5f4aSmatthiasgrimm * get the parent page
513a3ec5f4aSmatthiasgrimm *
514a3ec5f4aSmatthiasgrimm * Tries to find out which page is parent.
515a3ec5f4aSmatthiasgrimm * returns false if none is available
516a3ec5f4aSmatthiasgrimm *
51742ea7f44SGerrit Uitslag * @param string $id page id
51842ea7f44SGerrit Uitslag * @return false|string
5194dc42f7fSGerrit Uitslag *
5204dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
521a3ec5f4aSmatthiasgrimm */
522d868eb89SAndreas Gohrfunction tpl_getparent($id)
523d868eb89SAndreas Gohr{
5248c6be208SAndreas Gohr    $resolver = new PageResolver('root');
5258c6be208SAndreas Gohr
526377f9e97SAndreas Gohr    $parent = getNS($id) . ':';
5278c6be208SAndreas Gohr    $parent = $resolver->resolveId($parent);
528a197105eSmatthiasgrimm    if ($parent == $id) {
529a197105eSmatthiasgrimm        $pos = strrpos(getNS($id), ':');
530a197105eSmatthiasgrimm        $parent = substr($parent, 0, $pos) . ':';
5318c6be208SAndreas Gohr        $parent = $resolver->resolveId($parent);
532377f9e97SAndreas Gohr        if ($parent == $id) return false;
533a197105eSmatthiasgrimm    }
534377f9e97SAndreas Gohr    return $parent;
535a3ec5f4aSmatthiasgrimm}
536a3ec5f4aSmatthiasgrimm
537a3ec5f4aSmatthiasgrimm/**
5386b13307fSandi * Print one of the buttons
5396b13307fSandi *
540e0c26282SGerrit Uitslag * @param string $type
541e0c26282SGerrit Uitslag * @param bool $return
542e0c26282SGerrit Uitslag * @return bool|string html, or false if no data, true if printed
5434dc42f7fSGerrit Uitslag * @see    tpl_get_action
5444dc42f7fSGerrit Uitslag *
5454dc42f7fSGerrit Uitslag * @author Adrian Lang <mail@adrianlang.de>
546affc7ddfSAndreas Gohr * @deprecated 2017-09-01 see devel:menus
5476b13307fSandi */
548d868eb89SAndreas Gohrfunction tpl_button($type, $return = false)
549d868eb89SAndreas Gohr{
550affc7ddfSAndreas Gohr    dbg_deprecated('see devel:menus');
551a453d131SAdrian Lang    $data = tpl_get_action($type);
552a453d131SAdrian Lang    if ($data === false) {
553a453d131SAdrian Lang        return false;
554a453d131SAdrian Lang    } elseif (!is_array($data)) {
555a453d131SAdrian Lang        $out = sprintf($data, 'button');
556409d7af7SAndreas Gohr    } else {
557ac7a515fSAndreas Gohr        /**
558ac7a515fSAndreas Gohr         * @var string $accesskey
559ac7a515fSAndreas Gohr         * @var string $id
560ac7a515fSAndreas Gohr         * @var string $method
561ac7a515fSAndreas Gohr         * @var array $params
562ac7a515fSAndreas Gohr         */
563a453d131SAdrian Lang        extract($data);
564a453d131SAdrian Lang        if ($id === '#dokuwiki__top') {
565a453d131SAdrian Lang            $out = html_topbtn();
566409d7af7SAndreas Gohr        } else {
567a453d131SAdrian Lang            $out = html_btn($type, $id, $accesskey, $params, $method);
568409d7af7SAndreas Gohr        }
569409d7af7SAndreas Gohr    }
5701af98a77SAnika Henke    if ($return) return $out;
571a453d131SAdrian Lang    echo $out;
572a453d131SAdrian Lang    return true;
5736b13307fSandi}
5746b13307fSandi
5756b13307fSandi/**
576ed630903Sandi * Like the action buttons but links
577ed630903Sandi *
57842ea7f44SGerrit Uitslag * @param string $type action command
579e0c26282SGerrit Uitslag * @param string $pre prefix of link
580e0c26282SGerrit Uitslag * @param string $suf suffix of link
581e0c26282SGerrit Uitslag * @param string $inner innerHML of link
58221d806cdSGerrit Uitslag * @param bool $return if true it returns html, otherwise prints
583e0c26282SGerrit Uitslag * @return bool|string html or false if no data, true if printed
5844dc42f7fSGerrit Uitslag *
5854dc42f7fSGerrit Uitslag * @see    tpl_get_action
5864dc42f7fSGerrit Uitslag * @author Adrian Lang <mail@adrianlang.de>
587affc7ddfSAndreas Gohr * @deprecated 2017-09-01 see devel:menus
588a453d131SAdrian Lang */
589d868eb89SAndreas Gohrfunction tpl_actionlink($type, $pre = '', $suf = '', $inner = '', $return = false)
590d868eb89SAndreas Gohr{
591affc7ddfSAndreas Gohr    dbg_deprecated('see devel:menus');
592a453d131SAdrian Lang    global $lang;
593a453d131SAdrian Lang    $data = tpl_get_action($type);
594a453d131SAdrian Lang    if ($data === false) {
595a453d131SAdrian Lang        return false;
596a453d131SAdrian Lang    } elseif (!is_array($data)) {
597a453d131SAdrian Lang        $out = sprintf($data, 'link');
598a453d131SAdrian Lang    } else {
599ac7a515fSAndreas Gohr        /**
600ac7a515fSAndreas Gohr         * @var string $accesskey
601ac7a515fSAndreas Gohr         * @var string $id
602ac7a515fSAndreas Gohr         * @var string $method
603b1af9014SChristopher Smith         * @var bool $nofollow
604ac7a515fSAndreas Gohr         * @var array $params
605becfa414SGerrit Uitslag         * @var string $replacement
606ac7a515fSAndreas Gohr         */
607a453d131SAdrian Lang        extract($data);
608a453d131SAdrian Lang        if (strpos($id, '#') === 0) {
609a453d131SAdrian Lang            $linktarget = $id;
610a453d131SAdrian Lang        } else {
611a453d131SAdrian Lang            $linktarget = wl($id, $params);
612a453d131SAdrian Lang        }
613a453d131SAdrian Lang        $caption = $lang['btn_' . $type];
614becfa414SGerrit Uitslag        if (strpos($caption, '%s')) {
615becfa414SGerrit Uitslag            $caption = sprintf($caption, $replacement);
616becfa414SGerrit Uitslag        }
61724870174SAndreas Gohr        $akey = '';
61824870174SAndreas Gohr        $addTitle = '';
619c7e90e3fSAnika Henke        if ($accesskey) {
620c7e90e3fSAnika Henke            $akey = 'accesskey="' . $accesskey . '" ';
621c7e90e3fSAnika Henke            $addTitle = ' [' . strtoupper($accesskey) . ']';
622c7e90e3fSAnika Henke        }
623b1af9014SChristopher Smith        $rel = $nofollow ? 'rel="nofollow" ' : '';
624ac7a515fSAndreas Gohr        $out = tpl_link(
625dccd6b2bSAndreas Gohr            $linktarget,
626dccd6b2bSAndreas Gohr            $pre . ($inner ?: $caption) . $suf,
627a453d131SAdrian Lang            'class="action ' . $type . '" ' .
628b1af9014SChristopher Smith            $akey . $rel .
629dccd6b2bSAndreas Gohr            'title="' . hsc($caption) . $addTitle . '"',
630dccd6b2bSAndreas Gohr            true
631ac7a515fSAndreas Gohr        );
632a453d131SAdrian Lang    }
633a453d131SAdrian Lang    if ($return) return $out;
634a453d131SAdrian Lang    echo $out;
635a453d131SAdrian Lang    return true;
636a453d131SAdrian Lang}
637a453d131SAdrian Lang
638a453d131SAdrian Lang/**
639a453d131SAdrian Lang * Check the actions and get data for buttons and links
640ed630903Sandi *
641ac7a515fSAndreas Gohr * @param string $type
642ac7a515fSAndreas Gohr * @return array|bool|string
6434dc42f7fSGerrit Uitslag *
6444dc42f7fSGerrit Uitslag * @author Adrian Lang <mail@adrianlang.de>
6454dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
6464dc42f7fSGerrit Uitslag * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
647affc7ddfSAndreas Gohr * @deprecated 2017-09-01 see devel:menus
648ed630903Sandi */
649d868eb89SAndreas Gohrfunction tpl_get_action($type)
650d868eb89SAndreas Gohr{
651affc7ddfSAndreas Gohr    dbg_deprecated('see devel:menus');
652a453d131SAdrian Lang    if ($type == 'history') $type = 'revisions';
6534c4b65c8SMichael Hamann    if ($type == 'subscription') $type = 'subscribe';
6544887c154SAndreas Gohr    if ($type == 'img_backto') $type = 'imgBackto';
655409d7af7SAndreas Gohr
6564887c154SAndreas Gohr    $class = '\\dokuwiki\\Menu\\Item\\' . ucfirst($type);
6574887c154SAndreas Gohr    if (class_exists($class)) {
6584887c154SAndreas Gohr        try {
65924870174SAndreas Gohr            /** @var AbstractItem $item */
66073022918SAndreas Gohr            $item = new $class();
6614887c154SAndreas Gohr            $data = $item->getLegacyData();
6627b4365a7SGerrit Uitslag            $unknown = false;
6634dc42f7fSGerrit Uitslag        } catch (RuntimeException $ignored) {
6644887c154SAndreas Gohr            return false;
665b8a111f5SMichael Klier        }
666ed630903Sandi    } else {
6674887c154SAndreas Gohr        global $ID;
66824870174SAndreas Gohr        $data = [
6694887c154SAndreas Gohr            'accesskey' => null,
6704887c154SAndreas Gohr            'type' => $type,
6714887c154SAndreas Gohr            'id' => $ID,
6724887c154SAndreas Gohr            'method' => 'get',
67324870174SAndreas Gohr            'params' => ['do' => $type],
6744887c154SAndreas Gohr            'nofollow' => true,
67524870174SAndreas Gohr            'replacement' => ''
67624870174SAndreas Gohr        ];
6777b4365a7SGerrit Uitslag        $unknown = true;
678ed630903Sandi    }
6797b4365a7SGerrit Uitslag
680e1d9dcc8SAndreas Gohr    $evt = new Event('TPL_ACTION_GET', $data);
6817b4365a7SGerrit Uitslag    if ($evt->advise_before()) {
6827b4365a7SGerrit Uitslag        //handle unknown types
6837b4365a7SGerrit Uitslag        if ($unknown) {
68438d2ca46SGerrit Uitslag            $data = '[unknown %s type]';
6857b4365a7SGerrit Uitslag        }
6867b4365a7SGerrit Uitslag    }
6877b4365a7SGerrit Uitslag    $evt->advise_after();
6887b4365a7SGerrit Uitslag    unset($evt);
6897b4365a7SGerrit Uitslag
6907b4365a7SGerrit Uitslag    return $data;
691ed630903Sandi}
692ed630903Sandi
693ed630903Sandi/**
69401f17825SAnika Henke * Wrapper around tpl_button() and tpl_actionlink()
69501f17825SAnika Henke *
69642ea7f44SGerrit Uitslag * @param string $type action command
697ac7a515fSAndreas Gohr * @param bool $link link or form button?
698e0c26282SGerrit Uitslag * @param string|bool $wrapper HTML element wrapper
699ac7a515fSAndreas Gohr * @param bool $return return or print
700ac7a515fSAndreas Gohr * @param string $pre prefix for links
701ac7a515fSAndreas Gohr * @param string $suf suffix for links
702ac7a515fSAndreas Gohr * @param string $inner inner HTML for links
703ac7a515fSAndreas Gohr * @return bool|string
7044dc42f7fSGerrit Uitslag *
7054dc42f7fSGerrit Uitslag * @author Anika Henke <anika@selfthinker.org>
706affc7ddfSAndreas Gohr * @deprecated 2017-09-01 see devel:menus
70701f17825SAnika Henke */
708d868eb89SAndreas Gohrfunction tpl_action($type, $link = false, $wrapper = false, $return = false, $pre = '', $suf = '', $inner = '')
709d868eb89SAndreas Gohr{
710affc7ddfSAndreas Gohr    dbg_deprecated('see devel:menus');
71101f17825SAnika Henke    $out = '';
712ac7a515fSAndreas Gohr    if ($link) {
713e0c26282SGerrit Uitslag        $out .= tpl_actionlink($type, $pre, $suf, $inner, true);
714ac7a515fSAndreas Gohr    } else {
715e0c26282SGerrit Uitslag        $out .= tpl_button($type, true);
716ac7a515fSAndreas Gohr    }
71701f17825SAnika Henke    if ($out && $wrapper) $out = "<$wrapper>$out</$wrapper>";
71801f17825SAnika Henke
71901f17825SAnika Henke    if ($return) return $out;
72026dfc232SAndreas Gohr    echo $out;
72124870174SAndreas Gohr    return (bool)$out;
72201f17825SAnika Henke}
72301f17825SAnika Henke
72401f17825SAnika Henke/**
7256b13307fSandi * Print the search form
7266b13307fSandi *
72772645b75SAndreas Gohr * If the first parameter is given a div with the ID 'qsearch_out' will
72872645b75SAndreas Gohr * be added which instructs the ajax pagequicksearch to kick in and place
72972645b75SAndreas Gohr * its output into this div. The second parameter controls the propritary
73072645b75SAndreas Gohr * attribute autocomplete. If set to false this attribute will be set with an
73172645b75SAndreas Gohr * value of "off" to instruct the browser to disable it's own built in
73272645b75SAndreas Gohr * autocompletion feature (MSIE and Firefox)
73372645b75SAndreas Gohr *
734ac7a515fSAndreas Gohr * @param bool $ajax
735ac7a515fSAndreas Gohr * @param bool $autocomplete
736ac7a515fSAndreas Gohr * @return bool
7374dc42f7fSGerrit Uitslag *
7384dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
7396b13307fSandi */
740d868eb89SAndreas Gohrfunction tpl_searchform($ajax = true, $autocomplete = true)
741d868eb89SAndreas Gohr{
7426b13307fSandi    global $lang;
743c1e3b7d9Smatthiasgrimm    global $ACT;
744ad4aaef7SAndreas Gohr    global $QUERY;
745cbcc2fa5SMichael Große    global $ID;
746c1e3b7d9Smatthiasgrimm
747670ff54eSchris    // don't print the search form if search action has been disabled
74864276bbcSarbrk1    if (!actionOK('search')) return false;
749670ff54eSchris
75024870174SAndreas Gohr    $searchForm = new Form([
7513c7a3327SMichael Große        'action' => wl(),
7523c7a3327SMichael Große        'method' => 'get',
7533c7a3327SMichael Große        'role' => 'search',
7543c7a3327SMichael Große        'class' => 'search',
7553c7a3327SMichael Große        'id' => 'dw__search',
7567fa270bcSMichael Große    ], true);
7573eb2b869SMichael Große    $searchForm->addTagOpen('div')->addClass('no');
7583c7a3327SMichael Große    $searchForm->setHiddenField('do', 'search');
759d22b78c8SMichael Große    $searchForm->setHiddenField('id', $ID);
760d22b78c8SMichael Große    $searchForm->addTextInput('q')
7613c7a3327SMichael Große        ->addClass('edit')
7623c7a3327SMichael Große        ->attrs([
7633c7a3327SMichael Große            'title' => '[F]',
7643c7a3327SMichael Große            'accesskey' => 'f',
7653c7a3327SMichael Große            'placeholder' => $lang['btn_search'],
7663c7a3327SMichael Große            'autocomplete' => $autocomplete ? 'on' : 'off',
7673c7a3327SMichael Große        ])
7683c7a3327SMichael Große        ->id('qsearch__in')
7693c7a3327SMichael Große        ->val($ACT === 'search' ? $QUERY : '')
7704dc42f7fSGerrit Uitslag        ->useInput(false);
7713c7a3327SMichael Große    $searchForm->addButton('', $lang['btn_search'])->attrs([
7723c7a3327SMichael Große        'type' => 'submit',
7733c7a3327SMichael Große        'title' => $lang['btn_search'],
7743c7a3327SMichael Große    ]);
7753c7a3327SMichael Große    if ($ajax) {
7763c7a3327SMichael Große        $searchForm->addTagOpen('div')->id('qsearch__out')->addClass('ajax_qsearch JSpopup');
7773c7a3327SMichael Große        $searchForm->addTagClose('div');
7783c7a3327SMichael Große    }
7793eb2b869SMichael Große    $searchForm->addTagClose('div');
7803c7a3327SMichael Große
781c6977b3aSSatoshi Sahara    echo $searchForm->toHTML('QuickSearch');
7823c7a3327SMichael Große
78354e95700STom N Harris    return true;
7846b13307fSandi}
7856b13307fSandi
7866b13307fSandi/**
7876b13307fSandi * Print the breadcrumbs trace
7886b13307fSandi *
789ac7a515fSAndreas Gohr * @param string $sep Separator between entries
790c4a386f1SIain Hallam * @param bool $return return or print
791c4a386f1SIain Hallam * @return bool|string
7924dc42f7fSGerrit Uitslag *
7934dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
7946b13307fSandi */
795d868eb89SAndreas Gohrfunction tpl_breadcrumbs($sep = null, $return = false)
796d868eb89SAndreas Gohr{
7976b13307fSandi    global $lang;
7986b13307fSandi    global $conf;
7996b13307fSandi
8006b13307fSandi    //check if enabled
801359fab8bSMichael Hamann    if (!$conf['breadcrumbs']) return false;
8026b13307fSandi
803c4a386f1SIain Hallam    //set default
804c4a386f1SIain Hallam    if (is_null($sep)) $sep = '•';
805c4a386f1SIain Hallam
806c4a386f1SIain Hallam    $out = '';
807c4a386f1SIain Hallam
8086b13307fSandi    $crumbs = breadcrumbs(); //setup crumb trace
809265e3787Sandi
8102979a10bSKatriel Traum    $crumbs_sep = ' <span class="bcsep">' . $sep . '</span> ';
811265e3787Sandi
81240eb54bbSjan    //render crumbs, highlight the last one
813c4a386f1SIain Hallam    $out .= '<span class="bchead">' . $lang['breadcrumb'] . '</span>';
81440eb54bbSjan    $last = count($crumbs);
81540eb54bbSjan    $i = 0;
816a77f5846Sjan    foreach ($crumbs as $id => $name) {
81740eb54bbSjan        $i++;
818c4a386f1SIain Hallam        $out .= $crumbs_sep;
819c4a386f1SIain Hallam        if ($i == $last) $out .= '<span class="curid">';
820c4a386f1SIain Hallam        $out .= '<bdi>' . tpl_link(wl($id), hsc($name), 'class="breadcrumbs" title="' . $id . '"', true) . '</bdi>';
821c4a386f1SIain Hallam        if ($i == $last) $out .= '</span>';
8226b13307fSandi    }
823c4a386f1SIain Hallam    if ($return) return $out;
82426dfc232SAndreas Gohr    echo $out;
82524870174SAndreas Gohr    return (bool)$out;
8266b13307fSandi}
8276b13307fSandi
8286b13307fSandi/**
8291734437eSandi * Hierarchical breadcrumbs
8301734437eSandi *
83131e187f8SSean Coates * This code was suggested as replacement for the usual breadcrumbs.
8321734437eSandi * It only makes sense with a deep site structure.
8331734437eSandi *
834ac7a515fSAndreas Gohr * @param string $sep Separator between entries
835c4a386f1SIain Hallam * @param bool $return return or print
836c4a386f1SIain Hallam * @return bool|string
8374dc42f7fSGerrit Uitslag *
8384dc42f7fSGerrit Uitslag * @todo   May behave strangely in RTL languages
8394dc42f7fSGerrit Uitslag * @author <fredrik@averpil.com>
8404dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
8414dc42f7fSGerrit Uitslag * @author Nigel McNie <oracle.shinoda@gmail.com>
8424dc42f7fSGerrit Uitslag * @author Sean Coates <sean@caedmon.net>
8431734437eSandi */
844d868eb89SAndreas Gohrfunction tpl_youarehere($sep = null, $return = false)
845d868eb89SAndreas Gohr{
8461734437eSandi    global $conf;
8471734437eSandi    global $ID;
8481734437eSandi    global $lang;
8491734437eSandi
85031e187f8SSean Coates    // check if enabled
85154e95700STom N Harris    if (!$conf['youarehere']) return false;
8521734437eSandi
853c4a386f1SIain Hallam    //set default
854c4a386f1SIain Hallam    if (is_null($sep)) $sep = ' » ';
855c4a386f1SIain Hallam
856c4a386f1SIain Hallam    $out = '';
857c4a386f1SIain Hallam
8581734437eSandi    $parts = explode(':', $ID);
859796bafb3SAndreas Gohr    $count = count($parts);
8601734437eSandi
861c4a386f1SIain Hallam    $out .= '<span class="bchead">' . $lang['youarehere'] . ' </span>';
8623940c519SMark
86308d7a575SAndreas Gohr    // always print the startpage
864c4a386f1SIain Hallam    $out .= '<span class="home">' . tpl_pagelink(':' . $conf['start'], null, true) . '</span>';
865796bafb3SAndreas Gohr
866796bafb3SAndreas Gohr    // print intermediate namespace links
867796bafb3SAndreas Gohr    $part = '';
868796bafb3SAndreas Gohr    for ($i = 0; $i < $count - 1; $i++) {
869796bafb3SAndreas Gohr        $part .= $parts[$i] . ':';
870796bafb3SAndreas Gohr        $page = $part;
871796bafb3SAndreas Gohr        if ($page == $conf['start']) continue; // Skip startpage
872796bafb3SAndreas Gohr
87308d7a575SAndreas Gohr        // output
874c4a386f1SIain Hallam        $out .= $sep . tpl_pagelink($page, null, true);
87531e187f8SSean Coates    }
8761734437eSandi
877796bafb3SAndreas Gohr    // print current page, skipping start page, skipping for namespace index
8788c6be208SAndreas Gohr    if (isset($page)) {
8798c6be208SAndreas Gohr        $page = (new PageResolver('root'))->resolveId($page);
8808c6be208SAndreas Gohr        if ($page == $part . $parts[$i]) {
881a8c33dedSMichael Große            if ($return) return $out;
88226dfc232SAndreas Gohr            echo $out;
883a8c33dedSMichael Große            return true;
884a8c33dedSMichael Große        }
8858c6be208SAndreas Gohr    }
886796bafb3SAndreas Gohr    $page = $part . $parts[$i];
887a8c33dedSMichael Große    if ($page == $conf['start']) {
888a8c33dedSMichael Große        if ($return) return $out;
88926dfc232SAndreas Gohr        echo $out;
890a8c33dedSMichael Große        return true;
891a8c33dedSMichael Große    }
892c4a386f1SIain Hallam    $out .= $sep;
893c4a386f1SIain Hallam    $out .= tpl_pagelink($page, null, true);
894c4a386f1SIain Hallam    if ($return) return $out;
89526dfc232SAndreas Gohr    echo $out;
89624870174SAndreas Gohr    return (bool)$out;
8971734437eSandi}
8981734437eSandi
8991734437eSandi/**
9006b13307fSandi * Print info if the user is logged in
901a2488c3cSMatthias Grimm * and show full name in that case
9026b13307fSandi *
9036b13307fSandi * Could be enhanced with a profile link in future?
9046b13307fSandi *
905ac7a515fSAndreas Gohr * @return bool
9064dc42f7fSGerrit Uitslag *
9074dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
9086b13307fSandi */
909d868eb89SAndreas Gohrfunction tpl_userinfo()
910d868eb89SAndreas Gohr{
9116b13307fSandi    global $lang;
912585bf44eSChristopher Smith    /** @var Input $INPUT */
913585bf44eSChristopher Smith    global $INPUT;
914585bf44eSChristopher Smith
915585bf44eSChristopher Smith    if ($INPUT->server->str('REMOTE_USER')) {
91626dfc232SAndreas Gohr        echo $lang['loggedinas'] . ' ' . userlink();
91754e95700STom N Harris        return true;
91854e95700STom N Harris    }
91954e95700STom N Harris    return false;
9206b13307fSandi}
9216b13307fSandi
9226b13307fSandi/**
9236b13307fSandi * Print some info about the current page
9246b13307fSandi *
925ac7a515fSAndreas Gohr * @param bool $ret return content instead of printing it
926ac7a515fSAndreas Gohr * @return bool|string
9274dc42f7fSGerrit Uitslag *
9284dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
9296b13307fSandi */
930d868eb89SAndreas Gohrfunction tpl_pageinfo($ret = false)
931d868eb89SAndreas Gohr{
9326b13307fSandi    global $conf;
9336b13307fSandi    global $lang;
9346b13307fSandi    global $INFO;
935c6e92a3cSDavid Lorentsen    global $ID;
936c6e92a3cSDavid Lorentsen
937c6e92a3cSDavid Lorentsen    // return if we are not allowed to view the page
938ac7a515fSAndreas Gohr    if (!auth_quickaclcheck($ID)) {
939ac7a515fSAndreas Gohr        return false;
940ac7a515fSAndreas Gohr    }
9416b13307fSandi
9426b13307fSandi    // prepare date and path
9436b13307fSandi    $fn = $INFO['filepath'];
9446b13307fSandi    if (!$conf['fullpath']) {
945613bca54SAndreas Gohr        if ($INFO['rev']) {
946c83f69baSSatoshi Sahara            $fn = str_replace($conf['olddir'] . '/', '', $fn);
9476b13307fSandi        } else {
948c83f69baSSatoshi Sahara            $fn = str_replace($conf['datadir'] . '/', '', $fn);
9496b13307fSandi        }
9506b13307fSandi    }
951bee6dc82Sandi    $fn = utf8_decodeFN($fn);
9527370732eSSascha Leib    $dateLocal = dformat($INFO['lastmod']);
9537370732eSSascha Leib    $dateIso = date(DATE_ISO8601, $INFO['lastmod']);
9546b13307fSandi
955faecdfdfSAndreas Gohr    // print it
956faecdfdfSAndreas Gohr    if ($INFO['exists']) {
9574dc42f7fSGerrit Uitslag        $out = '<bdi>' . $fn . '</bdi>';
958e260f93bSAnika Henke        $out .= ' · ';
9594b0d3916SAndreas Gohr        $out .= $lang['lastmod'];
960fde860beSGerrit Uitslag        $out .= ' ';
9617370732eSSascha Leib        $out .= '<time datetime="' . $dateIso . '">' . $dateLocal . '</time>';
9626b13307fSandi        if ($INFO['editor']) {
9634b0d3916SAndreas Gohr            $out .= ' ' . $lang['by'] . ' ';
964d317fb5dSAnika Henke            $out .= '<bdi>' . editorinfo($INFO['editor']) . '</bdi>';
9655aa52fafSBen Coburn        } else {
9664b0d3916SAndreas Gohr            $out .= ' (' . $lang['external_edit'] . ')';
9676b13307fSandi        }
9686b13307fSandi        if ($INFO['locked']) {
969e260f93bSAnika Henke            $out .= ' · ';
9704b0d3916SAndreas Gohr            $out .= $lang['lockedby'];
971fde860beSGerrit Uitslag            $out .= ' ';
972d317fb5dSAnika Henke            $out .= '<bdi>' . editorinfo($INFO['locked']) . '</bdi>';
9736b13307fSandi        }
9744b0d3916SAndreas Gohr        if ($ret) {
9754b0d3916SAndreas Gohr            return $out;
9764b0d3916SAndreas Gohr        } else {
9774b0d3916SAndreas Gohr            echo $out;
97854e95700STom N Harris            return true;
9796b13307fSandi        }
9804b0d3916SAndreas Gohr    }
98154e95700STom N Harris    return false;
9826b13307fSandi}
9836b13307fSandi
984820fa24bSandi/**
985a6598f23SBen Coburn * Prints or returns the name of the given page (current one if none given).
98687c434ceSAndreas Gohr *
98787c434ceSAndreas Gohr * If useheading is enabled this will use the first headline else
988a6598f23SBen Coburn * the given ID is used.
98987c434ceSAndreas Gohr *
990ac7a515fSAndreas Gohr * @param string $id page id
991ac7a515fSAndreas Gohr * @param bool $ret return content instead of printing
992ac7a515fSAndreas Gohr * @return bool|string
9934dc42f7fSGerrit Uitslag *
9944dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
99587c434ceSAndreas Gohr */
996d868eb89SAndreas Gohrfunction tpl_pagetitle($id = null, $ret = false)
997d868eb89SAndreas Gohr{
9984dc42f7fSGerrit Uitslag    global $ACT, $conf, $lang;
999fffeeafeSChristopher Smith
100087c434ceSAndreas Gohr    if (is_null($id)) {
100187c434ceSAndreas Gohr        global $ID;
100287c434ceSAndreas Gohr        $id = $ID;
100387c434ceSAndreas Gohr    }
100487c434ceSAndreas Gohr
100587c434ceSAndreas Gohr    $name = $id;
1006fe9ec250SChris Smith    if (useHeading('navigation')) {
1007fffeeafeSChristopher Smith        $first_heading = p_get_first_heading($id);
1008fffeeafeSChristopher Smith        if ($first_heading) $name = $first_heading;
1009fffeeafeSChristopher Smith    }
1010fffeeafeSChristopher Smith
1011fffeeafeSChristopher Smith    // default page title is the page name, modify with the current action
1012fffeeafeSChristopher Smith    switch ($ACT) {
1013fffeeafeSChristopher Smith        // admin functions
1014fffeeafeSChristopher Smith        case 'admin':
1015fffeeafeSChristopher Smith            $page_title = $lang['btn_admin'];
1016fffeeafeSChristopher Smith            // try to get the plugin name
10174dc42f7fSGerrit Uitslag            /** @var AdminPlugin $plugin */
1018a61966c5SChristopher Smith            if ($plugin = plugin_getRequestAdminPlugin()) {
1019c248bda1SChristopher Smith                $plugin_title = $plugin->getMenuText($conf['lang']);
102024870174SAndreas Gohr                $page_title = $plugin_title ?: $plugin->getPluginName();
1021fffeeafeSChristopher Smith            }
1022fffeeafeSChristopher Smith            break;
1023fffeeafeSChristopher Smith
1024bcedcbabSAndreas Gohr        // show action as title
1025fffeeafeSChristopher Smith        case 'login':
1026fffeeafeSChristopher Smith        case 'profile':
1027fffeeafeSChristopher Smith        case 'register':
1028fffeeafeSChristopher Smith        case 'resendpwd':
1029fffeeafeSChristopher Smith        case 'index':
1030bcedcbabSAndreas Gohr        case 'search':
1031fffeeafeSChristopher Smith            $page_title = $lang['btn_' . $ACT];
1032fffeeafeSChristopher Smith            break;
1033bcedcbabSAndreas Gohr
1034bcedcbabSAndreas Gohr        // add pen during editing
1035fffeeafeSChristopher Smith        case 'edit':
10362f19acc2Sbleistivt        case 'preview':
1037fffeeafeSChristopher Smith            $page_title = "✎ " . $name;
1038fffeeafeSChristopher Smith            break;
1039fffeeafeSChristopher Smith
1040bcedcbabSAndreas Gohr        // add action to page name
1041fffeeafeSChristopher Smith        case 'revisions':
1042fffeeafeSChristopher Smith            $page_title = $name . ' - ' . $lang['btn_revs'];
1043fffeeafeSChristopher Smith            break;
1044fffeeafeSChristopher Smith
1045bcedcbabSAndreas Gohr        // add action to page name
1046fffeeafeSChristopher Smith        case 'backlink':
1047fffeeafeSChristopher Smith        case 'recent':
1048fffeeafeSChristopher Smith        case 'subscribe':
1049fffeeafeSChristopher Smith            $page_title = $name . ' - ' . $lang['btn_' . $ACT];
1050fffeeafeSChristopher Smith            break;
1051fffeeafeSChristopher Smith
1052fffeeafeSChristopher Smith        default: // SHOW and anything else not included
1053fffeeafeSChristopher Smith            $page_title = $name;
105487c434ceSAndreas Gohr    }
1055a6598f23SBen Coburn
1056a6598f23SBen Coburn    if ($ret) {
1057fffeeafeSChristopher Smith        return hsc($page_title);
1058a6598f23SBen Coburn    } else {
105926dfc232SAndreas Gohr        echo hsc($page_title);
106054e95700STom N Harris        return true;
106187c434ceSAndreas Gohr    }
1062a6598f23SBen Coburn}
1063340756e4Sandi
106455efc227SAndreas Gohr/**
106555efc227SAndreas Gohr * Returns the requested EXIF/IPTC tag from the current image
106655efc227SAndreas Gohr *
106755efc227SAndreas Gohr * If $tags is an array all given tags are tried until a
106855efc227SAndreas Gohr * value is found. If no value is found $alt is returned.
106955efc227SAndreas Gohr *
107055efc227SAndreas Gohr * Which texts are known is defined in the functions _exifTagNames
107155efc227SAndreas Gohr * and _iptcTagNames() in inc/jpeg.php (You need to prepend IPTC
107255efc227SAndreas Gohr * to the names of the latter one)
107355efc227SAndreas Gohr *
10743df72098SAndreas Gohr * Only allowed in: detail.php
107555efc227SAndreas Gohr *
107621d806cdSGerrit Uitslag * @param array|string $tags tag or array of tags to try
1077ac7a515fSAndreas Gohr * @param string $alt alternative output if no data was found
1078e0c26282SGerrit Uitslag * @param null|string $src the image src, uses global $SRC if not given
1079ac7a515fSAndreas Gohr * @return string
10804dc42f7fSGerrit Uitslag *
10814dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
108255efc227SAndreas Gohr */
1083d868eb89SAndreas Gohrfunction tpl_img_getTag($tags, $alt = '', $src = null)
1084d868eb89SAndreas Gohr{
108555efc227SAndreas Gohr    // Init Exif Reader
1086a46a7ce3Sasivery    global $SRC, $imgMeta;
10873df72098SAndreas Gohr
10883df72098SAndreas Gohr    if (is_null($src)) $src = $SRC;
10891eadd9e8SAndreas Gohr    if (is_null($src)) return $alt;
10903df72098SAndreas Gohr
10914dc42f7fSGerrit Uitslag    if (!isset($imgMeta)) {
10924dc42f7fSGerrit Uitslag        $imgMeta = new JpegMeta($src);
10934dc42f7fSGerrit Uitslag    }
1094a46a7ce3Sasivery    if ($imgMeta === false) return $alt;
1095a46a7ce3Sasivery    $info = cleanText($imgMeta->getField($tags));
10964dc42f7fSGerrit Uitslag    if (!$info) return $alt;
109755efc227SAndreas Gohr    return $info;
109855efc227SAndreas Gohr}
109955efc227SAndreas Gohr
1100a46a7ce3Sasivery
1101a46a7ce3Sasivery/**
1102a46a7ce3Sasivery * Garbage collects up the open JpegMeta object.
1103a46a7ce3Sasivery */
1104d868eb89SAndreas Gohrfunction tpl_img_close()
1105d868eb89SAndreas Gohr{
1106a46a7ce3Sasivery    global $imgMeta;
1107a46a7ce3Sasivery    $imgMeta = null;
1108a46a7ce3Sasivery}
1109a46a7ce3Sasivery
111055efc227SAndreas Gohr/**
11114dc42f7fSGerrit Uitslag * Prints a html description list of the metatags of the current image
1112becfa414SGerrit Uitslag */
1113d868eb89SAndreas Gohrfunction tpl_img_meta()
1114d868eb89SAndreas Gohr{
1115becfa414SGerrit Uitslag    global $lang;
1116becfa414SGerrit Uitslag
1117becfa414SGerrit Uitslag    $tags = tpl_get_img_meta();
1118becfa414SGerrit Uitslag
1119becfa414SGerrit Uitslag    echo '<dl>';
1120becfa414SGerrit Uitslag    foreach ($tags as $tag) {
1121becfa414SGerrit Uitslag        $label = $lang[$tag['langkey']];
1122fde860beSGerrit Uitslag        if (!$label) $label = $tag['langkey'] . ':';
1123becfa414SGerrit Uitslag
1124fde860beSGerrit Uitslag        echo '<dt>' . $label . '</dt><dd>';
1125becfa414SGerrit Uitslag        if ($tag['type'] == 'date') {
1126becfa414SGerrit Uitslag            echo dformat($tag['value']);
1127becfa414SGerrit Uitslag        } else {
1128becfa414SGerrit Uitslag            echo hsc($tag['value']);
1129becfa414SGerrit Uitslag        }
1130becfa414SGerrit Uitslag        echo '</dd>';
1131becfa414SGerrit Uitslag    }
1132becfa414SGerrit Uitslag    echo '</dl>';
1133becfa414SGerrit Uitslag}
1134becfa414SGerrit Uitslag
1135becfa414SGerrit Uitslag/**
1136becfa414SGerrit Uitslag * Returns metadata as configured in mediameta config file, ready for creating html
1137becfa414SGerrit Uitslag *
1138becfa414SGerrit Uitslag * @return array with arrays containing the entries:
1139becfa414SGerrit Uitslag *   - string langkey  key to lookup in the $lang var, if not found printed as is
1140becfa414SGerrit Uitslag *   - string type     type of value
1141becfa414SGerrit Uitslag *   - string value    tag value (unescaped)
1142becfa414SGerrit Uitslag */
1143d868eb89SAndreas Gohrfunction tpl_get_img_meta()
1144d868eb89SAndreas Gohr{
1145becfa414SGerrit Uitslag
1146becfa414SGerrit Uitslag    $config_files = getConfigFiles('mediameta');
1147becfa414SGerrit Uitslag    foreach ($config_files as $config_file) {
114879e79377SAndreas Gohr        if (file_exists($config_file)) {
1149becfa414SGerrit Uitslag            include($config_file);
1150becfa414SGerrit Uitslag        }
1151becfa414SGerrit Uitslag    }
115224870174SAndreas Gohr    $tags = [];
1153becfa414SGerrit Uitslag    foreach ($fields as $tag) {
115424870174SAndreas Gohr        $t = [];
1155becfa414SGerrit Uitslag        if (!empty($tag[0])) {
115624870174SAndreas Gohr            $t = [$tag[0]];
1157becfa414SGerrit Uitslag        }
1158056bf31fSDamien Regad        if (isset($tag[3]) && is_array($tag[3])) {
1159becfa414SGerrit Uitslag            $t = array_merge($t, $tag[3]);
1160becfa414SGerrit Uitslag        }
1161becfa414SGerrit Uitslag        $value = tpl_img_getTag($t);
1162becfa414SGerrit Uitslag        if ($value) {
116324870174SAndreas Gohr            $tags[] = ['langkey' => $tag[1], 'type' => $tag[2], 'value' => $value];
1164becfa414SGerrit Uitslag        }
1165becfa414SGerrit Uitslag    }
1166becfa414SGerrit Uitslag    return $tags;
1167becfa414SGerrit Uitslag}
1168becfa414SGerrit Uitslag
1169becfa414SGerrit Uitslag/**
117055efc227SAndreas Gohr * Prints the image with a link to the full sized version
117155efc227SAndreas Gohr *
117255efc227SAndreas Gohr * Only allowed in: detail.php
1173a02d2933SAndreas Gohr *
1174ac7a515fSAndreas Gohr * @triggers TPL_IMG_DISPLAY
11754dc42f7fSGerrit Uitslag * @param int $maxwidth - maximal width of the image
11764dc42f7fSGerrit Uitslag * @param int $maxheight - maximal height of the image
11774dc42f7fSGerrit Uitslag * @param bool $link - link to the orginal size?
11784dc42f7fSGerrit Uitslag * @param array $params - additional image attributes
117942ea7f44SGerrit Uitslag * @return bool Result of TPL_IMG_DISPLAY
118055efc227SAndreas Gohr */
1181d868eb89SAndreas Gohrfunction tpl_img($maxwidth = 0, $maxheight = 0, $link = true, $params = null)
1182d868eb89SAndreas Gohr{
118355efc227SAndreas Gohr    global $IMG;
1184585bf44eSChristopher Smith    /** @var Input $INPUT */
1185ac7a515fSAndreas Gohr    global $INPUT;
11865c2eed9aSlisps    global $REV;
118765d3a5dbSAndreas Gohr    $w = (int)tpl_img_getTag('File.Width');
118865d3a5dbSAndreas Gohr    $h = (int)tpl_img_getTag('File.Height');
118955efc227SAndreas Gohr
119055efc227SAndreas Gohr    //resize to given max values
119123a34783SAndreas Gohr    $ratio = 1;
119223a34783SAndreas Gohr    if ($w >= $h) {
1193f8925855Sjoe.lapp        if ($maxwidth && $w >= $maxwidth) {
119455efc227SAndreas Gohr            $ratio = $maxwidth / $w;
1195f8925855Sjoe.lapp        } elseif ($maxheight && $h > $maxheight) {
119655efc227SAndreas Gohr            $ratio = $maxheight / $h;
119755efc227SAndreas Gohr        }
119824870174SAndreas Gohr    } elseif ($maxheight && $h >= $maxheight) {
119955efc227SAndreas Gohr        $ratio = $maxheight / $h;
1200f8925855Sjoe.lapp    } elseif ($maxwidth && $w > $maxwidth) {
120155efc227SAndreas Gohr        $ratio = $maxwidth / $w;
120255efc227SAndreas Gohr    }
120355efc227SAndreas Gohr    if ($ratio) {
120455efc227SAndreas Gohr        $w = floor($ratio * $w);
120555efc227SAndreas Gohr        $h = floor($ratio * $h);
120655efc227SAndreas Gohr    }
120755efc227SAndreas Gohr
12086de3759aSAndreas Gohr    //prepare URLs
120924870174SAndreas Gohr    $url = ml($IMG, ['cache' => $INPUT->str('cache'), 'rev' => $REV], true, '&');
121024870174SAndreas Gohr    $src = ml($IMG, ['cache' => $INPUT->str('cache'), 'rev' => $REV, 'w' => $w, 'h' => $h], true, '&');
121155efc227SAndreas Gohr
12122684e50aSAndreas Gohr    //prepare attributes
121355efc227SAndreas Gohr    $alt = tpl_img_getTag('Simple.Title');
1214a02d2933SAndreas Gohr    if (is_null($params)) {
121524870174SAndreas Gohr        $p = [];
1216a02d2933SAndreas Gohr    } else {
1217a02d2933SAndreas Gohr        $p = $params;
1218a02d2933SAndreas Gohr    }
12192684e50aSAndreas Gohr    if ($w) $p['width'] = $w;
12202684e50aSAndreas Gohr    if ($h) $p['height'] = $h;
12212684e50aSAndreas Gohr    $p['class'] = 'img_detail';
12222684e50aSAndreas Gohr    if ($alt) {
12232684e50aSAndreas Gohr        $p['alt'] = $alt;
12242684e50aSAndreas Gohr        $p['title'] = $alt;
12252684e50aSAndreas Gohr    } else {
12262684e50aSAndreas Gohr        $p['alt'] = '';
12272684e50aSAndreas Gohr    }
1228a02d2933SAndreas Gohr    $p['src'] = $src;
122955efc227SAndreas Gohr
123024870174SAndreas Gohr    $data = ['url' => ($link ? $url : null), 'params' => $p];
1231cbb44eabSAndreas Gohr    return Event::createAndTrigger('TPL_IMG_DISPLAY', $data, '_tpl_img_action', true);
1232a02d2933SAndreas Gohr}
1233a02d2933SAndreas Gohr
1234a02d2933SAndreas Gohr/**
1235a02d2933SAndreas Gohr * Default action for TPL_IMG_DISPLAY
1236ac7a515fSAndreas Gohr *
1237ac7a515fSAndreas Gohr * @param array $data
1238ac7a515fSAndreas Gohr * @return bool
1239a02d2933SAndreas Gohr */
1240d868eb89SAndreas Gohrfunction _tpl_img_action($data)
1241d868eb89SAndreas Gohr{
124259f3611bSAnika Henke    global $lang;
1243a02d2933SAndreas Gohr    $p = buildAttributes($data['params']);
1244a02d2933SAndreas Gohr
124526dfc232SAndreas Gohr    if ($data['url']) echo '<a href="' . hsc($data['url']) . '" title="' . $lang['mediaview'] . '">';
124626dfc232SAndreas Gohr    echo '<img ' . $p . '/>';
124726dfc232SAndreas Gohr    if ($data['url']) echo '</a>';
124854e95700STom N Harris    return true;
124955efc227SAndreas Gohr}
125055efc227SAndreas Gohr
12517367b368SAndreas Gohr/**
1252881f2ee2SAndreas Haerter * This function inserts a small gif which in reality is the indexer function.
12537367b368SAndreas Gohr *
12544dc42f7fSGerrit Uitslag * Should be called somewhere at the very end of the main.php template
1255ac7a515fSAndreas Gohr *
1256ac7a515fSAndreas Gohr * @return bool
12577367b368SAndreas Gohr */
1258d868eb89SAndreas Gohrfunction tpl_indexerWebBug()
1259d868eb89SAndreas Gohr{
12607367b368SAndreas Gohr    global $ID;
12611dad36f5SAndreas Gohr
126224870174SAndreas Gohr    $p = [];
12634af80fe8SMichael Große    $p['src'] = DOKU_BASE . 'lib/exe/taskrunner.php?id=' . rawurlencode($ID) .
1264e68c51baSAndreas Gohr        '&' . time();
1265881f2ee2SAndreas Haerter    $p['width'] = 2; //no more 1x1 px image because we live in times of ad blockers...
12667367b368SAndreas Gohr    $p['height'] = 1;
12677367b368SAndreas Gohr    $p['alt'] = '';
12687367b368SAndreas Gohr    $att = buildAttributes($p);
126926dfc232SAndreas Gohr    echo "<img $att />";
127054e95700STom N Harris    return true;
12717367b368SAndreas Gohr}
12727367b368SAndreas Gohr
127378d4e784SEsther Brunner/**
127478d4e784SEsther Brunner * tpl_getConf($id)
127578d4e784SEsther Brunner *
127678d4e784SEsther Brunner * use this function to access template configuration variables
1277ac7a515fSAndreas Gohr *
127817448fb8SChristopher Smith * @param string $id name of the value to access
127917448fb8SChristopher Smith * @param mixed $notset what to return if the setting is not available
128017448fb8SChristopher Smith * @return mixed
128178d4e784SEsther Brunner */
1282d868eb89SAndreas Gohrfunction tpl_getConf($id, $notset = false)
1283d868eb89SAndreas Gohr{
128478d4e784SEsther Brunner    global $conf;
128517566ac6SAdrian Lang    static $tpl_configloaded = false;
128678d4e784SEsther Brunner
128778d4e784SEsther Brunner    $tpl = $conf['template'];
128878d4e784SEsther Brunner
128978d4e784SEsther Brunner    if (!$tpl_configloaded) {
129078d4e784SEsther Brunner        $tconf = tpl_loadConfig();
129178d4e784SEsther Brunner        if ($tconf !== false) {
129278d4e784SEsther Brunner            foreach ($tconf as $key => $value) {
129378d4e784SEsther Brunner                if (isset($conf['tpl'][$tpl][$key])) continue;
129478d4e784SEsther Brunner                $conf['tpl'][$tpl][$key] = $value;
129578d4e784SEsther Brunner            }
129678d4e784SEsther Brunner            $tpl_configloaded = true;
129778d4e784SEsther Brunner        }
129878d4e784SEsther Brunner    }
129978d4e784SEsther Brunner
130024870174SAndreas Gohr    return $conf['tpl'][$tpl][$id] ?? $notset;
130117448fb8SChristopher Smith}
130217448fb8SChristopher Smith
130378d4e784SEsther Brunner/**
130478d4e784SEsther Brunner * tpl_loadConfig()
1305ac7a515fSAndreas Gohr *
130678d4e784SEsther Brunner * reads all template configuration variables
130778d4e784SEsther Brunner * this function is automatically called by tpl_getConf()
1308ac7a515fSAndreas Gohr *
13094dc42f7fSGerrit Uitslag * @return false|array
131078d4e784SEsther Brunner */
1311d868eb89SAndreas Gohrfunction tpl_loadConfig()
1312d868eb89SAndreas Gohr{
131378d4e784SEsther Brunner
1314c4766956SAndreas Gohr    $file = tpl_incdir() . '/conf/default.php';
131524870174SAndreas Gohr    $conf = [];
131678d4e784SEsther Brunner
131779e79377SAndreas Gohr    if (!file_exists($file)) return false;
131878d4e784SEsther Brunner
131978d4e784SEsther Brunner    // load default config file
132078d4e784SEsther Brunner    include($file);
132178d4e784SEsther Brunner
132278d4e784SEsther Brunner    return $conf;
132378d4e784SEsther Brunner}
132478d4e784SEsther Brunner
132517566ac6SAdrian Lang// language methods
13264dc42f7fSGerrit Uitslag
132717566ac6SAdrian Lang/**
132817566ac6SAdrian Lang * tpl_getLang($id)
132917566ac6SAdrian Lang *
133017566ac6SAdrian Lang * use this function to access template language variables
133142ea7f44SGerrit Uitslag *
133242ea7f44SGerrit Uitslag * @param string $id key of language string
133342ea7f44SGerrit Uitslag * @return string
133417566ac6SAdrian Lang */
1335d868eb89SAndreas Gohrfunction tpl_getLang($id)
1336d868eb89SAndreas Gohr{
133724870174SAndreas Gohr    static $lang = [];
133817566ac6SAdrian Lang
133917566ac6SAdrian Lang    if (count($lang) === 0) {
1340dd7a6159SGerrit Uitslag        global $conf, $config_cascade; // definitely don't invoke "global $lang"
1341dd7a6159SGerrit Uitslag
1342c4766956SAndreas Gohr        $path = tpl_incdir() . 'lang/';
134317566ac6SAdrian Lang
134424870174SAndreas Gohr        $lang = [];
134517566ac6SAdrian Lang
134617566ac6SAdrian Lang        // don't include once
134717566ac6SAdrian Lang        @include($path . 'en/lang.php');
1348dd7a6159SGerrit Uitslag        foreach ($config_cascade['lang']['template'] as $config_file) {
134979e79377SAndreas Gohr            if (file_exists($config_file . $conf['template'] . '/en/lang.php')) {
1350dd7a6159SGerrit Uitslag                include($config_file . $conf['template'] . '/en/lang.php');
1351dd7a6159SGerrit Uitslag            }
135217566ac6SAdrian Lang        }
135317566ac6SAdrian Lang
1354dd7a6159SGerrit Uitslag        if ($conf['lang'] != 'en') {
1355dd7a6159SGerrit Uitslag            @include($path . $conf['lang'] . '/lang.php');
1356dd7a6159SGerrit Uitslag            foreach ($config_cascade['lang']['template'] as $config_file) {
135779e79377SAndreas Gohr                if (file_exists($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php')) {
1358dd7a6159SGerrit Uitslag                    include($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php');
1359dd7a6159SGerrit Uitslag                }
1360dd7a6159SGerrit Uitslag            }
1361dd7a6159SGerrit Uitslag        }
136217566ac6SAdrian Lang    }
136324870174SAndreas Gohr    return $lang[$id] ?? '';
136417566ac6SAdrian Lang}
136517566ac6SAdrian Lang
13663df72098SAndreas Gohr/**
1367c5c17fdaSKlap-in * Retrieve a language dependent file and pass to xhtml renderer for display
1368e8ec13b9SKlap-in * template equivalent of p_locale_xhtml()
1369e8ec13b9SKlap-in *
1370e8ec13b9SKlap-in * @param string $id id of language dependent wiki page
1371e8ec13b9SKlap-in * @return  string     parsed contents of the wiki page in xhtml format
1372e8ec13b9SKlap-in */
1373d868eb89SAndreas Gohrfunction tpl_locale_xhtml($id)
1374d868eb89SAndreas Gohr{
1375c5c17fdaSKlap-in    return p_cached_output(tpl_localeFN($id));
1376e8ec13b9SKlap-in}
1377e8ec13b9SKlap-in
1378e8ec13b9SKlap-in/**
1379c5c17fdaSKlap-in * Prepends appropriate path for a language dependent filename
138042ea7f44SGerrit Uitslag *
138142ea7f44SGerrit Uitslag * @param string $id id of localized text
138242ea7f44SGerrit Uitslag * @return string wiki text
1383e8ec13b9SKlap-in */
1384d868eb89SAndreas Gohrfunction tpl_localeFN($id)
1385d868eb89SAndreas Gohr{
1386e8ec13b9SKlap-in    $path = tpl_incdir() . 'lang/';
1387e8ec13b9SKlap-in    global $conf;
138838fb1fc7SGerrit Uitslag    $file = DOKU_CONF . 'template_lang/' . $conf['template'] . '/' . $conf['lang'] . '/' . $id . '.txt';
138979e79377SAndreas Gohr    if (!file_exists($file)) {
1390e8ec13b9SKlap-in        $file = $path . $conf['lang'] . '/' . $id . '.txt';
139179e79377SAndreas Gohr        if (!file_exists($file)) {
1392e8ec13b9SKlap-in            //fall back to english
1393e8ec13b9SKlap-in            $file = $path . 'en/' . $id . '.txt';
1394e8ec13b9SKlap-in        }
1395e8ec13b9SKlap-in    }
1396e8ec13b9SKlap-in    return $file;
1397e8ec13b9SKlap-in}
1398e8ec13b9SKlap-in
1399e8ec13b9SKlap-in/**
14007abc270fSGerrit Uitslag * prints the "main content" in the mediamanager popup
14013df72098SAndreas Gohr *
14023df72098SAndreas Gohr * Depending on the user's actions this may be a list of
14033df72098SAndreas Gohr * files in a namespace, the meta editing dialog or
14043df72098SAndreas Gohr * a message of referencing pages
14053df72098SAndreas Gohr *
14063df72098SAndreas Gohr * Only allowed in mediamanager.php
14073df72098SAndreas Gohr *
1408c182313eSAndreas Gohr * @triggers MEDIAMANAGER_CONTENT_OUTPUT
1409c182313eSAndreas Gohr * @param bool $fromajax - set true when calling this function via ajax
141042ea7f44SGerrit Uitslag * @param string $sort
14118702de7fSGerrit Uitslag *
14123df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
14133df72098SAndreas Gohr */
1414d868eb89SAndreas Gohrfunction tpl_mediaContent($fromajax = false, $sort = 'natural')
1415d868eb89SAndreas Gohr{
14163df72098SAndreas Gohr    global $IMG;
14173df72098SAndreas Gohr    global $AUTH;
14183df72098SAndreas Gohr    global $INUSE;
14193df72098SAndreas Gohr    global $NS;
14203df72098SAndreas Gohr    global $JUMPTO;
1421585bf44eSChristopher Smith    /** @var Input $INPUT */
1422ac7a515fSAndreas Gohr    global $INPUT;
14233df72098SAndreas Gohr
1424ac7a515fSAndreas Gohr    $do = $INPUT->extract('do')->str('do');
142524870174SAndreas Gohr    if (in_array($do, ['save', 'cancel'])) $do = '';
1426c182313eSAndreas Gohr
1427c182313eSAndreas Gohr    if (!$do) {
1428ac7a515fSAndreas Gohr        if ($INPUT->bool('edit')) {
1429c182313eSAndreas Gohr            $do = 'metaform';
1430c182313eSAndreas Gohr        } elseif (is_array($INUSE)) {
1431c182313eSAndreas Gohr            $do = 'filesinuse';
1432c182313eSAndreas Gohr        } else {
1433c182313eSAndreas Gohr            $do = 'filelist';
1434c182313eSAndreas Gohr        }
1435c182313eSAndreas Gohr    }
1436c182313eSAndreas Gohr
1437c182313eSAndreas Gohr    // output the content pane, wrapped in an event.
1438a664aabaSAndreas Gohr    if (!$fromajax) echo '<div id="media__content">';
143924870174SAndreas Gohr    $data = ['do' => $do];
1440e1d9dcc8SAndreas Gohr    $evt = new Event('MEDIAMANAGER_CONTENT_OUTPUT', $data);
1441c182313eSAndreas Gohr    if ($evt->advise_before()) {
1442c182313eSAndreas Gohr        $do = $data['do'];
144330fd72fbSKate Arzamastseva        if ($do == 'filesinuse') {
1444c182313eSAndreas Gohr            media_filesinuse($INUSE, $IMG);
1445c182313eSAndreas Gohr        } elseif ($do == 'filelist') {
144600e3e394SChristopher Smith            media_filelist($NS, $AUTH, $JUMPTO, false, $sort);
1447c9f56829SAndreas Gohr        } elseif ($do == 'searchlist') {
1448ac7a515fSAndreas Gohr            media_searchlist($INPUT->str('q'), $NS, $AUTH);
1449c182313eSAndreas Gohr        } else {
1450c182313eSAndreas Gohr            msg('Unknown action ' . hsc($do), -1);
1451c182313eSAndreas Gohr        }
1452c182313eSAndreas Gohr    }
1453c182313eSAndreas Gohr    $evt->advise_after();
1454c182313eSAndreas Gohr    unset($evt);
1455a664aabaSAndreas Gohr    if (!$fromajax) echo '</div>';
14563df72098SAndreas Gohr}
14573df72098SAndreas Gohr
14583df72098SAndreas Gohr/**
1459d9162c6cSKate Arzamastseva * Prints the central column in full-screen media manager
1460d9162c6cSKate Arzamastseva * Depending on the opened tab this may be a list of
1461d9162c6cSKate Arzamastseva * files in a namespace, upload form or search form
1462d9162c6cSKate Arzamastseva *
1463d9162c6cSKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net>
1464d9162c6cSKate Arzamastseva */
1465d868eb89SAndreas Gohrfunction tpl_mediaFileList()
1466d868eb89SAndreas Gohr{
1467d9162c6cSKate Arzamastseva    global $AUTH;
1468d9162c6cSKate Arzamastseva    global $NS;
1469d9162c6cSKate Arzamastseva    global $JUMPTO;
147095b451bcSAdrian Lang    global $lang;
1471585bf44eSChristopher Smith    /** @var Input $INPUT */
1472ac7a515fSAndreas Gohr    global $INPUT;
1473d9162c6cSKate Arzamastseva
1474ac7a515fSAndreas Gohr    $opened_tab = $INPUT->str('tab_files');
147524870174SAndreas Gohr    if (!$opened_tab || !in_array($opened_tab, ['files', 'upload', 'search'])) $opened_tab = 'files';
1476ac7a515fSAndreas Gohr    if ($INPUT->str('mediado') == 'update') $opened_tab = 'upload';
1477d9162c6cSKate Arzamastseva
147894add303SAnika Henke    echo '<h2 class="a11y">' . $lang['mediaselect'] . '</h2>' . NL;
147995b451bcSAdrian Lang
1480ed69a2aeSKate Arzamastseva    media_tabs_files($opened_tab);
148123846a98SKate Arzamastseva
148294add303SAnika Henke    echo '<div class="panelHeader">' . NL;
148395b451bcSAdrian Lang    echo '<h3>';
148424870174SAndreas Gohr    $tabTitle = $NS ?: '[' . $lang['mediaroot'] . ']';
1485c98f205eSAdrian Lang    printf($lang['media_' . $opened_tab], '<strong>' . hsc($tabTitle) . '</strong>');
148694add303SAnika Henke    echo '</h3>' . NL;
148795b451bcSAdrian Lang    if ($opened_tab === 'search' || $opened_tab === 'files') {
148895b451bcSAdrian Lang        media_tab_files_options();
148923846a98SKate Arzamastseva    }
149094add303SAnika Henke    echo '</div>' . NL;
1491d9162c6cSKate Arzamastseva
149294add303SAnika Henke    echo '<div class="panelContent">' . NL;
149395b451bcSAdrian Lang    if ($opened_tab == 'files') {
149495b451bcSAdrian Lang        media_tab_files($NS, $AUTH, $JUMPTO);
149595b451bcSAdrian Lang    } elseif ($opened_tab == 'upload') {
149695b451bcSAdrian Lang        media_tab_upload($NS, $AUTH, $JUMPTO);
149795b451bcSAdrian Lang    } elseif ($opened_tab == 'search') {
149895b451bcSAdrian Lang        media_tab_search($NS, $AUTH);
149995b451bcSAdrian Lang    }
150094add303SAnika Henke    echo '</div>' . NL;
1501d9162c6cSKate Arzamastseva}
1502d9162c6cSKate Arzamastseva
1503d9162c6cSKate Arzamastseva/**
1504d9162c6cSKate Arzamastseva * Prints the third column in full-screen media manager
1505d9162c6cSKate Arzamastseva * Depending on the opened tab this may be details of the
1506d9162c6cSKate Arzamastseva * selected file, the meta editing dialog or
1507d9162c6cSKate Arzamastseva * list of file revisions
1508d9162c6cSKate Arzamastseva *
1509f50a239bSTakamura * @param string $image
1510f50a239bSTakamura * @param boolean $rev
15114dc42f7fSGerrit Uitslag *
15124dc42f7fSGerrit Uitslag * @author Kate Arzamastseva <pshns@ukr.net>
1513d9162c6cSKate Arzamastseva */
1514d868eb89SAndreas Gohrfunction tpl_mediaFileDetails($image, $rev)
1515d868eb89SAndreas Gohr{
1516e8a2a143SMichael Hamann    global $conf, $DEL, $lang;
1517585bf44eSChristopher Smith    /** @var Input $INPUT */
1518585bf44eSChristopher Smith    global $INPUT;
1519d9162c6cSKate Arzamastseva
152064159a61SAndreas Gohr    $removed = (
152164159a61SAndreas Gohr        !file_exists(mediaFN($image)) &&
152264159a61SAndreas Gohr        file_exists(mediaMetaFN($image, '.changes')) &&
152364159a61SAndreas Gohr        $conf['mediarevisions']
152464159a61SAndreas Gohr    );
1525ac7a515fSAndreas Gohr    if (!$image || (!file_exists(mediaFN($image)) && !$removed) || $DEL) return;
15266dd095f5SKate Arzamastseva    if ($rev && !file_exists(mediaFN($image, $rev))) $rev = false;
1527e8a2a143SMichael Hamann    $ns = getNS($image);
1528ac7a515fSAndreas Gohr    $do = $INPUT->str('mediado');
15291eeeced2SKate Arzamastseva
1530ac7a515fSAndreas Gohr    $opened_tab = $INPUT->str('tab_details');
1531e5d185e1SKate Arzamastseva
153224870174SAndreas Gohr    $tab_array = ['view'];
153324870174SAndreas Gohr    [, $mime] = mimetype($image);
1534e5d185e1SKate Arzamastseva    if ($mime == 'image/jpeg') {
1535e5d185e1SKate Arzamastseva        $tab_array[] = 'edit';
1536e5d185e1SKate Arzamastseva    }
1537e5d185e1SKate Arzamastseva    if ($conf['mediarevisions']) {
1538e5d185e1SKate Arzamastseva        $tab_array[] = 'history';
1539e5d185e1SKate Arzamastseva    }
1540e5d185e1SKate Arzamastseva
1541e5d185e1SKate Arzamastseva    if (!$opened_tab || !in_array($opened_tab, $tab_array)) $opened_tab = 'view';
1542ac7a515fSAndreas Gohr    if ($INPUT->bool('edit')) $opened_tab = 'edit';
154323846a98SKate Arzamastseva    if ($do == 'restore') $opened_tab = 'view';
1544d9162c6cSKate Arzamastseva
1545ed69a2aeSKate Arzamastseva    media_tabs_details($image, $opened_tab);
154623846a98SKate Arzamastseva
154759f3611bSAnika Henke    echo '<div class="panelHeader"><h3>';
154824870174SAndreas Gohr    [$ext] = mimetype($image, false);
154995b451bcSAdrian Lang    $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
155095b451bcSAdrian Lang    $class = 'select mediafile mf_' . $class;
155124870174SAndreas Gohr
1552750a0b51SMichael Große    $attributes = $rev ? ['rev' => $rev] : [];
155390fb952cSAndreas Gohr    $tabTitle = sprintf(
155490fb952cSAndreas Gohr        '<strong><a href="%s" class="%s" title="%s">%s</a></strong>',
155590fb952cSAndreas Gohr        ml($image, $attributes),
155690fb952cSAndreas Gohr        $class,
155790fb952cSAndreas Gohr        $lang['mediaview'],
155890fb952cSAndreas Gohr        $image
155990fb952cSAndreas Gohr    );
156008317413SAdrian Lang    if ($opened_tab === 'view' && $rev) {
156108317413SAdrian Lang        printf($lang['media_viewold'], $tabTitle, dformat($rev));
156208317413SAdrian Lang    } else {
1563026d14a9SAnika Henke        printf($lang['media_' . $opened_tab], $tabTitle);
156408317413SAdrian Lang    }
1565b8a84c03SAndreas Gohr
156694add303SAnika Henke    echo '</h3></div>' . NL;
156795b451bcSAdrian Lang
156894add303SAnika Henke    echo '<div class="panelContent">' . NL;
156995b451bcSAdrian Lang
157023846a98SKate Arzamastseva    if ($opened_tab == 'view') {
1571e8a2a143SMichael Hamann        media_tab_view($image, $ns, null, $rev);
157292cac9a9SKate Arzamastseva    } elseif ($opened_tab == 'edit' && !$removed) {
1573e8a2a143SMichael Hamann        media_tab_edit($image, $ns);
1574e5d185e1SKate Arzamastseva    } elseif ($opened_tab == 'history' && $conf['mediarevisions']) {
1575e8a2a143SMichael Hamann        media_tab_history($image, $ns);
157623846a98SKate Arzamastseva    }
157795b451bcSAdrian Lang
157894add303SAnika Henke    echo '</div>' . NL;
1579d9162c6cSKate Arzamastseva}
1580d9162c6cSKate Arzamastseva
1581d9162c6cSKate Arzamastseva/**
15827abc270fSGerrit Uitslag * prints the namespace tree in the mediamanager popup
15833df72098SAndreas Gohr *
15843df72098SAndreas Gohr * Only allowed in mediamanager.php
15853df72098SAndreas Gohr *
15863df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
15873df72098SAndreas Gohr */
1588d868eb89SAndreas Gohrfunction tpl_mediaTree()
1589d868eb89SAndreas Gohr{
15903df72098SAndreas Gohr    global $NS;
1591a664aabaSAndreas Gohr    echo '<div id="media__tree">';
15923df72098SAndreas Gohr    media_nstree($NS);
1593a664aabaSAndreas Gohr    echo '</div>';
15943df72098SAndreas Gohr}
15953df72098SAndreas Gohr
1596a00de5b5SAndreas Gohr/**
1597a00de5b5SAndreas Gohr * Print a dropdown menu with all DokuWiki actions
1598a00de5b5SAndreas Gohr *
1599a00de5b5SAndreas Gohr * Note: this will not use any pretty URLs
1600a00de5b5SAndreas Gohr *
160142ea7f44SGerrit Uitslag * @param string $empty empty option label
160242ea7f44SGerrit Uitslag * @param string $button submit button label
16034dc42f7fSGerrit Uitslag *
16044dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
1605affc7ddfSAndreas Gohr * @deprecated 2017-09-01 see devel:menus
1606a00de5b5SAndreas Gohr */
1607d868eb89SAndreas Gohrfunction tpl_actiondropdown($empty = '', $button = '&gt;')
1608d868eb89SAndreas Gohr{
1609affc7ddfSAndreas Gohr    dbg_deprecated('see devel:menus');
161024870174SAndreas Gohr    $menu = new MobileMenu();
16111e875dcdSAndreas Gohr    echo $menu->getDropdown($empty, $button);
1612a00de5b5SAndreas Gohr}
1613a00de5b5SAndreas Gohr
1614066fee30SAndreas Gohr/**
1615066fee30SAndreas Gohr * Print a informational line about the used license
1616066fee30SAndreas Gohr *
1617ac7a515fSAndreas Gohr * @param string $img print image? (|button|badge)
1618ac7a515fSAndreas Gohr * @param bool $imgonly skip the textual description?
1619ac7a515fSAndreas Gohr * @param bool $return when true don't print, but return HTML
1620ac7a515fSAndreas Gohr * @param bool $wrap wrap in div with class="license"?
1621ac7a515fSAndreas Gohr * @return string
16224dc42f7fSGerrit Uitslag *
16234dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
1624066fee30SAndreas Gohr */
1625d868eb89SAndreas Gohrfunction tpl_license($img = 'badge', $imgonly = false, $return = false, $wrap = true)
1626d868eb89SAndreas Gohr{
1627066fee30SAndreas Gohr    global $license;
1628066fee30SAndreas Gohr    global $conf;
1629066fee30SAndreas Gohr    global $lang;
1630066fee30SAndreas Gohr    if (!$conf['license']) return '';
1631066fee30SAndreas Gohr    if (!is_array($license[$conf['license']])) return '';
1632066fee30SAndreas Gohr    $lic = $license[$conf['license']];
163353e15c8bSAnika Henke    $target = ($conf['target']['extern']) ? ' target="' . $conf['target']['extern'] . '"' : '';
1634066fee30SAndreas Gohr
163580083a41SAndreas Gohr    $out = '';
163680083a41SAndreas Gohr    if ($wrap) $out .= '<div class="license">';
1637066fee30SAndreas Gohr    if ($img) {
1638066fee30SAndreas Gohr        $src = license_img($img);
1639066fee30SAndreas Gohr        if ($src) {
164053e15c8bSAnika Henke            $out .= '<a href="' . $lic['url'] . '" rel="license"' . $target;
164153e15c8bSAnika Henke            $out .= '><img src="' . DOKU_BASE . $src . '" alt="' . $lic['name'] . '" /></a>';
164253e15c8bSAnika Henke            if (!$imgonly) $out .= ' ';
1643066fee30SAndreas Gohr        }
1644066fee30SAndreas Gohr    }
16454cefd216SMichael Klier    if (!$imgonly) {
164653e15c8bSAnika Henke        $out .= $lang['license'] . ' ';
1647d317fb5dSAnika Henke        $out .= '<bdi><a href="' . $lic['url'] . '" rel="license" class="urlextern"' . $target;
1648d317fb5dSAnika Henke        $out .= '>' . $lic['name'] . '</a></bdi>';
16494cefd216SMichael Klier    }
165080083a41SAndreas Gohr    if ($wrap) $out .= '</div>';
1651066fee30SAndreas Gohr
1652066fee30SAndreas Gohr    if ($return) return $out;
1653066fee30SAndreas Gohr    echo $out;
1654ac7a515fSAndreas Gohr    return '';
1655066fee30SAndreas Gohr}
1656066fee30SAndreas Gohr
1657a81910eeSAndreas Gohr/**
1658835dfcaeSAnika Henke * Includes the rendered HTML of a given page
1659a81910eeSAndreas Gohr *
1660a81910eeSAndreas Gohr * This function is useful to populate sidebars or similar features in a
1661a81910eeSAndreas Gohr * template
1662e0c26282SGerrit Uitslag *
16637a112df5SAndreas Gohr * @param string $pageid The page name you want to include
16647a112df5SAndreas Gohr * @param bool $print Should the content be printed or returned only
16657a112df5SAndreas Gohr * @param bool $propagate Search higher namespaces, too?
16667c3e4a67SAndreas Gohr * @param bool $useacl Include the page only if the ACLs check out?
1667e0c26282SGerrit Uitslag * @return bool|null|string
1668a81910eeSAndreas Gohr */
1669d868eb89SAndreas Gohrfunction tpl_include_page($pageid, $print = true, $propagate = false, $useacl = true)
1670d868eb89SAndreas Gohr{
16717a112df5SAndreas Gohr    if ($propagate) {
16727c3e4a67SAndreas Gohr        $pageid = page_findnearest($pageid, $useacl);
16737c3e4a67SAndreas Gohr    } elseif ($useacl && auth_quickaclcheck($pageid) == AUTH_NONE) {
16747a112df5SAndreas Gohr        return false;
16757a112df5SAndreas Gohr    }
1676c786a1b6SAnika Henke    if (!$pageid) return false;
1677835dfcaeSAnika Henke
1678c786a1b6SAnika Henke    global $TOC;
16799a2e250aSAndreas Gohr    $oldtoc = $TOC;
1680a81910eeSAndreas Gohr    $html = p_wiki_xhtml($pageid, '', false);
16819a2e250aSAndreas Gohr    $TOC = $oldtoc;
1682a81910eeSAndreas Gohr
1683a2e03c82SAndreas Gohr    if ($print) echo $html;
1684e66d3e6dSAndreas Gohr    return $html;
1685e66d3e6dSAndreas Gohr}
1686e66d3e6dSAndreas Gohr
1687e66d3e6dSAndreas Gohr/**
16885b75cd1fSAdrian Lang * Display the subscribe form
16895b75cd1fSAdrian Lang *
16905b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
1691848cb786SSatoshi Sahara * @deprecated 2020-07-23
16925b75cd1fSAdrian Lang */
1693d868eb89SAndreas Gohrfunction tpl_subscribe()
1694d868eb89SAndreas Gohr{
169524870174SAndreas Gohr    dbg_deprecated(Subscribe::class . '::show()');
169673022918SAndreas Gohr    (new Subscribe())->show();
16975b75cd1fSAdrian Lang}
16985b75cd1fSAdrian Lang
1699d059ba9bSAndreas Gohr/**
1700d059ba9bSAndreas Gohr * Tries to send already created content right to the browser
1701d059ba9bSAndreas Gohr *
1702d059ba9bSAndreas Gohr * Wraps around ob_flush() and flush()
1703d059ba9bSAndreas Gohr *
1704d059ba9bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1705d059ba9bSAndreas Gohr */
1706d868eb89SAndreas Gohrfunction tpl_flush()
1707d868eb89SAndreas Gohr{
1708c2009796SZemoj    if (ob_get_level() > 0) ob_flush();
1709d059ba9bSAndreas Gohr    flush();
1710d059ba9bSAndreas Gohr}
1711d059ba9bSAndreas Gohr
1712afca7e7eSAnika Henke/**
1713378325f9SAndreas Gohr * Tries to find a ressource file in the given locations.
1714afca7e7eSAnika Henke *
1715378325f9SAndreas Gohr * If a given location starts with a colon it is assumed to be a media
1716378325f9SAndreas Gohr * file, otherwise it is assumed to be relative to the current template
1717378325f9SAndreas Gohr *
171842ea7f44SGerrit Uitslag * @param string[] $search locations to look at
1719378325f9SAndreas Gohr * @param bool $abs if to use absolute URL
1720ac7a515fSAndreas Gohr * @param array    &$imginfo filled with getimagesize()
17216dc405e1SAndreas Gohr * @param bool $fallback use fallback image if target isn't found or return 'false' if potential
17226dc405e1SAndreas Gohr *                                false result is required
1723ac7a515fSAndreas Gohr * @return string
172442ea7f44SGerrit Uitslag *
1725378325f9SAndreas Gohr * @author Andreas  Gohr <andi@splitbrain.org>
1726afca7e7eSAnika Henke */
1727d868eb89SAndreas Gohrfunction tpl_getMediaFile($search, $abs = false, &$imginfo = null, $fallback = true)
1728d868eb89SAndreas Gohr{
1729ac7a515fSAndreas Gohr    $img = '';
1730ac7a515fSAndreas Gohr    $file = '';
1731ac7a515fSAndreas Gohr    $ismedia = false;
1732378325f9SAndreas Gohr    // loop through candidates until a match was found:
1733378325f9SAndreas Gohr    foreach ($search as $img) {
17346c16a3a9Sfiwswe        if (str_starts_with($img, ':')) {
1735378325f9SAndreas Gohr            $file = mediaFN($img);
1736378325f9SAndreas Gohr            $ismedia = true;
1737378325f9SAndreas Gohr        } else {
1738c4766956SAndreas Gohr            $file = tpl_incdir() . $img;
1739378325f9SAndreas Gohr            $ismedia = false;
17401f13e33dSAnika Henke        }
17410f747863Slupo49
1742378325f9SAndreas Gohr        if (file_exists($file)) break;
1743872a6d29SAnika Henke    }
1744378325f9SAndreas Gohr
174508a13262SSimon DELAGE    // manage non existing target
174608a13262SSimon DELAGE    if (!file_exists($file)) {
174708a13262SSimon DELAGE        // give result for fallback image
174824870174SAndreas Gohr        if ($fallback) {
1749e44b94a4SAndreas Gohr            $file = DOKU_INC . 'lib/images/blank.gif';
175008a13262SSimon DELAGE            // stop process if false result is required (if $fallback is false)
175108a13262SSimon DELAGE        } else {
175208a13262SSimon DELAGE            return false;
175308a13262SSimon DELAGE        }
175408a13262SSimon DELAGE    }
1755ca5b6a64SSimon DELAGE
1756378325f9SAndreas Gohr    // fetch image data if requested
1757378325f9SAndreas Gohr    if (!is_null($imginfo)) {
1758378325f9SAndreas Gohr        $imginfo = getimagesize($file);
1759378325f9SAndreas Gohr    }
1760378325f9SAndreas Gohr
1761378325f9SAndreas Gohr    // build URL
1762378325f9SAndreas Gohr    if ($ismedia) {
1763378325f9SAndreas Gohr        $url = ml($img, '', true, '', $abs);
1764378325f9SAndreas Gohr    } else {
1765c4766956SAndreas Gohr        $url = tpl_basedir() . $img;
1766378325f9SAndreas Gohr        if ($abs) $url = DOKU_URL . substr($url, strlen(DOKU_REL));
1767378325f9SAndreas Gohr    }
1768378325f9SAndreas Gohr
1769378325f9SAndreas Gohr    return $url;
1770a7e5f74cSlupo49}
17711f13e33dSAnika Henke
1772872a6d29SAnika Henke/**
1773e5d4768dSAndreas Gohr * PHP include a file
1774e5d4768dSAndreas Gohr *
1775e5d4768dSAndreas Gohr * either from the conf directory if it exists, otherwise use
1776e5d4768dSAndreas Gohr * file in the template's root directory.
1777e5d4768dSAndreas Gohr *
1778e5d4768dSAndreas Gohr * The function honours config cascade settings and looks for the given
1779e5d4768dSAndreas Gohr * file next to the ´main´ config files, in the order protected, local,
1780e5d4768dSAndreas Gohr * default.
1781e5d4768dSAndreas Gohr *
1782e5d4768dSAndreas Gohr * Note: no escaping or sanity checking is done here. Never pass user input
1783e5d4768dSAndreas Gohr * to this function!
1784e5d4768dSAndreas Gohr *
178542ea7f44SGerrit Uitslag * @param string $file
17864dc42f7fSGerrit Uitslag *
17874dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
17884dc42f7fSGerrit Uitslag * @author Anika Henke <anika@selfthinker.org>
1789e5d4768dSAndreas Gohr */
1790d868eb89SAndreas Gohrfunction tpl_includeFile($file)
1791d868eb89SAndreas Gohr{
1792e5d4768dSAndreas Gohr    global $config_cascade;
179324870174SAndreas Gohr    foreach (['protected', 'local', 'default'] as $config_group) {
1794e5d4768dSAndreas Gohr        if (empty($config_cascade['main'][$config_group])) continue;
1795e5d4768dSAndreas Gohr        foreach ($config_cascade['main'][$config_group] as $conf_file) {
1796e5d4768dSAndreas Gohr            $dir = dirname($conf_file);
1797e5d4768dSAndreas Gohr            if (file_exists("$dir/$file")) {
1798f3a1225fSAnika Henke                include("$dir/$file");
1799e5d4768dSAndreas Gohr                return;
1800e5d4768dSAndreas Gohr            }
1801e5d4768dSAndreas Gohr        }
1802e5d4768dSAndreas Gohr    }
1803e5d4768dSAndreas Gohr
1804e5d4768dSAndreas Gohr    // still here? try the template dir
1805e5d4768dSAndreas Gohr    $file = tpl_incdir() . $file;
1806e5d4768dSAndreas Gohr    if (file_exists($file)) {
1807f3a1225fSAnika Henke        include($file);
1808e5d4768dSAndreas Gohr    }
1809e5d4768dSAndreas Gohr}
1810e5d4768dSAndreas Gohr
1811e5d4768dSAndreas Gohr/**
1812872a6d29SAnika Henke * Returns <link> tag for various icon types (favicon|mobile|generic)
1813872a6d29SAnika Henke *
1814ac7a515fSAndreas Gohr * @param array $types - list of icon types to display (favicon|mobile|generic)
1815ac7a515fSAndreas Gohr * @return string
18164dc42f7fSGerrit Uitslag *
18174dc42f7fSGerrit Uitslag * @author Anika Henke <anika@selfthinker.org>
1818872a6d29SAnika Henke */
1819d868eb89SAndreas Gohrfunction tpl_favicon($types = ['favicon'])
1820d868eb89SAndreas Gohr{
1821872a6d29SAnika Henke
1822872a6d29SAnika Henke    $return = '';
1823872a6d29SAnika Henke
1824872a6d29SAnika Henke    foreach ($types as $type) {
1825872a6d29SAnika Henke        switch ($type) {
1826872a6d29SAnika Henke            case 'favicon':
182724870174SAndreas Gohr                $look = [':wiki:favicon.ico', ':favicon.ico', 'images/favicon.ico'];
1828378325f9SAndreas Gohr                $return .= '<link rel="shortcut icon" href="' . tpl_getMediaFile($look) . '" />' . NL;
1829872a6d29SAnika Henke                break;
1830872a6d29SAnika Henke            case 'mobile':
183124870174SAndreas Gohr                $look = [':wiki:apple-touch-icon.png', ':apple-touch-icon.png', 'images/apple-touch-icon.png'];
1832378325f9SAndreas Gohr                $return .= '<link rel="apple-touch-icon" href="' . tpl_getMediaFile($look) . '" />' . NL;
1833872a6d29SAnika Henke                break;
1834872a6d29SAnika Henke            case 'generic':
1835872a6d29SAnika Henke                // ideal world solution, which doesn't work in any browser yet
183624870174SAndreas Gohr                $look = [':wiki:favicon.svg', ':favicon.svg', 'images/favicon.svg'];
1837378325f9SAndreas Gohr                $return .= '<link rel="icon" href="' . tpl_getMediaFile($look) . '" type="image/svg+xml" />' . NL;
1838872a6d29SAnika Henke                break;
1839872a6d29SAnika Henke        }
1840872a6d29SAnika Henke    }
1841872a6d29SAnika Henke
1842872a6d29SAnika Henke    return $return;
1843afca7e7eSAnika Henke}
1844afca7e7eSAnika Henke
1845d9162c6cSKate Arzamastseva/**
1846d9162c6cSKate Arzamastseva * Prints full-screen media manager
1847d9162c6cSKate Arzamastseva *
1848d9162c6cSKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net>
1849d9162c6cSKate Arzamastseva */
1850d868eb89SAndreas Gohrfunction tpl_media()
1851d868eb89SAndreas Gohr{
1852ac7a515fSAndreas Gohr    global $NS, $IMG, $JUMPTO, $REV, $lang, $fullscreen, $INPUT;
185388a71175SKate Arzamastseva    $fullscreen = true;
185495b451bcSAdrian Lang    require_once DOKU_INC . 'lib/exe/mediamanager.php';
1855d9162c6cSKate Arzamastseva
1856ac7a515fSAndreas Gohr    $rev = '';
1857ac7a515fSAndreas Gohr    $image = cleanID($INPUT->str('image'));
185898f03b57SKate Arzamastseva    if (isset($IMG)) $image = $IMG;
185998f03b57SKate Arzamastseva    if (isset($JUMPTO)) $image = $JUMPTO;
18609c1bd4bcSKate Arzamastseva    if (isset($REV) && !$JUMPTO) $rev = $REV;
186198f03b57SKate Arzamastseva
186294add303SAnika Henke    echo '<div id="mediamanager__page">' . NL;
1863bc314c58SAnika Henke    echo '<h1>' . $lang['btn_media'] . '</h1>' . NL;
1864d9162c6cSKate Arzamastseva    html_msgarea();
186594add303SAnika Henke
186694add303SAnika Henke    echo '<div class="panel namespaces">' . NL;
186794add303SAnika Henke    echo '<h2>' . $lang['namespaces'] . '</h2>' . NL;
186895b451bcSAdrian Lang    echo '<div class="panelHeader">';
1869ba340a70SAnika Henke    echo $lang['media_namespaces'];
187094add303SAnika Henke    echo '</div>' . NL;
187195b451bcSAdrian Lang
187294add303SAnika Henke    echo '<div class="panelContent" id="media__tree">' . NL;
187395b451bcSAdrian Lang    media_nstree($NS);
187494add303SAnika Henke    echo '</div>' . NL;
187594add303SAnika Henke    echo '</div>' . NL;
1876fa8e5c77SKate Arzamastseva
187794add303SAnika Henke    echo '<div class="panel filelist">' . NL;
1878035e07f1SKate Arzamastseva    tpl_mediaFileList();
187994add303SAnika Henke    echo '</div>' . NL;
1880fa8e5c77SKate Arzamastseva
188194add303SAnika Henke    echo '<div class="panel file">' . NL;
188294add303SAnika Henke    echo '<h2 class="a11y">' . $lang['media_file'] . '</h2>' . NL;
1883035e07f1SKate Arzamastseva    tpl_mediaFileDetails($image, $rev);
188494add303SAnika Henke    echo '</div>' . NL;
1885ba340a70SAnika Henke
188694add303SAnika Henke    echo '</div>' . NL;
1887d9162c6cSKate Arzamastseva}
1888afca7e7eSAnika Henke
1889c71db656SAnika Henke/**
1890c71db656SAnika Henke * Return useful layout classes
1891c71db656SAnika Henke *
189242ea7f44SGerrit Uitslag * @return string
18934dc42f7fSGerrit Uitslag *
18944dc42f7fSGerrit Uitslag * @author Anika Henke <anika@selfthinker.org>
1895c71db656SAnika Henke */
1896d868eb89SAndreas Gohrfunction tpl_classes()
1897d868eb89SAndreas Gohr{
1898c71db656SAnika Henke    global $ACT, $conf, $ID, $INFO;
1899585bf44eSChristopher Smith    /** @var Input $INPUT */
1900585bf44eSChristopher Smith    global $INPUT;
1901585bf44eSChristopher Smith
190224870174SAndreas Gohr    $classes = [
1903c71db656SAnika Henke        'dokuwiki',
1904c71db656SAnika Henke        'mode_' . $ACT,
1905c71db656SAnika Henke        'tpl_' . $conf['template'],
1906585bf44eSChristopher Smith        $INPUT->server->bool('REMOTE_USER') ? 'loggedIn' : '',
190790eb1b7bSEduardo Mozart de Oliveira        (isset($INFO['exists']) && $INFO['exists']) ? '' : 'notFound',
190824870174SAndreas Gohr        ($ID == $conf['start']) ? 'home' : ''
190924870174SAndreas Gohr    ];
191024870174SAndreas Gohr    return implode(' ', $classes);
1911c71db656SAnika Henke}
1912c71db656SAnika Henke
191384dd2b1aSGerrit Uitslag/**
191484dd2b1aSGerrit Uitslag * Create event for tools menues
191584dd2b1aSGerrit Uitslag *
191684dd2b1aSGerrit Uitslag * @param string $toolsname name of menu
191784dd2b1aSGerrit Uitslag * @param array $items
191884dd2b1aSGerrit Uitslag * @param string $view e.g. 'main', 'detail', ...
19194dc42f7fSGerrit Uitslag *
19204dc42f7fSGerrit Uitslag * @author Anika Henke <anika@selfthinker.org>
1921affc7ddfSAndreas Gohr * @deprecated 2017-09-01 see devel:menus
192284dd2b1aSGerrit Uitslag */
1923d868eb89SAndreas Gohrfunction tpl_toolsevent($toolsname, $items, $view = 'main')
1924d868eb89SAndreas Gohr{
1925affc7ddfSAndreas Gohr    dbg_deprecated('see devel:menus');
192624870174SAndreas Gohr    $data = ['view' => $view, 'items' => $items];
192784dd2b1aSGerrit Uitslag
192884dd2b1aSGerrit Uitslag    $hook = 'TEMPLATE_' . strtoupper($toolsname) . '_DISPLAY';
1929e1d9dcc8SAndreas Gohr    $evt = new Event($hook, $data);
193084dd2b1aSGerrit Uitslag    if ($evt->advise_before()) {
193124870174SAndreas Gohr        foreach ($evt->data['items'] as $html) echo $html;
193284dd2b1aSGerrit Uitslag    }
193384dd2b1aSGerrit Uitslag    $evt->advise_after();
193484dd2b1aSGerrit Uitslag}
1935