From 3e062c808c289d61f75fdac3ba31ac5fc935fe39 Mon Sep 17 00:00:00 2001 From: Glomberg Date: Thu, 24 Aug 2023 10:50:47 +0300 Subject: [PATCH] New. Integration. Contact form protection implemented. --- cleantalkantispam.php | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/cleantalkantispam.php b/cleantalkantispam.php index 10e7f6e..810bf10 100644 --- a/cleantalkantispam.php +++ b/cleantalkantispam.php @@ -13,13 +13,13 @@ class CleantalkAntispam extends Module { - public const ENGINE = 'prestashop-1.0.0'; + public const ENGINE = 'prestashop-1.1.0'; public function __construct() { $this->name = 'cleantalkantispam'; $this->tab = 'administration'; - $this->version = '1.0.0'; + $this->version = '1.1.0'; $this->author = 'CleanTalk Developers Team'; $this->need_instance = 0; $this->ps_versions_compliancy = [ @@ -39,7 +39,8 @@ public function __construct() public function install() { return parent::install() - && $this->registerHook('actionSubmitAccountBefore'); + && $this->registerHook('actionSubmitAccountBefore') + && $this->registerHook('actionFrontControllerInitAfter'); } public function uninstall() @@ -123,6 +124,24 @@ public function displayForm() } public function hookActionSubmitAccountBefore($params) + { + $data = Tools::getAllValues(); + return $this->checkSpam($data, true); + } + + public function hookActionFrontControllerInitAfter(&$params) + { + // Contact Form integration + if ( Tools::isSubmit('submitMessage') && isset($params['controller']) && $params['controller'] instanceof \ContactController ) { + $form_data = Tools::getAllValues(); + $data['email'] = isset($form_data['from']) ? $form_data['from'] : ''; + $data['message'] = isset($form_data['message']) ? $form_data['message'] : ''; + return $this->checkSpam($data); + } + return true; + } + + private function checkSpam($data, $is_check_register = false) { if ( ! Configuration::get('CLEANTALKANTISPAM_API_KEY') ) { return true; @@ -130,8 +149,6 @@ public function hookActionSubmitAccountBefore($params) $ct_request = new CleantalkRequest; - $data = Tools::getAllValues(); - $ct_request->auth_key = Configuration::get('CLEANTALKANTISPAM_API_KEY'); $ct_request->agent = self::ENGINE; @@ -148,11 +165,12 @@ public function hookActionSubmitAccountBefore($params) $ct_request->sender_email = isset($data['email']) ? $data['email'] : ''; $ct_request->sender_nickname = isset($data['firstname']) ? $data['firstname'] : ''; $ct_request->sender_nickname .= isset($data['lastname']) ? ' ' . $data['lastname'] : ''; + $ct_request->message = isset($data['message']) ? ' ' . $data['message'] : ''; $ct = new Cleantalk(); $ct->server_url = 'https://moderate.cleantalk.org'; - $result = $ct->isAllowUser($ct_request); + $result = $is_check_register ? $ct->isAllowUser($ct_request) : $ct->isAllowMessage($ct_request); $result = json_decode(json_encode($result), true); if ($result['allow'] == 0) {