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