Skip to content

Commit

Permalink
Merge branch 'feature/akismet'
Browse files Browse the repository at this point in the history
  • Loading branch information
mystralkk committed Nov 19, 2017
2 parents 5d13a02 + 2f97d40 commit a173327
Show file tree
Hide file tree
Showing 33 changed files with 1,285 additions and 548 deletions.
6 changes: 3 additions & 3 deletions plugins/calendar/functions.inc
Expand Up @@ -10,7 +10,7 @@
// | API method and 2) implements all the common code needed by the Calendar |
// | plugin's PHP files. |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2000-2011 by the following authors: |
// | Copyright (C) 2000-2017 by the following authors: |
// | |
// | Authors: Tony Bibbs - tony AT tonybibbs DOT com |
// | Tom Willett - twillett AT users DOT sourceforge DOT net |
Expand Down Expand Up @@ -484,8 +484,8 @@ function plugin_savesubmission_calendar($A)
. $A['address1'] . '<br' . XHTML . '>' . $A['address2']
. '<br' . XHTML . '>' . $A['city'] . ', ' . $A['zipcode']
. '<br' . XHTML . '>' . $A['description'] . '</p>';
$result = PLG_checkforSpam($spamCheck, $_CONF['spamx']);
if ($result > 0) {
$result = PLG_checkForSpam($spamCheck, $_CONF['spamx'], $A['url'], Geeklog\Akismet::COMMENT_TYPE_EVENT);
if ($result > PLG_SPAM_NOT_FOUND) {
COM_updateSpeedlimit('submit');
COM_displayMessageAndAbort($result, 'spamx', 403, 'Forbidden');
}
Expand Down
9 changes: 5 additions & 4 deletions plugins/links/functions.inc
Expand Up @@ -10,7 +10,7 @@
// | API method and 2) implements all the common code needed by the Links |
// | Plugins' PHP files. |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2000-2010 by the following authors: |
// | Copyright (C) 2000-2017 by the following authors: |
// | |
// | Authors: Tony Bibbs - tony AT tonybibbs DOT com |
// | Mark Limburg - mlimburg AT users.sourceforge DOT net |
Expand Down Expand Up @@ -1073,10 +1073,11 @@ function plugin_save_submit_links($A)
} else {
$link = COM_createLink($A['title'], $A['url']);
}
$spamcheck = '<p>'. $link .' (' . $A['categorydd'] . ')<br' . XHTML . '>'
$spamCheck = '<p>'. $link .' (' . $A['categorydd'] . ')<br' . XHTML . '>'
. $A['description'] . '</p>';
$result = PLG_checkforSpam($spamcheck, $_CONF['spamx']);
if ($result > 0) {
$result = PLG_checkForSpam($spamCheck, $_CONF['spamx'], $link, Geeklog\Akismet::COMMENT_TYPE_LINK);

if ($result > PLG_SPAM_NOT_FOUND) {
COM_updateSpeedlimit('submit');
COM_displayMessageAndAbort($result, 'spamx', 403, 'Forbidden');
}
Expand Down
72 changes: 72 additions & 0 deletions plugins/spamx/Akismet.Examine.class.php
@@ -0,0 +1,72 @@
<?php

/**
* File: Akismet.Examine.class.php
* This is the Akismet Examine class for the Geeklog Spam-X plugin
* Copyright (C) 2017 by the following authors:
* Author Kenji ITO mystralkk AT gmail DOT com
* Licensed under the GNU General Public License
*
* @package Spam-X
* @subpackage Modules
*/
if (stripos($_SERVER['PHP_SELF'], basename(__FILE__)) !== false) {
die('This file can not be used on its own!');
}

// Include Base Classes
require_once $_CONF['path'] . 'plugins/spamx/' . 'BaseCommand.class.php';

/**
* Sends posts to Akismet for examination
*
* @package Spam-X
*/
class Akismet extends BaseCommand
{
/**
* Here we do the work
*
* @param string $comment
* @param string $permanentLink (since GL 2.2.0)
* @param string $commentType (since GL 2.2.0)
* @param string $commentAuthor (since GL 2.2.0)
* @param string $commentAuthorEmail (since GL 2.2.0)
* @param string $commentAuthorURL (since GL 2.2.0)
* @return int either PLG_SPAM_NOT_FOUND, PLG_SPAM_FOUND or PLG_SPAM_UNSURE
* @note As for valid value for $commentType, see system/classes/Akismet.php
*/
public function execute($comment, $permanentLink, $commentType = Geeklog\Akismet::COMMENT_TYPE_COMMENT,
$commentAuthor = null, $commentAuthorEmail = null, $commentAuthorURL = null)
{
global $_CONF, $_SPX_CONF, $LANG_SX00;

$answer = PLG_SPAM_NOT_FOUND;

if (!isset($_SPX_CONF['akismet_enabled'], $_SPX_CONF['akismet_api_key'])) {
$_SPX_CONF['akismet_enabled'] = false;
}

// Akismet is not enabled or the user hasn't set Akismet API key
if (!$_SPX_CONF['akismet_enabled']) {
return $answer;
}

$akismet = new Geeklog\Akismet($_SPX_CONF['akismet_api_key'], $_CONF['site_url']);

if($akismet->verifyAPIKey()) {
$answer = $akismet->checkForSpam(
$comment, $permanentLink, $commentType, $commentAuthor, $commentAuthorEmail, $commentAuthorURL
);

if (($answer == PLG_SPAM_FOUND) || ($answer == PLG_SPAM_UNSURE)) {
SPAMX_log(
$LANG_SX00['foundspam'] . 'Akismet' . $LANG_SX00['foundspam2'] . $this->getUid()
. $LANG_SX00['foundspam3'] . $_SERVER['REMOTE_ADDR']
);
}
}

return $answer;
}
}
23 changes: 15 additions & 8 deletions plugins/spamx/BannedUsers.Examine.class.php
Expand Up @@ -33,13 +33,20 @@ class BannedUsers extends BaseCommand
* Here we do the work
*
* @param string $comment
* @return int
* @param string $permanentLink (since GL 2.2.0)
* @param string $commentType (since GL 2.2.0)
* @param string $commentAuthor (since GL 2.2.0)
* @param string $commentAuthorEmail (since GL 2.2.0)
* @param string $commentAuthorURL (since GL 2.2.0)
* @return int either PLG_SPAM_NOT_FOUND, PLG_SPAM_FOUND or PLG_SPAM_UNSURE
* @note As for valid value for $commentType, see system/classes/Akismet.php
*/
public function execute($comment)
public function execute($comment, $permanentLink, $commentType = Geeklog\Akismet::COMMENT_TYPE_COMMENT,
$commentAuthor = null, $commentAuthorEmail = null, $commentAuthorURL = null)
{
global $_TABLES, $_USER, $LANG_SX00, $LANG28;
global $_TABLES, $LANG_SX00, $LANG28;

$uid = COM_isAnonUser() ? 1 : $_USER['uid'];
$uid = $this->getUid();

// Get homepage URLs of all banned users
$result = DB_query("SELECT DISTINCT homepage FROM {$_TABLES['users']} WHERE status = 0 AND homepage IS NOT NULL AND homepage <> ''");
Expand All @@ -54,14 +61,14 @@ public function execute($comment)
// hex notation
$comment = preg_replace_callback('/&#x([a-f0-9]+);/mi', array($this, 'callbackHex'), $comment);

$ans = 0;
$answer = PLG_SPAM_NOT_FOUND;

for ($i = 0; $i < $numRows; $i++) {
list($val) = DB_fetchArray($result);
$pattern = $this->prepareRegularExpression($val);

if (preg_match($pattern, $comment)) {
$ans = 1; // quit on first positive match
$answer = PLG_SPAM_FOUND; // quit on first positive match
SPAMX_log($LANG_SX00['foundspam'] . $val . ' (' . $LANG28[42] . ')' .
$LANG_SX00['foundspam2'] . $uid .
$LANG_SX00['foundspam3'] . $_SERVER['REMOTE_ADDR']
Expand All @@ -70,8 +77,8 @@ public function execute($comment)
}
}

$this->result = $ans;
$this->result = $answer;

return $ans;
return $answer;
}
}
16 changes: 15 additions & 1 deletion plugins/spamx/BaseCommand.class.php
Expand Up @@ -36,7 +36,21 @@ protected function callbackHex($str)
return chr('0x' . $str);
}

