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\ConstraintDefinitionException;
|
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
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
|
|
|
* @api
|
|
|
|
*/
|
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;
|
|
|
|
|
|
|
|
private static $suffices = array(
|
|
|
|
1 => 'bytes',
|
|
|
|
self::KB_BYTES => 'kB',
|
|
|
|
self::MB_BYTES => 'MB',
|
|
|
|
);
|
|
|
|
|
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:
|
2013-03-13 14:14:49 +00:00
|
|
|
if ($constraint->maxSize) {
|
|
|
|
if (ctype_digit((string) $constraint->maxSize)) {
|
2014-05-20 15:38:02 +01:00
|
|
|
$limitInBytes = (int) $constraint->maxSize;
|
2013-03-13 14:14:49 +00:00
|
|
|
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
|
2014-05-20 15:38:02 +01:00
|
|
|
$limitInBytes = $constraint->maxSize * self::KB_BYTES;
|
2013-03-13 14:14:49 +00:00
|
|
|
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
|
2014-05-20 15:38:02 +01:00
|
|
|
$limitInBytes = $constraint->maxSize * self::MB_BYTES;
|
2013-03-13 14:14:49 +00:00
|
|
|
} else {
|
|
|
|
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
|
|
|
|
}
|
2014-05-20 15:38:02 +01:00
|
|
|
$limitInBytes = min(UploadedFile::getMaxFilesize(), $limitInBytes);
|
2013-03-13 14:14:49 +00:00
|
|
|
} else {
|
2014-05-20 15:38:02 +01:00
|
|
|
$limitInBytes = UploadedFile::getMaxFilesize();
|
2013-03-13 14:14:49 +00:00
|
|
|
}
|
|
|
|
|
2012-05-11 15:14:27 +01:00
|
|
|
$this->context->addViolation($constraint->uploadIniSizeErrorMessage, array(
|
2014-05-20 15:38:02 +01:00
|
|
|
'{{ limit }}' => $limitInBytes,
|
2012-05-11 15:14:27 +01:00
|
|
|
'{{ suffix }}' => 'bytes',
|
|
|
|
));
|
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:
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->addViolation($constraint->uploadFormSizeErrorMessage);
|
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:
|
|
|
|
$this->context->addViolation($constraint->uploadPartialErrorMessage);
|
|
|
|
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2012-03-19 13:37:25 +00:00
|
|
|
case UPLOAD_ERR_NO_FILE:
|
|
|
|
$this->context->addViolation($constraint->uploadNoFileErrorMessage);
|
|
|
|
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2012-03-19 13:37:25 +00:00
|
|
|
case UPLOAD_ERR_NO_TMP_DIR:
|
|
|
|
$this->context->addViolation($constraint->uploadNoTmpDirErrorMessage);
|
|
|
|
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2012-03-19 13:37:25 +00:00
|
|
|
case UPLOAD_ERR_CANT_WRITE:
|
|
|
|
$this->context->addViolation($constraint->uploadCantWriteErrorMessage);
|
|
|
|
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2012-03-19 13:37:25 +00:00
|
|
|
case UPLOAD_ERR_EXTENSION:
|
|
|
|
$this->context->addViolation($constraint->uploadExtensionErrorMessage);
|
|
|
|
|
2012-02-09 16:54:30 +00:00
|
|
|
return;
|
2011-07-05 19:05:01 +01:00
|
|
|
default:
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->addViolation($constraint->uploadErrorMessage);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-07 20:01:44 +01:00
|
|
|
if (!is_scalar($value) && !$value instanceof FileObject && !(is_object($value) && method_exists($value, '__toString'))) {
|
2010-06-24 10:24:08 +01:00
|
|
|
throw new UnexpectedTypeException($value, 'string');
|
|
|
|
}
|
|
|
|
|
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)) {
|
2014-07-24 13:14:19 +01:00
|
|
|
$this->context->addViolation($constraint->notFoundMessage, array(
|
2014-09-21 19:53:12 +01:00
|
|
|
'{{ file }}' => $this->formatValue($path),
|
2014-07-24 13:14:19 +01:00
|
|
|
));
|
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)) {
|
2014-07-24 13:14:19 +01:00
|
|
|
$this->context->addViolation($constraint->notReadableMessage, array(
|
2014-09-21 19:53:12 +01:00
|
|
|
'{{ file }}' => $this->formatValue($path),
|
2014-07-24 13:14:19 +01:00
|
|
|
));
|
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->maxSize) {
|
2014-05-20 15:38:02 +01:00
|
|
|
$sizeInBytes = filesize($path);
|
|
|
|
$limitInBytes = (int) $constraint->maxSize;
|
|
|
|
|
|
|
|
if (preg_match('/^\d++k$/', $constraint->maxSize)) {
|
|
|
|
$limitInBytes *= self::KB_BYTES;
|
2013-03-13 14:14:49 +00:00
|
|
|
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
|
2014-05-20 15:38:02 +01:00
|
|
|
$limitInBytes *= self::MB_BYTES;
|
|
|
|
} elseif (!ctype_digit((string) $constraint->maxSize)) {
|
2010-06-24 10:24:08 +01:00
|
|
|
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
|
|
|
|
}
|
|
|
|
|
2014-05-20 15:38:02 +01:00
|
|
|
if ($sizeInBytes > $limitInBytes) {
|
|
|
|
// Convert the limit to the smallest possible number
|
|
|
|
// (i.e. try "MB", then "kB", then "bytes")
|
|
|
|
$coef = self::MB_BYTES;
|
|
|
|
$limitAsString = (string) ($limitInBytes / $coef);
|
|
|
|
|
|
|
|
// Restrict the limit to 2 decimals (without rounding! we
|
|
|
|
// need the precise value)
|
|
|
|
while (self::moreDecimalsThan($limitAsString, 2)) {
|
|
|
|
$coef /= 1000;
|
|
|
|
$limitAsString = (string) ($limitInBytes / $coef);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert size to the same measure, but round to 2 decimals
|
|
|
|
$sizeAsString = (string) round($sizeInBytes / $coef, 2);
|
|
|
|
|
|
|
|
// If the size and limit produce the same string output
|
|
|
|
// (due to rounding), reduce the coefficient
|
|
|
|
while ($sizeAsString === $limitAsString) {
|
|
|
|
$coef /= 1000;
|
|
|
|
$limitAsString = (string) ($limitInBytes / $coef);
|
|
|
|
$sizeAsString = (string) round($sizeInBytes / $coef, 2);
|
|
|
|
}
|
|
|
|
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->addViolation($constraint->maxSizeMessage, array(
|
2014-08-05 10:00:40 +01:00
|
|
|
'{{ size }}' => $sizeAsString,
|
|
|
|
'{{ limit }}' => $limitAsString,
|
|
|
|
'{{ suffix }}' => self::$suffices[$coef],
|
2014-07-24 13:14:19 +01:00
|
|
|
'{{ file }}' => $this->formatValue($path),
|
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();
|
|
|
|
$valid = false;
|
|
|
|
|
|
|
|
foreach ($mimeTypes as $mimeType) {
|
|
|
|
if ($mimeType === $mime) {
|
|
|
|
$valid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($discrete = strstr($mimeType, '/*', true)) {
|
|
|
|
if (strstr($mime, '/', true) === $discrete) {
|
|
|
|
$valid = true;
|
|
|
|
break;
|
|
|
|
}
|
2011-10-29 11:05:45 +01:00
|
|
|
}
|
2011-09-01 15:10:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (false === $valid) {
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->addViolation($constraint->mimeTypesMessage, array(
|
2014-07-24 13:14:19 +01:00
|
|
|
'{{ type }}' => $this->formatValue($mime),
|
|
|
|
'{{ types }}' => $this->formatValues($mimeTypes),
|
|
|
|
'{{ file }}' => $this->formatValue($path),
|
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)
|
|
|
|
{
|
|
|
|
return strlen((string) $double) > strlen(round($double, $numberOfDecimals));
|
|
|
|
}
|
2010-07-15 11:47:10 +01:00
|
|
|
}
|