Completed
Pull Request — master (#2413)
by Michael
17:27 queued 12:53
created

common.php ➔ saveDraft()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 5
nop 0
dl 0
loc 34
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Common DokuWiki functions
4
 *
5
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6
 * @author     Andreas Gohr <[email protected]>
7
 */
8
9
if(!defined('DOKU_INC')) die('meh.');
10
11
/**
12
 * These constants are used with the recents function
13
 */
14
define('RECENTS_SKIP_DELETED', 2);
15
define('RECENTS_SKIP_MINORS', 4);
16
define('RECENTS_SKIP_SUBSPACES', 8);
17
define('RECENTS_MEDIA_CHANGES', 16);
18
define('RECENTS_MEDIA_PAGES_MIXED', 32);
19
20
/**
21
 * Wrapper around htmlspecialchars()
22
 *
23
 * @author Andreas Gohr <[email protected]>
24
 * @see    htmlspecialchars()
25
 *
26
 * @param string $string the string being converted
27
 * @return string converted string
28
 */
29
function hsc($string) {
30
    return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
31
}
32
33
/**
34
 * Checks if the given input is blank
35
 *
36
 * This is similar to empty() but will return false for "0".
37
 *
38
 * Please note: when you pass uninitialized variables, they will implicitly be created
39
 * with a NULL value without warning.
40
 *
41
 * To avoid this it's recommended to guard the call with isset like this:
42
 *
43
 * (isset($foo) && !blank($foo))
44
 * (!isset($foo) || blank($foo))
45
 *
46
 * @param $in
47
 * @param bool $trim Consider a string of whitespace to be blank
48
 * @return bool
49
 */
50
function blank(&$in, $trim = false) {
51
    if(is_null($in)) return true;
52
    if(is_array($in)) return empty($in);
53
    if($in === "\0") return true;
54
    if($trim && trim($in) === '') return true;
55
    if(strlen($in) > 0) return false;
56
    return empty($in);
57
}
58
59
/**
60
 * print a newline terminated string
61
 *
62
 * You can give an indention as optional parameter
63
 *
64
 * @author Andreas Gohr <[email protected]>
65
 *
66
 * @param string $string  line of text
67
 * @param int    $indent  number of spaces indention
68
 */
69
function ptln($string, $indent = 0) {
70
    echo str_repeat(' ', $indent)."$string\n";
71
}
72
73
/**
74
 * strips control characters (<32) from the given string
75
 *
76
 * @author Andreas Gohr <[email protected]>
77
 *
78
 * @param string $string being stripped
79
 * @return string
80
 */
81
function stripctl($string) {
82
    return preg_replace('/[\x00-\x1F]+/s', '', $string);
83
}
84
85
/**
86
 * Return a secret token to be used for CSRF attack prevention
87
 *
88
 * @author  Andreas Gohr <[email protected]>
89
 * @link    http://en.wikipedia.org/wiki/Cross-site_request_forgery
90
 * @link    http://christ1an.blogspot.com/2007/04/preventing-csrf-efficiently.html
91
 *
92
 * @return  string
93
 */
94
function getSecurityToken() {
95
    /** @var Input $INPUT */
96
    global $INPUT;
97
98
    $user = $INPUT->server->str('REMOTE_USER');
99
    $session = session_id();
100
101
    // CSRF checks are only for logged in users - do not generate for anonymous
102
    if(trim($user) == '' || trim($session) == '') return '';
103
    return PassHash::hmac('md5', $session.$user, auth_cookiesalt());
0 ignored issues
show
Bug introduced by
It seems like auth_cookiesalt() can also be of type boolean; however, PassHash::hmac() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
104
}
105
106
/**
107
 * Check the secret CSRF token
108
 *
109
 * @param null|string $token security token or null to read it from request variable
110
 * @return bool success if the token matched
111
 */
112
function checkSecurityToken($token = null) {
113
    /** @var Input $INPUT */
114
    global $INPUT;
115
    if(!$INPUT->server->str('REMOTE_USER')) return true; // no logged in user, no need for a check
116
117
    if(is_null($token)) $token = $INPUT->str('sectok');
118
    if(getSecurityToken() != $token) {
119
        msg('Security Token did not match. Possible CSRF attack.', -1);
120
        return false;
121
    }
122
    return true;
123
}
124
125
/**
126
 * Print a hidden form field with a secret CSRF token
127
 *
128
 * @author  Andreas Gohr <[email protected]>
129
 *
130
 * @param bool $print  if true print the field, otherwise html of the field is returned
131
 * @return string html of hidden form field
132
 */
133
function formSecurityToken($print = true) {
134
    $ret = '<div class="no"><input type="hidden" name="sectok" value="'.getSecurityToken().'" /></div>'."\n";
135
    if($print) echo $ret;
136
    return $ret;
137
}
138
139
/**
140
 * Determine basic information for a request of $id
141
 *
142
 * @author Andreas Gohr <[email protected]>
143
 * @author Chris Smith <[email protected]>
144
 *
145
 * @param string $id         pageid
146
 * @param bool   $htmlClient add info about whether is mobile browser
147
 * @return array with info for a request of $id
148
 *
149
 */
150
function basicinfo($id, $htmlClient=true){
151
    global $USERINFO;
152
    /* @var Input $INPUT */
153
    global $INPUT;
154
155
    // set info about manager/admin status.
156
    $info = array();
157
    $info['isadmin']   = false;
158
    $info['ismanager'] = false;
159
    if($INPUT->server->has('REMOTE_USER')) {
160
        $info['userinfo']   = $USERINFO;
161
        $info['perm']       = auth_quickaclcheck($id);
162
        $info['client']     = $INPUT->server->str('REMOTE_USER');
163
164
        if($info['perm'] == AUTH_ADMIN) {
165
            $info['isadmin']   = true;
166
            $info['ismanager'] = true;
167
        } elseif(auth_ismanager()) {
168
            $info['ismanager'] = true;
169
        }
170
171
        // if some outside auth were used only REMOTE_USER is set
172
        if(!$info['userinfo']['name']) {
173
            $info['userinfo']['name'] = $INPUT->server->str('REMOTE_USER');
174
        }
175
176
    } else {
177
        $info['perm']       = auth_aclcheck($id, '', null);
178
        $info['client']     = clientIP(true);
179
    }
180
181
    $info['namespace'] = getNS($id);
182
183
    // mobile detection
184
    if ($htmlClient) {
185
        $info['ismobile'] = clientismobile();
186
    }
187
188
    return $info;
189
 }
190
191
/**
192
 * Return info about the current document as associative
193
 * array.
194
 *
195
 * @author Andreas Gohr <[email protected]>
196
 *
197
 * @return array with info about current document
198
 */
199
function pageinfo() {
200
    global $ID;
201
    global $REV;
202
    global $RANGE;
203
    global $lang;
204
    /* @var Input $INPUT */
205
    global $INPUT;
206
207
    $info = basicinfo($ID);
208
209
    // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml
210
    // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary
211
    $info['id']  = $ID;
212
    $info['rev'] = $REV;
213
214
    if($INPUT->server->has('REMOTE_USER')) {
215
        $sub = new Subscription();
216
        $info['subscribed'] = $sub->user_subscription();
217
    } else {
218
        $info['subscribed'] = false;
219
    }
220
221
    $info['locked']     = checklock($ID);
222
    $info['filepath']   = wikiFN($ID);
223
    $info['exists']     = file_exists($info['filepath']);
224
    $info['currentrev'] = @filemtime($info['filepath']);
225
    if($REV) {
226
        //check if current revision was meant
227
        if($info['exists'] && ($info['currentrev'] == $REV)) {
228
            $REV = '';
229
        } elseif($RANGE) {
230
            //section editing does not work with old revisions!
231
            $REV   = '';
232
            $RANGE = '';
233
            msg($lang['nosecedit'], 0);
234
        } else {
235
            //really use old revision
236
            $info['filepath'] = wikiFN($ID, $REV);
237
            $info['exists']   = file_exists($info['filepath']);
238
        }
239
    }
240
    $info['rev'] = $REV;
241
    if($info['exists']) {
242
        $info['writable'] = (is_writable($info['filepath']) &&
243
            ($info['perm'] >= AUTH_EDIT));
244
    } else {
245
        $info['writable'] = ($info['perm'] >= AUTH_CREATE);
246
    }
247
    $info['editable'] = ($info['writable'] && empty($info['locked']));
248
    $info['lastmod']  = @filemtime($info['filepath']);
249
250
    //load page meta data
251
    $info['meta'] = p_get_metadata($ID);
252
253
    //who's the editor
254
    $pagelog = new PageChangeLog($ID, 1024);
255
    if($REV) {
256
        $revinfo = $pagelog->getRevisionInfo($REV);
257
    } else {
258
        if(!empty($info['meta']['last_change']) && is_array($info['meta']['last_change'])) {
259
            $revinfo = $info['meta']['last_change'];
260
        } else {
261
            $revinfo = $pagelog->getRevisionInfo($info['lastmod']);
262
            // cache most recent changelog line in metadata if missing and still valid
263
            if($revinfo !== false) {
264
                $info['meta']['last_change'] = $revinfo;
265
                p_set_metadata($ID, array('last_change' => $revinfo));
266
            }
267
        }
268
    }
269
    //and check for an external edit
270
    if($revinfo !== false && $revinfo['date'] != $info['lastmod']) {
271
        // cached changelog line no longer valid
272
        $revinfo                     = false;
273
        $info['meta']['last_change'] = $revinfo;
274
        p_set_metadata($ID, array('last_change' => $revinfo));
275
    }
276
277
    $info['ip']   = $revinfo['ip'];
278
    $info['user'] = $revinfo['user'];
279
    $info['sum']  = $revinfo['sum'];
280
    // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID.
281
    // Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor'].
282
283
    if($revinfo['user']) {
284
        $info['editor'] = $revinfo['user'];
285
    } else {
286
        $info['editor'] = $revinfo['ip'];
287
    }
288
289
    // draft
290
    $draft = getCacheName($info['client'].$ID, '.draft');
291
    if(file_exists($draft)) {
292
        if(@filemtime($draft) < @filemtime(wikiFN($ID))) {
293
            // remove stale draft
294
            @unlink($draft);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
295
        } else {
296
            $info['draft'] = $draft;
297
        }
298
    }
299
300
    return $info;
301
}
302
303
/**
304
 * Initialize and/or fill global $JSINFO with some basic info to be given to javascript
305
 */
306
function jsinfo() {
307
    global $JSINFO, $ID, $INFO, $ACT;
308
309
    if (!is_array($JSINFO)) {
310
        $JSINFO = [];
311
    }
312
    //export minimal info to JS, plugins can add more
313
    $JSINFO['id']                    = $ID;
314
    $JSINFO['namespace']             = (string) $INFO['namespace'];
315
    $JSINFO['ACT']                   = act_clean($ACT);
316
    $JSINFO['useHeadingNavigation']  = (int) useHeading('navigation');
317
    $JSINFO['useHeadingContent']     = (int) useHeading('content');
318
}
319
320
/**
321
 * Return information about the current media item as an associative array.
322
 *
323
 * @return array with info about current media item
324
 */
325
function mediainfo(){
326
    global $NS;
327
    global $IMG;
328
329
    $info = basicinfo("$NS:*");
330
    $info['image'] = $IMG;
331
332
    return $info;
333
}
334
335
/**
336
 * Build an string of URL parameters
337
 *
338
 * @author Andreas Gohr
339
 *
340
 * @param array  $params    array with key-value pairs
341
 * @param string $sep       series of pairs are separated by this character
342
 * @return string query string
343
 */
344
function buildURLparams($params, $sep = '&amp;') {
345
    $url = '';
346
    $amp = false;
347
    foreach($params as $key => $val) {
348
        if($amp) $url .= $sep;
349
350
        $url .= rawurlencode($key).'=';
351
        $url .= rawurlencode((string) $val);
352
        $amp = true;
353
    }
354
    return $url;
355
}
356
357
/**
358
 * Build an string of html tag attributes
359
 *
360
 * Skips keys starting with '_', values get HTML encoded
361
 *
362
 * @author Andreas Gohr
363
 *
364
 * @param array $params    array with (attribute name-attribute value) pairs
365
 * @param bool  $skipempty skip empty string values?
366
 * @return string
367
 */
368
function buildAttributes($params, $skipempty = false) {
369
    $url   = '';
370
    $white = false;
371
    foreach($params as $key => $val) {
372
        if($key{0} == '_') continue;
373
        if($val === '' && $skipempty) continue;
374
        if($white) $url .= ' ';
375
376
        $url .= $key.'="';
377
        $url .= htmlspecialchars($val);
378
        $url .= '"';
379
        $white = true;
380
    }
381
    return $url;
382
}
383
384
/**
385
 * This builds the breadcrumb trail and returns it as array
386
 *
387
 * @author Andreas Gohr <[email protected]>
388
 *
389
 * @return string[] with the data: array(pageid=>name, ... )
390
 */
391
function breadcrumbs() {
392
    // we prepare the breadcrumbs early for quick session closing
393
    static $crumbs = null;
394
    if($crumbs != null) return $crumbs;
395
396
    global $ID;
397
    global $ACT;
398
    global $conf;
399
400
    //first visit?
401
    $crumbs = isset($_SESSION[DOKU_COOKIE]['bc']) ? $_SESSION[DOKU_COOKIE]['bc'] : array();
402
    //we only save on show and existing visible wiki documents
403
    $file = wikiFN($ID);
404
    if($ACT != 'show' || isHiddenPage($ID) || !file_exists($file)) {
405
        $_SESSION[DOKU_COOKIE]['bc'] = $crumbs;
406
        return $crumbs;
407
    }
408
409
    // page names
410
    $name = noNSorNS($ID);
411
    if(useHeading('navigation')) {
412
        // get page title
413
        $title = p_get_first_heading($ID, METADATA_RENDER_USING_SIMPLE_CACHE);
414
        if($title) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $title of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
415
            $name = $title;
416
        }
417
    }
418
419
    //remove ID from array
420
    if(isset($crumbs[$ID])) {
421
        unset($crumbs[$ID]);
422
    }
423
424
    //add to array
425
    $crumbs[$ID] = $name;
426
    //reduce size
427
    while(count($crumbs) > $conf['breadcrumbs']) {
428
        array_shift($crumbs);
429
    }
430
    //save to session
431
    $_SESSION[DOKU_COOKIE]['bc'] = $crumbs;
432
    return $crumbs;
433
}
434
435
/**
436
 * Filter for page IDs
437
 *
438
 * This is run on a ID before it is outputted somewhere
439
 * currently used to replace the colon with something else
440
 * on Windows (non-IIS) systems and to have proper URL encoding
441
 *
442
 * See discussions at https://github.com/splitbrain/dokuwiki/pull/84 and
443
 * https://github.com/splitbrain/dokuwiki/pull/173 why we use a whitelist of
444
 * unaffected servers instead of blacklisting affected servers here.
445
 *
446
 * Urlencoding is ommitted when the second parameter is false
447
 *
448
 * @author Andreas Gohr <[email protected]>
449
 *
450
 * @param string $id pageid being filtered
451
 * @param bool   $ue apply urlencoding?
452
 * @return string
453
 */
