PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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