scratch – Diff between revs 57 and 66

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