scratch – Diff between revs 49 and 52

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