Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Validator\Constraints;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\HttpFoundation\File\File as FileObject;
|
Chris@0
|
15 use Symfony\Component\HttpFoundation\File\UploadedFile;
|
Chris@0
|
16 use Symfony\Component\Validator\Constraint;
|
Chris@0
|
17 use Symfony\Component\Validator\ConstraintValidator;
|
Chris@0
|
18 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * @author Bernhard Schussek <bschussek@gmail.com>
|
Chris@0
|
22 */
|
Chris@0
|
23 class FileValidator extends ConstraintValidator
|
Chris@0
|
24 {
|
Chris@0
|
25 const KB_BYTES = 1000;
|
Chris@0
|
26 const MB_BYTES = 1000000;
|
Chris@0
|
27 const KIB_BYTES = 1024;
|
Chris@0
|
28 const MIB_BYTES = 1048576;
|
Chris@0
|
29
|
Chris@17
|
30 private static $suffices = [
|
Chris@0
|
31 1 => 'bytes',
|
Chris@0
|
32 self::KB_BYTES => 'kB',
|
Chris@0
|
33 self::MB_BYTES => 'MB',
|
Chris@0
|
34 self::KIB_BYTES => 'KiB',
|
Chris@0
|
35 self::MIB_BYTES => 'MiB',
|
Chris@17
|
36 ];
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * {@inheritdoc}
|
Chris@0
|
40 */
|
Chris@0
|
41 public function validate($value, Constraint $constraint)
|
Chris@0
|
42 {
|
Chris@0
|
43 if (!$constraint instanceof File) {
|
Chris@0
|
44 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\File');
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 if (null === $value || '' === $value) {
|
Chris@0
|
48 return;
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 if ($value instanceof UploadedFile && !$value->isValid()) {
|
Chris@0
|
52 switch ($value->getError()) {
|
Chris@0
|
53 case UPLOAD_ERR_INI_SIZE:
|
Chris@0
|
54 $iniLimitSize = UploadedFile::getMaxFilesize();
|
Chris@0
|
55 if ($constraint->maxSize && $constraint->maxSize < $iniLimitSize) {
|
Chris@0
|
56 $limitInBytes = $constraint->maxSize;
|
Chris@0
|
57 $binaryFormat = $constraint->binaryFormat;
|
Chris@0
|
58 } else {
|
Chris@0
|
59 $limitInBytes = $iniLimitSize;
|
Chris@17
|
60 $binaryFormat = null === $constraint->binaryFormat ? true : $constraint->binaryFormat;
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 list($sizeAsString, $limitAsString, $suffix) = $this->factorizeSizes(0, $limitInBytes, $binaryFormat);
|
Chris@0
|
64 $this->context->buildViolation($constraint->uploadIniSizeErrorMessage)
|
Chris@0
|
65 ->setParameter('{{ limit }}', $limitAsString)
|
Chris@0
|
66 ->setParameter('{{ suffix }}', $suffix)
|
Chris@0
|
67 ->setCode(UPLOAD_ERR_INI_SIZE)
|
Chris@0
|
68 ->addViolation();
|
Chris@0
|
69
|
Chris@0
|
70 return;
|
Chris@0
|
71 case UPLOAD_ERR_FORM_SIZE:
|
Chris@0
|
72 $this->context->buildViolation($constraint->uploadFormSizeErrorMessage)
|
Chris@0
|
73 ->setCode(UPLOAD_ERR_FORM_SIZE)
|
Chris@0
|
74 ->addViolation();
|
Chris@0
|
75
|
Chris@0
|
76 return;
|
Chris@0
|
77 case UPLOAD_ERR_PARTIAL:
|
Chris@0
|
78 $this->context->buildViolation($constraint->uploadPartialErrorMessage)
|
Chris@0
|
79 ->setCode(UPLOAD_ERR_PARTIAL)
|
Chris@0
|
80 ->addViolation();
|
Chris@0
|
81
|
Chris@0
|
82 return;
|
Chris@0
|
83 case UPLOAD_ERR_NO_FILE:
|
Chris@0
|
84 $this->context->buildViolation($constraint->uploadNoFileErrorMessage)
|
Chris@0
|
85 ->setCode(UPLOAD_ERR_NO_FILE)
|
Chris@0
|
86 ->addViolation();
|
Chris@0
|
87
|
Chris@0
|
88 return;
|
Chris@0
|
89 case UPLOAD_ERR_NO_TMP_DIR:
|
Chris@0
|
90 $this->context->buildViolation($constraint->uploadNoTmpDirErrorMessage)
|
Chris@0
|
91 ->setCode(UPLOAD_ERR_NO_TMP_DIR)
|
Chris@0
|
92 ->addViolation();
|
Chris@0
|
93
|
Chris@0
|
94 return;
|
Chris@0
|
95 case UPLOAD_ERR_CANT_WRITE:
|
Chris@0
|
96 $this->context->buildViolation($constraint->uploadCantWriteErrorMessage)
|
Chris@0
|
97 ->setCode(UPLOAD_ERR_CANT_WRITE)
|
Chris@0
|
98 ->addViolation();
|
Chris@0
|
99
|
Chris@0
|
100 return;
|
Chris@0
|
101 case UPLOAD_ERR_EXTENSION:
|
Chris@0
|
102 $this->context->buildViolation($constraint->uploadExtensionErrorMessage)
|
Chris@0
|
103 ->setCode(UPLOAD_ERR_EXTENSION)
|
Chris@0
|
104 ->addViolation();
|
Chris@0
|
105
|
Chris@0
|
106 return;
|
Chris@0
|
107 default:
|
Chris@0
|
108 $this->context->buildViolation($constraint->uploadErrorMessage)
|
Chris@0
|
109 ->setCode($value->getError())
|
Chris@0
|
110 ->addViolation();
|
Chris@0
|
111
|
Chris@0
|
112 return;
|
Chris@0
|
113 }
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@17
|
116 if (!is_scalar($value) && !$value instanceof FileObject && !(\is_object($value) && method_exists($value, '__toString'))) {
|
Chris@0
|
117 throw new UnexpectedTypeException($value, 'string');
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 $path = $value instanceof FileObject ? $value->getPathname() : (string) $value;
|
Chris@0
|
121
|
Chris@0
|
122 if (!is_file($path)) {
|
Chris@0
|
123 $this->context->buildViolation($constraint->notFoundMessage)
|
Chris@0
|
124 ->setParameter('{{ file }}', $this->formatValue($path))
|
Chris@0
|
125 ->setCode(File::NOT_FOUND_ERROR)
|
Chris@0
|
126 ->addViolation();
|
Chris@0
|
127
|
Chris@0
|
128 return;
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 if (!is_readable($path)) {
|
Chris@0
|
132 $this->context->buildViolation($constraint->notReadableMessage)
|
Chris@0
|
133 ->setParameter('{{ file }}', $this->formatValue($path))
|
Chris@0
|
134 ->setCode(File::NOT_READABLE_ERROR)
|
Chris@0
|
135 ->addViolation();
|
Chris@0
|
136
|
Chris@0
|
137 return;
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 $sizeInBytes = filesize($path);
|
Chris@0
|
141
|
Chris@0
|
142 if (0 === $sizeInBytes) {
|
Chris@0
|
143 $this->context->buildViolation($constraint->disallowEmptyMessage)
|
Chris@0
|
144 ->setParameter('{{ file }}', $this->formatValue($path))
|
Chris@0
|
145 ->setCode(File::EMPTY_ERROR)
|
Chris@0
|
146 ->addViolation();
|
Chris@0
|
147
|
Chris@0
|
148 return;
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 if ($constraint->maxSize) {
|
Chris@0
|
152 $limitInBytes = $constraint->maxSize;
|
Chris@0
|
153
|
Chris@0
|
154 if ($sizeInBytes > $limitInBytes) {
|
Chris@0
|
155 list($sizeAsString, $limitAsString, $suffix) = $this->factorizeSizes($sizeInBytes, $limitInBytes, $constraint->binaryFormat);
|
Chris@0
|
156 $this->context->buildViolation($constraint->maxSizeMessage)
|
Chris@0
|
157 ->setParameter('{{ file }}', $this->formatValue($path))
|
Chris@0
|
158 ->setParameter('{{ size }}', $sizeAsString)
|
Chris@0
|
159 ->setParameter('{{ limit }}', $limitAsString)
|
Chris@0
|
160 ->setParameter('{{ suffix }}', $suffix)
|
Chris@0
|
161 ->setCode(File::TOO_LARGE_ERROR)
|
Chris@0
|
162 ->addViolation();
|
Chris@0
|
163
|
Chris@0
|
164 return;
|
Chris@0
|
165 }
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 if ($constraint->mimeTypes) {
|
Chris@0
|
169 if (!$value instanceof FileObject) {
|
Chris@0
|
170 $value = new FileObject($value);
|
Chris@0
|
171 }
|
Chris@0
|
172
|
Chris@0
|
173 $mimeTypes = (array) $constraint->mimeTypes;
|
Chris@0
|
174 $mime = $value->getMimeType();
|
Chris@0
|
175
|
Chris@0
|
176 foreach ($mimeTypes as $mimeType) {
|
Chris@0
|
177 if ($mimeType === $mime) {
|
Chris@0
|
178 return;
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 if ($discrete = strstr($mimeType, '/*', true)) {
|
Chris@0
|
182 if (strstr($mime, '/', true) === $discrete) {
|
Chris@0
|
183 return;
|
Chris@0
|
184 }
|
Chris@0
|
185 }
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 $this->context->buildViolation($constraint->mimeTypesMessage)
|
Chris@0
|
189 ->setParameter('{{ file }}', $this->formatValue($path))
|
Chris@0
|
190 ->setParameter('{{ type }}', $this->formatValue($mime))
|
Chris@0
|
191 ->setParameter('{{ types }}', $this->formatValues($mimeTypes))
|
Chris@0
|
192 ->setCode(File::INVALID_MIME_TYPE_ERROR)
|
Chris@0
|
193 ->addViolation();
|
Chris@0
|
194 }
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@0
|
197 private static function moreDecimalsThan($double, $numberOfDecimals)
|
Chris@0
|
198 {
|
Chris@17
|
199 return \strlen((string) $double) > \strlen(round($double, $numberOfDecimals));
|
Chris@0
|
200 }
|
Chris@0
|
201
|
Chris@0
|
202 /**
|
Chris@0
|
203 * Convert the limit to the smallest possible number
|
Chris@0
|
204 * (i.e. try "MB", then "kB", then "bytes").
|
Chris@0
|
205 */
|
Chris@0
|
206 private function factorizeSizes($size, $limit, $binaryFormat)
|
Chris@0
|
207 {
|
Chris@0
|
208 if ($binaryFormat) {
|
Chris@0
|
209 $coef = self::MIB_BYTES;
|
Chris@0
|
210 $coefFactor = self::KIB_BYTES;
|
Chris@0
|
211 } else {
|
Chris@0
|
212 $coef = self::MB_BYTES;
|
Chris@0
|
213 $coefFactor = self::KB_BYTES;
|
Chris@0
|
214 }
|
Chris@0
|
215
|
Chris@0
|
216 $limitAsString = (string) ($limit / $coef);
|
Chris@0
|
217
|
Chris@0
|
218 // Restrict the limit to 2 decimals (without rounding! we
|
Chris@0
|
219 // need the precise value)
|
Chris@0
|
220 while (self::moreDecimalsThan($limitAsString, 2)) {
|
Chris@0
|
221 $coef /= $coefFactor;
|
Chris@0
|
222 $limitAsString = (string) ($limit / $coef);
|
Chris@0
|
223 }
|
Chris@0
|
224
|
Chris@0
|
225 // Convert size to the same measure, but round to 2 decimals
|
Chris@0
|
226 $sizeAsString = (string) round($size / $coef, 2);
|
Chris@0
|
227
|
Chris@0
|
228 // If the size and limit produce the same string output
|
Chris@0
|
229 // (due to rounding), reduce the coefficient
|
Chris@0
|
230 while ($sizeAsString === $limitAsString) {
|
Chris@0
|
231 $coef /= $coefFactor;
|
Chris@0
|
232 $limitAsString = (string) ($limit / $coef);
|
Chris@0
|
233 $sizeAsString = (string) round($size / $coef, 2);
|
Chris@0
|
234 }
|
Chris@0
|
235
|
Chris@17
|
236 return [$sizeAsString, $limitAsString, self::$suffices[$coef]];
|
Chris@0
|
237 }
|
Chris@0
|
238 }
|