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
|
|
|
/**
|
|
|
|
* Checks if the passed value is valid.
|
|
|
|
*
|
|
|
|
* @param mixed $value The value that should be validated
|
2011-08-10 15:11:31 +01:00
|
|
|
* @param Constraint $constraint The constraint for the validation
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function isValid($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:
|
2011-07-07 09:47:10 +01:00
|
|
|
$maxSize = UploadedFile::getMaxFilesize();
|
2011-07-05 19:05:01 +01:00
|
|
|
$maxSize = $constraint->maxSize ? min($maxSize, $constraint->maxSize) : $maxSize;
|
2012-02-01 12:53:45 +00:00
|
|
|
$this->context->addViolation($constraint->uploadIniSizeErrorMessage, array('{{ limit }}' => $maxSize.' 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);
|
|
|
|
$limit = $constraint->maxSize;
|
|
|
|
$suffix = ' bytes';
|
2011-12-18 13:42:59 +00:00
|
|
|
} elseif (preg_match('/^(\d+)k$/', $constraint->maxSize, $matches)) {
|
2010-06-24 10:24:08 +01:00
|
|
|
$size = round(filesize($path) / 1000, 2);
|
|
|
|
$limit = $matches[1];
|
|
|
|
$suffix = ' kB';
|
2011-12-18 13:42:59 +00:00
|
|
|
} elseif (preg_match('/^(\d+)M$/', $constraint->maxSize, $matches)) {
|
2010-06-24 10:24:08 +01:00
|
|
|
$size = round(filesize($path) / 1000000, 2);
|
|
|
|
$limit = $matches[1];
|
|
|
|
$suffix = ' MB';
|
|
|
|
} 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(
|
2011-06-23 13:07:53 +01:00
|
|
|
'{{ size }}' => $size.$suffix,
|
|
|
|
'{{ limit }}' => $limit.$suffix,
|
2011-06-14 09:47:04 +01:00
|
|
|
'{{ 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(
|
2011-09-01 15:10:16 +01:00
|
|
|
'{{ type }}' => '"'.$mime.'"',
|
|
|
|
'{{ types }}' => '"'.implode('", "', $mimeTypes) .'"',
|
2011-06-14 09:47:04 +01:00
|
|
|
'{{ 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
|
|
|
}
|