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 versions defined in lib/scripts/jquery/versions 124 * 125 * @return array 126 */ 127function getJqueryVersions() { 128 $versions = array(); 129 $lines = file(DOKU_INC . 'lib/scripts/jquery/versions'); 130 foreach($lines as $line ) { 131 $line = preg_replace('/#.*$/', '', $line); 132 list($key, $val) = explode('=', $line, 2); 133 $key = trim($key); 134 $val = trim($val); 135 $versions[$key] = $val; 136 } 137 return $versions; 138} 139 140/** 141 * returns array of wordblock patterns 142 * 143 */ 144function getWordblocks() { 145 static $wordblocks = null; 146 if ( !$wordblocks ) { 147 $wordblocks = retrieveConfig('wordblock','file',null,'array_merge_with_removal'); 148 } 149 return $wordblocks; 150} 151 152/** 153 * Gets the list of configured schemes 154 * 155 * @return array the schemes 156 */ 157function getSchemes() { 158 static $schemes = null; 159 if ( !$schemes ) { 160 $schemes = retrieveConfig('scheme','file',null,'array_merge_with_removal'); 161 $schemes = array_map('trim', $schemes); 162 $schemes = preg_replace('/^#.*/', '', $schemes); 163 $schemes = array_filter($schemes); 164 } 165 return $schemes; 166} 167 168/** 169 * Builds a hash from an array of lines 170 * 171 * If $lower is set to true all hash keys are converted to 172 * lower case. 173 * 174 * @author Harry Fuecks <hfuecks@gmail.com> 175 * @author Andreas Gohr <andi@splitbrain.org> 176 * @author Gina Haeussge <gina@foosel.net> 177 */ 178function linesToHash($lines, $lower=false) { 179 $conf = array(); 180 // remove BOM 181 if (isset($lines[0]) && substr($lines[0],0,3) == pack('CCC',0xef,0xbb,0xbf)) 182 $lines[0] = substr($lines[0],3); 183 foreach ( $lines as $line ) { 184 //ignore comments (except escaped ones) 185 $line = preg_replace('/(?<![&\\\\])#.*$/','',$line); 186 $line = str_replace('\\#','#',$line); 187 $line = trim($line); 188 if(empty($line)) continue; 189 $line = preg_split('/\s+/',$line,2); 190 // Build the associative array 191 if($lower){ 192 $conf[strtolower($line[0])] = $line[1]; 193 }else{ 194 $conf[$line[0]] = $line[1]; 195 } 196 } 197 198 return $conf; 199} 200 201/** 202 * Builds a hash from a configfile 203 * 204 * If $lower is set to true all hash keys are converted to 205 * lower case. 206 * 207 * @author Harry Fuecks <hfuecks@gmail.com> 208 * @author Andreas Gohr <andi@splitbrain.org> 209 * @author Gina Haeussge <gina@foosel.net> 210 */ 211function confToHash($file,$lower=false) { 212 $conf = array(); 213 $lines = @file( $file ); 214 if ( !$lines ) return $conf; 215 216 return linesToHash($lines, $lower); 217} 218 219/** 220 * Retrieve the requested configuration information 221 * 222 * @author Chris Smith <chris@jalakai.co.uk> 223 * 224 * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade 225 * @param callback $fn the function used to process the configuration file into an array 226 * @param array $params optional additional params to pass to the callback 227 * @param callback $combine the function used to combine arrays of values read from different configuration files; 228 * the function takes two parameters, 229 * $combined - the already read & merged configuration values 230 * $new - array of config values from the config cascade file being currently processed 231 * and returns an array of the merged configuration values. 232 * @return array configuration values 233 */ 234function retrieveConfig($type,$fn,$params=null,$combine='array_merge') { 235 global $config_cascade; 236 237 if(!is_array($params)) $params = array(); 238 239 $combined = array(); 240 if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING); 241 foreach (array('default','local','protected') as $config_group) { 242 if (empty($config_cascade[$type][$config_group])) continue; 243 foreach ($config_cascade[$type][$config_group] as $file) { 244 if (file_exists($file)) { 245 $config = call_user_func_array($fn,array_merge(array($file),$params)); 246 $combined = $combine($combined, $config); 247 } 248 } 249 } 250 251 return $combined; 252} 253 254/** 255 * Include the requested configuration information 256 * 257 * @author Chris Smith <chris@jalakai.co.uk> 258 * 259 * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade 260 * @return array list of files, default before local before protected 261 */ 262function getConfigFiles($type) { 263 global $config_cascade; 264 $files = array(); 265 266 if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING); 267 foreach (array('default','local','protected') as $config_group) { 268 if (empty($config_cascade[$type][$config_group])) continue; 269 $files = array_merge($files, $config_cascade[$type][$config_group]); 270 } 271 272 return $files; 273} 274 275/** 276 * check if the given action was disabled in config 277 * 278 * @author Andreas Gohr <andi@splitbrain.org> 279 * @param string $action 280 * @returns boolean true if enabled, false if disabled 281 */ 282function actionOK($action){ 283 static $disabled = null; 284 if(is_null($disabled) || defined('SIMPLE_TEST')){ 285 global $conf; 286 /** @var DokuWiki_Auth_Plugin $auth */ 287 global $auth; 288 289 // prepare disabled actions array and handle legacy options 290 $disabled = explode(',',$conf['disableactions']); 291 $disabled = array_map('trim',$disabled); 292 if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) { 293 $disabled[] = 'register'; 294 } 295 if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) { 296 $disabled[] = 'resendpwd'; 297 } 298 if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) { 299 $disabled[] = 'subscribe'; 300 } 301 if (is_null($auth) || !$auth->canDo('Profile')) { 302 $disabled[] = 'profile'; 303 } 304 if (is_null($auth) || !$auth->canDo('delUser')) { 305 $disabled[] = 'profile_delete'; 306 } 307 if (is_null($auth)) { 308 $disabled[] = 'login'; 309 } 310 if (is_null($auth) || !$auth->canDo('logout')) { 311 $disabled[] = 'logout'; 312 } 313 $disabled = array_unique($disabled); 314 } 315 316 return !in_array($action,$disabled); 317} 318 319/** 320 * check if headings should be used as link text for the specified link type 321 * 322 * @author Chris Smith <chris@jalakai.co.uk> 323 * 324 * @param string $linktype 'content'|'navigation', content applies to links in wiki text 325 * navigation applies to all other links 326 * @return boolean true if headings should be used for $linktype, false otherwise 327 */ 328function useHeading($linktype) { 329 static $useHeading = null; 330 331 if (is_null($useHeading)) { 332 global $conf; 333 334 if (!empty($conf['useheading'])) { 335 switch ($conf['useheading']) { 336 case 'content': 337 $useHeading['content'] = true; 338 break; 339 340 case 'navigation': 341 $useHeading['navigation'] = true; 342 break; 343 default: 344 $useHeading['content'] = true; 345 $useHeading['navigation'] = true; 346 } 347 } else { 348 $useHeading = array(); 349 } 350 } 351 352 return (!empty($useHeading[$linktype])); 353} 354 355/** 356 * obscure config data so information isn't plain text 357 * 358 * @param string $str data to be encoded 359 * @param string $code encoding method, values: plain, base64, uuencode. 360 * @return string the encoded value 361 */ 362function conf_encodeString($str,$code) { 363 switch ($code) { 364 case 'base64' : return '<b>'.base64_encode($str); 365 case 'uuencode' : return '<u>'.convert_uuencode($str); 366 case 'plain': 367 default: 368 return $str; 369 } 370} 371/** 372 * return obscured data as plain text 373 * 374 * @param string $str encoded data 375 * @return string plain text 376 */ 377function conf_decodeString($str) { 378 switch (substr($str,0,3)) { 379 case '<b>' : return base64_decode(substr($str,3)); 380 case '<u>' : return convert_uudecode(substr($str,3)); 381 default: // not encoded (or unknown) 382 return $str; 383 } 384} 385 386/** 387 * array combination function to remove negated values (prefixed by !) 388 * 389 * @param array $current 390 * @param array $new 391 * 392 * @return array the combined array, numeric keys reset 393 */ 394function array_merge_with_removal($current, $new) { 395 foreach ($new as $val) { 396 if (substr($val,0,1) == DOKU_CONF_NEGATION) { 397 $idx = array_search(trim(substr($val,1)),$current); 398 if ($idx !== false) { 399 unset($current[$idx]); 400 } 401 } else { 402 $current[] = trim($val); 403 } 404 } 405 406 return array_slice($current,0); 407} 408//Setup VIM: ex: et ts=4 : 409