Yaffs site version 1.1
[yaffs-website] / vendor / symfony / validator / Constraints / UrlValidator.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\Context\ExecutionContextInterface;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\ConstraintValidator;
17 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
18
19 /**
20  * @author Bernhard Schussek <bschussek@gmail.com>
21  */
22 class UrlValidator extends ConstraintValidator
23 {
24     const PATTERN = '~^
25             (%s)://                                 # protocol
26             (([\pL\pN-]+:)?([\pL\pN-]+)@)?          # basic auth
27             (
28                 ([\pL\pN\pS-\.])+(\.?([\pL\pN]|xn\-\-[\pL\pN-]+)+\.?) # a domain name
29                     |                                                 # or
30                 \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}                    # an IP address
31                     |                                                 # or
32                 \[
33                     (?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){4})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]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:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::))))
34                 \]  # an IPv6 address
35             )
36             (:[0-9]+)?                              # a port (optional)
37             (?:/ (?:[\pL\pN\-._\~!$&\'()*+,;=:@]|%%[0-9A-Fa-f]{2})* )*      # a path
38             (?:\? (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )?   # a query (optional)
39             (?:\# (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )?   # a fragment (optional)
40         $~ixu';
41
42     /**
43      * {@inheritdoc}
44      */
45     public function validate($value, Constraint $constraint)
46     {
47         if (!$constraint instanceof Url) {
48             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Url');
49         }
50
51         if (null === $value) {
52             return;
53         }
54
55         if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
56             throw new UnexpectedTypeException($value, 'string');
57         }
58
59         $value = (string) $value;
60         if ('' === $value) {
61             return;
62         }
63
64         $pattern = sprintf(static::PATTERN, implode('|', $constraint->protocols));
65
66         if (!preg_match($pattern, $value)) {
67             if ($this->context instanceof ExecutionContextInterface) {
68                 $this->context->buildViolation($constraint->message)
69                     ->setParameter('{{ value }}', $this->formatValue($value))
70                     ->setCode(Url::INVALID_URL_ERROR)
71                     ->addViolation();
72             } else {
73                 $this->buildViolation($constraint->message)
74                     ->setParameter('{{ value }}', $this->formatValue($value))
75                     ->setCode(Url::INVALID_URL_ERROR)
76                     ->addViolation();
77             }
78
79             return;
80         }
81
82         if ($constraint->checkDNS) {
83             $host = parse_url($value, PHP_URL_HOST);
84
85             if (!is_string($host) || !checkdnsrr($host, 'ANY')) {
86                 if ($this->context instanceof ExecutionContextInterface) {
87                     $this->context->buildViolation($constraint->dnsMessage)
88                         ->setParameter('{{ value }}', $this->formatValue($host))
89                         ->setCode(Url::INVALID_URL_ERROR)
90                         ->addViolation();
91                 } else {
92                     $this->buildViolation($constraint->dnsMessage)
93                         ->setParameter('{{ value }}', $this->formatValue($host))
94                         ->setCode(Url::INVALID_URL_ERROR)
95                         ->addViolation();
96                 }
97             }
98         }
99     }
100 }