Commit 5ceabe17 authored by Tino Goratsch's avatar Tino Goratsch

made the recaptcha work with multiple captcha per page

parent 07b9bd1c
<?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\Event\Listener;
use ACP3\Core\Settings\SettingsInterface;
use ACP3\Core\View;
use ACP3\Modules\ACP3\Captcha\Installer\Schema;
use ACP3\Modules\ACP3\Users\Model\UserModel;
class RenderRecaptchaOnLayoutContentBeforeListener
{
/**
* @var SettingsInterface
*/
private $settings;
/**
* @var View
*/
private $view;
/**
* @var UserModel
*/
private $userModel;
/**
* RenderRecaptchaOnLayoutContentBeforeListener constructor.
* @param SettingsInterface $settings
* @param View $view
* @param UserModel $userModel
*/
public function __construct(SettingsInterface $settings, View $view, UserModel $userModel)
{
$this->settings = $settings;
$this->view = $view;
$this->userModel = $userModel;
}
public function renderRecaptcha()
{
$settings = $this->settings->getSettings(Schema::MODULE_NAME);
if ($this->isRecaptcha($settings)) {
$this->view->assign('recaptcha', [
'sitekey' => $settings['recaptcha_sitekey']
]);
$this->view->displayTemplate('Captcha/Partials/captcha_recaptcha.onload.tpl');
}
}
/**
* @param array $settings
* @return bool
*/
private function isRecaptcha(array $settings)
{
return $settings['captcha'] === 'captcha.extension.recaptcha_captcha_extension'
&& !empty($settings['recaptcha_sitekey'])
&& !empty($settings['recaptcha_secret']);
}
}
......@@ -98,6 +98,10 @@ class ReCaptchaCaptchaExtension implements CaptchaExtensionInterface
*/
public function isCaptchaValid($formData, $formFieldName, array $extra = [])
{
if (empty($formData['g-recaptcha-response'])) {
return false;
}
$settings = $this->settings->getSettings(Schema::MODULE_NAME);
$recaptcha = new ReCaptcha($settings['recaptcha_secret']);
......
{javascripts}
<script type="text/javascript">
var onloadCallback = function() {
jQuery('.recaptcha-placeholder').each(function() {
grecaptcha.render(this.id, {
'sitekey': "{$recaptcha.sitekey}"
});
});
};
</script>
<script type="text/javascript"
src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&amp;render=explicit"
async
defer></script>
{/javascripts}
......@@ -9,6 +9,5 @@
{block FORM_GROUP_LABEL_ID}for="{$captcha.id}"{/block}
{block FORM_GROUP_FORM_FIELD}
<div class="g-recaptcha" data-sitekey="{$captcha.sitekey}"></div>
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?hl={$captcha.language}"></script>
<div id="{$captcha.id}" class="recaptcha-placeholder" data-sitekey="{$captcha.sitekey}"></div>
{/block}
......@@ -13,3 +13,12 @@ services:
- '@core.lang'
tags:
- { name: core.eventListener, event: captcha.validation.validate_captcha, method: validateCaptcha }
captcha.event.render_recaptcha_on_layout_content_before_listener:
class: ACP3\Modules\ACP3\Captcha\Event\Listener\RenderRecaptchaOnLayoutContentBeforeListener
arguments:
- '@core.config'
- '@core.view'
- '@users.model.user_model'
tags:
- { name: core.eventListener, event: layout.content_before, method: renderRecaptcha }
......@@ -7,7 +7,7 @@
<div class="form-group">
<input class="form-control" type="email" name="mail" maxlength="120" placeholder="{lang t="system|email_address"}" required>
</div>
{event name="captcha.event.display_captcha" length=3 input_only=true path='newsletter'}
{event name="captcha.event.display_captcha" length=3 input_only=true path='newsletter' input_id='captcha-newsletter-widget'}
<div class="form-group" style="margin: 10px 0 0">
<button type="submit" name="submit" class="btn btn-primary">{lang t="system|submit"}</button>
<input type="hidden" name="salutation" value="0">
......
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