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
<?php
namespace Laminas\Validator\File;
use Countable;
use Laminas\Validator\AbstractValidator;
use Laminas\Validator\Exception;
use Psr\Http\Message\UploadedFileInterface;
use Traversable;
use function array_key_exists;
use function array_merge;
use function count;
use function is_array;
use function is_string;
use function is_uploaded_file;
/**
* Validator for the maximum size of a file up to a max of 2GB
*/
class Upload extends AbstractValidator
{
/**
* @const string Error constants
*/
public const INI_SIZE = 'fileUploadErrorIniSize';
public const FORM_SIZE = 'fileUploadErrorFormSize';
public const PARTIAL = 'fileUploadErrorPartial';
public const NO_FILE = 'fileUploadErrorNoFile';
public const NO_TMP_DIR = 'fileUploadErrorNoTmpDir';
public const CANT_WRITE = 'fileUploadErrorCantWrite';
public const EXTENSION = 'fileUploadErrorExtension';
public const ATTACK = 'fileUploadErrorAttack';
public const FILE_NOT_FOUND = 'fileUploadErrorFileNotFound';
public const UNKNOWN = 'fileUploadErrorUnknown';
/** @var array<string, string> Error message templates */
protected $messageTemplates = [
self::INI_SIZE => "File '%value%' exceeds upload_max_filesize directive in php.ini",
self::FORM_SIZE => "File '%value%' exceeds the MAX_FILE_SIZE directive that was "
. 'specified in the HTML form',
self::PARTIAL => "File '%value%' was only partially uploaded",
self::NO_FILE => "File '%value%' was not uploaded",
self::NO_TMP_DIR => "Missing a temporary folder to store '%value%'",
self::CANT_WRITE => "Failed to write file '%value%' to disk",
self::EXTENSION => "A PHP extension stopped uploading the file '%value%'",
self::ATTACK => "File '%value%' was illegally uploaded. This could be a possible attack",
self::FILE_NOT_FOUND => "File '%value%' was not found",
self::UNKNOWN => "Unknown error while uploading file '%value%'",
];
/** @var array<string, mixed> */
protected $options = [
'files' => [],
];
/**
* Sets validator options
*
* The array $files must be given in syntax of Laminas\File\Transfer\Transfer to be checked
* If no files are given the $_FILES array will be used automatically.
* NOTE: This validator will only work with HTTP POST uploads!
*
* @param array|Traversable $options Array of files in syntax of \Laminas\File\Transfer\Transfer
*/
public function __construct($options = [])
{
if (is_array($options) && ! array_key_exists('files', $options)) {
$options = ['files' => $options];
}
parent::__construct($options);
}
/**
* Returns the array of set files
*
* @param string $file (Optional) The file to return in detail
* @return array
* @throws Exception\InvalidArgumentException If file is not found.
*/
public function getFiles($file = null)
{
if ($file !== null) {
$return = [];
foreach ($this->options['files'] as $name => $content) {
if ($name === $file) {
$return[$file] = $this->options['files'][$name];
}
if ($content instanceof UploadedFileInterface) {
if ($content->getClientFilename() === $file) {
$return[$name] = $this->options['files'][$name];
}
} elseif ($content['name'] === $file) {
$return[$name] = $this->options['files'][$name];
}
}
if (! $return) {
throw new Exception\InvalidArgumentException("The file '$file' was not found");
}
return $return;
}
return $this->options['files'];
}
/**
* Sets the files to be checked
*
* @param array $files The files to check in syntax of \Laminas\File\Transfer\Transfer
* @return $this Provides a fluent interface
*/
public function setFiles($files = [])
{
if (
null === $files
|| ((is_array($files) || $files instanceof Countable)
&& count($files) === 0)
) {
$this->options['files'] = $_FILES;
} else {
$this->options['files'] = $files;
}
if ($this->options['files'] === null) {
$this->options['files'] = [];
}
foreach ($this->options['files'] as $file => $content) {
if (
! $content instanceof UploadedFileInterface
&& ! isset($content['error'])
) {
unset($this->options['files'][$file]);
}
}
return $this;
}
/**
* Returns true if and only if the file was uploaded without errors
*
* @param string $value Single file to check for upload errors, when giving null the $_FILES array
* from initialization will be used
* @param mixed $file
* @return bool
*/
public function isValid($value, $file = null)
{
$files = [];
$this->setValue($value);
if (array_key_exists($value, $this->getFiles())) {
$files = array_merge($files, $this->getFiles($value));
} else {
foreach ($this->getFiles() as $file => $content) {
if ($content instanceof UploadedFileInterface) {
if ($content->getClientFilename() === $value) {
$files = array_merge($files, $this->getFiles($file));
}
// PSR cannot search by tmp_name because it does not have
// a public interface to get it, only user defined name
// from form field.
continue;
}
if (isset($content['name']) && ($content['name'] === $value)) {
$files = array_merge($files, $this->getFiles($file));
}
if (isset($content['tmp_name']) && ($content['tmp_name'] === $value)) {
$files = array_merge($files, $this->getFiles($file));
}
}
}
if (empty($files)) {
return $this->throwError($file, self::FILE_NOT_FOUND);
}
foreach ($files as $file => $content) {
$this->value = $file;
$error = $content instanceof UploadedFileInterface
? $content->getError()
: $content['error'];
switch ($error) {
case 0:
if ($content instanceof UploadedFileInterface) {
// done!
break;
}
// For standard SAPI environments, check that the upload
// was valid
if (! is_uploaded_file($content['tmp_name'])) {
$this->throwError($content, self::ATTACK);
}
break;
case 1:
$this->throwError($content, self::INI_SIZE);
break;
case 2:
$this->throwError($content, self::FORM_SIZE);
break;
case 3:
$this->throwError($content, self::PARTIAL);
break;
case 4:
$this->throwError($content, self::NO_FILE);
break;
case 6:
$this->throwError($content, self::NO_TMP_DIR);
break;
case 7:
$this->throwError($content, self::CANT_WRITE);
break;
case 8:
$this->throwError($content, self::EXTENSION);
break;
default:
$this->throwError($content, self::UNKNOWN);
break;
}
}
if ($this->getMessages()) {
return false;
}
return true;
}
/**
* Throws an error of the given type
*
* @param array|string|UploadedFileInterface $file
* @param string $errorType
* @return false
*/
protected function throwError($file, $errorType)
{
if ($file !== null) {
if (is_array($file)) {
if (array_key_exists('name', $file)) {
$this->value = $file['name'];
}
} elseif (is_string($file)) {
$this->value = $file;
} elseif ($file instanceof UploadedFileInterface) {
$this->value = $file->getClientFilename();
}
}
$this->error($errorType);
return false;
}
}