Completed
Push — jqsplit ( 7a5ae0...fa0786 )
by Andreas
04:36
created

confutils.php ➔ getCdnUrls()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 38
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 29
nc 10
nop 0
dl 0
loc 38
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Utilities for collecting data from config files
4
 *
5
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6
 * @author     Harry Fuecks <[email protected]>
7
 */
8
9
/*
10
 * line prefix used to negate single value config items
11
 * (scheme.conf & stopwords.conf), e.g.
12
 * !gopher
13
 */
14
const DOKU_CONF_NEGATION = '!';
15
16
/**
17
 * Returns the (known) extension and mimetype of a given filename
18
 *
19
 * If $knownonly is true (the default), then only known extensions
20
 * are returned.
21
 *
22
 * @author Andreas Gohr <[email protected]>
23
 *
24
 * @param string $file file name
25
 * @param bool   $knownonly
26
 * @return array with extension, mimetype and if it should be downloaded
27
 */
28
function mimetype($file, $knownonly=true){
29
    $mtypes = getMimeTypes();     // known mimetypes
30
    $ext    = strrpos($file, '.');
31
    if ($ext === false) {
32
        return array(false, false, false);
33
    }
34
    $ext = strtolower(substr($file, $ext + 1));
35
    if (!isset($mtypes[$ext])){
36
        if ($knownonly) {
37
            return array(false, false, false);
38
        } else {
39
            return array($ext, 'application/octet-stream', true);
40
        }
41
    }
42
    if($mtypes[$ext][0] == '!'){
43
        return array($ext, substr($mtypes[$ext],1), true);
44
    }else{
45
        return array($ext, $mtypes[$ext], false);
46
    }
47
}
48
49
/**
50
 * returns a hash of mimetypes
51
 *
52
 * @author Andreas Gohr <[email protected]>
53
 */
54
function getMimeTypes() {
55
    static $mime = null;
56
    if ( !$mime ) {
57
        $mime = retrieveConfig('mime','confToHash');
58
        $mime = array_filter($mime);
59
    }
60
    return $mime;
61
}
62
63
/**
64
 * returns a hash of acronyms
65
 *
66
 * @author Harry Fuecks <[email protected]>
67
 */
68
function getAcronyms() {
69
    static $acronyms = null;
70
    if ( !$acronyms ) {
71
        $acronyms = retrieveConfig('acronyms','confToHash');
72
        $acronyms = array_filter($acronyms, 'strlen');
73
    }
74
    return $acronyms;
75
}
76
77
/**
78
 * returns a hash of smileys
79
 *
80
 * @author Harry Fuecks <[email protected]>
81
 */
82
function getSmileys() {
83
    static $smileys = null;
84
    if ( !$smileys ) {
85
        $smileys = retrieveConfig('smileys','confToHash');
86
        $smileys = array_filter($smileys, 'strlen');
87
    }
88
    return $smileys;
89
}
90
91
/**
92
 * returns a hash of entities
93
 *
94
 * @author Harry Fuecks <[email protected]>
95
 */
96
function getEntities() {
97
    static $entities = null;
98
    if ( !$entities ) {
99
        $entities = retrieveConfig('entities','confToHash');
100
        $entities = array_filter($entities, 'strlen');
101
    }
102
    return $entities;
103
}
104
105
/**
106
 * returns a hash of interwikilinks
107
 *
108
 * @author Harry Fuecks <[email protected]>
109
 */
110
function getInterwiki() {
111
    static $wikis = null;
112
    if ( !$wikis ) {
113
        $wikis = retrieveConfig('interwiki','confToHash',array(true));
114
        $wikis = array_filter($wikis, 'strlen');
115
116
        //add sepecial case 'this'
117
        $wikis['this'] = DOKU_URL.'{NAME}';
118
    }
119
    return $wikis;
120
}
121
122
/**
123
 * Returns the jquery script URLs for the versions defined in lib/scripts/jquery/versions
124
 *
125
 * @trigger CONFUTIL_CDN_SELECT
126
 * @return array
127
 */
