No Description

IpUtils.php 3.4KB

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