scratch – Diff between revs 73 and 81
?pathlinks?
Rev 73 | Rev 81 | |||
---|---|---|---|---|
Line 16... | Line 16... | |||
16 | case 'POST': |
16 | case 'POST': |
|
17 | #### Retrieve uploaded file. |
17 | #### Retrieve uploaded file. |
|
18 | if (!empty($_FILES['file']) and |
18 | if (!empty($_FILES['file']) and |
|
19 | is_uploaded_file($_FILES['file']['tmp_name'])) { |
19 | is_uploaded_file($_FILES['file']['tmp_name'])) { |
|
20 | if($_FILES['file']['size'] > $config['ALLOWED_ASSET_SIZE'] * 1048576) { |
20 | if($_FILES['file']['size'] > $config['ALLOWED_ASSET_SIZE'] * 1048576) { |
|
- | 21 | http_response_code(403); |
||
21 | header('File size exceeds '.$config['ALLOWED_ASSET_SIZE'].'MiB.', true, 403); |
22 | die('File size exceeds '.$config['ALLOWED_ASSET_SIZE'].'MiB.'); |
|
22 | return; |
- | ||
23 | } |
23 | } |
|
24 | # Regular multipart/form-data upload. |
24 | # Regular multipart/form-data upload. |
|
25 | $name = $_FILES['file']['name']; |
25 | $name = $_FILES['file']['name']; |
|
26 | $data = atomized_get_contents($_FILES['file']['tmp_name']); |
26 | $data = atomized_get_contents($_FILES['file']['tmp_name']); |
|
27 | } else { |
27 | } else { |
|
28 | if((int)get_file_size("php://input") > $config['ALLOWED_ASSET_SIZE'] * 1048576) { |
28 | if((int)get_file_size("php://input") > $config['ALLOWED_ASSET_SIZE'] * 1048576) { |
|
- | 29 | http_response_code(403); |
||
29 | header('File size exceeds '.$config['ALLOWED_ASSET_SIZE'].'MiB.', true, 403); |
30 | die('File size exceeds '.$config['ALLOWED_ASSET_SIZE'].'MiB.'); |
|
30 | return; |
- | ||
31 | } |
31 | } |
|
32 | # Raw POST data. |
32 | # Raw POST data. |
|
33 | $name = urldecode(@$_SERVER['HTTP_X_FILE_NAME']); |
33 | $name = urldecode(@$_SERVER['HTTP_X_FILE_NAME']); |
|
34 | $data = atomized_get_contents("php://input"); |
34 | $data = atomized_get_contents("php://input"); |
|
35 | } |
35 | } |
|
Line 39... | Line 39... | |||
39 | |
39 | |
|
40 | #### If the extension is not allowed then change it to a text extension. |
40 | #### If the extension is not allowed then change it to a text extension. |
|
41 | if (!isset($fileExtension) || |
41 | if (!isset($fileExtension) || |
|
42 | !in_array(strtoupper($fileExtension), |
42 | !in_array(strtoupper($fileExtension), |
|
43 | array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) { |
43 | array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) { |
|
44 | header('File extension not allowed.', true, 403); |
44 | http_response_code(403); |
|
45 | return; |
45 | die('File extension not allowed.'); |
|
Line 46... | Line 46... | |||
46 | } |
46 | } |
|
47 | |
47 | |
|
48 | #### Hash filename. |
48 | #### Hash filename. |
|
Line 71... | Line 71... | |||
71 | |
71 | |
|
72 | #### Check for path traversals |
72 | #### Check for path traversals |
|
73 | $pathPart = pathinfo($userPath.'.'.$fileExtension); |
73 | $pathPart = pathinfo($userPath.'.'.$fileExtension); |
|
74 | if (strcasecmp( |
74 | if (strcasecmp( |
|
75 | realpath($pathPart['dirname']), realpath($config['STORE_FOLDER'])) != 0) { |
75 | realpath($pathPart['dirname']), realpath($config['STORE_FOLDER'])) != 0) { |
|
76 | header('Internal server error.', true, 500); |
76 | http_response_code(500); |
|
77 | return; |
77 | die('Internal server error.'); |
|
Line 78... | Line 78... | |||
78 | } |
78 | } |
|
79 | |
79 | |
|
Line 80... | Line 80... | |||
80 | #### Store the file. |
80 | #### Store the file. |
|
81 | atomized_put_contents($userPath.'.'.$fileExtension, $data); |
81 | $timestamp = atomized_put_contents($userPath.'.'.$fileExtension, $data); |
|
82 | |
82 | |
|
- | 83 | ### Return the URL to the file. |
||
- | 84 | header('Content-Type: text/plain; charset=utf-8'); |
||
- | 85 | echo json_encode( |
||
- | 86 | array( |
||
- | 87 | "hash" => $file, |
||
83 | ### Return the URL to the file. |
88 | "timestamp" => $timestamp |
|
84 | header('Content-Type: text/plain; charset=utf-8'); |
89 | ) |
|
85 | echo $file; |
90 | ); |
|
86 | break; |
91 | break; |
|
87 | case 'GET': |
92 | case 'GET': |
|
88 | ### If no file has been specified for download then return. |
93 | ### If no file has been specified for download then return. |
|
89 | if (!isset($_GET['o']) or empty($_GET['o'])) { |
94 | if (!isset($_GET['hash']) or empty($_GET['hash'])) { |
|
Line 90... | Line 95... | |||
90 | header('File not found.', true, 404); |
95 | http_response_code(404); |
|
91 | return; |
96 | die('File not found.'); |
|
92 | } |
97 | } |
|
93 | |
98 | |
|
94 | ### Find the requested file. |
99 | ### Find the requested file. |
|
95 | $file = array_shift( |
100 | $file = array_shift( |
|
96 | preg_grep( |
101 | preg_grep( |
|
Line 97... | Line 102... | |||
97 | "/$_GET[o]/", |
102 | '/'.$_GET['hash'].'/', |
|
- | 103 | scandir($config['STORE_FOLDER']) |
||
98 | scandir($config['STORE_FOLDER']) |
104 | ) |
|
- | 105 | ); |
||
Line 99... | Line 106... | |||
99 | ) |
106 | |
|
100 | ); |
107 | if (!isset($file) or empty($file)) { |
|
Line 101... | Line 108... | |||
101 | |
108 | http_response_code(404); |
|
102 | if (!isset($file) or empty($file)) |
109 | die('File not found.'); |
|
103 | return; |
110 | } |
|
104 | |
111 | |
|
105 | ### Check the path for path traversals. |
112 | ### Check the path for path traversals. |
|
106 | $fileExtension = pathinfo($file, PATHINFO_EXTENSION); |
113 | $fileExtension = pathinfo($file, PATHINFO_EXTENSION); |
|
107 | |
114 | |
|
Line 108... | Line 115... | |||
108 | #### If the extension is not allowed then return. |
115 | #### If the extension is not allowed then return. |
|
109 | if (!isset($fileExtension) || |
116 | if (!isset($fileExtension) || |
|
110 | !in_array(strtoupper($fileExtension), |
117 | !in_array(strtoupper($fileExtension), |
|
Line 124... | Line 131... | |||
124 | |
131 | |
|
125 | #### Check for path traversals |
132 | #### Check for path traversals |
|
126 | $pathPart = pathinfo($userPath); |
133 | $pathPart = pathinfo($userPath); |
|
127 | if (strcasecmp( |
134 | if (strcasecmp( |
|
128 | realpath($pathPart['dirname']), realpath($config['STORE_FOLDER'])) != 0) { |
135 | realpath($pathPart['dirname']), realpath($config['STORE_FOLDER'])) != 0) { |
|
129 | header('Internal server error.', true, 500); |
136 | http_response_code(500); |
|
130 | return; |
137 | die('Internal server error.'); |
|
Line 131... | Line 138... | |||
131 | } |
138 | } |
|
132 | |
139 | |
|
133 | ### Hook for HTML files to display properly. |
140 | ### Hook for HTML files to display properly. |
|
Line 139... | Line 146... | |||
139 | break; |
146 | break; |
|
140 | default: |
147 | default: |
|
141 | ### Open MIME info database and send the content type. |
148 | ### Open MIME info database and send the content type. |
|
142 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
149 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
|
143 | if (!$finfo) { |
150 | if (!$finfo) { |
|
144 | header('Internal server error.', true, 500); |
151 | http_response_code(500); |
|
145 | return; |
152 | die('Internal server error.'); |
|
146 | } |
153 | } |
|
147 | header('Content-type: '.finfo_file($finfo, $userPath)); |
154 | header('Content-type: '.finfo_file($finfo, $userPath)); |
|
148 | finfo_close($finfo); |
155 | finfo_close($finfo); |
|
149 | break; |
156 | break; |
|
150 | } |
157 | } |
|
Line -... | Line 158... | |||
- | 158 | |
||
- | 159 | ### Tell browser not to cache files. |
||
- | 160 | header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); |
||
- | 161 | header("Cache-Control: post-check=0, pre-check=0", false); |
||
151 | |
162 | header("Pragma: no-cache"); |
|
152 | ### Send the file along with the inline content disposition. |
163 | ### Send the file along with the inline content disposition. |
|
153 | header('Content-length: '.(int)get_file_size($userPath)); |
164 | header('Content-length: '.(int)get_file_size($userPath)); |
|
154 | header('Content-Disposition: inline; filename="' . basename($userPath) . '"'); |
165 | header('Content-Disposition: inline; filename="' . basename($userPath) . '"'); |
|
155 | header('Content-Transfer-Encoding: binary'); |
166 | header('Content-Transfer-Encoding: binary'); |