Finish 4.14.2

updated SpamChecker module
translation initiation updates
This commit is contained in:
Vaidas Lungis committed 2015-02-06 10:51:52 +02:00
commit e459292ed8
7 files changed
+483 -47

No files matched your search

+1
View File
@@ -13,6 +13,7 @@
require_once dirname(__FILE__) . '/bb-load.php';
$di = include dirname(__FILE__) . '/bb-di.php';
$di['translate']();
$interval = isset($argv[1]) ? $argv[1] : null;
$service = $di['mod_service']('cron');
+1 -2
View File
@@ -23,8 +23,7 @@ class Guest extends \Api_Abstract
*/
public function recaptcha($data)
{
$api = $this->getApiAdmin();
$config = $api->extension_config_get(array("ext"=>"mod_spamchecker"));
$config = $this->di['mod_config']('Spamchecker');
$result = array(
'publickey' => isset($config['captcha_recaptcha_publickey']) ? $config['captcha_recaptcha_publickey'] : null,
'enabled' => isset($config['captcha_enabled']) ? $config['captcha_enabled'] : false,
+76 -44
View File
@@ -11,62 +11,92 @@
*/
namespace Box\Mod\Spamchecker;
class Service
use Box\InjectionAwareInterface;
class Service implements InjectionAwareInterface
{
public static function onBeforeClientCreateForumTopic(\Box_Event $event)
/**
* @var \Box_Di
*/
protected $di = null;
/**
* @param \Box_Di $di
*/
public function setDi($di)
{
$api = $event->getApiAdmin();
$this->di = $di;
}
/**
* @return \Box_Di
*/
public function getDi()
{
return $this->di;
}
public function forumSpamChecker(\Box_Event $event)
{
$di = $event->getDi();
$params = $event->getParameters();
$client = $api->client_get(array('id'=>$params['client_id']));
$client = $di['db']->load('Client', $params['client_id']);
$comment = array(
'comment_type' => 'comment',
'comment_author' => $client['first_name'] . ' ' . $client['last_name'],
'comment_author_email' => $client['email'],
'comment_author' => $client->first_name . ' ' . $client->last_name,
'comment_author_email' => $client->email,
'comment_content' => $params['message'],
);
self::_isCommentSpam($event, $comment);
self::_isBlockedIp($event);
$spamCheckerService = $di['mod_service']('Spamchecker');
$spamCheckerService->isCommentSpam($event, $comment);
$spamCheckerService->isBlockedIp($event);
}
public static function onBeforeClientCreateForumTopic(\Box_Event $event)
{
$di = $event->getDi();
$spamCheckerService = $di['mod_service']('Spamchecker');
$spamCheckerService->forumSpamChecker($event);
}
public static function onBeforeClientRepliedInForum(\Box_Event $event)
{
$api = $event->getApiAdmin();
$params = $event->getParameters();
$client = $api->client_get(array('id'=>$params['client_id']));
$comment = array(
'comment_type' => 'comment',
'comment_author' => $client['first_name'] . ' ' . $client['last_name'],
'comment_author_email' => $client['email'],
'comment_content' => $params['message'],
);
self::_isCommentSpam($event, $comment);
self::_isBlockedIp($event);
$di = $event->getDi();
$spamCheckerService = $di['mod_service']('Spamchecker');
$spamCheckerService->forumSpamChecker($event);
}
public static function onBeforeClientSignUp(\Box_Event $event)
{
self::_isBlockedIp($event);
self::_isSpam($event);
$di = $event->getDi();
$spamCheckerService = $di['mod_service']('Spamchecker');
$spamCheckerService->isBlockedIp($event);
$spamCheckerService->isSpam($event);
}
public static function onBeforeGuestPublicTicketOpen(\Box_Event $event)
{
self::_isBlockedIp($event);
self::_isSpam($event);
$di = $event->getDi();
$spamCheckerService = $di['mod_service']('Spamchecker');
$spamCheckerService->isBlockedIp($event);
$spamCheckerService->isSpam($event);
}
/**
* @param \Box_Event $event
*/
private static function _isBlockedIp($event)
public function isBlockedIp($event)
{
$api = $event->getApiAdmin();
$config = $api->extension_config_get(array("ext"=>"mod_spamchecker"));
$di = $event->getDi();
$config = $di['mod_config']('Spamchecker');
if(isset($config['block_ips']) && $config['block_ips'] && isset($config['blocked_ips'])) {
$blocked_ips = explode(PHP_EOL, $config['blocked_ips']);
array_walk($blocked_ips, create_function('&$val', '$val = trim($val);'));
if(in_array(Box_Tools::getIpv4(), $blocked_ips)) {
throw new \Box_Exception('IP :ip is blocked', array(':ip'=>Box_Tools::getIpv4()), 403);
if(in_array($di['tools']->getIpv4(), $blocked_ips)) {
throw new \Box_Exception('IP :ip is blocked', array(':ip'=>$di['tools']->getIpv4()), 403);
}
}
}
@@ -74,21 +104,22 @@ class Service
/**
* @param \Box_Event $event
*/
private static function _isCommentSpam($event, $comment)
public function isCommentSpam($event, $comment)
{
$api = $event->getApiAdmin();
$config = $api->extension_config_get(array("ext"=>"mod_spamchecker"));
$di = $event->getDi();
$config = $di['mod_config']('Spamchecker');
if(!isset($config['akismet_enabled']) || !$config['akismet_enabled']) {
return false;
}
require_once BB_PATH_MODS . '/Spamchecker/akismet.curl.class.php';
$akismet = new akismet($config['akismet_api_key'], BB_URL);
$akismet = new \akismet($config['akismet_api_key'], $di['config']['url']);
if(!$akismet->valid_key()) {
if($event->getApiGuest()->extension_is_on(array('mod'=>'notification'))) {
$api->notification_add(array('message'=>'Akismet Key is not valid!'));
$extensionService = $di['mod_service']('Extension');
if($extensionService->isExtensionActive('mod', 'notification')) {
$notificationService = $di['mod_service']('Notification');
$notificationService->create('Akismet Key is not valid!');
} else {
error_log('Akismet Key is not valid!');
}
@@ -99,10 +130,10 @@ class Service
throw new \Box_Exception('Akismet detected this message is spam');
}
}
private static function _isSpam(\Box_Event $event)
public function isSpam(\Box_Event $event)
{
$api = $event->getApiAdmin();
$di = $event->getDi();
$params = $event->getParameters();
$data = array(
'ip' => isset($params['ip']) ? $params['ip'] : NULL,
@@ -110,8 +141,8 @@ class Service
'recaptcha_challenge_field' => isset($params['recaptcha_challenge_field']) ? $params['recaptcha_challenge_field'] : NULL,
'recaptcha_response_field' => isset($params['recaptcha_response_field']) ? $params['recaptcha_response_field'] : NULL,
);
$config = $api->extension_config_get(array("ext"=>"mod_spamchecker"));
$config = $di['mod_config']('Spamchecker');
if(isset($config['captcha_enabled']) && $config['captcha_enabled']) {
@@ -133,7 +164,8 @@ class Service
}
if(isset($config['sfs']) && $config['sfs']) {
self::_isInStopForumSpamDatabase($data);
$spamCheckerService = $di['mod_service']('Spamchecker');
$spamCheckerService->isInStopForumSpamDatabase($data);
}
}
@@ -147,14 +179,14 @@ class Service
* @param array $data
* @return bool
*/
private static function _isInStopForumSpamDatabase(array $data)
public function isInStopForumSpamDatabase(array $data)
{
$data['f'] = 'json';
$url = 'http://www.stopforumspam.com/api?'.http_build_query($data);
$file_contents = file_get_contents($url);
$file_contents = $this->di['tools']->file_get_contents($url);
$json = json_decode($file_contents);
if(!is_object($json) || !$json->success) {
if(!is_object($json) || isset($json->success) && !$json->success) {
return false;
}
+1
View File
@@ -455,6 +455,7 @@ final class Box_Installer
$emailService = new \Box\Mod\Email\Service();
$di = $di = include BB_PATH_ROOT . '/bb-di.php';
$di['translate']();
$emailService->setDi($di);
return $emailService->templateBatchGenerate();
}
@@ -0,0 +1,88 @@
<?php
namespace Box\Mod\Spamchecker\Api;
class GuestTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Box\Mod\Spamchecker\Api\Guest
*/
protected $api = null;
public function setup()
{
$this->api = new \Box\Mod\Spamchecker\Api\Guest();
}
public function testgetDi()
{
$di = new \Box_Di();
$this->api->setDi($di);
$getDi = $this->api->getDi();
$this->assertEquals($di, $getDi);
}
public function datarecaptchaConfig()
{
return array(
array(
array(
'captcha_recaptcha_publickey' => 1234,
'captcha_enabled' => true,
),
array(
'publickey' => 1234,
'enabled' => true,
),
),
array(
array(
'captcha_enabled' => true,
),
array(
'publickey' => null,
'enabled' => true,
),
),
array(
array(
'captcha_recaptcha_publickey' => 1234,
'captcha_enabled' => false,
),
array(
'publickey' => 1234,
'enabled' => false,
),
),
array(
array(
'captcha_enabled' => false,
),
array(
'publickey' => null,
'enabled' => false,
),
),
);
}
/**
* @dataProvider datarecaptchaConfig
*/
public function testrecaptcha($config, $expected)
{
$di = new \Box_Di();
$di['mod_config'] = $di->protect(function () use ($config){
return $config;
});
$this->api->setDi($di);
$result = $this->api->recaptcha(array());
$this->assertEquals($expected, $result);
}
}
@@ -0,0 +1,300 @@
<?php
namespace Box\Mod\Spamchecker;
class ServiceTest extends \PHPUnit_Framework_TestCase {
/**
* @var \Box\Mod\Spamchecker\Service
*/
protected $service = null;
public function setup()
{
$this->service= new \Box\Mod\Spamchecker\Service();
}
public function testgetDi()
{
$di = new \Box_Di();
$this->service->setDi($di);
$getDi = $this->service->getDi();
$this->assertEquals($di, $getDi);
}
public function testforumSpamChecker()
{
$params = array(
'client_id' => 1,
'message' => 'Hello world',
);
$model = new \Model_Client();
$model->loadBean(new \RedBeanPHP\OODBBean());
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('load')
->willReturn($model);
$spamCheckerService = $this->getMockBuilder('\Box\Mod\Spamchecker\Service')->getMock();
$spamCheckerService->expects($this->atLeastOnce())
->method('isCommentSpam');
$spamCheckerService->expects($this->atLeastOnce())
->method('isBlockedIp');
$di = new \Box_Di();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(function () use ($spamCheckerService){
return $spamCheckerService;
});
$boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()
->getMock();
$boxEventMock->expects($this->atLeastOnce())
->method('getDi')
->willReturn($di);
$boxEventMock->expects($this->atLeastOnce())
->method('getParameters')
->willReturn($params);
$this->service->forumSpamChecker($boxEventMock);
}
public function testonBeforeClientCreateForumTopic()
{
$spamCheckerService = $this->getMockBuilder('\Box\Mod\Spamchecker\Service')->getMock();
$spamCheckerService->expects($this->atLeastOnce())
->method('forumSpamChecker');
$di = new \Box_Di();
$di['mod_service'] = $di->protect(function () use ($spamCheckerService){
return $spamCheckerService;
});
$boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()
->getMock();
$boxEventMock->expects($this->atLeastOnce())
->method('getDi')
->willReturn($di);
$this->service->onBeforeClientCreateForumTopic($boxEventMock);
}
public function testonBeforeClientRepliedInForum()
{
$spamCheckerService = $this->getMockBuilder('\Box\Mod\Spamchecker\Service')->getMock();
$spamCheckerService->expects($this->atLeastOnce())
->method('forumSpamChecker');
$di = new \Box_Di();
$di['mod_service'] = $di->protect(function () use ($spamCheckerService){
return $spamCheckerService;
});
$boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()
->getMock();
$boxEventMock->expects($this->atLeastOnce())
->method('getDi')
->willReturn($di);
$this->service->onBeforeClientRepliedInForum($boxEventMock);
}
public function testonBeforeClientSignUp()
{
$spamCheckerService = $this->getMockBuilder('\Box\Mod\Spamchecker\Service')->getMock();
$spamCheckerService->expects($this->atLeastOnce())
->method('isBlockedIp');
$spamCheckerService->expects($this->atLeastOnce())
->method('isSpam');
$di = new \Box_Di();
$di['mod_service'] = $di->protect(function () use ($spamCheckerService){
return $spamCheckerService;
});
$boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()
->getMock();
$boxEventMock->expects($this->atLeastOnce())
->method('getDi')
->willReturn($di);
$this->service->onBeforeClientSignUp($boxEventMock);
}
public function testonBeforeGuestPublicTicketOpen()
{
$spamCheckerService = $this->getMockBuilder('\Box\Mod\Spamchecker\Service')->getMock();
$spamCheckerService->expects($this->atLeastOnce())
->method('isBlockedIp');
$spamCheckerService->expects($this->atLeastOnce())
->method('isSpam');
$di = new \Box_Di();
$di['mod_service'] = $di->protect(function () use ($spamCheckerService){
return $spamCheckerService;
});
$boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()
->getMock();
$boxEventMock->expects($this->atLeastOnce())
->method('getDi')
->willReturn($di);
$this->service->onBeforeGuestPublicTicketOpen($boxEventMock);
}
public function testisBlockedIp_IpBlocked()
{
$clientIp = '1.1.1.1';
$modConfig = array(
'block_ips' => true,
'blocked_ips' => '1.1.1.1'.PHP_EOL.'2.2.2.2',
);
$toolsMock = $this->getMockBuilder('\Box_Tools')->getMock();
$toolsMock->expects($this->atLeastOnce())
->method('getIpv4')
->willReturn($clientIp);
$di = new \Box_Di();
$di['mod_config'] = $di->protect(function($modName) use ($modConfig ){
if ($modName == 'Spamchecker'){
return $modConfig;
}
});
$di['tools'] = $toolsMock;
$boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()
->getMock();
$boxEventMock->expects($this->atLeastOnce())
->method('getDi')
->willReturn($di);
$this->setExpectedException('\Box_Exception', sprintf("IP %s is blocked", $clientIp), 403);
$this->service->isBlockedIp($boxEventMock);
}
public function testisBlockedIp_IpNotBlocked()
{
$clientIp = '214.1.4.99';
$modConfig = array(
'block_ips' => true,
'blocked_ips' => '1.1.1.1'.PHP_EOL.'2.2.2.2',
);
$toolsMock = $this->getMockBuilder('\Box_Tools')->getMock();
$toolsMock->expects($this->atLeastOnce())
->method('getIpv4')
->willReturn($clientIp);
$di = new \Box_Di();
$di['mod_config'] = $di->protect(function($modName) use ($modConfig ){
if ($modName == 'Spamchecker'){
return $modConfig;
}
});
$di['tools'] = $toolsMock;
$boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()
->getMock();
$boxEventMock->expects($this->atLeastOnce())
->method('getDi')
->willReturn($di);
$this->service->isBlockedIp($boxEventMock);
}
public function testisBlockedIp_BlockIpsNotEnabled()
{
$modConfig = array(
'block_ips' => false,
);
$di = new \Box_Di();
$di['mod_config'] = $di->protect(function($modName) use ($modConfig ){
if ($modName == 'Spamchecker'){
return $modConfig;
}
});
$boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()
->getMock();
$boxEventMock->expects($this->atLeastOnce())
->method('getDi')
->willReturn($di);
$this->service->isBlockedIp($boxEventMock);
}
public function testisInStopForumSpamDatabase_InvalidResponse()
{
$toolsMock = $this->getMockBuilder('\Box_Tools')->getMock();
$toolsMock->expects($this->atLeastOnce())
->method('file_get_contents')
->willReturn('{}');
$di = new \Box_Di();
$di['tools'] = $toolsMock;
$data = array();
$this->service->setDi($di);
$result = $this->service->isInStopForumSpamDatabase($data);
$this->assertFalse($result);
}
public function dataProviderSpamResponses()
{
return array(
array(
'{"success" : "true", "username" : {"appears" : "true" }}', 'Your Username is blacklisted in global database'
),
array(
'{"success" : "true", "email" : {"appears" : "true" }}', 'Your Email is blacklisted in global database'
),
array(
'{"success" : "true", "ip" : {"appears" : "true" }}', 'Your IP is blacklisted in global database'
),
);
}
/**
* @dataProvider dataProviderSpamResponses
*/
public function testisInStopForumSpamDatabase_UserNameBlackListed($json, $exceptionMessage)
{
$toolsMock = $this->getMockBuilder('\Box_Tools')->getMock();
$toolsMock->expects($this->atLeastOnce())
->method('file_get_contents')
->willReturn($json);
$di = new \Box_Di();
$di['tools'] = $toolsMock;
$data = array();
$this->service->setDi($di);
$this->setExpectedException('Box_Exception', $exceptionMessage);
$this->service->isInStopForumSpamDatabase($data);
}
public function testisInStopForumSpamDatabase_NotExists()
{
$json = '{"success" : "true", "username" : {}}';
$toolsMock = $this->getMockBuilder('\Box_Tools')->getMock();
$toolsMock->expects($this->atLeastOnce())
->method('file_get_contents')
->willReturn($json);
$di = new \Box_Di();
$di['tools'] = $toolsMock;
$data = array();
$this->service->setDi($di);
$result = $this->service->isInStopForumSpamDatabase($data);
$this->assertFalse($result);
}
}
@@ -12,9 +12,24 @@ class Box_Mod_Spamchecker_ServiceTest extends BBDbApiTestCase
'ip' => '127.0.0.1',
);
$event = new Box_Event(null, 'any', $parameters, $this->api_admin);
$event->setDi($this->di);
$object = new \Box\Mod\Spamchecker\Service();
$object->onBeforeClientSignUp($event);
}
public function testonBeforeClientCreateForumTopic()
{
$parameters = array(
'ip' => '127.0.0.1',
'message' => 'TestUnit',
'client_id' => 1,
);
$event = new Box_Event(null, 'any', $parameters, $this->api_admin);
$event->setDi($this->di);
$object = new \Box\Mod\Spamchecker\Service();
$object->onBeforeClientCreateForumTopic($event);
}
}