scratch – Diff between revs 53 and 57
?pathlinks?
Rev 53 | Rev 57 | |||
---|---|---|---|---|
Line 12... | Line 12... | |||
12 | switch ($_SERVER['REQUEST_METHOD']) { |
12 | switch ($_SERVER['REQUEST_METHOD']) { |
|
13 | case 'POST': |
13 | case 'POST': |
|
14 | #### Retrieve uploaded file. |
14 | #### Retrieve uploaded file. |
|
15 | if (!empty($_FILES['file']) and |
15 | if (!empty($_FILES['file']) and |
|
16 | is_uploaded_file($_FILES['file']['tmp_name'])) { |
16 | is_uploaded_file($_FILES['file']['tmp_name'])) { |
|
- | 17 | if($_FILES['file']['size'] > $ALLOWED_ASSET_SIZE * 1048576) { |
||
- | 18 | header('File size exceeds '.$ALLOWED_ASSET_SIZE.'MiB.', true, 403); |
||
- | 19 | return; |
||
- | 20 | } |
||
17 | # Regular multipart/form-data upload. |
21 | # Regular multipart/form-data upload. |
|
18 | $name = $_FILES['file']['name']; |
22 | $name = $_FILES['file']['name']; |
|
19 | $data = file_get_contents($_FILES['file']['tmp_name']); |
23 | $data = atomized_get_contents($_FILES['file']['tmp_name']); |
|
20 | } else { |
24 | } else { |
|
- | 25 | if((int)get_file_size("php://input") > $ALLOWED_ASSET_SIZE * 1048576) { |
||
- | 26 | header('File size exceeds '.$ALLOWED_ASSET_SIZE.'MiB.', true, 403); |
||
- | 27 | return; |
||
- | 28 | } |
||
21 | # Raw POST data. |
29 | # Raw POST data. |
|
22 | $name = urldecode(@$_SERVER['HTTP_X_FILE_NAME']); |
30 | $name = urldecode(@$_SERVER['HTTP_X_FILE_NAME']); |
|
23 | $data = file_get_contents("php://input"); |
31 | $data = atomized_get_contents("php://input"); |
|
24 | } |
32 | } |
|
Line 25... | Line 33... | |||
25 | |
33 | |
|
26 | #### Grab the file extension. |
34 | #### Grab the file extension. |
|
Line 27... | Line 35... | |||
27 | $fileExtension = pathinfo($name, PATHINFO_EXTENSION); |
35 | $fileExtension = pathinfo($name, PATHINFO_EXTENSION); |
|
28 | |
36 | |
|
29 | #### If the extension is not allowed then change it to a text extension. |
37 | #### If the extension is not allowed then change it to a text extension. |
|
30 | if (!isset($fileExtension) || |
38 | if (!isset($fileExtension) || |
|
31 | !in_array(strtoupper($fileExtension), |
39 | !in_array(strtoupper($fileExtension), |
|
32 | array_map('strtoupper', $ALLOWED_FILE_EXTENSIONS))) { |
40 | array_map('strtoupper', $ALLOWED_FILE_EXTENSIONS))) { |
|
33 | header("HTTP/1.1 500 Internal Server Error", true, 500); |
41 | header('File extension not allowed.', true, 403); |
|
Line 34... | Line 42... | |||
34 | return; |
42 | return; |
|
35 | } |
43 | } |
|
Line 60... | Line 68... | |||
60 | |
68 | |
|
61 | #### Check for path traversals |
69 | #### Check for path traversals |
|
62 | $pathPart = pathinfo($userPath.'.'.$fileExtension); |
70 | $pathPart = pathinfo($userPath.'.'.$fileExtension); |
|
63 | if (strcasecmp( |
71 | if (strcasecmp( |
|
- | 72 | realpath($pathPart['dirname']), realpath($STORE_FOLDER)) != 0) { |
||
64 | realpath($pathPart['dirname']), realpath($STORE_FOLDER)) != 0) { |
73 | header('Internal server error.', true, 500); |
|
65 | return; |
74 | return; |
|
Line 66... | Line 75... | |||
66 | } |
75 | } |
|
67 | |
76 | |
|
Line 73... | Line 82... | |||
73 | echo sprintf('%s/%s', trim($URL_PATH, '/'), $file); |
82 | echo sprintf('%s/%s', trim($URL_PATH, '/'), $file); |
|
74 | break; |
83 | break; |
|
75 | case 'GET': |
84 | case 'GET': |
|
76 | ### If no file has been specified for download then return. |
85 | ### If no file has been specified for download then return. |
|
77 | if (!isset($_GET['o']) or empty($_GET['o'])) { |
86 | if (!isset($_GET['o']) or empty($_GET['o'])) { |
|
78 | http_response_code(404); |
87 | header('File not found.', true, 404); |
|
79 | return; |
88 | return; |
|
80 | } |
89 | } |
|
Line 81... | Line 90... | |||
81 | |
90 | |
|
82 | ### Find the requested file. |
91 | ### Find the requested file. |
|
Line 95... | Line 104... | |||
95 | |
104 | |
|
96 | #### If the extension is not allowed then return. |
105 | #### If the extension is not allowed then return. |
|
97 | if (!isset($fileExtension) || |
106 | if (!isset($fileExtension) || |
|
98 | !in_array(strtoupper($fileExtension), |
107 | !in_array(strtoupper($fileExtension), |
|
99 | array_map('strtoupper', $ALLOWED_FILE_EXTENSIONS))) { |
108 | array_map('strtoupper', $ALLOWED_FILE_EXTENSIONS))) { |
|
100 | header("HTTP/1.1 500 Internal Server Error", true, 500); |
109 | header('File extension not allowed.', true, 403); |
|
101 | return; |
110 | return; |
|
Line 102... | Line 111... | |||
102 | } |
111 | } |
|
103 | |
112 | |
|
Line 112... | Line 121... | |||
112 | |
121 | |
|
113 | #### Check for path traversals |
122 | #### Check for path traversals |
|
114 | $pathPart = pathinfo($userPath); |
123 | $pathPart = pathinfo($userPath); |
|
115 | if (strcasecmp( |
124 | if (strcasecmp( |
|
- | 125 | realpath($pathPart['dirname']), realpath($STORE_FOLDER)) != 0) { |
||
116 | realpath($pathPart['dirname']), realpath($STORE_FOLDER)) != 0) { |
126 | header('Internal server error.', true, 500); |
|
117 | return; |
127 | return; |
|
Line 118... | Line 128... | |||
118 | } |
128 | } |
|
119 | |
129 | |
|
Line 126... | Line 136... | |||
126 | break; |
136 | break; |
|
127 | default: |
137 | default: |
|
128 | ### Open MIME info database and send the content type. |
138 | ### Open MIME info database and send the content type. |
|
129 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
139 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
|
130 | if (!$finfo) { |
140 | if (!$finfo) { |
|
131 | http_response_code(500); |
141 | header('Internal server error.', true, 500); |
|
132 | return; |
142 | return; |
|
133 | } |
143 | } |
|
134 | |
- | ||
135 | header('Content-type: '.finfo_file($finfo, $userPath)); |
144 | header('Content-type: '.finfo_file($finfo, $userPath)); |
|
136 | finfo_close($finfo); |
145 | finfo_close($finfo); |
|
137 | break; |
146 | break; |
|
138 | } |
147 | } |