xref: /dokuwiki/inc/confutils.php (revision c90718343a6d54f1212fc3900fca635375ea422d)
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 */
1410b38f10SChristopher Smithconst DOKU_CONF_NEGATION = '!';
15b625487dSandi
16b625487dSandi/**
17b625487dSandi * Returns the (known) extension and mimetype of a given filename
18b625487dSandi *
1927bf7924STom N Harris * If $knownonly is true (the default), then only known extensions
2027bf7924STom N Harris * are returned.
2127bf7924STom N Harris *
22b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
2342ea7f44SGerrit Uitslag *
2442ea7f44SGerrit Uitslag * @param string $file file name
2542ea7f44SGerrit Uitslag * @param bool   $knownonly
2642ea7f44SGerrit Uitslag * @return array with extension, mimetype and if it should be downloaded
27b625487dSandi */
2827bf7924STom N Harrisfunction mimetype($file, $knownonly=true){
29b625487dSandi    $mtypes = getMimeTypes();     // known mimetypes
30ad74fe66SAdrian Lang    $ext    = strrpos($file, '.');
31ad74fe66SAdrian Lang    if ($ext === false) {
32ad74fe66SAdrian Lang        return array(false, false, false);
3327bf7924STom N Harris    }
34ad74fe66SAdrian Lang    $ext = strtolower(substr($file, $ext + 1));
35ad74fe66SAdrian Lang    if (!isset($mtypes[$ext])){
36ad74fe66SAdrian Lang        if ($knownonly) {
37ad74fe66SAdrian Lang            return array(false, false, false);
38ecebf3a8SAndreas Gohr        } else {
39ad74fe66SAdrian Lang            return array($ext, 'application/octet-stream', true);
4027bf7924STom N Harris        }
41b625487dSandi    }
42ad74fe66SAdrian Lang    if($mtypes[$ext][0] == '!'){
43ad74fe66SAdrian Lang        return array($ext, substr($mtypes[$ext],1), true);
44ad74fe66SAdrian Lang    }else{
45ad74fe66SAdrian Lang        return array($ext, $mtypes[$ext], false);
46ad74fe66SAdrian Lang    }
47b625487dSandi}
48b625487dSandi
49b625487dSandi/**
50b625487dSandi * returns a hash of mimetypes
51b625487dSandi *
52b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
53b625487dSandi */
54b625487dSandifunction getMimeTypes() {
5549eb6e38SAndreas Gohr    static $mime = null;
56b625487dSandi    if ( !$mime ) {
57cb043f52SChris Smith        $mime = retrieveConfig('mime','confToHash');
5845ae4bb8SChristopher Smith        $mime = array_filter($mime);
59b625487dSandi    }
60b625487dSandi    return $mime;
61b625487dSandi}
62b625487dSandi
63b625487dSandi/**
64b625487dSandi * returns a hash of acronyms
65b625487dSandi *
66b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
67b625487dSandi */
68b625487dSandifunction getAcronyms() {
6949eb6e38SAndreas Gohr    static $acronyms = null;
70b625487dSandi    if ( !$acronyms ) {
71cb043f52SChris Smith        $acronyms = retrieveConfig('acronyms','confToHash');
72f266a919SChristopher Smith        $acronyms = array_filter($acronyms, 'strlen');
73b625487dSandi    }
74b625487dSandi    return $acronyms;
75b625487dSandi}
76b625487dSandi
77b625487dSandi/**
78b625487dSandi * returns a hash of smileys
79b625487dSandi *
80b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
81b625487dSandi */
82b625487dSandifunction getSmileys() {
8349eb6e38SAndreas Gohr    static $smileys = null;
84b625487dSandi    if ( !$smileys ) {
85cb043f52SChris Smith        $smileys = retrieveConfig('smileys','confToHash');
86f266a919SChristopher Smith        $smileys = array_filter($smileys, 'strlen');
87b625487dSandi    }
88b625487dSandi    return $smileys;
89b625487dSandi}
90b625487dSandi
91b625487dSandi/**
92b625487dSandi * returns a hash of entities
93b625487dSandi *
94b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
95b625487dSandi */
96b625487dSandifunction getEntities() {
9749eb6e38SAndreas Gohr    static $entities = null;
98b625487dSandi    if ( !$entities ) {
99cb043f52SChris Smith        $entities = retrieveConfig('entities','confToHash');
100f266a919SChristopher Smith        $entities = array_filter($entities, 'strlen');
101b625487dSandi    }
102b625487dSandi    return $entities;
103b625487dSandi}
104b625487dSandi
105b625487dSandi/**
106b625487dSandi * returns a hash of interwikilinks
107b625487dSandi *
108b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
109b625487dSandi */
110b625487dSandifunction getInterwiki() {
11149eb6e38SAndreas Gohr    static $wikis = null;
112b625487dSandi    if ( !$wikis ) {
1134c6a5eccSAndreas Gohr        $wikis = retrieveConfig('interwiki','confToHash',array(true));
114f266a919SChristopher Smith        $wikis = array_filter($wikis, 'strlen');
11545ae4bb8SChristopher Smith
11697a3e4e3Sandi        //add sepecial case 'this'
11727a2b085Sandi        $wikis['this'] = DOKU_URL.'{NAME}';
11845ae4bb8SChristopher Smith    }
119b625487dSandi    return $wikis;
120b625487dSandi}
121b625487dSandi
122b625487dSandi/**
123fa078663SAndreas Gohr * Returns the jquery script URLs for the versions defined in lib/scripts/jquery/versions
12461537d47SAndreas Gohr *
125fa078663SAndreas Gohr * @trigger CONFUTIL_CDN_SELECT
12661537d47SAndreas Gohr * @return array
12761537d47SAndreas Gohr */
128fa078663SAndreas Gohrfunction getCdnUrls() {
129fa078663SAndreas Gohr    global $conf;
130fa078663SAndreas Gohr
131fa078663SAndreas Gohr    // load version info
13261537d47SAndreas Gohr    $versions = array();
13361537d47SAndreas Gohr    $lines = file(DOKU_INC . 'lib/scripts/jquery/versions');
13461537d47SAndreas Gohr    foreach($lines as $line) {
1356453acd5SAndreas Gohr        $line = trim(preg_replace('/#.*$/', '', $line));
1366453acd5SAndreas Gohr        if($line === '') continue;
13761537d47SAndreas Gohr        list($key, $val) = explode('=', $line, 2);
13861537d47SAndreas Gohr        $key = trim($key);
13961537d47SAndreas Gohr        $val = trim($val);
14061537d47SAndreas Gohr        $versions[$key] = $val;
14161537d47SAndreas Gohr    }
142fa078663SAndreas Gohr
143fa078663SAndreas Gohr    $src = array();
144fa078663SAndreas Gohr    $data = array(
145fa078663SAndreas Gohr        'versions' => $versions,
146fa078663SAndreas Gohr        'src' => &$src
147fa078663SAndreas Gohr    );
148fa078663SAndreas Gohr    $event = new Doku_Event('CONFUTIL_CDN_SELECT', $data);
149fa078663SAndreas Gohr    if($event->advise_before()) {
150fa078663SAndreas Gohr        if(!$conf['jquerycdn']) {
151fa078663SAndreas Gohr            $jqmod = md5(join('-', $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/jquery-migrate-%s.min.js', $versions['JQM_VERSION']);
156fa078663SAndreas Gohr            $src[] = sprintf('https://code.jquery.com/ui/%s/jquery-ui.min.js', $versions['JQUI_VERSION']);
157fa078663SAndreas Gohr        } elseif($conf['jquerycdn'] == 'cdnjs') {
158fa078663SAndreas Gohr            $src[] = sprintf('https://cdnjs.cloudflare.com/ajax/libs/jquery/%s/jquery.min.js', $versions['JQ_VERSION']);
159fa078663SAndreas Gohr            $src[] = sprintf('https://cdnjs.cloudflare.com/ajax/libs/jquery-migrate/%s/jquery-migrate.min.js', $versions['JQM_VERSION']);
160fa078663SAndreas Gohr            $src[] = sprintf('https://cdnjs.cloudflare.com/ajax/libs/jqueryui/%s/jquery-ui.min.js', $versions['JQUI_VERSION']);
161fa078663SAndreas Gohr        }
162fa078663SAndreas Gohr    }
163fa078663SAndreas Gohr    $event->advise_after();
164fa078663SAndreas Gohr
165fa078663SAndreas Gohr    return $src;
16661537d47SAndreas Gohr}
16761537d47SAndreas Gohr
16861537d47SAndreas Gohr/**
169b9ac8716Schris * returns array of wordblock patterns
170b9ac8716Schris *
171b9ac8716Schris */
172b9ac8716Schrisfunction getWordblocks() {
17349eb6e38SAndreas Gohr    static $wordblocks = null;
174b9ac8716Schris    if ( !$wordblocks ) {
1754c353447SChristopher Smith        $wordblocks = retrieveConfig('wordblock','file',null,'array_merge_with_removal');
176b9ac8716Schris    }
177b9ac8716Schris    return $wordblocks;
178b9ac8716Schris}
179b9ac8716Schris
180e3ab6fc5SMichael Hamann/**
181e3ab6fc5SMichael Hamann * Gets the list of configured schemes
182e3ab6fc5SMichael Hamann *
183e3ab6fc5SMichael Hamann * @return array the schemes
184e3ab6fc5SMichael Hamann */
18536f2d7c1SGina Haeussgefunction getSchemes() {
18649eb6e38SAndreas Gohr    static $schemes = null;
18736f2d7c1SGina Haeussge    if ( !$schemes ) {
1884c353447SChristopher Smith        $schemes = retrieveConfig('scheme','file',null,'array_merge_with_removal');
18936f2d7c1SGina Haeussge        $schemes = array_map('trim', $schemes);
19036f2d7c1SGina Haeussge        $schemes = preg_replace('/^#.*/', '', $schemes);
19136f2d7c1SGina Haeussge        $schemes = array_filter($schemes);
1924c353447SChristopher Smith    }
19336f2d7c1SGina Haeussge    return $schemes;
19436f2d7c1SGina Haeussge}
19536f2d7c1SGina Haeussge
196b9ac8716Schris/**
197edcb01e5SGina Haeussge * Builds a hash from an array of lines
198b625487dSandi *
1993fd0b676Sandi * If $lower is set to true all hash keys are converted to
2003fd0b676Sandi * lower case.
2013fd0b676Sandi *
202b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com>
2033fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org>
204edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
205f50a239bSTakamura *
206f50a239bSTakamura * @param array $lines
207f50a239bSTakamura * @param bool $lower
208f50a239bSTakamura *
209f50a239bSTakamura * @return array
210b625487dSandi */
211edcb01e5SGina Haeussgefunction linesToHash($lines, $lower = false) {
212e5fc893fSAndreas Gohr    $conf = array();
213dd74fecfSMichael Hamann    // remove BOM
214dd74fecfSMichael Hamann    if(isset($lines[0]) && substr($lines[0], 0, 3) == pack('CCC', 0xef, 0xbb, 0xbf))
215dd74fecfSMichael Hamann        $lines[0] = substr($lines[0], 3);
216b625487dSandi    foreach($lines as $line) {
21703ff8795SAndreas Gohr        //ignore comments (except escaped ones)
21803ff8795SAndreas Gohr        $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line);
21903ff8795SAndreas Gohr        $line = str_replace('\\#', '#', $line);
220b625487dSandi        $line = trim($line);
22159ed97f2SAndreas Gohr        if($line === '') continue;
222b625487dSandi        $line = preg_split('/\s+/', $line, 2);
22359ed97f2SAndreas Gohr        $line = array_pad($line, 2, '');
224b625487dSandi        // Build the associative array
22527a2b085Sandi        if($lower) {
22627a2b085Sandi            $conf[strtolower($line[0])] = $line[1];
22727a2b085Sandi        } else {
228b625487dSandi            $conf[$line[0]] = $line[1];
229b625487dSandi        }
23027a2b085Sandi    }
231b625487dSandi
232b625487dSandi    return $conf;
233b625487dSandi}
234b625487dSandi
235409d7af7SAndreas Gohr/**
236edcb01e5SGina Haeussge * Builds a hash from a configfile
237edcb01e5SGina Haeussge *
238edcb01e5SGina Haeussge * If $lower is set to true all hash keys are converted to
239edcb01e5SGina Haeussge * lower case.
240edcb01e5SGina Haeussge *
241edcb01e5SGina Haeussge * @author Harry Fuecks <hfuecks@gmail.com>
242edcb01e5SGina Haeussge * @author Andreas Gohr <andi@splitbrain.org>
243edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net>
244f50a239bSTakamura *
245f50a239bSTakamura * @param string $file
246f50a239bSTakamura * @param bool $lower
247f50a239bSTakamura *
248f50a239bSTakamura * @return array
249edcb01e5SGina Haeussge */
250edcb01e5SGina Haeussgefunction confToHash($file,$lower=false) {
251edcb01e5SGina Haeussge    $conf = array();
252edcb01e5SGina Haeussge    $lines = @file( $file );
253edcb01e5SGina Haeussge    if ( !$lines ) return $conf;
254edcb01e5SGina Haeussge
255edcb01e5SGina Haeussge    return linesToHash($lines, $lower);
256edcb01e5SGina Haeussge}
257edcb01e5SGina Haeussge
258edcb01e5SGina Haeussge/**
259*c9071834SMichael Große * Read a json config file into an array
260*c9071834SMichael Große *
261*c9071834SMichael Große * @param string $file
262*c9071834SMichael Große * @return array
263*c9071834SMichael Große */
264*c9071834SMichael Großefunction jsonToArray($file)
265*c9071834SMichael Große{
266*c9071834SMichael Große    $json = file_get_contents($file);
267*c9071834SMichael Große
268*c9071834SMichael Große    $conf = json_decode($json, true);
269*c9071834SMichael Große
270*c9071834SMichael Große    $jsonError = json_last_error();
271*c9071834SMichael Große    if (!is_array($conf) && $jsonError !== JSON_ERROR_NONE) {
272*c9071834SMichael Große
273*c9071834SMichael Große        switch ($jsonError) {
274*c9071834SMichael Große            case JSON_ERROR_DEPTH:
275*c9071834SMichael Große                $jsonErrorText = 'The maximum stack depth has been exceeded';
276*c9071834SMichael Große                break;
277*c9071834SMichael Große            case JSON_ERROR_STATE_MISMATCH:
278*c9071834SMichael Große                $jsonErrorText = 'Invalid or malformed JSON';
279*c9071834SMichael Große                break;
280*c9071834SMichael Große            case JSON_ERROR_CTRL_CHAR:
281*c9071834SMichael Große                $jsonErrorText = 'Control character error, possibly incorrectly encoded';
282*c9071834SMichael Große                break;
283*c9071834SMichael Große            case JSON_ERROR_SYNTAX:
284*c9071834SMichael Große                $jsonErrorText = 'Syntax error';
285*c9071834SMichael Große                break;
286*c9071834SMichael Große            case JSON_ERROR_UTF8:
287*c9071834SMichael Große                $jsonErrorText = 'Malformed UTF-8 characters, possibly incorrectly encoded';
288*c9071834SMichael Große                break;
289*c9071834SMichael Große            case JSON_ERROR_RECURSION:
290*c9071834SMichael Große                $jsonErrorText = 'One or more recursive references in the value to be encoded';
291*c9071834SMichael Große                break;
292*c9071834SMichael Große            case JSON_ERROR_INF_OR_NAN:
293*c9071834SMichael Große                $jsonErrorText = 'One or more NAN or INF values in the value to be encoded';
294*c9071834SMichael Große                break;
295*c9071834SMichael Große            case JSON_ERROR_UNSUPPORTED_TYPE:
296*c9071834SMichael Große                $jsonErrorText = 'A value of a type that cannot be encoded was given';
297*c9071834SMichael Große                break;
298*c9071834SMichael Große            case JSON_ERROR_INVALID_PROPERTY_NAME:
299*c9071834SMichael Große                $jsonErrorText = 'A property name that cannot be encoded was given';
300*c9071834SMichael Große                break;
301*c9071834SMichael Große            case JSON_ERROR_UTF16:
302*c9071834SMichael Große                $jsonErrorText = 'Malformed UTF-16 characters, possibly incorrectly encoded';
303*c9071834SMichael Große                break;
304*c9071834SMichael Große            default:
305*c9071834SMichael Große                $jsonErrorText = 'Unknown Error Code';
306*c9071834SMichael Große        }
307*c9071834SMichael Große
308*c9071834SMichael Große        trigger_error('JSON decoding error "' . $jsonErrorText . '" for file ' . $file, E_USER_WARNING);
309*c9071834SMichael Große        return [];
310*c9071834SMichael Große    }
311*c9071834SMichael Große
312*c9071834SMichael Große    return $conf;
313*c9071834SMichael Große}
314*c9071834SMichael Große
315*c9071834SMichael Große/**
316cb043f52SChris Smith * Retrieve the requested configuration information
317cb043f52SChris Smith *
318cb043f52SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
319cb043f52SChris Smith *
320cb043f52SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
321cb043f52SChris Smith * @param  callback $fn       the function used to process the configuration file into an array
322e3ab6fc5SMichael Hamann * @param  array    $params   optional additional params to pass to the callback
3235a9597bbSTakamura * @param  callback $combine  the function used to combine arrays of values read from different configuration files;
324074b2b3fSTakamura *                            the function takes two parameters,
325074b2b3fSTakamura *                               $combined - the already read & merged configuration values
326074b2b3fSTakamura *                               $new - array of config values from the config cascade file being currently processed
327074b2b3fSTakamura *                            and returns an array of the merged configuration values.
328cb043f52SChris Smith * @return array    configuration values
329cb043f52SChris Smith */
3304c353447SChristopher Smithfunction retrieveConfig($type,$fn,$params=null,$combine='array_merge') {
331cb043f52SChris Smith    global $config_cascade;
332cb043f52SChris Smith
3334c6a5eccSAndreas Gohr    if(!is_array($params)) $params = array();
3344c6a5eccSAndreas Gohr
335cb043f52SChris Smith    $combined = array();
336cb043f52SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
337b303b92cSChris Smith    foreach (array('default','local','protected') as $config_group) {
338b303b92cSChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
339b303b92cSChris Smith        foreach ($config_cascade[$type][$config_group] as $file) {
34079e79377SAndreas Gohr            if (file_exists($file)) {
3414c6a5eccSAndreas Gohr                $config = call_user_func_array($fn,array_merge(array($file),$params));
3424c353447SChristopher Smith                $combined = $combine($combined, $config);
343cb043f52SChris Smith            }
344cb043f52SChris Smith        }
345b303b92cSChris Smith    }
346cb043f52SChris Smith
347cb043f52SChris Smith    return $combined;
348cb043f52SChris Smith}
349cb043f52SChris Smith
350cb043f52SChris Smith/**
351f8121585SChris Smith * Include the requested configuration information
352f8121585SChris Smith *
353f8121585SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
354f8121585SChris Smith *
355f8121585SChris Smith * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
356f8121585SChris Smith * @return array              list of files, default before local before protected
357f8121585SChris Smith */
358f8121585SChris Smithfunction getConfigFiles($type) {
359f8121585SChris Smith    global $config_cascade;
360f8121585SChris Smith    $files = array();
361f8121585SChris Smith
362f8121585SChris Smith    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
363f8121585SChris Smith    foreach (array('default','local','protected') as $config_group) {
364f8121585SChris Smith        if (empty($config_cascade[$type][$config_group])) continue;
365f8121585SChris Smith        $files = array_merge($files, $config_cascade[$type][$config_group]);
366f8121585SChris Smith    }
367f8121585SChris Smith
368f8121585SChris Smith    return $files;
369f8121585SChris Smith}
370f8121585SChris Smith
371f8121585SChris Smith/**
372409d7af7SAndreas Gohr * check if the given action was disabled in config
373409d7af7SAndreas Gohr *
374409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3750e2431b7SGerrit Uitslag * @param string $action
376409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled
377409d7af7SAndreas Gohr */
378409d7af7SAndreas Gohrfunction actionOK($action){
379409d7af7SAndreas Gohr    static $disabled = null;
380020ea9e1SChristopher Smith    if(is_null($disabled) || defined('SIMPLE_TEST')){
381409d7af7SAndreas Gohr        global $conf;
3820e2431b7SGerrit Uitslag        /** @var DokuWiki_Auth_Plugin $auth */
383de4d479aSAdrian Lang        global $auth;
384409d7af7SAndreas Gohr
385409d7af7SAndreas Gohr        // prepare disabled actions array and handle legacy options
386409d7af7SAndreas Gohr        $disabled = explode(',',$conf['disableactions']);
387409d7af7SAndreas Gohr        $disabled = array_map('trim',$disabled);
388e4eda66bSAndreas Gohr        if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) {
389de4d479aSAdrian Lang            $disabled[] = 'register';
390de4d479aSAdrian Lang        }
391e4eda66bSAndreas Gohr        if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) {
392de4d479aSAdrian Lang            $disabled[] = 'resendpwd';
393de4d479aSAdrian Lang        }
394e4eda66bSAndreas Gohr        if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) {
3953a48618aSAnika Henke            $disabled[] = 'subscribe';
3963a48618aSAnika Henke        }
3973a48618aSAnika Henke        if (is_null($auth) || !$auth->canDo('Profile')) {
3983a48618aSAnika Henke            $disabled[] = 'profile';
3993a48618aSAnika Henke        }
4002a7abf2dSChristopher Smith        if (is_null($auth) || !$auth->canDo('delUser')) {
4012a7abf2dSChristopher Smith            $disabled[] = 'profile_delete';
4022a7abf2dSChristopher Smith        }
4033a48618aSAnika Henke        if (is_null($auth)) {
4043a48618aSAnika Henke            $disabled[] = 'login';
4053a48618aSAnika Henke        }
4063a48618aSAnika Henke        if (is_null($auth) || !$auth->canDo('logout')) {
4073a48618aSAnika Henke            $disabled[] = 'logout';
4083a48618aSAnika Henke        }
409409d7af7SAndreas Gohr        $disabled = array_unique($disabled);
410409d7af7SAndreas Gohr    }
411409d7af7SAndreas Gohr
412409d7af7SAndreas Gohr    return !in_array($action,$disabled);
413409d7af7SAndreas Gohr}
414409d7af7SAndreas Gohr
415fe9ec250SChris Smith/**
416fe9ec250SChris Smith * check if headings should be used as link text for the specified link type
417fe9ec250SChris Smith *
418fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk>
419fe9ec250SChris Smith *
420fe9ec250SChris Smith * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
421fe9ec250SChris Smith *                                                      navigation applies to all other links
422e3ab6fc5SMichael Hamann * @return  boolean             true if headings should be used for $linktype, false otherwise
423fe9ec250SChris Smith */
424fe9ec250SChris Smithfunction useHeading($linktype) {
425fe9ec250SChris Smith    static $useHeading = null;
4266506eaacSAndreas Gohr    if(defined('DOKU_UNITTEST')) $useHeading = null; // don't cache during unit tests
427fe9ec250SChris Smith
428fe9ec250SChris Smith    if (is_null($useHeading)) {
429fe9ec250SChris Smith        global $conf;
430fe9ec250SChris Smith
431fe9ec250SChris Smith        if (!empty($conf['useheading'])) {
432fe9ec250SChris Smith            switch ($conf['useheading']) {
43349eb6e38SAndreas Gohr                case 'content':
43449eb6e38SAndreas Gohr                    $useHeading['content'] = true;
43549eb6e38SAndreas Gohr                    break;
43649eb6e38SAndreas Gohr
43749eb6e38SAndreas Gohr                case 'navigation':
43849eb6e38SAndreas Gohr                    $useHeading['navigation'] = true;
43949eb6e38SAndreas Gohr                    break;
440fe9ec250SChris Smith                default:
441fe9ec250SChris Smith                    $useHeading['content'] = true;
442fe9ec250SChris Smith                    $useHeading['navigation'] = true;
443fe9ec250SChris Smith            }
444fe9ec250SChris Smith        } else {
445fe9ec250SChris Smith            $useHeading = array();
446fe9ec250SChris Smith        }
447fe9ec250SChris Smith    }
448fe9ec250SChris Smith
449fe9ec250SChris Smith    return (!empty($useHeading[$linktype]));
450fe9ec250SChris Smith}
451fe9ec250SChris Smith
4523994772aSChris Smith/**
4533994772aSChris Smith * obscure config data so information isn't plain text
4543994772aSChris Smith *
4553994772aSChris Smith * @param string       $str     data to be encoded
4563994772aSChris Smith * @param string       $code    encoding method, values: plain, base64, uuencode.
4573994772aSChris Smith * @return string               the encoded value
4583994772aSChris Smith */
4593994772aSChris Smithfunction conf_encodeString($str,$code) {
4603994772aSChris Smith    switch ($code) {
4613994772aSChris Smith        case 'base64'   : return '<b>'.base64_encode($str);
4623994772aSChris Smith        case 'uuencode' : return '<u>'.convert_uuencode($str);
4633994772aSChris Smith        case 'plain':
4643994772aSChris Smith        default:
4653994772aSChris Smith                          return $str;
4663994772aSChris Smith    }
4673994772aSChris Smith}
4683994772aSChris Smith/**
4693994772aSChris Smith * return obscured data as plain text
4703994772aSChris Smith *
4713994772aSChris Smith * @param  string      $str   encoded data
4723994772aSChris Smith * @return string             plain text
4733994772aSChris Smith */
4743994772aSChris Smithfunction conf_decodeString($str) {
4753994772aSChris Smith    switch (substr($str,0,3)) {
4763994772aSChris Smith        case '<b>' : return base64_decode(substr($str,3));
4773994772aSChris Smith        case '<u>' : return convert_uudecode(substr($str,3));
478b96ff25bSElan Ruusamäe        default:  // not encoded (or unknown)
4793994772aSChris Smith                     return $str;
4803994772aSChris Smith    }
4813994772aSChris Smith}
4824c353447SChristopher Smith
4834c353447SChristopher Smith/**
4844c353447SChristopher Smith * array combination function to remove negated values (prefixed by !)
4854c353447SChristopher Smith *
4864c353447SChristopher Smith * @param  array $current
4874c353447SChristopher Smith * @param  array $new
4884c353447SChristopher Smith *
4894c353447SChristopher Smith * @return array the combined array, numeric keys reset
4904c353447SChristopher Smith */
4914c353447SChristopher Smithfunction array_merge_with_removal($current, $new) {
4924c353447SChristopher Smith    foreach ($new as $val) {
49310b38f10SChristopher Smith        if (substr($val,0,1) == DOKU_CONF_NEGATION) {
4943a7669bdSChristopher Smith            $idx = array_search(trim(substr($val,1)),$current);
4954c353447SChristopher Smith            if ($idx !== false) {
4964c353447SChristopher Smith                unset($current[$idx]);
4974c353447SChristopher Smith            }
4984c353447SChristopher Smith        } else {
4993a7669bdSChristopher Smith            $current[] = trim($val);
5004c353447SChristopher Smith        }
5014c353447SChristopher Smith    }
5024c353447SChristopher Smith
5034c353447SChristopher Smith    return array_slice($current,0);
5044c353447SChristopher Smith}
505e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 :
506