diff --git a/.gitignore b/.gitignore index 5a574c9ac..ff0d40c77 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,4 @@ node_modules ### friendsofphp/php-cs-fixer ### .php-cs-fixer.cache src/install/php_error.log -src/themes/huraga/config/settings_data.json +src/themes/huraga/config/settings_data.json \ No newline at end of file diff --git a/cspell.json b/cspell.json index e224a5509..631a3d7cf 100644 --- a/cspell.json +++ b/cspell.json @@ -412,7 +412,10 @@ "CSRF", "undismissed", "filp", - "phpunit" + "phpunit", + "hcaptcha", + "hCaptcha", + "LEMP" ], "ignorePaths": [ "tests-legacy/**", @@ -455,7 +458,6 @@ "src/.htaccess", "phpstan-baseline.neon", "Dockerfile", - ".ddev/**", - "LEMP" + ".ddev/**" ] } diff --git a/src/modules/Spamchecker/Api/Guest.php b/src/modules/Spamchecker/Api/Guest.php index 4e0d410c7..c2810c8ce 100644 --- a/src/modules/Spamchecker/Api/Guest.php +++ b/src/modules/Spamchecker/Api/Guest.php @@ -28,6 +28,9 @@ class Guest extends \Api_Abstract 'publickey' => $config['captcha_recaptcha_publickey'] ?? null, 'enabled' => $config['captcha_enabled'] ?? false, 'version' => $config['captcha_version'] ?? null, + 'captcha_provider' => $config['captcha_provider'] ?? 'recaptcha_v2', + 'turnstile_site_key' => $config['turnstile_site_key'] ?? null, + 'hcaptcha_site_key' => $config['hcaptcha_site_key'] ?? null, ]; } } diff --git a/src/modules/Spamchecker/Service.php b/src/modules/Spamchecker/Service.php index dd792bff1..efa9a4418 100644 --- a/src/modules/Spamchecker/Service.php +++ b/src/modules/Spamchecker/Service.php @@ -87,7 +87,9 @@ class Service implements InjectionAwareInterface $config = $di['mod_config']('Spamchecker'); if (isset($config['captcha_enabled']) && $config['captcha_enabled']) { - if (isset($config['captcha_version']) && $config['captcha_version'] == 2) { + $provider = $config['captcha_provider'] ?? 'recaptcha_v2'; + + if ($provider === 'recaptcha_v2') { if (!isset($config['captcha_recaptcha_privatekey']) || $config['captcha_recaptcha_privatekey'] == '') { throw new \FOSSBilling\InformationException("To use reCAPTCHA you must get an API key from here"); } @@ -106,14 +108,58 @@ class Service implements InjectionAwareInterface ]); $content = $response->toArray(); - if (!$content['success']) { + if (!isset($content['success']) || $content['success'] !== true) { throw new \FOSSBilling\InformationException('reCAPTCHA verification failed.'); } - } else { - throw new \FOSSBilling\InformationException('reCAPTCHA verification failed.'); + + } elseif ($provider === 'turnstile') { + if (empty($config['turnstile_secret_key'])) { + throw new \FOSSBilling\InformationException('Cloudflare Turnstile secret key is not configured.'); + } + + $turnstile_response = $params['cf-turnstile-response'] ?? null; + if (empty($turnstile_response)) { + throw new \FOSSBilling\InformationException('Please complete the CAPTCHA verification.'); + } + + $client = HttpClient::create(['bindto' => BIND_TO]); + $response = $client->request('POST', 'https://challenges.cloudflare.com/turnstile/v0/siteverify', [ + 'body' => [ + 'secret' => $config['turnstile_secret_key'], + 'response' => $turnstile_response, + 'remoteip' => $di['request']->getClientIp(), + ], + ]); + $content = $response->toArray(); + + if (!isset($content['success']) || $content['success'] !== true) { + throw new \FOSSBilling\InformationException('CAPTCHA verification failed. Please try again.'); + } + } elseif ($provider === 'hcaptcha') { + if (empty($config['hcaptcha_secret_key'])) { + throw new \FOSSBilling\InformationException('hCaptcha secret key is not configured.'); + } + + $hcaptcha_response = $params['h-captcha-response'] ?? null; + if (empty($hcaptcha_response)) { + throw new \FOSSBilling\InformationException('Please complete the CAPTCHA verification.'); + } + + $client = HttpClient::create(['bindto' => BIND_TO]); + $response = $client->request('POST', 'https://hcaptcha.com/siteverify', [ + 'body' => [ + 'secret' => $config['hcaptcha_secret_key'], + 'response' => $hcaptcha_response, + 'remoteip' => $di['request']->getClientIp(), + ], + ]); + $content = $response->toArray(); + + if (!isset($content['success']) || $content['success'] !== true) { + throw new \FOSSBilling\InformationException('CAPTCHA verification failed. Please try again.'); + } } } - if (isset($config['sfs']) && $config['sfs']) { $spamCheckerService = $di['mod_service']('Spamchecker'); $spamCheckerService->isInStopForumSpamDatabase($data); diff --git a/src/modules/Spamchecker/html_admin/mod_spamchecker_settings.html.twig b/src/modules/Spamchecker/html_admin/mod_spamchecker_settings.html.twig index ecdca878d..b6a6cbca6 100644 --- a/src/modules/Spamchecker/html_admin/mod_spamchecker_settings.html.twig +++ b/src/modules/Spamchecker/html_admin/mod_spamchecker_settings.html.twig @@ -51,14 +51,13 @@
-

{{ 'reCAPTCHA settings'|trans }}

