Skip to content

Commit b48b6ff

Browse files
committed
#275 / #308 Add a configuration to recaptcha V3 score
1 parent 16b9e0e commit b48b6ff

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed

changelog.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/**
22
* Module Captcha
33
* Add (re)captcha on contact and account creation forms
4-
* © Hervé Hennes 2013-2024
4+
* © Hervé Hennes 2013-2025
55
* https://github.com/nenes25/eicaptcha
66
* https://www.h-hennes.fr/blog/
77
*/
8-
- V 2.5.1 - 2024- #288 Fix php7.0 error
8+
9+
- V 2.6.0 - 2025-02-24 #288 Fix php7.0 error
10+
#275 / #308 Configure reCaptcha V3 min score
911
- V 2.5.0 - 2024-02-15 #229 Replace override of Authcontroller
1012
#247 Registration check on prestashop 8.x
1113
#221 recaptcha v3 doesn't validate in form added with elementor form builder of warehouse theme

eicaptcha.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __construct()
5353
$this->author = 'hhennes';
5454
$this->name = 'eicaptcha';
5555
$this->tab = 'front_office_features';
56-
$this->version = '2.5.0';
56+
$this->version = '2.6.0';
5757
$this->need_instance = 1;
5858

5959
$this->bootstrap = true;
@@ -447,13 +447,18 @@ protected function _validateCaptcha()
447447
}
448448

