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;
|
|
|
|
|
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-09-09 13:40:40 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\File as FileObject;
|
2011-07-05 19:05:01 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
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
|
|
|
|
{
|
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
|
|
|
{
|
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)) {
|
|
|
|
$maxSize = (int) $constraint->maxSize;
|
|
|
|
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
|
|
|
|
$maxSize = $constraint->maxSize * 1024;
|
|
|
|
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
|
|
|
|
$maxSize = $constraint->maxSize * 1048576;
|
|
|
|
} else {
|
|
|
|
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
|
|
|
|
}
|
|
|
|
$maxSize = min(UploadedFile::getMaxFilesize(), $maxSize);
|
|
|
|
} else {
|
|
|
|
$maxSize = UploadedFile::getMaxFilesize();
|
|
|
|
}
|
|
|
|
|
2012-05-11 15:14:27 +01:00
|
|
|
$this->context->addViolation($constraint->uploadIniSizeErrorMessage, array(
|
|
|
|
'{{ limit }}' => $maxSize,
|
|
|
|
'{{ 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)) {
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->addViolation($constraint->notFoundMessage, array('{{ file }}' => $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 (!is_readable($path)) {
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->addViolation($constraint->notReadableMessage, array('{{ file }}' => $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->maxSize) {
|
2011-03-31 13:02:00 +01:00
|
|
|
if (ctype_digit((string) $constraint->maxSize)) {
|
2010-06-24 10:24:08 +01:00
|
|
|
$size = filesize($path);
|
2013-03-13 14:14:49 +00:00
|
|
|
$limit = (int) $constraint->maxSize;
|
2012-05-08 20:06:26 +01:00
|
|
|
$suffix = 'bytes';
|
2013-03-13 14:14:49 +00:00
|
|
|
} elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
|
2010-06-24 10:24:08 +01:00
|
|
|
$size = round(filesize($path) / 1000, 2);
|
2013-03-13 14:14:49 +00:00
|
|
|
$limit = (int) $constraint->maxSize;
|
2012-05-08 20:06:26 +01:00
|
|
|
$suffix = 'kB';
|
2013-03-13 14:14:49 +00:00
|
|
|
} elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
|
2010-06-24 10:24:08 +01:00
|
|
|
$size = round(filesize($path) / 1000000, 2);
|
2013-03-13 14:14:49 +00:00
|
|
|
$limit = (int) $constraint->maxSize;
|
2012-05-08 20:06:26 +01:00
|
|
|
$suffix = 'MB';
|
2010-06-24 10:24:08 +01:00
|
|
|
} else {
|
|
|
|
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($size > $limit) {
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->addViolation($constraint->maxSizeMessage, array(
|
2014-04-10 16:39:39 +01:00
|
|
|
'{{ size }}' => $size,
|
|
|
|
'{{ limit }}' => $limit,
|
|
|
|
'{{ suffix }}' => $suffix,
|
|
|
|
'{{ file }}' => $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-04-10 16:39:39 +01:00
|
|
|
'{{ type }}' => '"'.$mime.'"',
|
|
|
|
'{{ types }}' => '"'.implode('", "', $mimeTypes) .'"',
|
|
|
|
'{{ file }}' => $path,
|
2010-06-24 10:24:08 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
2010-07-15 11:47:10 +01:00
|
|
|
}
|