Skip to content

Commit

Permalink
Merge pull request #10 from CleanTalk/bot_detector_av
Browse files Browse the repository at this point in the history
New. Bot detector. Adding bot detector, new option, js library, passing event_token
  • Loading branch information
AntonV1211 authored Sep 9, 2024
2 parents 35eff5a + c19ad66 commit 5ee17e5
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CleanTalk/XF/Spam/Checker/CleanTalkContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ protected function isSpam(\XF\Entity\User $user, $message, $extraParams)
$js_timezone = (isset($_COOKIE['ct_timezone']) ? $_COOKIE['ct_timezone'] : '');
$first_key_timestamp = (isset($_COOKIE['ct_fkp_timestamp']) ? $_COOKIE['ct_fkp_timestamp'] : '');
$pointer_data = (isset($_COOKIE['ct_pointer_data']) ? json_decode($_COOKIE['ct_pointer_data']) : '');
$event_token = isset($_POST['ct_bot_detector_event_token']) ? $_POST['ct_bot_detector_event_token'] : '';

$sender_info = json_encode(
array(
Expand Down Expand Up @@ -148,6 +149,7 @@ protected function isSpam(\XF\Entity\User $user, $message, $extraParams)
$ct_request->auth_key = $this->getApiKey();
$ct_request->sender_email = $user->email;
$ct_request->sender_nickname = $user->username;
$ct_request->event_token = $event_token;
$ct_request->message = $message;
$ct_request->sender_ip = CleantalkHelper::ipGet('real', false);
$ct_request->x_forwarded_for = CleantalkHelper::ipGet('x_forwarded_for', false);
Expand Down
2 changes: 2 additions & 0 deletions CleanTalk/XF/Spam/Checker/CleanTalkUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ protected function isSpam(\XF\Entity\User $user, $extraParams)
$js_timezone = (isset($_COOKIE['ct_timezone']) ? $_COOKIE['ct_timezone'] : '');
$first_key_timestamp = (isset($_COOKIE['ct_fkp_timestamp']) ? $_COOKIE['ct_fkp_timestamp'] : '');
$pointer_data = (isset($_COOKIE['ct_pointer_data']) ? json_decode($_COOKIE['ct_pointer_data']) : '');
$event_token = isset($_POST['ct_bot_detector_event_token']) ? $_POST['ct_bot_detector_event_token'] : '';

$sender_info = json_encode(
array(
Expand Down Expand Up @@ -82,6 +83,7 @@ protected function isSpam(\XF\Entity\User $user, $extraParams)
$ct_request->auth_key = $this->getApiKey();
$ct_request->sender_email = $user->email;
$ct_request->sender_nickname = $user->username;
$ct_request->event_token = $event_token;
$ct_request->sender_ip = CleantalkHelper::ipGet('real', false);
$ct_request->x_forwarded_for = CleantalkHelper::ipGet('x_forwarded_for', false);
$ct_request->x_real_ip = CleantalkHelper::ipGet('x_real_ip', false);
Expand Down
69 changes: 69 additions & 0 deletions CleanTalk/XF/Template/Templater.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,75 @@ public function renderTemplate(
);
}

// Bot detector
if ( $this->app->options()->ct_bot_detector && !\XF::visitor()->is_admin ) {
if ( strripos($output, '</head>') >= 0 ) {
$output = str_replace(
'</head>',
'<script src="https://moderate.cleantalk.org/ct-bot-detector-wrapper.js"></script>
<script>
let apbctLocalStorage = {
get: function(key, property) {
if ( typeof property === "undefined" ) {
property = "value";
}
const storageValue = localStorage.getItem(key);
if ( storageValue !== null ) {
try {
const json = JSON.parse(storageValue);
if ( json.hasOwnProperty(property) ) {
try {
// if property can be parsed as JSON - do it
return JSON.parse( json[property] );
} catch (e) {
// if not - return string of value
return json[property].toString();
}
} else {
return json;
}
} catch (e) {
return storageValue;
}
}
return false;
},
};
cronFormsHandler(2000);
/**
* Do handle periodical actions.
* @param {int} cronStartTimeout Time to go before cron start.
*/
function cronFormsHandler(cronStartTimeout = 2000) {
setTimeout(function() {
setInterval(function() {
restartBotDetectorEventTokenAttach();
}, 2000);
}, cronStartTimeout);
}
/**
* Restart event_token attachment if some forms load after document ready.
*/
function restartBotDetectorEventTokenAttach() {
try {
const token = apbctLocalStorage.get("bot_detector_event_token");
if (typeof setEventTokenField === "function" && token !== undefined && token.length === 64) {
setEventTokenField(token);
}
} catch (e) {
console.log(e.toString());
}
}
</script>
</head>',
$output
);
}
}

return $output;
}
}
4 changes: 4 additions & 0 deletions CleanTalk/_data/options.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
<default_value>1</default_value>
<relation group_id="cleantalk" display_order="4"/>
</option>
<option option_id="ct_bot_detector" edit_format="onoff" data_type="boolean">
<default_value>1</default_value>
<relation group_id="cleantalk" display_order="4"/>
</option>
<option option_id="ct_footerlink" edit_format="onoff" data_type="boolean">
<default_value>0</default_value>
<relation group_id="cleantalk" display_order="7"/>
Expand Down
2 changes: 2 additions & 0 deletions CleanTalk/_data/phrases.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
<phrase title="option.ct_check_pm" version_id="20" version_string="2.0"><![CDATA[Check private messages in conversations]]></phrase>
<phrase title="option.ct_checkcontent" version_id="10" version_string="1.0"><![CDATA[Check users content against the CleanTalk antispam service]]></phrase>
<phrase title="option.ct_checkreg" version_id="10" version_string="1.0"><![CDATA[Check new registrations against the CleanTalk antispam service]]></phrase>
<phrase title="option.ct_bot_detector" version_id="10" version_string="1.0"><![CDATA[Use Anti-Spam by CleanTalk JavaScript library]]></phrase>
<phrase title="option.ct_footerlink" version_id="14" version_string="1.4"><![CDATA[Tell others about CleanTalk]]></phrase>
<phrase title="option.ct_sfw" version_id="16" version_string="1.6"><![CDATA[Enable SpamFireWall]]></phrase>
<phrase title="option_explain.ct_apikey" version_id="10" version_string="1.0"><![CDATA[Click <a href='https://cleantalk.org/register?platform=xenforo2'>here</a> to get access key.]]></phrase>
<phrase title="option_explain.ct_block_type" version_id="20" version_string="2.0"><![CDATA[]]></phrase>
<phrase title="option_explain.ct_check_pm" version_id="23" version_string="2.3"><![CDATA[Anti-spam by CleanTalk will check all private messages of users with fewer than 3 messages]]></phrase>
<phrase title="option_explain.ct_bot_detector" version_id="23" version_string="2.3"><![CDATA[This option includes external Anti-Spam by CleanTalk JavaScript library to getting visitors info data]]></phrase>
<phrase title="option_explain.ct_checkcontent" version_id="23" version_string="2.3"><![CDATA[Anti-spam by CleanTalk will check content of users with fewer than 3 messages on your forum]]></phrase>
<phrase title="option_explain.ct_checkreg" version_id="10" version_string="1.0"><![CDATA[Anti-spam by CleanTalk will check all registrations on your forum]]></phrase>
<phrase title="option_explain.ct_footerlink" version_id="14" version_string="1.4"><![CDATA[Checking this box places a small link in the footer that lets others know what anti-spam tool protects your website]]></phrase>
Expand Down

0 comments on commit 5ee17e5

Please sign in to comment.