UrlValidator.php 5.4 KB

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