scratch – Diff between revs 49 and 52
?pathlinks?
Rev 49 | Rev 52 | |||
---|---|---|---|---|
Line 6... | Line 6... | |||
6 | |
6 | |
|
7 | require_once('inc/pseudocrypt.php'); |
7 | require_once('inc/pseudocrypt.php'); |
|
8 | require_once('inc/functions.php'); |
8 | require_once('inc/functions.php'); |
|
Line -... | Line 9... | |||
- | 9 | require_once('config.php'); |
||
- | 10 | |
||
- | 11 | #### POST -> upload / GET -> download |
||
9 | require_once('config.php'); |
12 | switch ($_SERVER['REQUEST_METHOD']) { |
|
10 | |
13 | case 'POST': |
|
11 | #### Retrieve uploaded file. |
14 | #### Retrieve uploaded file. |
|
12 | if (!empty($_FILES['file']) and |
15 | if (!empty($_FILES['file']) and |
|
13 | is_uploaded_file($_FILES['file']['tmp_name'])) { |
16 | is_uploaded_file($_FILES['file']['tmp_name'])) { |
|
14 | # Regular multipart/form-data upload. |
17 | # Regular multipart/form-data upload. |
|
15 | $name = $_FILES['file']['name']; |
18 | $name = $_FILES['file']['name']; |
|
16 | $data = file_get_contents($_FILES['file']['tmp_name']); |
19 | $data = file_get_contents($_FILES['file']['tmp_name']); |
|
17 | } else { |
20 | } else { |
|
18 | # Raw POST data. |
21 | # Raw POST data. |
|
- | 22 | $name = urldecode(@$_SERVER['HTTP_X_FILE_NAME']); |
||
- | 23 | $data = file_get_contents("php://input"); |
||
- | 24 | } |
||
- | 25 | |
||
- | 26 | #### Grab the file extension. |
||
- | 27 | $fileExtension = pathinfo($name, PATHINFO_EXTENSION); |
||
- | 28 | |
||
- | 29 | #### If the extension is not allowed then change it to a text extension. |
||
- | 30 | if (!isset($fileExtension) || |
||
- | 31 | !in_array(strtoupper($fileExtension), |
||
- | 32 | array_map('strtoupper', $ALLOWED_FILE_EXTENSIONS))) { |
||
- | 33 | header("HTTP/1.1 500 Internal Server Error", true, 500); |
||
19 | $name = urldecode(@$_SERVER['HTTP_X_FILE_NAME']); |
34 | return; |
|
- | 35 | } |
||
- | 36 | |
||
- | 37 | #### Hash filename. |
||
- | 38 | $file = strtolower( |
||
- | 39 | PseudoCrypt::hash( |
||
- | 40 | preg_replace( |
||
- | 41 | '/\D/', |
||
- | 42 | '', |
||
- | 43 | hash( |
||
- | 44 | 'sha512', |
||
- | 45 | $data |
||
- | 46 | ) |
||
- | 47 | ), |
||
- | 48 | $ASSET_HASH_SIZE |
||
Line 20... | Line 49... | |||
20 | $data = file_get_contents("php://input"); |
49 | ) |
|
- | 50 | ); |
||
21 | } |
51 | |
|
- | 52 | #### Build the user path. |
||
- | 53 | $userPath = join( |
||
- | 54 | DIRECTORY_SEPARATOR, |
||
- | 55 | array( |
||
- | 56 | $STORE_FOLDER, |
||
Line 22... | Line 57... | |||
22 | |
57 | $file |
|
23 | #### Grab the file extension. |
58 | ) |
|
24 | $fileExtension = pathinfo($name, PATHINFO_EXTENSION); |
59 | ); |
|
25 | |
60 | |
|
26 | #### If the extension is not allowed then change it to a text extension. |
61 | #### Check for path traversals |
|
27 | if (!isset($fileExtension) || |
62 | $pathPart = pathinfo($userPath.'.'.$fileExtension); |
|
28 | !in_array(strtoupper($fileExtension), |
63 | if (strcasecmp( |
|
- | 64 | realpath($pathPart['dirname']), realpath($STORE_FOLDER)) != 0) { |
||
- | 65 | return; |
||
29 | array_map('strtoupper', $ALLOWED_FILE_EXTENSIONS))) { |
66 | } |
|
30 | header("HTTP/1.1 500 Internal Server Error", true, 500); |
67 | |
|
- | 68 | #### Store the file. |
||
- | 69 | atomized_put_contents($userPath.'.'.$fileExtension, $data); |
||
31 | return; |
70 | |
|
32 | } |
71 | ### Return the URL to the file. |
|
- | 72 | header('Content-Type: text/plain; charset=utf-8'); |
||
- | 73 | echo sprintf('%s/%s', trim($URL_PATH, '/'), $file); |
||
33 | |
74 | break; |
|
34 | #### Hash filename. |
75 | case 'GET': |
|
35 | $file = strtolower( |
76 | ### If no file has been specified for download then return. |
|
- | 77 | if (!isset($_GET['o']) or empty($_GET['o'])) { |
||
- | 78 | http_response_code(404); |
||
36 | PseudoCrypt::hash( |
79 | return; |
|
37 | preg_replace( |
80 | } |
|
38 | '/\D/', |
81 | |
|
39 | '', |
82 | $file = array_shift( |
|
40 | hash( |
83 | preg_grep( |
|
41 | 'sha512', |
- | ||
42 | $data |
- | ||
43 | ) |
- | ||
44 | ), |
84 | "/$_GET[o]/", |
|
45 | $ASSET_HASH_SIZE |
- | ||
46 | ) |
- | ||
47 | ); |
- | ||
48 | |
- | ||
49 | #### Build the user path. |
85 | scandir($STORE_FOLDER) |
|
50 | $userPath = join( |
86 | ) |
|
51 | DIRECTORY_SEPARATOR, |
- | ||
52 | array( |
- | ||
53 | $STORE_FOLDER, |
87 | ); |
|
54 | $file |
88 | |
|
55 | ) |
89 | if (!isset($file) or empty($file)) |
|
56 | ); |
90 | return; |
|
57 | |
91 | |
|
58 | #### Check for path traversals |
92 | ### Open MIME info database and send the content type. |
|
- | 93 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
||
59 | $pathPart = pathinfo($userPath.'.'.$fileExtension); |
94 | if (!$finfo) { |
|
60 | if (strcasecmp( |
95 | http_response_code(500); |
|
61 | realpath($pathPart['dirname']), realpath($STORE_FOLDER)) != 0) |
96 | return; |
|
62 | return; |
97 | } |
|
63 | |
98 | |
|
64 | #### Store the file. |
99 | header('Content-type: '.finfo_file($finfo, $STORE_FOLDER.'/'.$file)); |
|
- | 100 | finfo_close($finfo); |
||
65 | atomized_put_contents($userPath.'.'.$fileExtension, $data); |
101 | |
|
- | 102 | ### Send the file along with the inline content disposition. |
||
- | 103 | header('Content-length: '.(int)get_file_size($STORE_FOLDER.'/'.$file)); |