454
function idfilter($id, $ue = true) {
455
    global $conf;
456
    /* @var Input $INPUT */
457
    global $INPUT;
458
459
    if($conf['useslash'] && $conf['userewrite']) {
460
        $id = strtr($id, ':', '/');
461
    } elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' &&
462
        $conf['userewrite'] &&
463
        strpos($INPUT->server->str('SERVER_SOFTWARE'), 'Microsoft-IIS') === false
464
    ) {
465
        $id = strtr($id, ':', ';');
466
    }
467
    if($ue) {
468
        $id = rawurlencode($id);
469
        $id = str_replace('%3A', ':', $id); //keep as colon
470
        $id = str_replace('%3B', ';', $id); //keep as semicolon
471
        $id = str_replace('%2F', '/', $id); //keep as slash
472
    }
473
    return $id;
474
}
475
476
/**
477
 * This builds a link to a wikipage
478
 *
479
 * It handles URL rewriting and adds additional parameters
480
 *
481
 * @author Andreas Gohr <[email protected]>
482
 *
483
 * @param string       $id             page id, defaults to start page
484
 * @param string|array $urlParameters  URL parameters, associative array recommended
485
 * @param bool         $absolute       request an absolute URL instead of relative
486
 * @param string       $separator      parameter separator
487
 * @return string
488
 */
489
function wl($id = '', $urlParameters = '', $absolute = false, $separator = '&amp;') {
490
    global $conf;
491
    if(is_array($urlParameters)) {
492
        if(isset($urlParameters['rev']) && !$urlParameters['rev']) unset($urlParameters['rev']);
493
        if(isset($urlParameters['at']) && $conf['date_at_format']) $urlParameters['at'] = date($conf['date_at_format'],$urlParameters['at']);
494
        $urlParameters = buildURLparams($urlParameters, $separator);
495
    } else {
496
        $urlParameters = str_replace(',', $separator, $urlParameters);
497
    }
498
    if($id === '') {
499
        $id = $conf['start'];
500
    }
501
    $id = idfilter($id);
502
    if($absolute) {
503
        $xlink = DOKU_URL;
504
    } else {
505
        $xlink = DOKU_BASE;
506
    }
507
508
    if($conf['userewrite'] == 2) {
509
        $xlink .= DOKU_SCRIPT.'/'.$id;
510
        if($urlParameters) $xlink .= '?'.$urlParameters;
511
    } elseif($conf['userewrite']) {
512
        $xlink .= $id;
513
        if($urlParameters) $xlink .= '?'.$urlParameters;
514
    } elseif($id) {
515
        $xlink .= DOKU_SCRIPT.'?id='.$id;
516
        if($urlParameters) $xlink .= $separator.$urlParameters;
517
    } else {
518
        $xlink .= DOKU_SCRIPT;
519
        if($urlParameters) $xlink .= '?'.$urlParameters;
520
    }
521
522
    return $xlink;
523
}
524
525
/**
526
 * This builds a link to an alternate page format
527
 *
528
 * Handles URL rewriting if enabled. Follows the style of wl().
529
 *
530
 * @author Ben Coburn <[email protected]>
531
 * @param string       $id             page id, defaults to start page
532
 * @param string       $format         the export renderer to use
533
 * @param string|array $urlParameters  URL parameters, associative array recommended
534
 * @param bool         $abs            request an absolute URL instead of relative
535
 * @param string       $sep            parameter separator
536
 * @return string
537
 */
538
function exportlink($id = '', $format = 'raw', $urlParameters = '', $abs = false, $sep = '&amp;') {
539
    global $conf;
540
    if(is_array($urlParameters)) {
541
        $urlParameters = buildURLparams($urlParameters, $sep);
542
    } else {
543
        $urlParameters = str_replace(',', $sep, $urlParameters);
544
    }
545
546
    $format = rawurlencode($format);
547
    $id     = idfilter($id);
548
    if($abs) {
549
        $xlink = DOKU_URL;
550
    } else {
551
        $xlink = DOKU_BASE;
552
    }
553
554
    if($conf['userewrite'] == 2) {
555
        $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format;
556
        if($urlParameters) $xlink .= $sep.$urlParameters;
557
    } elseif($conf['userewrite'] == 1) {
558
        $xlink .= '_export/'.$format.'/'.$id;
559
        if($urlParameters) $xlink .= '?'.$urlParameters;
560
    } else {
561
        $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id;
562
        if($urlParameters) $xlink .= $sep.$urlParameters;
563
    }
564
565
    return $xlink;
566
}
567
568
/**
569
 * Build a link to a media file
570
 *
571
 * Will return a link to the detail page if $direct is false
572
 *
573
 * The $more parameter should always be given as array, the function then
574
 * will strip default parameters to produce even cleaner URLs
575
 *
576
 * @param string  $id     the media file id or URL
577
 * @param mixed   $more   string or array with additional parameters
578
 * @param bool    $direct link to detail page if false
579
 * @param string  $sep    URL parameter separator
580
 * @param bool    $abs    Create an absolute URL
581
 * @return string
582
 */
