Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom reCAPTCHA providers #515

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
8 changes: 5 additions & 3 deletions form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -415,20 +416,21 @@ 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'];
$action = $form->value('action');
$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
Expand Down
4 changes: 4 additions & 0 deletions languages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions templates/forms/fields/captcha/captcha.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -12,8 +13,7 @@
{% if not site_key %}
<script type="application/javascript">console && console.error('site_key was not defined for form "{{ form.name }}" (Grav Form Plugin)')</script>
{% elseif config.plugins.form.recaptcha.version == 3 %}
{% do assets.addJs('https://www.google.com/recaptcha/api.js?render='~site_key~'&theme=' ~ theme) %}
{#<script src='https://www.google.com/recaptcha/api.js?render={{ site_key }}&theme={{ theme }}'></script>#}
{% do assets.addJs(js_url ~ '?render=' ~ site_key ~ '&theme=' ~ theme) %}
<script type="application/javascript">
window.gRecaptchaInstances = window.gRecaptchaInstances || {};
window.gRecaptchaInstances['{{ form.id }}'] = {
Expand Down Expand Up @@ -76,7 +76,7 @@
}
</script>

<script src="https://www.google.com/recaptcha/api.js?onload=captchaOnloadCallback_{{ formName }}&hl={{ grav.language.language }}&theme={{ theme }}"
<script src="{{ js_url }}?onload=captchaOnloadCallback_{{ formName }}&hl={{ grav.language.language }}&theme={{ theme }}"
async defer></script>
{% else %}
<script type="application/javascript">
Expand All @@ -93,7 +93,7 @@
grecaptcha.reset();
};
</script>
<script src="https://www.google.com/recaptcha/api.js?onload=captchaOnloadCallback_{{ formName }}&render=explicit&hl={{ grav.language.language }}&theme={{ theme }} "
<script src="{{ js_url }}?onload=captchaOnloadCallback_{{ formName }}&render=explicit&hl={{ grav.language.language }}&theme={{ theme }}"
async defer></script>
<div class="g-recaptcha" id="g-recaptcha-{{ formName }}"></div>
{% endif %}
Expand Down