scratch – Diff between revs 94 and 96
?pathlinks?
Rev 94 | Rev 96 | |||
---|---|---|---|---|
1 | <?php |
1 | <?php |
|
2 | |
2 | |
|
3 | ########################################################################### |
3 | ########################################################################### |
|
4 | ## Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 ## |
4 | ## Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 ## |
|
5 | ########################################################################### |
5 | ########################################################################### |
|
6 | |
6 | |
|
7 | require_once('php/pseudocrypt.php'); |
7 | require_once('php/pseudocrypt.php'); |
|
8 | require_once('php/functions.php'); |
8 | require_once('php/functions.php'); |
|
9 | require_once('vendor/autoload.php'); |
9 | require_once('vendor/autoload.php'); |
|
10 | |
10 | |
|
11 | ### Load configuration. |
11 | ### Load configuration. |
|
12 | $config = spyc_load_file('config.yaml'); |
12 | $config = spyc_load_file('config.yaml'); |
|
13 | |
13 | |
|
14 | #### POST -> upload / GET -> download |
14 | #### POST -> upload / GET -> download |
|
15 | switch ($_SERVER['REQUEST_METHOD']) { |
15 | switch ($_SERVER['REQUEST_METHOD']) { |
|
16 | case 'POST': |
16 | case 'POST': |
|
17 | #### Script restrictions. |
17 | #### Script restrictions. |
|
18 | if( |
- | ||
19 | ( |
- | ||
20 | !isset($_SERVER['HTTP_X_REQUESTED_WITH']) or |
- | ||
21 | empty($_SERVER['HTTP_X_REQUESTED_WITH']) or |
- | ||
22 | strtoupper($_SERVER['HTTP_X_REQUESTED_WITH']) != 'XMLHTTPREQUEST' |
- | ||
23 | ) |
- | ||
24 | or |
- | ||
25 | ( |
- | ||
26 | ( |
- | ||
27 | !isset($_SERVER['HTTP_REFERER']) or |
- | ||
28 | empty($_SERVER['HTTP_REFERER']) |
- | ||
29 | ) |
- | ||
30 | and |
18 | session_start(); |
|
31 | ( |
- | ||
32 | strtoupper($_SERVER['HTTP_REFERER']) != strtoupper($config['URL_PATH'].'FILE.HTML') or |
19 | if (empty($_POST['token']) || !hash_equals($_SESSION['token'], $_POST['token'])) { |
|
33 | strtoupper($_SERVER['HTTP_REFERER']) != strtoupper($config['URL_PATH'].'TEXT.HTML') |
- | ||
34 | ) |
- | ||
35 | ) |
- | ||
36 | ) |
- | ||
37 | { |
- | ||
38 | http_response_code(403); |
20 | http_response_code(403); |
|
39 | die('Forbidden.'); |
21 | die('Forbidden.'); |
|
40 | } |
22 | } |
|
41 | #### Retrieve uploaded file. |
23 | #### Retrieve uploaded file. |
|
42 | if (!empty($_FILES['file']) and |
24 | if (!empty($_FILES['file']) and |
|
43 | is_uploaded_file($_FILES['file']['tmp_name'])) { |
25 | is_uploaded_file($_FILES['file']['tmp_name'])) { |
|
44 | if($_FILES['file']['size'] > $config['ALLOWED_ASSET_SIZE'] * 1048576) { |
26 | if($_FILES['file']['size'] > $config['ALLOWED_ASSET_SIZE'] * 1048576) { |
|
45 | http_response_code(403); |
27 | http_response_code(403); |
|
46 | die('File size exceeds '.$config['ALLOWED_ASSET_SIZE'].'MiB.'); |
28 | die('File size exceeds '.$config['ALLOWED_ASSET_SIZE'].'MiB.'); |
|
47 | } |
29 | } |
|
48 | # Regular multipart/form-data upload. |
30 | # Regular multipart/form-data upload. |
|
49 | $name = $_FILES['file']['name']; |
31 | $name = $_FILES['file']['name']; |
|
50 | $data = atomized_get_contents($_FILES['file']['tmp_name']); |
32 | $data = atomized_get_contents($_FILES['file']['tmp_name']); |
|
51 | } else { |
33 | } else { |
|
52 | if((int)get_file_size("php://input") > $config['ALLOWED_ASSET_SIZE'] * 1048576) { |
34 | if((int)get_file_size("php://input") > $config['ALLOWED_ASSET_SIZE'] * 1048576) { |
|
53 | http_response_code(403); |
35 | http_response_code(403); |
|
54 | die('File size exceeds '.$config['ALLOWED_ASSET_SIZE'].'MiB.'); |
36 | die('File size exceeds '.$config['ALLOWED_ASSET_SIZE'].'MiB.'); |
|
55 | } |
37 | } |
|
56 | # Raw POST data. |
38 | # Raw POST data. |
|
57 | $name = urldecode(@$_SERVER['HTTP_X_FILE_NAME']); |
39 | $name = urldecode(@$_SERVER['HTTP_X_FILE_NAME']); |
|
58 | $data = atomized_get_contents("php://input"); |
40 | $data = atomized_get_contents("php://input"); |
|
59 | } |
41 | } |
|
60 | |
42 | |
|
61 | #### Grab the file extension. |
43 | #### Grab the file extension. |
|
62 | $fileExtension = pathinfo($name, PATHINFO_EXTENSION); |
44 | $fileExtension = pathinfo($name, PATHINFO_EXTENSION); |
|
63 | |
45 | |
|
64 | #### If the extension is not allowed then change it to a text extension. |
46 | #### If the extension is not allowed then change it to a text extension. |
|
65 | if (!isset($fileExtension) || |
47 | if (!isset($fileExtension) || |
|
66 | !in_array(strtoupper($fileExtension), |
48 | !in_array(strtoupper($fileExtension), |
|
67 | array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) { |
49 | array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) { |
|
68 | http_response_code(403); |
50 | http_response_code(403); |
|
69 | die('File extension not allowed.'); |
51 | die('File extension not allowed.'); |
|
70 | } |
52 | } |
|
71 | |
53 | |
|
72 | #### Hash filename. |
54 | #### Hash filename. |
|
73 | $file = strtolower( |
55 | $file = strtolower( |
|
74 | PseudoCrypt::hash( |
56 | PseudoCrypt::hash( |
|
75 | preg_replace( |
57 | preg_replace( |
|
76 | '/\D/', |
58 | '/\D/', |
|
77 | '', |
59 | '', |
|
78 | hash( |
60 | hash( |
|
79 | 'sha512', |
61 | 'sha512', |
|
80 | $data |
62 | $data |
|
81 | ) |
63 | ) |
|
82 | ), |
64 | ), |
|
83 | $config['ASSET_HASH_SIZE'] |
65 | $config['ASSET_HASH_SIZE'] |
|
84 | ) |
66 | ) |
|
85 | ); |
67 | ); |
|
86 | |
68 | |
|
87 | #### Build the user path. |
69 | #### Build the user path. |
|
88 | $userPath = join( |
70 | $userPath = join( |
|
89 | DIRECTORY_SEPARATOR, |
71 | DIRECTORY_SEPARATOR, |
|
90 | array( |
72 | array( |
|
91 | $config['STORE_FOLDER'], |
73 | $config['STORE_FOLDER'], |
|
92 | $file |
74 | $file |
|
93 | ) |
75 | ) |
|
94 | ); |
76 | ); |
|
95 | |
77 | |
|
96 | #### Check for path traversals |
78 | #### Check for path traversals |
|
97 | $pathPart = pathinfo($userPath.'.'.$fileExtension); |
79 | $pathPart = pathinfo($userPath.'.'.$fileExtension); |
|
98 | if (strcasecmp( |
80 | if (strcasecmp( |
|
99 | realpath($pathPart['dirname']), realpath($config['STORE_FOLDER'])) != 0) { |
81 | realpath($pathPart['dirname']), realpath($config['STORE_FOLDER'])) != 0) { |
|
100 | http_response_code(500); |
82 | http_response_code(500); |
|
101 | die('Internal server error.'); |
83 | die('Internal server error.'); |
|
102 | } |
84 | } |
|
103 | |
85 | |
|
104 | #### Store the file. |
86 | #### Store the file. |
|
105 | $timestamp = atomized_put_contents($userPath.'.'.$fileExtension, $data); |
87 | $timestamp = atomized_put_contents($userPath.'.'.$fileExtension, $data); |
|
106 | |
88 | |
|
107 | ### Hook for various file extensions. |
89 | ### Hook for various file extensions. |
|
108 | $opengraph = FALSE; |
90 | $opengraph = FALSE; |
|
109 | switch(strtoupper($fileExtension)) { |
91 | switch(strtoupper($fileExtension)) { |
|
110 | case 'GIF': |
92 | case 'GIF': |
|
111 | $opengraph = TRUE; |
93 | $opengraph = TRUE; |
|
112 | break; |
94 | break; |
|
113 | } |
95 | } |
|
114 | |
96 | |
|
115 | ### Return the URL to the file. |
97 | ### Return the URL to the file. |
|
116 | header('Content-Type: text/plain; charset=utf-8'); |
98 | header('Content-Type: text/plain; charset=utf-8'); |
|
117 | echo json_encode( |
99 | echo json_encode( |
|
118 | array( |
100 | array( |
|
119 | "hash" => $file, |
101 | "hash" => $file, |
|
120 | "timestamp" => $timestamp, |
102 | "timestamp" => $timestamp, |
|
121 | "opengraph" => $opengraph |
103 | "opengraph" => $opengraph |
|
122 | ) |
104 | ) |
|
123 | ); |
105 | ); |
|
124 | break; |
106 | break; |
|
125 | case 'GET': |
107 | case 'GET': |
|
126 | ### Tell browser not to cache files. |
108 | ### Tell browser not to cache files. |
|
127 | header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); |
109 | header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); |
|
128 | header("Cache-Control: post-check=0, pre-check=0", false); |
110 | header("Cache-Control: post-check=0, pre-check=0", false); |
|
129 | header("Pragma: no-cache"); |
111 | header("Pragma: no-cache"); |
|
130 | |
112 | |
|
131 | ### If no file has been specified for download then return. |
113 | ### If no file has been specified for download then return. |
|
132 | if (!isset($_GET['hash']) or empty($_GET['hash'])) { |
114 | if (!isset($_GET['hash']) or empty($_GET['hash'])) { |
|
133 | http_response_code(404); |
115 | http_response_code(404); |
|
134 | die('File not found.'); |
116 | die('File not found.'); |
|
135 | } |
117 | } |
|
136 | |
118 | |
|
137 | ### Find the requested file. |
119 | ### Find the requested file. |
|
138 | $file = array_shift( |
120 | $file = array_shift( |
|
139 | preg_grep( |
121 | preg_grep( |
|
140 | '/'.$_GET['hash'].'/', |
122 | '/'.$_GET['hash'].'/', |
|
141 | scandir($config['STORE_FOLDER']) |
123 | scandir($config['STORE_FOLDER']) |
|
142 | ) |
124 | ) |
|
143 | ); |
125 | ); |
|
144 | |
126 | |
|
145 | if (!isset($file) or empty($file)) { |
127 | if (!isset($file) or empty($file)) { |
|
146 | http_response_code(404); |
128 | http_response_code(404); |
|
147 | die('File not found.'); |
129 | die('File not found.'); |
|
148 | } |
130 | } |
|
149 | |
131 | |
|
150 | ### Check the path for path traversals. |
132 | ### Check the path for path traversals. |
|
151 | $fileExtension = pathinfo($file, PATHINFO_EXTENSION); |
133 | $fileExtension = pathinfo($file, PATHINFO_EXTENSION); |
|
152 | |
134 | |
|
153 | #### If the extension is not allowed then return. |
135 | #### If the extension is not allowed then return. |
|
154 | if (!isset($fileExtension) || |
136 | if (!isset($fileExtension) || |
|
155 | !in_array(strtoupper($fileExtension), |
137 | !in_array(strtoupper($fileExtension), |
|
156 | array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) { |
138 | array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) { |
|
157 | http_response_code(403); |
139 | http_response_code(403); |
|
158 | die('File extension not allowed.'); |
140 | die('File extension not allowed.'); |
|
159 | } |
141 | } |
|
160 | |
142 | |
|
161 | #### Build the user path. |
143 | #### Build the user path. |
|
162 | $userPath = join( |
144 | $userPath = join( |
|
163 | DIRECTORY_SEPARATOR, |
145 | DIRECTORY_SEPARATOR, |
|
164 | array( |
146 | array( |
|
165 | $config['STORE_FOLDER'], |
147 | $config['STORE_FOLDER'], |
|
166 | $file |
148 | $file |
|
167 | ) |
149 | ) |
|
168 | ); |
150 | ); |
|
169 | |
151 | |
|
170 | #### Check for path traversals |
152 | #### Check for path traversals |
|
171 | $pathPart = pathinfo($userPath); |
153 | $pathPart = pathinfo($userPath); |
|
172 | if (strcasecmp( |
154 | if (strcasecmp( |
|
173 | realpath($pathPart['dirname']), realpath($config['STORE_FOLDER'])) != 0) { |
155 | realpath($pathPart['dirname']), realpath($config['STORE_FOLDER'])) != 0) { |
|
174 | http_response_code(500); |
156 | http_response_code(500); |
|
175 | die('Internal server error.'); |
157 | die('Internal server error.'); |
|
176 | } |
158 | } |
|
177 | |
159 | |
|
178 | ### Hook for various file extensions. |
160 | ### Hook for various file extensions. |
|
179 | switch(strtoupper($fileExtension)) { |
161 | switch(strtoupper($fileExtension)) { |
|
180 | case "HTML": |
162 | case "HTML": |
|
181 | case "HTM": |
163 | case "HTM": |
|
182 | header('Content-type: text/html'); |
164 | header('Content-type: text/html'); |
|
183 | break; |
165 | break; |
|
184 | break; |
166 | break; |
|
185 | case "URL": |
167 | case "URL": |
|
186 | if(preg_match( |
168 | if(preg_match( |
|
187 | "/URL=(https?:\/\/[\-_\.\+!\*'\(\),a-zA-Z0-9]+:?[0-9]{0,5}\/.*?)\n/", |
169 | "/URL=(https?:\/\/[\-_\.\+!\*'\(\),a-zA-Z0-9]+:?[0-9]{0,5}\/.*?)\n/", |
|
188 | file_get_contents($userPath), $matches)) { |
170 | file_get_contents($userPath), $matches)) { |
|
189 | header('Location: '.$matches[1]); |
171 | header('Location: '.$matches[1]); |
|
190 | return; |
172 | return; |
|
191 | } |
173 | } |
|
192 | break; |
174 | break; |
|
193 | default: |
175 | default: |
|
194 | ### Open MIME info database and send the content type. |
176 | ### Open MIME info database and send the content type. |
|
195 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
177 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
|
196 | if (!$finfo) { |
178 | if (!$finfo) { |
|
197 | http_response_code(500); |
179 | http_response_code(500); |
|
198 | die('Internal server error.'); |
180 | die('Internal server error.'); |
|
199 | } |
181 | } |
|
200 | header('Content-type: '.finfo_file($finfo, $userPath)); |
182 | header('Content-type: '.finfo_file($finfo, $userPath)); |
|
201 | finfo_close($finfo); |
183 | finfo_close($finfo); |
|
202 | break; |
184 | break; |
|
203 | } |
185 | } |
|
204 | |
186 | |
|
205 | ### Send the file along with the inline content disposition. |
187 | ### Send the file along with the inline content disposition. |
|
206 | header('Content-length: '.(int)get_file_size($userPath)); |
188 | header('Content-length: '.(int)get_file_size($userPath)); |
|
207 | header('Content-Disposition: inline; filename="' . basename($userPath) . '"'); |
189 | header('Content-Disposition: inline; filename="' . basename($userPath) . '"'); |
|
208 | header('Content-Transfer-Encoding: binary'); |
190 | header('Content-Transfer-Encoding: binary'); |
|
209 | header('X-Sendfile: '.$userPath); |
191 | header('X-Sendfile: '.$userPath); |
|
210 | break; |
192 | break; |
|
211 | } |
193 | } |
|
212 | |
194 | |