583
function ml($id = '', $more = '', $direct = true, $sep = '&amp;', $abs = false) {
584
    global $conf;
585
    $isexternalimage = media_isexternal($id);
586
    if(!$isexternalimage) {
587
        $id = cleanID($id);
588
    }
589
590
    if(is_array($more)) {
591
        // add token for resized images
592
        if(!empty($more['w']) || !empty($more['h']) || $isexternalimage){
593
            $more['tok'] = media_get_token($id,$more['w'],$more['h']);
594
        }
595
        // strip defaults for shorter URLs
596
        if(isset($more['cache']) && $more['cache'] == 'cache') unset($more['cache']);
597
        if(empty($more['w'])) unset($more['w']);
598
        if(empty($more['h'])) unset($more['h']);
599
        if(isset($more['id']) && $direct) unset($more['id']);
600
        if(isset($more['rev']) && !$more['rev']) unset($more['rev']);
601
        $more = buildURLparams($more, $sep);
602
    } else {
603
        $matches = array();
604
        if (preg_match_all('/\b(w|h)=(\d*)\b/',$more,$matches,PREG_SET_ORDER) || $isexternalimage){
605
            $resize = array('w'=>0, 'h'=>0);
606
            foreach ($matches as $match){
0 ignored issues
show
Bug introduced by
The expression $matches of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
607
                $resize[$match[1]] = $match[2];
608
            }
609
            $more .= $more === '' ? '' : $sep;
610
            $more .= 'tok='.media_get_token($id,$resize['w'],$resize['h']);
611
        }
612
        $more = str_replace('cache=cache', '', $more); //skip default
613
        $more = str_replace(',,', ',', $more);
614
        $more = str_replace(',', $sep, $more);
615
    }
616
617
    if($abs) {
618
        $xlink = DOKU_URL;
619
    } else {
620
        $xlink = DOKU_BASE;
621
    }
622
623
    // external URLs are always direct without rewriting
624
    if($isexternalimage) {
625
        $xlink .= 'lib/exe/fetch.php';
626
        $xlink .= '?'.$more;
627
        $xlink .= $sep.'media='.rawurlencode($id);
628
        return $xlink;
629
    }
630
631
    $id = idfilter($id);
632
633
    // decide on scriptname
634
    if($direct) {
635
        if($conf['userewrite'] == 1) {
636
            $script = '_media';
637
        } else {
638
            $script = 'lib/exe/fetch.php';
639
        }
640
    } else {
641
        if($conf['userewrite'] == 1) {
642
            $script = '_detail';
643
        } else {
644
            $script = 'lib/exe/detail.php';
645
        }
646
    }
647
648
    // build URL based on rewrite mode
649
    if($conf['userewrite']) {
650
        $xlink .= $script.'/'.$id;
651
        if($more) $xlink .= '?'.$more;
652
    } else {
653
        if($more) {
654
            $xlink .= $script.'?'.$more;
655
            $xlink .= $sep.'media='.$id;
656
        } else {
657
            $xlink .= $script.'?media='.$id;
658
        }
659
    }
660
661
    return $xlink;
662
}
663
664
/**
665
 * Returns the URL to the DokuWiki base script
666
 *
667
 * Consider using wl() instead, unless you absoutely need the doku.php endpoint
668
 *
669
 * @author Andreas Gohr <[email protected]>
670
 *
671
 * @return string
672
 */
673
function script() {
674
    return DOKU_BASE.DOKU_SCRIPT;
675
}
676
677
/**
678
 * Spamcheck against wordlist
679
 *
680
 * Checks the wikitext against a list of blocked expressions
681
 * returns true if the text contains any bad words
682
 *
683
 * Triggers COMMON_WORDBLOCK_BLOCKED
684
 *
685
 *  Action Plugins can use this event to inspect the blocked data
686
 *  and gain information about the user who was blocked.
687
 *
688
 *  Event data:
689
 *    data['matches']  - array of matches
690
 *    data['userinfo'] - information about the blocked user
691
 *      [ip]           - ip address
692
 *      [user]         - username (if logged in)
693
 *      [mail]         - mail address (if logged in)
694
 *      [name]         - real name (if logged in)
695
 *
696
 * @author Andreas Gohr <[email protected]>
697
 * @author Michael Klier <[email protected]>
698
 *
699
 * @param  string $text - optional text to check, if not given the globals are used
700
 * @return bool         - true if a spam word was found
701
 */
702
function checkwordblock($text = '') {
703
    global $TEXT;
704
    global $PRE;
705
    global $SUF;
706
    global $SUM;
707
    global $conf;
708
    global $INFO;
709
    /* @var Input $INPUT */
710
    global $INPUT;
711
712
    if(!$conf['usewordblock']) return false;
713
714
    if(!$text) $text = "$PRE $TEXT $SUF $SUM";
715
716
    // we prepare the text a tiny bit to prevent spammers circumventing URL checks
717
    $text = preg_replace('!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i', '\1http://\2 \2\3', $text);
718
719
    $wordblocks = getWordblocks();
720
    // how many lines to read at once (to work around some PCRE limits)
721
    if(version_compare(phpversion(), '4.3.0', '<')) {
722
        // old versions of PCRE define a maximum of parenthesises even if no
723
        // backreferences are used - the maximum is 99
724
        // this is very bad performancewise and may even be too high still
725
        $chunksize = 40;
726
    } else {
727
        // read file in chunks of 200 - this should work around the
728
        // MAX_PATTERN_SIZE in modern PCRE
729
        $chunksize = 200;
730
    }
731
    while($blocks = array_splice($wordblocks, 0, $chunksize)) {
732
        $re = array();
733
        // build regexp from blocks
734
        foreach($blocks as $block) {
735
            $block = preg_replace('/#.*$/', '', $block);
736
            $block = trim($block);
737
            if(empty($block)) continue;
738
            $re[] = $block;
739
        }
740
        if(count($re) && preg_match('#('.join('|', $re).')#si', $text, $matches)) {
741
            // prepare event data
742
            $data = array();
743
            $data['matches']        = $matches;
744
            $data['userinfo']['ip'] = $INPUT->server->str('REMOTE_ADDR');
745
            if($INPUT->server->str('REMOTE_USER')) {
746
                $data['userinfo']['user'] = $INPUT->server->str('REMOTE_USER');
747
                $data['userinfo']['name'] = $INFO['userinfo']['name'];
748
                $data['userinfo']['mail'] = $INFO['userinfo']['mail'];
749
            }
750
            $callback = function () {
751
                return true;
752
            };
753
            return trigger_event('COMMON_WORDBLOCK_BLOCKED', $data, $callback, true);
754
        }
755
    }
756
    return false;
757
}
758
759
/**
760
 * Return the IP of the client
761
 *
762
 * Honours X-Forwarded-For and X-Real-IP Proxy Headers
763
 *
764
 * It returns a comma separated list of IPs if the above mentioned
765
 * headers are set. If the single parameter is set, it tries to return
766
 * a routable public address, prefering the ones suplied in the X
767
 * headers
768
 *
769
 * @author Andreas Gohr <[email protected]>
770
 *
771
 * @param  boolean $single If set only a single IP is returned
772
 * @return string
773
 */
774
function clientIP($single = false) {
775
    /* @var Input $INPUT */
776
    global $INPUT;
777
778
    $ip   = array();
779
    $ip[] = $INPUT->server->str('REMOTE_ADDR');
780
    if($INPUT->server->str('HTTP_X_FORWARDED_FOR')) {
781
        $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_FORWARDED_FOR'))));
782
    }
783
    if($INPUT->server->str('HTTP_X_REAL_IP')) {
784
        $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_REAL_IP'))));
785
    }
786
787
    // some IPv4/v6 regexps borrowed from Feyd
788
    // see: http://forums.devnetwork.net/viewtopic.php?f=38&t=53479
789
    $dec_octet   = '(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[0-9])';
790
    $hex_digit   = '[A-Fa-f0-9]';
791
    $h16         = "{$hex_digit}{1,4}";
792
    $IPv4Address = "$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet";
793
    $ls32        = "(?:$h16:$h16|$IPv4Address)";
794
    $IPv6Address =
795
        "(?:(?:{$IPv4Address})|(?:".
796
            "(?:$h16:){6}$ls32".
797
            "|::(?:$h16:){5}$ls32".
798
            "|(?:$h16)?::(?:$h16:){4}$ls32".
799
            "|(?:(?:$h16:){0,1}$h16)?::(?:$h16:){3}$ls32".
800
            "|(?:(?:$h16:){0,2}$h16)?::(?:$h16:){2}$ls32".
801
            "|(?:(?:$h16:){0,3}$h16)?::(?:$h16:){1}$ls32".
802
            "|(?:(?:$h16:){0,4}$h16)?::$ls32".
803
            "|(?:(?:$h16:){0,5}$h16)?::$h16".
804
            "|(?:(?:$h16:){0,6}$h16)?::".
805
            ")(?:\\/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))?)";
806
807
    // remove any non-IP stuff
808
    $cnt   = count($ip);
809
    $match = array();
810
    for($i = 0; $i < $cnt; $i++) {
811
        if(preg_match("/^$IPv4Address$/", $ip[$i], $match) || preg_match("/^$IPv6Address$/", $ip[$i], $match)) {
812
            $ip[$i] = $match[0];
813
        } else {
814
            $ip[$i] = '';
815
        }
816
        if(empty($ip[$i])) unset($ip[$i]);
817
    }
818
    $ip = array_values(array_unique($ip));
819
    if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP
820
821
    if(!$single) return join(',', $ip);
822
823
    // decide which IP to use, trying to avoid local addresses
824
    $ip = array_reverse($ip);
825
    foreach($ip as $i) {
826
        if(preg_match('/^(::1|[fF][eE]80:|127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/', $i)) {
827
            continue;
828
        } else {
829
            return $i;
830
        }
831
    }
832
    // still here? just use the first (last) address
833
    return $ip[0];
834
}
835
836
/**
837
 * Check if the browser is on a mobile device
838
 *
839
 * Adapted from the example code at url below
840
 *
841
 * @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code
842
 *
843
 * @return bool if true, client is mobile browser; otherwise false
844
 */
