From 15cc7496ed7993f6b5a18fb2d274bc3672ef4366 Mon Sep 17 00:00:00 2001
From: mystralkk
Date: Wed, 28 Aug 2019 07:03:33 +0900
Subject: [PATCH] Supported IPv6 addresses in the Spam-X plugin (feature #559)
---
plugins/spamx/IP.Examine.class.php | 76 +++-------------------------
plugins/spamx/functions.inc | 2 +-
public_html/docs/english/spamx.html | 3 ++
public_html/docs/japanese/spamx.html | 4 ++
system/classes/IP.php | 2 -
5 files changed, 15 insertions(+), 72 deletions(-)
diff --git a/plugins/spamx/IP.Examine.class.php b/plugins/spamx/IP.Examine.class.php
index 459c1baca..b6ab32f44 100644
--- a/plugins/spamx/IP.Examine.class.php
+++ b/plugins/spamx/IP.Examine.class.php
@@ -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);
}
/**
diff --git a/plugins/spamx/functions.inc b/plugins/spamx/functions.inc
index 47006aa6a..0bdeda5d9 100644
--- a/plugins/spamx/functions.inc
+++ b/plugins/spamx/functions.inc
@@ -721,7 +721,7 @@ function plugin_getlanguageoverrides_spamx()
*/
/**
- * @param string $ipToCheck
+ * @param string $ipToCheck (IPv4 or IPv6)
* @return bool
*/
function SPAMX_isIPBanned($ipToCheck)
diff --git a/public_html/docs/english/spamx.html b/public_html/docs/english/spamx.html
index 97c221d75..25b85bbd9 100644
--- a/public_html/docs/english/spamx.html
+++ b/public_html/docs/english/spamx.html
@@ -96,6 +96,9 @@
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.
+Update!
+As of Geeklog 2.2.1, IPv6 addresses are supported.
+
This module is only useful in a few special cases: Here you enter the IP
diff --git a/public_html/docs/japanese/spamx.html b/public_html/docs/japanese/spamx.html
index a2cf283ff..0e68b97fa 100644
--- a/public_html/docs/japanese/spamx.html
+++ b/public_html/docs/japanese/spamx.html
@@ -79,6 +79,10 @@
IPアドレスが実際にはあまりよい判断基準にはならないということに注意してください。インターネットサービスプロバイダやホスティングサービスの中にはスパムの温床として知られているものもありますが、それらのIPアドレスをブロックしてもあまり役には立たないでしょう。スパム送信者はインターネットへ接続し直すときに新しいIPアドレスを取得するのに対し、ブロックされたIPアドレスは他の罪もないユーザーが使用することがよくあるからです。
+情報更新!
+Geeklog 2.2.1でIPv6のアドレスをサポートしました。
+
+
diff --git a/system/classes/IP.php b/system/classes/IP.php
index 2290a2b5b..bc7f44fe8 100644
--- a/system/classes/IP.php
+++ b/system/classes/IP.php
@@ -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