3 * Utilities for collecting data from config files
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Harry Fuecks <hfuecks@gmail.com>
11 * Returns the (known) extension and mimetype of a given filename
13 * If $knownonly is true (the default), then only known extensions
16 * @author Andreas Gohr <andi@splitbrain.org>
18 function mimetype($file, $knownonly=true){
19 $mtypes = getMimeTypes(); // known mimetypes
20 $ext = strrpos($file, '.');
22 return array(false, false, false);
24 $ext = strtolower(substr($file, $ext + 1));
25 if (!isset($mtypes[$ext])){
27 return array(false, false, false);
29 return array($ext, 'application/octet-stream', true);
32 if($mtypes[$ext][0] == '!'){
33 return array($ext, substr($mtypes[$ext],1), true);
35 return array($ext, $mtypes[$ext], false);
40 * returns a hash of mimetypes
42 * @author Andreas Gohr <andi@splitbrain.org>
44 function getMimeTypes() {
47 $mime = retrieveConfig('mime','confToHash');
53 * returns a hash of acronyms
55 * @author Harry Fuecks <hfuecks@gmail.com>
57 function getAcronyms() {
58 static $acronyms = null;
60 $acronyms = retrieveConfig('acronyms','confToHash');
66 * returns a hash of smileys
68 * @author Harry Fuecks <hfuecks@gmail.com>
70 function getSmileys() {
71 static $smileys = null;
73 $smileys = retrieveConfig('smileys','confToHash');
79 * returns a hash of entities
81 * @author Harry Fuecks <hfuecks@gmail.com>
83 function getEntities() {
84 static $entities = null;
86 $entities = retrieveConfig('entities','confToHash');
92 * returns a hash of interwikilinks
94 * @author Harry Fuecks <hfuecks@gmail.com>
96 function getInterwiki() {
99 $wikis = retrieveConfig('interwiki','confToHash',array(true));
101 //add sepecial case 'this'
102 $wikis['this'] = DOKU_URL.'{NAME}';
107 * returns array of wordblock patterns
110 function getWordblocks() {
111 static $wordblocks = null;
112 if ( !$wordblocks ) {
113 $wordblocks = retrieveConfig('wordblock','file');
119 function getSchemes() {
120 static $schemes = null;
122 $schemes = retrieveConfig('scheme','file');
124 $schemes = array_map('trim', $schemes);
125 $schemes = preg_replace('/^#.*/', '', $schemes);
126 $schemes = array_filter($schemes);
131 * Builds a hash from an array of lines
133 * If $lower is set to true all hash keys are converted to
136 * @author Harry Fuecks <hfuecks@gmail.com>
137 * @author Andreas Gohr <andi@splitbrain.org>
138 * @author Gina Haeussge <gina@foosel.net>
140 function linesToHash($lines, $lower=false) {
142 foreach ( $lines as $line ) {
143 //ignore comments (except escaped ones)
144 $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
145 $line = str_replace('\\#','#',$line);
147 if(empty($line)) continue;
148 $line = preg_split('/\s+/',$line,2);
149 // Build the associative array
151 $conf[strtolower($line[0])] = $line[1];
153 $conf[$line[0]] = $line[1];
161 * Builds a hash from a configfile
163 * If $lower is set to true all hash keys are converted to
166 * @author Harry Fuecks <hfuecks@gmail.com>
167 * @author Andreas Gohr <andi@splitbrain.org>
168 * @author Gina Haeussge <gina@foosel.net>
170 function confToHash($file,$lower=false) {
172 $lines = @file( $file );
173 if ( !$lines ) return $conf;
175 return linesToHash($lines, $lower);
179 * Retrieve the requested configuration information
181 * @author Chris Smith <chris@jalakai.co.uk>
183 * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade
184 * @param callback $fn the function used to process the configuration file into an array
185 * @param array $param optional additional params to pass to the callback
186 * @return array configuration values
188 function retrieveConfig($type,$fn,$params=null) {
189 global $config_cascade;
191 if(!is_array($params)) $params = array();
194 if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
195 foreach (array('default','local','protected') as $config_group) {
196 if (empty($config_cascade[$type][$config_group])) continue;
197 foreach ($config_cascade[$type][$config_group] as $file) {
198 if (@file_exists($file)) {
199 $config = call_user_func_array($fn,array_merge(array($file),$params));
200 $combined = array_merge($combined, $config);
209 * Include the requested configuration information
211 * @author Chris Smith <chris@jalakai.co.uk>
213 * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade
214 * @return array list of files, default before local before protected
216 function getConfigFiles($type) {
217 global $config_cascade;
220 if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
221 foreach (array('default','local','protected') as $config_group) {
222 if (empty($config_cascade[$type][$config_group])) continue;
223 $files = array_merge($files, $config_cascade[$type][$config_group]);
230 * check if the given action was disabled in config
232 * @author Andreas Gohr <andi@splitbrain.org>
233 * @returns boolean true if enabled, false if disabled
235 function actionOK($action){
236 static $disabled = null;
237 if(is_null($disabled)){
241 // prepare disabled actions array and handle legacy options
242 $disabled = explode(',',$conf['disableactions']);
243 $disabled = array_map('trim',$disabled);
244 if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) {
245 $disabled[] = 'register';
247 if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) {
248 $disabled[] = 'resendpwd';
250 if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) {
251 $disabled[] = 'subscribe';
253 if (is_null($auth) || !$auth->canDo('Profile')) {
254 $disabled[] = 'profile';
256 if (is_null($auth)) {
257 $disabled[] = 'login';
259 if (is_null($auth) || !$auth->canDo('logout')) {
260 $disabled[] = 'logout';
262 $disabled = array_unique($disabled);
265 return !in_array($action,$disabled);
269 * check if headings should be used as link text for the specified link type
271 * @author Chris Smith <chris@jalakai.co.uk>
273 * @param string $linktype 'content'|'navigation', content applies to links in wiki text
274 * navigation applies to all other links
275 * @returns boolean true if headings should be used for $linktype, false otherwise
277 function useHeading($linktype) {
278 static $useHeading = null;
280 if (is_null($useHeading)) {
283 if (!empty($conf['useheading'])) {
284 switch ($conf['useheading']) {
286 $useHeading['content'] = true;
290 $useHeading['navigation'] = true;
293 $useHeading['content'] = true;
294 $useHeading['navigation'] = true;
297 $useHeading = array();
301 return (!empty($useHeading[$linktype]));
305 * obscure config data so information isn't plain text
307 * @param string $str data to be encoded
308 * @param string $code encoding method, values: plain, base64, uuencode.
309 * @return string the encoded value
311 function conf_encodeString($str,$code) {
313 case 'base64' : return '<b>'.base64_encode($str);
314 case 'uuencode' : return '<u>'.convert_uuencode($str);
321 * return obscured data as plain text
323 * @param string $str encoded data
324 * @return string plain text
326 function conf_decodeString($str) {
327 switch (substr($str,0,3)) {
328 case '<b>' : return base64_decode(substr($str,3));
329 case '<u>' : return convert_uudecode(substr($str,3));
330 default: // not encode (or unknown)
334 //Setup VIM: ex: et ts=4 :