845
function clientismobile() {
846
    /* @var Input $INPUT */
847
    global $INPUT;
848
849
    if($INPUT->server->has('HTTP_X_WAP_PROFILE')) return true;
850
851
    if(preg_match('/wap\.|\.wap/i', $INPUT->server->str('HTTP_ACCEPT'))) return true;
852
853
    if(!$INPUT->server->has('HTTP_USER_AGENT')) return false;
854
855
    $uamatches = 'midp|j2me|avantg|docomo|novarra|palmos|palmsource|240x320|opwv|chtml|pda|windows ce|mmp\/|blackberry|mib\/|symbian|wireless|nokia|hand|mobi|phone|cdm|up\.b|audio|SIE\-|SEC\-|samsung|HTC|mot\-|mitsu|sagem|sony|alcatel|lg|erics|vx|NEC|philips|mmm|xx|panasonic|sharp|wap|sch|rover|pocket|benq|java|pt|pg|vox|amoi|bird|compal|kg|voda|sany|kdd|dbt|sendo|sgh|gradi|jb|\d\d\di|moto';
856
857
    if(preg_match("/$uamatches/i", $INPUT->server->str('HTTP_USER_AGENT'))) return true;
858
859
    return false;
860
}
861
862
/**
863
 * check if a given link is interwiki link
864
 *
865
 * @param string $link the link, e.g. "wiki>page"
866
 * @return bool
867
 */
868
function link_isinterwiki($link){
869
    if (preg_match('/^[a-zA-Z0-9\.]+>/u',$link)) return true;
870
    return false;
871
}
872
873
/**
874
 * Convert one or more comma separated IPs to hostnames
875
 *
876
 * If $conf['dnslookups'] is disabled it simply returns the input string
877
 *
878
 * @author Glen Harris <[email protected]>
879
 *
880
 * @param  string $ips comma separated list of IP addresses
881
 * @return string a comma separated list of hostnames
882
 */
883
function gethostsbyaddrs($ips) {
884
    global $conf;
885
    if(!$conf['dnslookups']) return $ips;
886
887
    $hosts = array();
888
    $ips   = explode(',', $ips);
889
890
    if(is_array($ips)) {
891
        foreach($ips as $ip) {
892
            $hosts[] = gethostbyaddr(trim($ip));
893
        }
894
        return join(',', $hosts);
895
    } else {
896
        return gethostbyaddr(trim($ips));
897
    }
898
}
899
900
/**
901
 * Checks if a given page is currently locked.
902
 *
903
 * removes stale lockfiles
904
 *
905
 * @author Andreas Gohr <[email protected]>
906
 *
907
 * @param string $id page id
908
 * @return bool page is locked?
909
 */
910
function checklock($id) {
911
    global $conf;
912
    /* @var Input $INPUT */
913
    global $INPUT;
914
915
    $lock = wikiLockFN($id);
916
917
    //no lockfile
918
    if(!file_exists($lock)) return false;
919
920
    //lockfile expired
921
    if((time() - filemtime($lock)) > $conf['locktime']) {
922
        @unlink($lock);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
923
        return false;
924
    }
925
926
    //my own lock
927
    @list($ip, $session) = explode("\n", io_readFile($lock));
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
928
    if($ip == $INPUT->server->str('REMOTE_USER') || $ip == clientIP() || (session_id() && $session == session_id())) {
929
        return false;
930
    }
931
932
    return $ip;
933
}
934
935
/**
936
 * Lock a page for editing
937
 *
938
 * @author Andreas Gohr <[email protected]>
939
 *
940
 * @param string $id page id to lock
941
 */
942
function lock($id) {
943
    global $conf;
944
    /* @var Input $INPUT */
945
    global $INPUT;
946
947
    if($conf['locktime'] == 0) {
948
        return;
949
    }
950
951
    $lock = wikiLockFN($id);
952
    if($INPUT->server->str('REMOTE_USER')) {
953
        io_saveFile($lock, $INPUT->server->str('REMOTE_USER'));
954
    } else {
955
        io_saveFile($lock, clientIP()."\n".session_id());
956
    }
957
}
958
959
/**
960
 * Unlock a page if it was locked by the user
961
 *
962
 * @author Andreas Gohr <[email protected]>
963
 *
964
 * @param string $id page id to unlock
965
 * @return bool true if a lock was removed
966
 */
967
function unlock($id) {
968
    /* @var Input $INPUT */
969
    global $INPUT;
970
971
    $lock = wikiLockFN($id);
972
    if(file_exists($lock)) {
973
        @list($ip, $session) = explode("\n", io_readFile($lock));
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
974
        if($ip == $INPUT->server->str('REMOTE_USER') || $ip == clientIP() || $session == session_id()) {
975
            @unlink($lock);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
976
            return true;
977
        }
978
    }
979
    return false;
980
}
981
982
/**
983
 * convert line ending to unix format
984
 *
985
 * also makes sure the given text is valid UTF-8
986
 *
987
 * @see    formText() for 2crlf conversion
988
 * @author Andreas Gohr <[email protected]>
989
 *
990
 * @param string $text
991
 * @return string
992
 */
993
function cleanText($text) {
994
    $text = preg_replace("/(\015\012)|(\015)/", "\012", $text);
995
996
    // if the text is not valid UTF-8 we simply assume latin1
997
    // this won't break any worse than it breaks with the wrong encoding
998
    // but might actually fix the problem in many cases
999
    if(!utf8_check($text)) $text = utf8_encode($text);
1000
1001
    return $text;
1002
}
1003
1004
/**
1005
 * Prepares text for print in Webforms by encoding special chars.
1006
 * It also converts line endings to Windows format which is
1007
 * pseudo standard for webforms.
1008
 *
1009
 * @see    cleanText() for 2unix conversion
1010
 * @author Andreas Gohr <[email protected]>
1011
 *
1012
 * @param string $text
1013
 * @return string
1014
 */
1015
function formText($text) {
1016
    $text = str_replace("\012", "\015\012", $text);
1017
    return htmlspecialchars($text);
1018
}
1019
1020
/**
1021
 * Returns the specified local text in raw format
1022
 *
1023
 * @author Andreas Gohr <[email protected]>
1024
 *
1025
 * @param string $id   page id
1026
 * @param string $ext  extension of file being read, default 'txt'
1027
 * @return string
1028
 */
1029
function rawLocale($id, $ext = 'txt') {
1030
    return io_readFile(localeFN($id, $ext));
1031
}
1032
1033
/**
1034
 * Returns the raw WikiText
1035
 *
1036
 * @author Andreas Gohr <[email protected]>
1037
 *
1038
 * @param string $id   page id
1039
 * @param string|int $rev  timestamp when a revision of wikitext is desired
1040
 * @return string
1041
 */
1042
function rawWiki($id, $rev = '') {
1043
    return io_readWikiPage(wikiFN($id, $rev), $id, $rev);
1044
}
1045
1046
/**
1047
 * Returns the pagetemplate contents for the ID's namespace
1048
 *
1049
 * @triggers COMMON_PAGETPL_LOAD
1050
 * @author Andreas Gohr <[email protected]>
1051
 *
1052
 * @param string $id the id of the page to be created
1053
 * @return string parsed pagetemplate content
1054
 */
1055
function pageTemplate($id) {
1056
    global $conf;
1057
1058
    if(is_array($id)) $id = $id[0];
1059
1060
    // prepare initial event data
1061
    $data = array(
1062
        'id'        => $id, // the id of the page to be created
1063
        'tpl'       => '', // the text used as template
1064
        'tplfile'   => '', // the file above text was/should be loaded from
1065
        'doreplace' => true // should wildcard replacements be done on the text?
1066
    );
1067
1068
    $evt = new Doku_Event('COMMON_PAGETPL_LOAD', $data);
1069
    if($evt->advise_before(true)) {
1070
        // the before event might have loaded the content already
1071
        if(empty($data['tpl'])) {
1072
            // if the before event did not set a template file, try to find one
1073
            if(empty($data['tplfile'])) {
1074
                $path = dirname(wikiFN($id));
1075
                if(file_exists($path.'/_template.txt')) {
1076
                    $data['tplfile'] = $path.'/_template.txt';
1077
                } else {
1078
                    // search upper namespaces for templates
1079
                    $len = strlen(rtrim($conf['datadir'], '/'));
1080
                    while(strlen($path) >= $len) {
1081
                        if(file_exists($path.'/__template.txt')) {
1082
                            $data['tplfile'] = $path.'/__template.txt';
1083
                            break;
1084
                        }
1085
                        $path = substr($path, 0, strrpos($path, '/'));
1086
                    }
1087
                }
1088
            }
1089
            // load the content
1090
            $data['tpl'] = io_readFile($data['tplfile']);
1091
        }
1092
        if($data['doreplace']) parsePageTemplate($data);
1093
    }
1094
    $evt->advise_after();
1095
    unset($evt);
1096
1097
    return $data['tpl'];
1098
}
1099
1100
/**
1101
 * Performs common page template replacements
1102
 * This works on data from COMMON_PAGETPL_LOAD
1103
 *
1104
 * @author Andreas Gohr <[email protected]>
1105
 *
1106
 * @param array $data array with event data
1107
 * @return string
1108
 */
