1616 * @author Fabien Potencier <fabien@symfony.com>
1717 */
1818class IpUtils {
19+ /**
20+ * @var array<string,bool>
21+ */
1922 private static $ checkedIps = [];
2023
2124 /**
@@ -31,13 +34,7 @@ private function __construct() {
3134 *
3235 * @return bool
3336 */
34- public static function checkIp (?string $ requestIp , $ ips ) {
35- if (null === $ requestIp ) {
36- trigger_deprecation ('symfony/http-foundation ' , '5.4 ' , 'Passing null as $requestIp to "%s()" is deprecated, pass an empty string instead. ' , __METHOD__ );
37-
38- return false ;
39- }
40-
37+ public static function checkIp (string $ requestIp , $ ips ) {
4138 if (!\is_array ($ ips )) {
4239 $ ips = [$ ips ];
4340 }
@@ -61,13 +58,7 @@ public static function checkIp(?string $requestIp, $ips) {
6158 *
6259 * @return bool Whether the request IP matches the IP, or whether the request IP is within the CIDR subnet
6360 */
64- public static function checkIp4 (?string $ requestIp , string $ ip ) {
65- if (null === $ requestIp ) {
66- trigger_deprecation ('symfony/http-foundation ' , '5.4 ' , 'Passing null as $requestIp to "%s()" is deprecated, pass an empty string instead. ' , __METHOD__ );
67-
68- return false ;
69- }
70-
61+ public static function checkIp4 (string $ requestIp , string $ ip ) {
7162 $ cacheKey = $ requestIp .'- ' .$ ip ;
7263 if (isset (self ::$ checkedIps [$ cacheKey ])) {
7364 return self ::$ checkedIps [$ cacheKey ];
@@ -81,9 +72,11 @@ public static function checkIp4(?string $requestIp, string $ip) {
8172 [$ address , $ netmask ] = explode ('/ ' , $ ip , 2 );
8273
8374 if ('0 ' === $ netmask ) {
84- return self ::$ checkedIps [$ cacheKey ] = filter_var ($ address , \FILTER_VALIDATE_IP , \FILTER_FLAG_IPV4 );
75+ return self ::$ checkedIps [$ cacheKey ] = ( bool ) filter_var ($ address , \FILTER_VALIDATE_IP , \FILTER_FLAG_IPV4 );
8576 }
8677
78+ $ netmask = (int )$ netmask ;
79+
8780 if ($ netmask < 0 || $ netmask > 32 ) {
8881 return self ::$ checkedIps [$ cacheKey ] = false ;
8982 }
@@ -113,13 +106,7 @@ public static function checkIp4(?string $requestIp, string $ip) {
113106 *
114107 * @throws \RuntimeException When IPV6 support is not enabled
115108 */
116- public static function checkIp6 (?string $ requestIp , string $ ip ) {
117- if (null === $ requestIp ) {
118- trigger_deprecation ('symfony/http-foundation ' , '5.4 ' , 'Passing null as $requestIp to "%s()" is deprecated, pass an empty string instead. ' , __METHOD__ );
119-
120- return false ;
121- }
122-
109+ public static function checkIp6 (string $ requestIp , string $ ip ) {
123110 $ cacheKey = $ requestIp .'- ' .$ ip ;
124111 if (isset (self ::$ checkedIps [$ cacheKey ])) {
125112 return self ::$ checkedIps [$ cacheKey ];
@@ -136,6 +123,8 @@ public static function checkIp6(?string $requestIp, string $ip) {
136123 return (bool ) unpack ('n* ' , @inet_pton ($ address ));
137124 }
138125
126+ $ netmask = (int )$ netmask ;
127+
139128 if ($ netmask < 1 || $ netmask > 128 ) {
140129 return self ::$ checkedIps [$ cacheKey ] = false ;
141130 }
0 commit comments