Updating Media dependent modules to versions compatible with core Media.
[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\Constraint;
15 use Symfony\Component\Validator\ConstraintValidator;
16 use Symfony\Component\Validator\Exception\InvalidOptionsException;
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})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-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[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 || '' === $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             $this->context->buildViolation($constraint->message)
68                 ->setParameter('{{ value }}', $this->formatValue($value))
69                 ->setCode(Url::INVALID_URL_ERROR)
70                 ->addViolation();
71
72             return;
73         }
74
75         if ($constraint->checkDNS) {
76             // backwards compatibility
77             if (true === $constraint->checkDNS) {
78                 $constraint->checkDNS = Url::CHECK_DNS_TYPE_ANY;
79                 @trigger_error(sprintf('Use of the boolean TRUE for the "checkDNS" option in %s is deprecated.  Use Url::CHECK_DNS_TYPE_ANY instead.', Url::class), E_USER_DEPRECATED);
80             }
81
82             if (!in_array($constraint->checkDNS, array(
83                 Url::CHECK_DNS_TYPE_ANY,
84                 Url::CHECK_DNS_TYPE_A,
85                 Url::CHECK_DNS_TYPE_A6,
86                 Url::CHECK_DNS_TYPE_AAAA,
87                 Url::CHECK_DNS_TYPE_CNAME,
88                 Url::CHECK_DNS_TYPE_MX,
89                 Url::CHECK_DNS_TYPE_NAPTR,
90                 Url::CHECK_DNS_TYPE_NS,
91                 Url::CHECK_DNS_TYPE_PTR,
92                 Url::CHECK_DNS_TYPE_SOA,
93                 Url::CHECK_DNS_TYPE_SRV,
94                 Url::CHECK_DNS_TYPE_TXT,
95             ), true)) {
96                 throw new InvalidOptionsException(sprintf('Invalid value for option "checkDNS" in constraint %s', get_class($constraint)), array('checkDNS'));
97             }
98
99             $host = parse_url($value, PHP_URL_HOST);
100
101             if (!is_string($host) || !checkdnsrr($host, $constraint->checkDNS)) {
102                 $this->context->buildViolation($constraint->dnsMessage)
103                     ->setParameter('{{ value }}', $this->formatValue($host))
104                     ->setCode(Url::INVALID_URL_ERROR)
105                     ->addViolation();
106             }
107         }
108     }
109 }