Drupal investigation

IpValidator.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * Validates whether a value is a valid IP address.
  17. *
  18. * @author Bernhard Schussek <bschussek@gmail.com>
  19. * @author Joseph Bielawski <stloyd@gmail.com>
  20. */
  21. class IpValidator extends ConstraintValidator
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function validate($value, Constraint $constraint)
  27. {
  28. if (!$constraint instanceof Ip) {
  29. throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Ip');
  30. }
  31. if (null === $value || '' === $value) {
  32. return;
  33. }
  34. if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
  35. throw new UnexpectedTypeException($value, 'string');
  36. }
  37. $value = (string) $value;
  38. switch ($constraint->version) {
  39. case Ip::V4:
  40. $flag = FILTER_FLAG_IPV4;
  41. break;
  42. case Ip::V6:
  43. $flag = FILTER_FLAG_IPV6;
  44. break;
  45. case Ip::V4_NO_PRIV:
  46. $flag = FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE;
  47. break;
  48. case Ip::V6_NO_PRIV:
  49. $flag = FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE;
  50. break;
  51. case Ip::ALL_NO_PRIV:
  52. $flag = FILTER_FLAG_NO_PRIV_RANGE;
  53. break;
  54. case Ip::V4_NO_RES:
  55. $flag = FILTER_FLAG_IPV4 | FILTER_FLAG_NO_RES_RANGE;
  56. break;
  57. case Ip::V6_NO_RES:
  58. $flag = FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE;
  59. break;
  60. case Ip::ALL_NO_RES:
  61. $flag = FILTER_FLAG_NO_RES_RANGE;
  62. break;
  63. case Ip::V4_ONLY_PUBLIC:
  64. $flag = FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
  65. break;
  66. case Ip::V6_ONLY_PUBLIC:
  67. $flag = FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
  68. break;
  69. case Ip::ALL_ONLY_PUBLIC:
  70. $flag = FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
  71. break;
  72. default:
  73. $flag = null;
  74. break;
  75. }
  76. if (!filter_var($value, FILTER_VALIDATE_IP, $flag)) {
  77. if ($this->context instanceof ExecutionContextInterface) {
  78. $this->context->buildViolation($constraint->message)
  79. ->setParameter('{{ value }}', $this->formatValue($value))
  80. ->setCode(Ip::INVALID_IP_ERROR)
  81. ->addViolation();
  82. } else {
  83. $this->buildViolation($constraint->message)
  84. ->setParameter('{{ value }}', $this->formatValue($value))
  85. ->setCode(Ip::INVALID_IP_ERROR)
  86. ->addViolation();
  87. }
  88. }
  89. }
  90. }