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