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 910b38f10SChristopher Smith/* 1010b38f10SChristopher Smith * line prefix used to negate single value config items 1110b38f10SChristopher Smith * (scheme.conf & stopwords.conf), e.g. 1210b38f10SChristopher Smith * !gopher 1310b38f10SChristopher Smith */ 1410b38f10SChristopher Smithconst DOKU_CONF_NEGATION = '!'; 15b625487dSandi 16b625487dSandi/** 17b625487dSandi * Returns the (known) extension and mimetype of a given filename 18b625487dSandi * 1927bf7924STom N Harris * If $knownonly is true (the default), then only known extensions 2027bf7924STom N Harris * are returned. 2127bf7924STom N Harris * 22b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 2342ea7f44SGerrit Uitslag * 2442ea7f44SGerrit Uitslag * @param string $file file name 2542ea7f44SGerrit Uitslag * @param bool $knownonly 2642ea7f44SGerrit Uitslag * @return array with extension, mimetype and if it should be downloaded 27b625487dSandi */ 2827bf7924STom N Harrisfunction mimetype($file, $knownonly=true){ 29b625487dSandi $mtypes = getMimeTypes(); // known mimetypes 30ad74fe66SAdrian Lang $ext = strrpos($file, '.'); 31ad74fe66SAdrian Lang if ($ext === false) { 32ad74fe66SAdrian Lang return array(false, false, false); 3327bf7924STom N Harris } 34ad74fe66SAdrian Lang $ext = strtolower(substr($file, $ext + 1)); 35ad74fe66SAdrian Lang if (!isset($mtypes[$ext])){ 36ad74fe66SAdrian Lang if ($knownonly) { 37ad74fe66SAdrian Lang return array(false, false, false); 38ecebf3a8SAndreas Gohr } else { 39ad74fe66SAdrian Lang return array($ext, 'application/octet-stream', true); 4027bf7924STom N Harris } 41b625487dSandi } 42ad74fe66SAdrian Lang if($mtypes[$ext][0] == '!'){ 43ad74fe66SAdrian Lang return array($ext, substr($mtypes[$ext],1), true); 44ad74fe66SAdrian Lang }else{ 45ad74fe66SAdrian Lang return array($ext, $mtypes[$ext], false); 46ad74fe66SAdrian Lang } 47b625487dSandi} 48b625487dSandi 49b625487dSandi/** 50b625487dSandi * returns a hash of mimetypes 51b625487dSandi * 52b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 53b625487dSandi */ 54b625487dSandifunction getMimeTypes() { 5549eb6e38SAndreas Gohr static $mime = null; 56b625487dSandi if ( !$mime ) { 57cb043f52SChris Smith $mime = retrieveConfig('mime','confToHash'); 5845ae4bb8SChristopher Smith $mime = array_filter($mime); 59b625487dSandi } 60b625487dSandi return $mime; 61b625487dSandi} 62b625487dSandi 63b625487dSandi/** 64b625487dSandi * returns a hash of acronyms 65b625487dSandi * 66b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 67b625487dSandi */ 68b625487dSandifunction getAcronyms() { 6949eb6e38SAndreas Gohr static $acronyms = null; 70b625487dSandi if ( !$acronyms ) { 71cb043f52SChris Smith $acronyms = retrieveConfig('acronyms','confToHash'); 72f266a919SChristopher Smith $acronyms = array_filter($acronyms, 'strlen'); 73b625487dSandi } 74b625487dSandi return $acronyms; 75b625487dSandi} 76b625487dSandi 77b625487dSandi/** 78b625487dSandi * returns a hash of smileys 79b625487dSandi * 80b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 81b625487dSandi */ 82b625487dSandifunction getSmileys() { 8349eb6e38SAndreas Gohr static $smileys = null; 84b625487dSandi if ( !$smileys ) { 85cb043f52SChris Smith $smileys = retrieveConfig('smileys','confToHash'); 86f266a919SChristopher Smith $smileys = array_filter($smileys, 'strlen'); 87b625487dSandi } 88b625487dSandi return $smileys; 89b625487dSandi} 90b625487dSandi 91b625487dSandi/** 92b625487dSandi * returns a hash of entities 93b625487dSandi * 94b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 95b625487dSandi */ 96b625487dSandifunction getEntities() { 9749eb6e38SAndreas Gohr static $entities = null; 98b625487dSandi if ( !$entities ) { 99cb043f52SChris Smith $entities = retrieveConfig('entities','confToHash'); 100f266a919SChristopher Smith $entities = array_filter($entities, 'strlen'); 101b625487dSandi } 102b625487dSandi return $entities; 103b625487dSandi} 104b625487dSandi 105b625487dSandi/** 106b625487dSandi * returns a hash of interwikilinks 107b625487dSandi * 108b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 109b625487dSandi */ 110b625487dSandifunction getInterwiki() { 11149eb6e38SAndreas Gohr static $wikis = null; 112b625487dSandi if ( !$wikis ) { 1134c6a5eccSAndreas Gohr $wikis = retrieveConfig('interwiki','confToHash',array(true)); 114f266a919SChristopher Smith $wikis = array_filter($wikis, 'strlen'); 11545ae4bb8SChristopher Smith 11697a3e4e3Sandi //add sepecial case 'this' 11727a2b085Sandi $wikis['this'] = DOKU_URL.'{NAME}'; 11845ae4bb8SChristopher Smith } 119b625487dSandi return $wikis; 120b625487dSandi} 121b625487dSandi 122b625487dSandi/** 123b9ac8716Schris * returns array of wordblock patterns 124b9ac8716Schris * 125b9ac8716Schris */ 126b9ac8716Schrisfunction getWordblocks() { 12749eb6e38SAndreas Gohr static $wordblocks = null; 128b9ac8716Schris if ( !$wordblocks ) { 1294c353447SChristopher Smith $wordblocks = retrieveConfig('wordblock','file',null,'array_merge_with_removal'); 130b9ac8716Schris } 131b9ac8716Schris return $wordblocks; 132b9ac8716Schris} 133b9ac8716Schris 134e3ab6fc5SMichael Hamann/** 135e3ab6fc5SMichael Hamann * Gets the list of configured schemes 136e3ab6fc5SMichael Hamann * 137e3ab6fc5SMichael Hamann * @return array the schemes 138e3ab6fc5SMichael Hamann */ 13936f2d7c1SGina Haeussgefunction getSchemes() { 14049eb6e38SAndreas Gohr static $schemes = null; 14136f2d7c1SGina Haeussge if ( !$schemes ) { 1424c353447SChristopher Smith $schemes = retrieveConfig('scheme','file',null,'array_merge_with_removal'); 14336f2d7c1SGina Haeussge $schemes = array_map('trim', $schemes); 14436f2d7c1SGina Haeussge $schemes = preg_replace('/^#.*/', '', $schemes); 14536f2d7c1SGina Haeussge $schemes = array_filter($schemes); 1464c353447SChristopher Smith } 14736f2d7c1SGina Haeussge return $schemes; 14836f2d7c1SGina Haeussge} 14936f2d7c1SGina Haeussge 150b9ac8716Schris/** 151edcb01e5SGina Haeussge * Builds a hash from an array of lines 152b625487dSandi * 1533fd0b676Sandi * If $lower is set to true all hash keys are converted to 1543fd0b676Sandi * lower case. 1553fd0b676Sandi * 156b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 1573fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org> 158edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net> 159b625487dSandi */ 160edcb01e5SGina Haeussgefunction linesToHash($lines, $lower=false) { 161e5fc893fSAndreas Gohr $conf = array(); 162dd74fecfSMichael Hamann // remove BOM 163dd74fecfSMichael Hamann if (isset($lines[0]) && substr($lines[0],0,3) == pack('CCC',0xef,0xbb,0xbf)) 164dd74fecfSMichael Hamann $lines[0] = substr($lines[0],3); 165b625487dSandi foreach ( $lines as $line ) { 16603ff8795SAndreas Gohr //ignore comments (except escaped ones) 16703ff8795SAndreas Gohr $line = preg_replace('/(?<![&\\\\])#.*$/','',$line); 16803ff8795SAndreas Gohr $line = str_replace('\\#','#',$line); 169b625487dSandi $line = trim($line); 170b625487dSandi if(empty($line)) continue; 171b625487dSandi $line = preg_split('/\s+/',$line,2); 172b625487dSandi // Build the associative array 17327a2b085Sandi if($lower){ 17427a2b085Sandi $conf[strtolower($line[0])] = $line[1]; 17527a2b085Sandi }else{ 176b625487dSandi $conf[$line[0]] = $line[1]; 177b625487dSandi } 17827a2b085Sandi } 179b625487dSandi 180b625487dSandi return $conf; 181b625487dSandi} 182b625487dSandi 183409d7af7SAndreas Gohr/** 184edcb01e5SGina Haeussge * Builds a hash from a configfile 185edcb01e5SGina Haeussge * 186edcb01e5SGina Haeussge * If $lower is set to true all hash keys are converted to 187edcb01e5SGina Haeussge * lower case. 188edcb01e5SGina Haeussge * 189edcb01e5SGina Haeussge * @author Harry Fuecks <hfuecks@gmail.com> 190edcb01e5SGina Haeussge * @author Andreas Gohr <andi@splitbrain.org> 191edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net> 192edcb01e5SGina Haeussge */ 193edcb01e5SGina Haeussgefunction confToHash($file,$lower=false) { 194edcb01e5SGina Haeussge $conf = array(); 195edcb01e5SGina Haeussge $lines = @file( $file ); 196edcb01e5SGina Haeussge if ( !$lines ) return $conf; 197edcb01e5SGina Haeussge 198edcb01e5SGina Haeussge return linesToHash($lines, $lower); 199edcb01e5SGina Haeussge} 200edcb01e5SGina Haeussge 201edcb01e5SGina Haeussge/** 202cb043f52SChris Smith * Retrieve the requested configuration information 203cb043f52SChris Smith * 204cb043f52SChris Smith * @author Chris Smith <chris@jalakai.co.uk> 205cb043f52SChris Smith * 206cb043f52SChris Smith * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade 207cb043f52SChris Smith * @param callback $fn the function used to process the configuration file into an array 208e3ab6fc5SMichael Hamann * @param array $params optional additional params to pass to the callback 209*4286c64eSChristopher Smith * @param callback $combine the function used to combine arrays of values read from different configuration files; 210*4286c64eSChristopher Smith * the function takes two parameters, 211*4286c64eSChristopher Smith * $combined - the already read & merged configuration values 212*4286c64eSChristopher Smith * $new - array of config values from the config cascade file being currently processed 213*4286c64eSChristopher Smith * and returns an array of the merged configuration values. 214cb043f52SChris Smith * @return array configuration values 215cb043f52SChris Smith */ 2164c353447SChristopher Smithfunction retrieveConfig($type,$fn,$params=null,$combine='array_merge') { 217cb043f52SChris Smith global $config_cascade; 218cb043f52SChris Smith 2194c6a5eccSAndreas Gohr if(!is_array($params)) $params = array(); 2204c6a5eccSAndreas Gohr 221cb043f52SChris Smith $combined = array(); 222cb043f52SChris Smith if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING); 223b303b92cSChris Smith foreach (array('default','local','protected') as $config_group) { 224b303b92cSChris Smith if (empty($config_cascade[$type][$config_group])) continue; 225b303b92cSChris Smith foreach ($config_cascade[$type][$config_group] as $file) { 22679e79377SAndreas Gohr if (file_exists($file)) { 2274c6a5eccSAndreas Gohr $config = call_user_func_array($fn,array_merge(array($file),$params)); 2284c353447SChristopher Smith $combined = $combine($combined, $config); 229cb043f52SChris Smith } 230cb043f52SChris Smith } 231b303b92cSChris Smith } 232cb043f52SChris Smith 233cb043f52SChris Smith return $combined; 234cb043f52SChris Smith} 235cb043f52SChris Smith 236cb043f52SChris Smith/** 237f8121585SChris Smith * Include the requested configuration information 238f8121585SChris Smith * 239f8121585SChris Smith * @author Chris Smith <chris@jalakai.co.uk> 240f8121585SChris Smith * 241f8121585SChris Smith * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade 242f8121585SChris Smith * @return array list of files, default before local before protected 243f8121585SChris Smith */ 244f8121585SChris Smithfunction getConfigFiles($type) { 245f8121585SChris Smith global $config_cascade; 246f8121585SChris Smith $files = array(); 247f8121585SChris Smith 248f8121585SChris Smith if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING); 249f8121585SChris Smith foreach (array('default','local','protected') as $config_group) { 250f8121585SChris Smith if (empty($config_cascade[$type][$config_group])) continue; 251f8121585SChris Smith $files = array_merge($files, $config_cascade[$type][$config_group]); 252f8121585SChris Smith } 253f8121585SChris Smith 254f8121585SChris Smith return $files; 255f8121585SChris Smith} 256f8121585SChris Smith 257f8121585SChris Smith/** 258409d7af7SAndreas Gohr * check if the given action was disabled in config 259409d7af7SAndreas Gohr * 260409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2610e2431b7SGerrit Uitslag * @param string $action 262409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled 263409d7af7SAndreas Gohr */ 264409d7af7SAndreas Gohrfunction actionOK($action){ 265409d7af7SAndreas Gohr static $disabled = null; 266020ea9e1SChristopher Smith if(is_null($disabled) || defined('SIMPLE_TEST')){ 267409d7af7SAndreas Gohr global $conf; 2680e2431b7SGerrit Uitslag /** @var DokuWiki_Auth_Plugin $auth */ 269de4d479aSAdrian Lang global $auth; 270409d7af7SAndreas Gohr 271409d7af7SAndreas Gohr // prepare disabled actions array and handle legacy options 272409d7af7SAndreas Gohr $disabled = explode(',',$conf['disableactions']); 273409d7af7SAndreas Gohr $disabled = array_map('trim',$disabled); 274e4eda66bSAndreas Gohr if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) { 275de4d479aSAdrian Lang $disabled[] = 'register'; 276de4d479aSAdrian Lang } 277e4eda66bSAndreas Gohr if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) { 278de4d479aSAdrian Lang $disabled[] = 'resendpwd'; 279de4d479aSAdrian Lang } 280e4eda66bSAndreas Gohr if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) { 2813a48618aSAnika Henke $disabled[] = 'subscribe'; 2823a48618aSAnika Henke } 2833a48618aSAnika Henke if (is_null($auth) || !$auth->canDo('Profile')) { 2843a48618aSAnika Henke $disabled[] = 'profile'; 2853a48618aSAnika Henke } 2862a7abf2dSChristopher Smith if (is_null($auth) || !$auth->canDo('delUser')) { 2872a7abf2dSChristopher Smith $disabled[] = 'profile_delete'; 2882a7abf2dSChristopher Smith } 2893a48618aSAnika Henke if (is_null($auth)) { 2903a48618aSAnika Henke $disabled[] = 'login'; 2913a48618aSAnika Henke } 2923a48618aSAnika Henke if (is_null($auth) || !$auth->canDo('logout')) { 2933a48618aSAnika Henke $disabled[] = 'logout'; 2943a48618aSAnika Henke } 295409d7af7SAndreas Gohr $disabled = array_unique($disabled); 296409d7af7SAndreas Gohr } 297409d7af7SAndreas Gohr 298409d7af7SAndreas Gohr return !in_array($action,$disabled); 299409d7af7SAndreas Gohr} 300409d7af7SAndreas Gohr 301fe9ec250SChris Smith/** 302fe9ec250SChris Smith * check if headings should be used as link text for the specified link type 303fe9ec250SChris Smith * 304fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk> 305fe9ec250SChris Smith * 306fe9ec250SChris Smith * @param string $linktype 'content'|'navigation', content applies to links in wiki text 307fe9ec250SChris Smith * navigation applies to all other links 308e3ab6fc5SMichael Hamann * @return boolean true if headings should be used for $linktype, false otherwise 309fe9ec250SChris Smith */ 310fe9ec250SChris Smithfunction useHeading($linktype) { 311fe9ec250SChris Smith static $useHeading = null; 312fe9ec250SChris Smith 313fe9ec250SChris Smith if (is_null($useHeading)) { 314fe9ec250SChris Smith global $conf; 315fe9ec250SChris Smith 316fe9ec250SChris Smith if (!empty($conf['useheading'])) { 317fe9ec250SChris Smith switch ($conf['useheading']) { 31849eb6e38SAndreas Gohr case 'content': 31949eb6e38SAndreas Gohr $useHeading['content'] = true; 32049eb6e38SAndreas Gohr break; 32149eb6e38SAndreas Gohr 32249eb6e38SAndreas Gohr case 'navigation': 32349eb6e38SAndreas Gohr $useHeading['navigation'] = true; 32449eb6e38SAndreas Gohr break; 325fe9ec250SChris Smith default: 326fe9ec250SChris Smith $useHeading['content'] = true; 327fe9ec250SChris Smith $useHeading['navigation'] = true; 328fe9ec250SChris Smith } 329fe9ec250SChris Smith } else { 330fe9ec250SChris Smith $useHeading = array(); 331fe9ec250SChris Smith } 332fe9ec250SChris Smith } 333fe9ec250SChris Smith 334fe9ec250SChris Smith return (!empty($useHeading[$linktype])); 335fe9ec250SChris Smith} 336fe9ec250SChris Smith 3373994772aSChris Smith/** 3383994772aSChris Smith * obscure config data so information isn't plain text 3393994772aSChris Smith * 3403994772aSChris Smith * @param string $str data to be encoded 3413994772aSChris Smith * @param string $code encoding method, values: plain, base64, uuencode. 3423994772aSChris Smith * @return string the encoded value 3433994772aSChris Smith */ 3443994772aSChris Smithfunction conf_encodeString($str,$code) { 3453994772aSChris Smith switch ($code) { 3463994772aSChris Smith case 'base64' : return '<b>'.base64_encode($str); 3473994772aSChris Smith case 'uuencode' : return '<u>'.convert_uuencode($str); 3483994772aSChris Smith case 'plain': 3493994772aSChris Smith default: 3503994772aSChris Smith return $str; 3513994772aSChris Smith } 3523994772aSChris Smith} 3533994772aSChris Smith/** 3543994772aSChris Smith * return obscured data as plain text 3553994772aSChris Smith * 3563994772aSChris Smith * @param string $str encoded data 3573994772aSChris Smith * @return string plain text 3583994772aSChris Smith */ 3593994772aSChris Smithfunction conf_decodeString($str) { 3603994772aSChris Smith switch (substr($str,0,3)) { 3613994772aSChris Smith case '<b>' : return base64_decode(substr($str,3)); 3623994772aSChris Smith case '<u>' : return convert_uudecode(substr($str,3)); 3633994772aSChris Smith default: // not encode (or unknown) 3643994772aSChris Smith return $str; 3653994772aSChris Smith } 3663994772aSChris Smith} 3674c353447SChristopher Smith 3684c353447SChristopher Smith/** 3694c353447SChristopher Smith * array combination function to remove negated values (prefixed by !) 3704c353447SChristopher Smith * 3714c353447SChristopher Smith * @param array $current 3724c353447SChristopher Smith * @param array $new 3734c353447SChristopher Smith * 3744c353447SChristopher Smith * @return array the combined array, numeric keys reset 3754c353447SChristopher Smith */ 3764c353447SChristopher Smithfunction array_merge_with_removal($current, $new) { 3774c353447SChristopher Smith foreach ($new as $val) { 37810b38f10SChristopher Smith if (substr($val,0,1) == DOKU_CONF_NEGATION) { 3793a7669bdSChristopher Smith $idx = array_search(trim(substr($val,1)),$current); 3804c353447SChristopher Smith if ($idx !== false) { 3814c353447SChristopher Smith unset($current[$idx]); 3824c353447SChristopher Smith } 3834c353447SChristopher Smith } else { 3843a7669bdSChristopher Smith $current[] = trim($val); 3854c353447SChristopher Smith } 3864c353447SChristopher Smith } 3874c353447SChristopher Smith 3884c353447SChristopher Smith return array_slice($current,0); 3894c353447SChristopher Smith} 390e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 : 391