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){ 19b625487dSandi $mtypes = getMimeTypes(); // known mimetypes 20ad74fe66SAdrian Lang $ext = strrpos($file, '.'); 21ad74fe66SAdrian Lang if ($ext === false) { 22ad74fe66SAdrian Lang return array(false, false, false); 2327bf7924STom N Harris } 24ad74fe66SAdrian Lang $ext = strtolower(substr($file, $ext + 1)); 25ad74fe66SAdrian Lang if (!isset($mtypes[$ext])){ 26ad74fe66SAdrian Lang if ($knownonly) { 27ad74fe66SAdrian Lang return array(false, false, false); 28ecebf3a8SAndreas Gohr } else { 29ad74fe66SAdrian Lang return array($ext, 'application/octet-stream', true); 3027bf7924STom N Harris } 31b625487dSandi } 32ad74fe66SAdrian Lang if($mtypes[$ext][0] == '!'){ 33ad74fe66SAdrian Lang return array($ext, substr($mtypes[$ext],1), true); 34ad74fe66SAdrian Lang }else{ 35ad74fe66SAdrian Lang return array($ext, $mtypes[$ext], false); 36ad74fe66SAdrian Lang } 37b625487dSandi} 38b625487dSandi 39b625487dSandi/** 40b625487dSandi * returns a hash of mimetypes 41b625487dSandi * 42b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 43b625487dSandi */ 44b625487dSandifunction getMimeTypes() { 4549eb6e38SAndreas Gohr static $mime = null; 46b625487dSandi if ( !$mime ) { 47cb043f52SChris Smith $mime = retrieveConfig('mime','confToHash'); 48b625487dSandi } 49b625487dSandi return $mime; 50b625487dSandi} 51b625487dSandi 52b625487dSandi/** 53b625487dSandi * returns a hash of acronyms 54b625487dSandi * 55b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 56b625487dSandi */ 57b625487dSandifunction getAcronyms() { 5849eb6e38SAndreas Gohr static $acronyms = null; 59b625487dSandi if ( !$acronyms ) { 60cb043f52SChris Smith $acronyms = retrieveConfig('acronyms','confToHash'); 61b625487dSandi } 62b625487dSandi return $acronyms; 63b625487dSandi} 64b625487dSandi 65b625487dSandi/** 66b625487dSandi * returns a hash of smileys 67b625487dSandi * 68b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 69b625487dSandi */ 70b625487dSandifunction getSmileys() { 7149eb6e38SAndreas Gohr static $smileys = null; 72b625487dSandi if ( !$smileys ) { 73cb043f52SChris Smith $smileys = retrieveConfig('smileys','confToHash'); 74b625487dSandi } 75b625487dSandi return $smileys; 76b625487dSandi} 77b625487dSandi 78b625487dSandi/** 79b625487dSandi * returns a hash of entities 80b625487dSandi * 81b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 82b625487dSandi */ 83b625487dSandifunction getEntities() { 8449eb6e38SAndreas Gohr static $entities = null; 85b625487dSandi if ( !$entities ) { 86cb043f52SChris Smith $entities = retrieveConfig('entities','confToHash'); 87b625487dSandi } 88b625487dSandi return $entities; 89b625487dSandi} 90b625487dSandi 91b625487dSandi/** 92b625487dSandi * returns a hash of interwikilinks 93b625487dSandi * 94b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 95b625487dSandi */ 96b625487dSandifunction getInterwiki() { 9749eb6e38SAndreas Gohr static $wikis = null; 98b625487dSandi if ( !$wikis ) { 994c6a5eccSAndreas Gohr $wikis = retrieveConfig('interwiki','confToHash',array(true)); 100b625487dSandi } 10197a3e4e3Sandi //add sepecial case 'this' 10227a2b085Sandi $wikis['this'] = DOKU_URL.'{NAME}'; 103b625487dSandi return $wikis; 104b625487dSandi} 105b625487dSandi 106b625487dSandi/** 107b9ac8716Schris * returns array of wordblock patterns 108b9ac8716Schris * 109b9ac8716Schris */ 110b9ac8716Schrisfunction getWordblocks() { 11149eb6e38SAndreas Gohr static $wordblocks = null; 112b9ac8716Schris if ( !$wordblocks ) { 113cb043f52SChris Smith $wordblocks = retrieveConfig('wordblock','file'); 114b9ac8716Schris } 115b9ac8716Schris return $wordblocks; 116b9ac8716Schris} 117b9ac8716Schris 118*e3ab6fc5SMichael Hamann/** 119*e3ab6fc5SMichael Hamann * Gets the list of configured schemes 120*e3ab6fc5SMichael Hamann * 121*e3ab6fc5SMichael Hamann * @return array the schemes 122*e3ab6fc5SMichael Hamann */ 12336f2d7c1SGina Haeussgefunction getSchemes() { 12449eb6e38SAndreas Gohr static $schemes = null; 12536f2d7c1SGina Haeussge if ( !$schemes ) { 126cb043f52SChris Smith $schemes = retrieveConfig('scheme','file'); 12736f2d7c1SGina Haeussge } 12836f2d7c1SGina Haeussge $schemes = array_map('trim', $schemes); 12936f2d7c1SGina Haeussge $schemes = preg_replace('/^#.*/', '', $schemes); 13036f2d7c1SGina Haeussge $schemes = array_filter($schemes); 13136f2d7c1SGina Haeussge return $schemes; 13236f2d7c1SGina Haeussge} 13336f2d7c1SGina Haeussge 134b9ac8716Schris/** 135edcb01e5SGina Haeussge * Builds a hash from an array of lines 136b625487dSandi * 1373fd0b676Sandi * If $lower is set to true all hash keys are converted to 1383fd0b676Sandi * lower case. 1393fd0b676Sandi * 140b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 1413fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org> 142edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net> 143b625487dSandi */ 144edcb01e5SGina Haeussgefunction linesToHash($lines, $lower=false) { 145e5fc893fSAndreas Gohr $conf = array(); 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 189*e3ab6fc5SMichael Hamann * @param array $params 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; 243*e3ab6fc5SMichael Hamann /** @var auth_basic $auth */ 244de4d479aSAdrian Lang global $auth; 245409d7af7SAndreas Gohr 246409d7af7SAndreas Gohr // prepare disabled actions array and handle legacy options 247409d7af7SAndreas Gohr $disabled = explode(',',$conf['disableactions']); 248409d7af7SAndreas Gohr $disabled = array_map('trim',$disabled); 249e4eda66bSAndreas Gohr if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) { 250de4d479aSAdrian Lang $disabled[] = 'register'; 251de4d479aSAdrian Lang } 252e4eda66bSAndreas Gohr if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) { 253de4d479aSAdrian Lang $disabled[] = 'resendpwd'; 254de4d479aSAdrian Lang } 255e4eda66bSAndreas Gohr if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) { 2563a48618aSAnika Henke $disabled[] = 'subscribe'; 2573a48618aSAnika Henke } 2583a48618aSAnika Henke if (is_null($auth) || !$auth->canDo('Profile')) { 2593a48618aSAnika Henke $disabled[] = 'profile'; 2603a48618aSAnika Henke } 2613a48618aSAnika Henke if (is_null($auth)) { 2623a48618aSAnika Henke $disabled[] = 'login'; 2633a48618aSAnika Henke } 2643a48618aSAnika Henke if (is_null($auth) || !$auth->canDo('logout')) { 2653a48618aSAnika Henke $disabled[] = 'logout'; 2663a48618aSAnika Henke } 267409d7af7SAndreas Gohr $disabled = array_unique($disabled); 268409d7af7SAndreas Gohr } 269409d7af7SAndreas Gohr 270409d7af7SAndreas Gohr return !in_array($action,$disabled); 271409d7af7SAndreas Gohr} 272409d7af7SAndreas Gohr 273fe9ec250SChris Smith/** 274fe9ec250SChris Smith * check if headings should be used as link text for the specified link type 275fe9ec250SChris Smith * 276fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk> 277fe9ec250SChris Smith * 278fe9ec250SChris Smith * @param string $linktype 'content'|'navigation', content applies to links in wiki text 279fe9ec250SChris Smith * navigation applies to all other links 280*e3ab6fc5SMichael Hamann * @return boolean true if headings should be used for $linktype, false otherwise 281fe9ec250SChris Smith */ 282fe9ec250SChris Smithfunction useHeading($linktype) { 283fe9ec250SChris Smith static $useHeading = null; 284fe9ec250SChris Smith 285fe9ec250SChris Smith if (is_null($useHeading)) { 286fe9ec250SChris Smith global $conf; 287fe9ec250SChris Smith 288fe9ec250SChris Smith if (!empty($conf['useheading'])) { 289fe9ec250SChris Smith switch ($conf['useheading']) { 29049eb6e38SAndreas Gohr case 'content': 29149eb6e38SAndreas Gohr $useHeading['content'] = true; 29249eb6e38SAndreas Gohr break; 29349eb6e38SAndreas Gohr 29449eb6e38SAndreas Gohr case 'navigation': 29549eb6e38SAndreas Gohr $useHeading['navigation'] = true; 29649eb6e38SAndreas Gohr break; 297fe9ec250SChris Smith default: 298fe9ec250SChris Smith $useHeading['content'] = true; 299fe9ec250SChris Smith $useHeading['navigation'] = true; 300fe9ec250SChris Smith } 301fe9ec250SChris Smith } else { 302fe9ec250SChris Smith $useHeading = array(); 303fe9ec250SChris Smith } 304fe9ec250SChris Smith } 305fe9ec250SChris Smith 306fe9ec250SChris Smith return (!empty($useHeading[$linktype])); 307fe9ec250SChris Smith} 308fe9ec250SChris Smith 3093994772aSChris Smith/** 3103994772aSChris Smith * obscure config data so information isn't plain text 3113994772aSChris Smith * 3123994772aSChris Smith * @param string $str data to be encoded 3133994772aSChris Smith * @param string $code encoding method, values: plain, base64, uuencode. 3143994772aSChris Smith * @return string the encoded value 3153994772aSChris Smith */ 3163994772aSChris Smithfunction conf_encodeString($str,$code) { 3173994772aSChris Smith switch ($code) { 3183994772aSChris Smith case 'base64' : return '<b>'.base64_encode($str); 3193994772aSChris Smith case 'uuencode' : return '<u>'.convert_uuencode($str); 3203994772aSChris Smith case 'plain': 3213994772aSChris Smith default: 3223994772aSChris Smith return $str; 3233994772aSChris Smith } 3243994772aSChris Smith} 3253994772aSChris Smith/** 3263994772aSChris Smith * return obscured data as plain text 3273994772aSChris Smith * 3283994772aSChris Smith * @param string $str encoded data 3293994772aSChris Smith * @return string plain text 3303994772aSChris Smith */ 3313994772aSChris Smithfunction conf_decodeString($str) { 3323994772aSChris Smith switch (substr($str,0,3)) { 3333994772aSChris Smith case '<b>' : return base64_decode(substr($str,3)); 3343994772aSChris Smith case '<u>' : return convert_uudecode(substr($str,3)); 3353994772aSChris Smith default: // not encode (or unknown) 3363994772aSChris Smith return $str; 3373994772aSChris Smith } 3383994772aSChris Smith} 339e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 : 340