/usr/share/php/Symfony/Component/Validator/Constraints/FileValidator.php is in php-symfony-validator 2.7.10-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | <?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\HttpFoundation\File\File as FileObject;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class FileValidator extends ConstraintValidator
{
const KB_BYTES = 1000;
const MB_BYTES = 1000000;
const KIB_BYTES = 1024;
const MIB_BYTES = 1048576;
private static $suffices = array(
1 => 'bytes',
self::KB_BYTES => 'kB',
self::MB_BYTES => 'MB',
self::KIB_BYTES => 'KiB',
self::MIB_BYTES => 'MiB',
);
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof File) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\File');
}
if (null === $value || '' === $value) {
return;
}
if ($value instanceof UploadedFile && !$value->isValid()) {
switch ($value->getError()) {
case UPLOAD_ERR_INI_SIZE:
$iniLimitSize = UploadedFile::getMaxFilesize();
if ($constraint->maxSize && $constraint->maxSize < $iniLimitSize) {
$limitInBytes = $constraint->maxSize;
$binaryFormat = $constraint->binaryFormat;
} else {
$limitInBytes = $iniLimitSize;
$binaryFormat = true;
}
list($sizeAsString, $limitAsString, $suffix) = $this->factorizeSizes(0, $limitInBytes, $binaryFormat);
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->uploadIniSizeErrorMessage)
->setParameter('{{ limit }}', $limitAsString)
->setParameter('{{ suffix }}', $suffix)
->setCode(UPLOAD_ERR_INI_SIZE)
->addViolation();
} else {
$this->buildViolation($constraint->uploadIniSizeErrorMessage)
->setParameter('{{ limit }}', $limitAsString)
->setParameter('{{ suffix }}', $suffix)
->setCode(UPLOAD_ERR_INI_SIZE)
->addViolation();
}
return;
case UPLOAD_ERR_FORM_SIZE:
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->uploadFormSizeErrorMessage)
->setCode(UPLOAD_ERR_FORM_SIZE)
->addViolation();
} else {
$this->buildViolation($constraint->uploadFormSizeErrorMessage)
->setCode(UPLOAD_ERR_FORM_SIZE)
->addViolation();
}
return;
case UPLOAD_ERR_PARTIAL:
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->uploadPartialErrorMessage)
->setCode(UPLOAD_ERR_PARTIAL)
->addViolation();
} else {
$this->buildViolation($constraint->uploadPartialErrorMessage)
->setCode(UPLOAD_ERR_PARTIAL)
->addViolation();
}
return;
case UPLOAD_ERR_NO_FILE:
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->uploadNoFileErrorMessage)
->setCode(UPLOAD_ERR_NO_FILE)
->addViolation();
} else {
$this->buildViolation($constraint->uploadNoFileErrorMessage)
->setCode(UPLOAD_ERR_NO_FILE)
->addViolation();
}
return;
case UPLOAD_ERR_NO_TMP_DIR:
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->uploadNoTmpDirErrorMessage)
->setCode(UPLOAD_ERR_NO_TMP_DIR)
->addViolation();
} else {
$this->buildViolation($constraint->uploadNoTmpDirErrorMessage)
->setCode(UPLOAD_ERR_NO_TMP_DIR)
->addViolation();
}
return;
case UPLOAD_ERR_CANT_WRITE:
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->uploadCantWriteErrorMessage)
->setCode(UPLOAD_ERR_CANT_WRITE)
->addViolation();
} else {
$this->buildViolation($constraint->uploadCantWriteErrorMessage)
->setCode(UPLOAD_ERR_CANT_WRITE)
->addViolation();
}
return;
case UPLOAD_ERR_EXTENSION:
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->uploadExtensionErrorMessage)
->setCode(UPLOAD_ERR_EXTENSION)
->addViolation();
} else {
$this->buildViolation($constraint->uploadExtensionErrorMessage)
->setCode(UPLOAD_ERR_EXTENSION)
->addViolation();
}
return;
default:
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->uploadErrorMessage)
->setCode($value->getError())
->addViolation();
} else {
$this->buildViolation($constraint->uploadErrorMessage)
->setCode($value->getError())
->addViolation();
}
return;
}
}
if (!is_scalar($value) && !$value instanceof FileObject && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
$path = $value instanceof FileObject ? $value->getPathname() : (string) $value;
if (!is_file($path)) {
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->notFoundMessage)
->setParameter('{{ file }}', $this->formatValue($path))
->setCode(File::NOT_FOUND_ERROR)
->addViolation();
} else {
$this->buildViolation($constraint->notFoundMessage)
->setParameter('{{ file }}', $this->formatValue($path))
->setCode(File::NOT_FOUND_ERROR)
->addViolation();
}
return;
}
if (!is_readable($path)) {
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->notReadableMessage)
->setParameter('{{ file }}', $this->formatValue($path))
->setCode(File::NOT_READABLE_ERROR)
->addViolation();
} else {
$this->buildViolation($constraint->notReadableMessage)
->setParameter('{{ file }}', $this->formatValue($path))
->setCode(File::NOT_READABLE_ERROR)
->addViolation();
}
return;
}
$sizeInBytes = filesize($path);
if (0 === $sizeInBytes) {
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->disallowEmptyMessage)
->setParameter('{{ file }}', $this->formatValue($path))
->setCode(File::EMPTY_ERROR)
->addViolation();
} else {
$this->buildViolation($constraint->disallowEmptyMessage)
->setParameter('{{ file }}', $this->formatValue($path))
->setCode(File::EMPTY_ERROR)
->addViolation();
}
return;
}
if ($constraint->maxSize) {
$limitInBytes = $constraint->maxSize;
if ($sizeInBytes > $limitInBytes) {
list($sizeAsString, $limitAsString, $suffix) = $this->factorizeSizes($sizeInBytes, $limitInBytes, $constraint->binaryFormat);
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->maxSizeMessage)
->setParameter('{{ file }}', $this->formatValue($path))
->setParameter('{{ size }}', $sizeAsString)
->setParameter('{{ limit }}', $limitAsString)
->setParameter('{{ suffix }}', $suffix)
->setCode(File::TOO_LARGE_ERROR)
->addViolation();
} else {
$this->buildViolation($constraint->maxSizeMessage)
->setParameter('{{ file }}', $this->formatValue($path))
->setParameter('{{ size }}', $sizeAsString)
->setParameter('{{ limit }}', $limitAsString)
->setParameter('{{ suffix }}', $suffix)
->setCode(File::TOO_LARGE_ERROR)
->addViolation();
}
return;
}
}
if ($constraint->mimeTypes) {
if (!$value instanceof FileObject) {
$value = new FileObject($value);
}
$mimeTypes = (array) $constraint->mimeTypes;
$mime = $value->getMimeType();
foreach ($mimeTypes as $mimeType) {
if ($mimeType === $mime) {
return;
}
if ($discrete = strstr($mimeType, '/*', true)) {
if (strstr($mime, '/', true) === $discrete) {
return;
}
}
}
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->mimeTypesMessage)
->setParameter('{{ file }}', $this->formatValue($path))
->setParameter('{{ type }}', $this->formatValue($mime))
->setParameter('{{ types }}', $this->formatValues($mimeTypes))
->setCode(File::INVALID_MIME_TYPE_ERROR)
->addViolation();
} else {
$this->buildViolation($constraint->mimeTypesMessage)
->setParameter('{{ file }}', $this->formatValue($path))
->setParameter('{{ type }}', $this->formatValue($mime))
->setParameter('{{ types }}', $this->formatValues($mimeTypes))
->setCode(File::INVALID_MIME_TYPE_ERROR)
->addViolation();
}
}
}
private static function moreDecimalsThan($double, $numberOfDecimals)
{
return strlen((string) $double) > strlen(round($double, $numberOfDecimals));
}
/**
* Convert the limit to the smallest possible number
* (i.e. try "MB", then "kB", then "bytes").
*/
private function factorizeSizes($size, $limit, $binaryFormat)
{
if ($binaryFormat) {
$coef = self::MIB_BYTES;
$coefFactor = self::KIB_BYTES;
} else {
$coef = self::MB_BYTES;
$coefFactor = self::KB_BYTES;
}
$limitAsString = (string) ($limit / $coef);
// Restrict the limit to 2 decimals (without rounding! we
// need the precise value)
while (self::moreDecimalsThan($limitAsString, 2)) {
$coef /= $coefFactor;
$limitAsString = (string) ($limit / $coef);
}
// Convert size to the same measure, but round to 2 decimals
$sizeAsString = (string) round($size / $coef, 2);
// If the size and limit produce the same string output
// (due to rounding), reduce the coefficient
while ($sizeAsString === $limitAsString) {
$coef /= $coefFactor;
$limitAsString = (string) ($limit / $coef);
$sizeAsString = (string) round($size / $coef, 2);
}
return array($sizeAsString, $limitAsString, self::$suffices[$coef]);
}
}
|