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

Add hCaptcha support #562

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions blueprints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ form:
title: PLUGIN_FORM.RECAPTCHA

fields:
recaptcha.provider:
type: select
label: PLUGIN_FORM.RECAPTCHA_PROVIDER
help: PLUGIN_FORM.RECAPTCHA_PROVIDER_HELP
default: reCaptcha
options:
reCaptcha: reCaptcha (Google)
hCaptcha: hCaptcha (Intuition Machines)
recaptcha.version:
type: select
label: PLUGIN_FORM.RECAPTCHA_VERSION
Expand All @@ -189,10 +197,8 @@ form:
recaptcha.site_key:
type: text
label: PLUGIN_FORM.RECAPTCHA_SITE_KEY
help: PLUGIN_FORM.RECAPTCHA_SITE_KEY_HELP

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing these does not seem right to me.

default: ''
recaptcha.secret_key:
type: text
label: PLUGIN_FORM.RECAPTCHA_SECRET_KEY
help: PLUGIN_FORM.RECAPTCHA_SECRET_KEY_HELP
default: ''
60 changes: 43 additions & 17 deletions form.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,26 +465,52 @@ public function onFormProcessed(Event $event): void
$recaptcha = new ReCaptcha($secret, new CurlPost());
}

// get captcha version
$captcha_version = $captcha_config['version'] ?? 2;

// Add version 3 specific options
if ($captcha_version == 3) {
$token = $form->value('token');
$resp = $recaptcha
->setExpectedHostname($hostname)
->setExpectedAction($action)
->setScoreThreshold(0.5)
->verify($token, $ip);
$success = true;
$errors = '';

if ($this->config->get('plugins.form.recaptcha.provider','reCaptcha') == 'hCaptcha') {
// Use hCaptcha as provider: https://docs.hcaptcha.com/switch (see bottom of page for PHP code)
$data = array(
'secret' => $secret,
'response' => $_POST['h-captcha-response']
);
$verify = curl_init();
curl_setopt($verify, CURLOPT_URL, "https://hcaptcha.com/siteverify");
curl_setopt($verify, CURLOPT_POST, true);
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($verify);
$responseData = json_decode($response);
if(!$responseData->success) {
$success = false;
$errors = $responseData;
}
} else {
$token = $form->value('g-recaptcha-response', true);
$resp = $recaptcha
->setExpectedHostname($hostname)
->verify($token, $ip);
// get captcha version
$captcha_version = $captcha_config['version'] ?? 2;

// Add version 3 specific options
if ($captcha_version == 3) {
$token = $form->value('token');
$resp = $recaptcha
->setExpectedHostname($hostname)
->setExpectedAction($action)
->setScoreThreshold(0.5)
->verify($token, $ip);
} else {
$token = $form->value('g-recaptcha-response', true);
$resp = $recaptcha
->setExpectedHostname($hostname)
->verify($token, $ip);
}

if (!$resp->isSuccess()) {
$success = false;
$errors = $resp->getErrorCodes();
}
}

if (!$resp->isSuccess()) {
$errors = $resp->getErrorCodes();
if (!$success) {
$message = $this->grav['language']->translate('PLUGIN_FORM.ERROR_VALIDATING_CAPTCHA');

$fields = $form->value()->blueprints()->get('form/fields');
Expand Down
2 changes: 2 additions & 0 deletions languages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ en:
DATA_SUMMARY: "Here is the summary of what you wrote to us:"
NO_FORM_DATA: "No form data available"
RECAPTCHA: "reCAPTCHA"
RECAPTCHA_PROVIDER: "Captcha Provider"
RECAPTCHA_PROVIDER_HELP: "reCaptcha (Google): For more info visit https://developers.google.com/recaptcha\nhCaptcha (Intuition Machines): For more info visit https://docs.hcaptcha.com"
RECAPTCHA_VERSION: "Version"
RECAPTCHA_VERSION_V2_CHECKBOX: "v2 - Checkbox"
RECAPTCHA_VERSION_V2_INVISIBLE: "v2 - Invisible"
Expand Down
18 changes: 10 additions & 8 deletions templates/forms/fields/captcha/captcha.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
{% set action = (page.route|trim('/') ~ '-' ~ form.name)|underscorize %}
{% set formName = form.name|underscorize %}
{% set theme = config.plugins.form.recaptcha.theme ?? 'light' %}
{% set _captchaURL = config.plugins.form.recaptcha.provider == 'hCaptcha' ? 'https://js.hcaptcha.com/1/api.js' : 'https://www.google.com/recaptcha/api.js' %}
{% set _captchaName = config.plugins.form.recaptcha.provider == 'hCaptcha' ? 'h-captcha' : 'g-recaptcha' %}

{% block label %}{% endblock %}

{% block input %}
{% 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) %}
{% do assets.addJs(_captchaURL ~ '?render=' ~ site_key ~ '&theme=' ~ theme) %}
{#<script src='https://www.google.com/recaptcha/api.js?render={{ site_key }}&theme={{ theme }}'></script>#}
<script type="application/javascript">
window.gRecaptchaInstances = window.gRecaptchaInstances || {};
Expand Down Expand Up @@ -53,17 +55,17 @@
submits.forEach(function(submit) {
submit.addEventListener('click', function(event) {
event.preventDefault();
var captchaElement = form.querySelector('#g-recaptcha-{{ formName }}');
var captchaElement = form.querySelector('#{{ _captchaName }}-{{ formName }}');

if (captchaElement) {
captchaElement.remove();
}

captchaElement = document.createElement('div');
captchaElement.setAttribute('id', 'g-recaptcha-{{ formName }}');
captchaElement.setAttribute('id', '{{ _captchaName }}-{{ formName }}');
form.appendChild(captchaElement);

var widgetReference = grecaptcha.render('g-recaptcha-{{ formName }}', {
var widgetReference = grecaptcha.render('{{ _captchaName }}-{{ formName }}', {
sitekey: '{{ site_key }}', size: 'invisible',
callback: function(/* token */) {
form.submit();
Expand All @@ -76,12 +78,12 @@
}
</script>

<script src="https://www.google.com/recaptcha/api.js?onload=captchaOnloadCallback_{{ formName }}&hl={{ grav.language.language }}&theme={{ theme }}"
<script src="{{ _captchaURL }}?onload=captchaOnloadCallback_{{ formName }}&hl={{ grav.language.language }}&theme={{ theme }}"
async defer></script>
{% else %}
<script type="application/javascript">
var captchaOnloadCallback_{{ formName }} = function captchaOnloadCallback_{{ formName }}() {
grecaptcha.render('g-recaptcha-{{ formName }}', {
grecaptcha.render('{{ _captchaName }}-{{ formName }}', {
'sitekey': "{{ site_key }}",
'callback': captchaValidatedCallback_{{ formName }},
'expired-callback': captchaExpiredCallback_{{ formName }}
Expand All @@ -93,8 +95,8 @@
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="{{ _captchaURL }}?onload=captchaOnloadCallback_{{ formName }}&render=explicit&hl={{ grav.language.language }}&theme={{ theme }} "
async defer></script>
<div class="g-recaptcha" id="g-recaptcha-{{ formName }}" data-theme="{{ theme }}"></div>
<div class="{{ _captchaName }}" id="{{ _captchaName }}-{{ formName }}" data-theme="{{ theme }}"></div>
{% endif %}
{% endblock %}