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