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/** 11 * Returns the (known) extension and mimetype of a given filename 12 * 13 * If $knownonly is true (the default), then only known extensions 14 * are returned. 15 * 16 * @author Andreas Gohr <andi@splitbrain.org> 17 */ 18function mimetype($file, $knownonly=true){ 19 $ret = array(false,false,false); // return array 20 $mtypes = getMimeTypes(); // known mimetypes 21 $exts = join('|',array_keys($mtypes)); // known extensions (regexp) 22 if(!$knownonly){ 23 $exts = $exts.'|[_\-A-Za-z0-9]+'; // any extension 24 } 25 if(preg_match('#\.('.$exts.')$#i',$file,$matches)){ 26 $ext = strtolower($matches[1]); 27 } 28 29 if($ext){ 30 if (isset($mtypes[$ext])){ 31 if($mtypes[$ext][0] == '!'){ 32 $ret = array($ext, substr($mtypes[$ext],1), true); 33 }else{ 34 $ret = array($ext, $mtypes[$ext], false); 35 } 36 }elseif(!$knownonly){ 37 $ret = array($ext, 'application/octet-stream', true); 38 } 39 } 40 41 return $ret; 42} 43 44/** 45 * returns a hash of mimetypes 46 * 47 * @author Andreas Gohr <andi@splitbrain.org> 48 */ 49function getMimeTypes() { 50 static $mime = null; 51 if ( !$mime ) { 52 $mime = retrieveConfig('mime','confToHash'); 53 } 54 return $mime; 55} 56 57/** 58 * returns a hash of acronyms 59 * 60 * @author Harry Fuecks <hfuecks@gmail.com> 61 */ 62function getAcronyms() { 63 static $acronyms = null; 64 if ( !$acronyms ) { 65 $acronyms = retrieveConfig('acronyms','confToHash'); 66 } 67 return $acronyms; 68} 69 70/** 71 * returns a hash of smileys 72 * 73 * @author Harry Fuecks <hfuecks@gmail.com> 74 */ 75function getSmileys() { 76 static $smileys = null; 77 if ( !$smileys ) { 78 $smileys = retrieveConfig('smileys','confToHash'); 79 } 80 return $smileys; 81} 82 83/** 84 * returns a hash of entities 85 * 86 * @author Harry Fuecks <hfuecks@gmail.com> 87 */ 88function getEntities() { 89 static $entities = null; 90 if ( !$entities ) { 91 $entities = retrieveConfig('entities','confToHash'); 92 } 93 return $entities; 94} 95 96/** 97 * returns a hash of interwikilinks 98 * 99 * @author Harry Fuecks <hfuecks@gmail.com> 100 */ 101function getInterwiki() { 102 static $wikis = null; 103 if ( !$wikis ) { 104 $wikis = retrieveConfig('interwiki','confToHash',array(true)); 105 } 106 //add sepecial case 'this' 107 $wikis['this'] = DOKU_URL.'{NAME}'; 108 return $wikis; 109} 110 111/** 112 * returns array of wordblock patterns 113 * 114 */ 115function getWordblocks() { 116 static $wordblocks = null; 117 if ( !$wordblocks ) { 118 $wordblocks = retrieveConfig('wordblock','file'); 119 } 120 return $wordblocks; 121} 122 123 124function getSchemes() { 125 static $schemes = null; 126 if ( !$schemes ) { 127 $schemes = retrieveConfig('scheme','file'); 128 } 129 $schemes = array_map('trim', $schemes); 130 $schemes = preg_replace('/^#.*/', '', $schemes); 131 $schemes = array_filter($schemes); 132 return $schemes; 133} 134 135/** 136 * Builds a hash from an array of lines 137 * 138 * If $lower is set to true all hash keys are converted to 139 * lower case. 140 * 141 * @author Harry Fuecks <hfuecks@gmail.com> 142 * @author Andreas Gohr <andi@splitbrain.org> 143 * @author Gina Haeussge <gina@foosel.net> 144 */ 145function linesToHash($lines, $lower=false) { 146 $conf = array(); 147 foreach ( $lines as $line ) { 148 //ignore comments (except escaped ones) 149 $line = preg_replace('/(?<![&\\\\])#.*$/','',$line); 150 $line = str_replace('\\#','#',$line); 151 $line = trim($line); 152 if(empty($line)) continue; 153 $line = preg_split('/\s+/',$line,2); 154 // Build the associative array 155 if($lower){ 156 $conf[strtolower($line[0])] = $line[1]; 157 }else{ 158 $conf[$line[0]] = $line[1]; 159 } 160 } 161 162 return $conf; 163} 164 165/** 166 * Builds a hash from a configfile 167 * 168 * If $lower is set to true all hash keys are converted to 169 * lower case. 170 * 171 * @author Harry Fuecks <hfuecks@gmail.com> 172 * @author Andreas Gohr <andi@splitbrain.org> 173 * @author Gina Haeussge <gina@foosel.net> 174 */ 175function confToHash($file,$lower=false) { 176 $conf = array(); 177 $lines = @file( $file ); 178 if ( !$lines ) return $conf; 179 180 return linesToHash($lines, $lower); 181} 182 183/** 184 * Retrieve the requested configuration information 185 * 186 * @author Chris Smith <chris@jalakai.co.uk> 187 * 188 * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade 189 * @param callback $fn the function used to process the configuration file into an array 190 * @param array $param optional additional params to pass to the callback 191 * @return array configuration values 192 */ 193function retrieveConfig($type,$fn,$params=null) { 194 global $config_cascade; 195 196 if(!is_array($params)) $params = array(); 197 198 $combined = array(); 199 if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING); 200 foreach (array('default','local','protected') as $config_group) { 201 if (empty($config_cascade[$type][$config_group])) continue; 202 foreach ($config_cascade[$type][$config_group] as $file) { 203 if (@file_exists($file)) { 204 $config = call_user_func_array($fn,array_merge(array($file),$params)); 205 $combined = array_merge($combined, $config); 206 } 207 } 208 } 209 210 return $combined; 211} 212 213/** 214 * Include the requested configuration information 215 * 216 * @author Chris Smith <chris@jalakai.co.uk> 217 * 218 * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade 219 * @return array list of files, default before local before protected 220 */ 221function getConfigFiles($type) { 222 global $config_cascade; 223 $files = array(); 224 225 if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING); 226 foreach (array('default','local','protected') as $config_group) { 227 if (empty($config_cascade[$type][$config_group])) continue; 228 $files = array_merge($files, $config_cascade[$type][$config_group]); 229 } 230 231 return $files; 232} 233 234/** 235 * check if the given action was disabled in config 236 * 237 * @author Andreas Gohr <andi@splitbrain.org> 238 * @returns boolean true if enabled, false if disabled 239 */ 240function actionOK($action){ 241 static $disabled = null; 242 if(is_null($disabled)){ 243 global $conf; 244 global $auth; 245 246 // prepare disabled actions array and handle legacy options 247 $disabled = explode(',',$conf['disableactions']); 248 $disabled = array_map('trim',$disabled); 249 if(isset($conf['openregister']) && !$conf['openregister']) $disabled[] = 'register'; 250 if(isset($conf['resendpasswd']) && !$conf['resendpasswd']) $disabled[] = 'resendpwd'; 251 if(isset($conf['subscribers']) && !$conf['subscribers']) { 252 $disabled[] = 'subscribe'; 253 } 254 if (is_null($auth) || !$auth->canDo('addUser')) { 255 $disabled[] = 'register'; 256 } 257 if (is_null($auth) || !$auth->canDo('modPass')) { 258 $disabled[] = 'resendpwd'; 259 } 260 $disabled = array_unique($disabled); 261 } 262 263 return !in_array($action,$disabled); 264} 265 266/** 267 * check if headings should be used as link text for the specified link type 268 * 269 * @author Chris Smith <chris@jalakai.co.uk> 270 * 271 * @param string $linktype 'content'|'navigation', content applies to links in wiki text 272 * navigation applies to all other links 273 * @returns boolean true if headings should be used for $linktype, false otherwise 274 */ 275function useHeading($linktype) { 276 static $useHeading = null; 277 278 if (is_null($useHeading)) { 279 global $conf; 280 281 if (!empty($conf['useheading'])) { 282 switch ($conf['useheading']) { 283 case 'content': 284 $useHeading['content'] = true; 285 break; 286 287 case 'navigation': 288 $useHeading['navigation'] = true; 289 break; 290 default: 291 $useHeading['content'] = true; 292 $useHeading['navigation'] = true; 293 } 294 } else { 295 $useHeading = array(); 296 } 297 } 298 299 return (!empty($useHeading[$linktype])); 300} 301 302/** 303 * obscure config data so information isn't plain text 304 * 305 * @param string $str data to be encoded 306 * @param string $code encoding method, values: plain, base64, uuencode. 307 * @return string the encoded value 308 */ 309function conf_encodeString($str,$code) { 310 switch ($code) { 311 case 'base64' : return '<b>'.base64_encode($str); 312 case 'uuencode' : return '<u>'.convert_uuencode($str); 313 case 'plain': 314 default: 315 return $str; 316 } 317} 318/** 319 * return obscured data as plain text 320 * 321 * @param string $str encoded data 322 * @return string plain text 323 */ 324function conf_decodeString($str) { 325 switch (substr($str,0,3)) { 326 case '<b>' : return base64_decode(substr($str,3)); 327 case '<u>' : return convert_uudecode(substr($str,3)); 328 default: // not encode (or unknown) 329 return $str; 330 } 331} 332//Setup VIM: ex: et ts=4 enc=utf-8 : 333