IpUtils.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. }
  24. /**
  25. * Checks if an IPv4 or IPv6 address is contained in the list of given IPs or subnets.
  26. *
  27. * @param string $requestIp IP to check
  28. * @param string|array $ips List of IPs or subnets (can be a string if only a single one)
  29. *
  30. * @return bool Whether the IP is valid
  31. */
  32. public static function checkIp($requestIp, $ips)
  33. {
  34. if (!is_array($ips)) {
  35. $ips = array($ips);
  36. }
  37. $method = substr_count($requestIp, ':') > 1 ? 'checkIp6' : 'checkIp4';
  38. foreach ($ips as $ip) {
  39. if (self::$method($requestIp, $ip)) {
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45. /**
  46. * Compares two IPv4 addresses.
  47. * In case a subnet is given, it checks if it contains the request IP.
  48. *
  49. * @param string $requestIp IPv4 address to check
  50. * @param string $ip IPv4 address or subnet in CIDR notation
  51. *
  52. * @return bool Whether the request IP matches the IP, or whether the request IP is within the CIDR subnet.
  53. */
  54. public static function checkIp4($requestIp, $ip)
  55. {
  56. if (false !== strpos($ip, '/')) {
  57. list($address, $netmask) = explode('/', $ip, 2);
  58. if ($netmask === '0') {
  59. // Ensure IP is valid - using ip2long below implicitly validates, but we need to do it manually here
  60. return filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
  61. }
  62. if ($netmask < 0 || $netmask > 32) {
  63. return false;
  64. }
  65. } else {
  66. $address = $ip;
  67. $netmask = 32;
  68. }
  69. return 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
  70. }
  71. /**
  72. * Compares two IPv6 addresses.
  73. * In case a subnet is given, it checks if it contains the request IP.
  74. *
  75. * @author David Soria Parra <dsp at php dot net>
  76. *
  77. * @see https://github.com/dsp/v6tools
  78. *
  79. * @param string $requestIp IPv6 address to check
  80. * @param string $ip IPv6 address or subnet in CIDR notation
  81. *
  82. * @return bool Whether the IP is valid
  83. *
  84. * @throws \RuntimeException When IPV6 support is not enabled
  85. */
  86. public static function checkIp6($requestIp, $ip)
  87. {
  88. if (!((extension_loaded('sockets') && defined('AF_INET6')) || @inet_pton('::1'))) {
  89. throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
  90. }
  91. if (false !== strpos($ip, '/')) {
  92. list($address, $netmask) = explode('/', $ip, 2);
  93. if ($netmask < 1 || $netmask > 128) {
  94. return false;
  95. }
  96. } else {
  97. $address = $ip;
  98. $netmask = 128;
  99. }
  100. $bytesAddr = unpack('n*', inet_pton($address));
  101. $bytesTest = unpack('n*', inet_pton($requestIp));
  102. for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) {
  103. $left = $netmask - 16 * ($i - 1);
  104. $left = ($left <= 16) ? $left : 16;
  105. $mask = ~(0xffff >> $left) & 0xffff;
  106. if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {
  107. return false;
  108. }
  109. }
  110. return true;
  111. }
  112. }