local/modules/ReCaptcha/Action/ReCaptchaAction.php line 21

  1. <?php
  2. namespace ReCaptcha\Action;
  3. use ReCaptcha\Event\ReCaptchaCheckEvent;
  4. use ReCaptcha\Event\ReCaptchaEvents;
  5. use ReCaptcha\ReCaptcha;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. class ReCaptchaAction implements EventSubscriberInterface
  9. {
  10.     /** @var  Request */
  11.     protected $request;
  12.     public function __construct(Request $request)
  13.     {
  14.         $this->request $request;
  15.     }
  16.     public function checkCaptcha(ReCaptchaCheckEvent $event)
  17.     {
  18.         $requestUrl "https://www.google.com/recaptcha/api/siteverify";
  19.         $secretKey ReCaptcha::getConfigValue('secret_key');
  20.         $requestUrl .= "?secret=$secretKey";
  21.         $captchaResponse $event->getCaptchaResponse();
  22.         if (null === $captchaResponse) {
  23.             $captchaResponse $this->request->request->get('g-recaptcha-response');
  24.         }
  25.         $requestUrl .= "&response=$captchaResponse";
  26.         $remoteIp $event->getRemoteIp();
  27.         if (null === $remoteIp) {
  28.             $remoteIp $this->request->server->get('REMOTE_ADDR');
  29.         }
  30.         $requestUrl .= "&remoteip=$remoteIp";
  31.         $result json_decode(file_get_contents($requestUrl), true);
  32.         if ((bool) $result['success'] === true) {
  33.             $event->setHuman(true);
  34.         }
  35.     }
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.             ReCaptchaEvents::CHECK_CAPTCHA_EVENT => ['checkCaptcha'128],
  40.         ];
  41.     }
  42. }