Skip to content

Commit

Permalink
Merge pull request #74 from geekwright/refresh-02132021
Browse files Browse the repository at this point in the history
Refresh for newer libraries
  • Loading branch information
geekwright authored Feb 13, 2021
2 parents c326560 + 2fac3d4 commit b842b55
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
},
"require": {
"php": ">=5.3.9",
"kint-php/kint": "^2.2",
"symfony/yaml": "2.8.*",
"kint-php/kint": "^3.3",
"symfony/yaml": "^2.8",
"paragonie/random_compat": "^2",
"firebase/php-jwt": "^5.0.0",
"firebase/php-jwt": "^5.0",
"webmozart/assert": "^1.2"
},
"require-dev": {
Expand Down
48 changes: 22 additions & 26 deletions src/IPAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @category Xmf\IPAddress
* @package Xmf
* @author trabis <[email protected]>
* @copyright 2018-2020 XOOPS Project (https://xoops.org)
* @copyright 2018-2021 XOOPS Project (https://xoops.org)
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
* @link https://xoops.org
*/
Expand Down Expand Up @@ -52,8 +52,7 @@ public static function fromRequest()
$proxyCheck = new ProxyCheck();
$proxyIP = $proxyCheck->get();
$ip = (false === $proxyIP) ? $ip : $proxyIP;
$instance = new $class($ip);
return $instance;
return new $class($ip);
}

/**
Expand All @@ -65,8 +64,7 @@ public static function fromRequest()
*/
protected function normalize($ip)
{
$normal = inet_ntop(inet_pton($ip));
return $normal;
return inet_ntop(inet_pton($ip));
}

/**
Expand All @@ -89,8 +87,7 @@ public function asBinary()
if (false === $this->ip) {
return false;
}
$binary = inet_pton($this->ip);
return $binary;
return inet_pton($this->ip);
}

/**
Expand All @@ -100,14 +97,15 @@ public function asBinary()
*/
public function ipVersion()
{
$return = false;
if (false === $this->ip) {
return false;
$return = false;
} elseif (false !== filter_var($this->ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return 4;
$return = 4;
} elseif (false !== filter_var($this->ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
return 6;
$return = 6;
}
return false;
return $return;
}

/**
Expand All @@ -125,20 +123,18 @@ public function ipVersion()
public function sameSubnet($matchIp, $netMask4, $netMask6)
{
$match = new IPAddress($matchIp);
if (false === $this->ipVersion() || ($this->ipVersion() !== $match->ipVersion())) {
return false;
}
switch ($this->ipVersion()) {
case 4:
$mask = (-1) << (32 - $netMask4);
return ((ip2long($this->ip) & $mask) === (ip2long($match->asReadable()) & $mask));
break;
case 6:
$ipBits = $this->asBinaryString($this);
$matchBits = $this->asBinaryString($match);
$match = (0 === strncmp($ipBits, $matchBits, $netMask6));
return $match;
break;
if (false !== $this->ipVersion() && ($this->ipVersion() === $match->ipVersion())) {
switch ($this->ipVersion()) {
case 4:
$mask = (-1) << (32 - $netMask4);
return ((ip2long($this->ip) & $mask) === (ip2long($match->asReadable()) & $mask));
case 6:
$ipBits = $this->asBinaryString($this);
$matchBits = $this->asBinaryString($match);
return (0 === strncmp($ipBits, $matchBits, $netMask6));
default:
break;
}
}
return false;
}
Expand All @@ -155,7 +151,7 @@ protected function asBinaryString(IPAddress $ip)
$length = (4 === $ip->ipVersion()) ? 4 : 16;
$binaryIp = $ip->asBinary();
$bits = '';
for ($i = 0; $i < $length; $i++) {
for ($i = 0; $i < $length; ++$i) {
$byte = decbin(ord($binaryIp[$i]));
$bits .= substr("00000000" . $byte, -8);
}
Expand Down

0 comments on commit b842b55

Please sign in to comment.