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>
10 * line prefix used to negate single value config items
11 * (scheme.conf & stopwords.conf), e.g.
15 use dokuwiki\Extension\AuthPlugin
;
16 use dokuwiki\Extension\Event
;
17 const DOKU_CONF_NEGATION
= '!';
20 * Returns the (known) extension and mimetype of a given filename
22 * If $knownonly is true (the default), then only known extensions
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, '.');
36 return [false, false, false];
38 $ext = strtolower(substr($file, $ext +
1));
39 if (!isset($mtypes[$ext])) {
41 return [false, false, false];
43 return [$ext, 'application/octet-stream', true];
46 if ($mtypes[$ext][0] == '!') {
47 return [$ext, substr($mtypes[$ext], 1), true];
49 return [$ext, $mtypes[$ext], false];
54 * returns a hash of mimetypes
56 * @author Andreas Gohr <andi@splitbrain.org>
58 function getMimeTypes()
62 $mime = retrieveConfig('mime', 'confToHash');
63 $mime = array_filter($mime);
69 * returns a hash of acronyms
71 * @author Harry Fuecks <hfuecks@gmail.com>
73 function getAcronyms()
75 static $acronyms = null;
77 $acronyms = retrieveConfig('acronyms', 'confToHash');
78 $acronyms = array_filter($acronyms, 'strlen');
84 * returns a hash of smileys
86 * @author Harry Fuecks <hfuecks@gmail.com>
90 static $smileys = null;
92 $smileys = retrieveConfig('smileys', 'confToHash');
93 $smileys = array_filter($smileys, 'strlen');
99 * returns a hash of entities
101 * @author Harry Fuecks <hfuecks@gmail.com>
103 function getEntities()
105 static $entities = null;
107 $entities = retrieveConfig('entities', 'confToHash');
108 $entities = array_filter($entities, 'strlen');
114 * returns a hash of interwikilinks
116 * @author Harry Fuecks <hfuecks@gmail.com>
118 function getInterwiki()
120 static $wikis = null;
122 $wikis = retrieveConfig('interwiki', 'confToHash', [true]);
123 $wikis = array_filter($wikis, 'strlen');
125 //add sepecial case 'this'
126 $wikis['this'] = DOKU_URL
. '{NAME}';
132 * Returns the jquery script URLs for the versions defined in lib/scripts/jquery/versions
134 * @trigger CONFUTIL_CDN_SELECT
137 function getCdnUrls()
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, '');
150 $versions[$key] = $val;
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') {
165 'https://cdnjs.cloudflare.com/ajax/libs/jquery/%s/jquery.min.js',
166 $versions['JQ_VERSION']
169 'https://cdnjs.cloudflare.com/ajax/libs/jqueryui/%s/jquery-ui.min.js',
170 $versions['JQUI_VERSION']
174 $event->advise_after();
180 * returns array of wordblock patterns
183 function getWordblocks()
185 static $wordblocks = null;
187 $wordblocks = retrieveConfig('wordblock', 'file', null, 'array_merge_with_removal');
193 * Gets the list of configured schemes
195 * @return array the schemes
197 function getSchemes()
199 static $schemes = null;
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);
210 * Builds a hash from an array of lines
212 * If $lower is set to true all hash keys are converted to
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
224 function linesToHash($lines, $lower = false)
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);
235 if ($line === '') continue;
236 $line = preg_split('/\s+/', $line, 2);
237 $line = array_pad($line, 2, '');
238 // Build the associative array
240 $conf[strtolower($line[0])] = $line[1];
242 $conf[$line[0]] = $line[1];
250 * Builds a hash from a configfile
252 * If $lower is set to true all hash keys are converted to
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
264 function confToHash($file, $lower = false)
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
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) {
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 = [];
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);
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;
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]);
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')) {
362 /** @var AuthPlugin $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)) {
412 if (!empty($conf['useheading'])) {
413 switch ($conf['useheading']) {
415 $useHeading['content'] = true;
419 $useHeading['navigation'] = true;
422 $useHeading['content'] = true;
423 $useHeading['navigation'] = true;
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)
444 return '<b>' . base64_encode($str);
446 return '<u>' . convert_uuencode($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)) {
462 return base64_decode(substr($str, 3));
464 return convert_uudecode(substr($str, 3));
465 default: // not encoded (or unknown)
471 * array combination function to remove negated values (prefixed by !)
473 * @param array $current
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]);
487 $current[] = trim($val);
491 return array_slice($current, 0);
493 //Setup VIM: ex: et ts=4 :