scratch – Blame information for rev 103
?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); |
14 | office | 88 | |
90 | office | 89 | ### Hook for various file extensions. |
90 | $opengraph = FALSE; |
||
91 | switch(strtoupper($fileExtension)) { |
||
103 | office | 92 | case 'MP4': |
90 | office | 93 | case 'GIF': |
94 | $opengraph = TRUE; |
||
95 | break; |
||
96 | } |
||
97 | |||
52 | office | 98 | ### Return the URL to the file. |
99 | header('Content-Type: text/plain; charset=utf-8'); |
||
81 | office | 100 | echo json_encode( |
101 | array( |
||
102 | "hash" => $file, |
||
90 | office | 103 | "timestamp" => $timestamp, |
104 | "opengraph" => $opengraph |
||
81 | office | 105 | ) |
106 | ); |
||
52 | office | 107 | break; |
108 | case 'GET': |
||
83 | office | 109 | ### Tell browser not to cache files. |
110 | header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); |
||
111 | header("Cache-Control: post-check=0, pre-check=0", false); |
||
112 | header("Pragma: no-cache"); |
||
113 | |||
52 | office | 114 | ### If no file has been specified for download then return. |
81 | office | 115 | if (!isset($_GET['hash']) or empty($_GET['hash'])) { |
116 | http_response_code(404); |
||
117 | die('File not found.'); |
||
52 | office | 118 | } |
119 | |||
53 | office | 120 | ### Find the requested file. |
52 | office | 121 | $file = array_shift( |
122 | preg_grep( |
||
81 | office | 123 | '/'.$_GET['hash'].'/', |
67 | office | 124 | scandir($config['STORE_FOLDER']) |
52 | office | 125 | ) |
126 | ); |
||
127 | |||
81 | office | 128 | if (!isset($file) or empty($file)) { |
129 | http_response_code(404); |
||
130 | die('File not found.'); |
||
131 | } |
||
53 | office | 132 | |
133 | ### Check the path for path traversals. |
||
134 | $fileExtension = pathinfo($file, PATHINFO_EXTENSION); |
||
52 | office | 135 | |
53 | office | 136 | #### If the extension is not allowed then return. |
137 | if (!isset($fileExtension) || |
||
138 | !in_array(strtoupper($fileExtension), |
||
67 | office | 139 | array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) { |
81 | office | 140 | http_response_code(403); |
141 | die('File extension not allowed.'); |
||
52 | office | 142 | } |
53 | office | 143 | |
144 | #### Build the user path. |
||
145 | $userPath = join( |
||
146 | DIRECTORY_SEPARATOR, |
||
147 | array( |
||
67 | office | 148 | $config['STORE_FOLDER'], |
53 | office | 149 | $file |
150 | ) |
||
151 | ); |
||
52 | office | 152 | |
53 | office | 153 | #### Check for path traversals |
154 | $pathPart = pathinfo($userPath); |
||
155 | if (strcasecmp( |
||
67 | office | 156 | realpath($pathPart['dirname']), realpath($config['STORE_FOLDER'])) != 0) { |
81 | office | 157 | http_response_code(500); |
158 | die('Internal server error.'); |
||
53 | office | 159 | } |
160 | |||
83 | office | 161 | ### Hook for various file extensions. |
53 | office | 162 | switch(strtoupper($fileExtension)) { |
163 | case "HTML": |
||
164 | case "HTM": |
||
165 | header('Content-type: text/html'); |
||
166 | break; |
||
167 | break; |
||
83 | office | 168 | case "URL": |
91 | office | 169 | if(preg_match( |
170 | "/URL=(https?:\/\/[\-_\.\+!\*'\(\),a-zA-Z0-9]+:?[0-9]{0,5}\/.*?)\n/", |
||
171 | file_get_contents($userPath), $matches)) { |
||
83 | office | 172 | header('Location: '.$matches[1]); |
173 | return; |
||
174 | } |
||
175 | break; |
||
53 | office | 176 | default: |
177 | ### Open MIME info database and send the content type. |
||
178 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
||
179 | if (!$finfo) { |
||
81 | office | 180 | http_response_code(500); |
181 | die('Internal server error.'); |
||
53 | office | 182 | } |
183 | header('Content-type: '.finfo_file($finfo, $userPath)); |
||
184 | finfo_close($finfo); |
||
185 | break; |
||
186 | } |
||
187 | |||
52 | office | 188 | ### Send the file along with the inline content disposition. |
53 | office | 189 | header('Content-length: '.(int)get_file_size($userPath)); |
190 | header('Content-Disposition: inline; filename="' . basename($userPath) . '"'); |
||
191 | header('Content-Transfer-Encoding: binary'); |
||
192 | header('X-Sendfile: '.$userPath); |
||
52 | office | 193 | break; |
194 | } |