scratch – Blame information for rev 140
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
7 | office | 1 | <?php |
2 | |||
3 | ########################################################################### |
||
4 | ## Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 ## |
||
5 | ########################################################################### |
||
6 | |||
66 | office | 7 | require_once('php/pseudocrypt.php'); |
8 | require_once('php/functions.php'); |
||
87 | office | 9 | require_once('vendor/autoload.php'); |
7 | office | 10 | |
67 | office | 11 | ### Load configuration. |
12 | $config = spyc_load_file('config.yaml'); |
||
13 | |||
52 | office | 14 | #### POST -> upload / GET -> download |
15 | switch ($_SERVER['REQUEST_METHOD']) { |
||
16 | case 'POST': |
||
93 | office | 17 | #### Script restrictions. |
96 | office | 18 | session_start(); |
19 | if (empty($_POST['token']) || !hash_equals($_SESSION['token'], $_POST['token'])) { |
||
93 | office | 20 | http_response_code(403); |
21 | die('Forbidden.'); |
||
22 | } |
||
52 | office | 23 | #### Retrieve uploaded file. |
24 | if (!empty($_FILES['file']) and |
||
25 | is_uploaded_file($_FILES['file']['tmp_name'])) { |
||
67 | office | 26 | if($_FILES['file']['size'] > $config['ALLOWED_ASSET_SIZE'] * 1048576) { |
81 | office | 27 | http_response_code(403); |
28 | die('File size exceeds '.$config['ALLOWED_ASSET_SIZE'].'MiB.'); |
||
57 | office | 29 | } |
52 | office | 30 | # Regular multipart/form-data upload. |
31 | $name = $_FILES['file']['name']; |
||
57 | office | 32 | $data = atomized_get_contents($_FILES['file']['tmp_name']); |
52 | office | 33 | } else { |
67 | office | 34 | if((int)get_file_size("php://input") > $config['ALLOWED_ASSET_SIZE'] * 1048576) { |
81 | office | 35 | http_response_code(403); |
36 | die('File size exceeds '.$config['ALLOWED_ASSET_SIZE'].'MiB.'); |
||
57 | office | 37 | } |
52 | office | 38 | # Raw POST data. |
39 | $name = urldecode(@$_SERVER['HTTP_X_FILE_NAME']); |
||
57 | office | 40 | $data = atomized_get_contents("php://input"); |
52 | office | 41 | } |
7 | office | 42 | |
52 | office | 43 | #### Grab the file extension. |
44 | $fileExtension = pathinfo($name, PATHINFO_EXTENSION); |
||
11 | office | 45 | |
52 | office | 46 | #### If the extension is not allowed then change it to a text extension. |
47 | if (!isset($fileExtension) || |
||
48 | !in_array(strtoupper($fileExtension), |
||
67 | office | 49 | array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) { |
81 | office | 50 | http_response_code(403); |
51 | die('File extension not allowed.'); |
||
52 | office | 52 | } |
14 | office | 53 | |
52 | office | 54 | #### Hash filename. |
55 | $file = strtolower( |
||
56 | PseudoCrypt::hash( |
||
57 | preg_replace( |
||
58 | '/\D/', |
||
59 | '', |
||
60 | hash( |
||
61 | 'sha512', |
||
62 | $data |
||
63 | ) |
||
64 | ), |
||
67 | office | 65 | $config['ASSET_HASH_SIZE'] |
7 | office | 66 | ) |
52 | office | 67 | ); |
14 | office | 68 | |
52 | office | 69 | #### Build the user path. |
70 | $userPath = join( |
||
71 | DIRECTORY_SEPARATOR, |
||
72 | array( |
||
67 | office | 73 | $config['STORE_FOLDER'], |
52 | office | 74 | $file |
75 | ) |
||
76 | ); |
||
11 | office | 77 | |
102 | office | 78 | #### Check for path traversals. |
52 | office | 79 | $pathPart = pathinfo($userPath.'.'.$fileExtension); |
80 | if (strcasecmp( |
||
67 | office | 81 | realpath($pathPart['dirname']), realpath($config['STORE_FOLDER'])) != 0) { |
81 | office | 82 | http_response_code(500); |
83 | die('Internal server error.'); |
||
52 | office | 84 | } |
7 | office | 85 | |
52 | office | 86 | #### Store the file. |
81 | office | 87 | $timestamp = atomized_put_contents($userPath.'.'.$fileExtension, $data); |
125 | office | 88 | |
140 | office | 89 | ### Log IP address. |
90 | $db = new PDO('sqlite:db/scratch.sqlite3'); |
||
91 | $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
92 | try { |
||
93 | $db->beginTransaction(); |
||
94 | |||
95 | ## Create tags table if it does not exist. |
||
96 | $db->query('CREATE TABLE IF NOT EXISTS "uploaders" ("hash" text NOT NULL COLLATE NOCASE, "ip" text COLLATE NOCASE, UNIQUE("hash") ON CONFLICT REPLACE)'); |
||
97 | |||
98 | $q = $db->prepare('REPLACE INTO "uploaders" ("hash", "ip") VALUES(:hash, :ip)'); |
||
99 | $q->bindParam(':hash', $file); |
||
100 | $q->bindParam(':ip', $_SERVER['REMOTE_ADDR']); |
||
101 | $q->execute(); |
||
102 | |||
103 | $db->commit(); |
||
104 | } catch (Exception $e) { |
||
105 | error_log($e); |
||
106 | ## Rollback. |
||
107 | $db->rollback(); |
||
108 | } |
||
109 | |||
125 | office | 110 | ### Process any sent tags. |
111 | if(isset($_POST['tags'])) { |
||
112 | $tags = json_decode( |
||
113 | stripslashes( |
||
114 | $_POST['tags'] |
||
115 | ) |
||
116 | ); |
||
117 | |||
118 | ## If we have any tags then insert them into the database. |
||
119 | if(!empty($tags)) { |
||
126 | office | 120 | ## Connect or create the scratch database. |
121 | $db = new PDO('sqlite:db/scratch.sqlite3'); |
||
122 | ## Set the error mode to exceptions. |
||
123 | $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
||
125 | office | 124 | try { |
126 | office | 125 | |
125 | office | 126 | $db->beginTransaction(); |
126 | office | 127 | |
128 | ## Create tags table if it does not exist. |
||
128 | office | 129 | $db->query('CREATE TABLE IF NOT EXISTS "tags" ("hash" text NOT NULL COLLATE NOCASE, "tag" text COLLATE NOCASE, UNIQUE("hash", "tag") ON CONFLICT REPLACE)'); |
126 | office | 130 | |
131 | ## Now add all the tags. |
||
125 | office | 132 | foreach($tags as $tag) { |
126 | office | 133 | $q = $db->prepare('REPLACE INTO "tags" ("hash", "tag") VALUES(:hash, :tag)'); |
134 | $q->bindParam(':hash', $file); |
||
135 | $q->bindParam(':tag', $tag); |
||
136 | $q->execute(); |
||
125 | office | 137 | } |
138 | $db->commit(); |
||
139 | } catch (Exception $e) { |
||
126 | office | 140 | error_log($e); |
125 | office | 141 | ## Rollback. |
142 | $db->rollback(); |
||
143 | } |
||
144 | } |
||
145 | } |
||
14 | office | 146 | |
90 | office | 147 | ### Hook for various file extensions. |
148 | $opengraph = FALSE; |
||
149 | switch(strtoupper($fileExtension)) { |
||
103 | office | 150 | case 'MP4': |
90 | office | 151 | case 'GIF': |
152 | $opengraph = TRUE; |
||
153 | break; |
||
154 | } |
||
125 | office | 155 | |
52 | office | 156 | ### Return the URL to the file. |
157 | header('Content-Type: text/plain; charset=utf-8'); |
||
81 | office | 158 | echo json_encode( |
159 | array( |
||
160 | "hash" => $file, |
||
90 | office | 161 | "timestamp" => $timestamp, |
162 | "opengraph" => $opengraph |
||
81 | office | 163 | ) |
164 | ); |
||
52 | office | 165 | break; |
166 | case 'GET': |
||
83 | office | 167 | ### Tell browser not to cache files. |
168 | header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); |
||
169 | header("Cache-Control: post-check=0, pre-check=0", false); |
||
170 | header("Pragma: no-cache"); |
||
171 | |||
52 | office | 172 | ### If no file has been specified for download then return. |
81 | office | 173 | if (!isset($_GET['hash']) or empty($_GET['hash'])) { |
174 | http_response_code(404); |
||
175 | die('File not found.'); |
||
52 | office | 176 | } |
177 | |||
53 | office | 178 | ### Find the requested file. |
52 | office | 179 | $file = array_shift( |
180 | preg_grep( |
||
81 | office | 181 | '/'.$_GET['hash'].'/', |
67 | office | 182 | scandir($config['STORE_FOLDER']) |
52 | office | 183 | ) |
184 | ); |
||
185 | |||
81 | office | 186 | if (!isset($file) or empty($file)) { |
187 | http_response_code(404); |
||
188 | die('File not found.'); |
||
189 | } |
||
53 | office | 190 | |
191 | ### Check the path for path traversals. |
||
192 | $fileExtension = pathinfo($file, PATHINFO_EXTENSION); |
||
52 | office | 193 | |
53 | office | 194 | #### If the extension is not allowed then return. |
195 | if (!isset($fileExtension) || |
||
196 | !in_array(strtoupper($fileExtension), |
||
67 | office | 197 | array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) { |
81 | office | 198 | http_response_code(403); |
199 | die('File extension not allowed.'); |
||
52 | office | 200 | } |
53 | office | 201 | |
202 | #### Build the user path. |
||
203 | $userPath = join( |
||
204 | DIRECTORY_SEPARATOR, |
||
205 | array( |
||
67 | office | 206 | $config['STORE_FOLDER'], |
53 | office | 207 | $file |
208 | ) |
||
209 | ); |
||
52 | office | 210 | |
53 | office | 211 | #### Check for path traversals |
212 | $pathPart = pathinfo($userPath); |
||
213 | if (strcasecmp( |
||
67 | office | 214 | realpath($pathPart['dirname']), realpath($config['STORE_FOLDER'])) != 0) { |
81 | office | 215 | http_response_code(500); |
216 | die('Internal server error.'); |
||
53 | office | 217 | } |
218 | |||
83 | office | 219 | ### Hook for various file extensions. |
53 | office | 220 | switch(strtoupper($fileExtension)) { |
221 | case "HTML": |
||
222 | case "HTM": |
||
223 | header('Content-type: text/html'); |
||
224 | break; |
||
225 | break; |
||
83 | office | 226 | case "URL": |
91 | office | 227 | if(preg_match( |
228 | "/URL=(https?:\/\/[\-_\.\+!\*'\(\),a-zA-Z0-9]+:?[0-9]{0,5}\/.*?)\n/", |
||
229 | file_get_contents($userPath), $matches)) { |
||
83 | office | 230 | header('Location: '.$matches[1]); |
231 | return; |
||
232 | } |
||
233 | break; |
||
53 | office | 234 | default: |
235 | ### Open MIME info database and send the content type. |
||
236 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
||
237 | if (!$finfo) { |
||
81 | office | 238 | http_response_code(500); |
239 | die('Internal server error.'); |
||
53 | office | 240 | } |
241 | header('Content-type: '.finfo_file($finfo, $userPath)); |
||
242 | finfo_close($finfo); |
||
243 | break; |
||
244 | } |
||
245 | |||
52 | office | 246 | ### Send the file along with the inline content disposition. |
53 | office | 247 | header('Content-length: '.(int)get_file_size($userPath)); |
248 | header('Content-Disposition: inline; filename="' . basename($userPath) . '"'); |
||
249 | header('Content-Transfer-Encoding: binary'); |
||
250 | header('X-Sendfile: '.$userPath); |
||
52 | office | 251 | break; |
252 | } |