xref: /dokuwiki/inc/confutils.php (revision 6c16a3a9aa602bb7e269fb6d5d18e1353e17f97f)
1b625487dSandi<?php
2d4f83172SAndreas Gohr
3b625487dSandi/**
4b625487dSandi * Utilities for collecting data from config files
5b625487dSandi *
6b625487dSandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7b625487dSandi * @author     Harry Fuecks <[email protected]>
8b625487dSandi */
9b625487dSandi
1010b38f10SChristopher Smith/*
1110b38f10SChristopher Smith * line prefix used to negate single value config items
1210b38f10SChristopher Smith * (scheme.conf & stopwords.conf), e.g.
1310b38f10SChristopher Smith * !gopher
1410b38f10SChristopher Smith */
15e1d9dcc8SAndreas Gohr
16e1d9dcc8SAndreas Gohruse dokuwiki\Extension\AuthPlugin;
17e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event;
18d4f83172SAndreas Gohr
1910b38f10SChristopher Smithconst DOKU_CONF_NEGATION = '!';
20b625487dSandi
21b625487dSandi/**
22b625487dSandi * Returns the (known) extension and mimetype of a given filename
23b625487dSandi *
2427bf7924STom N Harris * If $knownonly is true (the default), then only known extensions
2527bf7924STom N Harris * are returned.
2627bf7924STom N Harris *
27b625487dSandi * @author Andreas Gohr <[email protected]>
2842ea7f44SGerrit Uitslag *
2942ea7f44SGerrit Uitslag * @param string $file file name
3042ea7f44SGerrit Uitslag * @param bool   $knownonly
3142ea7f44SGerrit Uitslag * @return array with extension, mimetype and if it should be downloaded
32b625487dSandi */
33d868eb89SAndreas Gohrfunction mimetype($file, $knownonly = true)
34d868eb89SAndreas Gohr{
35b625487dSandi    $mtypes = getMimeTypes();     // known mimetypes
36ad74fe66SAdrian Lang    $ext    = strrpos($file, '.');
37ad74fe66SAdrian Lang    if ($ext === false) {
3824870174SAndreas Gohr        return [false, false, false];
3927bf7924STom N Harris    }
40ad74fe66SAdrian Lang    $ext = strtolower(substr($file, $ext + 1));
41ad74fe66SAdrian Lang    if (!isset($mtypes[$ext])) {
42ad74fe66SAdrian Lang        if ($knownonly) {
4324870174SAndreas Gohr            return [false, false, false];
44ecebf3a8SAndreas Gohr        } else {
4524870174SAndreas Gohr            return [$ext, 'application/octet-stream', true];
4627bf7924STom N Harris        }
47b625487dSandi    }
48ad74fe66SAdrian Lang    if ($mtypes[$ext][0] == '!') {
4924870174SAndreas Gohr        return [$ext, substr($mtypes[$ext], 1), true];
50ad74fe66SAdrian Lang    } else {
5124870174SAndreas Gohr        return [$ext, $mtypes[$ext], false];
52ad74fe66SAdrian Lang    }
53b625487dSandi}
54b625487dSandi
55b625487dSandi/**
56b625487dSandi * returns a hash of mimetypes
57b625487dSandi *
58b625487dSandi * @author Andreas Gohr <[email protected]>
59b625487dSandi */
60d868eb89SAndreas Gohrfunction getMimeTypes()
61d868eb89SAndreas Gohr{
6249eb6e38SAndreas Gohr    static $mime = null;
63b625487dSandi    if (!$mime) {
64cb043f52SChris Smith        $mime = retrieveConfig('mime', 'confToHash');
6545ae4bb8SChristopher Smith        $mime = array_filter($mime);
66b625487dSandi    }
67b625487dSandi    return $mime;
68b625487dSandi}
69b625487dSandi
70b625487dSandi/**
71b625487dSandi * returns a hash of acronyms
72b625487dSandi *
73b625487dSandi * @author Harry Fuecks <[email protected]>
74b625487dSandi */
75d868eb89SAndreas Gohrfunction getAcronyms()
76d868eb89SAndreas Gohr{
7749eb6e38SAndreas Gohr    static $acronyms = null;
78b625487dSandi    if (!$acronyms) {
79cb043f52SChris Smith        $acronyms = retrieveConfig('acronyms', 'confToHash');
80f266a919SChristopher Smith        $acronyms = array_filter($acronyms, 'strlen');
81b625487dSandi    }
82b625487dSandi    return $acronyms;
83b625487dSandi}
84b625487dSandi
85b625487dSandi/**
86b625487dSandi * returns a hash of smileys
87b625487dSandi *
88b625487dSandi * @author Harry Fuecks <[email protected]>
89b625487dSandi */
90d868eb89SAndreas Gohrfunction getSmileys()
91d868eb89SAndreas Gohr{
9249eb6e38SAndreas Gohr    static $smileys = null;
93b625487dSandi    if (!$smileys) {
94cb043f52SChris Smith        $smileys = retrieveConfig('smileys', 'confToHash');
95f266a919SChristopher Smith        $smileys = array_filter($smileys, 'strlen');
96b625487dSandi    }
97b625487dSandi    return $smileys;
98b625487dSandi}
99b625487dSandi
100b625487dSandi/**
101b625487dSandi * returns a hash of entities
102b625487dSandi *
103b625487dSandi * @author Harry Fuecks <[email protected]>
104b625487dSandi */
105d868eb89SAndreas Gohrfunction getEntities()
106d868eb89SAndreas Gohr{
10749eb6e38SAndreas Gohr    static $entities = null;
108b625487dSandi    if (!$entities) {
109cb043f52SChris Smith        $entities = retrieveConfig('entities', 'confToHash');
110f266a919SChristopher Smith        $entities = array_filter($entities, 'strlen');
111b625487dSandi    }
112b625487dSandi    return $entities;
113b625487dSandi}
114b625487dSandi
115b625487dSandi/**
116b625487dSandi * returns a hash of interwikilinks
117b625487dSandi *
118b625487dSandi * @author Harry Fuecks <[email protected]>
119b625487dSandi */
120d868eb89SAndreas Gohrfunction getInterwiki()
121d868eb89SAndreas Gohr{
12249eb6e38SAndreas Gohr    static $wikis = null;
123b625487dSandi    if (!$wikis) {
12424870174SAndreas Gohr        $wikis = retrieveConfig('interwiki', 'confToHash', [true]);
125f266a919SChristopher Smith        $wikis = array_filter($wikis, 'strlen');
12645ae4bb8SChristopher Smith
12797a3e4e3Sandi        //add sepecial case 'this'
12827a2b085Sandi        $wikis['this'] = DOKU_URL . '{NAME}';
12945ae4bb8SChristopher Smith    }
130b625487dSandi    return $wikis;
131b625487dSandi}
132b625487dSandi
133b625487dSandi/**
134fa078663SAndreas Gohr * Returns the jquery script URLs for the versions defined in lib/scripts/jquery/versions
13561537d47SAndreas Gohr *
136fa078663SAndreas Gohr * @trigger CONFUTIL_CDN_SELECT
13761537d47SAndreas Gohr * @return array
13861537d47SAndreas Gohr */
139d868eb89SAndreas Gohrfunction getCdnUrls()
140d868eb89SAndreas Gohr{
141fa078663SAndreas Gohr    global $conf;
142fa078663SAndreas Gohr
143fa078663SAndreas Gohr    // load version info
14424870174SAndreas Gohr    $versions = [];
14561537d47SAndreas Gohr    $lines = file(DOKU_INC . 'lib/scripts/jquery/versions');
14661537d47SAndreas Gohr    foreach ($lines as $line) {
1476453acd5SAndreas Gohr        $line = trim(preg_replace('/#.*$/', '', $line));
1486453acd5SAndreas Gohr        if ($line === '') continue;
14924870174SAndreas Gohr        [$key, $val] = sexplode('=', $line, 2, '');
15061537d47SAndreas Gohr        $key = trim($key);
15161537d47SAndreas Gohr        $val = trim($val);
15261537d47SAndreas Gohr        $versions[$key] = $val;
15361537d47SAndreas Gohr    }
154fa078663SAndreas Gohr
15524870174SAndreas Gohr    $src = [];
15624870174SAndreas Gohr    $data = ['versions' => $versions, 'src' => &$src];
157e1d9dcc8SAndreas Gohr    $event = new Event('CONFUTIL_CDN_SELECT', $data);
158fa078663SAndreas Gohr    if ($event->advise_before()) {
159fa078663SAndreas Gohr        if (!$conf['jquerycdn']) {
16024870174SAndreas Gohr            $jqmod = md5(implode('-', $versions));
161fa078663SAndreas Gohr            $src[] = DOKU_BASE . 'lib/exe/jquery.php' . '?tseed=' . $jqmod;
162fa078663SAndreas Gohr        } elseif ($conf['jquerycdn'] == 'jquery') {
163fa078663SAndreas Gohr            $src[] = sprintf('https://code.jquery.com/jquery-%s.min.js', $versions['JQ_VERSION']);
164fa078663SAndreas Gohr            $src[] = sprintf('https://code.jquery.com/ui/%s/jquery-ui.min.js', $versions['JQUI_VERSION']);
165fa078663SAndreas Gohr        } elseif ($conf['jquerycdn'] == 'cdnjs') {
16664159a61SAndreas Gohr            $src[] = sprintf(
16764159a61SAndreas Gohr                'https://cdnjs.cloudflare.com/ajax/libs/jquery/%s/jquery.min.js',
16864159a61SAndreas Gohr                $versions['JQ_VERSION']
16964159a61SAndreas Gohr            );
17064159a61SAndreas Gohr            $src[] = sprintf(
17164159a61SAndreas Gohr                'https://cdnjs.cloudflare.com/ajax/libs/jqueryui/%s/jquery-ui.min.js',
17264159a61SAndreas Gohr                $versions['JQUI_VERSION']
17364159a61SAndreas Gohr            );
174fa078663SAndreas Gohr        }
175fa078663SAndreas Gohr    }
176fa078663SAndreas Gohr    $event->advise_after();
177fa078663SAndreas Gohr
178fa078663SAndreas Gohr    return $src;
17961537d47SAndreas Gohr}
18061537d47SAndreas Gohr
18161537d47SAndreas Gohr/**
182b9ac8716Schris * returns array of wordblock patterns
183b9ac8716Schris *
184b9ac8716Schris */
185d868eb89SAndreas Gohrfunction getWordblocks()
186d868eb89SAndreas Gohr{
18749eb6e38SAndreas Gohr    static $wordblocks = null;
188b9ac8716Schris    if (!$wordblocks) {
1894c353447SChristopher Smith        $wordblocks = retrieveConfig('wordblock', 'file', null, 'array_merge_with_removal');
190b9ac8716Schris    }
191b9ac8716Schris    return $wordblocks;
192b9ac8716Schris}
193b9ac8716Schris
194e3ab6fc5SMichael Hamann/**
195e3ab6fc5SMichael Hamann * Gets the list of configured schemes
196e3ab6fc5SMichael Hamann *
197e3ab6fc5SMichael Hamann * @return array the schemes
198e3ab6fc5SMichael Hamann */
199d868eb89SAndreas Gohrfunction getSchemes()
200d868eb89SAndreas Gohr{
20149eb6e38SAndreas Gohr    static $schemes = null;
20236f2d7c1SGina Haeussge    if (!$schemes) {
2034c353447SChristopher Smith        $schemes = retrieveConfig('scheme', 'file', null, 'array_merge_with_removal');
20436f2d7c1SGina Haeussge        $schemes = array_map('trim', $schemes);
20536f2d7c1SGina Haeussge        $schemes = preg_replace('/^#.*/', '', $schemes);
20636f2d7c1SGina Haeussge        $schemes = array_filter($schemes);
2074c353447SChristopher Smith    }
20836f2d7c1SGina Haeussge    return $schemes;
20936f2d7c1SGina Haeussge}
21036f2d7c1SGina Haeussge
211b9ac8716Schris/**
212edcb01e5SGina Haeussge * Builds a hash from an array of lines
213b625487dSandi *
2143fd0b676Sandi * If $lower is set to true all hash keys are converted to
2153fd0b676Sandi * lower case.
2163fd0b676Sandi *
217b625487dSandi * @author Harry Fuecks <[email protected]>
2183fd0b676Sandi * @author Andreas Gohr <[email protected]>
219edcb01e5SGina Haeussge * @author Gina Haeussge <[email protected]>
220f50a239bSTakamura *
221f50a239bSTakamura * @param array $lines
222f50a239bSTakamura * @param bool $lower
223f50a239bSTakamura *
224f50a239bSTakamura * @return array
225b625487dSandi */
226d868eb89SAndreas Gohrfunction linesToHash($lines, $lower = false)
227d868eb89SAndreas Gohr{
22824870174SAndreas Gohr    $conf = [];
229dd74fecfSMichael Hamann    // remove BOM
230*6c16a3a9Sfiwswe    if (isset($lines[0]) && str_starts_with($lines[0], pack('CCC', 0xef, 0xbb, 0xbf)))
231dd74fecfSMichael Hamann        $lines[0] = substr($lines[0], 3);
232b625487dSandi    foreach ($lines as $line) {
23303ff8795SAndreas Gohr        //ignore comments (except escaped ones)
23403ff8795SAndreas Gohr        $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line);
23503ff8795SAndreas Gohr        $line = str_replace('\\#', '#', $line);
236b625487dSandi        $line = trim($line);
23759ed97f2SAndreas Gohr        if ($line === '') continue;
238b625487dSandi        $line = preg_split('/\s+/', $line, 2);
23959ed97f2SAndreas Gohr        $line = array_pad($line, 2, '');
240b625487dSandi        // Build the associative array
24127a2b085Sandi        if ($lower) {
24227a2b085Sandi            $conf[strtolower($line[0])] = $line[1];
24327a2b085Sandi        } else {
244b625487dSandi            $conf[$line[0]] = $line[1];
245b625487dSandi        }
24627a2b085Sandi    }
247b625487dSandi
248b625487dSandi    return $conf;
249b625487dSandi}
250b625487dSandi
251409d7af7SAndreas Gohr/**
252edcb01e5SGina Haeussge * Builds a hash from a configfile
253edcb01e5SGina Haeussge *
254edcb01e5SGina Haeussge * If $lower is set to true all hash keys are converted to
255edcb01e5SGina Haeussge * lower case.
256edcb01e5SGina Haeussge *
257edcb01e5SGina Haeussge * @author Harry Fuecks <[email protected]>
258edcb01e5SGina Haeussge * @author Andreas Gohr <[email protected]>
259edcb01e5SGina Haeussge * @author Gina Haeussge <[email protected]>
260f50a239bSTakamura *
261f50a239bSTakamura * @param string $file
262f50a239bSTakamura * @param bool $lower
263f50a239bSTakamura *
264f50a239bSTakamura * @return array
265edcb01e5SGina Haeussge */
266d868eb89SAndreas Gohrfunction confToHash($file, $lower = false)
267d868eb89SAndreas Gohr{
26824870174SAndreas Gohr    $conf = [];
269edcb01e5SGina Haeussge    $lines = @file($file);
270edcb01e5SGina Haeussge    if (!$lines) return $conf;
271edcb01e5SGina Haeussge
272edcb01e5SGina Haeussge    return linesToHash($lines, $lower);
273edcb01e5SGina Haeussge}
274edcb01e5SGina Haeussge
275edcb01e5SGina Haeussge/**
276c9071834SMichael Große * Read a json config file into an array
277c9071834SMichael Große *
278c9071834SMichael Große * @param string $file
279c9071834SMichael Große * @return array
2804dc42f7fSGerrit Uitslag * @throws JsonException
281c9071834SMichael Große */
282c9071834SMichael Großefunction jsonToArray($file)
283c9071834SMichael Große{
284c9071834SMichael Große    $json = file_get_contents($file);
285c9071834SMichael Große
28624870174SAndreas Gohr    $conf = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
287c9071834SMichael Große
288dceb2cc1SMichael Große    if ($conf === null) {
289c9071834SMichael Große        return [];
290c9071834SMichael Große    }
291c9071834SMichael Große
292c9071834SMichael Große    return $conf;
293c9071834SMichael Große}
294c9071834SMichael Große
295c9071834SMichael Große/**
296cb043f52SChris Smith * Retrieve the requested configuration information
297cb043f52SChris Smith *
298cb043f52SChris Smith * @author Chris Smith <[email protected]>
299cb043f52SChris Smith *
300cb043f52SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
301cb043f52SChris Smith * @param  callback $fn       the function used to process the configuration file into an array
302e3ab6fc5SMichael Hamann * @param  array    $params   optional additional params to pass to the callback
3035a9597bbSTakamura * @param  callback $combine  the function used to combine arrays of values read from different configuration files;
304074b2b3fSTakamura *                            the function takes two parameters,
305074b2b3fSTakamura *                               $combined - the already read & merged configuration values
306074b2b3fSTakamura *                               $new - array of config values from the config cascade file being currently processed
307074b2b3fSTakamura *                            and returns an array of the merged configuration values.
308cb043f52SChris Smith * @return array    configuration values
309cb043f52SChris Smith */
310d868eb89SAndreas Gohrfunction retrieveConfig($type, $fn, $params = null, $combine = 'array_merge')
311d868eb89SAndreas Gohr{
312cb043f52SChris Smith    global $config_cascade;
313cb043f52SChris Smith
31424870174SAndreas Gohr    if (!is_array($params)) $params = [];
3154c6a5eccSAndreas Gohr
31624870174SAndreas Gohr    $combined = [];
317cb043f52SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "' . $type . '"', E_USER_WARNING);
31824870174SAndreas Gohr    foreach (['default', 'local', 'protected'] as $config_group) {
319b303b92cSChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
320b303b92cSChris Smith        foreach ($config_cascade[$type][$config_group] as $file) {
32179e79377SAndreas Gohr            if (file_exists($file)) {
32224870174SAndreas Gohr                $config = call_user_func_array($fn, array_merge([$file], $params));
3234c353447SChristopher Smith                $combined = $combine($combined, $config);
324cb043f52SChris Smith            }
325cb043f52SChris Smith        }
326b303b92cSChris Smith    }
327cb043f52SChris Smith
328cb043f52SChris Smith    return $combined;
329cb043f52SChris Smith}
330cb043f52SChris Smith
331cb043f52SChris Smith/**
332f8121585SChris Smith * Include the requested configuration information
333f8121585SChris Smith *
334f8121585SChris Smith * @author Chris Smith <[email protected]>
335f8121585SChris Smith *
336f8121585SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
337f8121585SChris Smith * @return array              list of files, default before local before protected
338f8121585SChris Smith */
339d868eb89SAndreas Gohrfunction getConfigFiles($type)
340d868eb89SAndreas Gohr{
341f8121585SChris Smith    global $config_cascade;
34224870174SAndreas Gohr    $files = [];
343f8121585SChris Smith
344f8121585SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "' . $type . '"', E_USER_WARNING);
34524870174SAndreas Gohr    foreach (['default', 'local', 'protected'] as $config_group) {
346f8121585SChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
347f8121585SChris Smith        $files = array_merge($files, $config_cascade[$type][$config_group]);
348f8121585SChris Smith    }
349f8121585SChris Smith
350f8121585SChris Smith    return $files;
351f8121585SChris Smith}
352f8121585SChris Smith
353f8121585SChris Smith/**
354409d7af7SAndreas Gohr * check if the given action was disabled in config
355409d7af7SAndreas Gohr *
356409d7af7SAndreas Gohr * @author Andreas Gohr <[email protected]>
3570e2431b7SGerrit Uitslag * @param string $action
358409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled
359409d7af7SAndreas Gohr */
360d868eb89SAndreas Gohrfunction actionOK($action)
361d868eb89SAndreas Gohr{
362409d7af7SAndreas Gohr    static $disabled = null;
363020ea9e1SChristopher Smith    if (is_null($disabled) || defined('SIMPLE_TEST')) {
364409d7af7SAndreas Gohr        global $conf;
365e1d9dcc8SAndreas Gohr        /** @var AuthPlugin $auth */
366de4d479aSAdrian Lang        global $auth;
367409d7af7SAndreas Gohr
368409d7af7SAndreas Gohr        // prepare disabled actions array and handle legacy options
369409d7af7SAndreas Gohr        $disabled = explode(',', $conf['disableactions']);
370409d7af7SAndreas Gohr        $disabled = array_map('trim', $disabled);
37171c734a9SGerrit Uitslag        if (
37271c734a9SGerrit Uitslag            (isset($conf['openregister']) && !$conf['openregister']) || !$auth instanceof AuthPlugin
37371c734a9SGerrit Uitslag            || !$auth->canDo('addUser')
37471c734a9SGerrit Uitslag        ) {
375de4d479aSAdrian Lang            $disabled[] = 'register';
376de4d479aSAdrian Lang        }
37771c734a9SGerrit Uitslag        if (
37871c734a9SGerrit Uitslag            (isset($conf['resendpasswd']) && !$conf['resendpasswd']) || !$auth instanceof AuthPlugin
37971c734a9SGerrit Uitslag            || !$auth->canDo('modPass')
38071c734a9SGerrit Uitslag        ) {
381de4d479aSAdrian Lang            $disabled[] = 'resendpwd';
382de4d479aSAdrian Lang        }
3836547cfc7SGerrit Uitslag        if ((isset($conf['subscribers']) && !$conf['subscribers']) || !$auth instanceof AuthPlugin) {
3843a48618aSAnika Henke            $disabled[] = 'subscribe';
3853a48618aSAnika Henke        }
3866547cfc7SGerrit Uitslag        if (!$auth instanceof AuthPlugin || !$auth->canDo('Profile')) {
3873a48618aSAnika Henke            $disabled[] = 'profile';
3883a48618aSAnika Henke        }
3896547cfc7SGerrit Uitslag        if (!$auth instanceof AuthPlugin || !$auth->canDo('delUser')) {
3902a7abf2dSChristopher Smith            $disabled[] = 'profile_delete';
3912a7abf2dSChristopher Smith        }
3926547cfc7SGerrit Uitslag        if (!$auth instanceof AuthPlugin) {
3933a48618aSAnika Henke            $disabled[] = 'login';
3943a48618aSAnika Henke        }
3956547cfc7SGerrit Uitslag        if (!$auth instanceof AuthPlugin || !$auth->canDo('logout')) {
3963a48618aSAnika Henke            $disabled[] = 'logout';
3973a48618aSAnika Henke        }
398409d7af7SAndreas Gohr        $disabled = array_unique($disabled);
399409d7af7SAndreas Gohr    }
400409d7af7SAndreas Gohr
401409d7af7SAndreas Gohr    return !in_array($action, $disabled);
402409d7af7SAndreas Gohr}
403409d7af7SAndreas Gohr
404fe9ec250SChris Smith/**
405fe9ec250SChris Smith * check if headings should be used as link text for the specified link type
406fe9ec250SChris Smith *
407fe9ec250SChris Smith * @author Chris Smith <[email protected]>
408fe9ec250SChris Smith *
409fe9ec250SChris Smith * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
410fe9ec250SChris Smith *                                                      navigation applies to all other links
411e3ab6fc5SMichael Hamann * @return  boolean             true if headings should be used for $linktype, false otherwise
412fe9ec250SChris Smith */
413d868eb89SAndreas Gohrfunction useHeading($linktype)
414d868eb89SAndreas Gohr{
415fe9ec250SChris Smith    static $useHeading = null;
4166506eaacSAndreas Gohr    if (defined('DOKU_UNITTEST')) $useHeading = null; // don't cache during unit tests
417fe9ec250SChris Smith
418fe9ec250SChris Smith    if (is_null($useHeading)) {
419fe9ec250SChris Smith        global $conf;
420fe9ec250SChris Smith
421fe9ec250SChris Smith        if (!empty($conf['useheading'])) {
422fe9ec250SChris Smith            switch ($conf['useheading']) {
42349eb6e38SAndreas Gohr                case 'content':
42449eb6e38SAndreas Gohr                    $useHeading['content'] = true;
42549eb6e38SAndreas Gohr                    break;
42649eb6e38SAndreas Gohr
42749eb6e38SAndreas Gohr                case 'navigation':
42849eb6e38SAndreas Gohr                    $useHeading['navigation'] = true;
42949eb6e38SAndreas Gohr                    break;
430fe9ec250SChris Smith                default:
431fe9ec250SChris Smith                    $useHeading['content'] = true;
432fe9ec250SChris Smith                    $useHeading['navigation'] = true;
433fe9ec250SChris Smith            }
434fe9ec250SChris Smith        } else {
43524870174SAndreas Gohr            $useHeading = [];
436fe9ec250SChris Smith        }
437fe9ec250SChris Smith    }
438fe9ec250SChris Smith
439fe9ec250SChris Smith    return (!empty($useHeading[$linktype]));
440fe9ec250SChris Smith}
441fe9ec250SChris Smith
4423994772aSChris Smith/**
4433994772aSChris Smith * obscure config data so information isn't plain text
4443994772aSChris Smith *
4453994772aSChris Smith * @param string       $str     data to be encoded
4463994772aSChris Smith * @param string       $code    encoding method, values: plain, base64, uuencode.
4473994772aSChris Smith * @return string               the encoded value
4483994772aSChris Smith */
449d868eb89SAndreas Gohrfunction conf_encodeString($str, $code)
450d868eb89SAndreas Gohr{
4513994772aSChris Smith    switch ($code) {
45262ad2d27SAndreas Gohr        case 'base64':
45362ad2d27SAndreas Gohr            return '<b>' . base64_encode($str);
45462ad2d27SAndreas Gohr        case 'uuencode':
45562ad2d27SAndreas Gohr            return '<u>' . convert_uuencode($str);
4563994772aSChris Smith        case 'plain':
4573994772aSChris Smith        default:
4583994772aSChris Smith            return $str;
4593994772aSChris Smith    }
4603994772aSChris Smith}
4613994772aSChris Smith/**
4623994772aSChris Smith * return obscured data as plain text
4633994772aSChris Smith *
4643994772aSChris Smith * @param  string      $str   encoded data
4653994772aSChris Smith * @return string             plain text
4663994772aSChris Smith */
467d868eb89SAndreas Gohrfunction conf_decodeString($str)
468d868eb89SAndreas Gohr{
4693994772aSChris Smith    switch (substr($str, 0, 3)) {
47062ad2d27SAndreas Gohr        case '<b>':
47162ad2d27SAndreas Gohr            return base64_decode(substr($str, 3));
47262ad2d27SAndreas Gohr        case '<u>':
47362ad2d27SAndreas Gohr            return convert_uudecode(substr($str, 3));
474b96ff25bSElan Ruusamäe        default:  // not encoded (or unknown)
4753994772aSChris Smith            return $str;
4763994772aSChris Smith    }
4773994772aSChris Smith}
4784c353447SChristopher Smith
4794c353447SChristopher Smith/**
4804c353447SChristopher Smith * array combination function to remove negated values (prefixed by !)
4814c353447SChristopher Smith *
4824c353447SChristopher Smith * @param  array $current
4834c353447SChristopher Smith * @param  array $new
4844c353447SChristopher Smith *
4854c353447SChristopher Smith * @return array the combined array, numeric keys reset
4864c353447SChristopher Smith */
487d868eb89SAndreas Gohrfunction array_merge_with_removal($current, $new)
488d868eb89SAndreas Gohr{
4894c353447SChristopher Smith    foreach ($new as $val) {
490*6c16a3a9Sfiwswe        if (str_starts_with($val, DOKU_CONF_NEGATION)) {
4913a7669bdSChristopher Smith            $idx = array_search(trim(substr($val, 1)), $current);
4924c353447SChristopher Smith            if ($idx !== false) {
4934c353447SChristopher Smith                unset($current[$idx]);
4944c353447SChristopher Smith            }
4954c353447SChristopher Smith        } else {
4963a7669bdSChristopher Smith            $current[] = trim($val);
4974c353447SChristopher Smith        }
4984c353447SChristopher Smith    }
4994c353447SChristopher Smith
5004c353447SChristopher Smith    return array_slice($current, 0);
5014c353447SChristopher Smith}
502e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
503