[04/03/2023] Ajout du module ReCaptcha et config

This commit is contained in:
2023-03-04 21:47:58 +01:00
parent 437bd2bf4f
commit 533e3439c6
24 changed files with 886 additions and 15 deletions

View File

@@ -0,0 +1,74 @@
<?php
namespace ReCaptcha\Form;
use ReCaptcha\ReCaptcha;
use Thelia\Core\Translation\Translator;
use Thelia\Form\BaseForm;
class ConfigurationForm extends BaseForm
{
protected function buildForm()
{
$this->formBuilder
->add(
"site_key",
"text",
[
"data" => ReCaptcha::getConfigValue("site_key"),
"label"=>Translator::getInstance()->trans("Site key", array(), ReCaptcha::DOMAIN_NAME),
"label_attr" => ["for" => "site_key"],
"required" => true
]
)
->add(
"secret_key",
"text",
[
"data" => ReCaptcha::getConfigValue("secret_key"),
"label"=>Translator::getInstance()->trans("Secret key", array(), ReCaptcha::DOMAIN_NAME),
"label_attr" => ["for" => "secret_key"],
"required" => true
]
)
->add(
"captcha_style",
"choice",
[
"data" => ReCaptcha::getConfigValue("captcha_style"),
"label"=>Translator::getInstance()->trans("ReCaptcha style", array(), ReCaptcha::DOMAIN_NAME),
"label_attr" => ["for" => "captcha_style"],
"required" => true,
'choices' => [
'normal'=>'Normal',
'compact'=>'Compact',
'invisible'=>'Invisible'
]
]
);
if (defined('\Thelia\Core\Event\TheliaEvents::CONTACT_SUBMIT')) {
$this->formBuilder
->add(
"add_to_contact_form",
"checkbox",
[
"required" => false,
"data" => (bool) ReCaptcha::getConfigValue("add_to_contact_form"),
"value" => 1,
"label" => $this->translator->trans("Add captcha to standard contact form", [], ReCaptcha::DOMAIN_NAME),
"label_attr" => [
"for" => "add_to_contact_form",
'help' => $this->translator->trans("Check this box to add a captcha to the standard Thelia 2 contact form", [], ReCaptcha::DOMAIN_NAME)
],
]
);
}
}
public function getName()
{
return "recaptcha_configuration_form";
}
}

View File

@@ -0,0 +1,34 @@
<?php namespace ReCaptcha\Form;
use ReCaptcha\Event\ReCaptchaCheckEvent;
use ReCaptcha\Event\ReCaptchaEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Thelia\Form\BaseForm;
use Thelia\Form\Exception\FormValidationException;
class MyTheliaFormValidator extends \Thelia\Core\Form\TheliaFormValidator
{
/** @var EventDispatcherInterface */
protected $dispatcher;
public function __construct(TranslatorInterface $translator, $environment, EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
parent::__construct($translator, $environment);
}
public function validateForm(BaseForm $aBaseForm, $expectedMethod = null)
{
if ($aBaseForm->getRequest()->get('captcha')) {
$checkCaptchaEvent = new ReCaptchaCheckEvent();
$this->dispatcher->dispatch(ReCaptchaEvents::CHECK_CAPTCHA_EVENT, $checkCaptchaEvent);
if ($checkCaptchaEvent->isHuman() == false) {
throw new FormValidationException('Veuillez confirmer que vous n\'êtes pas un robot.');
}
}
return parent::validateForm($aBaseForm, $expectedMethod); // TODO: Change the autogenerated stub
}
}