Skip to content

Commit ed1180c

Browse files
committed
upload file type validation
1 parent 31051a3 commit ed1180c

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

classes/FilePond/FilePondController.php

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Martin\Forms\Classes\FilePond;
44

5-
use Illuminate\Routing\Controller as BaseController;
5+
use Validator;
66
use Illuminate\Http\Request;
7+
use Martin\Forms\Models\Settings;
78
use Illuminate\Support\Facades\Response;
9+
use October\Rain\Filesystem\Definitions;
10+
use Illuminate\Routing\Controller as BaseController;
811

912
class FilePondController extends BaseController
1013
{
11-
1214
/**
1315
* @var Filepond
1416
*/
@@ -33,10 +35,17 @@ public function __construct(FilePond $filepond)
3335
*/
3436
public function upload(Request $request): \Illuminate\Http\Response
3537
{
36-
$field = $request->headers->get('FILEPOND-FIELD');
38+
$field = $this->getUploadFieldName();
3739
$input = $request->file($field);
3840
$this->file = is_array($input) ? $input[0] : $input;
3941

42+
/** VALIDATE UPLOAD FILE TYPE */
43+
if ($this->checkInvalidFile()) {
44+
return Response::make('File type not allowed', 422, [
45+
'Content-Type' => 'text/plain',
46+
]);
47+
}
48+
4049
if ($input === null) {
4150
return Response::make($field . ' is required', 422, [
4251
'Content-Type' => 'text/plain',
@@ -79,6 +88,16 @@ public function delete(Request $request): \Illuminate\Http\Response
7988
]);
8089
}
8190

91+
/**
92+
* Get field name used for uploads
93+
*
94+
* @return string
95+
*/
96+
private function getUploadFieldName(): string
97+
{
98+
return request()->headers->get('FILEPOND-FIELD');
99+
}
100+
82101
/**
83102
* Generate unique temporary filename
84103
*
@@ -93,4 +112,39 @@ private function generateTempFilename(): string
93112
$this->file->getClientOriginalName()
94113
]);
95114
}
115+
116+
/**
117+
* Check if uploaded file is a valid mime type
118+
*
119+
* @return boolean
120+
*/
121+
private function checkInvalidFile(): bool
122+
{
123+
$field = $this->getUploadFieldName();
124+
$types = $this->allowedFileTypes();
125+
126+
$validator = Validator::make(request()->all(), [
127+
$field . '.*' => 'mimes:' . $types,
128+
]);
129+
130+
return $validator->fails();
131+
}
132+
133+
/**
134+
* Get a list of allowed files types
135+
*
136+
* @return string
137+
*/
138+
private function allowedFileTypes(): string
139+
{
140+
$settings = Settings::get('global_allowed_files', false);
141+
142+
if ($settings) {
143+
return $settings;
144+
}
145+
146+
$default = Definitions::get('defaultExtensions');
147+
148+
return implode(',', $default);
149+
}
96150
}

models/settings/fields.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,33 @@ tabs:
66
path : $/martin/forms/models/settings/_plugin_help.htm
77
tab : martin.forms::lang.settings.tabs.general
88

9+
section_ui:
10+
label : UI
11+
type : section
12+
comment : Settings related to plugin user inferface
13+
tab : martin.forms::lang.settings.tabs.general
14+
cssClass: m-t
15+
916
global_hide_button:
1017
label : martin.forms::lang.settings.global_hide_button
1118
comment: martin.forms::lang.settings.global_hide_button_desc
1219
type : switch
1320
default: false
1421
tab : martin.forms::lang.settings.tabs.general
1522

23+
section_uploads:
24+
label : File uploads
25+
type : section
26+
comment : Settings related to file uploads through components
27+
tab : martin.forms::lang.settings.tabs.general
28+
cssClass: m-t-lg
29+
30+
global_allowed_files:
31+
label : Allowed file formats
32+
type : text
33+
default : jpg,jpeg,bmp,png,webp,gif,js,ico,css,ics,odt,doc,docx,ppt,pptx,pdf,txt,xml,ods,xls,xlsx,ttf,flv,wmv,mp3,ogg,wav,avi,mov,mp4,mpeg,webm,mkv,rar,xml,zip
34+
tab : martin.forms::lang.settings.tabs.general
35+
1636
recaptcha_help:
1737
type : partial
1838
path : $/martin/forms/models/settings/_recaptcha_help.htm

0 commit comments

Comments
 (0)