PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/src/Symfony/Component/HttpFoundation/IpUtils.php

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