scratch – Diff between revs 53 and 57

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 53 Rev 57
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('inc/pseudocrypt.php'); 7 require_once('inc/pseudocrypt.php');
8 require_once('inc/functions.php'); 8 require_once('inc/functions.php');
9 require_once('config.php'); 9 require_once('config.php');
10   10  
11 #### POST -> upload / GET -> download 11 #### POST -> upload / GET -> download
12 switch ($_SERVER['REQUEST_METHOD']) { 12 switch ($_SERVER['REQUEST_METHOD']) {
13 case 'POST': 13 case 'POST':
14 #### Retrieve uploaded file. 14 #### Retrieve uploaded file.
15 if (!empty($_FILES['file']) and 15 if (!empty($_FILES['file']) and
16 is_uploaded_file($_FILES['file']['tmp_name'])) { 16 is_uploaded_file($_FILES['file']['tmp_name'])) {
-   17 if($_FILES['file']['size'] > $ALLOWED_ASSET_SIZE * 1048576) {
-   18 header('File size exceeds '.$ALLOWED_ASSET_SIZE.'MiB.', true, 403);
-   19 return;
-   20 }
17 # Regular multipart/form-data upload. 21 # Regular multipart/form-data upload.
18 $name = $_FILES['file']['name']; 22 $name = $_FILES['file']['name'];
19 $data = file_get_contents($_FILES['file']['tmp_name']); 23 $data = atomized_get_contents($_FILES['file']['tmp_name']);
20 } else { 24 } else {
-   25 if((int)get_file_size("php://input") > $ALLOWED_ASSET_SIZE * 1048576) {
-   26 header('File size exceeds '.$ALLOWED_ASSET_SIZE.'MiB.', true, 403);
-   27 return;
-   28 }
21 # Raw POST data. 29 # Raw POST data.
22 $name = urldecode(@$_SERVER['HTTP_X_FILE_NAME']); 30 $name = urldecode(@$_SERVER['HTTP_X_FILE_NAME']);
23 $data = file_get_contents("php://input"); 31 $data = atomized_get_contents("php://input");
24 } 32 }
25   33  
26 #### Grab the file extension. 34 #### Grab the file extension.
27 $fileExtension = pathinfo($name, PATHINFO_EXTENSION); 35 $fileExtension = pathinfo($name, PATHINFO_EXTENSION);
28   36  
29 #### If the extension is not allowed then change it to a text extension. 37 #### If the extension is not allowed then change it to a text extension.
30 if (!isset($fileExtension) || 38 if (!isset($fileExtension) ||
31 !in_array(strtoupper($fileExtension), 39 !in_array(strtoupper($fileExtension),
32 array_map('strtoupper', $ALLOWED_FILE_EXTENSIONS))) { 40 array_map('strtoupper', $ALLOWED_FILE_EXTENSIONS))) {
33 header("HTTP/1.1 500 Internal Server Error", true, 500); 41 header('File extension not allowed.', true, 403);
34 return; 42 return;
35 } 43 }
36 44
37 #### Hash filename. 45 #### Hash filename.
38 $file = strtolower( 46 $file = strtolower(
39 PseudoCrypt::hash( 47 PseudoCrypt::hash(
40 preg_replace( 48 preg_replace(
41 '/\D/', 49 '/\D/',
42 '', 50 '',
43 hash( 51 hash(
44 'sha512', 52 'sha512',
45 $data 53 $data
46 ) 54 )
47 ), 55 ),
48 $ASSET_HASH_SIZE 56 $ASSET_HASH_SIZE
49 ) 57 )
50 ); 58 );
51   59  
52 #### Build the user path. 60 #### Build the user path.
53 $userPath = join( 61 $userPath = join(
54 DIRECTORY_SEPARATOR, 62 DIRECTORY_SEPARATOR,
55 array( 63 array(
56 $STORE_FOLDER, 64 $STORE_FOLDER,
57 $file 65 $file
58 ) 66 )
59 ); 67 );
60   68  
61 #### Check for path traversals 69 #### Check for path traversals
62 $pathPart = pathinfo($userPath.'.'.$fileExtension); 70 $pathPart = pathinfo($userPath.'.'.$fileExtension);
63 if (strcasecmp( 71 if (strcasecmp(
64 realpath($pathPart['dirname']), realpath($STORE_FOLDER)) != 0) { 72 realpath($pathPart['dirname']), realpath($STORE_FOLDER)) != 0) {
-   73 header('Internal server error.', true, 500);
65 return; 74 return;
66 } 75 }
67   76  
68 #### Store the file. 77 #### Store the file.
69 atomized_put_contents($userPath.'.'.$fileExtension, $data); 78 atomized_put_contents($userPath.'.'.$fileExtension, $data);
70   79  
71 ### Return the URL to the file. 80 ### Return the URL to the file.
72 header('Content-Type: text/plain; charset=utf-8'); 81 header('Content-Type: text/plain; charset=utf-8');
73 echo sprintf('%s/%s', trim($URL_PATH, '/'), $file); 82 echo sprintf('%s/%s', trim($URL_PATH, '/'), $file);
74 break; 83 break;
75 case 'GET': 84 case 'GET':
76 ### If no file has been specified for download then return. 85 ### If no file has been specified for download then return.
77 if (!isset($_GET['o']) or empty($_GET['o'])) { 86 if (!isset($_GET['o']) or empty($_GET['o'])) {
78 http_response_code(404); 87 header('File not found.', true, 404);
79 return; 88 return;
80 } 89 }
81   90  
82 ### Find the requested file. 91 ### Find the requested file.
83 $file = array_shift( 92 $file = array_shift(
84 preg_grep( 93 preg_grep(
85 "/$_GET[o]/", 94 "/$_GET[o]/",
86 scandir($STORE_FOLDER) 95 scandir($STORE_FOLDER)
87 ) 96 )
88 ); 97 );
89   98  
90 if (!isset($file) or empty($file)) 99 if (!isset($file) or empty($file))
91 return; 100 return;
92 101
93 ### Check the path for path traversals. 102 ### Check the path for path traversals.
94 $fileExtension = pathinfo($file, PATHINFO_EXTENSION); 103 $fileExtension = pathinfo($file, PATHINFO_EXTENSION);
95   104  
96 #### If the extension is not allowed then return. 105 #### If the extension is not allowed then return.
97 if (!isset($fileExtension) || 106 if (!isset($fileExtension) ||
98 !in_array(strtoupper($fileExtension), 107 !in_array(strtoupper($fileExtension),
99 array_map('strtoupper', $ALLOWED_FILE_EXTENSIONS))) { 108 array_map('strtoupper', $ALLOWED_FILE_EXTENSIONS))) {
100 header("HTTP/1.1 500 Internal Server Error", true, 500); 109 header('File extension not allowed.', true, 403);
101 return; 110 return;
102 } 111 }
103 112
104 #### Build the user path. 113 #### Build the user path.
105 $userPath = join( 114 $userPath = join(
106 DIRECTORY_SEPARATOR, 115 DIRECTORY_SEPARATOR,
107 array( 116 array(
108 $STORE_FOLDER, 117 $STORE_FOLDER,
109 $file 118 $file
110 ) 119 )
111 ); 120 );
112   121  
113 #### Check for path traversals 122 #### Check for path traversals
114 $pathPart = pathinfo($userPath); 123 $pathPart = pathinfo($userPath);
115 if (strcasecmp( 124 if (strcasecmp(
116 realpath($pathPart['dirname']), realpath($STORE_FOLDER)) != 0) { 125 realpath($pathPart['dirname']), realpath($STORE_FOLDER)) != 0) {
-   126 header('Internal server error.', true, 500);
117 return; 127 return;
118 } 128 }
119   129  
120 ### Hook for HTML files to display properly. 130 ### Hook for HTML files to display properly.
121 switch(strtoupper($fileExtension)) { 131 switch(strtoupper($fileExtension)) {
122 case "HTML": 132 case "HTML":
123 case "HTM": 133 case "HTM":
124 header('Content-type: text/html'); 134 header('Content-type: text/html');
125 break; 135 break;
126 break; 136 break;
127 default: 137 default:
128 ### Open MIME info database and send the content type. 138 ### Open MIME info database and send the content type.
129 $finfo = finfo_open(FILEINFO_MIME_TYPE); 139 $finfo = finfo_open(FILEINFO_MIME_TYPE);
130 if (!$finfo) { 140 if (!$finfo) {
131 http_response_code(500); 141 header('Internal server error.', true, 500);
132 return; 142 return;
133 } 143 }
134 -  
135 header('Content-type: '.finfo_file($finfo, $userPath)); 144 header('Content-type: '.finfo_file($finfo, $userPath));
136 finfo_close($finfo); 145 finfo_close($finfo);
137 break; 146 break;
138 } 147 }
139 148
140 ### Send the file along with the inline content disposition. 149 ### Send the file along with the inline content disposition.
141 header('Content-length: '.(int)get_file_size($userPath)); 150 header('Content-length: '.(int)get_file_size($userPath));
142 header('Content-Disposition: inline; filename="' . basename($userPath) . '"'); 151 header('Content-Disposition: inline; filename="' . basename($userPath) . '"');
143 header('Content-Transfer-Encoding: binary'); 152 header('Content-Transfer-Encoding: binary');
144 header('X-Sendfile: '.$userPath); 153 header('X-Sendfile: '.$userPath);
145 break; 154 break;
146 } 155 }
147   156