-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileCheck.php
286 lines (261 loc) · 9.83 KB
/
FileCheck.php
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* This class provides basic checks for an uploaded file. It exposes
* common attributes for the uploaded file (e.g. name, mime, location)
* and needs to pass validation for the upload to succeed.
*/
namespace CLC\Upload;
class FileCheck {
/**
* @var string $uploadAttachment $_Files object
* @var bool $isImage togged on detection of jpg
* @var string $mime detected file mime Type
* @var string $ext generated extention from mime
* @var string $newFileName generated random name with extention
* @var const $destination upload location const from UPLOAD_DESTINATION config should be outside document root
* @var string $finalDestination once uploaded final destination
* @var bool $errors bool are there errors
* @var string $errorMessage string thrown with error message
* @var array $allowedTypes allowed upload mimes
* @var array $allowedExt allowed extention types
* @var array $errorTypes array of error message types
*/
private $uploadAttachment;
private $isImage = false;
private $mime;
private $ext;
private $newFileName;
private $destination = UPLOAD_LOC;
private $finalDestination;
private $errors = false;
private $errorMessage;
private $allowedTypes = ['image/jpeg',
'application/pdf',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'];
private $allowedExt = ['image/jpeg' => 'jpg',
'application/pdf' => 'pdf',
'application/msword' => 'doc',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx'];
private $errorTypes = [ 1 => 'The uploaded file exceeds the upload max file size',
2 => 'The uploaded file exceeds the max file size in form',
3 => 'The uploaded file was only partially uploaded',
4 => 'No file was uploaded',
5 => 'Missing a temporary folder',
6 => 'Failed to write file to disk',
7 => 'File upload stopped by extension'];
/**
* Constructor
* @param string $uploadFile The $_FILES[] key
*/
public function __construct($uploadFile)
{
$this->uploadAttachment = $uploadFile;
}
/**
* checkFile
* @return Self
* @throws Exception If checks on file fail for any reason
*/
public function checkFile() {
try {
$this->checkMalformedError()
->checkUploadError()
->checkIsFile()
->checkFileSize()
->checkMime()
->randomName()
->checkExtention()
->checkIsImage()
->resizeImage()
->moveUpload();
}
catch (\Exception $e) {
$this->errors = true;
$this->errorMessage = $e->getMessage();
$this->setDefaults();
}
return $this;
}
/**
* checkMalformedError callable, checks $_Files['error'] is not missing or malformed
* @return Self
* @throws Exception If $Files['errors'] argument is malformed or missing
*/
private function checkMalformedError() {
if (!isset($this->uploadAttachment['error']) || is_array($this->uploadAttachment['error'])) {
throw new \Exception('$_FILES Corruption Attack');
}
return $this;
}
/**
* checkUploadError callable, checks $Files[] key has no errors
* @return Self
* @throws Exception If $Files['errors'] argument is populated with error message
*/
private function checkUploadError() {
if ($this->uploadAttachment['error'] != 0) {
$errorTypes = $this->errorTypes;
throw new \Exception('Upload Error! ' . $errorTypes[$this->uploadAttachment['error']]);
}
return $this;
}
/**
* checkIsFile callable, checks $Files[] key contains a file
* @return Self
* @throws Exception If $Files[] argument is not a file
*/
private function checkIsFile() {
if (!is_uploaded_file($this->uploadAttachment['tmp_name'])) {
throw new \Exception('No File Uploaded');
}
return $this;
}
/**
* checkFileSize callable, checks $Files[size] isnt greater than specified size (10485760 bytes 10meg)
* @return Self
* @throws Exception If $Files['size'] argument is > 10meg
*/
private function checkFileSize() {
if ($this->uploadAttachment['size'] > 10485760) {
throw new \Exception('Exceeded filesize limit');
}
return $this;
}
/**
* checkMime callable, gets mime type from uploaded file and updates $this->mime with string
* @return Self
*/
private function checkMime() {
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$fileContents = file_get_contents($this->uploadAttachment['tmp_name']);
$this->mime = $finfo->buffer($fileContents);
$this->makeExtension($finfo->buffer($fileContents));
return $this;
}
/**
* makeExtention callable, takes $mimeType string and generates file extention from whitelist
* @param string $mimeType mimetype string for comparison to whitelist
* @return Self
* @throws Exception If mimetype not in whitelist
*/
private function makeExtension($mimeType) {
$extensions = $this->allowedExt;
if (!isset($extensions[$mimeType]))
{
throw new \Exception('Not Allowed Filetype (mime) Type');
}
$this->ext = $extensions[$mimeType];
return $this;
}
/**
* randomName callable, creates unique random name for new file including generated extention.
* @return Self
*/
private function randomName() {
$this->newFileName = uniqid() . "." . $this->ext;
return $this;
}
/**
* checkExtention callable, compares final generated name againts whitelist.
* @return Self
* @throws Exception If extention not in whitelist
*/
private function checkExtention() {
$validFileExtensions = array(".jpg", ".pdf", ".doc", ".docx");
$fileExtension = strrchr($this->newFileName, ".");
if (!in_array($fileExtension, $validFileExtensions)) {
throw new \Exception('Not Valid Extension');
}
return $this;
}
/**
* isImage callable, if jpg checks image size to check its not script renamed to .jpg
* @return Self
* @throws Exception If file doesnt exist
* @throws Exception If upload is not a image
*/
private function checkIsImage() {
if ($this->ext === 'jpg') {
$this->isImage = true;
$shouldBeImage = $this->uploadAttachment['tmp_name'];
$imageSizeData = getimagesize($shouldBeImage);
if ($imageSizeData === FALSE)
{
throw new \Exception('Not Image file');
}
}
return $this;
}
/**
* resizeImage callable, if detected image resizes image to remove scripts in meta and save space on server
* @return Self
*/
private function resizeImage() {
if ($this->isImage === true) {
$resizeTmp = @imagecreatefromjpeg($this->uploadAttachment['tmp_name']);
if (!$resizeTmp) {
throw new \Exception('Malformed Image file');
}
$resizeScl = imagescale($resizeTmp, 1024);
imagejpeg($resizeScl, $this->uploadAttachment['tmp_name']);
imagedestroy($resizeTmp);
imagedestroy($resizeScl);
}
return $this;
}
/**
* moveUpload, moves file from php temp to final destination and updates finalDestination string
* @return Self
*/
private function moveUpload() {
$this->finalDestination = $this->destination . $this->newFileName;
move_uploaded_file($this->uploadAttachment['tmp_name'], $this->finalDestination);
return $this;
}
/**
* set vaules back to default can be trigged if errors detected
* @return Self
*/
private function setDefaults() {
$this->newFileName = $this->mime = $this->ext = $this->finalDestination = null;
$this->isImage = false;
return $this;
}
/**
* @return file upload location
*/
public function getLocation() {
return $this->finalDestination;
}
/**
* @return file upload name
*/
public function getFileName() {
return $this->newFileName;
}
/**
* @return file mime type
*/
public function getFileMime() {
return $this->mime;
}
/**
* @return bool is file image
*/
public function getIsFileImage() {
return $this->isImage;
}
/**
* @return bool any errors
*/
public function getErrors() {
return $this->errors;
}
/**
* @return string error messages
*/
public function getErrorMessage() {
return $this->errorMessage;
}
}