128
function getCdnUrls() {
129
    global $conf;
130
131
    // load version info
132
    $versions = array();
133
    $lines = file(DOKU_INC . 'lib/scripts/jquery/versions');
134
    foreach($lines as $line) {
135
        $line = preg_replace('/#.*$/', '', $line);
136
        list($key, $val) = explode('=', $line, 2);
137
        $key = trim($key);
138
        $val = trim($val);
139
        $versions[$key] = $val;
140
    }
141
142
    $src = array();
143
    $data = array(
144
        'versions' => $versions,
145
        'src' => &$src
146
    );
147
    $event = new Doku_Event('CONFUTIL_CDN_SELECT', $data);
148
    if($event->advise_before()) {
149
        if(!$conf['jquerycdn']) {
150
            $jqmod = md5(join('-', $versions));
151
            $src[] = DOKU_BASE . 'lib/exe/jquery.php' . '?tseed=' . $jqmod;
152
        } elseif($conf['jquerycdn'] == 'jquery') {
153
            $src[] = sprintf('https://code.jquery.com/jquery-%s.min.js', $versions['JQ_VERSION']);
154
            $src[] = sprintf('https://code.jquery.com/jquery-migrate-%s.min.js', $versions['JQM_VERSION']);
155
            $src[] = sprintf('https://code.jquery.com/ui/%s/jquery-ui.min.js', $versions['JQUI_VERSION']);
156
        } elseif($conf['jquerycdn'] == 'cdnjs') {
157
            $src[] = sprintf('https://cdnjs.cloudflare.com/ajax/libs/jquery/%s/jquery.min.js', $versions['JQ_VERSION']);
158
            $src[] = sprintf('https://cdnjs.cloudflare.com/ajax/libs/jquery-migrate/%s/jquery-migrate.min.js', $versions['JQM_VERSION']);
159
            $src[] = sprintf('https://cdnjs.cloudflare.com/ajax/libs/jqueryui/%s/jquery-ui.min.js', $versions['JQUI_VERSION']);
160
        }
161
    }
162
    $event->advise_after();
163
164
    return $src;
165
}
166
167
/**
168
 * returns array of wordblock patterns
169
 *
170
 */
171
function getWordblocks() {
172
    static $wordblocks = null;
173
    if ( !$wordblocks ) {
174
        $wordblocks = retrieveConfig('wordblock','file',null,'array_merge_with_removal');
175
    }
176
    return $wordblocks;
177
}
178
179
/**
180
 * Gets the list of configured schemes
181
 *
182
 * @return array the schemes
183
 */
184
function getSchemes() {
185
    static $schemes = null;
186
    if ( !$schemes ) {
187
        $schemes = retrieveConfig('scheme','file',null,'array_merge_with_removal');
188
        $schemes = array_map('trim', $schemes);
189
        $schemes = preg_replace('/^#.*/', '', $schemes);
190
        $schemes = array_filter($schemes);
191
    }
192
    return $schemes;
193
}
194
195
/**
196
 * Builds a hash from an array of lines
197
 *
198
 * If $lower is set to true all hash keys are converted to
199
 * lower case.
200
 *
201
 * @author Harry Fuecks <[email protected]>
202
 * @author Andreas Gohr <[email protected]>
203
 * @author Gina Haeussge <[email protected]>
204
 */
205
function linesToHash($lines, $lower=false) {
206
    $conf = array();
207
    // remove BOM
208
    if (isset($lines[0]) && substr($lines[0],0,3) == pack('CCC',0xef,0xbb,0xbf))
209
        $lines[0] = substr($lines[0],3);
210
    foreach ( $lines as $line ) {
211
        //ignore comments (except escaped ones)
212
        $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
213
        $line = str_replace('\\#','#',$line);
214
        $line = trim($line);
215
        if(empty($line)) continue;
216
        $line = preg_split('/\s+/',$line,2);
217
        // Build the associative array
218
        if($lower){
219
            $conf[strtolower($line[0])] = $line[1];
220
        }else{
221
            $conf[$line[0]] = $line[1];
222
        }
223
    }
224
225
    return $conf;
226
}
227
228
/**
229
 * Builds a hash from a configfile
230
 *
231
 * If $lower is set to true all hash keys are converted to
232
 * lower case.
233
 *
234
 * @author Harry Fuecks <[email protected]>
235
 * @author Andreas Gohr <[email protected]>
236
 * @author Gina Haeussge <[email protected]>
237
 */
238
function confToHash($file,$lower=false) {
239
    $conf = array();
240
    $lines = @file( $file );
241
    if ( !$lines ) return $conf;
242
243
    return linesToHash($lines, $lower);
244
}
245
246
/**
247
 * Retrieve the requested configuration information
248
 *
249
 * @author Chris Smith <[email protected]>
250
 *
251
 * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
252
 * @param  callback $fn       the function used to process the configuration file into an array
253
 * @param  array    $params   optional additional params to pass to the callback
254
 * @param  callback $combine  the function used to combine arrays of values read from different configuration files;
255
 *                            the function takes two parameters,
256
 *                               $combined - the already read & merged configuration values
257
 *                               $new - array of config values from the config cascade file being currently processed
258
 *                            and returns an array of the merged configuration values.
259
 * @return array    configuration values
260
 */
261
function retrieveConfig($type,$fn,$params=null,$combine='array_merge') {
262
    global $config_cascade;
263
264
    if(!is_array($params)) $params = array();
265
266
    $combined = array();
267
    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
268
    foreach (array('default','local','protected') as $config_group) {
269
        if (empty($config_cascade[$type][$config_group])) continue;
270
        foreach ($config_cascade[$type][$config_group] as $file) {
271
            if (file_exists($file)) {
272
                $config = call_user_func_array($fn,array_merge(array($file),$params));
273
                $combined = $combine($combined, $config);
274
            }
275
        }
276
    }
277
278
    return $combined;
279
}
280
281
/**
282
 * Include the requested configuration information
283
 *
284
 * @author Chris Smith <[email protected]>
285
 *
286
 * @param  string   $type     the configuration settings to be read, must correspond to a key/array in $config_cascade
287
 * @return array              list of files, default before local before protected
288
 */