1109
function parsePageTemplate(&$data) {
1110
    /**
1111
     * @var string $id        the id of the page to be created
1112
     * @var string $tpl       the text used as template
1113
     * @var string $tplfile   the file above text was/should be loaded from
1114
     * @var bool   $doreplace should wildcard replacements be done on the text?
1115
     */
1116
    extract($data);
1117
1118
    global $USERINFO;
1119
    global $conf;
1120
    /* @var Input $INPUT */
1121
    global $INPUT;
1122
1123
    // replace placeholders
1124
    $file = noNS($id);
1125
    $page = strtr($file, $conf['sepchar'], ' ');
1126
1127
    $tpl = str_replace(
1128
        array(
1129
             '@ID@',
1130
             '@NS@',
1131
             '@CURNS@',
1132
             '@FILE@',
1133
             '@!FILE@',
1134
             '@!FILE!@',
1135
             '@PAGE@',
1136
             '@!PAGE@',
1137
             '@!!PAGE@',
1138
             '@!PAGE!@',
1139
             '@USER@',
1140
             '@NAME@',
1141
             '@MAIL@',
1142
             '@DATE@',
1143
        ),
1144
        array(
1145
             $id,
1146
             getNS($id),
1147
             curNS($id),
1148
             $file,
1149
             utf8_ucfirst($file),
1150
             utf8_strtoupper($file),
1151
             $page,
1152
             utf8_ucfirst($page),
1153
             utf8_ucwords($page),
1154
             utf8_strtoupper($page),
1155
             $INPUT->server->str('REMOTE_USER'),
1156
             $USERINFO['name'],
1157
             $USERINFO['mail'],
1158
             $conf['dformat'],
1159
        ), $tpl
1160
    );
1161
1162
    // we need the callback to work around strftime's char limit
1163
    $tpl = preg_replace_callback(
1164
        '/%./',
1165
        function ($m) {
1166
            return strftime($m[0]);
1167
        },
1168
        $tpl
1169
    );
1170
    $data['tpl'] = $tpl;
1171
    return $tpl;
1172
}
1173
1174
/**
1175
 * Returns the raw Wiki Text in three slices.
1176
 *
1177
 * The range parameter needs to have the form "from-to"
1178
 * and gives the range of the section in bytes - no
1179
 * UTF-8 awareness is needed.
1180
 * The returned order is prefix, section and suffix.
1181
 *
1182
 * @author Andreas Gohr <[email protected]>
1183
 *
1184
 * @param string $range in form "from-to"
1185
 * @param string $id    page id
1186
 * @param string $rev   optional, the revision timestamp
1187
 * @return string[] with three slices
1188
 */
1189
function rawWikiSlices($range, $id, $rev = '') {
1190
    $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev);
1191
1192
    // Parse range
1193
    list($from, $to) = explode('-', $range, 2);
1194
    // Make range zero-based, use defaults if marker is missing
1195
    $from = !$from ? 0 : ($from - 1);
1196
    $to   = !$to ? strlen($text) : ($to - 1);
1197
1198
    $slices = array();
1199
    $slices[0] = substr($text, 0, $from);
1200
    $slices[1] = substr($text, $from, $to - $from);
1201
    $slices[2] = substr($text, $to);
1202
    return $slices;
1203
}
1204
1205
/**
1206
 * Joins wiki text slices
1207
 *
1208
 * function to join the text slices.
1209
 * When the pretty parameter is set to true it adds additional empty
1210
 * lines between sections if needed (used on saving).
1211
 *
1212
 * @author Andreas Gohr <[email protected]>
1213
 *
1214
 * @param string $pre   prefix
1215
 * @param string $text  text in the middle
1216
 * @param string $suf   suffix
1217
 * @param bool $pretty add additional empty lines between sections
1218
 * @return string
1219
 */
1220
function con($pre, $text, $suf, $pretty = false) {
1221
    if($pretty) {
1222
        if($pre !== '' && substr($pre, -1) !== "\n" &&
1223
            substr($text, 0, 1) !== "\n"
1224
        ) {
1225
            $pre .= "\n";
1226
        }
1227
        if($suf !== '' && substr($text, -1) !== "\n" &&
1228
            substr($suf, 0, 1) !== "\n"
1229
        ) {
1230
            $text .= "\n";
1231
        }
1232
    }
1233
1234
    return $pre.$text.$suf;
1235
}
1236
1237
/**
1238
 * Checks if the current page version is newer than the last entry in the page's
1239
 * changelog. If so, we assume it has been an external edit and we create an
1240
 * attic copy and add a proper changelog line.
1241
 *
1242
 * This check is only executed when the page is about to be saved again from the
1243
 * wiki, triggered in @see saveWikiText()
1244
 *
1245
 * @param string $id the page ID
1246
 */
1247
function detectExternalEdit($id) {
1248
    global $lang;
1249
1250
    $fileLastMod = wikiFN($id);
1251
    $lastMod     = @filemtime($fileLastMod); // from page
1252
    $pagelog     = new PageChangeLog($id, 1024);
1253
    $lastRev     = $pagelog->getRevisions(-1, 1); // from changelog
1254
    $lastRev     = (int) (empty($lastRev) ? 0 : $lastRev[0]);
1255
1256
    if(!file_exists(wikiFN($id, $lastMod)) && file_exists($fileLastMod) && $lastMod >= $lastRev) {
1257
        // add old revision to the attic if missing
1258
        saveOldRevision($id);
1259
        // add a changelog entry if this edit came from outside dokuwiki
1260
        if($lastMod > $lastRev) {
1261
            $fileLastRev = wikiFN($id, $lastRev);
1262
            $revinfo = $pagelog->getRevisionInfo($lastRev);
1263
            if(empty($lastRev) || !file_exists($fileLastRev) || $revinfo['type'] == DOKU_CHANGE_TYPE_DELETE) {
1264
                $filesize_old = 0;
1265
            } else {
1266
                $filesize_old = io_getSizeFile($fileLastRev);
1267
            }
1268
            $filesize_new = filesize($fileLastMod);
1269
            $sizechange = $filesize_new - $filesize_old;
1270
1271
            addLogEntry($lastMod, $id, DOKU_CHANGE_TYPE_EDIT, $lang['external_edit'], '', array('ExternalEdit'=> true), $sizechange);
1272
            // remove soon to be stale instructions
1273
            $cache = new cache_instructions($id, $fileLastMod);
1274
            $cache->removeCache();
1275
        }
1276
    }
1277
}
1278
1279
/**
1280
 * Saves a wikitext by calling io_writeWikiPage.
1281
 * Also directs changelog and attic updates.
1282
 *
1283
 * @author Andreas Gohr <[email protected]>
1284
 * @author Ben Coburn <[email protected]>
1285
 *
1286
 * @param string $id       page id
1287
 * @param string $text     wikitext being saved
1288
 * @param string $summary  summary of text update
1289
 * @param bool   $minor    mark this saved version as minor update
1290
 */
1291
function saveWikiText($id, $text, $summary, $minor = false) {
1292
    /* Note to developers:
1293
       This code is subtle and delicate. Test the behavior of
1294
       the attic and changelog with dokuwiki and external edits
1295
       after any changes. External edits change the wiki page
1296
       directly without using php or dokuwiki.
1297
     */
1298
    global $conf;
1299
    global $lang;
1300
    global $REV;
1301
    /* @var Input $INPUT */
1302
    global $INPUT;
1303
1304
    // prepare data for event
1305
    $svdta = array();
1306
    $svdta['id']             = $id;
1307
    $svdta['file']           = wikiFN($id);
1308
    $svdta['revertFrom']     = $REV;
1309
    $svdta['oldRevision']    = @filemtime($svdta['file']);
1310
    $svdta['newRevision']    = 0;
1311
    $svdta['newContent']     = $text;
1312
    $svdta['oldContent']     = rawWiki($id);
1313
    $svdta['summary']        = $summary;
1314
    $svdta['contentChanged'] = ($svdta['newContent'] != $svdta['oldContent']);
1315
    $svdta['changeInfo']     = '';
1316
    $svdta['changeType']     = DOKU_CHANGE_TYPE_EDIT;
1317
    $svdta['sizechange']     = null;
1318
1319
    // select changelog line type
1320
    if($REV) {
1321
        $svdta['changeType']  = DOKU_CHANGE_TYPE_REVERT;
1322
        $svdta['changeInfo'] = $REV;
1323
    } else if(!file_exists($svdta['file'])) {
1324
        $svdta['changeType'] = DOKU_CHANGE_TYPE_CREATE;
1325
    } else if(trim($text) == '') {
1326
        // empty or whitespace only content deletes
1327
        $svdta['changeType'] = DOKU_CHANGE_TYPE_DELETE;
1328
        // autoset summary on deletion
1329
        if(blank($svdta['summary'])) {
1330
            $svdta['summary'] = $lang['deleted'];
1331
        }
1332
    } else if($minor && $conf['useacl'] && $INPUT->server->str('REMOTE_USER')) {
1333
        //minor edits only for logged in users
1334
        $svdta['changeType'] = DOKU_CHANGE_TYPE_MINOR_EDIT;
1335
    }
1336
1337
    $event = new Doku_Event('COMMON_WIKIPAGE_SAVE', $svdta);
1338
    if(!$event->advise_before()) return;
1339
1340
    // if the content has not been changed, no save happens (plugins may override this)
1341
    if(!$svdta['contentChanged']) return;
1342
1343
    detectExternalEdit($id);
1344
1345
    if(
1346
        $svdta['changeType'] == DOKU_CHANGE_TYPE_CREATE ||
1347
        ($svdta['changeType'] == DOKU_CHANGE_TYPE_REVERT && !file_exists($svdta['file']))
1348
    ) {
1349
        $filesize_old = 0;
1350
    } else {
1351
        $filesize_old = filesize($svdta['file']);
1352
    }
1353
    if($svdta['changeType'] == DOKU_CHANGE_TYPE_DELETE) {
1354
        // Send "update" event with empty data, so plugins can react to page deletion
1355
        $data = array(array($svdta['file'], '', false), getNS($id), noNS($id), false);
1356
        trigger_event('IO_WIKIPAGE_WRITE', $data);
1357
        // pre-save deleted revision
1358
        @touch($svdta['file']);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1359
        clearstatcache();
1360
        $svdta['newRevision'] = saveOldRevision($id);
1361
        // remove empty file
1362
        @unlink($svdta['file']);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1363
        $filesize_new = 0;
1364
        // don't remove old meta info as it should be saved, plugins can use IO_WIKIPAGE_WRITE for removing their metadata...
1365
        // purge non-persistant meta data
1366
        p_purge_metadata($id);
1367
        // remove empty namespaces
1368
        io_sweepNS($id, 'datadir');
1369
        io_sweepNS($id, 'mediadir');
1370
    } else {
1371
        // save file (namespace dir is created in io_writeWikiPage)
1372
        io_writeWikiPage($svdta['file'], $svdta['newContent'], $id);
1373
        // pre-save the revision, to keep the attic in sync
1374
        $svdta['newRevision'] = saveOldRevision($id);
1375
        $filesize_new = filesize($svdta['file']);
1376
    }
1377
    $svdta['sizechange'] = $filesize_new - $filesize_old;
1378
1379
    $event->advise_after();
1380
1381
    addLogEntry($svdta['newRevision'], $svdta['id'], $svdta['changeType'], $svdta['summary'], $svdta['changeInfo'], null, $svdta['sizechange']);
1382
1383
    // send notify mails
1384
    notify($svdta['id'], 'admin', $svdta['oldRevision'], $svdta['summary'], $minor);
1385
    notify($svdta['id'], 'subscribers', $svdta['oldRevision'], $svdta['summary'], $minor);
1386
1387
    // update the purgefile (timestamp of the last time anything within the wiki was changed)
1388
    io_saveFile($conf['cachedir'].'/purgefile', time());
1389
1390
    // if useheading is enabled, purge the cache of all linking pages
1391
    if(useHeading('content')) {
1392
        $pages = ft_backlinks($id, true);
1393
        foreach($pages as $page) {
1394
            $cache = new cache_renderer($page, wikiFN($page), 'xhtml');
1395
            $cache->removeCache();
1396
        }
1397
    }
1398
}
1399
1400
/**
1401
 * moves the current version to the attic and returns its
1402
 * revision date
1403
 *
1404
 * @author Andreas Gohr <[email protected]>
1405
 *
1406
 * @param string $id page id
1407
 * @return int|string revision timestamp
1408
 */
