code style: static visibility
[dokuwiki.git] / inc / confutils.php
blobb0f94b47420e210209c8f379a75b0c1c82c68fdb
1 <?php
2 /**
3 * Utilities for collecting data from config files
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Harry Fuecks <hfuecks@gmail.com>
7 */
9 /*
10 * line prefix used to negate single value config items
11 * (scheme.conf & stopwords.conf), e.g.
12 * !gopher
15 use dokuwiki\Extension\AuthPlugin;
16 use dokuwiki\Extension\Event;
17 const DOKU_CONF_NEGATION = '!';
19 /**
20 * Returns the (known) extension and mimetype of a given filename
22 * If $knownonly is true (the default), then only known extensions
23 * are returned.
25 * @author Andreas Gohr <andi@splitbrain.org>
27 * @param string $file file name
28 * @param bool $knownonly
29 * @return array with extension, mimetype and if it should be downloaded
31 function mimetype($file, $knownonly = true)
33 $mtypes = getMimeTypes(); // known mimetypes
34 $ext = strrpos($file, '.');
35 if ($ext === false) {
36 return [false, false, false];
38 $ext = strtolower(substr($file, $ext + 1));
39 if (!isset($mtypes[$ext])) {
40 if ($knownonly) {
41 return [false, false, false];
42 } else {
43 return [$ext, 'application/octet-stream', true];
46 if ($mtypes[$ext][0] == '!') {
47 return [$ext, substr($mtypes[$ext], 1), true];
48 } else {
49 return [$ext, $mtypes[$ext], false];
53 /**
54 * returns a hash of mimetypes
56 * @author Andreas Gohr <andi@splitbrain.org>
58 function getMimeTypes()
60 static $mime = null;
61 if (!$mime) {
62 $mime = retrieveConfig('mime', 'confToHash');
63 $mime = array_filter($mime);
65 return $mime;
68 /**
69 * returns a hash of acronyms
71 * @author Harry Fuecks <hfuecks@gmail.com>
73 function getAcronyms()
75 static $acronyms = null;
76 if (!$acronyms) {
77 $acronyms = retrieveConfig('acronyms', 'confToHash');
78 $acronyms = array_filter($acronyms, 'strlen');
80 return $acronyms;
83 /**
84 * returns a hash of smileys
86 * @author Harry Fuecks <hfuecks@gmail.com>
88 function getSmileys()
90 static $smileys = null;
91 if (!$smileys) {
92 $smileys = retrieveConfig('smileys', 'confToHash');
93 $smileys = array_filter($smileys, 'strlen');
95 return $smileys;
98 /**
99 * returns a hash of entities
101 * @author Harry Fuecks <hfuecks@gmail.com>
103 function getEntities()
105 static $entities = null;
106 if (!$entities) {
107 $entities = retrieveConfig('entities', 'confToHash');
108 $entities = array_filter($entities, 'strlen');
110 return $entities;
114 * returns a hash of interwikilinks
116 * @author Harry Fuecks <hfuecks@gmail.com>
118 function getInterwiki()
120 static $wikis = null;
121 if (!$wikis) {
122 $wikis = retrieveConfig('interwiki', 'confToHash', [true]);
123 $wikis = array_filter($wikis, 'strlen');
125 //add sepecial case 'this'
126 $wikis['this'] = DOKU_URL . '{NAME}';
128 return $wikis;
132 * Returns the jquery script URLs for the versions defined in lib/scripts/jquery/versions
134 * @trigger CONFUTIL_CDN_SELECT
135 * @return array
137 function getCdnUrls()
139 global $conf;
141 // load version info
142 $versions = [];
143 $lines = file(DOKU_INC . 'lib/scripts/jquery/versions');
144 foreach ($lines as $line) {
145 $line = trim(preg_replace('/#.*$/', '', $line));
146 if ($line === '') continue;
147 [$key, $val] = sexplode('=', $line, 2, '');
148 $key = trim($key);
149 $val = trim($val);
150 $versions[$key] = $val;
153 $src = [];
154 $data = ['versions' => $versions, 'src' => &$src];
155 $event = new Event('CONFUTIL_CDN_SELECT', $data);
156 if ($event->advise_before()) {
157 if (!$conf['jquerycdn']) {
158 $jqmod = md5(implode('-', $versions));
159 $src[] = DOKU_BASE . 'lib/exe/jquery.php' . '?tseed=' . $jqmod;
160 } elseif ($conf['jquerycdn'] == 'jquery') {
161 $src[] = sprintf('https://code.jquery.com/jquery-%s.min.js', $versions['JQ_VERSION']);
162 $src[] = sprintf('https://code.jquery.com/ui/%s/jquery-ui.min.js', $versions['JQUI_VERSION']);
163 } elseif ($conf['jquerycdn'] == 'cdnjs') {
164 $src[] = sprintf(
165 'https://cdnjs.cloudflare.com/ajax/libs/jquery/%s/jquery.min.js',
166 $versions['JQ_VERSION']
168 $src[] = sprintf(
169 'https://cdnjs.cloudflare.com/ajax/libs/jqueryui/%s/jquery-ui.min.js',
170 $versions['JQUI_VERSION']
174 $event->advise_after();
176 return $src;
180 * returns array of wordblock patterns
183 function getWordblocks()
185 static $wordblocks = null;
186 if (!$wordblocks) {
187 $wordblocks = retrieveConfig('wordblock', 'file', null, 'array_merge_with_removal');
189 return $wordblocks;
193 * Gets the list of configured schemes
195 * @return array the schemes
197 function getSchemes()
199 static $schemes = null;
200 if (!$schemes) {
201 $schemes = retrieveConfig('scheme', 'file', null, 'array_merge_with_removal');
202 $schemes = array_map('trim', $schemes);
203 $schemes = preg_replace('/^#.*/', '', $schemes);
204 $schemes = array_filter($schemes);
206 return $schemes;
210 * Builds a hash from an array of lines
212 * If $lower is set to true all hash keys are converted to
213 * lower case.
215 * @author Harry Fuecks <hfuecks@gmail.com>
216 * @author Andreas Gohr <andi@splitbrain.org>
217 * @author Gina Haeussge <gina@foosel.net>
219 * @param array $lines
220 * @param bool $lower
222 * @return array
224 function linesToHash($lines, $lower = false)
226 $conf = [];
227 // remove BOM
228 if (isset($lines[0]) && substr($lines[0], 0, 3) === pack('CCC', 0xef, 0xbb, 0xbf))
229 $lines[0] = substr($lines[0], 3);
230 foreach ($lines as $line) {
231 //ignore comments (except escaped ones)
232 $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line);
233 $line = str_replace('\\#', '#', $line);
234 $line = trim($line);
235 if ($line === '') continue;
236 $line = preg_split('/\s+/', $line, 2);
237 $line = array_pad($line, 2, '');
238 // Build the associative array
239 if ($lower) {
240 $conf[strtolower($line[0])] = $line[1];
241 } else {
242 $conf[$line[0]] = $line[1];
246 return $conf;
250 * Builds a hash from a configfile
252 * If $lower is set to true all hash keys are converted to
253 * lower case.
255 * @author Harry Fuecks <hfuecks@gmail.com>
256 * @author Andreas Gohr <andi@splitbrain.org>
257 * @author Gina Haeussge <gina@foosel.net>
259 * @param string $file
260 * @param bool $lower
262 * @return array
264 function confToHash($file, $lower = false)
266 $conf = [];
267 $lines = @file($file);
268 if (!$lines) return $conf;
270 return linesToHash($lines, $lower);
274 * Read a json config file into an array
276 * @param string $file
277 * @return array
279 function jsonToArray($file)
281 $json = file_get_contents($file);
283 $conf = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
285 if ($conf === null) {
286 return [];
289 return $conf;
293 * Retrieve the requested configuration information
295 * @author Chris Smith <chris@jalakai.co.uk>
297 * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade
298 * @param callback $fn the function used to process the configuration file into an array
299 * @param array $params optional additional params to pass to the callback
300 * @param callback $combine the function used to combine arrays of values read from different configuration files;
301 * the function takes two parameters,
302 * $combined - the already read & merged configuration values
303 * $new - array of config values from the config cascade file being currently processed
304 * and returns an array of the merged configuration values.
305 * @return array configuration values
307 function retrieveConfig($type, $fn, $params = null, $combine = 'array_merge')
309 global $config_cascade;
311 if (!is_array($params)) $params = [];
313 $combined = [];
314 if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "' . $type . '"', E_USER_WARNING);
315 foreach (['default', 'local', 'protected'] as $config_group) {
316 if (empty($config_cascade[$type][$config_group])) continue;
317 foreach ($config_cascade[$type][$config_group] as $file) {
318 if (file_exists($file)) {
319 $config = call_user_func_array($fn, array_merge([$file], $params));
320 $combined = $combine($combined, $config);
325 return $combined;
329 * Include the requested configuration information
331 * @author Chris Smith <chris@jalakai.co.uk>
333 * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade
334 * @return array list of files, default before local before protected
336 function getConfigFiles($type)
338 global $config_cascade;
339 $files = [];
341 if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "' . $type . '"', E_USER_WARNING);
342 foreach (['default', 'local', 'protected'] as $config_group) {
343 if (empty($config_cascade[$type][$config_group])) continue;
344 $files = array_merge($files, $config_cascade[$type][$config_group]);
347 return $files;
351 * check if the given action was disabled in config
353 * @author Andreas Gohr <andi@splitbrain.org>
354 * @param string $action
355 * @returns boolean true if enabled, false if disabled
357 function actionOK($action)
359 static $disabled = null;
360 if (is_null($disabled) || defined('SIMPLE_TEST')) {
361 global $conf;
362 /** @var AuthPlugin $auth */
363 global $auth;
365 // prepare disabled actions array and handle legacy options
366 $disabled = explode(',', $conf['disableactions']);
367 $disabled = array_map('trim', $disabled);
368 if ((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) {
369 $disabled[] = 'register';
371 if ((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) {
372 $disabled[] = 'resendpwd';
374 if ((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) {
375 $disabled[] = 'subscribe';
377 if (is_null($auth) || !$auth->canDo('Profile')) {
378 $disabled[] = 'profile';
380 if (is_null($auth) || !$auth->canDo('delUser')) {
381 $disabled[] = 'profile_delete';
383 if (is_null($auth)) {
384 $disabled[] = 'login';
386 if (is_null($auth) || !$auth->canDo('logout')) {
387 $disabled[] = 'logout';
389 $disabled = array_unique($disabled);
392 return !in_array($action, $disabled);
396 * check if headings should be used as link text for the specified link type
398 * @author Chris Smith <chris@jalakai.co.uk>
400 * @param string $linktype 'content'|'navigation', content applies to links in wiki text
401 * navigation applies to all other links
402 * @return boolean true if headings should be used for $linktype, false otherwise
404 function useHeading($linktype)
406 static $useHeading = null;
407 if (defined('DOKU_UNITTEST')) $useHeading = null; // don't cache during unit tests
409 if (is_null($useHeading)) {
410 global $conf;
412 if (!empty($conf['useheading'])) {
413 switch ($conf['useheading']) {
414 case 'content':
415 $useHeading['content'] = true;
416 break;
418 case 'navigation':
419 $useHeading['navigation'] = true;
420 break;
421 default:
422 $useHeading['content'] = true;
423 $useHeading['navigation'] = true;
425 } else {
426 $useHeading = [];
430 return (!empty($useHeading[$linktype]));
434 * obscure config data so information isn't plain text
436 * @param string $str data to be encoded
437 * @param string $code encoding method, values: plain, base64, uuencode.
438 * @return string the encoded value
440 function conf_encodeString($str, $code)
442 switch ($code) {
443 case 'base64':
444 return '<b>' . base64_encode($str);
445 case 'uuencode':
446 return '<u>' . convert_uuencode($str);
447 case 'plain':
448 default:
449 return $str;
453 * return obscured data as plain text
455 * @param string $str encoded data
456 * @return string plain text
458 function conf_decodeString($str)
460 switch (substr($str, 0, 3)) {
461 case '<b>':
462 return base64_decode(substr($str, 3));
463 case '<u>':
464 return convert_uudecode(substr($str, 3));
465 default: // not encoded (or unknown)
466 return $str;
471 * array combination function to remove negated values (prefixed by !)
473 * @param array $current
474 * @param array $new
476 * @return array the combined array, numeric keys reset
478 function array_merge_with_removal($current, $new)
480 foreach ($new as $val) {
481 if (substr($val, 0, 1) == DOKU_CONF_NEGATION) {
482 $idx = array_search(trim(substr($val, 1)), $current);
483 if ($idx !== false) {
484 unset($current[$idx]);
486 } else {
487 $current[] = trim($val);
491 return array_slice($current, 0);
493 //Setup VIM: ex: et ts=4 :