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