1409
function saveOldRevision($id) {
1410
    $oldf = wikiFN($id);
1411
    if(!file_exists($oldf)) return '';
1412
    $date = filemtime($oldf);
1413
    $newf = wikiFN($id, $date);
1414
    io_writeWikiPage($newf, rawWiki($id), $id, $date);
1415
    return $date;
1416
}
1417
1418
/**
1419
 * Save a draft of a current edit session
1420
 *
1421
 * The draft will not be saved if
1422
 *   - drafts are deactivated in the config
1423
 *   - or the editarea is empty and there are no event handlers registered
1424
 *   - or the event is prevented
1425
 *
1426
 * @triggers COMMON_DRAFT_SAVE
1427
 *
1428
 * @return bool whether has the draft been saved
1429
 */
1430
function saveDraft()
1431
{
1432
    global $INPUT, $ID, $INFO, $EVENT_HANDLER, $conf;
1433
1434
    if (!$conf['usedraft']) {
1435
        return false;
1436
    }
1437
    if (!$INPUT->post->has('wikitext') &&
1438
        !$EVENT_HANDLER->hasHandlerForEvent('COMMON_DRAFT_SAVE')) {
1439
        return false;
1440
    }
1441
1442
    $draft = array(
1443
        'id' => $ID,
1444
        'prefix' => substr($INPUT->post->str('prefix'), 0, -1),
1445
        'text' => $INPUT->post->str('wikitext'),
1446
        'suffix' => $INPUT->post->str('suffix'),
1447
        'date' => $INPUT->post->int('date'),
1448
        'client' => $INFO['client'],
1449
        'cname' => getCacheName($INFO['client'] . $ID, '.draft'),
1450
    );
1451
    $event = new \Doku_Event('COMMON_DRAFT_SAVE', $draft);
1452
    if ($event->advise_before()) {
1453
        $draft['hasBeenSaved'] = io_saveFile($draft['cname'], serialize($draft));
1454
        if ($draft['hasBeenSaved']) {
1455
            $INFO['draft'] = $draft['cname'];
1456
        }
1457
    } else {
1458
        $draft['hasBeenSaved'] = false;
1459
    }
1460
1461
    $event->advise_after();
1462
    return (bool)$draft['hasBeenSaved'];
1463
}
1464
1465
/**
1466
 * Sends a notify mail on page change or registration
1467
 *
1468
 * @param string     $id       The changed page
1469
 * @param string     $who      Who to notify (admin|subscribers|register)
1470
 * @param int|string $rev Old page revision
1471
 * @param string     $summary  What changed
1472
 * @param boolean    $minor    Is this a minor edit?
1473
 * @param string[]   $replace  Additional string substitutions, @KEY@ to be replaced by value
1474
 * @return bool
1475
 *
1476
 * @author Andreas Gohr <[email protected]>
1477
 */
1478
function notify($id, $who, $rev = '', $summary = '', $minor = false, $replace = array()) {
1479
    global $conf;
1480
    /* @var Input $INPUT */
1481
    global $INPUT;
1482
1483
    // decide if there is something to do, eg. whom to mail
1484
    if($who == 'admin') {
1485
        if(empty($conf['notify'])) return false; //notify enabled?
1486
        $tpl = 'mailtext';
1487
        $to  = $conf['notify'];
1488
    } elseif($who == 'subscribers') {
1489
        if(!actionOK('subscribe')) return false; //subscribers enabled?
1490
        if($conf['useacl'] && $INPUT->server->str('REMOTE_USER') && $minor) return false; //skip minors
1491
        $data = array('id' => $id, 'addresslist' => '', 'self' => false, 'replacements' => $replace);
1492
        trigger_event(
1493
            'COMMON_NOTIFY_ADDRESSLIST', $data,
1494
            array(new Subscription(), 'notifyaddresses')
1495
        );
1496
        $to = $data['addresslist'];
1497
        if(empty($to)) return false;
1498
        $tpl = 'subscr_single';
1499
    } else {
1500
        return false; //just to be safe
1501
    }
1502
1503
    // prepare content
1504
    $subscription = new Subscription();
1505
    return $subscription->send_diff($to, $tpl, $id, $rev, $summary);
1506
}
1507
1508
/**
1509
 * extracts the query from a search engine referrer
1510
 *
1511
 * @author Andreas Gohr <[email protected]>
1512
 * @author Todd Augsburger <[email protected]>
1513
 *
1514
 * @return array|string
1515
 */
1516
function getGoogleQuery() {
1517
    /* @var Input $INPUT */
1518
    global $INPUT;
1519
1520
    if(!$INPUT->server->has('HTTP_REFERER')) {
1521
        return '';
1522
    }
1523
    $url = parse_url($INPUT->server->str('HTTP_REFERER'));
1524
1525
    // only handle common SEs
1526
    if(!preg_match('/(google|bing|yahoo|ask|duckduckgo|babylon|aol|yandex)/',$url['host'])) return '';
1527
1528
    $query = array();
1529
    // temporary workaround against PHP bug #49733
1530
    // see http://bugs.php.net/bug.php?id=49733
1531
    if(UTF8_MBSTRING) $enc = mb_internal_encoding();
1532
    parse_str($url['query'], $query);
1533
    if(UTF8_MBSTRING) mb_internal_encoding($enc);
0 ignored issues
show
Bug introduced by
The variable $enc does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1534
1535
    $q = '';
1536
    if(isset($query['q'])){
1537
        $q = $query['q'];
1538
    }elseif(isset($query['p'])){
1539
        $q = $query['p'];
1540
    }elseif(isset($query['query'])){
1541
        $q = $query['query'];
1542
    }
1543
    $q = trim($q);
1544
1545
    if(!$q) return '';
1546
    $q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/', $q, -1, PREG_SPLIT_NO_EMPTY);
1547
    return $q;
1548
}
1549
1550
/**
1551
 * Return the human readable size of a file
1552
 *
1553
 * @param int $size A file size
1554
 * @param int $dec A number of decimal places
1555
 * @return string human readable size
1556
 *
1557
 * @author      Martin Benjamin <[email protected]>
1558
 * @author      Aidan Lister <[email protected]>
1559
 * @version     1.0.0
1560
 */
1561
function filesize_h($size, $dec = 1) {
1562
    $sizes = array('B', 'KB', 'MB', 'GB');
1563
    $count = count($sizes);
1564
    $i     = 0;
1565
1566
    while($size >= 1024 && ($i < $count - 1)) {
1567
        $size /= 1024;
1568
        $i++;
1569
    }
1570
1571
    return round($size, $dec)."\xC2\xA0".$sizes[$i]; //non-breaking space
1572
}
1573
1574
/**
1575
 * Return the given timestamp as human readable, fuzzy age
1576
 *
1577
 * @author Andreas Gohr <[email protected]>
1578
 *
1579
 * @param int $dt timestamp
1580
 * @return string
1581
 */
1582
function datetime_h($dt) {
1583
    global $lang;
1584
1585
    $ago = time() - $dt;
1586
    if($ago > 24 * 60 * 60 * 30 * 12 * 2) {
1587
        return sprintf($lang['years'], round($ago / (24 * 60 * 60 * 30 * 12)));
1588
    }
1589
    if($ago > 24 * 60 * 60 * 30 * 2) {
1590
        return sprintf($lang['months'], round($ago / (24 * 60 * 60 * 30)));
1591
    }
1592
    if($ago > 24 * 60 * 60 * 7 * 2) {
1593
        return sprintf($lang['weeks'], round($ago / (24 * 60 * 60 * 7)));
1594
    }
1595
    if($ago > 24 * 60 * 60 * 2) {
1596
        return sprintf($lang['days'], round($ago / (24 * 60 * 60)));
1597
    }
1598
    if($ago > 60 * 60 * 2) {
1599
        return sprintf($lang['hours'], round($ago / (60 * 60)));
1600
    }
1601
    if($ago > 60 * 2) {
1602
        return sprintf($lang['minutes'], round($ago / (60)));
1603
    }
1604
    return sprintf($lang['seconds'], $ago);
1605
}
1606
1607
/**
1608
 * Wraps around strftime but provides support for fuzzy dates
1609
 *
1610
 * The format default to $conf['dformat']. It is passed to
1611
 * strftime - %f can be used to get the value from datetime_h()
1612
 *
1613
 * @see datetime_h
1614
 * @author Andreas Gohr <[email protected]>
1615
 *
1616
 * @param int|null $dt      timestamp when given, null will take current timestamp
1617
 * @param string   $format  empty default to $conf['dformat'], or provide format as recognized by strftime()
1618
 * @return string
1619
 */
