[ Index ] |
PHP Cross Reference of DokuWiki |
[Summary view] [Print] [Text view]
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 15 use dokuwiki\Extension\AuthPlugin; 16 use dokuwiki\Extension\Event; 17 const 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 */ 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); 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 */ 57 function 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 */ 71 function 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 */ 85 function 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 */ 99 function 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 */ 113 function 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 */ 131 function 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) = sexplode('=', $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/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'] 163 ); 164 $src[] = sprintf( 165 'https://cdnjs.cloudflare.com/ajax/libs/jqueryui/%s/jquery-ui.min.js', 166 $versions['JQUI_VERSION'] 167 ); 168 } 169 } 170 $event->advise_after(); 171 172 return $src; 173 } 174 175 /** 176 * returns array of wordblock patterns 177 * 178 */ 179 function getWordblocks() { 180 static $wordblocks = null; 181 if ( !$wordblocks ) { 182 $wordblocks = retrieveConfig('wordblock','file',null,'array_merge_with_removal'); 183 } 184 return $wordblocks; 185 } 186 187 /** 188 * Gets the list of configured schemes 189 * 190 * @return array the schemes 191 */ 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); 199 } 200 return $schemes; 201 } 202 203 /** 204 * Builds a hash from an array of lines 205 * 206 * If $lower is set to true all hash keys are converted to 207 * lower case. 208 * 209 * @author Harry Fuecks <hfuecks@gmail.com> 210 * @author Andreas Gohr <andi@splitbrain.org> 211 * @author Gina Haeussge <gina@foosel.net> 212 * 213 * @param array $lines 214 * @param bool $lower 215 * 216 * @return array 217 */ 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]; 236 } 237 } 238 239 return $conf; 240 } 241 242 /** 243 * Builds a hash from a configfile 244 * 245 * If $lower is set to true all hash keys are converted to 246 * lower case. 247 * 248 * @author Harry Fuecks <hfuecks@gmail.com> 249 * @author Andreas Gohr <andi@splitbrain.org> 250 * @author Gina Haeussge <gina@foosel.net> 251 * 252 * @param string $file 253 * @param bool $lower 254 * 255 * @return array 256 */ 257 function confToHash($file,$lower=false) { 258 $conf = array(); 259 $lines = @file( $file ); 260 if ( !$lines ) return $conf; 261 262 return linesToHash($lines, $lower); 263 } 264 265 /** 266 * Read a json config file into an array 267 * 268 * @param string $file 269 * @return array 270 */ 271 function jsonToArray($file) 272 { 273 $json = file_get_contents($file); 274 275 $conf = json_decode($json, true); 276 277 if ($conf === null) { 278 return []; 279 } 280 281 return $conf; 282 } 283 284 /** 285 * Retrieve the requested configuration information 286 * 287 * @author Chris Smith <chris@jalakai.co.uk> 288 * 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 298 */ 299 function retrieveConfig($type,$fn,$params=null,$combine='array_merge') { 300 global $config_cascade; 301 302 if(!is_array($params)) $params = array(); 303 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); 312 } 313 } 314 } 315 316 return $combined; 317 } 318 319 /** 320 * Include the requested configuration information 321 * 322 * @author Chris Smith <chris@jalakai.co.uk> 323 * 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 326 */ 327 function getConfigFiles($type) { 328 global $config_cascade; 329 $files = array(); 330 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]); 335 } 336 337 return $files; 338 } 339 340 /** 341 * check if the given action was disabled in config 342 * 343 * @author Andreas Gohr <andi@splitbrain.org> 344 * @param string $action 345 * @returns boolean true if enabled, false if disabled 346 */ 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; 353 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'; 359 } 360 if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) { 361 $disabled[] = 'resendpwd'; 362 } 363 if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) { 364 $disabled[] = 'subscribe'; 365 } 366 if (is_null($auth) || !$auth->canDo('Profile')) { 367 $disabled[] = 'profile'; 368 } 369 if (is_null($auth) || !$auth->canDo('delUser')) { 370 $disabled[] = 'profile_delete'; 371 } 372 if (is_null($auth)) { 373 $disabled[] = 'login'; 374 } 375 if (is_null($auth) || !$auth->canDo('logout')) { 376 $disabled[] = 'logout'; 377 } 378 $disabled = array_unique($disabled); 379 } 380 381 return !in_array($action,$disabled); 382 } 383 384 /** 385 * check if headings should be used as link text for the specified link type 386 * 387 * @author Chris Smith <chris@jalakai.co.uk> 388 * 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 392 */ 393 function useHeading($linktype) { 394 static $useHeading = null; 395 if(defined('DOKU_UNITTEST')) $useHeading = null; // don't cache during unit tests 396 397 if (is_null($useHeading)) { 398 global $conf; 399 400 if (!empty($conf['useheading'])) { 401 switch ($conf['useheading']) { 402 case 'content': 403 $useHeading['content'] = true; 404 break; 405 406 case 'navigation': 407 $useHeading['navigation'] = true; 408 break; 409 default: 410 $useHeading['content'] = true; 411 $useHeading['navigation'] = true; 412 } 413 } else { 414 $useHeading = array(); 415 } 416 } 417 418 return (!empty($useHeading[$linktype])); 419 } 420 421 /** 422 * obscure config data so information isn't plain text 423 * 424 * @param string $str data to be encoded 425 * @param string $code encoding method, values: plain, base64, uuencode. 426 * @return string the encoded value 427 */ 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; 435 } 436 } 437 /** 438 * return obscured data as plain text 439 * 440 * @param string $str encoded data 441 * @return string plain text 442 */ 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; 449 } 450 } 451 452 /** 453 * array combination function to remove negated values (prefixed by !) 454 * 455 * @param array $current 456 * @param array $new 457 * 458 * @return array the combined array, numeric keys reset 459 */ 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]); 466 } 467 } else { 468 $current[] = trim($val); 469 } 470 } 471 472 return array_slice($current,0); 473 } 474 //Setup VIM: ex: et ts=4 :
title
Description
Body
title
Description
Body
title
Description
Body
title
Body