Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bot detector frontend data logging. Restrictions. #545

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
6 changes: 6 additions & 0 deletions inc/cleantalk-common.php
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,12 @@ function apbct__bot_detector_get_fd_log()
);
// Initialize result array with default values

if (defined('APBCT_DO_NOT_COLLECT_FRONTEND_DATA_LOGS')) {
$result['plugin_status'] = 'OK';
$result['error_msg'] = 'bot detector logs collection is disabled via constant definition';
return json_encode($result);
}

try {
if ( TT::toString($apbct->settings['data__bot_detector_enabled']) === '0') {
throw new \Exception('bot detector library usage is disabled');
Expand Down
47 changes: 38 additions & 9 deletions js/src/apbct-public--2--public.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let ctMouseReadInterval;
let ctMouseWriteDataInterval;
let tokenCheckerIntervalId;
let botDetectorLogLastUpdate = 0;
let botDetectorLogEventTypesCollected = [];

// eslint-disable-next-line require-jsdoc,camelcase
function apbct_attach_event_handler(elem, event, callback) {
Expand Down Expand Up @@ -38,9 +39,11 @@ cronFormsHandler(2000);
// bot_detector frontend_data log alt session saving cron
if (
ctPublicFunctions.hasOwnProperty('data__bot_detector_enabled') &&
ctPublicFunctions.data__bot_detector_enabled == 1
ctPublicFunctions.data__bot_detector_enabled == 1 &&
ctPublicFunctions.hasOwnProperty('data__frontend_data_log_enabled') &&
ctPublicFunctions.data__frontend_data_log_enabled == 1
) {
sendBotDetectorLogToAltSessions(500);
sendBotDetectorLogToAltSessions(1000);
}
/**
* Cron jobs end.
Expand Down Expand Up @@ -92,25 +95,51 @@ function cronFormsHandler(cronStartTimeout = 2000) {

/**
* Send BotDetector logs data to alternative sessions.
* If log_last_update has changed, the log will be sent to the alternative sessions.
* If log_last_update has changed and log contains new event types, the log will be sent to the alternative sessions.
* @param {int} cronStartTimeout delay before cron start
* @param {int} interval check fires on interval
*/
function sendBotDetectorLogToAltSessions(cronStartTimeout = 3000, interval = 1000) {
setTimeout(function() {
setInterval(function() {
const currentLog = apbctLocalStorage.get('ct_bot_detector_frontend_data_log');
if (currentLog && currentLog.hasOwnProperty('log_last_update')) {
if (botDetectorLogLastUpdate !== currentLog.log_last_update) {
botDetectorLogLastUpdate = currentLog.log_last_update;
// the log will be taken from javascriptclientdata
ctSetAlternativeCookie([], {forceAltCookies: true});
}
if (needsSaveLogToAltSessions(currentLog)) {
botDetectorLogLastUpdate = currentLog.log_last_update;
// the log will be taken from javascriptclientdata
ctSetAlternativeCookie([], {forceAltCookies: true});
}
}, interval);
}, cronStartTimeout);
}

/**
* Check if the log needs to be saved to the alt sessions. If the log has new event types, it will be saved.
* @param {object} currentLog
* @return {boolean}
*/
function needsSaveLogToAltSessions(currentLog) {
if (
currentLog && currentLog.hasOwnProperty('log_last_update') &&
botDetectorLogLastUpdate !== currentLog.log_last_update
) {
try {
for (let i = 0; i < currentLog.records.length; i++) {
const currentType = currentLog.records[i].frontend_data.js_event;
// check if this event type was already collected
if (currentType !== undefined && botDetectorLogEventTypesCollected.includes(currentType)) {
continue;
}
// add new event type to collection, this type will be sent to the alt sessions further
botDetectorLogEventTypesCollected.push(currentType);
return true;
}
} catch (e) {
console.log('APBCT: bot detector log collection error: ' . e.toString());
}
}
return false;
}

/**
* Restart event_token attachment if some forms load after document ready.
*/
Expand Down
47 changes: 38 additions & 9 deletions js/src/apbct-public-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2048,6 +2048,7 @@ let ctMouseReadInterval;
let ctMouseWriteDataInterval;
let tokenCheckerIntervalId;
let botDetectorLogLastUpdate = 0;
let botDetectorLogEventTypesCollected = [];

// eslint-disable-next-line require-jsdoc,camelcase
function apbct_attach_event_handler(elem, event, callback) {
Expand Down Expand Up @@ -2076,9 +2077,11 @@ cronFormsHandler(2000);
// bot_detector frontend_data log alt session saving cron
if (
ctPublicFunctions.hasOwnProperty('data__bot_detector_enabled') &&
ctPublicFunctions.data__bot_detector_enabled == 1
ctPublicFunctions.data__bot_detector_enabled == 1 &&
ctPublicFunctions.hasOwnProperty('data__frontend_data_log_enabled') &&
ctPublicFunctions.data__frontend_data_log_enabled == 1
) {
sendBotDetectorLogToAltSessions(500);
sendBotDetectorLogToAltSessions(1000);
}
/**
* Cron jobs end.
Expand Down Expand Up @@ -2130,25 +2133,51 @@ function cronFormsHandler(cronStartTimeout = 2000) {

/**
* Send BotDetector logs data to alternative sessions.
* If log_last_update has changed, the log will be sent to the alternative sessions.
* If log_last_update has changed and log contains new event types, the log will be sent to the alternative sessions.
* @param {int} cronStartTimeout delay before cron start
* @param {int} interval check fires on interval
*/
function sendBotDetectorLogToAltSessions(cronStartTimeout = 3000, interval = 1000) {
setTimeout(function() {
setInterval(function() {
const currentLog = apbctLocalStorage.get('ct_bot_detector_frontend_data_log');
if (currentLog && currentLog.hasOwnProperty('log_last_update')) {
if (botDetectorLogLastUpdate !== currentLog.log_last_update) {
botDetectorLogLastUpdate = currentLog.log_last_update;
// the log will be taken from javascriptclientdata
ctSetAlternativeCookie([], {forceAltCookies: true});
}
if (needsSaveLogToAltSessions(currentLog)) {
botDetectorLogLastUpdate = currentLog.log_last_update;
// the log will be taken from javascriptclientdata
ctSetAlternativeCookie([], {forceAltCookies: true});
}
}, interval);
}, cronStartTimeout);
}

/**
* Check if the log needs to be saved to the alt sessions. If the log has new event types, it will be saved.
* @param {object} currentLog
* @return {boolean}
*/
function needsSaveLogToAltSessions(currentLog) {
if (
currentLog && currentLog.hasOwnProperty('log_last_update') &&
botDetectorLogLastUpdate !== currentLog.log_last_update
) {
try {
for (let i = 0; i < currentLog.records.length; i++) {
const currentType = currentLog.records[i].frontend_data.js_event;
// check if this event type was already collected
if (currentType !== undefined && botDetectorLogEventTypesCollected.includes(currentType)) {
continue;
}
// add new event type to collection, this type will be sent to the alt sessions further
botDetectorLogEventTypesCollected.push(currentType);
return true;
}
} catch (e) {
console.log('APBCT: bot detector log collection error: ' . e.toString());
}
}
return false;
}

/**
* Restart event_token attachment if some forms load after document ready.
*/
Expand Down
47 changes: 38 additions & 9 deletions js/src/apbct-public-bundle_ext-protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2048,6 +2048,7 @@ let ctMouseReadInterval;
let ctMouseWriteDataInterval;
let tokenCheckerIntervalId;
let botDetectorLogLastUpdate = 0;
let botDetectorLogEventTypesCollected = [];

// eslint-disable-next-line require-jsdoc,camelcase
function apbct_attach_event_handler(elem, event, callback) {
Expand Down Expand Up @@ -2076,9 +2077,11 @@ cronFormsHandler(2000);
// bot_detector frontend_data log alt session saving cron
if (
ctPublicFunctions.hasOwnProperty('data__bot_detector_enabled') &&
ctPublicFunctions.data__bot_detector_enabled == 1
ctPublicFunctions.data__bot_detector_enabled == 1 &&
ctPublicFunctions.hasOwnProperty('data__frontend_data_log_enabled') &&
ctPublicFunctions.data__frontend_data_log_enabled == 1
) {
sendBotDetectorLogToAltSessions(500);
sendBotDetectorLogToAltSessions(1000);
}
/**
* Cron jobs end.
Expand Down Expand Up @@ -2130,25 +2133,51 @@ function cronFormsHandler(cronStartTimeout = 2000) {

/**
* Send BotDetector logs data to alternative sessions.
* If log_last_update has changed, the log will be sent to the alternative sessions.
* If log_last_update has changed and log contains new event types, the log will be sent to the alternative sessions.
* @param {int} cronStartTimeout delay before cron start
* @param {int} interval check fires on interval
*/
function sendBotDetectorLogToAltSessions(cronStartTimeout = 3000, interval = 1000) {
setTimeout(function() {
setInterval(function() {
const currentLog = apbctLocalStorage.get('ct_bot_detector_frontend_data_log');
if (currentLog && currentLog.hasOwnProperty('log_last_update')) {
if (botDetectorLogLastUpdate !== currentLog.log_last_update) {
botDetectorLogLastUpdate = currentLog.log_last_update;
// the log will be taken from javascriptclientdata
ctSetAlternativeCookie([], {forceAltCookies: true});
}
if (needsSaveLogToAltSessions(currentLog)) {
botDetectorLogLastUpdate = currentLog.log_last_update;
// the log will be taken from javascriptclientdata
ctSetAlternativeCookie([], {forceAltCookies: true});
}
}, interval);
}, cronStartTimeout);
}

/**
* Check if the log needs to be saved to the alt sessions. If the log has new event types, it will be saved.
* @param {object} currentLog
* @return {boolean}
*/
function needsSaveLogToAltSessions(currentLog) {
if (
currentLog && currentLog.hasOwnProperty('log_last_update') &&
botDetectorLogLastUpdate !== currentLog.log_last_update
) {
try {
for (let i = 0; i < currentLog.records.length; i++) {
const currentType = currentLog.records[i].frontend_data.js_event;
// check if this event type was already collected
if (currentType !== undefined && botDetectorLogEventTypesCollected.includes(currentType)) {
continue;
}
// add new event type to collection, this type will be sent to the alt sessions further
botDetectorLogEventTypesCollected.push(currentType);
return true;
}
} catch (e) {
console.log('APBCT: bot detector log collection error: ' . e.toString());
}
}
return false;
}

/**
* Restart event_token attachment if some forms load after document ready.
*/
Expand Down
47 changes: 38 additions & 9 deletions js/src/apbct-public-bundle_full-protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2048,6 +2048,7 @@ let ctMouseReadInterval;
let ctMouseWriteDataInterval;
let tokenCheckerIntervalId;
let botDetectorLogLastUpdate = 0;
let botDetectorLogEventTypesCollected = [];

// eslint-disable-next-line require-jsdoc,camelcase
function apbct_attach_event_handler(elem, event, callback) {
Expand Down Expand Up @@ -2076,9 +2077,11 @@ cronFormsHandler(2000);
// bot_detector frontend_data log alt session saving cron
if (
ctPublicFunctions.hasOwnProperty('data__bot_detector_enabled') &&
ctPublicFunctions.data__bot_detector_enabled == 1
ctPublicFunctions.data__bot_detector_enabled == 1 &&
ctPublicFunctions.hasOwnProperty('data__frontend_data_log_enabled') &&
ctPublicFunctions.data__frontend_data_log_enabled == 1
) {
sendBotDetectorLogToAltSessions(500);
sendBotDetectorLogToAltSessions(1000);
}
/**
* Cron jobs end.
Expand Down Expand Up @@ -2130,25 +2133,51 @@ function cronFormsHandler(cronStartTimeout = 2000) {

/**
* Send BotDetector logs data to alternative sessions.
* If log_last_update has changed, the log will be sent to the alternative sessions.
* If log_last_update has changed and log contains new event types, the log will be sent to the alternative sessions.
* @param {int} cronStartTimeout delay before cron start
* @param {int} interval check fires on interval
*/
function sendBotDetectorLogToAltSessions(cronStartTimeout = 3000, interval = 1000) {
setTimeout(function() {
setInterval(function() {
const currentLog = apbctLocalStorage.get('ct_bot_detector_frontend_data_log');
if (currentLog && currentLog.hasOwnProperty('log_last_update')) {
if (botDetectorLogLastUpdate !== currentLog.log_last_update) {
botDetectorLogLastUpdate = currentLog.log_last_update;
// the log will be taken from javascriptclientdata
ctSetAlternativeCookie([], {forceAltCookies: true});
}
if (needsSaveLogToAltSessions(currentLog)) {
botDetectorLogLastUpdate = currentLog.log_last_update;
// the log will be taken from javascriptclientdata
ctSetAlternativeCookie([], {forceAltCookies: true});
}
}, interval);
}, cronStartTimeout);
}

/**
* Check if the log needs to be saved to the alt sessions. If the log has new event types, it will be saved.
* @param {object} currentLog
* @return {boolean}
*/
function needsSaveLogToAltSessions(currentLog) {
if (
currentLog && currentLog.hasOwnProperty('log_last_update') &&
botDetectorLogLastUpdate !== currentLog.log_last_update
) {
try {
for (let i = 0; i < currentLog.records.length; i++) {
const currentType = currentLog.records[i].frontend_data.js_event;
// check if this event type was already collected
if (currentType !== undefined && botDetectorLogEventTypesCollected.includes(currentType)) {
continue;
}
// add new event type to collection, this type will be sent to the alt sessions further
botDetectorLogEventTypesCollected.push(currentType);
return true;
}
} catch (e) {
console.log('APBCT: bot detector log collection error: ' . e.toString());
}
}
return false;
}

/**
* Restart event_token attachment if some forms load after document ready.
*/
Expand Down
Loading
Loading