1620
function dformat($dt = null, $format = '') {
1621
    global $conf;
1622
1623
    if(is_null($dt)) $dt = time();
1624
    $dt = (int) $dt;
1625
    if(!$format) $format = $conf['dformat'];
1626
1627
    $format = str_replace('%f', datetime_h($dt), $format);
1628
    return strftime($format, $dt);
1629
}
1630
1631
/**
1632
 * Formats a timestamp as ISO 8601 date
1633
 *
1634
 * @author <ungu at terong dot com>
1635
 * @link http://php.net/manual/en/function.date.php#54072
1636
 *
1637
 * @param int $int_date current date in UNIX timestamp
1638
 * @return string
1639
 */
1640
function date_iso8601($int_date) {
1641
    $date_mod     = date('Y-m-d\TH:i:s', $int_date);
1642
    $pre_timezone = date('O', $int_date);
1643
    $time_zone    = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2);
1644
    $date_mod .= $time_zone;
1645
    return $date_mod;
1646
}
1647
1648
/**
1649
 * return an obfuscated email address in line with $conf['mailguard'] setting
1650
 *
1651
 * @author Harry Fuecks <[email protected]>
1652
 * @author Christopher Smith <[email protected]>
1653
 *
1654
 * @param string $email email address
1655
 * @return string
1656
 */
1657
function obfuscate($email) {
1658
    global $conf;
1659
1660
    switch($conf['mailguard']) {
1661
        case 'visible' :
1662
            $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
1663
            return strtr($email, $obfuscate);
1664
1665
        case 'hex' :
1666
            $encode = '';
1667
            $len    = strlen($email);
1668
            for($x = 0; $x < $len; $x++) {
1669
                $encode .= '&#x'.bin2hex($email{$x}).';';
1670
            }
1671
            return $encode;
1672
1673
        case 'none' :
1674
        default :
1675
            return $email;
1676
    }
1677
}
1678
1679
/**
1680
 * Removes quoting backslashes
1681
 *
1682
 * @author Andreas Gohr <[email protected]>
1683
 *
1684
 * @param string $string
1685
 * @param string $char backslashed character
1686
 * @return string
1687
 */
1688
function unslash($string, $char = "'") {
1689
    return str_replace('\\'.$char, $char, $string);
1690
}
1691
1692
/**
1693
 * Convert php.ini shorthands to byte
1694
 *
1695
 * @author <gilthans dot NO dot SPAM at gmail dot com>
1696
 * @link   http://php.net/manual/en/ini.core.php#79564
1697
 *
1698
 * @param string $v shorthands
1699
 * @return int|string
1700
 */
1701
function php_to_byte($v) {
1702
    $l   = substr($v, -1);
1703
    $ret = substr($v, 0, -1);
1704
    switch(strtoupper($l)) {
1705
        /** @noinspection PhpMissingBreakStatementInspection */
1706
        case 'P':
1707
            $ret *= 1024;
1708
        /** @noinspection PhpMissingBreakStatementInspection */
1709
        case 'T':
1710
            $ret *= 1024;
1711
        /** @noinspection PhpMissingBreakStatementInspection */
1712
        case 'G':
1713
            $ret *= 1024;
1714
        /** @noinspection PhpMissingBreakStatementInspection */
1715
        case 'M':
1716
            $ret *= 1024;
1717
        /** @noinspection PhpMissingBreakStatementInspection */
1718
        case 'K':
1719
            $ret *= 1024;
1720
            break;
1721
        default;
1722
            $ret *= 10;
1723
            break;
1724
    }
1725
    return $ret;
1726
}
1727
1728
/**
1729
 * Wrapper around preg_quote adding the default delimiter
1730
 *
1731
 * @param string $string
1732
 * @return string
1733
 */
1734
function preg_quote_cb($string) {
1735
    return preg_quote($string, '/');
1736
}
1737
1738
/**
1739
 * Shorten a given string by removing data from the middle
1740
 *
1741
 * You can give the string in two parts, the first part $keep
1742
 * will never be shortened. The second part $short will be cut
1743
 * in the middle to shorten but only if at least $min chars are
1744
 * left to display it. Otherwise it will be left off.
1745
 *
1746
 * @param string $keep   the part to keep
1747
 * @param string $short  the part to shorten
1748
 * @param int    $max    maximum chars you want for the whole string
1749
 * @param int    $min    minimum number of chars to have left for middle shortening
1750
 * @param string $char   the shortening character to use
1751
 * @return string
1752
 */
1753
function shorten($keep, $short, $max, $min = 9, $char = '…') {
1754
    $max = $max - utf8_strlen($keep);
1755
    if($max < $min) return $keep;
1756
    $len = utf8_strlen($short);
1757
    if($len <= $max) return $keep.$short;
1758
    $half = floor($max / 2);
1759
    return $keep.utf8_substr($short, 0, $half - 1).$char.utf8_substr($short, $len - $half);
1760
}
1761
1762
/**
1763
 * Return the users real name or e-mail address for use
1764
 * in page footer and recent changes pages
1765
 *
1766
 * @param string|null $username or null when currently logged-in user should be used
1767
 * @param bool $textonly true returns only plain text, true allows returning html
1768
 * @return string html or plain text(not escaped) of formatted user name
1769
 *
1770
 * @author Andy Webber <dokuwiki AT andywebber DOT com>
1771
 */
1772
function editorinfo($username, $textonly = false) {
1773
    return userlink($username, $textonly);
1774
}
1775
1776
/**
1777
 * Returns users realname w/o link
1778
 *
1779
 * @param string|null $username or null when currently logged-in user should be used
1780
 * @param bool $textonly true returns only plain text, true allows returning html
1781
 * @return string html or plain text(not escaped) of formatted user name
1782
 *
1783
 * @triggers COMMON_USER_LINK
1784
 */
1785
function userlink($username = null, $textonly = false) {
1786
    global $conf, $INFO;
1787
    /** @var DokuWiki_Auth_Plugin $auth */
1788
    global $auth;
1789
    /** @var Input $INPUT */
1790
    global $INPUT;
1791
1792
    // prepare initial event data
1793
    $data = array(
1794
        'username' => $username, // the unique user name
1795
        'name' => '',
1796
        'link' => array( //setting 'link' to false disables linking
1797
                         'target' => '',
1798
                         'pre' => '',
1799
                         'suf' => '',
1800
                         'style' => '',
1801
                         'more' => '',
1802
                         'url' => '',
1803
                         'title' => '',
1804
                         'class' => ''
1805
        ),
1806
        'userlink' => '', // formatted user name as will be returned
1807
        'textonly' => $textonly
1808
    );
1809
    if($username === null) {
1810
        $data['username'] = $username = $INPUT->server->str('REMOTE_USER');
1811
        if($textonly){
1812
            $data['name'] = $INFO['userinfo']['name']. ' (' . $INPUT->server->str('REMOTE_USER') . ')';
1813
        }else {
1814
            $data['name'] = '<bdi>' . hsc($INFO['userinfo']['name']) . '</bdi> (<bdi>' . hsc($INPUT->server->str('REMOTE_USER')) . '</bdi>)';
1815
        }
1816
    }
1817
1818
    $evt = new Doku_Event('COMMON_USER_LINK', $data);
1819
    if($evt->advise_before(true)) {
1820
        if(empty($data['name'])) {
1821
            if($auth) $info = $auth->getUserData($username);
1822
            if($conf['showuseras'] != 'loginname' && isset($info) && $info) {
1823
                switch($conf['showuseras']) {
1824
                    case 'username':
1825
                    case 'username_link':
1826
                        $data['name'] = $textonly ? $info['name'] : hsc($info['name']);
1827
                        break;
1828
                    case 'email':
1829
                    case 'email_link':
1830
                        $data['name'] = obfuscate($info['mail']);
1831
                        break;
1832
                }
1833
            } else {
1834
                $data['name'] = $textonly ? $data['username'] : hsc($data['username']);
1835
            }
1836
        }
1837
1838
        /** @var Doku_Renderer_xhtml $xhtml_renderer */
1839
        static $xhtml_renderer = null;
1840
1841
        if(!$data['textonly'] && empty($data['link']['url'])) {
1842
1843
            if(in_array($conf['showuseras'], array('email_link', 'username_link'))) {
1844
                if(!isset($info)) {
1845
                    if($auth) $info = $auth->getUserData($username);
1846
                }
1847
                if(isset($info) && $info) {
1848
                    if($conf['showuseras'] == 'email_link') {
1849
                        $data['link']['url'] = 'mailto:' . obfuscate($info['mail']);
1850
                    } else {
1851
                        if(is_null($xhtml_renderer)) {
1852
                            $xhtml_renderer = p_get_renderer('xhtml');
1853
                        }
1854
                        if(empty($xhtml_renderer->interwiki)) {
1855
                            $xhtml_renderer->interwiki = getInterwiki();
1856
                        }
1857
                        $shortcut = 'user';
1858
                        $exists = null;
1859
                        $data['link']['url'] = $xhtml_renderer->_resolveInterWiki($shortcut, $username, $exists);
1860
                        $data['link']['class'] .= ' interwiki iw_user';
1861
                        if($exists !== null) {
1862
                            if($exists) {
1863
                                $data['link']['class'] .= ' wikilink1';
1864
                            } else {
1865
                                $data['link']['class'] .= ' wikilink2';
1866
                                $data['link']['rel'] = 'nofollow';
1867
                            }
1868
                        }
1869
                    }
1870
                } else {
1871
                    $data['textonly'] = true;
1872
                }
1873
1874
            } else {
1875
                $data['textonly'] = true;
1876
            }
1877
        }
1878
1879
        if($data['textonly']) {
1880
            $data['userlink'] = $data['name'];
1881
        } else {
1882
            $data['link']['name'] = $data['name'];
1883
            if(is_null($xhtml_renderer)) {
1884
                $xhtml_renderer = p_get_renderer('xhtml');
1885
            }
1886
            $data['userlink'] = $xhtml_renderer->_formatLink($data['link']);
1887
        }
1888
    }
1889
    $evt->advise_after();
1890
    unset($evt);
1891
1892
    return $data['userlink'];
1893
}
1894
1895
/**
1896
 * Returns the path to a image file for the currently chosen license.
1897
 * When no image exists, returns an empty string
1898
 *
1899
 * @author Andreas Gohr <[email protected]>
1900
 *
1901
 * @param  string $type - type of image 'badge' or 'button'
1902
 * @return string
1903
 */
