|
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
|
|
|
|