PageRenderTime 31ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/laradock/vendor/symfony/http-foundation/IpUtils.php

https://gitlab.com/hoangduys4k5/laravelproject
PHP | 181 lines | 107 code | 28 blank | 46 comment | 29 complexity | f650702b22d559f8fecc4125e62636ee 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. private static array $checkedIps = [];
  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|array $ips List of IPs or subnets (can be a string if only a single one)
  29. */
  30. public static function checkIp(string $requestIp, string|array $ips): bool
  31. {
  32. if (!\is_array($ips)) {
  33. $ips = [$ips];
  34. }
  35. $method = substr_count($requestIp, ':') > 1 ? '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 $ip IPv4 address or subnet in CIDR notation
  48. *
  49. * @return bool Whether the request IP matches the IP, or whether the request IP is within the CIDR subnet
  50. */
  51. public static function checkIp4(string $requestIp, string $ip): bool
  52. {
  53. $cacheKey = $requestIp.'-'.$ip;
  54. if (isset(self::$checkedIps[$cacheKey])) {
  55. return self::$checkedIps[$cacheKey];
  56. }
  57. if (!filter_var($requestIp, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
  58. return self::$checkedIps[$cacheKey] = false;
  59. }
  60. if (str_contains($ip, '/')) {
  61. [$address, $netmask] = explode('/', $ip, 2);
  62. if ('0' === $netmask) {
  63. return self::$checkedIps[$cacheKey] = filter_var($address, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4);
  64. }
  65. if ($netmask < 0 || $netmask > 32) {
  66. return self::$checkedIps[$cacheKey] = false;
  67. }
  68. } else {
  69. $address = $ip;
  70. $netmask = 32;
  71. }
  72. if (false === ip2long($address)) {
  73. return self::$checkedIps[$cacheKey] = false;
  74. }
  75. return self::$checkedIps[$cacheKey] = 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
  76. }
  77. /**
  78. * Compares two IPv6 addresses.
  79. * In case a subnet is given, it checks if it contains the request IP.
  80. *
  81. * @author David Soria Parra <dsp at php dot net>
  82. *
  83. * @see https://github.com/dsp/v6tools
  84. *
  85. * @param string $ip IPv6 address or subnet in CIDR notation
  86. *
  87. * @throws \RuntimeException When IPV6 support is not enabled
  88. */
  89. public static function checkIp6(string $requestIp, string $ip): bool
  90. {
  91. $cacheKey = $requestIp.'-'.$ip;
  92. if (isset(self::$checkedIps[$cacheKey])) {
  93. return self::$checkedIps[$cacheKey];
  94. }
  95. if (!((\extension_loaded('sockets') && \defined('AF_INET6')) || @inet_pton('::1'))) {
  96. throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
  97. }
  98. if (str_contains($ip, '/')) {
  99. [$address, $netmask] = explode('/', $ip, 2);
  100. if ('0' === $netmask) {
  101. return (bool) unpack('n*', @inet_pton($address));
  102. }
  103. if ($netmask < 1 || $netmask > 128) {
  104. return self::$checkedIps[$cacheKey] = false;
  105. }
  106. } else {
  107. $address = $ip;
  108. $netmask = 128;
  109. }
  110. $bytesAddr = unpack('n*', @inet_pton($address));
  111. $bytesTest = unpack('n*', @inet_pton($requestIp));
  112. if (!$bytesAddr || !$bytesTest) {
  113. return self::$checkedIps[$cacheKey] = false;
  114. }
  115. for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) {
  116. $left = $netmask - 16 * ($i - 1);
  117. $left = ($left <= 16) ? $left : 16;
  118. $mask = ~(0xFFFF >> $left) & 0xFFFF;
  119. if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {
  120. return self::$checkedIps[$cacheKey] = false;
  121. }
  122. }
  123. return self::$checkedIps[$cacheKey] = true;
  124. }
  125. /**
  126. * Anonymizes an IP/IPv6.
  127. *
  128. * Removes the last byte for v4 and the last 8 bytes for v6 IPs
  129. */
  130. public static function anonymize(string $ip): string
  131. {
  132. $wrappedIPv6 = false;
  133. if ('[' === substr($ip, 0, 1) && ']' === substr($ip, -1, 1)) {
  134. $wrappedIPv6 = true;
  135. $ip = substr($ip, 1, -1);
  136. }
  137. $packedAddress = inet_pton($ip);
  138. if (4 === \strlen($packedAddress)) {
  139. $mask = '255.255.255.0';
  140. } elseif ($ip === inet_ntop($packedAddress & inet_pton('::ffff:ffff:ffff'))) {
  141. $mask = '::ffff:ffff:ff00';
  142. } elseif ($ip === inet_ntop($packedAddress & inet_pton('::ffff:ffff'))) {
  143. $mask = '::ffff:ff00';
  144. } else {
  145. $mask = 'ffff:ffff:ffff:ffff:0000:0000:0000:0000';
  146. }
  147. $ip = inet_ntop($packedAddress & inet_pton($mask));
  148. if ($wrappedIPv6) {
  149. $ip = '['.$ip.']';
  150. }
  151. return $ip;
  152. }
  153. }