1904
function license_img($type) {
1905
    global $license;
1906
    global $conf;
1907
    if(!$conf['license']) return '';
1908
    if(!is_array($license[$conf['license']])) return '';
1909
    $try   = array();
1910
    $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.png';
1911
    $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.gif';
1912
    if(substr($conf['license'], 0, 3) == 'cc-') {
1913
        $try[] = 'lib/images/license/'.$type.'/cc.png';
1914
    }
1915
    foreach($try as $src) {
1916
        if(file_exists(DOKU_INC.$src)) return $src;
1917
    }
1918
    return '';
1919
}
1920
1921
/**
1922
 * Checks if the given amount of memory is available
1923
 *
1924
 * If the memory_get_usage() function is not available the
1925
 * function just assumes $bytes of already allocated memory
1926
 *
1927
 * @author Filip Oscadal <[email protected]>
1928
 * @author Andreas Gohr <[email protected]>
1929
 *
1930
 * @param int  $mem    Size of memory you want to allocate in bytes
1931
 * @param int  $bytes  already allocated memory (see above)
1932
 * @return bool
1933
 */
1934
function is_mem_available($mem, $bytes = 1048576) {
1935
    $limit = trim(ini_get('memory_limit'));
1936
    if(empty($limit)) return true; // no limit set!
1937
1938
    // parse limit to bytes
1939
    $limit = php_to_byte($limit);
1940
1941
    // get used memory if possible
1942
    if(function_exists('memory_get_usage')) {
1943
        $used = memory_get_usage();
1944
    } else {
1945
        $used = $bytes;
1946
    }
1947
1948
    if($used + $mem > $limit) {
1949
        return false;
1950
    }
1951
1952
    return true;
1953
}
1954
1955
/**
1956
 * Send a HTTP redirect to the browser
1957
 *
1958
 * Works arround Microsoft IIS cookie sending bug. Exits the script.
1959
 *
1960
 * @link   http://support.microsoft.com/kb/q176113/
1961
 * @author Andreas Gohr <[email protected]>
1962
 *
1963
 * @param string $url url being directed to
1964
 */
1965
function send_redirect($url) {
1966
    $url = stripctl($url); // defend against HTTP Response Splitting
1967
1968
    /* @var Input $INPUT */
1969
    global $INPUT;
1970
1971
    //are there any undisplayed messages? keep them in session for display
1972
    global $MSG;
1973
    if(isset($MSG) && count($MSG) && !defined('NOSESSION')) {
1974
        //reopen session, store data and close session again
1975
        @session_start();
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1976
        $_SESSION[DOKU_COOKIE]['msg'] = $MSG;
1977
    }
1978
1979
    // always close the session
1980
    session_write_close();
1981
1982
    // check if running on IIS < 6 with CGI-PHP
1983
    if($INPUT->server->has('SERVER_SOFTWARE') && $INPUT->server->has('GATEWAY_INTERFACE') &&
1984
        (strpos($INPUT->server->str('GATEWAY_INTERFACE'), 'CGI') !== false) &&
1985
        (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($INPUT->server->str('SERVER_SOFTWARE')), $matches)) &&
1986
        $matches[1] < 6
1987
    ) {
1988
        header('Refresh: 0;url='.$url);
1989
    } else {
1990
        header('Location: '.$url);
1991
    }
1992
1993
    // no exits during unit tests
1994
    if(defined('DOKU_UNITTEST')) {
1995
        // pass info about the redirect back to the test suite
1996
        $testRequest = TestRequest::getRunning();
1997
        if($testRequest !== null) {
1998
            $testRequest->addData('send_redirect', $url);
1999
        }
2000
        return;
2001
    }
2002
2003
    exit;
2004
}
2005
2006
/**
2007
 * Validate a value using a set of valid values
2008
 *
2009
 * This function checks whether a specified value is set and in the array
2010
 * $valid_values. If not, the function returns a default value or, if no
2011
 * default is specified, throws an exception.
2012
 *
2013
 * @param string $param        The name of the parameter
2014
 * @param array  $valid_values A set of valid values; Optionally a default may
2015
 *                             be marked by the key “default”.
2016
 * @param array  $array        The array containing the value (typically $_POST
2017
 *                             or $_GET)
2018
 * @param string $exc          The text of the raised exception
2019
 *
2020
 * @throws Exception
2021
 * @return mixed
2022
 * @author Adrian Lang <[email protected]>
2023
 */
2024
function valid_input_set($param, $valid_values, $array, $exc = '') {
2025
    if(isset($array[$param]) && in_array($array[$param], $valid_values)) {
2026
        return $array[$param];
2027
    } elseif(isset($valid_values['default'])) {
2028
        return $valid_values['default'];
2029
    } else {
2030
        throw new Exception($exc);
2031
    }
2032
}
2033
2034
/**
2035
 * Read a preference from the DokuWiki cookie
2036
 * (remembering both keys & values are urlencoded)
2037
 *
2038
 * @param string $pref     preference key
2039
 * @param mixed  $default  value returned when preference not found
2040
 * @return string preference value
2041
 */
2042
function get_doku_pref($pref, $default) {
2043
    $enc_pref = urlencode($pref);
2044
    if(isset($_COOKIE['DOKU_PREFS']) && strpos($_COOKIE['DOKU_PREFS'], $enc_pref) !== false) {
2045
        $parts = explode('#', $_COOKIE['DOKU_PREFS']);
2046
        $cnt   = count($parts);
2047
        for($i = 0; $i < $cnt; $i += 2) {
2048
            if($parts[$i] == $enc_pref) {
2049
                return urldecode($parts[$i + 1]);
2050
            }
2051
        }
2052
    }
2053
    return $default;
2054
}
2055
2056
/**
2057
 * Add a preference to the DokuWiki cookie
2058
 * (remembering $_COOKIE['DOKU_PREFS'] is urlencoded)
2059
 * Remove it by setting $val to false
2060
 *
2061
 * @param string $pref  preference key
2062
 * @param string $val   preference value
2063
 */
2064
function set_doku_pref($pref, $val) {
2065
    global $conf;
2066
    $orig = get_doku_pref($pref, false);
2067
    $cookieVal = '';
2068
2069
    if($orig && ($orig != $val)) {
2070
        $parts = explode('#', $_COOKIE['DOKU_PREFS']);
2071
        $cnt   = count($parts);
2072
        // urlencode $pref for the comparison
2073
        $enc_pref = rawurlencode($pref);
2074
        for($i = 0; $i < $cnt; $i += 2) {
2075
            if($parts[$i] == $enc_pref) {
2076
                if ($val !== false) {
2077
                    $parts[$i + 1] = rawurlencode($val);
2078
                } else {
2079
                    unset($parts[$i]);
2080
                    unset($parts[$i + 1]);
2081
                }
2082
                break;
2083
            }
2084
        }
2085
        $cookieVal = implode('#', $parts);
2086
    } else if (!$orig && $val !== false) {
2087
        $cookieVal = ($_COOKIE['DOKU_PREFS'] ? $_COOKIE['DOKU_PREFS'].'#' : '').rawurlencode($pref).'#'.rawurlencode($val);
2088
    }
2089
2090
    if (!empty($cookieVal)) {
2091
        $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
2092
        setcookie('DOKU_PREFS', $cookieVal, time()+365*24*3600, $cookieDir, '', ($conf['securecookie'] && is_ssl()));
2093
    }
2094
}
2095
2096
/**
2097
 * Strips source mapping declarations from given text #601
2098
 *
2099
 * @param string &$text reference to the CSS or JavaScript code to clean
2100
 */
2101
function stripsourcemaps(&$text){
2102
    $text = preg_replace('/^(\/\/|\/\*)[@#]\s+sourceMappingURL=.*?(\*\/)?$/im', '\\1\\2', $text);
2103
}
2104
2105
/**
2106
 * Returns the contents of a given SVG file for embedding
2107
 *
2108
 * Inlining SVGs saves on HTTP requests and more importantly allows for styling them through
2109
 * CSS. However it should used with small SVGs only. The $maxsize setting ensures only small
2110
 * files are embedded.
2111
 *
2112
 * This strips unneeded headers, comments and newline. The result is not a vaild standalone SVG!
2113
 *
2114
 * @param string $file full path to the SVG file
2115
 * @param int $maxsize maximum allowed size for the SVG to be embedded
2116
 * @return string|false the SVG content, false if the file couldn't be loaded
2117
 */
2118
function inlineSVG($file, $maxsize = 2048) {
2119
    $file = trim($file);
2120
    if($file === '') return false;
2121
    if(!file_exists($file)) return false;
2122
    if(filesize($file) > $maxsize) return false;
2123
    if(!is_readable($file)) return false;
2124
    $content = file_get_contents($file);
2125
    $content = preg_replace('/<!--.*?(-->)/s','', $content); // comments
2126
    $content = preg_replace('/<\?xml .*?\?>/i', '', $content); // xml header
2127
    $content = preg_replace('/<!DOCTYPE .*?>/i', '', $content); // doc type
2128
    $content = preg_replace('/>\s+</s', '><', $content); // newlines between tags
2129
    $content = trim($content);
2130
    if(substr($content, 0, 5) !== '<svg ') return false;
2131
    return $content;
2132
}
2133
2134
//Setup VIM: ex: et ts=2 :
2135