Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bcb8056

Browse filesBrowse files
Cache ipCheck
1 parent 102f930 commit bcb8056
Copy full SHA for bcb8056

File tree

1 file changed

+20
-8
lines changed
Filter options

1 file changed

+20
-8
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/IpUtils.php
+20-8Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
class IpUtils
2020
{
21+
private static $checkedIps = array();
22+
2123
/**
2224
* This class should not be instantiated.
2325
*/
@@ -61,26 +63,31 @@ public static function checkIp($requestIp, $ips)
6163
*/
6264
public static function checkIp4($requestIp, $ip)
6365
{
66+
$cacheKey = $requestIp.'-'.$ip;
67+
if (isset(self::$checkedIps[$cacheKey])) {
68+
return self::$checkedIps[$cacheKey];
69+
}
70+
6471
if (!filter_var($requestIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
65-
return false;
72+
return self::$checkedIps[$cacheKey] = false;
6673
}
6774

6875
if (false !== strpos($ip, '/')) {
6976
list($address, $netmask) = explode('/', $ip, 2);
7077

7178
if ($netmask === '0') {
72-
return filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
79+
return self::$checkedIps[$cacheKey] = filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
7380
}
7481

7582
if ($netmask < 0 || $netmask > 32) {
76-
return false;
83+
return self::$checkedIps[$cacheKey] = false;
7784
}
7885
} else {
7986
$address = $ip;
8087
$netmask = 32;
8188
}
8289

83-
return 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
90+
return self::$checkedIps[$cacheKey] = 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
8491
}
8592

8693
/**
@@ -100,6 +107,11 @@ public static function checkIp4($requestIp, $ip)
100107
*/
101108
public static function checkIp6($requestIp, $ip)
102109
{
110+
$cacheKey = $requestIp.'-'.$ip;
111+
if (isset(self::$checkedIps[$cacheKey])) {
112+
return self::$checkedIps[$cacheKey];
113+
}
114+
103115
if (!((extension_loaded('sockets') && defined('AF_INET6')) || @inet_pton('::1'))) {
104116
throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
105117
}
@@ -108,7 +120,7 @@ public static function checkIp6($requestIp, $ip)
108120
list($address, $netmask) = explode('/', $ip, 2);
109121

110122
if ($netmask < 1 || $netmask > 128) {
111-
return false;
123+
return self::$checkedIps[$cacheKey] = false;
112124
}
113125
} else {
114126
$address = $ip;
@@ -119,18 +131,18 @@ public static function checkIp6($requestIp, $ip)
119131
$bytesTest = unpack('n*', @inet_pton($requestIp));
120132

121133
if (!$bytesAddr || !$bytesTest) {
122-
return false;
134+
return self::$checkedIps[$cacheKey] = false;
123135
}
124136

125137
for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) {
126138
$left = $netmask - 16 * ($i - 1);
127139
$left = ($left <= 16) ? $left : 16;
128140
$mask = ~(0xffff >> $left) & 0xffff;
129141
if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {
130-
return false;
142+
return self::$checkedIps[$cacheKey] = false;
131143
}
132144
}
133145

134-
return true;
146+
return self::$checkedIps[$cacheKey] = true;
135147
}
136148
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.