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