449449
$context = Context::getContext();
450+
$captchaVersion = Configuration::get('CAPTCHA_VERSION');
451+
$captchaV3MinScore = Configuration::get('CAPTCHA_V3_MINIMAL_SCORE');
450452
//Fix issue if allow_url_open is set to 0
451453
if (function_exists('ini_get') && !ini_get('allow_url_fopen')) {
452454
$recaptchaMethod = new \ReCaptcha\RequestMethod\CurlPost();
453455
} else {
454456
$recaptchaMethod = null;
455457
}
456458
$captcha = new ReCaptcha(Configuration::get('CAPTCHA_PRIVATE_KEY'), $recaptchaMethod);
459+
if ($captchaVersion == 3) {
460+
$captcha->setScoreThreshold($captchaV3MinScore);
461+
}
457462
$result = $captcha->verify(
458463
Tools::getValue('g-recaptcha-response'),
459464
Tools::getRemoteAddr()
@@ -463,6 +468,17 @@ protected function _validateCaptcha()
463468
$errorMessage = $this->l('Please validate the captcha field before submitting your request');
464469
$this->debugger->log($errorMessage);
465470
$this->debugger->log(sprintf($this->l('Recaptcha response %s'), print_r($result->getErrorCodes(), true)));
471+
if ($captchaVersion == 3) {
472+
if ($result->getScore() < $captchaV3MinScore) {
473+
$errorMessageV3 =
474+
sprintf(
475+
$this->l('Your request has been blocked by the captcha system, due to a low score of %s , required score is %s'),
476+
$result->getScore(),
477+
$captchaV3MinScore
478+
);
479+
$this->debugger->log($errorMessageV3);
480+
}
481+
}
466482
$context->controller->errors[] = $errorMessage;
467483

468484
return false;

src/ConfigForm.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ public function renderForm()
9090
],
9191
'tab' => 'general',
9292
],
93+
[
94+
'type' => 'text',
95+
'label' => $this->l('Captcha V3 mininum score'),
96+
'hint' => sprintf(
97+
$this->l('The minimum score required to validate the captcha is a note between 0 and 1. Default is 0.5 , you can read more about it here: %s'),
98+
'<a href="https://developers.google.com/recaptcha/docs/v3?#interpreting_the_score" target="_blank">https://developers.google.com/recaptcha/docs/v3?#interpreting_the_score</a>'
99+
),
100+
'name' => 'CAPTCHA_V3_MINIMAL_SCORE',
101+
'required' => true,
102+
'empty_message' => $this->l('Please fill the captcha v3 minimal score.'),
103+
'tab' => 'general',
104+
],
93105
[
94106
'type' => 'text',
95107
'label' => $this->l('Captcha public key (Site key)'),
@@ -352,6 +364,7 @@ public function postProcess()
352364
{
353365
if (Tools::isSubmit('SubmitCaptchaConfiguration')) {
354366
Configuration::updateValue('CAPTCHA_VERSION', Tools::getValue('CAPTCHA_VERSION'));
367+
Configuration::updateValue('CAPTCHA_V3_MINIMAL_SCORE', Tools::getValue('CAPTCHA_V3_MINIMAL_SCORE'));
355368
Configuration::updateValue('CAPTCHA_PUBLIC_KEY', Tools::getValue('CAPTCHA_PUBLIC_KEY'));
356369
Configuration::updateValue('CAPTCHA_PRIVATE_KEY', Tools::getValue('CAPTCHA_PRIVATE_KEY'));
357370
Configuration::updateValue('CAPTCHA_ENABLE_LOGGED_CUSTOMERS', Tools::getValue('CAPTCHA_ENABLE_LOGGED_CUSTOMERS'));
@@ -377,6 +390,7 @@ public function getConfigFieldsValues()
377390
{
378391
return [
379392
'CAPTCHA_VERSION' => Tools::getValue('CAPTCHA_VERSION', Configuration::get('CAPTCHA_VERSION')),
393+
'CAPTCHA_V3_MINIMAL_SCORE' => Tools::getValue('CAPTCHA_V3_MINIMAL_SCORE', Configuration::get('CAPTCHA_V3_MINIMAL_SCORE')),
380394
'CAPTCHA_PRIVATE_KEY' => Tools::getValue('CAPTCHA_PRIVATE_KEY', Configuration::get('CAPTCHA_PRIVATE_KEY')),
381395
'CAPTCHA_PUBLIC_KEY' => Tools::getValue('CAPTCHA_PUBLIC_KEY', Configuration::get('CAPTCHA_PUBLIC_KEY')),
382396
'CAPTCHA_ENABLE_LOGGED_CUSTOMERS' => Tools::getValue('CAPTCHA_ENABLE_LOGGED_CUSTOMERS', Configuration::get('CAPTCHA_ENABLE_LOGGED_CUSTOMERS')),

translations/fr.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
$_MODULE['<{eicaptcha}prestashop>eicaptcha_793b58515cefe26f6a3c5ab782460a69'] = 'Le module Eicatpcha doit être configuré';
88
$_MODULE['<{eicaptcha}prestashop>eicaptcha_632ee8e447c5c09ca7577f9281cbb999'] = 'Merci de valider le captcha';
99
$_MODULE['<{eicaptcha}prestashop>eicaptcha_9ed81dbbae814f9a81cd15927ebd4795'] = 'Réponse recaptcha %s';
10+
$_MODULE['<{eicaptcha}prestashop>eicaptcha_5113573bdec931c88c64ccdd4e297026'] = 'Votre requête a été bloquée par le captcha v3 avec un score de %s, score requis de %s';
1011
$_MODULE['<{eicaptcha}prestashop>eicaptcha_e8a0559d823da2ac04358b8bf1a1dbd1'] = 'Captcha soumis avec succès';
1112
$_MODULE['<{eicaptcha}prestashop>debugger_01447f1863a21f8171b5894cc3e535ac'] = 'Ce module nécessite composer pour fonctionner, merci de vous rendre dans le dossier %s, et de lancer la commande \"composer install\" ou de télécharger la dernière release sur github';
1213
$_MODULE['<{eicaptcha}prestashop>debugger_35d70791f9c315b2bd646de3464b1686'] = 'Le module n\'est pas compatible avec votre version de Prestashop';
@@ -38,6 +39,9 @@
3839
$_MODULE['<{eicaptcha}prestashop>configform_c25456954fac1e3669128249766ef488'] = 'Version de Recaptcha';
3940
$_MODULE['<{eicaptcha}prestashop>configform_ec765f1adc3b4253f2d3b131a4a8618f'] = 'V2';
4041
$_MODULE['<{eicaptcha}prestashop>configform_d5b50b8cf96bcc8aba90f306f5e6189c'] = 'V3';
42+
$_MODULE['<{eicaptcha}prestashop>configform_0575fd3dd9df1c1eb623d805420c4ae1'] = 'Score minimum pour le captcha V3';
43+
$_MODULE['<{eicaptcha}prestashop>configform_37071d2ea59963609c35f1104d8b9e35'] = 'La note minimum pour valider le captcha est une note entre 0 et 1. Par défaut la valeur de de 0.5. Vous pouvez trouver des informations sur le sujet ici : %s';
44+
$_MODULE['<{eicaptcha}prestashop>configform_9901fa09c200314688f085cc5e358fdd'] = 'Merci de saisir le score minimum pour le captcha v3';
4145
$_MODULE['<{eicaptcha}prestashop>configform_2d6759f60f165404e274683029df8e5b'] = 'Clé de site';
4246
$_MODULE['<{eicaptcha}prestashop>configform_de7e39ae12eb7d5e08c84ff8739ee5fc'] = 'Merci de saisir la clé de site';
4347
$_MODULE['<{eicaptcha}prestashop>configform_532719b9b22c0444d8c65bdc6b80fa1e'] = 'Clé secrète';

upgrade/upgrade-2.6.0.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* NOTICE OF LICENSE
4+
*
5+
* This source file is subject to the Academic Free License (AFL 3.0)
6+
* that is bundled with this package in the file docs/licenses/LICENSE.txt.
7+
* It is also available through the world-wide-web at this URL:
8+
* https://opensource.org/licenses/afl-3.0.php
9+
* If you did not receive a copy of the license and are unable to
10+
* obtain it through the world-wide-web, please send an email
11+
* to [email protected] so we can send you a copy immediately.
12+
*
13+
* @author Hervé HENNES <[email protected]> and contributors / https://github.com/nenes25/eicaptcha
14+
* @copyright since 2013 Hervé HENNES
15+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License ("AFL") v. 3.0
16+
*/
17+
if (!defined('_PS_VERSION_')) {
18+
exit;
19+
}
20+
21+
/**
22+
* Upgrade module 2_6_0 Add a new configuration to allow to define the recaptcha v3 minimal score
23+
*
24+
* @param Module $module
25+
*
26+
* @return bool
27+
*/
28+
function upgrade_module_2_6_0($module)
29+
{
30+
Configuration::updateValue('CAPTCHA_V3_MINIMAL_SCORE', '0.5');
31+
32+
return true;
33+
}

0 commit comments

Comments
 (0)