IpValidator.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Exception\UnexpectedTypeException;
  14. /**
  15. * Validates whether a value is a valid IP address
  16. *
  17. * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  18. */
  19. class IpValidator extends ConstraintValidator
  20. {
  21. /**
  22. * @inheritDoc
  23. */
  24. public function isValid($value, Constraint $constraint)
  25. {
  26. if (null === $value || '' === $value) {
  27. return true;
  28. }
  29. if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString()'))) {
  30. throw new UnexpectedTypeException($value, 'string');
  31. }
  32. $value = (string) $value;
  33. $valid = false;
  34. if ($constraint->version == Ip::V4 || $constraint->version == Ip::ALL) {
  35. $valid = $this->isValidV4($value);
  36. }
  37. if ($constraint->version == Ip::V6 || $constraint->version == Ip::ALL) {
  38. $valid = $valid || $this->isValidV6($value);
  39. }
  40. if (!$valid) {
  41. $this->setMessage($constraint->message, array('{{ value }}' => $value));
  42. return false;
  43. }
  44. return true;
  45. }
  46. /**
  47. * Validates that a value is a valid IPv4 address
  48. *
  49. * @param string $value
  50. */
  51. protected function isValidV4($value)
  52. {
  53. if (!preg_match('/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $value, $matches)) {
  54. return false;
  55. }
  56. for ($i = 1; $i <= 4; ++$i) {
  57. if ($matches[$i] > 255) {
  58. return false;
  59. }
  60. }
  61. return true;
  62. }
  63. /**
  64. * Validates that a value is a valid IPv6 address
  65. *
  66. * @param string $value
  67. */
  68. protected function isValidV6($value)
  69. {
  70. if (!preg_match('/^[0-9a-fA-F]{0,4}(:[0-9a-fA-F]{0,4}){1,5}((:[0-9a-fA-F]{0,4}){1,2}|:([\d\.]+))$/', $value, $matches)) {
  71. return false;
  72. }
  73. // allow V4 addresses mapped to V6
  74. if (isset($matches[4]) && !$this->isValidV4($matches[4])) {
  75. return false;
  76. }
  77. // "::" is only allowed once per address
  78. if (($offset = strpos($value, '::')) !== false) {
  79. if (strpos($value, '::', $offset + 1) !== false) {
  80. return false;
  81. }
  82. }
  83. return true;
  84. }
  85. }