Skip to content

Instantly share code, notes, and snippets.

@QROkes
Last active Jan 11, 2020
Embed
What would you like to do?
Akismet SPAM Check for Question2Answer
<?php
/*
File: qa-plugin/webinoly-custom/qa-plugin.php
Description: Anti SPAM Registration Plugin for Question2Answer
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
/*
Plugin Name: Akismet Spam
Plugin URI: https://webinoly.com/support/
Plugin Description: Anti SPAM Plugin for Question2Answer
Plugin Version: 1.0
Plugin Date: 2019-06-5
Plugin Author: QROkes
Plugin Author URI: https://qrokes.com/en/
Plugin License: GPLv2
Plugin Minimum Question2Answer Version: 1.8.0
Plugin Update Check URI:
*/
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
header('Location: ../../');
exit;
}
qa_register_plugin_module( 'filter', 'qa-spam-akismet.php', 'qa_akismet', 'Q2A Spam Akismet' );
<?php
// https://akismet.com/development/api/#comment-check
class qa_akismet {
function akismet_comment_check( $data ) {
$key = '123456789';
$data['blog'] = "https://domain.com/forum/";
$data['user_agent'] = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6";
$data['user_ip'] = qa_remote_ip_address();
if( empty($data['comment_author']) ){$data['comment_author']=qa_get_logged_in_handle();}
if( empty($data['comment_author_email']) ){$data['comment_author_email']=qa_get_logged_in_email();}
$request = 'blog='. urlencode($data['blog']) .
'&user_ip='. urlencode($data['user_ip']) .
'&user_agent='. urlencode($data['user_agent']) .
'&comment_type='. urlencode($data['comment_type']) .
'&comment_author='. urlencode($data['comment_author']) .
'&comment_author_email='. urlencode($data['comment_author_email']) .
'&comment_content='. urlencode($data['comment_content']);
$host = $http_host = $key.'.rest.akismet.com';
$path = '/1.1/comment-check';
$port = 443;
$akismet_ua = "Question2Answer | Community Forum";
$content_length = strlen( $request );
$http_request = "POST $path HTTP/1.0\r\n";
$http_request .= "Host: $host\r\n";
$http_request .= "Content-Type: application/x-www-form-urlencoded\r\n";
$http_request .= "Content-Length: {$content_length}\r\n";
$http_request .= "User-Agent: {$akismet_ua}\r\n";
$http_request .= "\r\n";
$http_request .= $request;
$response = '';
if( false != ( $fs = @fsockopen( 'ssl://' . $http_host, $port, $errno, $errstr, 10 ) ) ) {
fwrite( $fs, $http_request );
while ( !feof( $fs ) )
$response .= fgets( $fs, 1160 ); // One TCP-IP packet
fclose( $fs );
$response = explode( "\r\n\r\n", $response, 2 );
}
// Passes back true (it's spam) or false (it's ham)
if ( 'true' == $response[1] )
return true;
else
return false;
}
function filter_question(&$question, &$errors, $oldquestion) {
// Don't check for admins and known users
if (( is_numeric(qa_get_logged_in_level()) and qa_get_logged_in_level() > 49 ) || (is_numeric(qa_get_logged_in_points()) and qa_get_logged_in_points() > 200))
return;
// combine question title and content for spam processing
$combined_content = $question['title'].' '.$question['text'];
$check_content = array(
'comment_type' => 'forum-post',
'comment_author_email' => $question['email'],
'comment_content' => $combined_content
);
if (self::akismet_comment_check($check_content)) {
$question['queued']=true;
}
}
function filter_answer(&$answer, &$errors, $question, $oldanswer) {
// Don't check for admins and known users
if (( is_numeric(qa_get_logged_in_level()) and qa_get_logged_in_level() > 49 ) || (is_numeric(qa_get_logged_in_points()) and qa_get_logged_in_points() > 200))
return;
$check_content = array(
'comment_type' => 'reply',
'comment_author_email' => $answer['email'],
'comment_content' => $answer['text']
);
if (self::akismet_comment_check($check_content)) {
$answer['queued']=true;
}
}
function filter_comment(&$comment, &$errors, $question, $parent, $oldcomment) {
// Don't check for admins and known users
if (( is_numeric(qa_get_logged_in_level()) and qa_get_logged_in_level() > 49 ) || (is_numeric(qa_get_logged_in_points()) and qa_get_logged_in_points() > 200))
return;
$check_content = array(
'comment_type' => 'reply',
'comment_author_email' => $comment['email'],
'comment_content' => $comment['text']
);
if (self::akismet_comment_check($check_content)) {
$comment['queued']=true;
}
}
}
@thierryzoller

This comment has been minimized.

Copy link

@thierryzoller thierryzoller commented Jan 11, 2020

Hey,
Thank you, where to get the "key" from ?

@QROkes

This comment has been minimized.

Copy link
Owner Author

@QROkes QROkes commented Jan 11, 2020

@thierryzoller "key" is the API-Key for Akismet service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment