1<?php 2 3/** 4 * Utilities for collecting data from config files 5 * 6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 7 * @author Harry Fuecks <hfuecks@gmail.com> 8 */ 9 10/* 11 * line prefix used to negate single value config items 12 * (scheme.conf & stopwords.conf), e.g. 13 * !gopher 14 */ 15 16use dokuwiki\Extension\AuthPlugin; 17use dokuwiki\Extension\Event; 18 19const DOKU_CONF_NEGATION = '!'; 20 21/** 22 * Returns the (known) extension and mimetype of a given filename 23 * 24 * If $knownonly is true (the default), then only known extensions 25 * are returned. 26 * 27 * @author Andreas Gohr <andi@splitbrain.org> 28 * 29 * @param string $file file name 30 * @param bool $knownonly 31 * @return array with extension, mimetype and if it should be downloaded 32 */ 33function mimetype($file, $knownonly = true) 34{ 35 $mtypes = getMimeTypes(); // known mimetypes 36 $ext = strrpos($file, '.'); 37 if ($ext === false) { 38 return [false, false, false]; 39 } 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]; 46 } 47 } 48 if ($mtypes[$ext][0] == '!') { 49 return [$ext, substr($mtypes[$ext], 1), true]; 50 } else { 51 return [$ext, $mtypes[$ext], false]; 52 } 53} 54 55/** 56 * returns a hash of mimetypes 57 * 58 * @author Andreas Gohr <andi@splitbrain.org> 59 */ 60function getMimeTypes() 61{ 62 static $mime = null; 63 if (!$mime) { 64 $mime = retrieveConfig('mime', 'confToHash'); 65 $mime = array_filter($mime); 66 } 67 return $mime; 68} 69 70/** 71 * returns a hash of acronyms 72 * 73 * @author Harry Fuecks <hfuecks@gmail.com> 74 */ 75function getAcronyms() 76{ 77 static $acronyms = null; 78 if (!$acronyms) { 79 $acronyms = retrieveConfig('acronyms', 'confToHash'); 80 $acronyms = array_filter($acronyms, 'strlen'); 81 } 82 return $acronyms; 83} 84 85/** 86 * returns a hash of smileys 87 * 88 * @author Harry Fuecks <hfuecks@gmail.com> 89 */ 90function getSmileys() 91{ 92 static $smileys = null; 93 if (!$smileys) { 94 $smileys = retrieveConfig('smileys', 'confToHash'); 95 $smileys = array_filter($smileys, 'strlen'); 96 } 97 return $smileys; 98} 99 100/** 101 * returns a hash of entities 102 * 103 * @author Harry Fuecks <hfuecks@gmail.com> 104 */ 105function getEntities() 106{ 107 static $entities = null; 108 if (!$entities) { 109 $entities = retrieveConfig('entities', 'confToHash'); 110 $entities = array_filter($entities, 'strlen'); 111 } 112 return $entities; 113} 114 115/** 116 * returns a hash of interwikilinks 117 * 118 * @author Harry Fuecks <hfuecks@gmail.com> 119 */ 120function getInterwiki() 121{ 122 static $wikis = null; 123 if (!$wikis) { 124 $wikis = retrieveConfig('interwiki', 'confToHash', [true]); 125 $wikis = array_filter($wikis, 'strlen'); 126 127 //add sepecial case 'this' 128 $wikis['this'] = DOKU_URL . '{NAME}'; 129 } 130 return $wikis; 131} 132 133/** 134 * Returns the jquery script URLs for the versions defined in lib/scripts/jquery/versions 135 * 136 * @trigger CONFUTIL_CDN_SELECT 137 * @return array 138 */ 139function getCdnUrls() 140{ 141 global $conf; 142 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; 153 } 154 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'] 169 ); 170 $src[] = sprintf( 171 'https://cdnjs.cloudflare.com/ajax/libs/jqueryui/%s/jquery-ui.min.js', 172 $versions['JQUI_VERSION'] 173 ); 174 } 175 } 176 $event->advise_after(); 177 178 return $src; 179} 180 181/** 182 * returns array of wordblock patterns 183 * 184 */ 185function getWordblocks() 186{ 187 static $wordblocks = null; 188 if (!$wordblocks) { 189 $wordblocks = retrieveConfig('wordblock', 'file', null, 'array_merge_with_removal'); 190 } 191 return $wordblocks; 192} 193 194/** 195 * Gets the list of configured schemes 196 * 197 * @return array the schemes 198 */ 199function getSchemes() 200{ 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); 207 } 208 return $schemes; 209} 210 211/** 212 * Builds a hash from an array of lines 213 * 214 * If $lower is set to true all hash keys are converted to 215 * lower case. 216 * 217 * @author Harry Fuecks <hfuecks@gmail.com> 218 * @author Andreas Gohr <andi@splitbrain.org> 219 * @author Gina Haeussge <gina@foosel.net> 220 * 221 * @param array $lines 222 * @param bool $lower 223 * 224 * @return array 225 */ 226function linesToHash($lines, $lower = false) 227{ 228 $conf = []; 229 // remove BOM 230 if (isset($lines[0]) && substr($lines[0], 0, 3) === 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]; 245 } 246 } 247 248 return $conf; 249} 250 251/** 252 * Builds a hash from a configfile 253 * 254 * If $lower is set to true all hash keys are converted to 255 * lower case. 256 * 257 * @author Harry Fuecks <hfuecks@gmail.com> 258 * @author Andreas Gohr <andi@splitbrain.org> 259 * @author Gina Haeussge <gina@foosel.net> 260 * 261 * @param string $file 262 * @param bool $lower 263 * 264 * @return array 265 */ 266function confToHash($file, $lower = false) 267{ 268 $conf = []; 269 $lines = @file($file); 270 if (!$lines) return $conf; 271 272 return linesToHash($lines, $lower); 273} 274 275/** 276 * Read a json config file into an array 277 * 278 * @param string $file 279 * @return array 280 * @throws JsonException 281 */ 282function jsonToArray($file) 283{ 284 $json = file_get_contents($file); 285 286 $conf = json_decode($json, true, 512, JSON_THROW_ON_ERROR); 287 288 if ($conf === null) { 289 return []; 290 } 291 292 return $conf; 293} 294 295/** 296 * Retrieve the requested configuration information 297 * 298 * @author Chris Smith <chris@jalakai.co.uk> 299 * 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 309 */ 310function retrieveConfig($type, $fn, $params = null, $combine = 'array_merge') 311{ 312 global $config_cascade; 313 314 if (!is_array($params)) $params = []; 315 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); 324 } 325 } 326 } 327 328 return $combined; 329} 330 331/** 332 * Include the requested configuration information 333 * 334 * @author Chris Smith <chris@jalakai.co.uk> 335 * 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 338 */ 339function getConfigFiles($type) 340{ 341 global $config_cascade; 342 $files = []; 343 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]); 348 } 349 350 return $files; 351} 352 353/** 354 * check if the given action was disabled in config 355 * 356 * @author Andreas Gohr <andi@splitbrain.org> 357 * @param string $action 358 * @returns boolean true if enabled, false if disabled 359 */ 360function actionOK($action) 361{ 362 static $disabled = null; 363 if (is_null($disabled) || defined('SIMPLE_TEST')) { 364 global $conf; 365 /** @var AuthPlugin $auth */ 366 global $auth; 367 368 // prepare disabled actions array and handle legacy options 369 $disabled = explode(',', $conf['disableactions']); 370 $disabled = array_map('trim', $disabled); 371 if ((isset($conf['openregister']) && !$conf['openregister']) || !$auth instanceof AuthPlugin || !$auth->canDo('addUser')) { 372 $disabled[] = 'register'; 373 } 374 if ((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || !$auth instanceof AuthPlugin || !$auth->canDo('modPass')) { 375 $disabled[] = 'resendpwd'; 376 } 377 if ((isset($conf['subscribers']) && !$conf['subscribers']) || !$auth instanceof AuthPlugin) { 378 $disabled[] = 'subscribe'; 379 } 380 if (!$auth instanceof AuthPlugin || !$auth->canDo('Profile')) { 381 $disabled[] = 'profile'; 382 } 383 if (!$auth instanceof AuthPlugin || !$auth->canDo('delUser')) { 384 $disabled[] = 'profile_delete'; 385 } 386 if (!$auth instanceof AuthPlugin) { 387 $disabled[] = 'login'; 388 } 389 if (!$auth instanceof AuthPlugin || !$auth->canDo('logout')) { 390 $disabled[] = 'logout'; 391 } 392 $disabled = array_unique($disabled); 393 } 394 395 return !in_array($action, $disabled); 396} 397 398/** 399 * check if headings should be used as link text for the specified link type 400 * 401 * @author Chris Smith <chris@jalakai.co.uk> 402 * 403 * @param string $linktype 'content'|'navigation', content applies to links in wiki text 404 * navigation applies to all other links 405 * @return boolean true if headings should be used for $linktype, false otherwise 406 */ 407function useHeading($linktype) 408{ 409 static $useHeading = null; 410 if (defined('DOKU_UNITTEST')) $useHeading = null; // don't cache during unit tests 411 412 if (is_null($useHeading)) { 413 global $conf; 414 415 if (!empty($conf['useheading'])) { 416 switch ($conf['useheading']) { 417 case 'content': 418 $useHeading['content'] = true; 419 break; 420 421 case 'navigation': 422 $useHeading['navigation'] = true; 423 break; 424 default: 425 $useHeading['content'] = true; 426 $useHeading['navigation'] = true; 427 } 428 } else { 429 $useHeading = []; 430 } 431 } 432 433 return (!empty($useHeading[$linktype])); 434} 435 436/** 437 * obscure config data so information isn't plain text 438 * 439 * @param string $str data to be encoded 440 * @param string $code encoding method, values: plain, base64, uuencode. 441 * @return string the encoded value 442 */ 443function conf_encodeString($str, $code) 444{ 445 switch ($code) { 446 case 'base64': 447 return '<b>' . base64_encode($str); 448 case 'uuencode': 449 return '<u>' . convert_uuencode($str); 450 case 'plain': 451 default: 452 return $str; 453 } 454} 455/** 456 * return obscured data as plain text 457 * 458 * @param string $str encoded data 459 * @return string plain text 460 */ 461function conf_decodeString($str) 462{ 463 switch (substr($str, 0, 3)) { 464 case '<b>': 465 return base64_decode(substr($str, 3)); 466 case '<u>': 467 return convert_uudecode(substr($str, 3)); 468 default: // not encoded (or unknown) 469 return $str; 470 } 471} 472 473/** 474 * array combination function to remove negated values (prefixed by !) 475 * 476 * @param array $current 477 * @param array $new 478 * 479 * @return array the combined array, numeric keys reset 480 */ 481function array_merge_with_removal($current, $new) 482{ 483 foreach ($new as $val) { 484 if (substr($val, 0, 1) == DOKU_CONF_NEGATION) { 485 $idx = array_search(trim(substr($val, 1)), $current); 486 if ($idx !== false) { 487 unset($current[$idx]); 488 } 489 } else { 490 $current[] = trim($val); 491 } 492 } 493 494 return array_slice($current, 0); 495} 496//Setup VIM: ex: et ts=4 : 497