How to Protect PrestaShop From Spambots
This guide is outdated, please use the new guide to protect your PrestaShop site from spam and spam bots.
PrestaShop 1.7.0.0 and higher.
1. Download CleanTalk API from Github.com.
2. Unzip the downloaded archive to your <root directory>/classes/. You will see the folder with the name "php-antispam-master". Rename it to "cleantalk".
3. Go to <root directory>/classes/cleantalk and open CleantalkValidate.php. Find string:
public static $access_key = 'your access key';
Enter your CleanTalk access key instead of your access key:
public static $access_key = '123456';
4. To protect the registration form, go to classes/form/ and open CustomerForm.php.
Paste the following code at the beginning (after <?php):
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/classes/cleantalk/CleantalkValidate.php';
Paste the following code in the validate section (after $this->validateByModules();):
$spamCheckResult = CleantalkValidate::spamCheckUser($customer->firstname.' '.$customer->lastname, $customer->email);
if ($spamCheckResult->allow == 0)
$emailField->addError($this->translator->trans(
$spamCheckResult->comment, array(), 'Shop.Notifications.Error'
));
5. Use the blacklisted e-mail stop_email@example.com to register. As a result, you will see the blocking message.
6. To protect contact form, go to <root directory>/modules/contactform and open contactform.php
Paste the following code at the beginning (after <?php):
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/classes/cleantalk/CleantalkValidate.php';
Paste before
$customer = $this->context->customer;
the following code:
$spamCheck = CleantalkValidate::spamCheckMessage('', trim(Tools::getValue('from')), $message);
if ($spamCheck->allow == 0) {
$this->context->controller->errors[] = $this->trans(
$spamCheck->comment,
[],
'Shop.Notifications.Error'
);
}
It will look something like this:
...
} else {
$spamCheck = CleantalkValidate::spamCheckMessage('', trim(Tools::getValue('from')), $message);
if ($spamCheck->allow == 0) {
$this->context->controller->errors[] = $this->trans(
$spamCheck->comment,
[],
'Shop.Notifications.Error'
);
}
$customer = $this->context->customer;
...
For old versions (older than 1.7.0.0) steps 1-3 will be the same.
4. To protect the registration form, go to <root directory>/controllers/front and open AuthController.php.
Paste the following code at the beginning (after <?php):
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/classes/cleantalk/CleantalkValidate.php';
Paste after this line (#442):
if (!count($this->errors)) {
the following code:
$spamCheckResult = CleantalkValidate::spamCheckUser($customer->firstname.' '.$customer->lastname, $customer->email);
if ($spamCheckResult->allow == 0)
{
$this->errors[] = Tools::displayError('User forbidden. Reason: '.$spamCheckResult->comment);
}
5. To protect comments, go to <root directory>/modules/productcomments/controllers/front/ and open default.php.
Paste the following code at the beginning (after <?php):
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/classes/cleantalk/CleantalkValidate.php';
Paste the following code:
$spamCheckResult = CleantalkValidate::spamCheckUser($customer->firstname.' '.$customer->lastname, $customer->email);
if ($spamCheckResult->allow == 0)
{
$this->errors[] = Tools::displayError('User forbidden. Reason: '.$spamCheckResult->comment);
}
After this line (#70):
$errors = array();
Notice: to protect comments, you need to have this module installed in your prestashop directory.
6. To protect newsletter sign-ups, go to <root directory>/modules/blocknewsletter/ and open blocknewsletter.php.
Paste the following code at the beginning (after <?php):
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/classes/cleantalk/CleantalkValidate.php';
Paste the following code at the top of the newsletterRegistration() function:
$spamCheckResult = CleantalkValidate::spamCheckUser($customer->firstname.' '.$customer->lastname, $customer->email);
if ($spamCheckResult->allow == 0)
{
$this->errors[] = Tools::displayError('User forbidden. Reason: '.$spamCheckResult->comment);
}
It would also be interesting
- How the CleanTalk Anti-Spam API Works. API Main HelpAPI Main Help Method "check_newuser" Method "check_message" Send_feedback Built-In...
- Spam Check API - API Method spam_checkAPI Method "spam_check" Common description Method's get parameters Response Explanation Multiple...
- CleanTalk API methodsCleanTalk APIs CleanTalk provides API services that enable you to integrate CleanTalk Anti-Spam functionality...