scratch – Blame information for rev 66

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