-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupload.php
More file actions
217 lines (183 loc) · 6.16 KB
/
upload.php
File metadata and controls
217 lines (183 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
session_start();
// Enable error reporting for debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Define upload directory and max file size
$uploadDir = 'uploads/';
$maxFileSize = 500 * 1024 * 1024; // 500MB
$maxTotalSize = 150 * 1024 * 1024 * 1024; // 150GB
// Allowed MIME types
$allowedMimeTypes = [
'image/jpeg', 'image/png', 'image/gif', 'image/webp',
'video/webm', 'video/mp4',
'audio/wav', 'audio/mpeg', 'audio/ogg',
'application/zip', 'application/x-rar-compressed', 'application/x-7z-compressed',
'application/pdf', 'text/plain',
'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/octet-stream'
];
// Allowed file extensions
$allowedExtensions = [
'jpg', 'jpeg', 'png', 'gif', 'webp', 'webm', 'mp4',
'wav', 'mp3', 'ogg', 'zip', 'rar', '7z', 'pdf', 'txt', 'doc', 'docx'
];
// Forbidden MIME types (executable types)
$forbiddenMimeTypes = [
'application/x-php', 'text/x-php', 'application/x-sh', 'application/x-csh',
'application/x-msdos-program', 'application/x-msdownload', 'application/x-exe',
'application/x-object', 'application/x-bat', 'application/x-python', 'application/x-perl'
];
// Forbidden file extensions
$forbiddenExtensions = [
'php', 'php3', 'php4', 'php5', 'phtml', 'sh', 'bash', 'bat', 'cmd', 'exe', 'msi',
'bin', 'cgi', 'pl', 'py', 'asp', 'aspx', 'jsp', 'vbs'
];
// Security headers
header('Content-Security-Policy: default-src \'self\';');
header('X-Content-Type-Options: nosniff');
header('X-Frame-Options: DENY');
header('X-XSS-Protection: 1; mode=block');
// Function to get the total directory size
function getDirectorySize($path)
{
$totalSize = 0;
try {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
foreach ($iterator as $file) {
if ($file->isFile()) {
$totalSize += $file->getSize();
}
}
} catch (Exception $e) {
return false;
}
return $totalSize;
}
// Function to delete the oldest file
function deleteOldestFile($dir)
{
$oldestFile = null;
$oldestTime = PHP_INT_MAX;
foreach (new DirectoryIterator($dir) as $fileInfo) {
if ($fileInfo->isFile()) {
$fileTime = $fileInfo->getCTime();
if ($fileTime < $oldestTime) {
$oldestTime = $fileTime;
$oldestFile = $fileInfo->getPathname();
}
}
}
return $oldestFile !== null ? unlink($oldestFile) : false;
}
// Function to prevent excessive uploads
function isRateLimited()
{
$maxUploadsPerMinute = 2;
if (!isset($_SESSION['uploads'])) {
$_SESSION['uploads'] = [];
}
$currentTime = time();
$_SESSION['uploads'] = array_filter($_SESSION['uploads'], function ($timestamp) use ($currentTime) {
return $timestamp >= $currentTime - 60;
});
if (count($_SESSION['uploads']) >= $maxUploadsPerMinute) {
return true;
}
$_SESSION['uploads'][] = $currentTime;
return false;
}
// Ensure the request is POST with a file upload
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
if (isRateLimited()) {
http_response_code(429);
echo 'Rate limit exceeded. Try again later.';
exit;
}
$file = $_FILES['file'];
if ($file['error'] !== UPLOAD_ERR_OK) {
http_response_code(400);
echo 'File upload error.';
exit;
}
if ($file['size'] > $maxFileSize) {
http_response_code(400);
echo 'File too large.';
exit;
}
// Get MIME type
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$fileMimeType = finfo_file($finfo, $file['tmp_name']);
finfo_close($finfo);
// Get file extension
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
// Block dangerous MIME types
if (in_array($fileMimeType, $forbiddenMimeTypes)) {
http_response_code(400);
echo 'Executable files are not allowed.';
exit;
}
// Allow application/octet-stream only if the extension is whitelisted
if ($fileMimeType === 'application/octet-stream') {
if (!in_array($fileExtension, $allowedExtensions)) {
http_response_code(400);
echo 'Invalid file type.';
exit;
}
} elseif (!in_array($fileMimeType, $allowedMimeTypes)) {
http_response_code(400);
echo 'Invalid file type.';
exit;
}
// Block forbidden extensions
if (in_array($fileExtension, $forbiddenExtensions)) {
http_response_code(400);
echo 'Executable files are not allowed.';
exit;
}
// Generate a random 16-byte hex string
$randomHex = bin2hex(random_bytes(16));
// Preserve the original file extension
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
// New filename: 16-byte hex + original extension
$uniqueName = $randomHex . '.' . $fileExtension;
$targetFilePath = $uploadDir . $uniqueName;
// Ensure upload directory exists
if (!is_dir($uploadDir) && !mkdir($uploadDir, 0755, true)) {
http_response_code(500);
echo 'Failed to create upload directory.';
exit;
}
if (!is_writable($uploadDir)) {
http_response_code(500);
echo 'Upload directory is not writable.';
exit;
}
// Manage total upload size
$currentTotalSize = getDirectorySize($uploadDir);
if ($currentTotalSize === false) {
http_response_code(500);
echo 'Failed to calculate total size.';
exit;
}
while ($currentTotalSize + $file['size'] > $maxTotalSize) {
if (!deleteOldestFile($uploadDir)) {
http_response_code(500);
echo 'Failed to free space.';
exit;
}
$currentTotalSize = getDirectorySize($uploadDir);
}
// Move uploaded file
if (move_uploaded_file($file['tmp_name'], $targetFilePath)) {
echo htmlspecialchars($uniqueName, ENT_QUOTES, 'UTF-8');
} else {
http_response_code(500);
echo 'Failed to save file.';
}
} else {
http_response_code(400);
echo 'Invalid request.';
}
?>