Skip to content

Commit

Permalink
make checkIp() reuseable
Browse files Browse the repository at this point in the history
  • Loading branch information
lsmith77 committed Jun 14, 2011
1 parent f1e8c1b commit 2f04bdb
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/Symfony/Component/HttpFoundation/RequestMatcher.php
Expand Up @@ -121,49 +121,49 @@ public function matches(Request $request)
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);
Expand Down

0 comments on commit 2f04bdb

Please sign in to comment.