Merge pull request #3467 from dokuwiki-translate/lang_update_291_1617824346
[dokuwiki.git] / inc / confutils.php
blob31724d27d4c83b65580595e4f68626bbbfe8aac7
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){
32 $mtypes = getMimeTypes(); // known mimetypes
33 $ext = strrpos($file, '.');
34 if ($ext === false) {
35 return array(false, false, false);
37 $ext = strtolower(substr($file, $ext + 1));
38 if (!isset($mtypes[$ext])){
39 if ($knownonly) {
40 return array(false, false, false);
41 } else {
42 return array($ext, 'application/octet-stream', true);
45 if($mtypes[$ext][0] == '!'){
46 return array($ext, substr($mtypes[$ext],1), true);
47 }else{
48 return array($ext, $mtypes[$ext], false);
52 /**
53 * returns a hash of mimetypes
55 * @author Andreas Gohr <andi@splitbrain.org>
57 function getMimeTypes() {
58 static $mime = null;
59 if ( !$mime ) {
60 $mime = retrieveConfig('mime','confToHash');
61 $mime = array_filter($mime);
63 return $mime;
66 /**
67 * returns a hash of acronyms
69 * @author Harry Fuecks <hfuecks@gmail.com>
71 function getAcronyms() {
72 static $acronyms = null;
73 if ( !$acronyms ) {
74 $acronyms = retrieveConfig('acronyms','confToHash');
75 $acronyms = array_filter($acronyms, 'strlen');
77 return $acronyms;
80 /**
81 * returns a hash of smileys
83 * @author Harry Fuecks <hfuecks@gmail.com>
85 function getSmileys() {
86 static $smileys = null;
87 if ( !$smileys ) {
88 $smileys = retrieveConfig('smileys','confToHash');
89 $smileys = array_filter($smileys, 'strlen');
91 return $smileys;
94 /**
95 * returns a hash of entities
97 * @author Harry Fuecks <hfuecks@gmail.com>
99 function getEntities() {
100 static $entities = null;
101 if ( !$entities ) {
102 $entities = retrieveConfig('entities','confToHash');
103 $entities = array_filter($entities, 'strlen');
105 return $entities;
109 * returns a hash of interwikilinks
111 * @author Harry Fuecks <hfuecks@gmail.com>
113 function getInterwiki() {
114 static $wikis = null;
115 if ( !$wikis ) {
116 $wikis = retrieveConfig('interwiki','confToHash',array(true));
117 $wikis = array_filter($wikis, 'strlen');
119 //add sepecial case 'this'
120 $wikis['this'] = DOKU_URL.'{NAME}';
122 return $wikis;
126 * Returns the jquery script URLs for the versions defined in lib/scripts/jquery/versions
128 * @trigger CONFUTIL_CDN_SELECT
129 * @return array
131 function getCdnUrls() {
132 global $conf;
134 // load version info
135 $versions = array();
136 $lines = file(DOKU_INC . 'lib/scripts/jquery/versions');
137 foreach($lines as $line) {
138 $line = trim(preg_replace('/#.*$/', '', $line));
139 if($line === '') continue;
140 list($key, $val) = explode('=', $line, 2);
141 $key = trim($key);
142 $val = trim($val);
143 $versions[$key] = $val;
146 $src = array();
147 $data = array(
148 'versions' => $versions,
149 'src' => &$src
151 $event = new Event('CONFUTIL_CDN_SELECT', $data);
152 if($event->advise_before()) {
153 if(!$conf['jquerycdn']) {
154 $jqmod = md5(join('-', $versions));
155 $src[] = DOKU_BASE . 'lib/exe/jquery.php' . '?tseed=' . $jqmod;
156 } elseif($conf['jquerycdn'] == 'jquery') {
157 $src[] = sprintf('https://code.jquery.com/jquery-%s.min.js', $versions['JQ_VERSION']);
158 $src[] = sprintf('https://code.jquery.com/ui/%s/jquery-ui.min.js', $versions['JQUI_VERSION']);
159 } elseif($conf['jquerycdn'] == 'cdnjs') {
160 $src[] = sprintf(
161 'https://cdnjs.cloudflare.com/ajax/libs/jquery/%s/jquery.min.js',
162 $versions['JQ_VERSION']
164 $src[] = sprintf(
165 'https://cdnjs.cloudflare.com/ajax/libs/jqueryui/%s/jquery-ui.min.js',
166 $versions['JQUI_VERSION']
170 $event->advise_after();
172 return $src;
176 * returns array of wordblock patterns
179 function getWordblocks() {
180 static $wordblocks = null;
181 if ( !$wordblocks ) {
182 $wordblocks = retrieveConfig('wordblock','file',null,'array_merge_with_removal');
184 return $wordblocks;
188 * Gets the list of configured schemes
190 * @return array the schemes
192 function getSchemes() {
193 static $schemes = null;
194 if ( !$schemes ) {
195 $schemes = retrieveConfig('scheme','file',null,'array_merge_with_removal');
196 $schemes = array_map('trim', $schemes);
197 $schemes = preg_replace('/^#.*/', '', $schemes);
198 $schemes = array_filter($schemes);
200 return $schemes;
204 * Builds a hash from an array of lines
206 * If $lower is set to true all hash keys are converted to
207 * lower case.
209 * @author Harry Fuecks <hfuecks@gmail.com>
210 * @author Andreas Gohr <andi@splitbrain.org>
211 * @author Gina Haeussge <gina@foosel.net>
213 * @param array $lines
214 * @param bool $lower
216 * @return array
218 function linesToHash($lines, $lower = false) {
219 $conf = array();
220 // remove BOM
221 if(isset($lines[0]) && substr($lines[0], 0, 3) == pack('CCC', 0xef, 0xbb, 0xbf))
222 $lines[0] = substr($lines[0], 3);
223 foreach($lines as $line) {
224 //ignore comments (except escaped ones)
225 $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line);
226 $line = str_replace('\\#', '#', $line);
227 $line = trim($line);
228 if($line === '') continue;
229 $line = preg_split('/\s+/', $line, 2);
230 $line = array_pad($line, 2, '');
231 // Build the associative array
232 if($lower) {
233 $conf[strtolower($line[0])] = $line[1];
234 } else {
235 $conf[$line[0]] = $line[1];
239 return $conf;
243 * Builds a hash from a configfile
245 * If $lower is set to true all hash keys are converted to
246 * lower case.
248 * @author Harry Fuecks <hfuecks@gmail.com>
249 * @author Andreas Gohr <andi@splitbrain.org>
250 * @author Gina Haeussge <gina@foosel.net>
252 * @param string $file
253 * @param bool $lower
255 * @return array
257 function confToHash($file,$lower=false) {
258 $conf = array();
259 $lines = @file( $file );
260 if ( !$lines ) return $conf;
262 return linesToHash($lines, $lower);
266 * Read a json config file into an array
268 * @param string $file
269 * @return array
271 function jsonToArray($file)
273 $json = file_get_contents($file);
275 $conf = json_decode($json, true);
277 if ($conf === null) {
278 return [];
281 return $conf;
285 * Retrieve the requested configuration information
287 * @author Chris Smith <chris@jalakai.co.uk>
289 * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade
290 * @param callback $fn the function used to process the configuration file into an array
291 * @param array $params optional additional params to pass to the callback
292 * @param callback $combine the function used to combine arrays of values read from different configuration files;
293 * the function takes two parameters,
294 * $combined - the already read & merged configuration values
295 * $new - array of config values from the config cascade file being currently processed
296 * and returns an array of the merged configuration values.
297 * @return array configuration values
299 function retrieveConfig($type,$fn,$params=null,$combine='array_merge') {
300 global $config_cascade;
302 if(!is_array($params)) $params = array();
304 $combined = array();
305 if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
306 foreach (array('default','local','protected') as $config_group) {
307 if (empty($config_cascade[$type][$config_group])) continue;
308 foreach ($config_cascade[$type][$config_group] as $file) {
309 if (file_exists($file)) {
310 $config = call_user_func_array($fn,array_merge(array($file),$params));
311 $combined = $combine($combined, $config);
316 return $combined;
320 * Include the requested configuration information
322 * @author Chris Smith <chris@jalakai.co.uk>
324 * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade
325 * @return array list of files, default before local before protected
327 function getConfigFiles($type) {
328 global $config_cascade;
329 $files = array();
331 if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
332 foreach (array('default','local','protected') as $config_group) {
333 if (empty($config_cascade[$type][$config_group])) continue;
334 $files = array_merge($files, $config_cascade[$type][$config_group]);
337 return $files;
341 * check if the given action was disabled in config
343 * @author Andreas Gohr <andi@splitbrain.org>
344 * @param string $action
345 * @returns boolean true if enabled, false if disabled
347 function actionOK($action){
348 static $disabled = null;
349 if(is_null($disabled) || defined('SIMPLE_TEST')){
350 global $conf;
351 /** @var AuthPlugin $auth */
352 global $auth;
354 // prepare disabled actions array and handle legacy options
355 $disabled = explode(',',$conf['disableactions']);
356 $disabled = array_map('trim',$disabled);
357 if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) {
358 $disabled[] = 'register';
360 if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) {
361 $disabled[] = 'resendpwd';
363 if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) {
364 $disabled[] = 'subscribe';
366 if (is_null($auth) || !$auth->canDo('Profile')) {
367 $disabled[] = 'profile';
369 if (is_null($auth) || !$auth->canDo('delUser')) {
370 $disabled[] = 'profile_delete';
372 if (is_null($auth)) {
373 $disabled[] = 'login';
375 if (is_null($auth) || !$auth->canDo('logout')) {
376 $disabled[] = 'logout';
378 $disabled = array_unique($disabled);
381 return !in_array($action,$disabled);
385 * check if headings should be used as link text for the specified link type
387 * @author Chris Smith <chris@jalakai.co.uk>
389 * @param string $linktype 'content'|'navigation', content applies to links in wiki text
390 * navigation applies to all other links
391 * @return boolean true if headings should be used for $linktype, false otherwise
393 function useHeading($linktype) {
394 static $useHeading = null;
395 if(defined('DOKU_UNITTEST')) $useHeading = null; // don't cache during unit tests
397 if (is_null($useHeading)) {
398 global $conf;
400 if (!empty($conf['useheading'])) {
401 switch ($conf['useheading']) {
402 case 'content':
403 $useHeading['content'] = true;
404 break;
406 case 'navigation':
407 $useHeading['navigation'] = true;
408 break;
409 default:
410 $useHeading['content'] = true;
411 $useHeading['navigation'] = true;
413 } else {
414 $useHeading = array();
418 return (!empty($useHeading[$linktype]));
422 * obscure config data so information isn't plain text
424 * @param string $str data to be encoded
425 * @param string $code encoding method, values: plain, base64, uuencode.
426 * @return string the encoded value
428 function conf_encodeString($str,$code) {
429 switch ($code) {
430 case 'base64' : return '<b>'.base64_encode($str);
431 case 'uuencode' : return '<u>'.convert_uuencode($str);
432 case 'plain':
433 default:
434 return $str;
438 * return obscured data as plain text
440 * @param string $str encoded data
441 * @return string plain text
443 function conf_decodeString($str) {
444 switch (substr($str,0,3)) {
445 case '<b>' : return base64_decode(substr($str,3));
446 case '<u>' : return convert_uudecode(substr($str,3));
447 default: // not encoded (or unknown)
448 return $str;
453 * array combination function to remove negated values (prefixed by !)
455 * @param array $current
456 * @param array $new
458 * @return array the combined array, numeric keys reset
460 function array_merge_with_removal($current, $new) {
461 foreach ($new as $val) {
462 if (substr($val,0,1) == DOKU_CONF_NEGATION) {
463 $idx = array_search(trim(substr($val,1)),$current);
464 if ($idx !== false) {
465 unset($current[$idx]);
467 } else {
468 $current[] = trim($val);
472 return array_slice($current,0);
474 //Setup VIM: ex: et ts=4 :