Skip to content

Commit

Permalink
Checking comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
znaeff committed Mar 18, 2016
1 parent 33c5fe2 commit 05a0d1a
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 61 deletions.
4 changes: 3 additions & 1 deletion CleanTalk/Base/CleanTalk.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ static public function getCheckjsName() {
public static function hookAdminSettings(XenForo_Visitor &$visitor )
{
$options = XenForo_Application::getOptions();
if ($options->get('cleantalk', 'enabled') && sizeof($_POST)>0 && isset($_POST['options']) && isset($_POST['options']['cleantalk']))
if ($options->get('cleantalk', 'enabled_reg') && sizeof($_POST)>0 && isset($_POST['options']) && isset($_POST['options']['cleantalk']))
{
require_once 'CleanTalk/Base/cleantalk.class.php';
$ct_ws = array(
Expand Down Expand Up @@ -83,6 +83,8 @@ function ctSetCookie(c_name, value) {
$ret_val.="<div style='width:100%;text-align:center'><a href='https://cleantalk.org/xenforo-antispam-addon'>XenForo spam</a> blocked by CleanTalk.</div>";
}
}
// XenForo_Application::getSession()->set('ct_submit_comment_time', time()); - got error 'The session has been saved and is now read-only'
// XenForo_Application::setSimpleCacheData('ct_submit_comment_time', time());
return $ret_val;
}

Expand Down
15 changes: 0 additions & 15 deletions CleanTalk/ControllerPublic/CleanTalk.php

This file was deleted.

16 changes: 16 additions & 0 deletions CleanTalk/ControllerPublic/CleanTalkForum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

class CleanTalk_ControllerPublic_CleanTalkForum extends XFCP_CleanTalk_ControllerPublic_CleanTalkForum {

public function actionCreateThread() {
$options = XenForo_Application::getOptions();
if ($options->get('cleantalk', 'enabled_comm')) {
XenForo_Application::getSession()->set('ct_submit_comment_time', time());
$field_name = CleanTalk_Base_CleanTalk::getCheckjsName();
$ct_check_def = CleanTalk_Base_CleanTalk::getCheckjsDefaultValue();
setcookie($field_name, $ct_check_def, 0, '/');
}
return parent::actionCreateThread();
}

}
16 changes: 16 additions & 0 deletions CleanTalk/ControllerPublic/CleanTalkPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

class CleanTalk_ControllerPublic_CleanTalkPost extends XFCP_CleanTalk_ControllerPublic_CleanTalkPost {

public function actionEdit() {
$options = XenForo_Application::getOptions();
if ($options->get('cleantalk', 'enabled_comm')) {
XenForo_Application::getSession()->set('ct_submit_comment_time', time());
$field_name = CleanTalk_Base_CleanTalk::getCheckjsName();
$ct_check_def = CleanTalk_Base_CleanTalk::getCheckjsDefaultValue();
setcookie($field_name, $ct_check_def, 0, '/');
}
return parent::actionEdit();
}

}
16 changes: 16 additions & 0 deletions CleanTalk/ControllerPublic/CleanTalkRegister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

class CleanTalk_ControllerPublic_CleanTalkRegister extends XFCP_CleanTalk_ControllerPublic_CleanTalkRegister {

protected function _getRegisterFormResponse(array $fields, array $errors = array()) {
$options = XenForo_Application::getOptions();
if ($options->get('cleantalk', 'enabled_reg')) {
XenForo_Application::getSession()->set('ct_submit_register_time', time());
$field_name = CleanTalk_Base_CleanTalk::getCheckjsName();
$ct_check_def = CleanTalk_Base_CleanTalk::getCheckjsDefaultValue();
setcookie($field_name, $ct_check_def, 0, '/');
}
return parent::_getRegisterFormResponse($fields, $errors);
}

}
16 changes: 16 additions & 0 deletions CleanTalk/ControllerPublic/CleanTalkThread.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

class CleanTalk_ControllerPublic_CleanTalkThread extends XFCP_CleanTalk_ControllerPublic_CleanTalkThread {

public function actionReply() {
$options = XenForo_Application::getOptions();
if ($options->get('cleantalk', 'enabled_comm')) {
XenForo_Application::getSession()->set('ct_submit_comment_time', time());
$field_name = CleanTalk_Base_CleanTalk::getCheckjsName();
$ct_check_def = CleanTalk_Base_CleanTalk::getCheckjsDefaultValue();
setcookie($field_name, $ct_check_def, 0, '/');
}
return parent::actionReply();
}

}
22 changes: 20 additions & 2 deletions CleanTalk/Listener/LoadClassController.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
<?php
class CleanTalk_Listener_LoadClassController {

public static function loadClassListener($class, &$extend) {
public static function loadClassListenerRegister($class, &$extend) {
if ($class == 'XenForo_ControllerPublic_Register') {
$extend[] = 'CleanTalk_ControllerPublic_CleanTalk';
$extend[] = 'CleanTalk_ControllerPublic_CleanTalkRegister';
}
}

public static function loadClassListenerPost($class, &$extend) {
if ($class == 'XenForo_ControllerPublic_Post') {
$extend[] = 'CleanTalk_ControllerPublic_CleanTalkPost';
}
}

public static function loadClassListenerForum($class, &$extend) {
if ($class == 'XenForo_ControllerPublic_Forum') {
$extend[] = 'CleanTalk_ControllerPublic_CleanTalkForum';
}
}

public static function loadClassListenerThread($class, &$extend) {
if ($class == 'XenForo_ControllerPublic_Thread') {
$extend[] = 'CleanTalk_ControllerPublic_CleanTalkThread';
}
}

Expand Down
103 changes: 86 additions & 17 deletions CleanTalk/Model/CleanTalk.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,23 @@ protected function _allowRegistration(array $user, Zend_Controller_Request_Http
return $decisions;
}

protected function _allowMessage($content, array $extraParams = array(), Zend_Controller_Request_Http $request) {
$decisions = parent::_allowMessage($content, $extraParams, $request);
if (!is_array($decisions)) {
$decisions = array(self::RESULT_ALLOWED);
}
if (!is_array($this->_resultDetails)) {
$this->_resultDetails = array();
}
$decisions[] = $this->_checkMessage($content, $extraParams, $request);
return $decisions;
}

protected function _checkNewUser(array $user, Zend_Controller_Request_Http $request) {
$decision = self::RESULT_ALLOWED;

$options = XenForo_Application::getOptions();
if ($options->get('cleantalk', 'enabled')) {
if ($options->get('cleantalk', 'enabled_reg')) {
if(!is_array($this->_resultDetails)) {
$this->_resultDetails = array();
}
Expand Down Expand Up @@ -60,6 +72,59 @@ protected function _checkNewUser(array $user, Zend_Controller_Request_Http $requ
return $decision;
}

protected function _checkMessage($content, array $extraParams = array(), Zend_Controller_Request_Http $request) {
$decision = self::RESULT_ALLOWED;

$options = XenForo_Application::getOptions();
if ($options->get('cleantalk', 'enabled_comm')) {
if(!is_array($this->_resultDetails)) {
$this->_resultDetails = array();
}

$visitor = XenForo_Visitor::getInstance();

$spam_check = array();
$spam_check['type'] = 'comment';
$spam_check['sender_email'] = $visitor['email'];
$spam_check['sender_nickname'] = $visitor['username'];
$spam_check['message_body'] = $content;

// $spam_check['sender_email'] = $user['email'];
// $spam_check['sender_nickname'] = $user['username'];
// $spam_check['timezone'] = $user['timezone'];

$field_name = CleanTalk_Base_CleanTalk::getCheckjsName();

if (!isset($_COOKIE[$field_name])) {
$checkjs = NULL;
}
//elseif ($_COOKIE[$field_name] == CleanTalk_Base_CleanTalk::getCheckjsValue()) {
elseif (in_array($_COOKIE[$field_name], CleanTalk_Base_CleanTalk::getCheckJSArray())) {
$checkjs = 1;
}
else {
$checkjs = 0;
}

$spam_result = $this->_checkSpam($spam_check, $options);
if (isset($spam_result)
&& is_array($spam_result)
&& $spam_result['errno'] == 0
&& $spam_result['allow'] != 1 ||
($spam_result['errno'] !=0 && $checkjs != 1)
) {
$decision = self::RESULT_DENIED;
$this->_resultDetails[] = array(
'phrase' => 'cleantalk_response',
'data' => array('response' => $spam_result['ct_result_comment']
)
);
}

}
return $decision;
}

protected function _checkSpam($spam_check, $options) {
require_once 'CleanTalk/Base/cleantalk.class.php';

Expand Down Expand Up @@ -98,8 +163,11 @@ protected function _checkSpam($spam_check, $options) {
$ct->server_changed = $ct_ws['server_changed'];

$options = XenForo_Application::getOptions();
$ct_options=array('enabled' => $options->get('cleantalk', 'enabled'),
'apikey' => $options->get('cleantalk', 'apikey'));
$ct_options=array(
'enabled_reg' => $options->get('cleantalk', 'enabled_reg'),
'enabled_comm' => $options->get('cleantalk', 'enabled_comm'),
'apikey' => $options->get('cleantalk', 'apikey')
);

$sender_info = json_encode(
array(
Expand All @@ -126,41 +194,42 @@ protected function _checkSpam($spam_check, $options) {
switch ($spam_check['type']) {
case 'comment':
$stored_time = XenForo_Application::getSession()->get('ct_submit_comment_time');
//$stored_time = XenForo_Application::getSimpleCacheData('ct_submit_comment_time');
if (isset($stored_time)) {
$ct_submit_time = time() - $stored_time;
}
$timelabels_key = 'e_comm';

$ct_request->submit_time = $ct_submit_time;
$ct_request->message = $spam_check['message_title'] . " \n\n" . $spam_check['message_body'];
$ct_request->message = $spam_check['message_body'];

$example = '';
$a_example = array();
$a_example['title'] = $spam_check['example_title'];
$a_example['body'] = $spam_check['example_body'];
$a_example['comments'] = $spam_check['example_comments'];
// $example = '';
// $a_example = array();
// $a_example['title'] = $spam_check['example_title'];
// $a_example['body'] = $spam_check['example_body'];
// $a_example['comments'] = $spam_check['example_comments'];

// Additional info.
$post_info = '';
$a_post_info['comment_type'] = 'comment';

// JSON format.
$example = json_encode($a_example);
// $example = json_encode($a_example);
$post_info = json_encode($a_post_info);

// Plain text format.
if ($example === FALSE) {
$example = '';
$example .= $a_example['title'] . " \n\n";
$example .= $a_example['body'] . " \n\n";
$example .= $a_example['comments'];
}
// if ($example === FALSE) {
// $example = '';
// $example .= $a_example['title'] . " \n\n";
// $example .= $a_example['body'] . " \n\n";
// $example .= $a_example['comments'];
// }
if ($post_info === FALSE) {
$post_info = '';
}

// Example text + last N comments in json or plain text format.
$ct_request->example = $example;
// $ct_request->example = $example;
$ct_request->post_info = $post_info;

$ct_result = $ct->isAllowMessage($ct_request);
Expand Down
28 changes: 15 additions & 13 deletions CleanTalk/addon-CleanTalk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@
<xen:label>{xen:phrase cleantalk_options_title}:</xen:label>
<xen:option name="{$fieldPrefix}[{$preparedOption.option_id}][link]" selected="{$preparedOption.option_value.link}" label="{xen:phrase cleantalk_options_link}"><br />
</xen:option>
<xen:option name="{$fieldPrefix}[{$preparedOption.option_id}][enabled]" selected="{$preparedOption.option_value.enabled}" label="{xen:phrase cleantalk_options_enabled}">
<xen:option name="{$fieldPrefix}[{$preparedOption.option_id}][enabled_reg]" selected="{$preparedOption.option_value.enabled_reg}" label="{xen:phrase cleantalk_options_enabled_reg}">
<xen:disabled>
<div>{xen:phrase cleantalk_options_apikey}:</div>
<xen:textbox name="{$fieldPrefix}[{$preparedOption.option_id}][apikey]" value="{$preparedOption.option_value.apikey}" inputclass="indented" />
<p class="hint">{xen:phrase cleantalk_options_apikey_explain}</p>
</xen:disabled>
<xen:disabled>
<div>{xen:phrase cleantalk_options_num_mes}:</div>
<xen:textbox name="{$fieldPrefix}[{$preparedOption.option_id}][num_mes]" value="{$preparedOption.option_value.num_mes}" inputclass="indented" />
<p class="hint">{xen:phrase cleantalk_options_num_mes_explain}</p>
</xen:disabled>
</xen:option>
<xen:option name="{$fieldPrefix}[{$preparedOption.option_id}][enabled_comm]" selected="{$preparedOption.option_value.enabled_comm}" label="{xen:phrase cleantalk_options_enabled_comm}">
</xen:option>
<xen:html>
<input type="hidden" name="{$listedFieldName}" value="{$preparedOption.option_id}" />
Expand All @@ -30,7 +27,10 @@
<admin_template_modifications/>
<code_events/>
<code_event_listeners>
<listener event_id="load_class_controller" execute_order="10" callback_class="CleanTalk_Listener_LoadClassController" callback_method="loadClassListener" active="1" hint="XenForo_ControllerPublic_Register" description="Listens for XenForo_ControllerPublic_Register class"/>
<listener event_id="load_class_controller" execute_order="10" callback_class="CleanTalk_Listener_LoadClassController" callback_method="loadClassListenerRegister" active="1" hint="XenForo_ControllerPublic_Register" description="Listens for XenForo_ControllerPublic_Register class"/>
<listener event_id="load_class_controller" execute_order="10" callback_class="CleanTalk_Listener_LoadClassController" callback_method="loadClassListenerPost" active="1" hint="XenForo_ControllerPublic_Post" description="Listens for XenForo_ControllerPublic_Post class"/>
<listener event_id="load_class_controller" execute_order="10" callback_class="CleanTalk_Listener_LoadClassController" callback_method="loadClassListenerForum" active="1" hint="XenForo_ControllerPublic_Forum" description="Listens for XenForo_ControllerPublic_Forum class"/>
<listener event_id="load_class_controller" execute_order="10" callback_class="CleanTalk_Listener_LoadClassController" callback_method="loadClassListenerThread" active="1" hint="XenForo_ControllerPublic_Thread" description="Listens for XenForo_ControllerPublic_Thread class"/>
<listener event_id="load_class_model" execute_order="10" callback_class="CleanTalk_Listener_LoadClassModel" callback_method="loadClassListener" active="1" hint="XenForo_Model_SpamPrevention" description="Listens for XenForo_Model_SpamPrevention class"/>
<listener event_id="visitor_setup" execute_order="10" callback_class="CleanTalk_Base_CleanTalk" callback_method="hookAdminSettings" active="1" hint="" description="Hook settings saving"/>
</code_event_listeners>
Expand All @@ -49,12 +49,13 @@
<email_template_modifications/>
<optiongroups>
<option option_id="cleantalk" edit_format="template" data_type="array" can_backup="1">
<default_value>a:1:{s:7:"enabled";s:1:"1";}</default_value>
<default_value>a:1:{s:7:"enabled";s:7:"disbled";s:1:"1";}</default_value>
<edit_format_params>option_template_CleanTalk</edit_format_params>
<sub_options>enabled
<sub_options>enabled_reg
enabled_comm
link
num_mes
apikey</sub_options>
<relation group_id="messageOptions" display_order="5"/>
<relation group_id="spam" display_order="5"/>
<relation group_id="usersAndRegistration" display_order="5"/>
</option>
Expand All @@ -68,13 +69,14 @@ apikey</sub_options>
<phrase title="cleantalk_error" version_id="1" version_string="1.0"><![CDATA[CleanTalk addon error]]></phrase>
<phrase title="cleantalk_options_apikey" version_id="1" version_string="1.0"><![CDATA[Access key]]></phrase>
<phrase title="cleantalk_options_apikey_explain" version_id="1" version_string="1.0"><![CDATA[Click <a target="_blank" href="https://cleantalk.org/register?platform=xenforo">here</a> to get access key.]]></phrase>
<phrase title="cleantalk_options_enabled" version_id="1" version_string="1.0"><![CDATA[Check new registrations against the CleanTalk antispam service.]]></phrase>
<phrase title="cleantalk_options_enabled_comm" version_id="1" version_string="1.0"><![CDATA[Check users comments and threads against the CleanTalk antispam service.]]></phrase>
<phrase title="cleantalk_options_enabled_reg" version_id="1" version_string="1.0"><![CDATA[Check new registrations against the CleanTalk antispam service.]]></phrase>
<phrase title="cleantalk_options_link" version_id="1" version_string="1.0"><![CDATA[Tell others about CleanTalk]]></phrase>
<phrase title="cleantalk_options_link_explain" version_id="1" version_string="1.0"><![CDATA[Checking this box places a small link under the comment form that lets others know what anti-spam tool protects your site.]]></phrase>
<phrase title="cleantalk_options_num_mes" version_id="1" version_string="1.0"><![CDATA[Check only first N messages]]></phrase>
<phrase title="cleantalk_options_num_mes_explain" version_id="1" version_string="1.0"><![CDATA[Don't check comments for users, that have above N approved comments. Leave 0 to check all comments.]]></phrase>
<phrase title="cleantalk_options_title" version_id="1" version_string="1.0"><![CDATA[Antispam by CleanTalk]]></phrase>
<phrase title="cleantalk_response" version_id="1" version_string="1.0"><![CDATA[CleanTalk: {response}]]></phrase>
<phrase title="option_cleantalk" version_id="1" version_string="1.6"><![CDATA[CleanTalk]]></phrase>
<phrase title="option_cleantalk_explain" version_id="1" version_string="1.6"><![CDATA[]]></phrase>
</phrases>
<route_prefixes/>
<style_properties/>
Expand Down
Loading

0 comments on commit 05a0d1a

Please sign in to comment.