scratch – Diff between revs 67 and 73

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 67 Rev 73
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/mustangostang/spyc/Spyc.php'); 9 require_once('vendor/mustangostang/spyc/Spyc.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 header('File size exceeds '.$config['ALLOWED_ASSET_SIZE'].'MiB.', true, 403); 21 header('File size exceeds '.$config['ALLOWED_ASSET_SIZE'].'MiB.', true, 403);
22 return; 22 return;
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 header('File size exceeds '.$config['ALLOWED_ASSET_SIZE'].'MiB.', true, 403); 29 header('File size exceeds '.$config['ALLOWED_ASSET_SIZE'].'MiB.', true, 403);
30 return; 30 return;
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 header('File extension not allowed.', true, 403); 44 header('File extension not allowed.', true, 403);
45 return; 45 return;
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 header('Internal server error.', true, 500); 76 header('Internal server error.', true, 500);
77 return; 77 return;
78 } 78 }
79   79  
80 #### Store the file. 80 #### Store the file.
81 atomized_put_contents($userPath.'.'.$fileExtension, $data); 81 atomized_put_contents($userPath.'.'.$fileExtension, $data);
82   82  
83 ### Return the URL to the file. 83 ### Return the URL to the file.
84 header('Content-Type: text/plain; charset=utf-8'); 84 header('Content-Type: text/plain; charset=utf-8');
85 echo sprintf('%s/%s', trim($config['URL_PATH'], '/'), $file); 85 echo $file;
86 break; 86 break;
87 case 'GET': 87 case 'GET':
88 ### If no file has been specified for download then return. 88 ### If no file has been specified for download then return.
89 if (!isset($_GET['o']) or empty($_GET['o'])) { 89 if (!isset($_GET['o']) or empty($_GET['o'])) {
90 header('File not found.', true, 404); 90 header('File not found.', true, 404);
91 return; 91 return;
92 } 92 }
93   93  
94 ### Find the requested file. 94 ### Find the requested file.
95 $file = array_shift( 95 $file = array_shift(
96 preg_grep( 96 preg_grep(
97 "/$_GET[o]/", 97 "/$_GET[o]/",
98 scandir($config['STORE_FOLDER']) 98 scandir($config['STORE_FOLDER'])
99 ) 99 )
100 ); 100 );
101   101  
102 if (!isset($file) or empty($file)) 102 if (!isset($file) or empty($file))
103 return; 103 return;
104 104
105 ### Check the path for path traversals. 105 ### Check the path for path traversals.
106 $fileExtension = pathinfo($file, PATHINFO_EXTENSION); 106 $fileExtension = pathinfo($file, PATHINFO_EXTENSION);
107   107  
108 #### If the extension is not allowed then return. 108 #### If the extension is not allowed then return.
109 if (!isset($fileExtension) || 109 if (!isset($fileExtension) ||
110 !in_array(strtoupper($fileExtension), 110 !in_array(strtoupper($fileExtension),
111 array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) { 111 array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) {
112 header('File extension not allowed.', true, 403); 112 header('File extension not allowed.', true, 403);
113 return; 113 return;
114 } 114 }
115 115
116 #### Build the user path. 116 #### Build the user path.
117 $userPath = join( 117 $userPath = join(
118 DIRECTORY_SEPARATOR, 118 DIRECTORY_SEPARATOR,
119 array( 119 array(
120 $config['STORE_FOLDER'], 120 $config['STORE_FOLDER'],
121 $file 121 $file
122 ) 122 )
123 ); 123 );
124   124  
125 #### Check for path traversals 125 #### Check for path traversals
126 $pathPart = pathinfo($userPath); 126 $pathPart = pathinfo($userPath);
127 if (strcasecmp( 127 if (strcasecmp(
128 realpath($pathPart['dirname']), realpath($config['STORE_FOLDER'])) != 0) { 128 realpath($pathPart['dirname']), realpath($config['STORE_FOLDER'])) != 0) {
129 header('Internal server error.', true, 500); 129 header('Internal server error.', true, 500);
130 return; 130 return;
131 } 131 }
132   132  
133 ### Hook for HTML files to display properly. 133 ### Hook for HTML files to display properly.
134 switch(strtoupper($fileExtension)) { 134 switch(strtoupper($fileExtension)) {
135 case "HTML": 135 case "HTML":
136 case "HTM": 136 case "HTM":
137 header('Content-type: text/html'); 137 header('Content-type: text/html');
138 break; 138 break;
139 break; 139 break;
140 default: 140 default:
141 ### Open MIME info database and send the content type. 141 ### Open MIME info database and send the content type.
142 $finfo = finfo_open(FILEINFO_MIME_TYPE); 142 $finfo = finfo_open(FILEINFO_MIME_TYPE);
143 if (!$finfo) { 143 if (!$finfo) {
144 header('Internal server error.', true, 500); 144 header('Internal server error.', true, 500);
145 return; 145 return;
146 } 146 }
147 header('Content-type: '.finfo_file($finfo, $userPath)); 147 header('Content-type: '.finfo_file($finfo, $userPath));
148 finfo_close($finfo); 148 finfo_close($finfo);
149 break; 149 break;
150 } 150 }
151 151
152 ### Send the file along with the inline content disposition. 152 ### Send the file along with the inline content disposition.
153 header('Content-length: '.(int)get_file_size($userPath)); 153 header('Content-length: '.(int)get_file_size($userPath));
154 header('Content-Disposition: inline; filename="' . basename($userPath) . '"'); 154 header('Content-Disposition: inline; filename="' . basename($userPath) . '"');
155 header('Content-Transfer-Encoding: binary'); 155 header('Content-Transfer-Encoding: binary');
156 header('X-Sendfile: '.$userPath); 156 header('X-Sendfile: '.$userPath);
157 break; 157 break;
158 } 158 }
159   159