Skip to content

Instantly share code, notes, and snippets.

@mikelfo
Last active April 4, 2017 08:14
Show Gist options
  • Select an option

  • Save mikelfo/f625e4383dcd257d526b6187ed3f3ce3 to your computer and use it in GitHub Desktop.

Select an option

Save mikelfo/f625e4383dcd257d526b6187ed3f3ce3 to your computer and use it in GitHub Desktop.
simple Symfony 2.8 service integrating BlogSpam.net free spam filter API
<?php
/**
* Description of BlogSpam.net, Detection for Blog & Forum SPAM free API (v2.0)
* @see https://blogspam.net/api/2.0/
* @note The BlogsSapm.net API supports only HTTP queries.
*
* @version 1.0.2
*
* @requires Buzz HTTP library. https://github.com/sensiolabs/SensioBuzzBundle
* and cUrl (php5-curl), etc.
*
* @author Mikel Fernández <mikel.fernandez (at) owasys.com>
* @copy (C) 2017 Owasys (https://www.owasys.com)
* @license MIT
*/
namespace MyApp\MyBundleBundle\Services;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Symfony\Component\HttpFoundation\Request as Request;
/**
* BlogSpam class
*/
class BlogSpam {
/// BlogSpam Account: the URL of *our* site
/**
* URL of the action to test a comment for spam
* @see https://blogspam.net/api/2.0/testComment.html
*/
const TEST_COMMENT_URL = "http://test.blogspam.net:9999/";
/**
* URL of the action to train the spam rejection plugins
* @see https://blogspam.net/api/2.0/classify.html
*/
const RECLASSIFY_COMMENT_URL = "http://test.blogspam.net:9999/classify";
private $logger;
private $container;
private $enabled;
public function __construct($logger, Container $container, $useSpamFilter = TRUE) {
$this->logger = $logger;
$this->container = $container;
$this->enabled = $useSpamFilter;
}
/**
* Composes a JSON-encoded string with a valid BlogSpam.net API request
* for both testComment and classifyComment actions
* @param Request $request
* @param string $train
* @return string
*/
private function composeBlogSpamRequest(Request $request, $train = null) {
$toBeSent = array(
'site' => $request->getSchemeAndHttpHost(),
'comment' => $request->request->get('message'),
'email' => $request->request->get('email'),
'name' => $request->request->get('name'),
'agent' => $request->server->get('HTTP_USER_AGENT'),
'ip' => $request->getClientIp()
);
if ($train) { // composing request for reclassify action
if ($train === 'spam' || $train === 'ok') {
$toBeSent['train'] = $train;
}
}
$this->logger->debug('BlogSpam.net service request:'.json_encode($toBeSent));
return json_encode($toBeSent);
}
/**
* Test comment or contact request form spam with BlogSpam.net API before processing a form
* @param Request $request
* @return
*/
private function testComment(Request $request) {
$buzz = $this->container->get('buzz');
$headers = ['Content-Type', 'application/json'];
$response = $buzz->post(
BlogSpam::TEST_COMMENT_URL,
$headers,
$this->composeBlogSpamRequest($request)
);
return json_decode($response->getContent());
}
/**
* Test comment or contact request form spam with BlogSpam.net API before processing a form
* Log the failed attempts in standard Symfony log
* @param Request $request
* @return boolean
*/
public function isSpam(Request $request) {
$clientIp = $request->getClientIp();
//$this->logger->debug('before check: '.$request);
if ($this->enabled) {
$resp = $this->testComment($request);
if ($resp && $resp->result) {
$this->logger->info('BlogSpam.net service : '.json_encode($resp).' :: client IP ' . $clientIp . ' host ' . $request->server->get('HTTP_HOST') . ' :: UA ' . $request->server->get('HTTP_USER_AGENT') . ' :: lang: ' . $request->server->get('HTTP_ACCEPT_LANGUAGE'));
return ($resp->result === 'SPAM');
}
else {
$this->logger->error('BlogSpam.net service Connection ERROR :: client IP ' . $clientIp . ' :: host ' . $request->server->get('HTTP_HOST') . ' :: UA ' . $request->server->get('HTTP_USER_AGENT') . ' :: lang: ' . $request->server->get('HTTP_ACCEPT_LANGUAGE'));
return false; //we do not know whether the message is spam
}
}
// else {
// $this->logger->warn('BlogSpam.net service DISABLED :: client IP ' . $clientIp . ' :: host ' . $request->server->get('HTTP_HOST') . ' :: UA ' . $request->server->get('HTTP_USER_AGENT') . ' :: lang: ' . $request->server->get('HTTP_ACCEPT_LANGUAGE'));
// }
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment