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