From 0e008210bccee55ead20fc7a93de1cdb96eb2561 Mon Sep 17 00:00:00 2001 From: Artem Anoshin Date: Wed, 22 Feb 2023 10:31:54 +0400 Subject: [PATCH 1/7] Added event_token to CleantalkRequest --- lib/CleantalkRequest.php | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/lib/CleantalkRequest.php b/lib/CleantalkRequest.php index 2da2f48..07e8b18 100644 --- a/lib/CleantalkRequest.php +++ b/lib/CleantalkRequest.php @@ -120,17 +120,6 @@ class CleantalkRequest { public $x_forwarded_for = ''; public $x_real_ip = ''; - /** - * Is enable Java Script, - * valid are 0|1|2 - * Status: - * null - JS html code not inserted into phpBB templates - * 0 - JS disabled at the client browser - * 1 - JS enabled at the client broswer - * @var int - */ - public $js_on = null; - /** * user time zone * @var string @@ -154,7 +143,12 @@ class CleantalkRequest { * Method name * @var string */ - public $method_name = 'check_message'; + public $method_name = 'check_message'; + + /** + * @var string|null + */ + public $event_token; /** * Fill params with constructor From 493a6c8b41c59e7c46a2681209fbaa21ad1a7e83 Mon Sep 17 00:00:00 2001 From: Artem Anoshin Date: Wed, 22 Feb 2023 11:36:29 +0400 Subject: [PATCH 2/7] Simplified the use of the library --- cleantalk-antispam.php | 72 ++++++++++++++++++++++++++++++++++++++++ lib/CleantalkRequest.php | 4 ++- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 cleantalk-antispam.php diff --git a/cleantalk-antispam.php b/cleantalk-antispam.php new file mode 100644 index 0000000..9bfecb5 --- /dev/null +++ b/cleantalk-antispam.php @@ -0,0 +1,72 @@ +apikey = $apikey; + $this->email_field = $email_field; + $this->user_name_field = $user_name_field; + $this->message_field = $message_field; + } + + public function handle() + { + if (count($_POST) === 0) { + $_SESSION['ct_submit_time'] = time(); + return; + } + + $sender_email = isset($_POST[$this->email_field]) ? $_POST[$this->email_field] : ''; + $sender_nickname = isset($_POST[$this->user_name_field]) ? $_POST[$this->user_name_field] : ''; + $message = isset($_POST[$this->message_field]) ? $_POST[$this->message_field] : ''; + $sender_ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null; + + $ct_request = new CleantalkRequest(); + + $ct_request->auth_key = $this->apikey; + $ct_request->agent = 'php-api'; + $ct_request->sender_email = $sender_email; + $ct_request->sender_ip = $sender_ip; + $ct_request->sender_nickname = $sender_nickname; + $ct_request->message = $message; + $ct_request->submit_time = time() - (int) $_SESSION['ct_submit_time']; + $ct_request->event_token = isset($_POST['ct_bot_detector_event_token']) ? $_POST['ct_bot_detector_event_token'] : null; + + $ct = new Cleantalk(); + $ct->server_url = $ct_request::CLEANTALK_API_URL; + + // Check + $ct_result = $ct->isAllowMessage($ct_request); + + if ($ct_result->allow == 1) { + echo 'Message allowed. Reason ' . $ct_result->comment; + } else { + echo 'Message forbidden. Reason ' . $ct_result->comment; + } + echo '

'; + } + + public function frontendScript() + { + echo ''; + } +} diff --git a/lib/CleantalkRequest.php b/lib/CleantalkRequest.php index 07e8b18..b2ab971 100644 --- a/lib/CleantalkRequest.php +++ b/lib/CleantalkRequest.php @@ -4,7 +4,9 @@ /** * Request class */ -class CleantalkRequest { +class CleantalkRequest +{ + const CLEANTALK_API_URL = 'https://moderate.cleantalk.org/api2.0/'; /** * All http request headers From 412d83e844e8a078daaff6a7633b0b8e2e7f932c Mon Sep 17 00:00:00 2001 From: Artem Anoshin Date: Wed, 22 Feb 2023 11:55:46 +0400 Subject: [PATCH 3/7] Updated readme.md --- README.md | 86 +++++++++---------------------------------------------- 1 file changed, 13 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index 1d55a2d..a6428ff 100644 --- a/README.md +++ b/README.md @@ -111,96 +111,36 @@ else ```php auth_key = $auth_key; - $ct_request->agent = 'php-api'; - $ct_request->sender_email = $sender_email; - $ct_request->sender_ip = $sender_ip; - $ct_request->sender_nickname = $sender_nickname; - $ct_request->js_on = $js_on; - $ct_request->message = $message; - $ct_request->submit_time = time() - (int) $_SESSION['ct_submit_time']; - - $ct = new Cleantalk(); - $ct->server_url = $config_url; - - // Check - $ct_result = $ct->isAllowMessage($ct_request); - - if ($ct_result->allow == 1) { - echo 'Message allowed. Reason ' . $ct_result->comment; - } else { - echo 'Message forbidden. Reason ' . $ct_result->comment; - } - echo '

'; -} -else -{ - $_SESSION['ct_submit_time'] = time(); -} +//require_once "lib/cleantalk-php-patch.php"; -- PHP-FPM +$apikey = 'your_cleantalk_api_key'; +$email_field = 'name_email_form_field'; +$user_name_field = 'name_user_name_form_field'; +$message_field = 'name_message_form_field'; + +require_once 'php-antispam/cleantalk-antispam.php'; +$cleantalk_antispam = new CleantalkAntispam($apikey, $email_field, $user_name_field, $message_field); +$cleantalk_antispam->handle(); ?>