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> 1742ea7f44SGerrit Uitslag * 1842ea7f44SGerrit Uitslag * @param string $file file name 1942ea7f44SGerrit Uitslag * @param bool $knownonly 2042ea7f44SGerrit Uitslag * @return array with extension, mimetype and if it should be downloaded 21b625487dSandi */ 2227bf7924STom N Harrisfunction mimetype($file, $knownonly=true){ 23b625487dSandi $mtypes = getMimeTypes(); // known mimetypes 24ad74fe66SAdrian Lang $ext = strrpos($file, '.'); 25ad74fe66SAdrian Lang if ($ext === false) { 26ad74fe66SAdrian Lang return array(false, false, false); 2727bf7924STom N Harris } 28ad74fe66SAdrian Lang $ext = strtolower(substr($file, $ext + 1)); 29ad74fe66SAdrian Lang if (!isset($mtypes[$ext])){ 30ad74fe66SAdrian Lang if ($knownonly) { 31ad74fe66SAdrian Lang return array(false, false, false); 32ecebf3a8SAndreas Gohr } else { 33ad74fe66SAdrian Lang return array($ext, 'application/octet-stream', true); 3427bf7924STom N Harris } 35b625487dSandi } 36ad74fe66SAdrian Lang if($mtypes[$ext][0] == '!'){ 37ad74fe66SAdrian Lang return array($ext, substr($mtypes[$ext],1), true); 38ad74fe66SAdrian Lang }else{ 39ad74fe66SAdrian Lang return array($ext, $mtypes[$ext], false); 40ad74fe66SAdrian Lang } 41b625487dSandi} 42b625487dSandi 43b625487dSandi/** 44b625487dSandi * returns a hash of mimetypes 45b625487dSandi * 46b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 47b625487dSandi */ 48b625487dSandifunction getMimeTypes() { 4949eb6e38SAndreas Gohr static $mime = null; 50b625487dSandi if ( !$mime ) { 51cb043f52SChris Smith $mime = retrieveConfig('mime','confToHash'); 5245ae4bb8SChristopher Smith $mime = array_filter($mime); 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'); 66*f266a919SChristopher Smith $acronyms = array_filter($acronyms, 'strlen'); 67b625487dSandi } 68b625487dSandi return $acronyms; 69b625487dSandi} 70b625487dSandi 71b625487dSandi/** 72b625487dSandi * returns a hash of smileys 73b625487dSandi * 74b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 75b625487dSandi */ 76b625487dSandifunction getSmileys() { 7749eb6e38SAndreas Gohr static $smileys = null; 78b625487dSandi if ( !$smileys ) { 79cb043f52SChris Smith $smileys = retrieveConfig('smileys','confToHash'); 80*f266a919SChristopher Smith $smileys = array_filter($smileys, 'strlen'); 81b625487dSandi } 82b625487dSandi return $smileys; 83b625487dSandi} 84b625487dSandi 85b625487dSandi/** 86b625487dSandi * returns a hash of entities 87b625487dSandi * 88b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 89b625487dSandi */ 90b625487dSandifunction getEntities() { 9149eb6e38SAndreas Gohr static $entities = null; 92b625487dSandi if ( !$entities ) { 93cb043f52SChris Smith $entities = retrieveConfig('entities','confToHash'); 94*f266a919SChristopher Smith $entities = array_filter($entities, 'strlen'); 95b625487dSandi } 96b625487dSandi return $entities; 97b625487dSandi} 98b625487dSandi 99b625487dSandi/** 100b625487dSandi * returns a hash of interwikilinks 101b625487dSandi * 102b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 103b625487dSandi */ 104b625487dSandifunction getInterwiki() { 10549eb6e38SAndreas Gohr static $wikis = null; 106b625487dSandi if ( !$wikis ) { 1074c6a5eccSAndreas Gohr $wikis = retrieveConfig('interwiki','confToHash',array(true)); 108*f266a919SChristopher Smith $wikis = array_filter($wikis, 'strlen'); 10945ae4bb8SChristopher Smith 11097a3e4e3Sandi //add sepecial case 'this' 11127a2b085Sandi $wikis['this'] = DOKU_URL.'{NAME}'; 11245ae4bb8SChristopher Smith } 113b625487dSandi return $wikis; 114b625487dSandi} 115b625487dSandi 116b625487dSandi/** 117b9ac8716Schris * returns array of wordblock patterns 118b9ac8716Schris * 119b9ac8716Schris */ 120b9ac8716Schrisfunction getWordblocks() { 12149eb6e38SAndreas Gohr static $wordblocks = null; 122b9ac8716Schris if ( !$wordblocks ) { 1234c353447SChristopher Smith $wordblocks = retrieveConfig('wordblock','file',null,'array_merge_with_removal'); 124b9ac8716Schris } 125b9ac8716Schris return $wordblocks; 126b9ac8716Schris} 127b9ac8716Schris 128e3ab6fc5SMichael Hamann/** 129e3ab6fc5SMichael Hamann * Gets the list of configured schemes 130e3ab6fc5SMichael Hamann * 131e3ab6fc5SMichael Hamann * @return array the schemes 132e3ab6fc5SMichael Hamann */ 13336f2d7c1SGina Haeussgefunction getSchemes() { 13449eb6e38SAndreas Gohr static $schemes = null; 13536f2d7c1SGina Haeussge if ( !$schemes ) { 1364c353447SChristopher Smith $schemes = retrieveConfig('scheme','file',null,'array_merge_with_removal'); 13736f2d7c1SGina Haeussge $schemes = array_map('trim', $schemes); 13836f2d7c1SGina Haeussge $schemes = preg_replace('/^#.*/', '', $schemes); 13936f2d7c1SGina Haeussge $schemes = array_filter($schemes); 1404c353447SChristopher Smith } 14136f2d7c1SGina Haeussge return $schemes; 14236f2d7c1SGina Haeussge} 14336f2d7c1SGina Haeussge 144b9ac8716Schris/** 145edcb01e5SGina Haeussge * Builds a hash from an array of lines 146b625487dSandi * 1473fd0b676Sandi * If $lower is set to true all hash keys are converted to 1483fd0b676Sandi * lower case. 1493fd0b676Sandi * 150b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 1513fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org> 152edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net> 153b625487dSandi */ 154edcb01e5SGina Haeussgefunction linesToHash($lines, $lower=false) { 155e5fc893fSAndreas Gohr $conf = array(); 156dd74fecfSMichael Hamann // remove BOM 157dd74fecfSMichael Hamann if (isset($lines[0]) && substr($lines[0],0,3) == pack('CCC',0xef,0xbb,0xbf)) 158dd74fecfSMichael Hamann $lines[0] = substr($lines[0],3); 159b625487dSandi foreach ( $lines as $line ) { 16003ff8795SAndreas Gohr //ignore comments (except escaped ones) 16103ff8795SAndreas Gohr $line = preg_replace('/(?<![&\\\\])#.*$/','',$line); 16203ff8795SAndreas Gohr $line = str_replace('\\#','#',$line); 163b625487dSandi $line = trim($line); 164b625487dSandi if(empty($line)) continue; 165b625487dSandi $line = preg_split('/\s+/',$line,2); 166b625487dSandi // Build the associative array 16727a2b085Sandi if($lower){ 16827a2b085Sandi $conf[strtolower($line[0])] = $line[1]; 16927a2b085Sandi }else{ 170b625487dSandi $conf[$line[0]] = $line[1]; 171b625487dSandi } 17227a2b085Sandi } 173b625487dSandi 174b625487dSandi return $conf; 175b625487dSandi} 176b625487dSandi 177409d7af7SAndreas Gohr/** 178edcb01e5SGina Haeussge * Builds a hash from a configfile 179edcb01e5SGina Haeussge * 180edcb01e5SGina Haeussge * If $lower is set to true all hash keys are converted to 181edcb01e5SGina Haeussge * lower case. 182edcb01e5SGina Haeussge * 183edcb01e5SGina Haeussge * @author Harry Fuecks <hfuecks@gmail.com> 184edcb01e5SGina Haeussge * @author Andreas Gohr <andi@splitbrain.org> 185edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net> 186edcb01e5SGina Haeussge */ 187edcb01e5SGina Haeussgefunction confToHash($file,$lower=false) { 188edcb01e5SGina Haeussge $conf = array(); 189edcb01e5SGina Haeussge $lines = @file( $file ); 190edcb01e5SGina Haeussge if ( !$lines ) return $conf; 191edcb01e5SGina Haeussge 192edcb01e5SGina Haeussge return linesToHash($lines, $lower); 193edcb01e5SGina Haeussge} 194edcb01e5SGina Haeussge 195edcb01e5SGina Haeussge/** 196cb043f52SChris Smith * Retrieve the requested configuration information 197cb043f52SChris Smith * 198cb043f52SChris Smith * @author Chris Smith <chris@jalakai.co.uk> 199cb043f52SChris Smith * 200cb043f52SChris Smith * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade 201cb043f52SChris Smith * @param callback $fn the function used to process the configuration file into an array 202e3ab6fc5SMichael Hamann * @param array $params optional additional params to pass to the callback 203cb043f52SChris Smith * @return array configuration values 204cb043f52SChris Smith */ 2054c353447SChristopher Smithfunction retrieveConfig($type,$fn,$params=null,$combine='array_merge') { 206cb043f52SChris Smith global $config_cascade; 207cb043f52SChris Smith 2084c6a5eccSAndreas Gohr if(!is_array($params)) $params = array(); 2094c6a5eccSAndreas Gohr 210cb043f52SChris Smith $combined = array(); 211cb043f52SChris Smith if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING); 212b303b92cSChris Smith foreach (array('default','local','protected') as $config_group) { 213b303b92cSChris Smith if (empty($config_cascade[$type][$config_group])) continue; 214b303b92cSChris Smith foreach ($config_cascade[$type][$config_group] as $file) { 21579e79377SAndreas Gohr if (file_exists($file)) { 2164c6a5eccSAndreas Gohr $config = call_user_func_array($fn,array_merge(array($file),$params)); 2174c353447SChristopher Smith $combined = $combine($combined, $config); 218cb043f52SChris Smith } 219cb043f52SChris Smith } 220b303b92cSChris Smith } 221cb043f52SChris Smith 222cb043f52SChris Smith return $combined; 223cb043f52SChris Smith} 224cb043f52SChris Smith 225cb043f52SChris Smith/** 226f8121585SChris Smith * Include the requested configuration information 227f8121585SChris Smith * 228f8121585SChris Smith * @author Chris Smith <chris@jalakai.co.uk> 229f8121585SChris Smith * 230f8121585SChris Smith * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade 231f8121585SChris Smith * @return array list of files, default before local before protected 232f8121585SChris Smith */ 233f8121585SChris Smithfunction getConfigFiles($type) { 234f8121585SChris Smith global $config_cascade; 235f8121585SChris Smith $files = array(); 236f8121585SChris Smith 237f8121585SChris Smith if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING); 238f8121585SChris Smith foreach (array('default','local','protected') as $config_group) { 239f8121585SChris Smith if (empty($config_cascade[$type][$config_group])) continue; 240f8121585SChris Smith $files = array_merge($files, $config_cascade[$type][$config_group]); 241f8121585SChris Smith } 242f8121585SChris Smith 243f8121585SChris Smith return $files; 244f8121585SChris Smith} 245f8121585SChris Smith 246f8121585SChris Smith/** 247409d7af7SAndreas Gohr * check if the given action was disabled in config 248409d7af7SAndreas Gohr * 249409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2500e2431b7SGerrit Uitslag * @param string $action 251409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled 252409d7af7SAndreas Gohr */ 253409d7af7SAndreas Gohrfunction actionOK($action){ 254409d7af7SAndreas Gohr static $disabled = null; 255020ea9e1SChristopher Smith if(is_null($disabled) || defined('SIMPLE_TEST')){ 256409d7af7SAndreas Gohr global $conf; 2570e2431b7SGerrit Uitslag /** @var DokuWiki_Auth_Plugin $auth */ 258de4d479aSAdrian Lang global $auth; 259409d7af7SAndreas Gohr 260409d7af7SAndreas Gohr // prepare disabled actions array and handle legacy options 261409d7af7SAndreas Gohr $disabled = explode(',',$conf['disableactions']); 262409d7af7SAndreas Gohr $disabled = array_map('trim',$disabled); 263e4eda66bSAndreas Gohr if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) { 264de4d479aSAdrian Lang $disabled[] = 'register'; 265de4d479aSAdrian Lang } 266e4eda66bSAndreas Gohr if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) { 267de4d479aSAdrian Lang $disabled[] = 'resendpwd'; 268de4d479aSAdrian Lang } 269e4eda66bSAndreas Gohr if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) { 2703a48618aSAnika Henke $disabled[] = 'subscribe'; 2713a48618aSAnika Henke } 2723a48618aSAnika Henke if (is_null($auth) || !$auth->canDo('Profile')) { 2733a48618aSAnika Henke $disabled[] = 'profile'; 2743a48618aSAnika Henke } 2752a7abf2dSChristopher Smith if (is_null($auth) || !$auth->canDo('delUser')) { 2762a7abf2dSChristopher Smith $disabled[] = 'profile_delete'; 2772a7abf2dSChristopher Smith } 2783a48618aSAnika Henke if (is_null($auth)) { 2793a48618aSAnika Henke $disabled[] = 'login'; 2803a48618aSAnika Henke } 2813a48618aSAnika Henke if (is_null($auth) || !$auth->canDo('logout')) { 2823a48618aSAnika Henke $disabled[] = 'logout'; 2833a48618aSAnika Henke } 284409d7af7SAndreas Gohr $disabled = array_unique($disabled); 285409d7af7SAndreas Gohr } 286409d7af7SAndreas Gohr 287409d7af7SAndreas Gohr return !in_array($action,$disabled); 288409d7af7SAndreas Gohr} 289409d7af7SAndreas Gohr 290fe9ec250SChris Smith/** 291fe9ec250SChris Smith * check if headings should be used as link text for the specified link type 292fe9ec250SChris Smith * 293fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk> 294fe9ec250SChris Smith * 295fe9ec250SChris Smith * @param string $linktype 'content'|'navigation', content applies to links in wiki text 296fe9ec250SChris Smith * navigation applies to all other links 297e3ab6fc5SMichael Hamann * @return boolean true if headings should be used for $linktype, false otherwise 298fe9ec250SChris Smith */ 299fe9ec250SChris Smithfunction useHeading($linktype) { 300fe9ec250SChris Smith static $useHeading = null; 301fe9ec250SChris Smith 302fe9ec250SChris Smith if (is_null($useHeading)) { 303fe9ec250SChris Smith global $conf; 304fe9ec250SChris Smith 305fe9ec250SChris Smith if (!empty($conf['useheading'])) { 306fe9ec250SChris Smith switch ($conf['useheading']) { 30749eb6e38SAndreas Gohr case 'content': 30849eb6e38SAndreas Gohr $useHeading['content'] = true; 30949eb6e38SAndreas Gohr break; 31049eb6e38SAndreas Gohr 31149eb6e38SAndreas Gohr case 'navigation': 31249eb6e38SAndreas Gohr $useHeading['navigation'] = true; 31349eb6e38SAndreas Gohr break; 314fe9ec250SChris Smith default: 315fe9ec250SChris Smith $useHeading['content'] = true; 316fe9ec250SChris Smith $useHeading['navigation'] = true; 317fe9ec250SChris Smith } 318fe9ec250SChris Smith } else { 319fe9ec250SChris Smith $useHeading = array(); 320fe9ec250SChris Smith } 321fe9ec250SChris Smith } 322fe9ec250SChris Smith 323fe9ec250SChris Smith return (!empty($useHeading[$linktype])); 324fe9ec250SChris Smith} 325fe9ec250SChris Smith 3263994772aSChris Smith/** 3273994772aSChris Smith * obscure config data so information isn't plain text 3283994772aSChris Smith * 3293994772aSChris Smith * @param string $str data to be encoded 3303994772aSChris Smith * @param string $code encoding method, values: plain, base64, uuencode. 3313994772aSChris Smith * @return string the encoded value 3323994772aSChris Smith */ 3333994772aSChris Smithfunction conf_encodeString($str,$code) { 3343994772aSChris Smith switch ($code) { 3353994772aSChris Smith case 'base64' : return '<b>'.base64_encode($str); 3363994772aSChris Smith case 'uuencode' : return '<u>'.convert_uuencode($str); 3373994772aSChris Smith case 'plain': 3383994772aSChris Smith default: 3393994772aSChris Smith return $str; 3403994772aSChris Smith } 3413994772aSChris Smith} 3423994772aSChris Smith/** 3433994772aSChris Smith * return obscured data as plain text 3443994772aSChris Smith * 3453994772aSChris Smith * @param string $str encoded data 3463994772aSChris Smith * @return string plain text 3473994772aSChris Smith */ 3483994772aSChris Smithfunction conf_decodeString($str) { 3493994772aSChris Smith switch (substr($str,0,3)) { 3503994772aSChris Smith case '<b>' : return base64_decode(substr($str,3)); 3513994772aSChris Smith case '<u>' : return convert_uudecode(substr($str,3)); 3523994772aSChris Smith default: // not encode (or unknown) 3533994772aSChris Smith return $str; 3543994772aSChris Smith } 3553994772aSChris Smith} 3564c353447SChristopher Smith 3574c353447SChristopher Smith/** 3584c353447SChristopher Smith * array combination function to remove negated values (prefixed by !) 3594c353447SChristopher Smith * 3604c353447SChristopher Smith * @param array $current 3614c353447SChristopher Smith * @param array $new 3624c353447SChristopher Smith * 3634c353447SChristopher Smith * @return array the combined array, numeric keys reset 3644c353447SChristopher Smith */ 3654c353447SChristopher Smithfunction array_merge_with_removal($current, $new) { 3664c353447SChristopher Smith foreach ($new as $val) { 3674c353447SChristopher Smith if (substr($val,0,1) == '!') { 3684c353447SChristopher Smith $idx = array_search(substr($val,1),$current); 3694c353447SChristopher Smith if ($idx !== false) { 3704c353447SChristopher Smith unset($current[$idx]); 3714c353447SChristopher Smith } 3724c353447SChristopher Smith } else { 3734c353447SChristopher Smith $current[] = $val; 3744c353447SChristopher Smith } 3754c353447SChristopher Smith } 3764c353447SChristopher Smith 3774c353447SChristopher Smith return array_slice($current,0); 3784c353447SChristopher Smith} 379e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 : 380