xref: /dokuwiki/inc/confutils.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
1b625487dSandi<?php
2*d4f83172SAndreas 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 <hfuecks@gmail.com>
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;
18*d4f83172SAndreas 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 <andi@splitbrain.org>
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 <andi@splitbrain.org>
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 <hfuecks@gmail.com>
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 <hfuecks@gmail.com>
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 <hfuecks@gmail.com>
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 <hfuecks@gmail.com>
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 <hfuecks@gmail.com>
2183fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org>
219edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
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
23024870174SAndreas Gohr    if (isset($lines[0]) && substr($lines[0], 0, 3) === 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 <hfuecks@gmail.com>
258edcb01e5SGina Haeussge * @author Andreas Gohr <andi@splitbrain.org>
259edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
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
280c9071834SMichael Große */
281c9071834SMichael Großefunction jsonToArray($file)
282c9071834SMichael Große{
283c9071834SMichael Große    $json = file_get_contents($file);
284c9071834SMichael Große
28524870174SAndreas Gohr    $conf = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
286c9071834SMichael Große
287dceb2cc1SMichael Große    if ($conf === null) {
288c9071834SMichael Große        return [];
289c9071834SMichael Große    }
290c9071834SMichael Große
291c9071834SMichael Große    return $conf;
292c9071834SMichael Große}
293c9071834SMichael Große
294c9071834SMichael Große/**
295cb043f52SChris Smith * Retrieve the requested configuration information
296cb043f52SChris Smith *
297cb043f52SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
298cb043f52SChris Smith *
299cb043f52SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
300cb043f52SChris Smith * @param  callback $fn       the function used to process the configuration file into an array
301e3ab6fc5SMichael Hamann * @param  array    $params   optional additional params to pass to the callback
3025a9597bbSTakamura * @param  callback $combine  the function used to combine arrays of values read from different configuration files;
303074b2b3fSTakamura *                            the function takes two parameters,
304074b2b3fSTakamura *                               $combined - the already read & merged configuration values
305074b2b3fSTakamura *                               $new - array of config values from the config cascade file being currently processed
306074b2b3fSTakamura *                            and returns an array of the merged configuration values.
307cb043f52SChris Smith * @return array    configuration values
308cb043f52SChris Smith */
309d868eb89SAndreas Gohrfunction retrieveConfig($type, $fn, $params = null, $combine = 'array_merge')
310d868eb89SAndreas Gohr{
311cb043f52SChris Smith    global $config_cascade;
312cb043f52SChris Smith
31324870174SAndreas Gohr    if (!is_array($params)) $params = [];
3144c6a5eccSAndreas Gohr
31524870174SAndreas Gohr    $combined = [];
316cb043f52SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "' . $type . '"', E_USER_WARNING);
31724870174SAndreas Gohr    foreach (['default', 'local', 'protected'] as $config_group) {
318b303b92cSChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
319b303b92cSChris Smith        foreach ($config_cascade[$type][$config_group] as $file) {
32079e79377SAndreas Gohr            if (file_exists($file)) {
32124870174SAndreas Gohr                $config = call_user_func_array($fn, array_merge([$file], $params));
3224c353447SChristopher Smith                $combined = $combine($combined, $config);
323cb043f52SChris Smith            }
324cb043f52SChris Smith        }
325b303b92cSChris Smith    }
326cb043f52SChris Smith
327cb043f52SChris Smith    return $combined;
328cb043f52SChris Smith}
329cb043f52SChris Smith
330cb043f52SChris Smith/**
331f8121585SChris Smith * Include the requested configuration information
332f8121585SChris Smith *
333f8121585SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
334f8121585SChris Smith *
335f8121585SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
336f8121585SChris Smith * @return array              list of files, default before local before protected
337f8121585SChris Smith */
338d868eb89SAndreas Gohrfunction getConfigFiles($type)
339d868eb89SAndreas Gohr{
340f8121585SChris Smith    global $config_cascade;
34124870174SAndreas Gohr    $files = [];
342f8121585SChris Smith
343f8121585SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "' . $type . '"', E_USER_WARNING);
34424870174SAndreas Gohr    foreach (['default', 'local', 'protected'] as $config_group) {
345f8121585SChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
346f8121585SChris Smith        $files = array_merge($files, $config_cascade[$type][$config_group]);
347f8121585SChris Smith    }
348f8121585SChris Smith
349f8121585SChris Smith    return $files;
350f8121585SChris Smith}
351f8121585SChris Smith
352f8121585SChris Smith/**
353409d7af7SAndreas Gohr * check if the given action was disabled in config
354409d7af7SAndreas Gohr *
355409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3560e2431b7SGerrit Uitslag * @param string $action
357409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled
358409d7af7SAndreas Gohr */
359d868eb89SAndreas Gohrfunction actionOK($action)
360d868eb89SAndreas Gohr{
361409d7af7SAndreas Gohr    static $disabled = null;
362020ea9e1SChristopher Smith    if (is_null($disabled) || defined('SIMPLE_TEST')) {
363409d7af7SAndreas Gohr        global $conf;
364e1d9dcc8SAndreas Gohr        /** @var AuthPlugin $auth */
365de4d479aSAdrian Lang        global $auth;
366409d7af7SAndreas Gohr
367409d7af7SAndreas Gohr        // prepare disabled actions array and handle legacy options
368409d7af7SAndreas Gohr        $disabled = explode(',', $conf['disableactions']);
369409d7af7SAndreas Gohr        $disabled = array_map('trim', $disabled);
370e4eda66bSAndreas Gohr        if ((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) {
371de4d479aSAdrian Lang            $disabled[] = 'register';
372de4d479aSAdrian Lang        }
373e4eda66bSAndreas Gohr        if ((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) {
374de4d479aSAdrian Lang            $disabled[] = 'resendpwd';
375de4d479aSAdrian Lang        }
376e4eda66bSAndreas Gohr        if ((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) {
3773a48618aSAnika Henke            $disabled[] = 'subscribe';
3783a48618aSAnika Henke        }
3793a48618aSAnika Henke        if (is_null($auth) || !$auth->canDo('Profile')) {
3803a48618aSAnika Henke            $disabled[] = 'profile';
3813a48618aSAnika Henke        }
3822a7abf2dSChristopher Smith        if (is_null($auth) || !$auth->canDo('delUser')) {
3832a7abf2dSChristopher Smith            $disabled[] = 'profile_delete';
3842a7abf2dSChristopher Smith        }
3853a48618aSAnika Henke        if (is_null($auth)) {
3863a48618aSAnika Henke            $disabled[] = 'login';
3873a48618aSAnika Henke        }
3883a48618aSAnika Henke        if (is_null($auth) || !$auth->canDo('logout')) {
3893a48618aSAnika Henke            $disabled[] = 'logout';
3903a48618aSAnika Henke        }
391409d7af7SAndreas Gohr        $disabled = array_unique($disabled);
392409d7af7SAndreas Gohr    }
393409d7af7SAndreas Gohr
394409d7af7SAndreas Gohr    return !in_array($action, $disabled);
395409d7af7SAndreas Gohr}
396409d7af7SAndreas Gohr
397fe9ec250SChris Smith/**
398fe9ec250SChris Smith * check if headings should be used as link text for the specified link type
399fe9ec250SChris Smith *
400fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
401fe9ec250SChris Smith *
402fe9ec250SChris Smith * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
403fe9ec250SChris Smith *                                                      navigation applies to all other links
404e3ab6fc5SMichael Hamann * @return  boolean             true if headings should be used for $linktype, false otherwise
405fe9ec250SChris Smith */
406d868eb89SAndreas Gohrfunction useHeading($linktype)
407d868eb89SAndreas Gohr{
408fe9ec250SChris Smith    static $useHeading = null;
4096506eaacSAndreas Gohr    if (defined('DOKU_UNITTEST')) $useHeading = null; // don't cache during unit tests
410fe9ec250SChris Smith
411fe9ec250SChris Smith    if (is_null($useHeading)) {
412fe9ec250SChris Smith        global $conf;
413fe9ec250SChris Smith
414fe9ec250SChris Smith        if (!empty($conf['useheading'])) {
415fe9ec250SChris Smith            switch ($conf['useheading']) {
41649eb6e38SAndreas Gohr                case 'content':
41749eb6e38SAndreas Gohr                    $useHeading['content'] = true;
41849eb6e38SAndreas Gohr                    break;
41949eb6e38SAndreas Gohr
42049eb6e38SAndreas Gohr                case 'navigation':
42149eb6e38SAndreas Gohr                    $useHeading['navigation'] = true;
42249eb6e38SAndreas Gohr                    break;
423fe9ec250SChris Smith                default:
424fe9ec250SChris Smith                    $useHeading['content'] = true;
425fe9ec250SChris Smith                    $useHeading['navigation'] = true;
426fe9ec250SChris Smith            }
427fe9ec250SChris Smith        } else {
42824870174SAndreas Gohr            $useHeading = [];
429fe9ec250SChris Smith        }
430fe9ec250SChris Smith    }
431fe9ec250SChris Smith
432fe9ec250SChris Smith    return (!empty($useHeading[$linktype]));
433fe9ec250SChris Smith}
434fe9ec250SChris Smith
4353994772aSChris Smith/**
4363994772aSChris Smith * obscure config data so information isn't plain text
4373994772aSChris Smith *
4383994772aSChris Smith * @param string       $str     data to be encoded
4393994772aSChris Smith * @param string       $code    encoding method, values: plain, base64, uuencode.
4403994772aSChris Smith * @return string               the encoded value
4413994772aSChris Smith */
442d868eb89SAndreas Gohrfunction conf_encodeString($str, $code)
443d868eb89SAndreas Gohr{
4443994772aSChris Smith    switch ($code) {
44562ad2d27SAndreas Gohr        case 'base64':
44662ad2d27SAndreas Gohr            return '<b>' . base64_encode($str);
44762ad2d27SAndreas Gohr        case 'uuencode':
44862ad2d27SAndreas Gohr            return '<u>' . convert_uuencode($str);
4493994772aSChris Smith        case 'plain':
4503994772aSChris Smith        default:
4513994772aSChris Smith            return $str;
4523994772aSChris Smith    }
4533994772aSChris Smith}
4543994772aSChris Smith/**
4553994772aSChris Smith * return obscured data as plain text
4563994772aSChris Smith *
4573994772aSChris Smith * @param  string      $str   encoded data
4583994772aSChris Smith * @return string             plain text
4593994772aSChris Smith */
460d868eb89SAndreas Gohrfunction conf_decodeString($str)
461d868eb89SAndreas Gohr{
4623994772aSChris Smith    switch (substr($str, 0, 3)) {
46362ad2d27SAndreas Gohr        case '<b>':
46462ad2d27SAndreas Gohr            return base64_decode(substr($str, 3));
46562ad2d27SAndreas Gohr        case '<u>':
46662ad2d27SAndreas Gohr            return convert_uudecode(substr($str, 3));
467b96ff25bSElan Ruusamäe        default:  // not encoded (or unknown)
4683994772aSChris Smith            return $str;
4693994772aSChris Smith    }
4703994772aSChris Smith}
4714c353447SChristopher Smith
4724c353447SChristopher Smith/**
4734c353447SChristopher Smith * array combination function to remove negated values (prefixed by !)
4744c353447SChristopher Smith *
4754c353447SChristopher Smith * @param  array $current
4764c353447SChristopher Smith * @param  array $new
4774c353447SChristopher Smith *
4784c353447SChristopher Smith * @return array the combined array, numeric keys reset
4794c353447SChristopher Smith */
480d868eb89SAndreas Gohrfunction array_merge_with_removal($current, $new)
481d868eb89SAndreas Gohr{
4824c353447SChristopher Smith    foreach ($new as $val) {
48310b38f10SChristopher Smith        if (substr($val, 0, 1) == DOKU_CONF_NEGATION) {
4843a7669bdSChristopher Smith            $idx = array_search(trim(substr($val, 1)), $current);
4854c353447SChristopher Smith            if ($idx !== false) {
4864c353447SChristopher Smith                unset($current[$idx]);
4874c353447SChristopher Smith            }
4884c353447SChristopher Smith        } else {
4893a7669bdSChristopher Smith            $current[] = trim($val);
4904c353447SChristopher Smith        }
4914c353447SChristopher Smith    }
4924c353447SChristopher Smith
4934c353447SChristopher Smith    return array_slice($current, 0);
4944c353447SChristopher Smith}
495e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
496