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){
32 $mtypes = getMimeTypes(); // known mimetypes
33 $ext = strrpos($file, '.');
35 return array(false, false, false);
37 $ext = strtolower(substr($file, $ext +
1));
38 if (!isset($mtypes[$ext])){
40 return array(false, false, false);
42 return array($ext, 'application/octet-stream', true);
45 if($mtypes[$ext][0] == '!'){
46 return array($ext, substr($mtypes[$ext],1), true);
48 return array($ext, $mtypes[$ext], false);
53 * returns a hash of mimetypes
55 * @author Andreas Gohr <andi@splitbrain.org>
57 function getMimeTypes() {
60 $mime = retrieveConfig('mime','confToHash');
61 $mime = array_filter($mime);
67 * returns a hash of acronyms
69 * @author Harry Fuecks <hfuecks@gmail.com>
71 function getAcronyms() {
72 static $acronyms = null;
74 $acronyms = retrieveConfig('acronyms','confToHash');
75 $acronyms = array_filter($acronyms, 'strlen');
81 * returns a hash of smileys
83 * @author Harry Fuecks <hfuecks@gmail.com>
85 function getSmileys() {
86 static $smileys = null;
88 $smileys = retrieveConfig('smileys','confToHash');
89 $smileys = array_filter($smileys, 'strlen');
95 * returns a hash of entities
97 * @author Harry Fuecks <hfuecks@gmail.com>
99 function getEntities() {
100 static $entities = null;
102 $entities = retrieveConfig('entities','confToHash');
103 $entities = array_filter($entities, 'strlen');
109 * returns a hash of interwikilinks
111 * @author Harry Fuecks <hfuecks@gmail.com>
113 function getInterwiki() {
114 static $wikis = null;
116 $wikis = retrieveConfig('interwiki','confToHash',array(true));
117 $wikis = array_filter($wikis, 'strlen');
119 //add sepecial case 'this'
120 $wikis['this'] = DOKU_URL
.'{NAME}';
126 * Returns the jquery script URLs for the versions defined in lib/scripts/jquery/versions
128 * @trigger CONFUTIL_CDN_SELECT
131 function getCdnUrls() {
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);
143 $versions[$key] = $val;
148 'versions' => $versions,
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') {
161 'https://cdnjs.cloudflare.com/ajax/libs/jquery/%s/jquery.min.js',
162 $versions['JQ_VERSION']
165 'https://cdnjs.cloudflare.com/ajax/libs/jqueryui/%s/jquery-ui.min.js',
166 $versions['JQUI_VERSION']
170 $event->advise_after();
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');
188 * Gets the list of configured schemes
190 * @return array the schemes
192 function getSchemes() {
193 static $schemes = null;
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);
204 * Builds a hash from an array of lines
206 * If $lower is set to true all hash keys are converted to
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
218 function linesToHash($lines, $lower = false) {
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);
228 if($line === '') continue;
229 $line = preg_split('/\s+/', $line, 2);
230 $line = array_pad($line, 2, '');
231 // Build the associative array
233 $conf[strtolower($line[0])] = $line[1];
235 $conf[$line[0]] = $line[1];
243 * Builds a hash from a configfile
245 * If $lower is set to true all hash keys are converted to
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
257 function confToHash($file,$lower=false) {
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
271 function jsonToArray($file)
273 $json = file_get_contents($file);
275 $conf = json_decode($json, true);
277 if ($conf === null) {
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();
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);
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;
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]);
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')){
351 /** @var AuthPlugin $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)) {
400 if (!empty($conf['useheading'])) {
401 switch ($conf['useheading']) {
403 $useHeading['content'] = true;
407 $useHeading['navigation'] = true;
410 $useHeading['content'] = true;
411 $useHeading['navigation'] = true;
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) {
430 case 'base64' : return '<b>'.base64_encode($str);
431 case 'uuencode' : return '<u>'.convert_uuencode($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)
453 * array combination function to remove negated values (prefixed by !)
455 * @param array $current
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]);
468 $current[] = trim($val);
472 return array_slice($current,0);
474 //Setup VIM: ex: et ts=4 :