[04/03/2023] Ajout du module ReCaptcha et config
This commit is contained in:
55
local/modules/ReCaptcha/Action/ReCaptchaAction.php
Normal file
55
local/modules/ReCaptcha/Action/ReCaptchaAction.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace ReCaptcha\Action;
|
||||
|
||||
use ReCaptcha\Event\ReCaptchaCheckEvent;
|
||||
use ReCaptcha\Event\ReCaptchaEvents;
|
||||
use ReCaptcha\ReCaptcha;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class ReCaptchaAction implements EventSubscriberInterface
|
||||
{
|
||||
/** @var Request */
|
||||
protected $request;
|
||||
|
||||
public function __construct(Request $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
public function checkCaptcha(ReCaptchaCheckEvent $event)
|
||||
{
|
||||
$requestUrl = "https://www.google.com/recaptcha/api/siteverify";
|
||||
|
||||
$secretKey = ReCaptcha::getConfigValue('secret_key');
|
||||
$requestUrl .= "?secret=$secretKey";
|
||||
|
||||
$captchaResponse = $event->getCaptchaResponse();
|
||||
if (null === $captchaResponse) {
|
||||
$captchaResponse = $this->request->request->get('g-recaptcha-response');
|
||||
}
|
||||
|
||||
$requestUrl .= "&response=$captchaResponse";
|
||||
|
||||
$remoteIp = $event->getRemoteIp();
|
||||
if (null === $remoteIp) {
|
||||
$remoteIp = $this->request->server->get('REMOTE_ADDR');
|
||||
}
|
||||
|
||||
$requestUrl .= "&remoteip=$remoteIp";
|
||||
|
||||
$result = json_decode(file_get_contents($requestUrl), true);
|
||||
|
||||
if ((bool) $result['success'] === true) {
|
||||
$event->setHuman(true);
|
||||
}
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
ReCaptchaEvents::CHECK_CAPTCHA_EVENT => ['checkCaptcha', 128],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user