🤖 Rector and PHPCS fixes
[dokuwiki.git] / inc / confutils.php
blob6be7a1a72a6130f4a0f9524db4c09412600bb3d2
1 <?php
3 /**
4 * Utilities for collecting data from config files
6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author Harry Fuecks <hfuecks@gmail.com>
8 */
11 * line prefix used to negate single value config items
12 * (scheme.conf & stopwords.conf), e.g.
13 * !gopher
16 use dokuwiki\Extension\AuthPlugin;
17 use dokuwiki\Extension\Event;
19 const DOKU_CONF_NEGATION = '!';
21 /**
22 * Returns the (known) extension and mimetype of a given filename
24 * If $knownonly is true (the default), then only known extensions
25 * are returned.
27 * @author Andreas Gohr <andi@splitbrain.org>
29 * @param string $file file name
30 * @param bool $knownonly
31 * @return array with extension, mimetype and if it should be downloaded
33 function mimetype($file, $knownonly = true)
35 $mtypes = getMimeTypes(); // known mimetypes
36 $ext = strrpos($file, '.');
37 if ($ext === false) {
38 return [false, false, false];
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];
48 if ($mtypes[$ext][0] == '!') {
49 return [$ext, substr($mtypes[$ext], 1), true];
50 } else {
51 return [$ext, $mtypes[$ext], false];
55 /**
56 * returns a hash of mimetypes
58 * @author Andreas Gohr <andi@splitbrain.org>
60 function getMimeTypes()
62 static $mime = null;
63 if (!$mime) {
64 $mime = retrieveConfig('mime', 'confToHash');
65 $mime = array_filter($mime);
67 return $mime;
70 /**
71 * returns a hash of acronyms
73 * @author Harry Fuecks <hfuecks@gmail.com>
75 function getAcronyms()
77 static $acronyms = null;
78 if (!$acronyms) {
79 $acronyms = retrieveConfig('acronyms', 'confToHash');
80 $acronyms = array_filter($acronyms, 'strlen');
82 return $acronyms;
85 /**
86 * returns a hash of smileys
88 * @author Harry Fuecks <hfuecks@gmail.com>
90 function getSmileys()
92 static $smileys = null;
93 if (!$smileys) {
94 $smileys = retrieveConfig('smileys', 'confToHash');
95 $smileys = array_filter($smileys, 'strlen');
97 return $smileys;
101 * returns a hash of entities
103 * @author Harry Fuecks <hfuecks@gmail.com>
105 function getEntities()
107 static $entities = null;
108 if (!$entities) {
109 $entities = retrieveConfig('entities', 'confToHash');
110 $entities = array_filter($entities, 'strlen');
112 return $entities;
116 * returns a hash of interwikilinks
118 * @author Harry Fuecks <hfuecks@gmail.com>
120 function getInterwiki()
122 static $wikis = null;
123 if (!$wikis) {
124 $wikis = retrieveConfig('interwiki', 'confToHash', [true]);
125 $wikis = array_filter($wikis, 'strlen');
127 //add sepecial case 'this'
128 $wikis['this'] = DOKU_URL . '{NAME}';
130 return $wikis;
134 * Returns the jquery script URLs for the versions defined in lib/scripts/jquery/versions
136 * @trigger CONFUTIL_CDN_SELECT
137 * @return array
139 function getCdnUrls()
141 global $conf;
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;
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']
170 $src[] = sprintf(
171 'https://cdnjs.cloudflare.com/ajax/libs/jqueryui/%s/jquery-ui.min.js',
172 $versions['JQUI_VERSION']
176 $event->advise_after();
178 return $src;
182 * returns array of wordblock patterns
185 function getWordblocks()
187 static $wordblocks = null;
188 if (!$wordblocks) {
189 $wordblocks = retrieveConfig('wordblock', 'file', null, 'array_merge_with_removal');
191 return $wordblocks;
195 * Gets the list of configured schemes
197 * @return array the schemes
199 function getSchemes()
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);
208 return $schemes;
212 * Builds a hash from an array of lines
214 * If $lower is set to true all hash keys are converted to
215 * lower case.
217 * @author Harry Fuecks <hfuecks@gmail.com>
218 * @author Andreas Gohr <andi@splitbrain.org>
219 * @author Gina Haeussge <gina@foosel.net>
221 * @param array $lines
222 * @param bool $lower
224 * @return array
226 function linesToHash($lines, $lower = false)
228 $conf = [];
229 // remove BOM
230 if (isset($lines[0]) && str_starts_with($lines[0], 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];
248 return $conf;
252 * Builds a hash from a configfile
254 * If $lower is set to true all hash keys are converted to
255 * lower case.
257 * @author Harry Fuecks <hfuecks@gmail.com>
258 * @author Andreas Gohr <andi@splitbrain.org>
259 * @author Gina Haeussge <gina@foosel.net>
261 * @param string $file
262 * @param bool $lower
264 * @return array
266 function confToHash($file, $lower = false)
268 $conf = [];
269 $lines = @file($file);
270 if (!$lines) return $conf;
272 return linesToHash($lines, $lower);
276 * Read a json config file into an array
278 * @param string $file
279 * @return array
280 * @throws JsonException
282 function jsonToArray($file)
284 $json = file_get_contents($file);
286 $conf = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
288 if ($conf === null) {
289 return [];
292 return $conf;
296 * Retrieve the requested configuration information
298 * @author Chris Smith <chris@jalakai.co.uk>
300 * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade
301 * @param callback $fn the function used to process the configuration file into an array
302 * @param array $params optional additional params to pass to the callback
303 * @param callback $combine the function used to combine arrays of values read from different configuration files;
304 * the function takes two parameters,
305 * $combined - the already read & merged configuration values
306 * $new - array of config values from the config cascade file being currently processed
307 * and returns an array of the merged configuration values.
308 * @return array configuration values
310 function retrieveConfig($type, $fn, $params = null, $combine = 'array_merge')
312 global $config_cascade;
314 if (!is_array($params)) $params = [];
316 $combined = [];
317 if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "' . $type . '"', E_USER_WARNING);
318 foreach (['default', 'local', 'protected'] as $config_group) {
319 if (empty($config_cascade[$type][$config_group])) continue;
320 foreach ($config_cascade[$type][$config_group] as $file) {
321 if (file_exists($file)) {
322 $config = call_user_func_array($fn, array_merge([$file], $params));
323 $combined = $combine($combined, $config);
328 return $combined;
332 * Include the requested configuration information
334 * @author Chris Smith <chris@jalakai.co.uk>
336 * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade
337 * @return array list of files, default before local before protected
339 function getConfigFiles($type)
341 global $config_cascade;
342 $files = [];
344 if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "' . $type . '"', E_USER_WARNING);
345 foreach (['default', 'local', 'protected'] as $config_group) {
346 if (empty($config_cascade[$type][$config_group])) continue;
347 $files = array_merge($files, $config_cascade[$type][$config_group]);
350 return $files;
354 * check if the given action was disabled in config
356 * @author Andreas Gohr <andi@splitbrain.org>
357 * @param string $action
358 * @returns boolean true if enabled, false if disabled
360 function actionOK($action)
362 static $disabled = null;
363 if (is_null($disabled) || defined('SIMPLE_TEST')) {
364 global $conf;
365 /** @var AuthPlugin $auth */
366 global $auth;
368 // prepare disabled actions array and handle legacy options
369 $disabled = explode(',', $conf['disableactions']);
370 $disabled = array_map('trim', $disabled);
371 if (
372 (isset($conf['openregister']) && !$conf['openregister']) || !$auth instanceof AuthPlugin
373 || !$auth->canDo('addUser')
375 $disabled[] = 'register';
377 if (
378 (isset($conf['resendpasswd']) && !$conf['resendpasswd']) || !$auth instanceof AuthPlugin
379 || !$auth->canDo('modPass')
381 $disabled[] = 'resendpwd';
383 if ((isset($conf['subscribers']) && !$conf['subscribers']) || !$auth instanceof AuthPlugin) {
384 $disabled[] = 'subscribe';
386 if (!$auth instanceof AuthPlugin || !$auth->canDo('Profile')) {
387 $disabled[] = 'profile';
389 if (!$auth instanceof AuthPlugin || !$auth->canDo('delUser')) {
390 $disabled[] = 'profile_delete';
392 if (!$auth instanceof AuthPlugin) {
393 $disabled[] = 'login';
395 if (!$auth instanceof AuthPlugin || !$auth->canDo('logout')) {
396 $disabled[] = 'logout';
398 $disabled = array_unique($disabled);
401 return !in_array($action, $disabled);
405 * check if headings should be used as link text for the specified link type
407 * @author Chris Smith <chris@jalakai.co.uk>
409 * @param string $linktype 'content'|'navigation', content applies to links in wiki text
410 * navigation applies to all other links
411 * @return boolean true if headings should be used for $linktype, false otherwise
413 function useHeading($linktype)
415 static $useHeading = null;
416 if (defined('DOKU_UNITTEST')) $useHeading = null; // don't cache during unit tests
418 if (is_null($useHeading)) {
419 global $conf;
421 if (!empty($conf['useheading'])) {
422 switch ($conf['useheading']) {
423 case 'content':
424 $useHeading['content'] = true;
425 break;
427 case 'navigation':
428 $useHeading['navigation'] = true;
429 break;
430 default:
431 $useHeading['content'] = true;
432 $useHeading['navigation'] = true;
434 } else {
435 $useHeading = [];
439 return (!empty($useHeading[$linktype]));
443 * obscure config data so information isn't plain text
445 * @param string $str data to be encoded
446 * @param string $code encoding method, values: plain, base64, uuencode.
447 * @return string the encoded value
449 function conf_encodeString($str, $code)
451 switch ($code) {
452 case 'base64':
453 return '<b>' . base64_encode($str);
454 case 'uuencode':
455 return '<u>' . convert_uuencode($str);
456 case 'plain':
457 default:
458 return $str;
462 * return obscured data as plain text
464 * @param string $str encoded data
465 * @return string plain text
467 function conf_decodeString($str)
469 switch (substr($str, 0, 3)) {
470 case '<b>':
471 return base64_decode(substr($str, 3));
472 case '<u>':
473 return convert_uudecode(substr($str, 3));
474 default: // not encoded (or unknown)
475 return $str;
480 * array combination function to remove negated values (prefixed by !)
482 * @param array $current
483 * @param array $new
485 * @return array the combined array, numeric keys reset
487 function array_merge_with_removal($current, $new)
489 foreach ($new as $val) {
490 if (str_starts_with($val, DOKU_CONF_NEGATION)) {
491 $idx = array_search(trim(substr($val, 1)), $current);
492 if ($idx !== false) {
493 unset($current[$idx]);
495 } else {
496 $current[] = trim($val);
500 return array_slice($current, 0);
502 //Setup VIM: ex: et ts=4 :