+

{{ 'CAPTCHA settings'|trans }}

{{ 'Enable captcha for client registration and contact forms'|trans }}
- {{ 'Sign up for reCAPTCHA key for your domain at'|trans }} - Google reCAPTCHA + {{ 'Get keys from %recaptcha%, %turnstile% or %hcaptcha%.'|trans({ '%recaptcha%': 'Google reCAPTCHA', '%turnstile%': 'Cloudflare Turnstile', '%hcaptcha%': 'hCaptcha' })|raw }}

- +
+
- -
- -{# #} -
-
-
- +
- +
-
- -
- + +
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+
+ +
+ +
+
+
+ +
+ +
@@ -132,3 +160,35 @@
{% endblock %} + +{% block js %} + +{% endblock %} diff --git a/src/modules/Spamchecker/html_client/mod_spamchecker_recaptcha.html.twig b/src/modules/Spamchecker/html_client/mod_spamchecker_recaptcha.html.twig index b818ead69..68d921972 100644 --- a/src/modules/Spamchecker/html_client/mod_spamchecker_recaptcha.html.twig +++ b/src/modules/Spamchecker/html_client/mod_spamchecker_recaptcha.html.twig @@ -1,9 +1,21 @@ {% if guest.extension_is_on({"mod":"spamchecker"}) %} {% set rc = guest.spamchecker_recaptcha %} {% if rc.enabled %} - {% if rc.version == 2 %} - -
- {% endif %} + {% set provider = rc.captcha_provider|default('recaptcha_v2') %} + {% if provider == 'turnstile' %} + {% if rc.turnstile_site_key %} + +
+ {% endif %} + {% elseif provider == 'recaptcha_v2' %} + {% if rc.publickey %} + +
+ {% endif %} + {% elseif provider == 'hcaptcha' %} + {% if rc.hcaptcha_site_key %} + +
+ {% endif %} {% endif %} {% endif %} \ No newline at end of file diff --git a/src/themes/huraga/html/macro_functions.html.twig b/src/themes/huraga/html/macro_functions.html.twig index d8d37d718..c849e4eca 100644 --- a/src/themes/huraga/html/macro_functions.html.twig +++ b/src/themes/huraga/html/macro_functions.html.twig @@ -67,15 +67,36 @@ {% macro recaptcha() %} -{% if guest.extension_is_on({"mod":"spamchecker"}) %} -{% set rc = guest.spamchecker_recaptcha %} - {% if rc.enabled %} - {% if rc.version == 2 %} - -
+ {% if guest.extension_is_on({"mod":"spamchecker"}) %} + + {% set config = guest.spamchecker_recaptcha %} + {% if config.enabled %} + {% set provider = config.captcha_provider|default('recaptcha_v2') %} + {% if provider == 'turnstile' %} + {% if config.turnstile_site_key %} +
+ +
+
+ {% endif %} + + {% elseif provider == 'recaptcha_v2' %} + {% if config.publickey %} +
+ +
+
+ {% endif %} + {% elseif provider == 'hcaptcha' %} + {% if config.hcaptcha_site_key %} +
+ +
+
+ {% endif %} + {% endif %} {% endif %} {% endif %} -{% endif %} {% endmacro %} {% macro wysiwyg(selector) %} diff --git a/tests-legacy/modules/Spamchecker/Api/GuestTest.php b/tests-legacy/modules/Spamchecker/Api/GuestTest.php index 6d98dd41d..d7ba5e324 100644 --- a/tests-legacy/modules/Spamchecker/Api/GuestTest.php +++ b/tests-legacy/modules/Spamchecker/Api/GuestTest.php @@ -34,6 +34,9 @@ class GuestTest extends \BBTestCase 'publickey' => 1234, 'enabled' => true, 'version' => null, + 'captcha_provider'=> 'recaptcha_v2', + 'turnstile_site_key'=> null, + 'hcaptcha_site_key' => null, ], ], [ @@ -44,6 +47,9 @@ class GuestTest extends \BBTestCase 'publickey' => null, 'enabled' => true, 'version' => null, + 'captcha_provider'=> 'recaptcha_v2', + 'turnstile_site_key'=> null, + 'hcaptcha_site_key'=> null, ], ], [ @@ -56,6 +62,9 @@ class GuestTest extends \BBTestCase 'publickey' => 1234, 'enabled' => false, 'version' => 2, + 'captcha_provider'=> 'recaptcha_v2', + 'turnstile_site_key'=> null, + 'hcaptcha_site_key' => null, ], ], [ @@ -66,6 +75,39 @@ class GuestTest extends \BBTestCase 'publickey' => null, 'enabled' => false, 'version' => null, + 'captcha_provider'=> 'recaptcha_v2', + 'turnstile_site_key'=> null, + 'hcaptcha_site_key' => null, + ], + ], + [ + [ + 'captcha_enabled' => true, + 'captcha_provider' => 'turnstile', + 'turnstile_site_key' => 'abc', + ], + [ + 'publickey' => null, + 'enabled' => true, + 'version' => null, + 'captcha_provider' => 'turnstile', + 'turnstile_site_key'=> 'abc', + 'hcaptcha_site_key' => null, + ] + ], + [ + [ + 'captcha_enabled' => true, + 'captcha_provider' => 'hcaptcha', + 'hcaptcha_site_key' => 'abc', + ], + [ + 'publickey' => null, + 'enabled' => true, + 'version' => null, + 'captcha_provider' => 'hcaptcha', + 'turnstile_site_key' => null, + 'hcaptcha_site_key' => 'abc', ], ], ];