1b625487dSandi<?php 2b625487dSandi/** 3b625487dSandi * Utilities for collecting data from config files 4b625487dSandi * 5b625487dSandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 7b625487dSandi */ 8b625487dSandi 9b625487dSandi 10b625487dSandi/** 11b625487dSandi * Returns the (known) extension and mimetype of a given filename 12b625487dSandi * 1327bf7924STom N Harris * If $knownonly is true (the default), then only known extensions 1427bf7924STom N Harris * are returned. 1527bf7924STom N Harris * 16b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 17b625487dSandi */ 1827bf7924STom N Harrisfunction mimetype($file, $knownonly=true){ 19ecebf3a8SAndreas Gohr $ret = array(false,false,false); // return array 20b625487dSandi $mtypes = getMimeTypes(); // known mimetypes 21b625487dSandi $exts = join('|',array_keys($mtypes)); // known extensions (regexp) 2227bf7924STom N Harris if(!$knownonly){ 2327bf7924STom N Harris $exts = $exts.'|[_\-A-Za-z0-9]+'; // any extension 2427bf7924STom N Harris } 25b625487dSandi if(preg_match('#\.('.$exts.')$#i',$file,$matches)){ 26b625487dSandi $ext = strtolower($matches[1]); 27b625487dSandi } 28b625487dSandi 2927bf7924STom N Harris if($ext){ 3027bf7924STom N Harris if (isset($mtypes[$ext])){ 31ecebf3a8SAndreas Gohr if($mtypes[$ext][0] == '!'){ 32ecebf3a8SAndreas Gohr $ret = array($ext, substr($mtypes[$ext],1), true); 33ecebf3a8SAndreas Gohr }else{ 34ecebf3a8SAndreas Gohr $ret = array($ext, $mtypes[$ext], false); 35ecebf3a8SAndreas Gohr } 36*88b173d7SAndreas Gohr }elseif(!$knownonly){ 3727bf7924STom N Harris $ret = array($ext, 'application/octet-stream', true); 3827bf7924STom N Harris } 39b625487dSandi } 40b625487dSandi 41b625487dSandi return $ret; 42b625487dSandi} 43b625487dSandi 44b625487dSandi/** 45b625487dSandi * returns a hash of mimetypes 46b625487dSandi * 47b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 48b625487dSandi */ 49b625487dSandifunction getMimeTypes() { 50b625487dSandi static $mime = NULL; 51b625487dSandi if ( !$mime ) { 52cb043f52SChris Smith $mime = retrieveConfig('mime','confToHash'); 53b625487dSandi } 54b625487dSandi return $mime; 55b625487dSandi} 56b625487dSandi 57b625487dSandi/** 58b625487dSandi * returns a hash of acronyms 59b625487dSandi * 60b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 61b625487dSandi */ 62b625487dSandifunction getAcronyms() { 63b625487dSandi static $acronyms = NULL; 64b625487dSandi if ( !$acronyms ) { 65cb043f52SChris Smith $acronyms = retrieveConfig('acronyms','confToHash'); 66b625487dSandi } 67b625487dSandi return $acronyms; 68b625487dSandi} 69b625487dSandi 70b625487dSandi/** 71b625487dSandi * returns a hash of smileys 72b625487dSandi * 73b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 74b625487dSandi */ 75b625487dSandifunction getSmileys() { 76b625487dSandi static $smileys = NULL; 77b625487dSandi if ( !$smileys ) { 78cb043f52SChris Smith $smileys = retrieveConfig('smileys','confToHash'); 79b625487dSandi } 80b625487dSandi return $smileys; 81b625487dSandi} 82b625487dSandi 83b625487dSandi/** 84b625487dSandi * returns a hash of entities 85b625487dSandi * 86b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 87b625487dSandi */ 88b625487dSandifunction getEntities() { 89b625487dSandi static $entities = NULL; 90b625487dSandi if ( !$entities ) { 91cb043f52SChris Smith $entities = retrieveConfig('entities','confToHash'); 92b625487dSandi } 93b625487dSandi return $entities; 94b625487dSandi} 95b625487dSandi 96b625487dSandi/** 97b625487dSandi * returns a hash of interwikilinks 98b625487dSandi * 99b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 100b625487dSandi */ 101b625487dSandifunction getInterwiki() { 102b625487dSandi static $wikis = NULL; 103b625487dSandi if ( !$wikis ) { 1044c6a5eccSAndreas Gohr $wikis = retrieveConfig('interwiki','confToHash',array(true)); 105b625487dSandi } 10697a3e4e3Sandi //add sepecial case 'this' 10727a2b085Sandi $wikis['this'] = DOKU_URL.'{NAME}'; 108b625487dSandi return $wikis; 109b625487dSandi} 110b625487dSandi 111b625487dSandi/** 112b9ac8716Schris * returns array of wordblock patterns 113b9ac8716Schris * 114b9ac8716Schris */ 115b9ac8716Schrisfunction getWordblocks() { 116b9ac8716Schris static $wordblocks = NULL; 117b9ac8716Schris if ( !$wordblocks ) { 118cb043f52SChris Smith $wordblocks = retrieveConfig('wordblock','file'); 119b9ac8716Schris } 120b9ac8716Schris return $wordblocks; 121b9ac8716Schris} 122b9ac8716Schris 123b9ac8716Schris 12436f2d7c1SGina Haeussgefunction getSchemes() { 12536f2d7c1SGina Haeussge static $schemes = NULL; 12636f2d7c1SGina Haeussge if ( !$schemes ) { 127cb043f52SChris Smith $schemes = retrieveConfig('scheme','file'); 12836f2d7c1SGina Haeussge } 12936f2d7c1SGina Haeussge $schemes = array_map('trim', $schemes); 13036f2d7c1SGina Haeussge $schemes = preg_replace('/^#.*/', '', $schemes); 13136f2d7c1SGina Haeussge $schemes = array_filter($schemes); 13236f2d7c1SGina Haeussge return $schemes; 13336f2d7c1SGina Haeussge} 13436f2d7c1SGina Haeussge 135b9ac8716Schris/** 136edcb01e5SGina Haeussge * Builds a hash from an array of lines 137b625487dSandi * 1383fd0b676Sandi * If $lower is set to true all hash keys are converted to 1393fd0b676Sandi * lower case. 1403fd0b676Sandi * 141b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 1423fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org> 143edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net> 144b625487dSandi */ 145edcb01e5SGina Haeussgefunction linesToHash($lines, $lower=false) { 146b625487dSandi foreach ( $lines as $line ) { 14703ff8795SAndreas Gohr //ignore comments (except escaped ones) 14803ff8795SAndreas Gohr $line = preg_replace('/(?<![&\\\\])#.*$/','',$line); 14903ff8795SAndreas Gohr $line = str_replace('\\#','#',$line); 150b625487dSandi $line = trim($line); 151b625487dSandi if(empty($line)) continue; 152b625487dSandi $line = preg_split('/\s+/',$line,2); 153b625487dSandi // Build the associative array 15427a2b085Sandi if($lower){ 15527a2b085Sandi $conf[strtolower($line[0])] = $line[1]; 15627a2b085Sandi }else{ 157b625487dSandi $conf[$line[0]] = $line[1]; 158b625487dSandi } 15927a2b085Sandi } 160b625487dSandi 161b625487dSandi return $conf; 162b625487dSandi} 163b625487dSandi 164409d7af7SAndreas Gohr/** 165edcb01e5SGina Haeussge * Builds a hash from a configfile 166edcb01e5SGina Haeussge * 167edcb01e5SGina Haeussge * If $lower is set to true all hash keys are converted to 168edcb01e5SGina Haeussge * lower case. 169edcb01e5SGina Haeussge * 170edcb01e5SGina Haeussge * @author Harry Fuecks <hfuecks@gmail.com> 171edcb01e5SGina Haeussge * @author Andreas Gohr <andi@splitbrain.org> 172edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net> 173edcb01e5SGina Haeussge */ 174edcb01e5SGina Haeussgefunction confToHash($file,$lower=false) { 175edcb01e5SGina Haeussge $conf = array(); 176edcb01e5SGina Haeussge $lines = @file( $file ); 177edcb01e5SGina Haeussge if ( !$lines ) return $conf; 178edcb01e5SGina Haeussge 179edcb01e5SGina Haeussge return linesToHash($lines, $lower); 180edcb01e5SGina Haeussge} 181edcb01e5SGina Haeussge 182edcb01e5SGina Haeussge/** 183cb043f52SChris Smith * Retrieve the requested configuration information 184cb043f52SChris Smith * 185cb043f52SChris Smith * @author Chris Smith <chris@jalakai.co.uk> 186cb043f52SChris Smith * 187cb043f52SChris Smith * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade 188cb043f52SChris Smith * @param callback $fn the function used to process the configuration file into an array 1894c6a5eccSAndreas Gohr * @param array $param optional additional params to pass to the callback 190cb043f52SChris Smith * @return array configuration values 191cb043f52SChris Smith */ 1924c6a5eccSAndreas Gohrfunction retrieveConfig($type,$fn,$params=null) { 193cb043f52SChris Smith global $config_cascade; 194cb043f52SChris Smith 1954c6a5eccSAndreas Gohr if(!is_array($params)) $params = array(); 1964c6a5eccSAndreas Gohr 197cb043f52SChris Smith $combined = array(); 198cb043f52SChris Smith if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING); 199b303b92cSChris Smith foreach (array('default','local','protected') as $config_group) { 200b303b92cSChris Smith if (empty($config_cascade[$type][$config_group])) continue; 201b303b92cSChris Smith foreach ($config_cascade[$type][$config_group] as $file) { 202cb043f52SChris Smith if (@file_exists($file)) { 2034c6a5eccSAndreas Gohr $config = call_user_func_array($fn,array_merge(array($file),$params)); 204cb043f52SChris Smith $combined = array_merge($combined, $config); 205cb043f52SChris Smith } 206cb043f52SChris Smith } 207b303b92cSChris Smith } 208cb043f52SChris Smith 209cb043f52SChris Smith return $combined; 210cb043f52SChris Smith} 211cb043f52SChris Smith 212cb043f52SChris Smith/** 213f8121585SChris Smith * Include the requested configuration information 214f8121585SChris Smith * 215f8121585SChris Smith * @author Chris Smith <chris@jalakai.co.uk> 216f8121585SChris Smith * 217f8121585SChris Smith * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade 218f8121585SChris Smith * @return array list of files, default before local before protected 219f8121585SChris Smith */ 220f8121585SChris Smithfunction getConfigFiles($type) { 221f8121585SChris Smith global $config_cascade; 222f8121585SChris Smith $files = array(); 223f8121585SChris Smith 224f8121585SChris Smith if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING); 225f8121585SChris Smith foreach (array('default','local','protected') as $config_group) { 226f8121585SChris Smith if (empty($config_cascade[$type][$config_group])) continue; 227f8121585SChris Smith $files = array_merge($files, $config_cascade[$type][$config_group]); 228f8121585SChris Smith } 229f8121585SChris Smith 230f8121585SChris Smith return $files; 231f8121585SChris Smith} 232f8121585SChris Smith 233f8121585SChris Smith/** 234409d7af7SAndreas Gohr * check if the given action was disabled in config 235409d7af7SAndreas Gohr * 236409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 237409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled 238409d7af7SAndreas Gohr */ 239409d7af7SAndreas Gohrfunction actionOK($action){ 240409d7af7SAndreas Gohr static $disabled = null; 241409d7af7SAndreas Gohr if(is_null($disabled)){ 242409d7af7SAndreas Gohr global $conf; 243409d7af7SAndreas Gohr 244409d7af7SAndreas Gohr // prepare disabled actions array and handle legacy options 245409d7af7SAndreas Gohr $disabled = explode(',',$conf['disableactions']); 246409d7af7SAndreas Gohr $disabled = array_map('trim',$disabled); 247409d7af7SAndreas Gohr if(isset($conf['openregister']) && !$conf['openregister']) $disabled[] = 'register'; 248409d7af7SAndreas Gohr if(isset($conf['resendpasswd']) && !$conf['resendpasswd']) $disabled[] = 'resendpwd'; 2498c286148SMichael Klier if(isset($conf['subscribers']) && !$conf['subscribers']) { 2508c286148SMichael Klier $disabled[] = 'subscribe'; 2518c286148SMichael Klier $disabled[] = 'subscribens'; 2528c286148SMichael Klier } 253409d7af7SAndreas Gohr $disabled = array_unique($disabled); 254409d7af7SAndreas Gohr } 255409d7af7SAndreas Gohr 256409d7af7SAndreas Gohr return !in_array($action,$disabled); 257409d7af7SAndreas Gohr} 258409d7af7SAndreas Gohr 259fe9ec250SChris Smith/** 260fe9ec250SChris Smith * check if headings should be used as link text for the specified link type 261fe9ec250SChris Smith * 262fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk> 263fe9ec250SChris Smith * 264fe9ec250SChris Smith * @param string $linktype 'content'|'navigation', content applies to links in wiki text 265fe9ec250SChris Smith * navigation applies to all other links 266fe9ec250SChris Smith * @returns boolean true if headings should be used for $linktype, false otherwise 267fe9ec250SChris Smith */ 268fe9ec250SChris Smithfunction useHeading($linktype) { 269fe9ec250SChris Smith static $useHeading = null; 270fe9ec250SChris Smith 271fe9ec250SChris Smith if (is_null($useHeading)) { 272fe9ec250SChris Smith global $conf; 273fe9ec250SChris Smith 274fe9ec250SChris Smith if (!empty($conf['useheading'])) { 275fe9ec250SChris Smith switch ($conf['useheading']) { 276fe9ec250SChris Smith case 'content' : $useHeading['content'] = true; break; 277fe9ec250SChris Smith case 'navigation' : $useHeading['navigation'] = true; break; 278fe9ec250SChris Smith default: 279fe9ec250SChris Smith $useHeading['content'] = true; 280fe9ec250SChris Smith $useHeading['navigation'] = true; 281fe9ec250SChris Smith } 282fe9ec250SChris Smith } else { 283fe9ec250SChris Smith $useHeading = array(); 284fe9ec250SChris Smith } 285fe9ec250SChris Smith } 286fe9ec250SChris Smith 287fe9ec250SChris Smith return (!empty($useHeading[$linktype])); 288fe9ec250SChris Smith} 289fe9ec250SChris Smith 2903994772aSChris Smith/** 2913994772aSChris Smith * obscure config data so information isn't plain text 2923994772aSChris Smith * 2933994772aSChris Smith * @param string $str data to be encoded 2943994772aSChris Smith * @param string $code encoding method, values: plain, base64, uuencode. 2953994772aSChris Smith * @return string the encoded value 2963994772aSChris Smith */ 2973994772aSChris Smithfunction conf_encodeString($str,$code) { 2983994772aSChris Smith switch ($code) { 2993994772aSChris Smith case 'base64' : return '<b>'.base64_encode($str); 3003994772aSChris Smith case 'uuencode' : return '<u>'.convert_uuencode($str); 3013994772aSChris Smith case 'plain': 3023994772aSChris Smith default: 3033994772aSChris Smith return $str; 3043994772aSChris Smith } 3053994772aSChris Smith} 3063994772aSChris Smith/** 3073994772aSChris Smith * return obscured data as plain text 3083994772aSChris Smith * 3093994772aSChris Smith * @param string $str encoded data 3103994772aSChris Smith * @return string plain text 3113994772aSChris Smith */ 3123994772aSChris Smithfunction conf_decodeString($str) { 3133994772aSChris Smith switch (substr($str,0,3)) { 3143994772aSChris Smith case '<b>' : return base64_decode(substr($str,3)); 3153994772aSChris Smith case '<u>' : return convert_uudecode(substr($str,3)); 3163994772aSChris Smith default: // not encode (or unknown) 3173994772aSChris Smith return $str; 3183994772aSChris Smith } 3193994772aSChris Smith} 320b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 : 321