Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Validator\Constraints;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Validator\Constraint;
|
Chris@0
|
15 use Symfony\Component\Validator\ConstraintValidator;
|
Chris@14
|
16 use Symfony\Component\Validator\Exception\InvalidOptionsException;
|
Chris@0
|
17 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * @author Bernhard Schussek <bschussek@gmail.com>
|
Chris@0
|
21 */
|
Chris@0
|
22 class UrlValidator extends ConstraintValidator
|
Chris@0
|
23 {
|
Chris@0
|
24 const PATTERN = '~^
|
Chris@0
|
25 (%s):// # protocol
|
Chris@14
|
26 (([\.\pL\pN-]+:)?([\.\pL\pN-]+)@)? # basic auth
|
Chris@0
|
27 (
|
Chris@14
|
28 ([\pL\pN\pS\-\.])+(\.?([\pL\pN]|xn\-\-[\pL\pN-]+)+\.?) # a domain name
|
Chris@0
|
29 | # or
|
Chris@0
|
30 \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # an IP address
|
Chris@0
|
31 | # or
|
Chris@0
|
32 \[
|
Chris@0
|
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])))))))|(?:(?:::(?:(?:(?:[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-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,4}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:(?:(?:(?:[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,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::))))
|
Chris@0
|
34 \] # an IPv6 address
|
Chris@0
|
35 )
|
Chris@0
|
36 (:[0-9]+)? # a port (optional)
|
Chris@0
|
37 (?:/ (?:[\pL\pN\-._\~!$&\'()*+,;=:@]|%%[0-9A-Fa-f]{2})* )* # a path
|
Chris@0
|
38 (?:\? (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )? # a query (optional)
|
Chris@0
|
39 (?:\# (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )? # a fragment (optional)
|
Chris@0
|
40 $~ixu';
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * {@inheritdoc}
|
Chris@0
|
44 */
|
Chris@0
|
45 public function validate($value, Constraint $constraint)
|
Chris@0
|
46 {
|
Chris@0
|
47 if (!$constraint instanceof Url) {
|
Chris@0
|
48 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Url');
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@12
|
51 if (null === $value || '' === $value) {
|
Chris@0
|
52 return;
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@17
|
55 if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
|
Chris@0
|
56 throw new UnexpectedTypeException($value, 'string');
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 $value = (string) $value;
|
Chris@0
|
60 if ('' === $value) {
|
Chris@0
|
61 return;
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 $pattern = sprintf(static::PATTERN, implode('|', $constraint->protocols));
|
Chris@0
|
65
|
Chris@0
|
66 if (!preg_match($pattern, $value)) {
|
Chris@0
|
67 $this->context->buildViolation($constraint->message)
|
Chris@0
|
68 ->setParameter('{{ value }}', $this->formatValue($value))
|
Chris@0
|
69 ->setCode(Url::INVALID_URL_ERROR)
|
Chris@0
|
70 ->addViolation();
|
Chris@0
|
71
|
Chris@0
|
72 return;
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 if ($constraint->checkDNS) {
|
Chris@14
|
76 // backwards compatibility
|
Chris@14
|
77 if (true === $constraint->checkDNS) {
|
Chris@14
|
78 $constraint->checkDNS = Url::CHECK_DNS_TYPE_ANY;
|
Chris@14
|
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);
|
Chris@14
|
80 }
|
Chris@14
|
81
|
Chris@17
|
82 if (!\in_array($constraint->checkDNS, [
|
Chris@14
|
83 Url::CHECK_DNS_TYPE_ANY,
|
Chris@14
|
84 Url::CHECK_DNS_TYPE_A,
|
Chris@14
|
85 Url::CHECK_DNS_TYPE_A6,
|
Chris@14
|
86 Url::CHECK_DNS_TYPE_AAAA,
|
Chris@14
|
87 Url::CHECK_DNS_TYPE_CNAME,
|
Chris@14
|
88 Url::CHECK_DNS_TYPE_MX,
|
Chris@14
|
89 Url::CHECK_DNS_TYPE_NAPTR,
|
Chris@14
|
90 Url::CHECK_DNS_TYPE_NS,
|
Chris@14
|
91 Url::CHECK_DNS_TYPE_PTR,
|
Chris@14
|
92 Url::CHECK_DNS_TYPE_SOA,
|
Chris@14
|
93 Url::CHECK_DNS_TYPE_SRV,
|
Chris@14
|
94 Url::CHECK_DNS_TYPE_TXT,
|
Chris@17
|
95 ], true)) {
|
Chris@17
|
96 throw new InvalidOptionsException(sprintf('Invalid value for option "checkDNS" in constraint %s', \get_class($constraint)), ['checkDNS']);
|
Chris@14
|
97 }
|
Chris@14
|
98
|
Chris@0
|
99 $host = parse_url($value, PHP_URL_HOST);
|
Chris@0
|
100
|
Chris@17
|
101 if (!\is_string($host) || !checkdnsrr($host, $constraint->checkDNS)) {
|
Chris@0
|
102 $this->context->buildViolation($constraint->dnsMessage)
|
Chris@0
|
103 ->setParameter('{{ value }}', $this->formatValue($host))
|
Chris@0
|
104 ->setCode(Url::INVALID_URL_ERROR)
|
Chris@0
|
105 ->addViolation();
|
Chris@0
|
106 }
|
Chris@0
|
107 }
|
Chris@0
|
108 }
|
Chris@0
|
109 }
|