2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-10-02 11:42:31 +01:00
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-10-02 11:42:31 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Validator\Constraints;
|
|
|
|
|
2014-03-17 15:44:19 +00:00
|
|
|
use Symfony\Component\HttpFoundation\File\File as FileObject;
|
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
2018-07-10 14:47:44 +01:00
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedValueException;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-07-20 09:37:57 +01:00
|
|
|
/**
|
2012-02-09 16:54:30 +00:00
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
2011-07-20 09:37:57 +01:00
|
|
|
*/
|
2010-06-24 09:40:05 +01:00
|
|
|
class FileValidator extends ConstraintValidator
|
|
|
|
{
|
2014-05-20 15:38:02 +01:00
|
|
|
const KB_BYTES = 1000;
|
|
|
|
const MB_BYTES = 1000000;
|
2014-05-31 22:12:08 +01:00
|
|
|
const KIB_BYTES = 1024;
|
|
|
|
const MIB_BYTES = 1048576;
|
2014-05-20 15:38:02 +01:00
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
private static $suffices = [
|
2014-05-20 15:38:02 +01:00
|
|
|
1 => 'bytes',
|
|
|
|
self::KB_BYTES => 'kB',
|
|
|
|
self::MB_BYTES => 'MB',
|
2014-05-31 22:12:08 +01:00
|
|
|
self::KIB_BYTES => 'KiB',
|
|
|
|
self::MIB_BYTES => 'MiB',
|
2019-01-16 09:39:14 +00:00
|
|
|
];
|
2014-05-20 15:38:02 +01:00
|
|
|
|
2011-07-20 09:37:57 +01:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2011-07-20 09:37:57 +01:00
|
|
|
*/
|
2012-02-09 17:02:12 +00:00
|
|
|
public function validate($value, Constraint $constraint)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2014-03-10 12:51:04 +00:00
|
|
|
if (!$constraint instanceof File) {
|
|
|
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\File');
|
|
|
|
}
|
|
|
|
|
2010-12-21 02:59:17 +00:00
|
|
|
if (null === $value || '' === $value) {
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-07-05 19:05:01 +01:00
|
|
|
if ($value instanceof UploadedFile && !$value->isValid()) {
|
|
|
|
switch ($value->getError()) {
|
|
|
|
case UPLOAD_ERR_INI_SIZE:
|
2014-10-04 08:41:02 +01:00
|
|
|
$iniLimitSize = UploadedFile::getMaxFilesize();
|
|
|
|
if ($constraint->maxSize && $constraint->maxSize < $iniLimitSize) {
|
|
|
|
$limitInBytes = $constraint->maxSize;
|
|
|
|
$binaryFormat = $constraint->binaryFormat;
|
2013-03-13 14:14:49 +00:00
|
|
|
} else {
|
2014-10-04 08:41:02 +01:00
|
|
|
$limitInBytes = $iniLimitSize;
|
2018-07-08 22:39:51 +01:00
|
|
|
$binaryFormat = null === $constraint->binaryFormat ? true : $constraint->binaryFormat;
|
2013-03-13 14:14:49 +00:00
|
|
|
}
|
|
|
|
|
2014-10-04 08:41:02 +01:00
|
|
|
list($sizeAsString, $limitAsString, $suffix) = $this->factorizeSizes(0, $limitInBytes, $binaryFormat);
|
2016-01-14 00:18:51 +00:00
|
|
|
$this->context->buildViolation($constraint->uploadIniSizeErrorMessage)
|
|
|
|
->setParameter('{{ limit }}', $limitAsString)
|
|
|
|
->setParameter('{{ suffix }}', $suffix)
|
|
|
|
->setCode(UPLOAD_ERR_INI_SIZE)
|
|
|
|
->addViolation();
|
2011-07-05 19:05:01 +01:00
|
|
|
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2011-07-05 19:05:01 +01:00
|
|
|
case UPLOAD_ERR_FORM_SIZE:
|
2016-01-14 00:18:51 +00:00
|
|
|
$this->context->buildViolation($constraint->uploadFormSizeErrorMessage)
|
|
|
|
->setCode(UPLOAD_ERR_FORM_SIZE)
|
|
|
|
->addViolation();
|
2011-07-05 19:05:01 +01:00
|
|
|
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2012-03-19 13:37:25 +00:00
|
|
|
case UPLOAD_ERR_PARTIAL:
|
2016-01-14 00:18:51 +00:00
|
|
|
$this->context->buildViolation($constraint->uploadPartialErrorMessage)
|
|
|
|
->setCode(UPLOAD_ERR_PARTIAL)
|
|
|
|
->addViolation();
|
2012-03-19 13:37:25 +00:00
|
|
|
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2012-03-19 13:37:25 +00:00
|
|
|
case UPLOAD_ERR_NO_FILE:
|
2016-01-14 00:18:51 +00:00
|
|
|
$this->context->buildViolation($constraint->uploadNoFileErrorMessage)
|
|
|
|
->setCode(UPLOAD_ERR_NO_FILE)
|
|
|
|
->addViolation();
|
2012-03-19 13:37:25 +00:00
|
|
|
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2012-03-19 13:37:25 +00:00
|
|
|
case UPLOAD_ERR_NO_TMP_DIR:
|
2016-01-14 00:18:51 +00:00
|
|
|
$this->context->buildViolation($constraint->uploadNoTmpDirErrorMessage)
|
|
|
|
->setCode(UPLOAD_ERR_NO_TMP_DIR)
|
|
|
|
->addViolation();
|
2012-03-19 13:37:25 +00:00
|
|
|
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2012-03-19 13:37:25 +00:00
|
|
|
case UPLOAD_ERR_CANT_WRITE:
|
2016-01-14 00:18:51 +00:00
|
|
|
$this->context->buildViolation($constraint->uploadCantWriteErrorMessage)
|
|
|
|
->setCode(UPLOAD_ERR_CANT_WRITE)
|
|
|
|
->addViolation();
|
2012-03-19 13:37:25 +00:00
|
|
|
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2012-03-19 13:37:25 +00:00
|
|
|
case UPLOAD_ERR_EXTENSION:
|
2016-01-14 00:18:51 +00:00
|
|
|
$this->context->buildViolation($constraint->uploadExtensionErrorMessage)
|
|
|
|
->setCode(UPLOAD_ERR_EXTENSION)
|
|
|
|
->addViolation();
|
2012-03-19 13:37:25 +00:00
|
|
|
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2011-07-05 19:05:01 +01:00
|
|
|
default:
|
2016-01-14 00:18:51 +00:00
|
|
|
$this->context->buildViolation($constraint->uploadErrorMessage)
|
|
|
|
->setCode($value->getError())
|
|
|
|
->addViolation();
|
2011-07-05 19:05:01 +01:00
|
|
|
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2011-07-05 19:05:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 12:24:53 +01:00
|
|
|
if (!is_scalar($value) && !$value instanceof FileObject && !(\is_object($value) && method_exists($value, '__toString'))) {
|
2018-07-10 14:47:44 +01:00
|
|
|
throw new UnexpectedValueException($value, 'string');
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-06-14 09:47:04 +01:00
|
|
|
$path = $value instanceof FileObject ? $value->getPathname() : (string) $value;
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2011-08-29 14:28:03 +01:00
|
|
|
if (!is_file($path)) {
|
2016-01-14 00:18:51 +00:00
|
|
|
$this->context->buildViolation($constraint->notFoundMessage)
|
|
|
|
->setParameter('{{ file }}', $this->formatValue($path))
|
|
|
|
->setCode(File::NOT_FOUND_ERROR)
|
|
|
|
->addViolation();
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_readable($path)) {
|
2016-01-14 00:18:51 +00:00
|
|
|
$this->context->buildViolation($constraint->notReadableMessage)
|
|
|
|
->setParameter('{{ file }}', $this->formatValue($path))
|
|
|
|
->setCode(File::NOT_READABLE_ERROR)
|
|
|
|
->addViolation();
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2014-06-15 14:48:08 +01:00
|
|
|
$sizeInBytes = filesize($path);
|
2018-02-21 17:19:11 +00:00
|
|
|
$basename = $value instanceof UploadedFile ? $value->getClientOriginalName() : basename($path);
|
2014-09-24 11:08:18 +01:00
|
|
|
|
2014-06-15 14:48:08 +01:00
|
|
|
if (0 === $sizeInBytes) {
|
2016-01-14 00:18:51 +00:00
|
|
|
$this->context->buildViolation($constraint->disallowEmptyMessage)
|
|
|
|
->setParameter('{{ file }}', $this->formatValue($path))
|
2018-02-21 17:19:11 +00:00
|
|
|
->setParameter('{{ name }}', $this->formatValue($basename))
|
2016-01-14 00:18:51 +00:00
|
|
|
->setCode(File::EMPTY_ERROR)
|
|
|
|
->addViolation();
|
2014-09-24 11:08:18 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($constraint->maxSize) {
|
2014-06-17 10:58:55 +01:00
|
|
|
$limitInBytes = $constraint->maxSize;
|
2014-05-20 15:38:02 +01:00
|
|
|
|
|
|
|
if ($sizeInBytes > $limitInBytes) {
|
2014-10-02 21:15:16 +01:00
|
|
|
list($sizeAsString, $limitAsString, $suffix) = $this->factorizeSizes($sizeInBytes, $limitInBytes, $constraint->binaryFormat);
|
2016-01-14 00:18:51 +00:00
|
|
|
$this->context->buildViolation($constraint->maxSizeMessage)
|
|
|
|
->setParameter('{{ file }}', $this->formatValue($path))
|
|
|
|
->setParameter('{{ size }}', $sizeAsString)
|
|
|
|
->setParameter('{{ limit }}', $limitAsString)
|
|
|
|
->setParameter('{{ suffix }}', $suffix)
|
2018-02-21 17:19:11 +00:00
|
|
|
->setParameter('{{ name }}', $this->formatValue($basename))
|
2016-01-14 00:18:51 +00:00
|
|
|
->setCode(File::TOO_LARGE_ERROR)
|
|
|
|
->addViolation();
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($constraint->mimeTypes) {
|
2010-07-15 11:47:10 +01:00
|
|
|
if (!$value instanceof FileObject) {
|
2010-12-15 18:22:14 +00:00
|
|
|
$value = new FileObject($value);
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-09-01 15:10:16 +01:00
|
|
|
$mimeTypes = (array) $constraint->mimeTypes;
|
|
|
|
$mime = $value->getMimeType();
|
|
|
|
|
|
|
|
foreach ($mimeTypes as $mimeType) {
|
|
|
|
if ($mimeType === $mime) {
|
2014-09-24 10:48:52 +01:00
|
|
|
return;
|
2011-09-01 15:10:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($discrete = strstr($mimeType, '/*', true)) {
|
|
|
|
if (strstr($mime, '/', true) === $discrete) {
|
2014-09-24 10:48:52 +01:00
|
|
|
return;
|
2011-09-01 15:10:16 +01:00
|
|
|
}
|
2011-10-29 11:05:45 +01:00
|
|
|
}
|
2011-09-01 15:10:16 +01:00
|
|
|
}
|
|
|
|
|
2016-01-14 00:18:51 +00:00
|
|
|
$this->context->buildViolation($constraint->mimeTypesMessage)
|
|
|
|
->setParameter('{{ file }}', $this->formatValue($path))
|
|
|
|
->setParameter('{{ type }}', $this->formatValue($mime))
|
|
|
|
->setParameter('{{ types }}', $this->formatValues($mimeTypes))
|
2018-02-21 17:19:11 +00:00
|
|
|
->setParameter('{{ name }}', $this->formatValue($basename))
|
2016-01-14 00:18:51 +00:00
|
|
|
->setCode(File::INVALID_MIME_TYPE_ERROR)
|
|
|
|
->addViolation();
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
2014-05-20 15:38:02 +01:00
|
|
|
|
|
|
|
private static function moreDecimalsThan($double, $numberOfDecimals)
|
|
|
|
{
|
2018-07-05 12:24:53 +01:00
|
|
|
return \strlen((string) $double) > \strlen(round($double, $numberOfDecimals));
|
2014-05-20 15:38:02 +01:00
|
|
|
}
|
2014-10-02 21:15:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the limit to the smallest possible number
|
2015-08-24 07:53:33 +01:00
|
|
|
* (i.e. try "MB", then "kB", then "bytes").
|
2014-10-02 21:15:16 +01:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
return [$sizeAsString, $limitAsString, self::$suffices[$coef]];
|
2014-10-02 21:15:16 +01:00
|
|
|
}
|
2010-07-15 11:47:10 +01:00
|
|
|
}
|