289
function getConfigFiles($type) {
290
    global $config_cascade;
291
    $files = array();
292
293
    if (!is_array($config_cascade[$type])) trigger_error('Missing config cascade for "'.$type.'"',E_USER_WARNING);
294
    foreach (array('default','local','protected') as $config_group) {
295
        if (empty($config_cascade[$type][$config_group])) continue;
296
        $files = array_merge($files, $config_cascade[$type][$config_group]);
297
    }
298
299
    return $files;
300
}
301
302
/**
303
 * check if the given action was disabled in config
304
 *
305
 * @author Andreas Gohr <[email protected]>
306
 * @param string $action
307
 * @returns boolean true if enabled, false if disabled
308
 */
309
function actionOK($action){
310
    static $disabled = null;
311
    if(is_null($disabled) || defined('SIMPLE_TEST')){
312
        global $conf;
313
        /** @var DokuWiki_Auth_Plugin $auth */
314
        global $auth;
315
316
        // prepare disabled actions array and handle legacy options
317
        $disabled = explode(',',$conf['disableactions']);
318
        $disabled = array_map('trim',$disabled);
319
        if((isset($conf['openregister']) && !$conf['openregister']) || is_null($auth) || !$auth->canDo('addUser')) {
320
            $disabled[] = 'register';
321
        }
322
        if((isset($conf['resendpasswd']) && !$conf['resendpasswd']) || is_null($auth) || !$auth->canDo('modPass')) {
323
            $disabled[] = 'resendpwd';
324
        }
325
        if((isset($conf['subscribers']) && !$conf['subscribers']) || is_null($auth)) {
326
            $disabled[] = 'subscribe';
327
        }
328
        if (is_null($auth) || !$auth->canDo('Profile')) {
329
            $disabled[] = 'profile';
330
        }
331
        if (is_null($auth) || !$auth->canDo('delUser')) {
332
            $disabled[] = 'profile_delete';
333
        }
334
        if (is_null($auth)) {
335
            $disabled[] = 'login';
336
        }
337
        if (is_null($auth) || !$auth->canDo('logout')) {
338
            $disabled[] = 'logout';
339
        }
340
        $disabled = array_unique($disabled);
341
    }
342
343
    return !in_array($action,$disabled);
344
}
345
346
/**
347
 * check if headings should be used as link text for the specified link type
348
 *
349
 * @author Chris Smith <[email protected]>
350
 *
351
 * @param   string  $linktype   'content'|'navigation', content applies to links in wiki text
352
 *                                                      navigation applies to all other links
353
 * @return  boolean             true if headings should be used for $linktype, false otherwise
354
 */
355
function useHeading($linktype) {
356
    static $useHeading = null;
357
358
    if (is_null($useHeading)) {
359
        global $conf;
360
361
        if (!empty($conf['useheading'])) {
362
            switch ($conf['useheading']) {
363
                case 'content':
364
                    $useHeading['content'] = true;
365
                    break;
366
367
                case 'navigation':
368
                    $useHeading['navigation'] = true;
369
                    break;
370
                default:
371
                    $useHeading['content'] = true;
372
                    $useHeading['navigation'] = true;
373
            }
374
        } else {
375
            $useHeading = array();
376
        }
377
    }
378
379
    return (!empty($useHeading[$linktype]));
380
}
381
382
/**
383
 * obscure config data so information isn't plain text
384
 *
385
 * @param string       $str     data to be encoded
386
 * @param string       $code    encoding method, values: plain, base64, uuencode.
387
 * @return string               the encoded value
388
 */
389
function conf_encodeString($str,$code) {
390
    switch ($code) {
391
        case 'base64'   : return '<b>'.base64_encode($str);
392
        case 'uuencode' : return '<u>'.convert_uuencode($str);
393
        case 'plain':
394
        default:
395
                          return $str;
396
    }
397
}
398
/**
399
 * return obscured data as plain text
400
 *
401
 * @param  string      $str   encoded data
402
 * @return string             plain text
403
 */
404
function conf_decodeString($str) {
405
    switch (substr($str,0,3)) {
406
        case '<b>' : return base64_decode(substr($str,3));
407
        case '<u>' : return convert_uudecode(substr($str,3));
408
        default:  // not encoded (or unknown)
409
                     return $str;
410
    }
411
}
412
413
/**
414
 * array combination function to remove negated values (prefixed by !)
415
 *
416
 * @param  array $current
417
 * @param  array $new
418
 *
419
 * @return array the combined array, numeric keys reset
420
 */
421
function array_merge_with_removal($current, $new) {
422
    foreach ($new as $val) {
423
        if (substr($val,0,1) == DOKU_CONF_NEGATION) {
424
            $idx = array_search(trim(substr($val,1)),$current);
425
            if ($idx !== false) {
426
                unset($current[$idx]);
427
            }
428
        } else {
429
            $current[] = trim($val);
430
        }
431
    }
432
433
    return array_slice($current,0);
434
}
435
//Setup VIM: ex: et ts=4 :
436