scratch – Blame information for rev 81

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