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