18
18
*/
19
19
class IpUtils
20
20
{
21
+ private static $ checkedIps = array ();
22
+
21
23
/**
22
24
* This class should not be instantiated.
23
25
*/
@@ -61,26 +63,31 @@ public static function checkIp($requestIp, $ips)
61
63
*/
62
64
public static function checkIp4 ($ requestIp , $ ip )
63
65
{
66
+ $ cacheKey = $ requestIp .'- ' .$ ip ;
67
+ if (isset (self ::$ checkedIps [$ cacheKey ])) {
68
+ return self ::$ checkedIps [$ cacheKey ];
69
+ }
70
+
64
71
if (!filter_var ($ requestIp , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 )) {
65
- return false ;
72
+ return self :: $ checkedIps [ $ cacheKey ] = false ;
66
73
}
67
74
68
75
if (false !== strpos ($ ip , '/ ' )) {
69
76
list ($ address , $ netmask ) = explode ('/ ' , $ ip , 2 );
70
77
71
78
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 );
73
80
}
74
81
75
82
if ($ netmask < 0 || $ netmask > 32 ) {
76
- return false ;
83
+ return self :: $ checkedIps [ $ cacheKey ] = false ;
77
84
}
78
85
} else {
79
86
$ address = $ ip ;
80
87
$ netmask = 32 ;
81
88
}
82
89
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 );
84
91
}
85
92
86
93
/**
@@ -100,6 +107,11 @@ public static function checkIp4($requestIp, $ip)
100
107
*/
101
108
public static function checkIp6 ($ requestIp , $ ip )
102
109
{
110
+ $ cacheKey = $ requestIp .'- ' .$ ip ;
111
+ if (isset (self ::$ checkedIps [$ cacheKey ])) {
112
+ return self ::$ checkedIps [$ cacheKey ];
113
+ }
114
+
103
115
if (!((extension_loaded ('sockets ' ) && defined ('AF_INET6 ' )) || @inet_pton ('::1 ' ))) {
104
116
throw new \RuntimeException ('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6". ' );
105
117
}
@@ -108,7 +120,7 @@ public static function checkIp6($requestIp, $ip)
108
120
list ($ address , $ netmask ) = explode ('/ ' , $ ip , 2 );
109
121
110
122
if ($ netmask < 1 || $ netmask > 128 ) {
111
- return false ;
123
+ return self :: $ checkedIps [ $ cacheKey ] = false ;
112
124
}
113
125
} else {
114
126
$ address = $ ip ;
@@ -119,18 +131,18 @@ public static function checkIp6($requestIp, $ip)
119
131
$ bytesTest = unpack ('n* ' , @inet_pton ($ requestIp ));
120
132
121
133
if (!$ bytesAddr || !$ bytesTest ) {
122
- return false ;
134
+ return self :: $ checkedIps [ $ cacheKey ] = false ;
123
135
}
124
136
125
137
for ($ i = 1 , $ ceil = ceil ($ netmask / 16 ); $ i <= $ ceil ; ++$ i ) {
126
138
$ left = $ netmask - 16 * ($ i - 1 );
127
139
$ left = ($ left <= 16 ) ? $ left : 16 ;
128
140
$ mask = ~(0xffff >> $ left ) & 0xffff ;
129
141
if (($ bytesAddr [$ i ] & $ mask ) != ($ bytesTest [$ i ] & $ mask )) {
130
- return false ;
142
+ return self :: $ checkedIps [ $ cacheKey ] = false ;
131
143
}
132
144
}
133
145
134
- return true ;
146
+ return self :: $ checkedIps [ $ cacheKey ] = true ;
135
147
}
136
148
}
0 commit comments