Bladeren bron

merged branch lsmith77/checkIp (PR #1322)

Commits
-------

2f04bdb make checkIp() reuseable

Discussion
----------

[RequestMatcher] make checkIp() reuseable
Fabien Potencier 14 jaren geleden
bovenliggende
commit
b763d667f5
1 gewijzigde bestanden met toevoegingen van 13 en 13 verwijderingen
  1. 13 13
      src/Symfony/Component/HttpFoundation/RequestMatcher.php

+ 13 - 13
src/Symfony/Component/HttpFoundation/RequestMatcher.php

@@ -121,49 +121,49 @@ class RequestMatcher implements RequestMatcherInterface
             return false;
         }
 
-        if (null !== $this->ip && !$this->checkIp($request->getClientIp())) {
+        if (null !== $this->ip && !$this->checkIp($request->getClientIp(), $this->ip)) {
             return false;
         }
 
         return true;
     }
 
-    protected function checkIp($ip)
+    protected function checkIp($requestIp, $ip)
     {
         // IPv6 address
-        if (false !== strpos($ip, ':')) {
-            return $this->checkIp6($ip);
+        if (false !== strpos($requestIp, ':')) {
+            return $this->checkIp6($requestIp, $ip);
         } else {
-            return $this->checkIp4($ip);
+            return $this->checkIp4($requestIp, $ip);
         }
     }
 
-    protected function checkIp4($ip)
+    protected function checkIp4($requestIp, $ip)
     {
-        if (false !== strpos($this->ip, '/')) {
-            list($address, $netmask) = explode('/', $this->ip);
+        if (false !== strpos($ip, '/')) {
+            list($address, $netmask) = explode('/', $ip);
 
             if ($netmask < 1 || $netmask > 32) {
                 return false;
             }
         } else {
-            $address = $this->ip;
+            $address = $ip;
             $netmask = 32;
         }
 
-        return 0 === substr_compare(sprintf('%032b', ip2long($ip)), sprintf('%032b', ip2long($address)), 0, $netmask);
+        return 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
     }
 
     /**
      * @author David Soria Parra <dsp at php dot net>
      * @see https://github.com/dsp/v6tools
      */
-    protected function checkIp6($ip)
+    protected function checkIp6($requestIp, $ip)
     {
-        list($address, $netmask) = explode('/', $this->ip);
+        list($address, $netmask) = explode('/', $ip);
 
         $bytes_addr = unpack("n*", inet_pton($address));
-        $bytes_test = unpack("n*", inet_pton($ip));
+        $bytes_test = unpack("n*", inet_pton($requestIp));
 
         for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; $i++) {
             $left = $netmask - 16 * ($i-1);