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> 17*42ea7f44SGerrit Uitslag * 18*42ea7f44SGerrit Uitslag * @param string $file file name 19*42ea7f44SGerrit Uitslag * @param bool $knownonly 20*42ea7f44SGerrit 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'); 52b625487dSandi } 53b625487dSandi return $mime; 54b625487dSandi} 55b625487dSandi 56b625487dSandi/** 57b625487dSandi * returns a hash of acronyms 58b625487dSandi * 59b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 60b625487dSandi */ 61b625487dSandifunction getAcronyms() { 6249eb6e38SAndreas Gohr static $acronyms = null; 63b625487dSandi if ( !$acronyms ) { 64cb043f52SChris Smith $acronyms = retrieveConfig('acronyms','confToHash'); 65b625487dSandi } 66b625487dSandi return $acronyms; 67b625487dSandi} 68b625487dSandi 69b625487dSandi/** 70b625487dSandi * returns a hash of smileys 71b625487dSandi * 72b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 73b625487dSandi */ 74b625487dSandifunction getSmileys() { 7549eb6e38SAndreas Gohr static $smileys = null; 76b625487dSandi if ( !$smileys ) { 77cb043f52SChris Smith $smileys = retrieveConfig('smileys','confToHash'); 78b625487dSandi } 79b625487dSandi return $smileys; 80b625487dSandi} 81b625487dSandi 82b625487dSandi/** 83b625487dSandi * returns a hash of entities 84b625487dSandi * 85b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 86b625487dSandi */ 87b625487dSandifunction getEntities() { 8849eb6e38SAndreas Gohr static $entities = null; 89b625487dSandi if ( !$entities ) { 90cb043f52SChris Smith $entities = retrieveConfig('entities','confToHash'); 91b625487dSandi } 92b625487dSandi return $entities; 93b625487dSandi} 94b625487dSandi 95b625487dSandi/** 96b625487dSandi * returns a hash of interwikilinks 97b625487dSandi * 98b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 99b625487dSandi */ 100b625487dSandifunction getInterwiki() { 10149eb6e38SAndreas Gohr static $wikis = null; 102b625487dSandi if ( !$wikis ) { 1034c6a5eccSAndreas Gohr $wikis = retrieveConfig('interwiki','confToHash',array(true)); 104b625487dSandi } 10597a3e4e3Sandi //add sepecial case 'this' 10627a2b085Sandi $wikis['this'] = DOKU_URL.'{NAME}'; 107b625487dSandi return $wikis; 108b625487dSandi} 109b625487dSandi 110b625487dSandi/** 111b9ac8716Schris * returns array of wordblock patterns 112b9ac8716Schris * 113b9ac8716Schris */ 114b9ac8716Schrisfunction getWordblocks() { 11549eb6e38SAndreas Gohr static $wordblocks = null; 116b9ac8716Schris if ( !$wordblocks ) { 117cb043f52SChris Smith $wordblocks = retrieveConfig('wordblock','file'); 118b9ac8716Schris } 119b9ac8716Schris return $wordblocks; 120b9ac8716Schris} 121b9ac8716Schris 122e3ab6fc5SMichael Hamann/** 123e3ab6fc5SMichael Hamann * Gets the list of configured schemes 124e3ab6fc5SMichael Hamann * 125e3ab6fc5SMichael Hamann * @return array the schemes 126e3ab6fc5SMichael Hamann */ 12736f2d7c1SGina Haeussgefunction getSchemes() { 12849eb6e38SAndreas Gohr static $schemes = null; 12936f2d7c1SGina Haeussge if ( !$schemes ) { 130cb043f52SChris Smith $schemes = retrieveConfig('scheme','file'); 13136f2d7c1SGina Haeussge } 13236f2d7c1SGina Haeussge $schemes = array_map('trim', $schemes); 13336f2d7c1SGina Haeussge $schemes = preg_replace('/^#.*/', '', $schemes); 13436f2d7c1SGina Haeussge $schemes = array_filter($schemes); 13536f2d7c1SGina Haeussge return $schemes; 13636f2d7c1SGina Haeussge} 13736f2d7c1SGina Haeussge 138b9ac8716Schris/** 139edcb01e5SGina Haeussge * Builds a hash from an array of lines 140b625487dSandi * 1413fd0b676Sandi * If $lower is set to true all hash keys are converted to 1423fd0b676Sandi * lower case. 1433fd0b676Sandi * 144b625487dSandi * @author Harry Fuecks <hfuecks@gmail.com> 1453fd0b676Sandi * @author Andreas Gohr <andi@splitbrain.org> 146edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net> 147b625487dSandi */ 148edcb01e5SGina Haeussgefunction linesToHash($lines, $lower=false) { 149e5fc893fSAndreas Gohr $conf = array(); 150dd74fecfSMichael Hamann // remove BOM 151dd74fecfSMichael Hamann if (isset($lines[0]) && substr($lines[0],0,3) == pack('CCC',0xef,0xbb,0xbf)) 152dd74fecfSMichael Hamann $lines[0] = substr($lines[0],3); 153b625487dSandi foreach ( $lines as $line ) { 15403ff8795SAndreas Gohr //ignore comments (except escaped ones) 15503ff8795SAndreas Gohr $line = preg_replace('/(?<![&\\\\])#.*$/','',$line); 15603ff8795SAndreas Gohr $line = str_replace('\\#','#',$line); 157b625487dSandi $line = trim($line); 158b625487dSandi if(empty($line)) continue; 159b625487dSandi $line = preg_split('/\s+/',$line,2); 160b625487dSandi // Build the associative array 16127a2b085Sandi if($lower){ 16227a2b085Sandi $conf[strtolower($line[0])] = $line[1]; 16327a2b085Sandi }else{ 164b625487dSandi $conf[$line[0]] = $line[1]; 165b625487dSandi } 16627a2b085Sandi } 167b625487dSandi 168b625487dSandi return $conf; 169b625487dSandi} 170b625487dSandi 171409d7af7SAndreas Gohr/** 172edcb01e5SGina Haeussge * Builds a hash from a configfile 173edcb01e5SGina Haeussge * 174edcb01e5SGina Haeussge * If $lower is set to true all hash keys are converted to 175edcb01e5SGina Haeussge * lower case. 176edcb01e5SGina Haeussge * 177edcb01e5SGina Haeussge * @author Harry Fuecks <hfuecks@gmail.com> 178edcb01e5SGina Haeussge * @author Andreas Gohr <andi@splitbrain.org> 179edcb01e5SGina Haeussge * @author Gina Haeussge <gina@foosel.net> 180edcb01e5SGina Haeussge */ 181edcb01e5SGina Haeussgefunction confToHash($file,$lower=false) { 182edcb01e5SGina Haeussge $conf = array(); 183edcb01e5SGina Haeussge $lines = @file( $file ); 184edcb01e5SGina Haeussge if ( !$lines ) return $conf; 185edcb01e5SGina Haeussge 186edcb01e5SGina Haeussge return linesToHash($lines, $lower); 187edcb01e5SGina Haeussge} 188edcb01e5SGina Haeussge 189edcb01e5SGina Haeussge/** 190cb043f52SChris Smith * Retrieve the requested configuration information 191cb043f52SChris Smith * 192cb043f52SChris Smith * @author Chris Smith <chris@jalakai.co.uk> 193cb043f52SChris Smith * 194cb043f52SChris Smith * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade 195cb043f52SChris Smith * @param callback $fn the function used to process the configuration file into an array 196e3ab6fc5SMichael Hamann * @param array $params optional additional params to pass to the callback 197cb043f52SChris Smith * @return array configuration values 198cb043f52SChris Smith */ 1994c6a5eccSAndreas Gohrfunction retrieveConfig($type,$fn,$params=null) { 200cb043f52SChris Smith global $config_cascade; 201cb043f52SChris Smith 2024c6a5eccSAndreas Gohr if(!is_array($params)) $params = array(); 2034c6a5eccSAndreas Gohr 204cb043f52SChris Smith $combined = array(); 205cb043f52SChris Smith if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING); 206b303b92cSChris Smith foreach (array('default','local','protected') as $config_group) { 207b303b92cSChris Smith if (empty($config_cascade[$type][$config_group])) continue; 208b303b92cSChris Smith foreach ($config_cascade[$type][$config_group] as $file) { 209cb043f52SChris Smith if (@file_exists($file)) { 2104c6a5eccSAndreas Gohr $config = call_user_func_array($fn,array_merge(array($file),$params)); 211cb043f52SChris Smith $combined = array_merge($combined, $config); 212cb043f52SChris Smith } 213cb043f52SChris Smith } 214b303b92cSChris Smith } 215cb043f52SChris Smith 216cb043f52SChris Smith return $combined; 217cb043f52SChris Smith} 218cb043f52SChris Smith 219cb043f52SChris Smith/** 220f8121585SChris Smith * Include the requested configuration information 221f8121585SChris Smith * 222f8121585SChris Smith * @author Chris Smith <chris@jalakai.co.uk> 223f8121585SChris Smith * 224f8121585SChris Smith * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade 225f8121585SChris Smith * @return array list of files, default before local before protected 226f8121585SChris Smith */ 227f8121585SChris Smithfunction getConfigFiles($type) { 228f8121585SChris Smith global $config_cascade; 229f8121585SChris Smith $files = array(); 230f8121585SChris Smith 231f8121585SChris Smith if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING); 232f8121585SChris Smith foreach (array('default','local','protected') as $config_group) { 233f8121585SChris Smith if (empty($config_cascade[$type][$config_group])) continue; 234f8121585SChris Smith $files = array_merge($files, $config_cascade[$type][$config_group]); 235f8121585SChris Smith } 236f8121585SChris Smith 237f8121585SChris Smith return $files; 238f8121585SChris Smith} 239f8121585SChris Smith 240f8121585SChris Smith/** 241409d7af7SAndreas Gohr * check if the given action was disabled in config 242409d7af7SAndreas Gohr * 243409d7af7SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2440e2431b7SGerrit Uitslag * @param string $action 245409d7af7SAndreas Gohr * @returns boolean true if enabled, false if disabled 246409d7af7SAndreas Gohr */ 247409d7af7SAndreas Gohrfunction actionOK($action){ 248409d7af7SAndreas Gohr static $disabled = null; 249020ea9e1SChristopher Smith if(is_null($disabled) || defined('SIMPLE_TEST')){ 250409d7af7SAndreas Gohr global $conf; 2510e2431b7SGerrit Uitslag /** @var DokuWiki_Auth_Plugin $auth */ 252de4d479aSAdrian Lang global $auth; 253409d7af7SAndreas Gohr 254409d7af7SAndreas Gohr // prepare disabled actions array and handle legacy options 255409d7af7SAndreas Gohr $disabled = explode(',',$conf['disableactions']); 256409d7af7SAndreas Gohr $disabled = array_map('trim',$disabled); 257e4eda66bSAndreas Gohr if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) { 258de4d479aSAdrian Lang $disabled[] = 'register'; 259de4d479aSAdrian Lang } 260e4eda66bSAndreas Gohr if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) { 261de4d479aSAdrian Lang $disabled[] = 'resendpwd'; 262de4d479aSAdrian Lang } 263e4eda66bSAndreas Gohr if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) { 2643a48618aSAnika Henke $disabled[] = 'subscribe'; 2653a48618aSAnika Henke } 2663a48618aSAnika Henke if (is_null($auth) || !$auth->canDo('Profile')) { 2673a48618aSAnika Henke $disabled[] = 'profile'; 2683a48618aSAnika Henke } 2692a7abf2dSChristopher Smith if (is_null($auth) || !$auth->canDo('delUser')) { 2702a7abf2dSChristopher Smith $disabled[] = 'profile_delete'; 2712a7abf2dSChristopher Smith } 2723a48618aSAnika Henke if (is_null($auth)) { 2733a48618aSAnika Henke $disabled[] = 'login'; 2743a48618aSAnika Henke } 2753a48618aSAnika Henke if (is_null($auth) || !$auth->canDo('logout')) { 2763a48618aSAnika Henke $disabled[] = 'logout'; 2773a48618aSAnika Henke } 278409d7af7SAndreas Gohr $disabled = array_unique($disabled); 279409d7af7SAndreas Gohr } 280409d7af7SAndreas Gohr 281409d7af7SAndreas Gohr return !in_array($action,$disabled); 282409d7af7SAndreas Gohr} 283409d7af7SAndreas Gohr 284fe9ec250SChris Smith/** 285fe9ec250SChris Smith * check if headings should be used as link text for the specified link type 286fe9ec250SChris Smith * 287fe9ec250SChris Smith * @author Chris Smith <chris@jalakai.co.uk> 288fe9ec250SChris Smith * 289fe9ec250SChris Smith * @param string $linktype 'content'|'navigation', content applies to links in wiki text 290fe9ec250SChris Smith * navigation applies to all other links 291e3ab6fc5SMichael Hamann * @return boolean true if headings should be used for $linktype, false otherwise 292fe9ec250SChris Smith */ 293fe9ec250SChris Smithfunction useHeading($linktype) { 294fe9ec250SChris Smith static $useHeading = null; 295fe9ec250SChris Smith 296fe9ec250SChris Smith if (is_null($useHeading)) { 297fe9ec250SChris Smith global $conf; 298fe9ec250SChris Smith 299fe9ec250SChris Smith if (!empty($conf['useheading'])) { 300fe9ec250SChris Smith switch ($conf['useheading']) { 30149eb6e38SAndreas Gohr case 'content': 30249eb6e38SAndreas Gohr $useHeading['content'] = true; 30349eb6e38SAndreas Gohr break; 30449eb6e38SAndreas Gohr 30549eb6e38SAndreas Gohr case 'navigation': 30649eb6e38SAndreas Gohr $useHeading['navigation'] = true; 30749eb6e38SAndreas Gohr break; 308fe9ec250SChris Smith default: 309fe9ec250SChris Smith $useHeading['content'] = true; 310fe9ec250SChris Smith $useHeading['navigation'] = true; 311fe9ec250SChris Smith } 312fe9ec250SChris Smith } else { 313fe9ec250SChris Smith $useHeading = array(); 314fe9ec250SChris Smith } 315fe9ec250SChris Smith } 316fe9ec250SChris Smith 317fe9ec250SChris Smith return (!empty($useHeading[$linktype])); 318fe9ec250SChris Smith} 319fe9ec250SChris Smith 3203994772aSChris Smith/** 3213994772aSChris Smith * obscure config data so information isn't plain text 3223994772aSChris Smith * 3233994772aSChris Smith * @param string $str data to be encoded 3243994772aSChris Smith * @param string $code encoding method, values: plain, base64, uuencode. 3253994772aSChris Smith * @return string the encoded value 3263994772aSChris Smith */ 3273994772aSChris Smithfunction conf_encodeString($str,$code) { 3283994772aSChris Smith switch ($code) { 3293994772aSChris Smith case 'base64' : return '<b>'.base64_encode($str); 3303994772aSChris Smith case 'uuencode' : return '<u>'.convert_uuencode($str); 3313994772aSChris Smith case 'plain': 3323994772aSChris Smith default: 3333994772aSChris Smith return $str; 3343994772aSChris Smith } 3353994772aSChris Smith} 3363994772aSChris Smith/** 3373994772aSChris Smith * return obscured data as plain text 3383994772aSChris Smith * 3393994772aSChris Smith * @param string $str encoded data 3403994772aSChris Smith * @return string plain text 3413994772aSChris Smith */ 3423994772aSChris Smithfunction conf_decodeString($str) { 3433994772aSChris Smith switch (substr($str,0,3)) { 3443994772aSChris Smith case '<b>' : return base64_decode(substr($str,3)); 3453994772aSChris Smith case '<u>' : return convert_uudecode(substr($str,3)); 3463994772aSChris Smith default: // not encode (or unknown) 3473994772aSChris Smith return $str; 3483994772aSChris Smith } 3493994772aSChris Smith} 350e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 : 351