Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / validator / Constraints / ImageValidator.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Constraints;
13
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
16 use Symfony\Component\Validator\Exception\RuntimeException;
17 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
18
19 /**
20  * Validates whether a value is a valid image file and is valid
21  * against minWidth, maxWidth, minHeight and maxHeight constraints.
22  *
23  * @author Benjamin Dulau <benjamin.dulau@gmail.com>
24  * @author Bernhard Schussek <bschussek@gmail.com>
25  */
26 class ImageValidator extends FileValidator
27 {
28     /**
29      * {@inheritdoc}
30      */
31     public function validate($value, Constraint $constraint)
32     {
33         if (!$constraint instanceof Image) {
34             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Image');
35         }
36
37         $violations = count($this->context->getViolations());
38
39         parent::validate($value, $constraint);
40
41         $failed = count($this->context->getViolations()) !== $violations;
42
43         if ($failed || null === $value || '' === $value) {
44             return;
45         }
46
47         if (null === $constraint->minWidth && null === $constraint->maxWidth
48             && null === $constraint->minHeight && null === $constraint->maxHeight
49             && null === $constraint->minRatio && null === $constraint->maxRatio
50             && $constraint->allowSquare && $constraint->allowLandscape && $constraint->allowPortrait
51             && !$constraint->detectCorrupted) {
52             return;
53         }
54
55         $size = @getimagesize($value);
56
57         if (empty($size) || ($size[0] === 0) || ($size[1] === 0)) {
58             $this->context->buildViolation($constraint->sizeNotDetectedMessage)
59                 ->setCode(Image::SIZE_NOT_DETECTED_ERROR)
60                 ->addViolation();
61
62             return;
63         }
64
65         $width = $size[0];
66         $height = $size[1];
67
68         if ($constraint->minWidth) {
69             if (!ctype_digit((string) $constraint->minWidth)) {
70                 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum width', $constraint->minWidth));
71             }
72
73             if ($width < $constraint->minWidth) {
74                 $this->context->buildViolation($constraint->minWidthMessage)
75                     ->setParameter('{{ width }}', $width)
76                     ->setParameter('{{ min_width }}', $constraint->minWidth)
77                     ->setCode(Image::TOO_NARROW_ERROR)
78                     ->addViolation();
79
80                 return;
81             }
82         }
83
84         if ($constraint->maxWidth) {
85             if (!ctype_digit((string) $constraint->maxWidth)) {
86                 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum width', $constraint->maxWidth));
87             }
88
89             if ($width > $constraint->maxWidth) {
90                 $this->context->buildViolation($constraint->maxWidthMessage)
91                     ->setParameter('{{ width }}', $width)
92                     ->setParameter('{{ max_width }}', $constraint->maxWidth)
93                     ->setCode(Image::TOO_WIDE_ERROR)
94                     ->addViolation();
95
96                 return;
97             }
98         }
99
100         if ($constraint->minHeight) {
101             if (!ctype_digit((string) $constraint->minHeight)) {
102                 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum height', $constraint->minHeight));
103             }
104
105             if ($height < $constraint->minHeight) {
106                 $this->context->buildViolation($constraint->minHeightMessage)
107                     ->setParameter('{{ height }}', $height)
108                     ->setParameter('{{ min_height }}', $constraint->minHeight)
109                     ->setCode(Image::TOO_LOW_ERROR)
110                     ->addViolation();
111
112                 return;
113             }
114         }
115
116         if ($constraint->maxHeight) {
117             if (!ctype_digit((string) $constraint->maxHeight)) {
118                 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum height', $constraint->maxHeight));
119             }
120
121             if ($height > $constraint->maxHeight) {
122                 $this->context->buildViolation($constraint->maxHeightMessage)
123                     ->setParameter('{{ height }}', $height)
124                     ->setParameter('{{ max_height }}', $constraint->maxHeight)
125                     ->setCode(Image::TOO_HIGH_ERROR)
126                     ->addViolation();
127             }
128         }
129
130         $ratio = round($width / $height, 2);
131
132         if (null !== $constraint->minRatio) {
133             if (!is_numeric((string) $constraint->minRatio)) {
134                 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum ratio', $constraint->minRatio));
135             }
136
137             if ($ratio < $constraint->minRatio) {
138                 $this->context->buildViolation($constraint->minRatioMessage)
139                     ->setParameter('{{ ratio }}', $ratio)
140                     ->setParameter('{{ min_ratio }}', $constraint->minRatio)
141                     ->setCode(Image::RATIO_TOO_SMALL_ERROR)
142                     ->addViolation();
143             }
144         }
145
146         if (null !== $constraint->maxRatio) {
147             if (!is_numeric((string) $constraint->maxRatio)) {
148                 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum ratio', $constraint->maxRatio));
149             }
150
151             if ($ratio > $constraint->maxRatio) {
152                 $this->context->buildViolation($constraint->maxRatioMessage)
153                     ->setParameter('{{ ratio }}', $ratio)
154                     ->setParameter('{{ max_ratio }}', $constraint->maxRatio)
155                     ->setCode(Image::RATIO_TOO_BIG_ERROR)
156                     ->addViolation();
157             }
158         }
159
160         if (!$constraint->allowSquare && $width == $height) {
161             $this->context->buildViolation($constraint->allowSquareMessage)
162                 ->setParameter('{{ width }}', $width)
163                 ->setParameter('{{ height }}', $height)
164                 ->setCode(Image::SQUARE_NOT_ALLOWED_ERROR)
165                 ->addViolation();
166         }
167
168         if (!$constraint->allowLandscape && $width > $height) {
169             $this->context->buildViolation($constraint->allowLandscapeMessage)
170                 ->setParameter('{{ width }}', $width)
171                 ->setParameter('{{ height }}', $height)
172                 ->setCode(Image::LANDSCAPE_NOT_ALLOWED_ERROR)
173                 ->addViolation();
174         }
175
176         if (!$constraint->allowPortrait && $width < $height) {
177             $this->context->buildViolation($constraint->allowPortraitMessage)
178                 ->setParameter('{{ width }}', $width)
179                 ->setParameter('{{ height }}', $height)
180                 ->setCode(Image::PORTRAIT_NOT_ALLOWED_ERROR)
181                 ->addViolation();
182         }
183
184         if ($constraint->detectCorrupted) {
185             if (!function_exists('imagecreatefromstring')) {
186                 throw new RuntimeException('Corrupted images detection requires installed and enabled GD extension');
187             }
188
189             $resource = @imagecreatefromstring(file_get_contents($value));
190
191             if (false === $resource) {
192                 $this->context->buildViolation($constraint->corruptedMessage)
193                     ->setCode(Image::CORRUPTED_IMAGE_ERROR)
194                     ->addViolation();
195
196                 return;
197             }
198
199             imagedestroy($resource);
200         }
201     }
202 }