IpUtils.php 2.9 KB

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