Passed
Push — 4.9 ( 8f0f3a...f6adb4 )
by Mikhail
01:57
created

sanitize_slashes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
function sanitize_filename($input)
4
{
5
    $input = sanitize_slashes($input);
6
    $output = '';
7
8
    while ($input !== '') {
9
        if (
10
            ($prefix = substr($input, 0, 3)) == '../' ||
11
            ($prefix = substr($input, 0, 2)) == './'
12
           ) {
13
            $input = substr($input, strlen($prefix));
14
        } else if (
15
            ($prefix = substr($input, 0, 3)) == '/./' ||
16
            ($prefix = $input) == '/.'
17
           ) {
18
            $input = '/' . substr($input, strlen($prefix));
19
        } else
20
21
        if (
22
            ($prefix = substr($input, 0, 4)) == '/../' ||
23
            ($prefix = $input) == '/..'
24
           ) {
25
            $input = '/' . substr($input, strlen($prefix));
26
            $output = substr($output, 0, strrpos($output, '/'));
27
        } else if ($input == '.' || $input == '..') {
28
            $input = '';
29
        } else {
30
            $pos = strpos($input, '/');
31
            if ($pos === 0) $pos = strpos($input, '/', $pos+1);
32
            if ($pos === false) $pos = strlen($input);
33
            $output .= substr($input, 0, $pos);
34
            $input = (string) substr($input, $pos);
35
        }
36
    }
37
38
    return $output;
39
}
40
41
function sanitize_slashes($filename)
42
{
43
    return str_replace('\\', '/', $filename);
44
}
45