Drupal investigation

UrlValidator.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Context\ExecutionContextInterface;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\Validator\ConstraintValidator;
  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. (/?|/\S+|\?\S*|\#\S*) # a /, nothing, a / with something, a query or a fragment
  34. $~ixu';
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function validate($value, Constraint $constraint)
  39. {
  40. if (!$constraint instanceof Url) {
  41. throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Url');
  42. }
  43. if (null === $value) {
  44. return;
  45. }
  46. if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
  47. throw new UnexpectedTypeException($value, 'string');
  48. }
  49. $value = (string) $value;
  50. if ('' === $value) {
  51. return;
  52. }
  53. $pattern = sprintf(static::PATTERN, implode('|', $constraint->protocols));
  54. if (!preg_match($pattern, $value)) {
  55. if ($this->context instanceof ExecutionContextInterface) {
  56. $this->context->buildViolation($constraint->message)
  57. ->setParameter('{{ value }}', $this->formatValue($value))
  58. ->setCode(Url::INVALID_URL_ERROR)
  59. ->addViolation();
  60. } else {
  61. $this->buildViolation($constraint->message)
  62. ->setParameter('{{ value }}', $this->formatValue($value))
  63. ->setCode(Url::INVALID_URL_ERROR)
  64. ->addViolation();
  65. }
  66. return;
  67. }
  68. if ($constraint->checkDNS) {
  69. $host = parse_url($value, PHP_URL_HOST);
  70. if (!checkdnsrr($host, 'ANY')) {
  71. if ($this->context instanceof ExecutionContextInterface) {
  72. $this->context->buildViolation($constraint->dnsMessage)
  73. ->setParameter('{{ value }}', $this->formatValue($host))
  74. ->setCode(Url::INVALID_URL_ERROR)
  75. ->addViolation();
  76. } else {
  77. $this->buildViolation($constraint->dnsMessage)
  78. ->setParameter('{{ value }}', $this->formatValue($host))
  79. ->setCode(Url::INVALID_URL_ERROR)
  80. ->addViolation();
  81. }
  82. }
  83. }
  84. }
  85. }