add icons to code block downloads
[dokuwiki/radio.git] / inc / confutils.php
blobaab0be4bef1ff5fcab26c75c7ee9b22ee006947a
1 <?php
2 /**
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>
7 */
10 /**
11 * Returns the (known) extension and mimetype of a given filename
13 * @author Andreas Gohr <andi@splitbrain.org>
15 function mimetype($file){
16 $ret = array(false,false,false); // return array
17 $mtypes = getMimeTypes(); // known mimetypes
18 $exts = join('|',array_keys($mtypes)); // known extensions (regexp)
19 if(preg_match('#\.('.$exts.')$#i',$file,$matches)){
20 $ext = strtolower($matches[1]);
23 if($ext && $mtypes[$ext]){
24 if($mtypes[$ext][0] == '!'){
25 $ret = array($ext, substr($mtypes[$ext],1), true);
26 }else{
27 $ret = array($ext, $mtypes[$ext], false);
31 return $ret;
34 /**
35 * returns a hash of mimetypes
37 * @author Andreas Gohr <andi@splitbrain.org>
39 function getMimeTypes() {
40 static $mime = NULL;
41 if ( !$mime ) {
42 $mime = retrieveConfig('mime','confToHash');
44 return $mime;
47 /**
48 * returns a hash of acronyms
50 * @author Harry Fuecks <hfuecks@gmail.com>
52 function getAcronyms() {
53 static $acronyms = NULL;
54 if ( !$acronyms ) {
55 $acronyms = retrieveConfig('acronyms','confToHash');
57 return $acronyms;
60 /**
61 * returns a hash of smileys
63 * @author Harry Fuecks <hfuecks@gmail.com>
65 function getSmileys() {
66 static $smileys = NULL;
67 if ( !$smileys ) {
68 $smileys = retrieveConfig('smileys','confToHash');
70 return $smileys;
73 /**
74 * returns a hash of entities
76 * @author Harry Fuecks <hfuecks@gmail.com>
78 function getEntities() {
79 static $entities = NULL;
80 if ( !$entities ) {
81 $entities = retrieveConfig('entities','confToHash');
83 return $entities;
86 /**
87 * returns a hash of interwikilinks
89 * @author Harry Fuecks <hfuecks@gmail.com>
91 function getInterwiki() {
92 static $wikis = NULL;
93 if ( !$wikis ) {
94 $wikis = retrieveConfig('interwiki','confToHash',array(true));
96 //add sepecial case 'this'
97 $wikis['this'] = DOKU_URL.'{NAME}';
98 return $wikis;
102 * returns array of wordblock patterns
105 function getWordblocks() {
106 static $wordblocks = NULL;
107 if ( !$wordblocks ) {
108 $wordblocks = retrieveConfig('wordblock','file');
110 return $wordblocks;
114 function getSchemes() {
115 static $schemes = NULL;
116 if ( !$schemes ) {
117 $schemes = retrieveConfig('scheme','file');
119 $schemes = array_map('trim', $schemes);
120 $schemes = preg_replace('/^#.*/', '', $schemes);
121 $schemes = array_filter($schemes);
122 return $schemes;
126 * Builds a hash from an array of lines
128 * If $lower is set to true all hash keys are converted to
129 * lower case.
131 * @author Harry Fuecks <hfuecks@gmail.com>
132 * @author Andreas Gohr <andi@splitbrain.org>
133 * @author Gina Haeussge <gina@foosel.net>
135 function linesToHash($lines, $lower=false) {
136 foreach ( $lines as $line ) {
137 //ignore comments (except escaped ones)
138 $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
139 $line = str_replace('\\#','#',$line);
140 $line = trim($line);
141 if(empty($line)) continue;
142 $line = preg_split('/\s+/',$line,2);
143 // Build the associative array
144 if($lower){
145 $conf[strtolower($line[0])] = $line[1];
146 }else{
147 $conf[$line[0]] = $line[1];
151 return $conf;
155 * Builds a hash from a configfile
157 * If $lower is set to true all hash keys are converted to
158 * lower case.
160 * @author Harry Fuecks <hfuecks@gmail.com>
161 * @author Andreas Gohr <andi@splitbrain.org>
162 * @author Gina Haeussge <gina@foosel.net>
164 function confToHash($file,$lower=false) {
165 $conf = array();
166 $lines = @file( $file );
167 if ( !$lines ) return $conf;
169 return linesToHash($lines, $lower);
173 * Retrieve the requested configuration information
175 * @author Chris Smith <chris@jalakai.co.uk>
177 * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade
178 * @param callback $fn the function used to process the configuration file into an array
179 * @param array $param optional additional params to pass to the callback
180 * @return array configuration values
182 function retrieveConfig($type,$fn,$params=null) {
183 global $config_cascade;
185 if(!is_array($params)) $params = array();
187 $combined = array();
188 if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
189 foreach (array('default','local','protected') as $config_group) {
190 if (empty($config_cascade[$type][$config_group])) continue;
191 foreach ($config_cascade[$type][$config_group] as $file) {
192 if (@file_exists($file)) {
193 $config = call_user_func_array($fn,array_merge(array($file),$params));
194 $combined = array_merge($combined, $config);
199 return $combined;
203 * Include the requested configuration information
205 * @author Chris Smith <chris@jalakai.co.uk>
207 * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade
208 * @return array list of files, default before local before protected
210 function getConfigFiles($type) {
211 global $config_cascade;
212 $files = array();
214 if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
215 foreach (array('default','local','protected') as $config_group) {
216 if (empty($config_cascade[$type][$config_group])) continue;
217 $files = array_merge($files, $config_cascade[$type][$config_group]);
220 return $files;
224 * check if the given action was disabled in config
226 * @author Andreas Gohr <andi@splitbrain.org>
227 * @returns boolean true if enabled, false if disabled
229 function actionOK($action){
230 static $disabled = null;
231 if(is_null($disabled)){
232 global $conf;
234 // prepare disabled actions array and handle legacy options
235 $disabled = explode(',',$conf['disableactions']);
236 $disabled = array_map('trim',$disabled);
237 if(isset($conf['openregister']) && !$conf['openregister']) $disabled[] = 'register';
238 if(isset($conf['resendpasswd']) && !$conf['resendpasswd']) $disabled[] = 'resendpwd';
239 if(isset($conf['subscribers']) && !$conf['subscribers']) {
240 $disabled[] = 'subscribe';
241 $disabled[] = 'subscribens';
243 $disabled = array_unique($disabled);
246 return !in_array($action,$disabled);
250 * check if headings should be used as link text for the specified link type
252 * @author Chris Smith <chris@jalakai.co.uk>
254 * @param string $linktype 'content'|'navigation', content applies to links in wiki text
255 * navigation applies to all other links
256 * @returns boolean true if headings should be used for $linktype, false otherwise
258 function useHeading($linktype) {
259 static $useHeading = null;
261 if (is_null($useHeading)) {
262 global $conf;
264 if (!empty($conf['useheading'])) {
265 switch ($conf['useheading']) {
266 case 'content' : $useHeading['content'] = true; break;
267 case 'navigation' : $useHeading['navigation'] = true; break;
268 default:
269 $useHeading['content'] = true;
270 $useHeading['navigation'] = true;
272 } else {
273 $useHeading = array();
277 return (!empty($useHeading[$linktype]));
281 * obscure config data so information isn't plain text
283 * @param string $str data to be encoded
284 * @param string $code encoding method, values: plain, base64, uuencode.
285 * @return string the encoded value
287 function conf_encodeString($str,$code) {
288 switch ($code) {
289 case 'base64' : return '<b>'.base64_encode($str);
290 case 'uuencode' : return '<u>'.convert_uuencode($str);
291 case 'plain':
292 default:
293 return $str;
297 * return obscured data as plain text
299 * @param string $str encoded data
300 * @return string plain text
302 function conf_decodeString($str) {
303 switch (substr($str,0,3)) {
304 case '<b>' : return base64_decode(substr($str,3));
305 case '<u>' : return convert_uudecode(substr($str,3));
306 default: // not encode (or unknown)
307 return $str;
310 //Setup VIM: ex: et ts=2 enc=utf-8 :