xref: /dokuwiki/inc/confutils.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
1<?php
2
3/**
4 * Utilities for collecting data from config files
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Harry Fuecks <hfuecks@gmail.com>
8 */
9
10/*
11 * line prefix used to negate single value config items
12 * (scheme.conf & stopwords.conf), e.g.
13 * !gopher
14 */
15
16use dokuwiki\Extension\AuthPlugin;
17use dokuwiki\Extension\Event;
18
19const DOKU_CONF_NEGATION = '!';
20
21/**
22 * Returns the (known) extension and mimetype of a given filename
23 *
24 * If $knownonly is true (the default), then only known extensions
25 * are returned.
26 *
27 * @author Andreas Gohr <andi@splitbrain.org>
28 *
29 * @param string $file file name
30 * @param bool   $knownonly
31 * @return array with extension, mimetype and if it should be downloaded
32 */
33function mimetype($file, $knownonly = true)
34{
35    $mtypes = getMimeTypes();     // known mimetypes
36    $ext    = strrpos($file, '.');
37    if ($ext === false) {
38        return [false, false, false];
39    }
40    $ext = strtolower(substr($file, $ext + 1));
41    if (!isset($mtypes[$ext])) {
42        if ($knownonly) {
43            return [false, false, false];
44        } else {
45            return [$ext, 'application/octet-stream', true];
46        }
47    }
48    if ($mtypes[$ext][0] == '!') {
49        return [$ext, substr($mtypes[$ext], 1), true];
50    } else {
51        return [$ext, $mtypes[$ext], false];
52    }
53}
54
55/**
56 * returns a hash of mimetypes
57 *
58 * @author Andreas Gohr <andi@splitbrain.org>
59 */
60function getMimeTypes()
61{
62    static $mime = null;
63    if (!$mime) {
64        $mime = retrieveConfig('mime', 'confToHash');
65        $mime = array_filter($mime);
66    }
67    return $mime;
68}
69
70/**
71 * returns a hash of acronyms
72 *
73 * @author Harry Fuecks <hfuecks@gmail.com>
74 */
75function getAcronyms()
76{
77    static $acronyms = null;
78    if (!$acronyms) {
79        $acronyms = retrieveConfig('acronyms', 'confToHash');
80        $acronyms = array_filter($acronyms, 'strlen');
81    }
82    return $acronyms;
83}
84
85/**
86 * returns a hash of smileys
87 *
88 * @author Harry Fuecks <hfuecks@gmail.com>
89 */
90function getSmileys()
91{
92    static $smileys = null;
93    if (!$smileys) {
94        $smileys = retrieveConfig('smileys', 'confToHash');
95        $smileys = array_filter($smileys, 'strlen');
96    }
97    return $smileys;
98}
99
100/**
101 * returns a hash of entities
102 *
103 * @author Harry Fuecks <hfuecks@gmail.com>
104 */
105function getEntities()
106{
107    static $entities = null;
108    if (!$entities) {
109        $entities = retrieveConfig('entities', 'confToHash');
110        $entities = array_filter($entities, 'strlen');
111    }
112    return $entities;
113}
114
115/**
116 * returns a hash of interwikilinks
117 *
118 * @author Harry Fuecks <hfuecks@gmail.com>
119 */
120function getInterwiki()
121{
122    static $wikis = null;
123    if (!$wikis) {
124        $wikis = retrieveConfig('interwiki', 'confToHash', [true]);
125        $wikis = array_filter($wikis, 'strlen');
126
127        //add sepecial case 'this'
128        $wikis['this'] = DOKU_URL . '{NAME}';
129    }
130    return $wikis;
131}
132
133/**
134 * Returns the jquery script URLs for the versions defined in lib/scripts/jquery/versions
135 *
136 * @trigger CONFUTIL_CDN_SELECT
137 * @return array
138 */
139function getCdnUrls()
140{
141    global $conf;
142
143    // load version info
144    $versions = [];
145    $lines = file(DOKU_INC . 'lib/scripts/jquery/versions');
146    foreach ($lines as $line) {
147        $line = trim(preg_replace('/#.*$/', '', $line));
148        if ($line === '') continue;
149        [$key, $val] = sexplode('=', $line, 2, '');
150        $key = trim($key);
151        $val = trim($val);
152        $versions[$key] = $val;
153    }
154
155    $src = [];
156    $data = ['versions' => $versions, 'src' => &$src];
157    $event = new Event('CONFUTIL_CDN_SELECT', $data);
158    if ($event->advise_before()) {
159        if (!$conf['jquerycdn']) {
160            $jqmod = md5(implode('-', $versions));
161            $src[] = DOKU_BASE . 'lib/exe/jquery.php' . '?tseed=' . $jqmod;
162        } elseif ($conf['jquerycdn'] == 'jquery') {
163            $src[] = sprintf('https://code.jquery.com/jquery-%s.min.js', $versions['JQ_VERSION']);
164            $src[] = sprintf('https://code.jquery.com/ui/%s/jquery-ui.min.js', $versions['JQUI_VERSION']);
165        } elseif ($conf['jquerycdn'] == 'cdnjs') {
166            $src[] = sprintf(
167                'https://cdnjs.cloudflare.com/ajax/libs/jquery/%s/jquery.min.js',
168                $versions['JQ_VERSION']
169            );
170            $src[] = sprintf(
171                'https://cdnjs.cloudflare.com/ajax/libs/jqueryui/%s/jquery-ui.min.js',
172                $versions['JQUI_VERSION']
173            );
174        }
175    }
176    $event->advise_after();
177
178    return $src;
179}
180
181/**
182 * returns array of wordblock patterns
183 *
184 */
185function getWordblocks()
186{
187    static $wordblocks = null;
188    if (!$wordblocks) {
189        $wordblocks = retrieveConfig('wordblock', 'file', null, 'array_merge_with_removal');
190    }
191    return $wordblocks;
192}
193
194/**
195 * Gets the list of configured schemes
196 *
197 * @return array the schemes
198 */
199function getSchemes()
200{
201    static $schemes = null;
202    if (!$schemes) {
203        $schemes = retrieveConfig('scheme', 'file', null, 'array_merge_with_removal');
204        $schemes = array_map('trim', $schemes);
205        $schemes = preg_replace('/^#.*/', '', $schemes);
206        $schemes = array_filter($schemes);
207    }
208    return $schemes;
209}
210
211/**
212 * Builds a hash from an array of lines
213 *
214 * If $lower is set to true all hash keys are converted to
215 * lower case.
216 *
217 * @author Harry Fuecks <hfuecks@gmail.com>
218 * @author Andreas Gohr <andi@splitbrain.org>
219 * @author Gina Haeussge <gina@foosel.net>
220 *
221 * @param array $lines
222 * @param bool $lower
223 *
224 * @return array
225 */
226function linesToHash($lines, $lower = false)
227{
228    $conf = [];
229    // remove BOM
230    if (isset($lines[0]) && substr($lines[0], 0, 3) === pack('CCC', 0xef, 0xbb, 0xbf))
231        $lines[0] = substr($lines[0], 3);
232    foreach ($lines as $line) {
233        //ignore comments (except escaped ones)
234        $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line);
235        $line = str_replace('\\#', '#', $line);
236        $line = trim($line);
237        if ($line === '') continue;
238        $line = preg_split('/\s+/', $line, 2);
239        $line = array_pad($line, 2, '');
240        // Build the associative array
241        if ($lower) {
242            $conf[strtolower($line[0])] = $line[1];
243        } else {
244            $conf[$line[0]] = $line[1];
245        }
246    }
247
248    return $conf;
249}
250
251/**
252 * Builds a hash from a configfile
253 *
254 * If $lower is set to true all hash keys are converted to
255 * lower case.
256 *
257 * @author Harry Fuecks <hfuecks@gmail.com>
258 * @author Andreas Gohr <andi@splitbrain.org>
259 * @author Gina Haeussge <gina@foosel.net>
260 *
261 * @param string $file
262 * @param bool $lower
263 *
264 * @return array
265 */
266function confToHash($file, $lower = false)
267{
268    $conf = [];
269    $lines = @file($file);
270    if (!$lines) return $conf;
271
272    return linesToHash($lines, $lower);
273}
274
275/**
276 * Read a json config file into an array
277 *
278 * @param string $file
279 * @return array
280 */
281function jsonToArray($file)
282{
283    $json = file_get_contents($file);
284
285    $conf = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
286
287    if ($conf === null) {
288        return [];
289    }
290
291    return $conf;
292}
293
294/**
295 * Retrieve the requested configuration information
296 *
297 * @author Chris Smith <chris@jalakai.co.uk>
298 *
299 * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
300 * @param  callback $fn       the function used to process the configuration file into an array
301 * @param  array    $params   optional additional params to pass to the callback
302 * @param  callback $combine  the function used to combine arrays of values read from different configuration files;
303 *                            the function takes two parameters,
304 *                               $combined - the already read & merged configuration values
305 *                               $new - array of config values from the config cascade file being currently processed
306 *                            and returns an array of the merged configuration values.
307 * @return array    configuration values
308 */
309function retrieveConfig($type, $fn, $params = null, $combine = 'array_merge')
310{
311    global $config_cascade;
312
313    if (!is_array($params)) $params = [];
314
315    $combined = [];
316    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "' . $type . '"', E_USER_WARNING);
317    foreach (['default', 'local', 'protected'] as $config_group) {
318        if (empty($config_cascade[$type][$config_group])) continue;
319        foreach ($config_cascade[$type][$config_group] as $file) {
320            if (file_exists($file)) {
321                $config = call_user_func_array($fn, array_merge([$file], $params));
322                $combined = $combine($combined, $config);
323            }
324        }
325    }
326
327    return $combined;
328}
329
330/**
331 * Include the requested configuration information
332 *
333 * @author Chris Smith <chris@jalakai.co.uk>
334 *
335 * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
336 * @return array              list of files, default before local before protected
337 */
338function getConfigFiles($type)
339{
340    global $config_cascade;
341    $files = [];
342
343    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "' . $type . '"', E_USER_WARNING);
344    foreach (['default', 'local', 'protected'] as $config_group) {
345        if (empty($config_cascade[$type][$config_group])) continue;
346        $files = array_merge($files, $config_cascade[$type][$config_group]);
347    }
348
349    return $files;
350}
351
352/**
353 * check if the given action was disabled in config
354 *
355 * @author Andreas Gohr <andi@splitbrain.org>
356 * @param string $action
357 * @returns boolean true if enabled, false if disabled
358 */
359function actionOK($action)
360{
361    static $disabled = null;
362    if (is_null($disabled) || defined('SIMPLE_TEST')) {
363        global $conf;
364        /** @var AuthPlugin $auth */
365        global $auth;
366
367        // prepare disabled actions array and handle legacy options
368        $disabled = explode(',', $conf['disableactions']);
369        $disabled = array_map('trim', $disabled);
370        if ((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) {
371            $disabled[] = 'register';
372        }
373        if ((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) {
374            $disabled[] = 'resendpwd';
375        }
376        if ((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) {
377            $disabled[] = 'subscribe';
378        }
379        if (is_null($auth) || !$auth->canDo('Profile')) {
380            $disabled[] = 'profile';
381        }
382        if (is_null($auth) || !$auth->canDo('delUser')) {
383            $disabled[] = 'profile_delete';
384        }
385        if (is_null($auth)) {
386            $disabled[] = 'login';
387        }
388        if (is_null($auth) || !$auth->canDo('logout')) {
389            $disabled[] = 'logout';
390        }
391        $disabled = array_unique($disabled);
392    }
393
394    return !in_array($action, $disabled);
395}
396
397/**
398 * check if headings should be used as link text for the specified link type
399 *
400 * @author Chris Smith <chris@jalakai.co.uk>
401 *
402 * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
403 *                                                      navigation applies to all other links
404 * @return  boolean             true if headings should be used for $linktype, false otherwise
405 */
406function useHeading($linktype)
407{
408    static $useHeading = null;
409    if (defined('DOKU_UNITTEST')) $useHeading = null; // don't cache during unit tests
410
411    if (is_null($useHeading)) {
412        global $conf;
413
414        if (!empty($conf['useheading'])) {
415            switch ($conf['useheading']) {
416                case 'content':
417                    $useHeading['content'] = true;
418                    break;
419
420                case 'navigation':
421                    $useHeading['navigation'] = true;
422                    break;
423                default:
424                    $useHeading['content'] = true;
425                    $useHeading['navigation'] = true;
426            }
427        } else {
428            $useHeading = [];
429        }
430    }
431
432    return (!empty($useHeading[$linktype]));
433}
434
435/**
436 * obscure config data so information isn't plain text
437 *
438 * @param string       $str     data to be encoded
439 * @param string       $code    encoding method, values: plain, base64, uuencode.
440 * @return string               the encoded value
441 */
442function conf_encodeString($str, $code)
443{
444    switch ($code) {
445        case 'base64':
446            return '<b>' . base64_encode($str);
447        case 'uuencode':
448            return '<u>' . convert_uuencode($str);
449        case 'plain':
450        default:
451            return $str;
452    }
453}
454/**
455 * return obscured data as plain text
456 *
457 * @param  string      $str   encoded data
458 * @return string             plain text
459 */
460function conf_decodeString($str)
461{
462    switch (substr($str, 0, 3)) {
463        case '<b>':
464            return base64_decode(substr($str, 3));
465        case '<u>':
466            return convert_uudecode(substr($str, 3));
467        default:  // not encoded (or unknown)
468            return $str;
469    }
470}
471
472/**
473 * array combination function to remove negated values (prefixed by !)
474 *
475 * @param  array $current
476 * @param  array $new
477 *
478 * @return array the combined array, numeric keys reset
479 */
480function array_merge_with_removal($current, $new)
481{
482    foreach ($new as $val) {
483        if (substr($val, 0, 1) == DOKU_CONF_NEGATION) {
484            $idx = array_search(trim(substr($val, 1)), $current);
485            if ($idx !== false) {
486                unset($current[$idx]);
487            }
488        } else {
489            $current[] = trim($val);
490        }
491    }
492
493    return array_slice($current, 0);
494}
495//Setup VIM: ex: et ts=4 :
496