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 } 3688b173d7SAndreas 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() { 5049eb6e38SAndreas Gohr 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() { 6349eb6e38SAndreas Gohr 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() { 7649eb6e38SAndreas Gohr 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() { 8949eb6e38SAndreas Gohr 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() { 10249eb6e38SAndreas Gohr 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() { 11649eb6e38SAndreas Gohr static $wordblocks = null; 117b9ac8716Schris if ( !$wordblocks ) { 118cb043f52SChris Smith $wordblocks = retrieveConfig('wordblock','file'); 119b9ac8716Schris } 120b9ac8716Schris return $wordblocks; 121b9ac8716Schris} 122b9ac8716Schris 123b9ac8716Schris 12436f2d7c1SGina Haeussgefunction getSchemes() { 12549eb6e38SAndreas Gohr 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) { 146*e5fc893fSAndreas Gohr $conf = array(); 147b625487dSandi foreach ( $lines as $line ) { 14803ff8795SAndreas Gohr //ignore comments (except escaped ones) 14903ff8795SAndreas Gohr $line = preg_replace('/(?<![&\\\\])#.*$/','',$line); 15003ff8795SAndreas Gohr $line = str_replace('\\#','#',$line); 151b625487dSandi $line = trim($line); 152b625487dSandi if(empty($line)) continue; 153b625487dSandi $line = preg_split('/\s+/',$line,2); 154b625487dSandi // Build the associative array 15527a2b085Sandi if($lower){ 15627a2b085Sandi $conf[strtolower($line[0])] = $line[1]; 15727a2b085Sandi }else{ 158b625487dSandi $conf[$line[0]] = $line[1]; 159b625487dSandi } 16027a2b085Sandi } 161b625487dSandi 162b625487dSandi return $conf; 163b625487dSandi} 164b625487dSandi 165409d7af7SAndreas Gohr/** 166edcb01e5SGina Haeussge * Builds a hash from a configfile 167edcb01e5SGina Haeussge * 168edcb01e5SGina Haeussge * If $lower is set to true all hash keys are converted to 169edcb01e5SGina Haeussge * lower case. 170edcb01e5SGina Haeussge * 171edcb01e5SGina Haeussge * @author Harry Fuecks <hfuecks@gmail.com> 172edcb01e5SGina Haeussge * @author Andreas Gohr <andi@splitbrain.org> 173edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net> 174edcb01e5SGina Haeussge */ 175edcb01e5SGina Haeussgefunction confToHash($file,$lower=false) { 176edcb01e5SGina Haeussge $conf = array(); 177edcb01e5SGina Haeussge $lines = @file( $file ); 178edcb01e5SGina Haeussge if ( !$lines ) return $conf; 179edcb01e5SGina Haeussge 180edcb01e5SGina Haeussge return linesToHash($lines, $lower); 181edcb01e5SGina Haeussge} 182edcb01e5SGina Haeussge 183edcb01e5SGina Haeussge/** 184cb043f52SChris Smith * Retrieve the requested configuration information 185cb043f52SChris Smith * 186cb043f52SChris Smith * @author Chris Smith <chris@jalakai.co.uk> 187cb043f52SChris Smith * 188cb043f52SChris Smith * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade 189cb043f52SChris Smith * @param callback $fn the function used to process the configuration file into an array 1904c6a5eccSAndreas Gohr * @param array $param optional additional params to pass to the callback 191cb043f52SChris Smith * @return array configuration values 192cb043f52SChris Smith */ 1934c6a5eccSAndreas Gohrfunction retrieveConfig($type,$fn,$params=null) { 194cb043f52SChris Smith global $config_cascade; 195cb043f52SChris Smith 1964c6a5eccSAndreas Gohr if(!is_array($params)) $params = array(); 1974c6a5eccSAndreas Gohr 198cb043f52SChris Smith $combined = array(); 199cb043f52SChris Smith if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING); 200b303b92cSChris Smith foreach (array('default','local','protected') as $config_group) { 201b303b92cSChris Smith if (empty($config_cascade[$type][$config_group])) continue; 202b303b92cSChris Smith foreach ($config_cascade[$type][$config_group] as $file) { 203cb043f52SChris Smith if (@file_exists($file)) { 2044c6a5eccSAndreas Gohr $config = call_user_func_array($fn,array_merge(array($file),$params)); 205cb043f52SChris Smith $combined = array_merge($combined, $config); 206cb043f52SChris Smith } 207cb043f52SChris Smith } 208b303b92cSChris Smith } 209cb043f52SChris Smith 210cb043f52SChris Smith return $combined; 211cb043f52SChris Smith} 212cb043f52SChris Smith 213cb043f52SChris Smith/** 214f8121585SChris Smith * Include the requested configuration information 215f8121585SChris Smith * 216f8121585SChris Smith * @author Chris Smith <chris@jalakai.co.uk> 217f8121585SChris Smith * 218f8121585SChris Smith * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade 219f8121585SChris Smith * @return array list of files, default before local before protected 220f8121585SChris Smith */ 221f8121585SChris Smithfunction getConfigFiles($type) { 222f8121585SChris Smith global $config_cascade; 223f8121585SChris Smith $files = array(); 224f8121585SChris Smith 225f8121585SChris Smith if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING); 226f8121585SChris Smith foreach (array('default','local','protected') as $config_group) { 227f8121585SChris Smith if (empty($config_cascade[$type][$config_group])) continue; 228f8121585SChris Smith $files = array_merge($files, $config_cascade[$type][$config_group]); 229f8121585SChris Smith } 230f8121585SChris Smith 231f8121585SChris Smith return $files; 232f8121585SChris Smith} 233f8121585SChris Smith 234f8121585SChris Smith/** 235409d7af7SAndreas Gohr * check if the given action was disabled in config 236409d7af7SAndreas Gohr * 237409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 238409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled 239409d7af7SAndreas Gohr */ 240409d7af7SAndreas Gohrfunction actionOK($action){ 241409d7af7SAndreas Gohr static $disabled = null; 242409d7af7SAndreas Gohr if(is_null($disabled)){ 243409d7af7SAndreas Gohr global $conf; 244409d7af7SAndreas Gohr 245409d7af7SAndreas Gohr // prepare disabled actions array and handle legacy options 246409d7af7SAndreas Gohr $disabled = explode(',',$conf['disableactions']); 247409d7af7SAndreas Gohr $disabled = array_map('trim',$disabled); 248409d7af7SAndreas Gohr if(isset($conf['openregister']) && !$conf['openregister']) $disabled[] = 'register'; 249409d7af7SAndreas Gohr if(isset($conf['resendpasswd']) && !$conf['resendpasswd']) $disabled[] = 'resendpwd'; 2508c286148SMichael Klier if(isset($conf['subscribers']) && !$conf['subscribers']) { 2518c286148SMichael Klier $disabled[] = 'subscribe'; 2528c286148SMichael Klier $disabled[] = 'subscribens'; 2538c286148SMichael Klier } 254409d7af7SAndreas Gohr $disabled = array_unique($disabled); 255409d7af7SAndreas Gohr } 256409d7af7SAndreas Gohr 257409d7af7SAndreas Gohr return !in_array($action,$disabled); 258409d7af7SAndreas Gohr} 259409d7af7SAndreas Gohr 260fe9ec250SChris Smith/** 261fe9ec250SChris Smith * check if headings should be used as link text for the specified link type 262fe9ec250SChris Smith * 263fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk> 264fe9ec250SChris Smith * 265fe9ec250SChris Smith * @param string $linktype 'content'|'navigation', content applies to links in wiki text 266fe9ec250SChris Smith * navigation applies to all other links 267fe9ec250SChris Smith * @returns boolean true if headings should be used for $linktype, false otherwise 268fe9ec250SChris Smith */ 269fe9ec250SChris Smithfunction useHeading($linktype) { 270fe9ec250SChris Smith static $useHeading = null; 271fe9ec250SChris Smith 272fe9ec250SChris Smith if (is_null($useHeading)) { 273fe9ec250SChris Smith global $conf; 274fe9ec250SChris Smith 275fe9ec250SChris Smith if (!empty($conf['useheading'])) { 276fe9ec250SChris Smith switch ($conf['useheading']) { 27749eb6e38SAndreas Gohr case 'content': 27849eb6e38SAndreas Gohr $useHeading['content'] = true; 27949eb6e38SAndreas Gohr break; 28049eb6e38SAndreas Gohr 28149eb6e38SAndreas Gohr case 'navigation': 28249eb6e38SAndreas Gohr $useHeading['navigation'] = true; 28349eb6e38SAndreas Gohr break; 284fe9ec250SChris Smith default: 285fe9ec250SChris Smith $useHeading['content'] = true; 286fe9ec250SChris Smith $useHeading['navigation'] = true; 287fe9ec250SChris Smith } 288fe9ec250SChris Smith } else { 289fe9ec250SChris Smith $useHeading = array(); 290fe9ec250SChris Smith } 291fe9ec250SChris Smith } 292fe9ec250SChris Smith 293fe9ec250SChris Smith return (!empty($useHeading[$linktype])); 294fe9ec250SChris Smith} 295fe9ec250SChris Smith 2963994772aSChris Smith/** 2973994772aSChris Smith * obscure config data so information isn't plain text 2983994772aSChris Smith * 2993994772aSChris Smith * @param string $str data to be encoded 3003994772aSChris Smith * @param string $code encoding method, values: plain, base64, uuencode. 3013994772aSChris Smith * @return string the encoded value 3023994772aSChris Smith */ 3033994772aSChris Smithfunction conf_encodeString($str,$code) { 3043994772aSChris Smith switch ($code) { 3053994772aSChris Smith case 'base64' : return '<b>'.base64_encode($str); 3063994772aSChris Smith case 'uuencode' : return '<u>'.convert_uuencode($str); 3073994772aSChris Smith case 'plain': 3083994772aSChris Smith default: 3093994772aSChris Smith return $str; 3103994772aSChris Smith } 3113994772aSChris Smith} 3123994772aSChris Smith/** 3133994772aSChris Smith * return obscured data as plain text 3143994772aSChris Smith * 3153994772aSChris Smith * @param string $str encoded data 3163994772aSChris Smith * @return string plain text 3173994772aSChris Smith */ 3183994772aSChris Smithfunction conf_decodeString($str) { 3193994772aSChris Smith switch (substr($str,0,3)) { 3203994772aSChris Smith case '<b>' : return base64_decode(substr($str,3)); 3213994772aSChris Smith case '<u>' : return convert_uudecode(substr($str,3)); 3223994772aSChris Smith default: // not encode (or unknown) 3233994772aSChris Smith return $str; 3243994772aSChris Smith } 3253994772aSChris Smith} 32649eb6e38SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 : 327