IpUtils.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\HttpFoundation;
  11. /**
  12. * Http utility functions.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class IpUtils
  17. {
  18. /**
  19. * This class should not be instantiated
  20. */
  21. private function __construct() {}
  22. /**
  23. * Validates an IPv4 or IPv6 address.
  24. *
  25. * @param string $requestIp
  26. * @param string|array $ips
  27. *
  28. * @return boolean Whether the IP is valid
  29. */
  30. public static function checkIp($requestIp, $ips)
  31. {
  32. if (!is_array($ips)) {
  33. $ips = array($ips);
  34. }
  35. $method = false !== strpos($requestIp, ':') ? 'checkIp6': 'checkIp4';
  36. foreach ($ips as $ip) {
  37. if (self::$method($requestIp, $ip)) {
  38. return true;
  39. }
  40. }
  41. return false;
  42. }
  43. /**
  44. * Validates an IPv4 address.
  45. *
  46. * @param string $requestIp
  47. * @param string $ip
  48. *
  49. * @return boolean Whether the IP is valid
  50. */
  51. public static function checkIp4($requestIp, $ip)
  52. {
  53. if (false !== strpos($ip, '/')) {
  54. list($address, $netmask) = explode('/', $ip, 2);
  55. if ($netmask < 1 || $netmask > 32) {
  56. return false;
  57. }
  58. } else {
  59. $address = $ip;
  60. $netmask = 32;
  61. }
  62. return 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
  63. }
  64. /**
  65. * Validates an IPv6 address.
  66. *
  67. * @author David Soria Parra <dsp at php dot net>
  68. * @see https://github.com/dsp/v6tools
  69. *
  70. * @param string $requestIp
  71. * @param string $ip
  72. *
  73. * @return boolean Whether the IP is valid
  74. *
  75. * @throws \RuntimeException When IPV6 support is not enabled
  76. */
  77. public static function checkIp6($requestIp, $ip)
  78. {
  79. if (!((extension_loaded('sockets') && defined('AF_INET6')) || @inet_pton('::1'))) {
  80. throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
  81. }
  82. if (false !== strpos($ip, '/')) {
  83. list($address, $netmask) = explode('/', $ip, 2);
  84. if ($netmask < 1 || $netmask > 128) {
  85. return false;
  86. }
  87. } else {
  88. $address = $ip;
  89. $netmask = 128;
  90. }
  91. $bytesAddr = unpack("n*", inet_pton($address));
  92. $bytesTest = unpack("n*", inet_pton($requestIp));
  93. for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; $i++) {
  94. $left = $netmask - 16 * ($i-1);
  95. $left = ($left <= 16) ? $left : 16;
  96. $mask = ~(0xffff >> $left) & 0xffff;
  97. if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {
  98. return false;
  99. }
  100. }
  101. return true;
  102. }
  103. }