xref: /dokuwiki/inc/confutils.php (revision 24870174d2ee45460ba6bcfe5f5a0ae94715efd7)
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 */
3127bf7924STom N Harrisfunction mimetype($file, $knownonly=true){
32b625487dSandi    $mtypes = getMimeTypes();     // known mimetypes
33ad74fe66SAdrian Lang    $ext    = strrpos($file, '.');
34ad74fe66SAdrian Lang    if ($ext === false) {
35*24870174SAndreas Gohr        return [false, false, false];
3627bf7924STom N Harris    }
37ad74fe66SAdrian Lang    $ext = strtolower(substr($file, $ext + 1));
38ad74fe66SAdrian Lang    if (!isset($mtypes[$ext])){
39ad74fe66SAdrian Lang        if ($knownonly) {
40*24870174SAndreas Gohr            return [false, false, false];
41ecebf3a8SAndreas Gohr        } else {
42*24870174SAndreas Gohr            return [$ext, 'application/octet-stream', true];
4327bf7924STom N Harris        }
44b625487dSandi    }
45ad74fe66SAdrian Lang    if($mtypes[$ext][0] == '!'){
46*24870174SAndreas Gohr        return [$ext, substr($mtypes[$ext],1), true];
47ad74fe66SAdrian Lang    }else{
48*24870174SAndreas Gohr        return [$ext, $mtypes[$ext], false];
49ad74fe66SAdrian Lang    }
50b625487dSandi}
51b625487dSandi
52b625487dSandi/**
53b625487dSandi * returns a hash of mimetypes
54b625487dSandi *
55b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
56b625487dSandi */
57b625487dSandifunction getMimeTypes() {
5849eb6e38SAndreas Gohr    static $mime = null;
59b625487dSandi    if ( !$mime ) {
60cb043f52SChris Smith        $mime = retrieveConfig('mime','confToHash');
6145ae4bb8SChristopher Smith        $mime = array_filter($mime);
62b625487dSandi    }
63b625487dSandi    return $mime;
64b625487dSandi}
65b625487dSandi
66b625487dSandi/**
67b625487dSandi * returns a hash of acronyms
68b625487dSandi *
69b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
70b625487dSandi */
71b625487dSandifunction getAcronyms() {
7249eb6e38SAndreas Gohr    static $acronyms = null;
73b625487dSandi    if ( !$acronyms ) {
74cb043f52SChris Smith        $acronyms = retrieveConfig('acronyms','confToHash');
75f266a919SChristopher Smith        $acronyms = array_filter($acronyms, 'strlen');
76b625487dSandi    }
77b625487dSandi    return $acronyms;
78b625487dSandi}
79b625487dSandi
80b625487dSandi/**
81b625487dSandi * returns a hash of smileys
82b625487dSandi *
83b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
84b625487dSandi */
85b625487dSandifunction getSmileys() {
8649eb6e38SAndreas Gohr    static $smileys = null;
87b625487dSandi    if ( !$smileys ) {
88cb043f52SChris Smith        $smileys = retrieveConfig('smileys','confToHash');
89f266a919SChristopher Smith        $smileys = array_filter($smileys, 'strlen');
90b625487dSandi    }
91b625487dSandi    return $smileys;
92b625487dSandi}
93b625487dSandi
94b625487dSandi/**
95b625487dSandi * returns a hash of entities
96b625487dSandi *
97b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
98b625487dSandi */
99b625487dSandifunction getEntities() {
10049eb6e38SAndreas Gohr    static $entities = null;
101b625487dSandi    if ( !$entities ) {
102cb043f52SChris Smith        $entities = retrieveConfig('entities','confToHash');
103f266a919SChristopher Smith        $entities = array_filter($entities, 'strlen');
104b625487dSandi    }
105b625487dSandi    return $entities;
106b625487dSandi}
107b625487dSandi
108b625487dSandi/**
109b625487dSandi * returns a hash of interwikilinks
110b625487dSandi *
111b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
112b625487dSandi */
113b625487dSandifunction getInterwiki() {
11449eb6e38SAndreas Gohr    static $wikis = null;
115b625487dSandi    if ( !$wikis ) {
116*24870174SAndreas Gohr        $wikis = retrieveConfig('interwiki','confToHash',[true]);
117f266a919SChristopher Smith        $wikis = array_filter($wikis, 'strlen');
11845ae4bb8SChristopher Smith
11997a3e4e3Sandi        //add sepecial case 'this'
12027a2b085Sandi        $wikis['this'] = DOKU_URL.'{NAME}';
12145ae4bb8SChristopher Smith    }
122b625487dSandi    return $wikis;
123b625487dSandi}
124b625487dSandi
125b625487dSandi/**
126fa078663SAndreas Gohr * Returns the jquery script URLs for the versions defined in lib/scripts/jquery/versions
12761537d47SAndreas Gohr *
128fa078663SAndreas Gohr * @trigger CONFUTIL_CDN_SELECT
12961537d47SAndreas Gohr * @return array
13061537d47SAndreas Gohr */
131fa078663SAndreas Gohrfunction getCdnUrls() {
132fa078663SAndreas Gohr    global $conf;
133fa078663SAndreas Gohr
134fa078663SAndreas Gohr    // load version info
135*24870174SAndreas Gohr    $versions = [];
13661537d47SAndreas Gohr    $lines = file(DOKU_INC . 'lib/scripts/jquery/versions');
13761537d47SAndreas Gohr    foreach($lines as $line) {
1386453acd5SAndreas Gohr        $line = trim(preg_replace('/#.*$/', '', $line));
1396453acd5SAndreas Gohr        if($line === '') continue;
140*24870174SAndreas Gohr        [$key, $val] = sexplode('=', $line, 2, '');
14161537d47SAndreas Gohr        $key = trim($key);
14261537d47SAndreas Gohr        $val = trim($val);
14361537d47SAndreas Gohr        $versions[$key] = $val;
14461537d47SAndreas Gohr    }
145fa078663SAndreas Gohr
146*24870174SAndreas Gohr    $src = [];
147*24870174SAndreas Gohr    $data = ['versions' => $versions, 'src' => &$src];
148e1d9dcc8SAndreas Gohr    $event = new Event('CONFUTIL_CDN_SELECT', $data);
149fa078663SAndreas Gohr    if($event->advise_before()) {
150fa078663SAndreas Gohr        if(!$conf['jquerycdn']) {
151*24870174SAndreas Gohr            $jqmod = md5(implode('-', $versions));
152fa078663SAndreas Gohr            $src[] = DOKU_BASE . 'lib/exe/jquery.php' . '?tseed=' . $jqmod;
153fa078663SAndreas Gohr        } elseif($conf['jquerycdn'] == 'jquery') {
154fa078663SAndreas Gohr            $src[] = sprintf('https://code.jquery.com/jquery-%s.min.js', $versions['JQ_VERSION']);
155fa078663SAndreas Gohr            $src[] = sprintf('https://code.jquery.com/ui/%s/jquery-ui.min.js', $versions['JQUI_VERSION']);
156fa078663SAndreas Gohr        } elseif($conf['jquerycdn'] == 'cdnjs') {
15764159a61SAndreas Gohr            $src[] = sprintf(
15864159a61SAndreas Gohr                'https://cdnjs.cloudflare.com/ajax/libs/jquery/%s/jquery.min.js',
15964159a61SAndreas Gohr                $versions['JQ_VERSION']
16064159a61SAndreas Gohr            );
16164159a61SAndreas Gohr            $src[] = sprintf(
16264159a61SAndreas Gohr                'https://cdnjs.cloudflare.com/ajax/libs/jqueryui/%s/jquery-ui.min.js',
16364159a61SAndreas Gohr                $versions['JQUI_VERSION']
16464159a61SAndreas Gohr            );
165fa078663SAndreas Gohr        }
166fa078663SAndreas Gohr    }
167fa078663SAndreas Gohr    $event->advise_after();
168fa078663SAndreas Gohr
169fa078663SAndreas Gohr    return $src;
17061537d47SAndreas Gohr}
17161537d47SAndreas Gohr
17261537d47SAndreas Gohr/**
173b9ac8716Schris * returns array of wordblock patterns
174b9ac8716Schris *
175b9ac8716Schris */
176b9ac8716Schrisfunction getWordblocks() {
17749eb6e38SAndreas Gohr    static $wordblocks = null;
178b9ac8716Schris    if ( !$wordblocks ) {
1794c353447SChristopher Smith        $wordblocks = retrieveConfig('wordblock','file',null,'array_merge_with_removal');
180b9ac8716Schris    }
181b9ac8716Schris    return $wordblocks;
182b9ac8716Schris}
183b9ac8716Schris
184e3ab6fc5SMichael Hamann/**
185e3ab6fc5SMichael Hamann * Gets the list of configured schemes
186e3ab6fc5SMichael Hamann *
187e3ab6fc5SMichael Hamann * @return array the schemes
188e3ab6fc5SMichael Hamann */
18936f2d7c1SGina Haeussgefunction getSchemes() {
19049eb6e38SAndreas Gohr    static $schemes = null;
19136f2d7c1SGina Haeussge    if ( !$schemes ) {
1924c353447SChristopher Smith        $schemes = retrieveConfig('scheme','file',null,'array_merge_with_removal');
19336f2d7c1SGina Haeussge        $schemes = array_map('trim', $schemes);
19436f2d7c1SGina Haeussge        $schemes = preg_replace('/^#.*/', '', $schemes);
19536f2d7c1SGina Haeussge        $schemes = array_filter($schemes);
1964c353447SChristopher Smith    }
19736f2d7c1SGina Haeussge    return $schemes;
19836f2d7c1SGina Haeussge}
19936f2d7c1SGina Haeussge
200b9ac8716Schris/**
201edcb01e5SGina Haeussge * Builds a hash from an array of lines
202b625487dSandi *
2033fd0b676Sandi * If $lower is set to true all hash keys are converted to
2043fd0b676Sandi * lower case.
2053fd0b676Sandi *
206b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
2073fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org>
208edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
209f50a239bSTakamura *
210f50a239bSTakamura * @param array $lines
211f50a239bSTakamura * @param bool $lower
212f50a239bSTakamura *
213f50a239bSTakamura * @return array
214b625487dSandi */
215edcb01e5SGina Haeussgefunction linesToHash($lines, $lower = false) {
216*24870174SAndreas Gohr    $conf = [];
217dd74fecfSMichael Hamann    // remove BOM
218*24870174SAndreas Gohr    if(isset($lines[0]) && substr($lines[0], 0, 3) === pack('CCC', 0xef, 0xbb, 0xbf))
219dd74fecfSMichael Hamann        $lines[0] = substr($lines[0], 3);
220b625487dSandi    foreach($lines as $line) {
22103ff8795SAndreas Gohr        //ignore comments (except escaped ones)
22203ff8795SAndreas Gohr        $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line);
22303ff8795SAndreas Gohr        $line = str_replace('\\#', '#', $line);
224b625487dSandi        $line = trim($line);
22559ed97f2SAndreas Gohr        if($line === '') continue;
226b625487dSandi        $line = preg_split('/\s+/', $line, 2);
22759ed97f2SAndreas Gohr        $line = array_pad($line, 2, '');
228b625487dSandi        // Build the associative array
22927a2b085Sandi        if($lower) {
23027a2b085Sandi            $conf[strtolower($line[0])] = $line[1];
23127a2b085Sandi        } else {
232b625487dSandi            $conf[$line[0]] = $line[1];
233b625487dSandi        }
23427a2b085Sandi    }
235b625487dSandi
236b625487dSandi    return $conf;
237b625487dSandi}
238b625487dSandi
239409d7af7SAndreas Gohr/**
240edcb01e5SGina Haeussge * Builds a hash from a configfile
241edcb01e5SGina Haeussge *
242edcb01e5SGina Haeussge * If $lower is set to true all hash keys are converted to
243edcb01e5SGina Haeussge * lower case.
244edcb01e5SGina Haeussge *
245edcb01e5SGina Haeussge * @author Harry Fuecks <hfuecks@gmail.com>
246edcb01e5SGina Haeussge * @author Andreas Gohr <andi@splitbrain.org>
247edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
248f50a239bSTakamura *
249f50a239bSTakamura * @param string $file
250f50a239bSTakamura * @param bool $lower
251f50a239bSTakamura *
252f50a239bSTakamura * @return array
253edcb01e5SGina Haeussge */
254edcb01e5SGina Haeussgefunction confToHash($file,$lower=false) {
255*24870174SAndreas Gohr    $conf = [];
256edcb01e5SGina Haeussge    $lines = @file( $file );
257edcb01e5SGina Haeussge    if ( !$lines ) return $conf;
258edcb01e5SGina Haeussge
259edcb01e5SGina Haeussge    return linesToHash($lines, $lower);
260edcb01e5SGina Haeussge}
261edcb01e5SGina Haeussge
262edcb01e5SGina Haeussge/**
263c9071834SMichael Große * Read a json config file into an array
264c9071834SMichael Große *
265c9071834SMichael Große * @param string $file
266c9071834SMichael Große * @return array
267c9071834SMichael Große */
268c9071834SMichael Großefunction jsonToArray($file)
269c9071834SMichael Große{
270c9071834SMichael Große    $json = file_get_contents($file);
271c9071834SMichael Große
272*24870174SAndreas Gohr    $conf = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
273c9071834SMichael Große
274dceb2cc1SMichael Große    if ($conf === null) {
275c9071834SMichael Große        return [];
276c9071834SMichael Große    }
277c9071834SMichael Große
278c9071834SMichael Große    return $conf;
279c9071834SMichael Große}
280c9071834SMichael Große
281c9071834SMichael Große/**
282cb043f52SChris Smith * Retrieve the requested configuration information
283cb043f52SChris Smith *
284cb043f52SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
285cb043f52SChris Smith *
286cb043f52SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
287cb043f52SChris Smith * @param  callback $fn       the function used to process the configuration file into an array
288e3ab6fc5SMichael Hamann * @param  array    $params   optional additional params to pass to the callback
2895a9597bbSTakamura * @param  callback $combine  the function used to combine arrays of values read from different configuration files;
290074b2b3fSTakamura *                            the function takes two parameters,
291074b2b3fSTakamura *                               $combined - the already read & merged configuration values
292074b2b3fSTakamura *                               $new - array of config values from the config cascade file being currently processed
293074b2b3fSTakamura *                            and returns an array of the merged configuration values.
294cb043f52SChris Smith * @return array    configuration values
295cb043f52SChris Smith */
2964c353447SChristopher Smithfunction retrieveConfig($type,$fn,$params=null,$combine='array_merge') {
297cb043f52SChris Smith    global $config_cascade;
298cb043f52SChris Smith
299*24870174SAndreas Gohr    if(!is_array($params)) $params = [];
3004c6a5eccSAndreas Gohr
301*24870174SAndreas Gohr    $combined = [];
302cb043f52SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
303*24870174SAndreas Gohr    foreach (['default', 'local', 'protected'] as $config_group) {
304b303b92cSChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
305b303b92cSChris Smith        foreach ($config_cascade[$type][$config_group] as $file) {
30679e79377SAndreas Gohr            if (file_exists($file)) {
307*24870174SAndreas Gohr                $config = call_user_func_array($fn,array_merge([$file],$params));
3084c353447SChristopher Smith                $combined = $combine($combined, $config);
309cb043f52SChris Smith            }
310cb043f52SChris Smith        }
311b303b92cSChris Smith    }
312cb043f52SChris Smith
313cb043f52SChris Smith    return $combined;
314cb043f52SChris Smith}
315cb043f52SChris Smith
316cb043f52SChris Smith/**
317f8121585SChris Smith * Include the requested configuration information
318f8121585SChris Smith *
319f8121585SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
320f8121585SChris Smith *
321f8121585SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
322f8121585SChris Smith * @return array              list of files, default before local before protected
323f8121585SChris Smith */
324f8121585SChris Smithfunction getConfigFiles($type) {
325f8121585SChris Smith    global $config_cascade;
326*24870174SAndreas Gohr    $files = [];
327f8121585SChris Smith
328f8121585SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
329*24870174SAndreas Gohr    foreach (['default', 'local', 'protected'] as $config_group) {
330f8121585SChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
331f8121585SChris Smith        $files = array_merge($files, $config_cascade[$type][$config_group]);
332f8121585SChris Smith    }
333f8121585SChris Smith
334f8121585SChris Smith    return $files;
335f8121585SChris Smith}
336f8121585SChris Smith
337f8121585SChris Smith/**
338409d7af7SAndreas Gohr * check if the given action was disabled in config
339409d7af7SAndreas Gohr *
340409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3410e2431b7SGerrit Uitslag * @param string $action
342409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled
343409d7af7SAndreas Gohr */
344409d7af7SAndreas Gohrfunction actionOK($action){
345409d7af7SAndreas Gohr    static $disabled = null;
346020ea9e1SChristopher Smith    if(is_null($disabled) || defined('SIMPLE_TEST')){
347409d7af7SAndreas Gohr        global $conf;
348e1d9dcc8SAndreas Gohr        /** @var AuthPlugin $auth */
349de4d479aSAdrian Lang        global $auth;
350409d7af7SAndreas Gohr
351409d7af7SAndreas Gohr        // prepare disabled actions array and handle legacy options
352409d7af7SAndreas Gohr        $disabled = explode(',',$conf['disableactions']);
353409d7af7SAndreas Gohr        $disabled = array_map('trim',$disabled);
354e4eda66bSAndreas Gohr        if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) {
355de4d479aSAdrian Lang            $disabled[] = 'register';
356de4d479aSAdrian Lang        }
357e4eda66bSAndreas Gohr        if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) {
358de4d479aSAdrian Lang            $disabled[] = 'resendpwd';
359de4d479aSAdrian Lang        }
360e4eda66bSAndreas Gohr        if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) {
3613a48618aSAnika Henke            $disabled[] = 'subscribe';
3623a48618aSAnika Henke        }
3633a48618aSAnika Henke        if (is_null($auth) || !$auth->canDo('Profile')) {
3643a48618aSAnika Henke            $disabled[] = 'profile';
3653a48618aSAnika Henke        }
3662a7abf2dSChristopher Smith        if (is_null($auth) || !$auth->canDo('delUser')) {
3672a7abf2dSChristopher Smith            $disabled[] = 'profile_delete';
3682a7abf2dSChristopher Smith        }
3693a48618aSAnika Henke        if (is_null($auth)) {
3703a48618aSAnika Henke            $disabled[] = 'login';
3713a48618aSAnika Henke        }
3723a48618aSAnika Henke        if (is_null($auth) || !$auth->canDo('logout')) {
3733a48618aSAnika Henke            $disabled[] = 'logout';
3743a48618aSAnika Henke        }
375409d7af7SAndreas Gohr        $disabled = array_unique($disabled);
376409d7af7SAndreas Gohr    }
377409d7af7SAndreas Gohr
378409d7af7SAndreas Gohr    return !in_array($action,$disabled);
379409d7af7SAndreas Gohr}
380409d7af7SAndreas Gohr
381fe9ec250SChris Smith/**
382fe9ec250SChris Smith * check if headings should be used as link text for the specified link type
383fe9ec250SChris Smith *
384fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
385fe9ec250SChris Smith *
386fe9ec250SChris Smith * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
387fe9ec250SChris Smith *                                                      navigation applies to all other links
388e3ab6fc5SMichael Hamann * @return  boolean             true if headings should be used for $linktype, false otherwise
389fe9ec250SChris Smith */
390fe9ec250SChris Smithfunction useHeading($linktype) {
391fe9ec250SChris Smith    static $useHeading = null;
3926506eaacSAndreas Gohr    if(defined('DOKU_UNITTEST')) $useHeading = null; // don't cache during unit tests
393fe9ec250SChris Smith
394fe9ec250SChris Smith    if (is_null($useHeading)) {
395fe9ec250SChris Smith        global $conf;
396fe9ec250SChris Smith
397fe9ec250SChris Smith        if (!empty($conf['useheading'])) {
398fe9ec250SChris Smith            switch ($conf['useheading']) {
39949eb6e38SAndreas Gohr                case 'content':
40049eb6e38SAndreas Gohr                    $useHeading['content'] = true;
40149eb6e38SAndreas Gohr                    break;
40249eb6e38SAndreas Gohr
40349eb6e38SAndreas Gohr                case 'navigation':
40449eb6e38SAndreas Gohr                    $useHeading['navigation'] = true;
40549eb6e38SAndreas Gohr                    break;
406fe9ec250SChris Smith                default:
407fe9ec250SChris Smith                    $useHeading['content'] = true;
408fe9ec250SChris Smith                    $useHeading['navigation'] = true;
409fe9ec250SChris Smith            }
410fe9ec250SChris Smith        } else {
411*24870174SAndreas Gohr            $useHeading = [];
412fe9ec250SChris Smith        }
413fe9ec250SChris Smith    }
414fe9ec250SChris Smith
415fe9ec250SChris Smith    return (!empty($useHeading[$linktype]));
416fe9ec250SChris Smith}
417fe9ec250SChris Smith
4183994772aSChris Smith/**
4193994772aSChris Smith * obscure config data so information isn't plain text
4203994772aSChris Smith *
4213994772aSChris Smith * @param string       $str     data to be encoded
4223994772aSChris Smith * @param string       $code    encoding method, values: plain, base64, uuencode.
4233994772aSChris Smith * @return string               the encoded value
4243994772aSChris Smith */
4253994772aSChris Smithfunction conf_encodeString($str,$code) {
4263994772aSChris Smith    switch ($code) {
4273994772aSChris Smith        case 'base64'   : return '<b>'.base64_encode($str);
4283994772aSChris Smith        case 'uuencode' : return '<u>'.convert_uuencode($str);
4293994772aSChris Smith        case 'plain':
4303994772aSChris Smith        default:
4313994772aSChris Smith                          return $str;
4323994772aSChris Smith    }
4333994772aSChris Smith}
4343994772aSChris Smith/**
4353994772aSChris Smith * return obscured data as plain text
4363994772aSChris Smith *
4373994772aSChris Smith * @param  string      $str   encoded data
4383994772aSChris Smith * @return string             plain text
4393994772aSChris Smith */
4403994772aSChris Smithfunction conf_decodeString($str) {
4413994772aSChris Smith    switch (substr($str,0,3)) {
4423994772aSChris Smith        case '<b>' : return base64_decode(substr($str,3));
4433994772aSChris Smith        case '<u>' : return convert_uudecode(substr($str,3));
444b96ff25bSElan Ruusamäe        default:  // not encoded (or unknown)
4453994772aSChris Smith                     return $str;
4463994772aSChris Smith    }
4473994772aSChris Smith}
4484c353447SChristopher Smith
4494c353447SChristopher Smith/**
4504c353447SChristopher Smith * array combination function to remove negated values (prefixed by !)
4514c353447SChristopher Smith *
4524c353447SChristopher Smith * @param  array $current
4534c353447SChristopher Smith * @param  array $new
4544c353447SChristopher Smith *
4554c353447SChristopher Smith * @return array the combined array, numeric keys reset
4564c353447SChristopher Smith */
4574c353447SChristopher Smithfunction array_merge_with_removal($current, $new) {
4584c353447SChristopher Smith    foreach ($new as $val) {
45910b38f10SChristopher Smith        if (substr($val,0,1) == DOKU_CONF_NEGATION) {
4603a7669bdSChristopher Smith            $idx = array_search(trim(substr($val,1)),$current);
4614c353447SChristopher Smith            if ($idx !== false) {
4624c353447SChristopher Smith                unset($current[$idx]);
4634c353447SChristopher Smith            }
4644c353447SChristopher Smith        } else {
4653a7669bdSChristopher Smith            $current[] = trim($val);
4664c353447SChristopher Smith        }
4674c353447SChristopher Smith    }
4684c353447SChristopher Smith
4694c353447SChristopher Smith    return array_slice($current,0);
4704c353447SChristopher Smith}
471e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
472