From a16f2bf8a7295a41f8e69a61789f5168abf2b01c Mon Sep 17 00:00:00 2001 From: z38 Date: Tue, 30 Mar 2021 13:32:31 +0200 Subject: [PATCH] Support custom reCAPTCHA providers --- blueprints.yaml | 9 ++++++++- form.php | 8 +++++--- languages.yaml | 4 ++++ templates/forms/fields/captcha/captcha.html.twig | 8 ++++---- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/blueprints.yaml b/blueprints.yaml index 66b8ab07..d665729f 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -185,7 +185,6 @@ form: options: light: PLUGIN_FORM.RECAPTCHA_THEME_LIGHT dark: PLUGIN_FORM.RECAPTCHA_THEME_DARK - recaptcha.site_key: recaptcha.site_key: type: text label: PLUGIN_FORM.RECAPTCHA_SITE_KEY @@ -196,3 +195,11 @@ form: label: PLUGIN_FORM.RECAPTCHA_SECRET_KEY help: PLUGIN_FORM.RECAPTCHA_SECRET_KEY_HELP default: '' + recaptcha.js_url: + type: text + label: PLUGIN_FORM.RECAPTCHA_JS_URL + help: PLUGIN_FORM.RECAPTCHA_JS_URL_HELP + recaptcha.verify_url: + type: text + label: PLUGIN_FORM.RECAPTCHA_VERIFY_URL + help: PLUGIN_FORM.RECAPTCHA_VERIFY_URL_HELP diff --git a/form.php b/form.php index 6fad1d53..2609ea78 100644 --- a/form.php +++ b/form.php @@ -23,6 +23,7 @@ use Grav\Plugin\Form\Forms; use ReCaptcha\ReCaptcha; use ReCaptcha\RequestMethod\CurlPost; +use ReCaptcha\RequestMethod\Post; use RecursiveArrayIterator; use RecursiveIteratorIterator; use RocketTheme\Toolbox\File\JsonFile; @@ -415,10 +416,10 @@ public function onFormProcessed(Event $event): void switch ($action) { case 'captcha': - $captcha_config = $this->config->get('plugins.form.recaptcha'); $secret = $params['recaptcha_secret'] ?? $params['recatpcha_secret'] ?? $captcha_config['secret_key']; + $verify_url = $captcha_config['verify_url'] ?? ReCaptcha::SITE_VERIFY_URL; /** @var Uri $uri */ $uri = $this->grav['uri']; @@ -426,9 +427,10 @@ public function onFormProcessed(Event $event): void $hostname = $uri->host(); $ip = Uri::ip(); - $recaptcha = new ReCaptcha($secret); if (extension_loaded('curl')) { - $recaptcha = new ReCaptcha($secret, new CurlPost()); + $recaptcha = new ReCaptcha($secret, new CurlPost(null, $verify_url)); + } else { + $recaptcha = new ReCaptcha($secret, new Post($verify_url)); } // get captcha version diff --git a/languages.yaml b/languages.yaml index 4b2f06d5..f5244162 100644 --- a/languages.yaml +++ b/languages.yaml @@ -25,6 +25,10 @@ en: RECAPTCHA_SITE_KEY_HELP: "For more info visit https://developers.google.com/recaptcha" RECAPTCHA_SECRET_KEY: "Secret key" RECAPTCHA_SECRET_KEY_HELP: "For more info visit https://developers.google.com/recaptcha" + RECAPTCHA_JS_URL: "JavaScript URL" + RECAPTCHA_JS_URL_HELP: "The script used to display CAPTCHAs" + RECAPTCHA_VERIFY_URL: "Verify URL" + RECAPTCHA_VERIFY_URL_HELP: "The endpoint used to verify submissions" GENERAL: "General" USE_BUILT_IN_CSS: "Use built-in CSS" USE_INLINE_CSS: "Use inline CSS" diff --git a/templates/forms/fields/captcha/captcha.html.twig b/templates/forms/fields/captcha/captcha.html.twig index 07ce494a..aa2d1dcc 100644 --- a/templates/forms/fields/captcha/captcha.html.twig +++ b/templates/forms/fields/captcha/captcha.html.twig @@ -4,6 +4,7 @@ {% set site_key = field.recaptcha_site_key and field.recaptcha_site_key != 'ENTER_YOUR_CAPTCHA_SITE_KEY' ? field.recaptcha_site_key : config.plugins.form.recaptcha.site_key %} {% set action = (page.route|trim('/') ~ '-' ~ form.name)|underscorize %} {% set formName = form.name|underscorize %} +{% set js_url = config.plugins.form.recaptcha.js_url ?? 'https://www.google.com/recaptcha/api.js' %} {% set theme = config.plugins.form.recaptcha.theme ?? 'light' %} {% block label %}{% endblock %} @@ -12,8 +13,7 @@ {% if not site_key %} {% elseif config.plugins.form.recaptcha.version == 3 %} - {% do assets.addJs('https://www.google.com/recaptcha/api.js?render='~site_key~'&theme=' ~ theme) %} - {##} + {% do assets.addJs(js_url ~ '?render=' ~ site_key ~ '&theme=' ~ theme) %} - {% else %} -
{% endif %}