scratch – Blame information for rev 67

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