IpUtils.php 4.4 KB

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