scratch – Diff between revs 52 and 53

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