abstract public function execute($comment);
/**
* Here we do the work
*
* @param string $comment
* @param string $permanentLink (since GL 2.2.0)
* @param string $commentType (since GL 2.2.0)
* @param string $commentAuthor (since GL 2.2.0)
* @param string $commentAuthorEmail (since GL 2.2.0)
* @param string $commentAuthorURL (since GL 2.2.0)
* @return int either PLG_SPAM_NOT_FOUND, PLG_SPAM_FOUND or PLG_SPAM_UNSURE
* @note As for valid value for $commentType, see system/classes/Akismet.php
*/
abstract public function execute(
$comment, $permanentLink, $commentType = Geeklog\Akismet::COMMENT_TYPE_COMMENT,
$commentAuthor = null, $commentAuthorEmail = null, $commentAuthorURL = null);

/**
* Returns one of the result codes defined in "lib-plugins.php"
Expand Down
23 changes: 15 additions & 8 deletions plugins/spamx/BlackList.Examine.class.php
Expand Up @@ -31,10 +31,17 @@ class BlackList extends BaseCommand
/**
* Here we do the work
*
* @param string
* @return int
* @param string $comment
* @param string $permanentLink (since GL 2.2.0)
* @param string $commentType (since GL 2.2.0)
* @param string $commentAuthor (since GL 2.2.0)
* @param string $commentAuthorEmail (since GL 2.2.0)
* @param string $commentAuthorURL (since GL 2.2.0)
* @return int either PLG_SPAM_NOT_FOUND, PLG_SPAM_FOUND or PLG_SPAM_UNSURE
* @note As for valid value for $commentType, see system/classes/Akismet.php
*/
public function execute($comment)
public function execute($comment, $permanentLink, $commentType = Geeklog\Akismet::COMMENT_TYPE_COMMENT,
$commentAuthor = null, $commentAuthorEmail = null, $commentAuthorURL = null)
{
global $_CONF, $_TABLES, $LANG_SX00;

Expand All @@ -44,7 +51,7 @@ public function execute($comment)
* Include Blacklist Data
*/
$result = DB_query("SELECT value FROM {$_TABLES['spamx']} WHERE name='Personal'", 1);
$nrows = DB_numRows($result);
$numRows = DB_numRows($result);

// named entities
$comment = html_entity_decode($comment);
Expand All @@ -55,14 +62,14 @@ public function execute($comment)
// hex notation
$comment = preg_replace_callback('/&#x([a-f0-9]+);/mi', array($this, 'callbackHex'), $comment);

$ans = PLG_SPAM_NOT_FOUND;
$answer = PLG_SPAM_NOT_FOUND;

for ($i = 1; $i <= $nrows; $i++) {
for ($i = 1; $i <= $numRows; $i++) {
list($val) = DB_fetchArray($result);
$pattern = $this->prepareRegularExpression($val);

if (preg_match($pattern, $comment)) {
$ans = PLG_SPAM_FOUND; // quit on first positive match
$answer = PLG_SPAM_FOUND; // quit on first positive match
$this->updateStat('Personal', $val);
SPAMX_log($LANG_SX00['foundspam'] . $val .
$LANG_SX00['foundspam2'] . $uid .
Expand All @@ -71,6 +78,6 @@ public function execute($comment)
}
}

return $ans;
return $answer;
}
}
22 changes: 15 additions & 7 deletions plugins/spamx/Header.Examine.class.php
Expand Up @@ -30,10 +30,17 @@ class Header extends BaseCommand
/**
* Here we do the work
*
* @param string
* @return int
* @param string $comment
* @param string $permanentLink (since GL 2.2.0)
* @param string $commentType (since GL 2.2.0)
* @param string $commentAuthor (since GL 2.2.0)
* @param string $commentAuthorEmail (since GL 2.2.0)
* @param string $commentAuthorURL (since GL 2.2.0)
* @return int either PLG_SPAM_NOT_FOUND, PLG_SPAM_FOUND or PLG_SPAM_UNSURE
* @note As for valid value for $commentType, see system/classes/Akismet.php
*/
public function execute($comment)
public function execute($comment, $permanentLink, $commentType = Geeklog\Akismet::COMMENT_TYPE_COMMENT,
$commentAuthor = null, $commentAuthorEmail = null, $commentAuthorURL = null)
{
global $_TABLES, $LANG_SX00;

Expand All @@ -58,7 +65,7 @@ public function execute($comment)
$result = DB_query("SELECT value FROM {$_TABLES['spamx']} WHERE name='HTTPHeader'", 1);
$numRows = DB_numRows($result);

$ans = PLG_SPAM_NOT_FOUND;
$answer = PLG_SPAM_NOT_FOUND;

for ($i = 0; $i < $numRows; $i++) {
list ($entry) = DB_fetchArray($result);
Expand All @@ -71,18 +78,19 @@ public function execute($comment)
foreach ($headers as $key => $content) {
if (strcasecmp($name, $key) === 0) {
if (preg_match($pattern, $content)) {
$ans = PLG_SPAM_FOUND; // quit on first positive match
$answer = PLG_SPAM_FOUND; // quit on first positive match
$this->updateStat('HTTPHeader', $entry);
SPAMX_log($LANG_SX00['foundspam'] . $entry .
$LANG_SX00['foundspam2'] . $uid .
$LANG_SX00['foundspam3'] .
$_SERVER['REMOTE_ADDR']);
$_SERVER['REMOTE_ADDR']
);
break;
}
}
}
}

return $ans;
return $answer;
}
}
35 changes: 21 additions & 14 deletions plugins/spamx/IP.Examine.class.php
Expand Up @@ -27,13 +27,19 @@
class IP extends BaseCommand
{
/**
* The execute method examines the IP address a comment is coming from,
* comparing it against a blacklist of banned IP addresses.
* Here we do the work
*
* @param string $comment Comment text to examine
* @return int 0: no spam, else: spam detected
* @param string $comment
* @param string $permanentLink (since GL 2.2.0)
* @param string $commentType (since GL 2.2.0)
* @param string $commentAuthor (since GL 2.2.0)
* @param string $commentAuthorEmail (since GL 2.2.0)
* @param string $commentAuthorURL (since GL 2.2.0)
* @return int either PLG_SPAM_NOT_FOUND, PLG_SPAM_FOUND or PLG_SPAM_UNSURE
* @note As for valid value for $commentType, see system/classes/Akismet.php
*/
public function execute($comment)
public function execute($comment, $permanentLink, $commentType = Geeklog\Akismet::COMMENT_TYPE_COMMENT,
$commentAuthor = null, $commentAuthorEmail = null, $commentAuthorURL = null)
{
return $this->_process($_SERVER['REMOTE_ADDR']);
}
Expand All @@ -59,17 +65,17 @@ 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 $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
*/
private function _matchCIDR($iptocheck, $CIDR)
private function _matchCIDR($ipToCheck, $CIDR)
{
// not for IPv6 addresses
if (strpos($iptocheck, ':') !== false) {
if (strpos($ipToCheck, ':') !== false) {
return false;
}

Expand Down Expand Up @@ -97,7 +103,7 @@ private function _matchCIDR($iptocheck, $CIDR)
$high = $i | (~$mask & 0xFFFFFFFF);

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

if (count($ex) == 4) {
// now convert the ip we're checking against to an int
Expand Down Expand Up @@ -147,7 +153,7 @@ private function _matchRange($ip, $range)
* address against a blacklist of IP regular expressions.
*
* @param string $ip IP address of comment poster
* @return int 0: no spam, else: spam detected
* @return int PLG_SPAM_NOT_FOUND: no spam, PLG_SPAM_FOUND: spam detected
*/
private function _process($ip)
{
Expand All @@ -161,7 +167,7 @@ private function _process($ip)
$result = DB_query("SELECT value FROM {$_TABLES['spamx']} WHERE name='IP'", 1);
$numRows = DB_numRows($result);

$ans = PLG_SPAM_NOT_FOUND;
$answer = PLG_SPAM_NOT_FOUND;

for ($i = 0; $i < $numRows; $i++) {
list($val) = DB_fetchArray($result);
Expand All @@ -176,15 +182,16 @@ private function _process($ip)
}

if ($matches) {
$ans = PLG_SPAM_FOUND; // quit on first positive match
$answer = PLG_SPAM_FOUND; // quit on first positive match
$this->updateStat('IP', $val);
SPAMX_log($LANG_SX00['foundspam'] . $val .
$LANG_SX00['foundspam2'] . $uid .
$LANG_SX00['foundspam3'] . $ip);
$LANG_SX00['foundspam3'] . $ip)
;
break;
}
}

return $ans;
return $answer;
}
}

0 comments on commit a173327

Please sign in to comment.