Skip to content

Commit 47863c0

Browse files
committed
restriction config updated
1 parent 9fdcd39 commit 47863c0

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

index.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
'root' => __DIR__.'/tmp/storage/',
1616
'cache' => __DIR__.'/tmp/.cache/',
1717
'uploads' => [
18-
'allowed_types' => [
18+
'max_upload_size' => 0,
19+
'mime_check' => true,
20+
'allowed_types' => [
1921
'image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/svg\+xml', 'image/svg'
2022
]
2123
]

src/Plugins/Core.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,21 +286,33 @@ private function recursive_delete($target)
286286
public function upload()
287287
{
288288
/** @var UploadedFile $file */
289-
$file = request()->files->get('file');
289+
$file = request()->files->get('file');
290+
291+
// ensure if the file is allowed to be uploaded
292+
ensureSafeFile($file->getRealPath());
293+
294+
$max_upload_size = config('uploads.max_upload_size');
295+
296+
if ($max_upload_size) {
297+
if ($max_upload_size * 1024 < $file->getSize()) {
298+
abort(406, ['message' => 'File size must be less than '.$max_upload_size.'MB']);
299+
}
300+
}
301+
290302
$filename = absolutePath(request_path(), $file->getClientOriginalName());
291303
if ($filename) {
292304
$option = request('option');
293305
if ($option === 'replace') {
294306
// replace the existing file
295-
filesystem()->remove($filename);
296307
deleteThumb($filename);
308+
filesystem()->remove($filename);
297309
$file->move(request_path(), $file->getClientOriginalName());
298310
} elseif ($option === 'keep-both') {
299311
// keep both files
300312
// save the new file under new name
301313
$_filename = pathinfo($filename, PATHINFO_FILENAME);
302-
$_ext = pathinfo($filename, PATHINFO_EXTENSION);
303-
$name = getSafePath($_filename, $_ext);
314+
$_ext = pathinfo($filename, PATHINFO_EXTENSION);
315+
$name = getSafePath($_filename, $_ext);
304316
$file->move(request_path(), pathinfo($name, PATHINFO_BASENAME));
305317
} else {
306318
// send the message to confirm an option
@@ -315,8 +327,6 @@ public function upload()
315327
$filepath = absolutePath(request_path(), $file->getClientOriginalName());
316328

317329
if (filesystem()->exists($filepath)) {
318-
ensureSafeFile($filepath);
319-
320330
return jsonResponse(['message' => 'File upload successful']);
321331
}
322332

src/config.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
return [
44
'root' => null,
55
'cache' => null,
6-
'uploads' => [],
6+
'uploads' => [
7+
'max_upload_size' => 0,
8+
'mime_check' => false,
9+
'allowed_types' => [],
10+
],
711
'plugins' => [
812
'Core' => \Rocky\FileManager\Plugins\Core::class,
913
]

src/helpers.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ function getFileInfo(\Symfony\Component\Finder\SplFileInfo $file)
329329
if ($file->isFile()) {
330330
$mime = mimeTypes()->guessMimeType($file->getRealPath());
331331
if (preg_match('#^image/#', $mime)) {
332-
$dimension = getimagesize($file->getRealPath());
333-
if($info) {
332+
$dimension = getimagesize($file->getRealPath());
333+
if ($info) {
334334
$info['image_info'] = [
335335
'width' => $dimension['0'],
336336
'height' => $dimension['1'],
@@ -375,19 +375,21 @@ function getSafePath($name, $ext = '')
375375
*/
376376
function ensureSafeFile($filepath)
377377
{
378-
$mime = mimeTypes()->guessMimeType($filepath);
379-
$valid = false;
380-
foreach (config('uploads.allowed_types') as $allowed_type) {
381-
if (preg_match("#^{$allowed_type}$#", $mime)) {
382-
$valid = true;
383-
break;
378+
$mime = mimeTypes()->guessMimeType($filepath);
379+
if (config('uploads.mime_check')) {
380+
$valid = false;
381+
foreach (config('uploads.allowed_types') as $allowed_type) {
382+
if (preg_match("#^{$allowed_type}$#", $mime)) {
383+
$valid = true;
384+
break;
385+
}
384386
}
385-
}
386387

387-
if ( ! $valid) {
388-
filesystem()->remove($filepath);
388+
if ( ! $valid) {
389+
filesystem()->remove($filepath);
389390

390-
abort(403, ['message' => 'File type not allowed']);
391+
abort(403, ['message' => 'File type not allowed']);
392+
}
391393
}
392394

393395
return $mime;

0 commit comments

Comments
 (0)