IpUtils.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 (!filter_var($requestIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  57. return false;
  58. }
  59. if (false !== strpos($ip, '/')) {
  60. list($address, $netmask) = explode('/', $ip, 2);
  61. if ($netmask === '0') {
  62. return filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
  63. }
  64. if ($netmask < 0 || $netmask > 32) {
  65. return false;
  66. }
  67. } else {
  68. $address = $ip;
  69. $netmask = 32;
  70. }
  71. return 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
  72. }
  73. /**
  74. * Compares two IPv6 addresses.
  75. * In case a subnet is given, it checks if it contains the request IP.
  76. *
  77. * @author David Soria Parra <dsp at php dot net>
  78. *
  79. * @see https://github.com/dsp/v6tools
  80. *
  81. * @param string $requestIp IPv6 address to check
  82. * @param string $ip IPv6 address or subnet in CIDR notation
  83. *
  84. * @return bool Whether the IP is valid
  85. *
  86. * @throws \RuntimeException When IPV6 support is not enabled
  87. */
  88. public static function checkIp6($requestIp, $ip)
  89. {
  90. if (!((extension_loaded('sockets') && defined('AF_INET6')) || @inet_pton('::1'))) {
  91. throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
  92. }
  93. if (false !== strpos($ip, '/')) {
  94. list($address, $netmask) = explode('/', $ip, 2);
  95. if ($netmask < 1 || $netmask > 128) {
  96. return false;
  97. }
  98. } else {
  99. $address = $ip;
  100. $netmask = 128;
  101. }
  102. $bytesAddr = unpack('n*', @inet_pton($address));
  103. $bytesTest = unpack('n*', @inet_pton($requestIp));
  104. if (!$bytesAddr || !$bytesTest) {
  105. return false;
  106. }
  107. for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) {
  108. $left = $netmask - 16 * ($i - 1);
  109. $left = ($left <= 16) ? $left : 16;
  110. $mask = ~(0xffff >> $left) & 0xffff;
  111. if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {
  112. return false;
  113. }
  114. }
  115. return true;
  116. }
  117. }