Commit 7a51af16 authored by Tino Goratsch's avatar Tino Goratsch

save the current work in progress at implementing support for the invisible reCAPTCHA

parent 96443415
......@@ -59,17 +59,28 @@ class RenderRecaptchaOnLayoutContentBeforeListener
'sitekey' => $settings['recaptcha_sitekey'],
'lang' => $this->translator->getShortIsoCode()
]);
$this->view->displayTemplate('Captcha/Partials/captcha_recaptcha.onload.tpl');
$this->view->displayTemplate($this->getServiceIdTemplateMap()[$settings['captcha']]);
}
}
/**
* @return array
*/
private function getServiceIdTemplateMap()
{
return [
'captcha.extension.recaptcha_captcha_extension' => 'Captcha/Partials/captcha_recaptcha.onload.tpl',
'captcha.extension.nocaptcha_captcha_extension' => 'Captcha/Partials/captcha_nocaptcha.onload.tpl'
];
}
/**
* @param array $settings
* @return bool
*/
private function isRecaptcha(array $settings)
{
return $settings['captcha'] === 'captcha.extension.recaptcha_captcha_extension'
return array_key_exists($settings['captcha'], $this->getServiceIdTemplateMap())
&& !empty($settings['recaptcha_sitekey'])
&& !empty($settings['recaptcha_secret']);
}
......
<?php
/**
* Copyright (c) 2017 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Captcha\Extension;
use ACP3\Core\Http\RequestInterface;
use ACP3\Core\I18n\Translator;
use ACP3\Core\Settings\SettingsInterface;
use ACP3\Core\View;
use ACP3\Modules\ACP3\Users\Model\UserModel;
class NoCaptchaCaptchaExtension extends ReCaptchaCaptchaExtension
{
const TEMPLATE = 'Captcha/Partials/captcha_nocaptcha.tpl';
/**
* @var Translator
*/
private $translator;
/**
* NoCaptchaCaptchaExtension constructor.
* @param Translator $translator
* @param RequestInterface $request
* @param SettingsInterface $settings
* @param View $view
* @param UserModel $user
*/
public function __construct(
Translator $translator,
RequestInterface $request,
SettingsInterface $settings,
View $view,
UserModel $user
) {
parent::__construct($translator, $request, $settings, $view, $user);
$this->translator = $translator;
}
/**
* @inheritdoc
*/
public function getCaptchaName()
{
return $this->translator->t('captcha', 'recaptcha_invisible');
}
}
......@@ -16,6 +16,7 @@ use ReCaptcha\ReCaptcha;
class ReCaptchaCaptchaExtension implements CaptchaExtensionInterface
{
const TEMPLATE = 'Captcha/Partials/captcha_recaptcha.tpl';
/**
* @var Translator
*/
......@@ -83,7 +84,7 @@ class ReCaptchaCaptchaExtension implements CaptchaExtensionInterface
'length' => $captchaLength
]);
return $this->view->fetchTemplate('Captcha/Partials/captcha_recaptcha.tpl');
return $this->view->fetchTemplate(static::TEMPLATE);
}
return '';
......
......@@ -4,9 +4,13 @@
*/
jQuery(document).ready(function ($) {
var $recaptchaWrapper = $('#recaptcha-wrapper');
var $recaptchaWrapper = $('#recaptcha-wrapper'),
serviceIds = [
'captcha.extension.nocaptcha_captcha_extension',
'captcha.extension.recaptcha_captcha_extension'
];
$('#captcha').on('change', function() {
$recaptchaWrapper.toggle(this.value === 'captcha.extension.recaptcha_captcha_extension');
$recaptchaWrapper.toggle(serviceIds.indexOf(this.value) !== -1);
}).filter(':selected').triggerHandler('change');
});
{javascripts}
<script type="text/javascript"
src="https://www.google.com/recaptcha/api.js?hl={$recaptcha.lang}"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$(document).on('acp3.ajaxFrom.submit.before', function(e, ajaxForm) {
jQuery('.recaptcha-placeholder').each(function() {
console.log(this.id);
var widget = grecaptcha.render(this.id, {
'sitekey': '{$recaptcha.sitekey}',
'size': this.dataset.size,
'callback': function() {
ajaxForm.isFormValid = true;
}
});
grecaptcha.execute(widget);
});
});
});
</script>
{/javascripts}
{extends file="asset:System/Partials/form_group.base.tpl"}
{$required=true}
{if $captcha.input_only}
{$cssSelector=''}
{/if}
{block FORM_GROUP_FORM_FIELD}
<div id="{$captcha.id}"
class="recaptcha-placeholder"
data-size="invisible">
</div>
<div class="recaptcha-placeholder"></div>
{/block}
......@@ -5,7 +5,7 @@
{$cssSelector=''}
{/if}
{block FORM_GROUP_LABEL_ID}for="{$captcha.id}"{/block}
{block FORM_GROUP_FORM_FIELD}
<div id="{$captcha.id}" class="recaptcha-placeholder"{if $captcha.length < 5} data-size="compact"{/if}></div>
<div id="{$captcha.id}"
class="recaptcha-placeholder"{if $captcha.length < 5} data-size="compact"{/if}></div>
{/block}
......@@ -23,6 +23,12 @@ services:
tags:
- { name: 'captcha.extension.captcha' }
captcha.extension.nocaptcha_captcha_extension:
class: ACP3\Modules\ACP3\Captcha\Extension\NoCaptchaCaptchaExtension
parent: captcha.extension.recaptcha_captcha_extension
tags:
- { name: 'captcha.extension.captcha' }
captcha.extension.recaptcha_captcha_extension:
class: ACP3\Modules\ACP3\Captcha\Extension\ReCaptchaCaptchaExtension
arguments:
......
......@@ -12,6 +12,7 @@
<item key="mod_description">Captchas bieten wirkungsvollen Schutz gegen Spambots.</item>
<item key="native">ACP3-Nativ</item>
<item key="recaptcha">reCAPTCHA</item>
<item key="recaptcha_invisible">reCAPTCHA (Unsichtbar)</item>
<item key="recaptcha_secret">reCAPTCHA Secret</item>
<item key="recaptcha_sitekey">reCAPTCHA Sitekey</item>
<item key="select_captcha_type">Bitte wählen Sie den zu verwendenten Captcha-Typen aus.</item>
......
......@@ -12,6 +12,11 @@
<item key="mod_description">Captchas provide effective protection against spambots.</item>
<item key="native">ACP3 native</item>
<item key="recaptcha">reCAPTCHA</item>
<item key="recaptcha_invisible">Invisible reCAPTCHA</item>
<item key="recaptcha_secret">reCAPTCHA secret</item>
<item key="recaptcha_sitekey">reCAPTCHA sitekey</item>
<item key="select_captcha_type">Please select the captcha type which should be used.</item>
<item key="type_in_recaptcha_secret">Please type in the reCAPTCHA secret.</item>
<item key="type_in_recaptcha_sitekey">Please type in the reCAPTCHA sitekey.</item>
</keys>
</language>
......@@ -14,6 +14,7 @@
function Plugin(element, options) {
this.element = element;
this.isFormValid = true;
this.settings = $.extend({}, defaults, options);
this._defaults = defaults;
......@@ -31,9 +32,9 @@
$(this.element).on('submit', function (e) {
e.preventDefault();
$(document).trigger('acp3.ajaxFrom.submit.before');
$(document).trigger('acp3.ajaxFrom.submit.before', [ that ]);
if (that.preValidateForm(that.element)) {
if (that.isFormValid && that.preValidateForm(that.element)) {
that.processAjaxRequest();
}
}).on('click', function (e) {
......@@ -51,8 +52,7 @@
});
},
preValidateForm: function (form) {
var field,
isValid = true;
var field;
this.removeAllPreviousErrors();
......@@ -67,13 +67,13 @@
this.addErrorDecorationToFormGroup($(field));
this.addErrorMessageToFormField($(field), field.validationMessage);
isValid = false;
this.isFormValid = false;
}
}
this.focusTabWithFirstErrorMessage();
return isValid;
return this.isFormValid;
},
removeAllPreviousErrors: function () {
$('form .form-group.has-error')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment