scratch – Diff between revs 90 and 91

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