Skip to content

Commit

Permalink
Supported IPv6 addresses in the Spam-X plugin (feature #559)
Browse files Browse the repository at this point in the history
  • Loading branch information
mystralkk committed Aug 27, 2019
1 parent 1b8273f commit 15cc749
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 72 deletions.
76 changes: 7 additions & 69 deletions plugins/spamx/IP.Examine.class.php
Expand Up @@ -65,87 +65,25 @@ public function reexecute($comment, $date, $ip, $type)
/**
* Private internal method to match an IP address against a CIDR
*
* @param string $ipToCheck IP address to check
* @param string $CIDR IP address range to check against
* @return boolean true if IP falls into the CIDR, else false
* @todo CIDR support for IPv6 addresses
* Original author: Ian B, taken from
* @link http://www.php.net/manual/en/function.ip2long.php#71939
* @param string $ipToCheck IP address (IPv4 or IPv6) to check
* @param string $CIDR IP address range to check against
* @return boolean true if IP falls into the CIDR, else false
*/
private function _matchCIDR($ipToCheck, $CIDR)
{
// not for IPv6 addresses
if (strpos($ipToCheck, ':') !== false) {
return false;
}

// get the base and the bits from the ban in the database
list($base, $bits) = explode('/', $CIDR);

// now split it up into its classes
$classes = explode('.', $base);
$elements = count($classes);
if ($elements < 4) {
for ($i = $elements; $i < 4; $i++) {
$classes[$i] = 0;
}
}
list($a, $b, $c, $d) = $classes;

// now do some bit shifting/switching to convert to ints
$i = ($a << 24) + ($b << 16) + ($c << 8) + $d;
$mask = $bits == 0 ? 0 : (~0 << (32 - $bits));

// here's our lowest int
$low = $i & $mask;

// here's our highest int
$high = $i | (~$mask & 0xFFFFFFFF);

// now split the ip we're checking against up into classes
$ex = explode('.', $ipToCheck);

if (count($ex) == 4) {
// now convert the ip we're checking against to an int
$check = ($ex[0] << 24) + ($ex[1] << 16) + ($ex[2] << 8) + $ex[3];

// if the ip is within the range, including
// highest/lowest values, then it's witin the CIDR range
if (($check >= $low) && ($check <= $high)) {
return true;
}
}

return false;
return Geeklog\IP::matchCIDR($ipToCheck, $CIDR);
}

/**
* Private internal method to match an IP address against an address range
* Original authors: dh06 and Stephane, taken from
*
* @link http://www.php.net/manual/en/function.ip2long.php#70707
* @param string $ip IP address to check
* @param string $ip IP address (IPv4 or IPv6) to check
* @param string $range IP address range to check against
* @return boolean true if IP falls into the IP range, else false
* @return boolean true if IP falls into the IP range, else false
*/
private function _matchRange($ip, $range)
{
// not for IPv6 addresses
if (strpos($ip, ':') !== false) {
return false;
}

$d = strpos($range, '-');
if ($d !== false) {
$from = ip2long(trim(substr($range, 0, $d)));
$to = ip2long(trim(substr($range, $d + 1)));

$ip = ip2long($ip);

return (($ip >= $from) && ($ip <= $to));
}

return false;
return Geeklog\IP::matchRange($ip, $range);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion plugins/spamx/functions.inc
Expand Up @@ -721,7 +721,7 @@ function plugin_getlanguageoverrides_spamx()
*/

/**
* @param string $ipToCheck
* @param string $ipToCheck (IPv4 or IPv6)
* @return bool
*/
function SPAMX_isIPBanned($ipToCheck)
Expand Down
3 changes: 3 additions & 0 deletions public_html/docs/english/spamx.html
Expand Up @@ -96,6 +96,9 @@ <h3><a name="ip">IP Filter</a></h3>
get a new IP address the next time he connects to the internet, while the
blocked IP address will be reused and may be used by some innocent user.</p>

<h4>Update!</h4>
<p>As of Geeklog 2.2.1, IPv6 addresses are supported.</p>

<h3><a name="ipofurl">IP of URL Filter</a></h3>

<p>This module is only useful in a few special cases: Here you enter the IP
Expand Down
4 changes: 4 additions & 0 deletions public_html/docs/japanese/spamx.html
Expand Up @@ -79,6 +79,10 @@ <h3><a name="ip">IPフィルター(IP Filter)</a></h3>
<p>
IPアドレスが実際にはあまりよい判断基準にはならないということに注意してください。インターネットサービスプロバイダやホスティングサービスの中にはスパムの温床として知られているものもありますが、それらのIPアドレスをブロックしてもあまり役には立たないでしょう。スパム送信者はインターネットへ接続し直すときに新しいIPアドレスを取得するのに対し、ブロックされたIPアドレスは他の罪もないユーザーが使用することがよくあるからです。</p>

<h4>情報更新!</h4>
<p>Geeklog 2.2.1でIPv6のアドレスをサポートしました。</p>


<h3><a name="ipofurl">URLのIPフィルター(IP of URL Filter)</a></h3>

<p>
Expand Down
2 changes: 0 additions & 2 deletions system/classes/IP.php
Expand Up @@ -4,10 +4,8 @@

use InvalidArgumentException;
use IPLib\Address\AddressInterface;
use IPLib\Address\IPv4;
use IPLib\Factory;
use IPLib\Range\RangeInterface;
use IPLib\Range\Subnet;

/**
* Class IP
Expand Down

0 comments on commit 15cc749

Please sign in to comment.