-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from geekwright/refresh-02132021
Refresh for newer libraries
- Loading branch information
Showing
2 changed files
with
25 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
*/ | ||
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
@@ -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)); | ||
} | ||
|
||
/** | ||
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
|
@@ -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); | ||
} | ||
|