xref: /dokuwiki/inc/confutils.php (revision 62ad2d27a5bf130380e25f6ac29b983d8e8cf063)
1b625487dSandi<?php
2b625487dSandi/**
3b625487dSandi * Utilities for collecting data from config files
4b625487dSandi *
5b625487dSandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6b625487dSandi * @author     Harry Fuecks <hfuecks@gmail.com>
7b625487dSandi */
8b625487dSandi
910b38f10SChristopher Smith/*
1010b38f10SChristopher Smith * line prefix used to negate single value config items
1110b38f10SChristopher Smith * (scheme.conf & stopwords.conf), e.g.
1210b38f10SChristopher Smith * !gopher
1310b38f10SChristopher Smith */
14e1d9dcc8SAndreas Gohr
15e1d9dcc8SAndreas Gohruse dokuwiki\Extension\AuthPlugin;
16e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event;
1710b38f10SChristopher Smithconst DOKU_CONF_NEGATION = '!';
18b625487dSandi
19b625487dSandi/**
20b625487dSandi * Returns the (known) extension and mimetype of a given filename
21b625487dSandi *
2227bf7924STom N Harris * If $knownonly is true (the default), then only known extensions
2327bf7924STom N Harris * are returned.
2427bf7924STom N Harris *
25b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
2642ea7f44SGerrit Uitslag *
2742ea7f44SGerrit Uitslag * @param string $file file name
2842ea7f44SGerrit Uitslag * @param bool   $knownonly
2942ea7f44SGerrit Uitslag * @return array with extension, mimetype and if it should be downloaded
30b625487dSandi */
31d868eb89SAndreas Gohrfunction mimetype($file, $knownonly = true)
32d868eb89SAndreas Gohr{
33b625487dSandi    $mtypes = getMimeTypes();     // known mimetypes
34ad74fe66SAdrian Lang    $ext    = strrpos($file, '.');
35ad74fe66SAdrian Lang    if ($ext === false) {
3624870174SAndreas Gohr        return [false, false, false];
3727bf7924STom N Harris    }
38ad74fe66SAdrian Lang    $ext = strtolower(substr($file, $ext + 1));
39ad74fe66SAdrian Lang    if (!isset($mtypes[$ext])) {
40ad74fe66SAdrian Lang        if ($knownonly) {
4124870174SAndreas Gohr            return [false, false, false];
42ecebf3a8SAndreas Gohr        } else {
4324870174SAndreas Gohr            return [$ext, 'application/octet-stream', true];
4427bf7924STom N Harris        }
45b625487dSandi    }
46ad74fe66SAdrian Lang    if ($mtypes[$ext][0] == '!') {
4724870174SAndreas Gohr        return [$ext, substr($mtypes[$ext], 1), true];
48ad74fe66SAdrian Lang    } else {
4924870174SAndreas Gohr        return [$ext, $mtypes[$ext], false];
50ad74fe66SAdrian Lang    }
51b625487dSandi}
52b625487dSandi
53b625487dSandi/**
54b625487dSandi * returns a hash of mimetypes
55b625487dSandi *
56b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
57b625487dSandi */
58d868eb89SAndreas Gohrfunction getMimeTypes()
59d868eb89SAndreas Gohr{
6049eb6e38SAndreas Gohr    static $mime = null;
61b625487dSandi    if (!$mime) {
62cb043f52SChris Smith        $mime = retrieveConfig('mime', 'confToHash');
6345ae4bb8SChristopher Smith        $mime = array_filter($mime);
64b625487dSandi    }
65b625487dSandi    return $mime;
66b625487dSandi}
67b625487dSandi
68b625487dSandi/**
69b625487dSandi * returns a hash of acronyms
70b625487dSandi *
71b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
72b625487dSandi */
73d868eb89SAndreas Gohrfunction getAcronyms()
74d868eb89SAndreas Gohr{
7549eb6e38SAndreas Gohr    static $acronyms = null;
76b625487dSandi    if (!$acronyms) {
77cb043f52SChris Smith        $acronyms = retrieveConfig('acronyms', 'confToHash');
78f266a919SChristopher Smith        $acronyms = array_filter($acronyms, 'strlen');
79b625487dSandi    }
80b625487dSandi    return $acronyms;
81b625487dSandi}
82b625487dSandi
83b625487dSandi/**
84b625487dSandi * returns a hash of smileys
85b625487dSandi *
86b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
87b625487dSandi */
88d868eb89SAndreas Gohrfunction getSmileys()
89d868eb89SAndreas Gohr{
9049eb6e38SAndreas Gohr    static $smileys = null;
91b625487dSandi    if (!$smileys) {
92cb043f52SChris Smith        $smileys = retrieveConfig('smileys', 'confToHash');
93f266a919SChristopher Smith        $smileys = array_filter($smileys, 'strlen');
94b625487dSandi    }
95b625487dSandi    return $smileys;
96b625487dSandi}
97b625487dSandi
98b625487dSandi/**
99b625487dSandi * returns a hash of entities
100b625487dSandi *
101b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
102b625487dSandi */
103d868eb89SAndreas Gohrfunction getEntities()
104d868eb89SAndreas Gohr{
10549eb6e38SAndreas Gohr    static $entities = null;
106b625487dSandi    if (!$entities) {
107cb043f52SChris Smith        $entities = retrieveConfig('entities', 'confToHash');
108f266a919SChristopher Smith        $entities = array_filter($entities, 'strlen');
109b625487dSandi    }
110b625487dSandi    return $entities;
111b625487dSandi}
112b625487dSandi
113b625487dSandi/**
114b625487dSandi * returns a hash of interwikilinks
115b625487dSandi *
116b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
117b625487dSandi */
118d868eb89SAndreas Gohrfunction getInterwiki()
119d868eb89SAndreas Gohr{
12049eb6e38SAndreas Gohr    static $wikis = null;
121b625487dSandi    if (!$wikis) {
12224870174SAndreas Gohr        $wikis = retrieveConfig('interwiki', 'confToHash', [true]);
123f266a919SChristopher Smith        $wikis = array_filter($wikis, 'strlen');
12445ae4bb8SChristopher Smith
12597a3e4e3Sandi        //add sepecial case 'this'
12627a2b085Sandi        $wikis['this'] = DOKU_URL.'{NAME}';
12745ae4bb8SChristopher Smith    }
128b625487dSandi    return $wikis;
129b625487dSandi}
130b625487dSandi
131b625487dSandi/**
132fa078663SAndreas Gohr * Returns the jquery script URLs for the versions defined in lib/scripts/jquery/versions
13361537d47SAndreas Gohr *
134fa078663SAndreas Gohr * @trigger CONFUTIL_CDN_SELECT
13561537d47SAndreas Gohr * @return array
13661537d47SAndreas Gohr */
137d868eb89SAndreas Gohrfunction getCdnUrls()
138d868eb89SAndreas Gohr{
139fa078663SAndreas Gohr    global $conf;
140fa078663SAndreas Gohr
141fa078663SAndreas Gohr    // load version info
14224870174SAndreas Gohr    $versions = [];
14361537d47SAndreas Gohr    $lines = file(DOKU_INC . 'lib/scripts/jquery/versions');
14461537d47SAndreas Gohr    foreach ($lines as $line) {
1456453acd5SAndreas Gohr        $line = trim(preg_replace('/#.*$/', '', $line));
1466453acd5SAndreas Gohr        if ($line === '') continue;
14724870174SAndreas Gohr        [$key, $val] = sexplode('=', $line, 2, '');
14861537d47SAndreas Gohr        $key = trim($key);
14961537d47SAndreas Gohr        $val = trim($val);
15061537d47SAndreas Gohr        $versions[$key] = $val;
15161537d47SAndreas Gohr    }
152fa078663SAndreas Gohr
15324870174SAndreas Gohr    $src = [];
15424870174SAndreas Gohr    $data = ['versions' => $versions, 'src' => &$src];
155e1d9dcc8SAndreas Gohr    $event = new Event('CONFUTIL_CDN_SELECT', $data);
156fa078663SAndreas Gohr    if ($event->advise_before()) {
157fa078663SAndreas Gohr        if (!$conf['jquerycdn']) {
15824870174SAndreas Gohr            $jqmod = md5(implode('-', $versions));
159fa078663SAndreas Gohr            $src[] = DOKU_BASE . 'lib/exe/jquery.php' . '?tseed=' . $jqmod;
160fa078663SAndreas Gohr        } elseif ($conf['jquerycdn'] == 'jquery') {
161fa078663SAndreas Gohr            $src[] = sprintf('https://code.jquery.com/jquery-%s.min.js', $versions['JQ_VERSION']);
162fa078663SAndreas Gohr            $src[] = sprintf('https://code.jquery.com/ui/%s/jquery-ui.min.js', $versions['JQUI_VERSION']);
163fa078663SAndreas Gohr        } elseif ($conf['jquerycdn'] == 'cdnjs') {
16464159a61SAndreas Gohr            $src[] = sprintf(
16564159a61SAndreas Gohr                'https://cdnjs.cloudflare.com/ajax/libs/jquery/%s/jquery.min.js',
16664159a61SAndreas Gohr                $versions['JQ_VERSION']
16764159a61SAndreas Gohr            );
16864159a61SAndreas Gohr            $src[] = sprintf(
16964159a61SAndreas Gohr                'https://cdnjs.cloudflare.com/ajax/libs/jqueryui/%s/jquery-ui.min.js',
17064159a61SAndreas Gohr                $versions['JQUI_VERSION']
17164159a61SAndreas Gohr            );
172fa078663SAndreas Gohr        }
173fa078663SAndreas Gohr    }
174fa078663SAndreas Gohr    $event->advise_after();
175fa078663SAndreas Gohr
176fa078663SAndreas Gohr    return $src;
17761537d47SAndreas Gohr}
17861537d47SAndreas Gohr
17961537d47SAndreas Gohr/**
180b9ac8716Schris * returns array of wordblock patterns
181b9ac8716Schris *
182b9ac8716Schris */
183d868eb89SAndreas Gohrfunction getWordblocks()
184d868eb89SAndreas Gohr{
18549eb6e38SAndreas Gohr    static $wordblocks = null;
186b9ac8716Schris    if (!$wordblocks) {
1874c353447SChristopher Smith        $wordblocks = retrieveConfig('wordblock', 'file', null, 'array_merge_with_removal');
188b9ac8716Schris    }
189b9ac8716Schris    return $wordblocks;
190b9ac8716Schris}
191b9ac8716Schris
192e3ab6fc5SMichael Hamann/**
193e3ab6fc5SMichael Hamann * Gets the list of configured schemes
194e3ab6fc5SMichael Hamann *
195e3ab6fc5SMichael Hamann * @return array the schemes
196e3ab6fc5SMichael Hamann */
197d868eb89SAndreas Gohrfunction getSchemes()
198d868eb89SAndreas Gohr{
19949eb6e38SAndreas Gohr    static $schemes = null;
20036f2d7c1SGina Haeussge    if (!$schemes) {
2014c353447SChristopher Smith        $schemes = retrieveConfig('scheme', 'file', null, 'array_merge_with_removal');
20236f2d7c1SGina Haeussge        $schemes = array_map('trim', $schemes);
20336f2d7c1SGina Haeussge        $schemes = preg_replace('/^#.*/', '', $schemes);
20436f2d7c1SGina Haeussge        $schemes = array_filter($schemes);
2054c353447SChristopher Smith    }
20636f2d7c1SGina Haeussge    return $schemes;
20736f2d7c1SGina Haeussge}
20836f2d7c1SGina Haeussge
209b9ac8716Schris/**
210edcb01e5SGina Haeussge * Builds a hash from an array of lines
211b625487dSandi *
2123fd0b676Sandi * If $lower is set to true all hash keys are converted to
2133fd0b676Sandi * lower case.
2143fd0b676Sandi *
215b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
2163fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org>
217edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
218f50a239bSTakamura *
219f50a239bSTakamura * @param array $lines
220f50a239bSTakamura * @param bool $lower
221f50a239bSTakamura *
222f50a239bSTakamura * @return array
223b625487dSandi */
224d868eb89SAndreas Gohrfunction linesToHash($lines, $lower = false)
225d868eb89SAndreas Gohr{
22624870174SAndreas Gohr    $conf = [];
227dd74fecfSMichael Hamann    // remove BOM
22824870174SAndreas Gohr    if (isset($lines[0]) && substr($lines[0], 0, 3) === pack('CCC', 0xef, 0xbb, 0xbf))
229dd74fecfSMichael Hamann        $lines[0] = substr($lines[0], 3);
230b625487dSandi    foreach ($lines as $line) {
23103ff8795SAndreas Gohr        //ignore comments (except escaped ones)
23203ff8795SAndreas Gohr        $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line);
23303ff8795SAndreas Gohr        $line = str_replace('\\#', '#', $line);
234b625487dSandi        $line = trim($line);
23559ed97f2SAndreas Gohr        if ($line === '') continue;
236b625487dSandi        $line = preg_split('/\s+/', $line, 2);
23759ed97f2SAndreas Gohr        $line = array_pad($line, 2, '');
238b625487dSandi        // Build the associative array
23927a2b085Sandi        if ($lower) {
24027a2b085Sandi            $conf[strtolower($line[0])] = $line[1];
24127a2b085Sandi        } else {
242b625487dSandi            $conf[$line[0]] = $line[1];
243b625487dSandi        }
24427a2b085Sandi    }
245b625487dSandi
246b625487dSandi    return $conf;
247b625487dSandi}
248b625487dSandi
249409d7af7SAndreas Gohr/**
250edcb01e5SGina Haeussge * Builds a hash from a configfile
251edcb01e5SGina Haeussge *
252edcb01e5SGina Haeussge * If $lower is set to true all hash keys are converted to
253edcb01e5SGina Haeussge * lower case.
254edcb01e5SGina Haeussge *
255edcb01e5SGina Haeussge * @author Harry Fuecks <hfuecks@gmail.com>
256edcb01e5SGina Haeussge * @author Andreas Gohr <andi@splitbrain.org>
257edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
258f50a239bSTakamura *
259f50a239bSTakamura * @param string $file
260f50a239bSTakamura * @param bool $lower
261f50a239bSTakamura *
262f50a239bSTakamura * @return array
263edcb01e5SGina Haeussge */
264d868eb89SAndreas Gohrfunction confToHash($file, $lower = false)
265d868eb89SAndreas Gohr{
26624870174SAndreas Gohr    $conf = [];
267edcb01e5SGina Haeussge    $lines = @file($file);
268edcb01e5SGina Haeussge    if (!$lines) return $conf;
269edcb01e5SGina Haeussge
270edcb01e5SGina Haeussge    return linesToHash($lines, $lower);
271edcb01e5SGina Haeussge}
272edcb01e5SGina Haeussge
273edcb01e5SGina Haeussge/**
274c9071834SMichael Große * Read a json config file into an array
275c9071834SMichael Große *
276c9071834SMichael Große * @param string $file
277c9071834SMichael Große * @return array
278c9071834SMichael Große */
279c9071834SMichael Großefunction jsonToArray($file)
280c9071834SMichael Große{
281c9071834SMichael Große    $json = file_get_contents($file);
282c9071834SMichael Große
28324870174SAndreas Gohr    $conf = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
284c9071834SMichael Große
285dceb2cc1SMichael Große    if ($conf === null) {
286c9071834SMichael Große        return [];
287c9071834SMichael Große    }
288c9071834SMichael Große
289c9071834SMichael Große    return $conf;
290c9071834SMichael Große}
291c9071834SMichael Große
292c9071834SMichael Große/**
293cb043f52SChris Smith * Retrieve the requested configuration information
294cb043f52SChris Smith *
295cb043f52SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
296cb043f52SChris Smith *
297cb043f52SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
298cb043f52SChris Smith * @param  callback $fn       the function used to process the configuration file into an array
299e3ab6fc5SMichael Hamann * @param  array    $params   optional additional params to pass to the callback
3005a9597bbSTakamura * @param  callback $combine  the function used to combine arrays of values read from different configuration files;
301074b2b3fSTakamura *                            the function takes two parameters,
302074b2b3fSTakamura *                               $combined - the already read & merged configuration values
303074b2b3fSTakamura *                               $new - array of config values from the config cascade file being currently processed
304074b2b3fSTakamura *                            and returns an array of the merged configuration values.
305cb043f52SChris Smith * @return array    configuration values
306cb043f52SChris Smith */
307d868eb89SAndreas Gohrfunction retrieveConfig($type, $fn, $params = null, $combine = 'array_merge')
308d868eb89SAndreas Gohr{
309cb043f52SChris Smith    global $config_cascade;
310cb043f52SChris Smith
31124870174SAndreas Gohr    if (!is_array($params)) $params = [];
3124c6a5eccSAndreas Gohr
31324870174SAndreas Gohr    $combined = [];
314cb043f52SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"', E_USER_WARNING);
31524870174SAndreas Gohr    foreach (['default', 'local', 'protected'] as $config_group) {
316b303b92cSChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
317b303b92cSChris Smith        foreach ($config_cascade[$type][$config_group] as $file) {
31879e79377SAndreas Gohr            if (file_exists($file)) {
31924870174SAndreas Gohr                $config = call_user_func_array($fn, array_merge([$file], $params));
3204c353447SChristopher Smith                $combined = $combine($combined, $config);
321cb043f52SChris Smith            }
322cb043f52SChris Smith        }
323b303b92cSChris Smith    }
324cb043f52SChris Smith
325cb043f52SChris Smith    return $combined;
326cb043f52SChris Smith}
327cb043f52SChris Smith
328cb043f52SChris Smith/**
329f8121585SChris Smith * Include the requested configuration information
330f8121585SChris Smith *
331f8121585SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
332f8121585SChris Smith *
333f8121585SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
334f8121585SChris Smith * @return array              list of files, default before local before protected
335f8121585SChris Smith */
336d868eb89SAndreas Gohrfunction getConfigFiles($type)
337d868eb89SAndreas Gohr{
338f8121585SChris Smith    global $config_cascade;
33924870174SAndreas Gohr    $files = [];
340f8121585SChris Smith
341f8121585SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"', E_USER_WARNING);
34224870174SAndreas Gohr    foreach (['default', 'local', 'protected'] as $config_group) {
343f8121585SChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
344f8121585SChris Smith        $files = array_merge($files, $config_cascade[$type][$config_group]);
345f8121585SChris Smith    }
346f8121585SChris Smith
347f8121585SChris Smith    return $files;
348f8121585SChris Smith}
349f8121585SChris Smith
350f8121585SChris Smith/**
351409d7af7SAndreas Gohr * check if the given action was disabled in config
352409d7af7SAndreas Gohr *
353409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3540e2431b7SGerrit Uitslag * @param string $action
355409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled
356409d7af7SAndreas Gohr */
357d868eb89SAndreas Gohrfunction actionOK($action)
358d868eb89SAndreas Gohr{
359409d7af7SAndreas Gohr    static $disabled = null;
360020ea9e1SChristopher Smith    if (is_null($disabled) || defined('SIMPLE_TEST')) {
361409d7af7SAndreas Gohr        global $conf;
362e1d9dcc8SAndreas Gohr        /** @var AuthPlugin $auth */
363de4d479aSAdrian Lang        global $auth;
364409d7af7SAndreas Gohr
365409d7af7SAndreas Gohr        // prepare disabled actions array and handle legacy options
366409d7af7SAndreas Gohr        $disabled = explode(',', $conf['disableactions']);
367409d7af7SAndreas Gohr        $disabled = array_map('trim', $disabled);
368e4eda66bSAndreas Gohr        if ((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) {
369de4d479aSAdrian Lang            $disabled[] = 'register';
370de4d479aSAdrian Lang        }
371e4eda66bSAndreas Gohr        if ((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) {
372de4d479aSAdrian Lang            $disabled[] = 'resendpwd';
373de4d479aSAdrian Lang        }
374e4eda66bSAndreas Gohr        if ((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) {
3753a48618aSAnika Henke            $disabled[] = 'subscribe';
3763a48618aSAnika Henke        }
3773a48618aSAnika Henke        if (is_null($auth) || !$auth->canDo('Profile')) {
3783a48618aSAnika Henke            $disabled[] = 'profile';
3793a48618aSAnika Henke        }
3802a7abf2dSChristopher Smith        if (is_null($auth) || !$auth->canDo('delUser')) {
3812a7abf2dSChristopher Smith            $disabled[] = 'profile_delete';
3822a7abf2dSChristopher Smith        }
3833a48618aSAnika Henke        if (is_null($auth)) {
3843a48618aSAnika Henke            $disabled[] = 'login';
3853a48618aSAnika Henke        }
3863a48618aSAnika Henke        if (is_null($auth) || !$auth->canDo('logout')) {
3873a48618aSAnika Henke            $disabled[] = 'logout';
3883a48618aSAnika Henke        }
389409d7af7SAndreas Gohr        $disabled = array_unique($disabled);
390409d7af7SAndreas Gohr    }
391409d7af7SAndreas Gohr
392409d7af7SAndreas Gohr    return !in_array($action, $disabled);
393409d7af7SAndreas Gohr}
394409d7af7SAndreas Gohr
395fe9ec250SChris Smith/**
396fe9ec250SChris Smith * check if headings should be used as link text for the specified link type
397fe9ec250SChris Smith *
398fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
399fe9ec250SChris Smith *
400fe9ec250SChris Smith * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
401fe9ec250SChris Smith *                                                      navigation applies to all other links
402e3ab6fc5SMichael Hamann * @return  boolean             true if headings should be used for $linktype, false otherwise
403fe9ec250SChris Smith */
404d868eb89SAndreas Gohrfunction useHeading($linktype)
405d868eb89SAndreas Gohr{
406fe9ec250SChris Smith    static $useHeading = null;
4076506eaacSAndreas Gohr    if (defined('DOKU_UNITTEST')) $useHeading = null; // don't cache during unit tests
408fe9ec250SChris Smith
409fe9ec250SChris Smith    if (is_null($useHeading)) {
410fe9ec250SChris Smith        global $conf;
411fe9ec250SChris Smith
412fe9ec250SChris Smith        if (!empty($conf['useheading'])) {
413fe9ec250SChris Smith            switch ($conf['useheading']) {
41449eb6e38SAndreas Gohr                case 'content':
41549eb6e38SAndreas Gohr                    $useHeading['content'] = true;
41649eb6e38SAndreas Gohr                    break;
41749eb6e38SAndreas Gohr
41849eb6e38SAndreas Gohr                case 'navigation':
41949eb6e38SAndreas Gohr                    $useHeading['navigation'] = true;
42049eb6e38SAndreas Gohr                    break;
421fe9ec250SChris Smith                default:
422fe9ec250SChris Smith                    $useHeading['content'] = true;
423fe9ec250SChris Smith                    $useHeading['navigation'] = true;
424fe9ec250SChris Smith            }
425fe9ec250SChris Smith        } else {
42624870174SAndreas Gohr            $useHeading = [];
427fe9ec250SChris Smith        }
428fe9ec250SChris Smith    }
429fe9ec250SChris Smith
430fe9ec250SChris Smith    return (!empty($useHeading[$linktype]));
431fe9ec250SChris Smith}
432fe9ec250SChris Smith
4333994772aSChris Smith/**
4343994772aSChris Smith * obscure config data so information isn't plain text
4353994772aSChris Smith *
4363994772aSChris Smith * @param string       $str     data to be encoded
4373994772aSChris Smith * @param string       $code    encoding method, values: plain, base64, uuencode.
4383994772aSChris Smith * @return string               the encoded value
4393994772aSChris Smith */
440d868eb89SAndreas Gohrfunction conf_encodeString($str, $code)
441d868eb89SAndreas Gohr{
4423994772aSChris Smith    switch ($code) {
443*62ad2d27SAndreas Gohr        case 'base64':
444*62ad2d27SAndreas Gohr            return '<b>'.base64_encode($str);
445*62ad2d27SAndreas Gohr        case 'uuencode':
446*62ad2d27SAndreas Gohr            return '<u>'.convert_uuencode($str);
4473994772aSChris Smith        case 'plain':
4483994772aSChris Smith        default:
4493994772aSChris Smith            return $str;
4503994772aSChris Smith    }
4513994772aSChris Smith}
4523994772aSChris Smith/**
4533994772aSChris Smith * return obscured data as plain text
4543994772aSChris Smith *
4553994772aSChris Smith * @param  string      $str   encoded data
4563994772aSChris Smith * @return string             plain text
4573994772aSChris Smith */
458d868eb89SAndreas Gohrfunction conf_decodeString($str)
459d868eb89SAndreas Gohr{
4603994772aSChris Smith    switch (substr($str, 0, 3)) {
461*62ad2d27SAndreas Gohr        case '<b>':
462*62ad2d27SAndreas Gohr            return base64_decode(substr($str, 3));
463*62ad2d27SAndreas Gohr        case '<u>':
464*62ad2d27SAndreas Gohr            return convert_uudecode(substr($str, 3));
465b96ff25bSElan Ruusamäe        default:  // not encoded (or unknown)
4663994772aSChris Smith            return $str;
4673994772aSChris Smith    }
4683994772aSChris Smith}
4694c353447SChristopher Smith
4704c353447SChristopher Smith/**
4714c353447SChristopher Smith * array combination function to remove negated values (prefixed by !)
4724c353447SChristopher Smith *
4734c353447SChristopher Smith * @param  array $current
4744c353447SChristopher Smith * @param  array $new
4754c353447SChristopher Smith *
4764c353447SChristopher Smith * @return array the combined array, numeric keys reset
4774c353447SChristopher Smith */
478d868eb89SAndreas Gohrfunction array_merge_with_removal($current, $new)
479d868eb89SAndreas Gohr{
4804c353447SChristopher Smith    foreach ($new as $val) {
48110b38f10SChristopher Smith        if (substr($val, 0, 1) == DOKU_CONF_NEGATION) {
4823a7669bdSChristopher Smith            $idx = array_search(trim(substr($val, 1)), $current);
4834c353447SChristopher Smith            if ($idx !== false) {
4844c353447SChristopher Smith                unset($current[$idx]);
4854c353447SChristopher Smith            }
4864c353447SChristopher Smith        } else {
4873a7669bdSChristopher Smith            $current[] = trim($val);
4884c353447SChristopher Smith        }
4894c353447SChristopher Smith    }
4904c353447SChristopher Smith
4914c353447SChristopher Smith    return array_slice($current, 0);
4924c353447SChristopher Smith}
493e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
494