app/Plugin/AlwaysReCaptcha42/Event.php line 162

Open in your IDE?
  1. <?php
  2. namespace Plugin\AlwaysReCaptcha42;
  3. use Eccube\Common\Constant;
  4. use Eccube\Event\TemplateEvent;
  5. use Eccube\Form\Type\Admin\LoginType;
  6. use Eccube\Form\Type\Front\CustomerLoginType;
  7. use Plugin\AlwaysReCaptcha42\Entity\Config;
  8. use Plugin\AlwaysReCaptcha42\Exception\TokenNotFoundException;
  9. use Plugin\AlwaysReCaptcha42\Exception\VerifyFailException;
  10. use Plugin\AlwaysReCaptcha42\Repository\ConfigRepository;
  11. use ReCaptcha\ReCaptcha;
  12. use ReCaptcha\RequestMethod\CurlPost;
  13. use ReCaptcha\RequestMethod\Post;
  14. use ReCaptcha\RequestMethod\SocketPost;
  15. use Symfony\Component\Form\FormFactoryInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpKernel\Event\RequestEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. use Symfony\Component\Security\Core\Security;
  20. use function Symfony\Component\Translation\t;
  21. class Event implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
  22. {
  23.     /** @var ConfigRepository */
  24.     protected $configRepository;
  25.     /** @var FormFactoryInterface */
  26.     private $formFactory;
  27.     /**
  28.      * ConfigController constructor.
  29.      *
  30.      * @param ConfigRepository $configRepository
  31.      * @param FormFactoryInterface $formFactory
  32.      */
  33.     public function __construct(
  34.         ConfigRepository $configRepository,
  35.         FormFactoryInterface $formFactory
  36.     ) {
  37.         $this->configRepository $configRepository;
  38.         $this->formFactory $formFactory;
  39.     }
  40.     /**
  41.      * @inheritDoc
  42.      */
  43.     public static function getSubscribedEvents(): array
  44.     {
  45.         return [
  46.             'Entry/index.twig' => 'entryIndexTwig',
  47.             'Entry/confirm.twig' => 'entryConfirmTwig',
  48.             'Forgot/index.twig' => 'forgotIndexTwig',
  49.             'Contact/index.twig' => 'contactIndexTwig',
  50.             'Contact/confirm.twig' => 'contactConfirmTwig',
  51.             'Mypage/login.twig' => 'mypageLoginTwig',
  52.             'Shopping/index.twig' => 'shoppingIndexTwig',
  53.             'Shopping/confirm.twig' => 'shoppingConfirmTwig',
  54.             'Shopping/login.twig' => 'shoppingLoginTwig',
  55.             'Shopping/nonmember.twig' => 'shoppingNonMemberTwig',
  56.             'OrderForm/index.twig' => 'orderFormIndexTwig',
  57.             'OrderForm/confirm.twig' => 'orderFormConfirmTwig',
  58.             KernelEvents::REQUEST => ['onKernelRequest'9],
  59.         ];
  60.     }
  61.     /**
  62.      * @param TemplateEvent $event
  63.      */
  64.     public function orderFormIndexTwig(TemplateEvent $event)
  65.     {
  66.         /** @var Config $Config */
  67.         $Config $this->configRepository->get();
  68.         if ($Config->isEnableContactOrderform() && !empty($Config->getSiteKey()) && !empty($Config->getSecretKey())) {
  69.             $this->addReCaptchaSnippet($eventtrue$Config->getSiteKey(), $Config->isHideBadge(), 'orderform_recaptcha_response''order');
  70.         }
  71.     }
  72.     /**
  73.      * @param TemplateEvent $event
  74.      */
  75.     public function orderFormConfirmTwig(TemplateEvent $event)
  76.     {
  77.         /** @var Config $Config */
  78.         $Config $this->configRepository->get();
  79.         if ($Config->isEnableContactOrderform() && !empty($Config->getSiteKey()) && !empty($Config->getSecretKey())) {
  80.             $this->addReCaptchaSnippet($eventtrue$Config->getSiteKey(), $Config->isHideBadge(), 'orderform_recaptcha_response''order');
  81.         }
  82.     }
  83.     /**
  84.      * @param TemplateEvent $event
  85.      */
  86.     public function entryIndexTwig(TemplateEvent $event)
  87.     {
  88.         /** @var Config $Config */
  89.         $Config $this->configRepository->get();
  90.         if ($Config->isEnableEntry() && !empty($Config->getSiteKey()) && !empty($Config->getSecretKey())) {
  91.             $this->addReCaptchaSnippet($eventtrue$Config->getSiteKey(), $Config->isHideBadge(), 'entry_recaptcha_response''entry');
  92.         }
  93.     }
  94.     /**
  95.      * @param TemplateEvent $event
  96.      */
  97.     public function entryConfirmTwig(TemplateEvent $event)
  98.     {
  99.         /** @var Config $Config */
  100.         $Config $this->configRepository->get();
  101.         if ($Config->isEnableEntry() && !empty($Config->getSiteKey()) && !empty($Config->getSecretKey())) {
  102.             $this->addReCaptchaSnippet($eventtrue$Config->getSiteKey(), $Config->isHideBadge(), 'entry_recaptcha_response''entry');
  103.         }
  104.     }
  105.     /**
  106.      * @param TemplateEvent $event
  107.      */
  108.     public function forgotIndexTwig(TemplateEvent $event)
  109.     {
  110.         /** @var Config $Config */
  111.         $Config $this->configRepository->get();
  112.         if ($Config->isEnableForgot() && !empty($Config->getSiteKey()) && !empty($Config->getSecretKey())) {
  113.             $this->addReCaptchaSnippet($eventfalse$Config->getSiteKey(), $Config->isHideBadge(), 'form1''forgot');
  114.         }
  115.     }
  116.     /**
  117.      * @param TemplateEvent $event
  118.      */
  119.     public function contactIndexTwig(TemplateEvent $event)
  120.     {
  121.         /** @var Config $Config */
  122.         $Config $this->configRepository->get();
  123.         if ($Config->isEnableContact() && !empty($Config->getSiteKey()) && !empty($Config->getSecretKey())) {
  124.             $this->addReCaptchaSnippet($eventtrue$Config->getSiteKey(), $Config->isHideBadge(), 'contact_recaptcha_response''contact');
  125.         }
  126.     }
  127.     /**
  128.      * @param TemplateEvent $event
  129.      */
  130.     public function contactConfirmTwig(TemplateEvent $event)
  131.     {
  132.         /** @var Config $Config */
  133.         $Config $this->configRepository->get();
  134.         if ($Config->isEnableContact() && !empty($Config->getSiteKey()) && !empty($Config->getSecretKey())) {
  135.             $this->addReCaptchaSnippet($eventtrue$Config->getSiteKey(), $Config->isHideBadge(), 'contact_recaptcha_response''contact');
  136.         }
  137.     }
  138.     /**
  139.      * @param TemplateEvent $event
  140.      */
  141.     public function mypageLoginTwig(TemplateEvent $event)
  142.     {
  143.         /** @var Config $Config */
  144.         $Config $this->configRepository->get();
  145.         if ($Config->isEnableFrontLogin() && !empty($Config->getSiteKey()) && !empty($Config->getSecretKey())) {
  146.             $this->addReCaptchaSnippet($eventfalse$Config->getSiteKey(), $Config->isHideBadge(), 'login_mypage''login');
  147.         }
  148.     }
  149.     /**
  150.      * @param TemplateEvent $event
  151.      */
  152.     public function shoppingIndexTwig(TemplateEvent $event)
  153.     {
  154.         /** @var Config $Config */
  155.         $Config $this->configRepository->get();
  156.         if ($Config->isEnableShopping() && !empty($Config->getSiteKey()) && !empty($Config->getSecretKey())) {
  157.             $this->addReCaptchaSnippet($eventfalse$Config->getSiteKey(), $Config->isHideBadge(), 'shopping-form''shopping');
  158.         }
  159.     }
  160.     /**
  161.      * @param TemplateEvent $event
  162.      */
  163.     public function shoppingConfirmTwig(TemplateEvent $event)
  164.     {
  165.         /** @var Config $Config */
  166.         $Config $this->configRepository->get();
  167.         if ($Config->isEnableShopping() && !empty($Config->getSiteKey()) && !empty($Config->getSecretKey())) {
  168.             $this->addReCaptchaSnippet($eventfalse$Config->getSiteKey(), $Config->isHideBadge(), 'shopping-form''shopping');
  169.         }
  170.     }
  171.     /**
  172.      * @param TemplateEvent $event
  173.      */
  174.     public function shoppingLoginTwig(TemplateEvent $event)
  175.     {
  176.         /** @var Config $Config */
  177.         $Config $this->configRepository->get();
  178.         if ($Config->isEnableFrontLogin() && !empty($Config->getSiteKey()) && !empty($Config->getSecretKey())) {
  179.             $this->addReCaptchaSnippet($eventfalse$Config->getSiteKey(), $Config->isHideBadge(), 'shopping_login''login');
  180.         }
  181.     }
  182.     /**
  183.      * @param TemplateEvent $event
  184.      */
  185.     public function shoppingNonMemberTwig(TemplateEvent $event)
  186.     {
  187.         /** @var Config $Config */
  188.         $Config $this->configRepository->get();
  189.         if ($Config->isEnableNonMember() && !empty($Config->getSiteKey()) && !empty($Config->getSecretKey())) {
  190.             $event->addSnippet('@AlwaysReCaptcha42/Form/non_member_snippet.twig');
  191.             $event->setParameter('recaptcha_site_key'$Config->getSiteKey());
  192.             $event->setParameter('recaptcha_action''non_member');
  193.             if ($Config->isHideBadge()) {
  194.                 $event->addAsset('@AlwaysReCaptcha42/Form/hide_badge_asset.twig');
  195.             }
  196.         }
  197.     }
  198.     /**
  199.      * @param TemplateEvent $event
  200.      * @param bool $withConfirm
  201.      * @param string $siteKey
  202.      * @param bool $hideBadge
  203.      * @param string $formId
  204.      * @param string $action
  205.      */
  206.     private function addReCaptchaSnippet(
  207.         TemplateEvent $event,
  208.         bool $withConfirm,
  209.         string $siteKey,
  210.         bool $hideBadge,
  211.         string $formId,
  212.         string $action
  213.     ) {
  214.         if ($withConfirm) {
  215.             $event->addSnippet('@AlwaysReCaptcha42/Form/with_confirm_snippet.twig');
  216.             $event->setParameter('recaptcha_input_id'$formId);
  217.         } else {
  218.             $event->addSnippet('@AlwaysReCaptcha42/Form/no_confirm_snippet.twig');
  219.             $event->setParameter('recaptcha_form_id'$formId);
  220.         }
  221.         $event->setParameter('recaptcha_site_key'$siteKey);
  222.         $event->setParameter('recaptcha_action'$action);
  223.         if ($hideBadge) {
  224.             $event->addAsset('@AlwaysReCaptcha42/Form/hide_badge_asset.twig');
  225.         }
  226.     }
  227.     public function onKernelRequest(RequestEvent $event)
  228.     {
  229.         /** @var Config $Config */
  230.         $Config $this->configRepository->get();
  231.         $route $event->getRequest()->attributes->get('_route');
  232.         if (
  233.             'admin_login' === $route
  234.             && $Config->isEnableAdminLogin() && !empty($Config->getSiteKey()) && !empty($Config->getSecretKey())
  235.         ) {
  236.             $loginForm $this->formFactory->createNamed(''LoginType::class);
  237.             $loginForm->handleRequest($event->getRequest());
  238.             if (!$loginForm->isSubmitted()) {
  239.                 return;
  240.             }
  241.             $threshold $Config->getThresholdAdminLogin();
  242.             $userName $loginForm->get('login_id')->getData();
  243.         } elseif (
  244.             ('shopping_login' === $route || 'mypage_login' === $route)
  245.             && $Config->isEnableFrontLogin() && !empty($Config->getSiteKey()) && !empty($Config->getSecretKey())
  246.         ) {
  247.             $loginForm $this->formFactory->createNamed(''CustomerLoginType::class);
  248.             $loginForm->handleRequest($event->getRequest());
  249.             if (!$loginForm->isSubmitted()) {
  250.                 return;
  251.             }
  252.             $threshold $Config->getThreshold();
  253.             $userName $loginForm->get('login_email')->getData();
  254.         } else {
  255.             return;
  256.         }
  257.         $request $event->getRequest();
  258.         $session $request->getSession();
  259.         $recaptchaToken $loginForm->get('recaptcha_response')->getData();
  260.         if (empty($recaptchaToken)) {
  261.             $session->set(Security::AUTHENTICATION_ERROR, new TokenNotFoundException('Google reCAPTCHA Token is not set. Try again later or contact the site administrator.'));
  262.         } else {
  263.             switch ($Config->getRequestMethod()) {
  264.                 case Config::REQUEST_METHOD_CURL:
  265.                     $requestMethod = new CurlPost();
  266.                     break;
  267.                 case Config::REQUEST_METHOD_SOCKET:
  268.                     $requestMethod = new SocketPost();
  269.                     break;
  270.                 default:
  271.                     $requestMethod = new Post();
  272.                     break;
  273.             }
  274.             $recaptcha = new ReCaptcha($Config->getSecretKey(), $requestMethod);
  275.             $resp $recaptcha->setExpectedHostname($request->getHost())
  276.                 ->setExpectedAction('login')
  277.                 ->setScoreThreshold($threshold)
  278.                 ->verify($recaptchaToken$request->getClientIp());
  279.             if ($resp->isSuccess()) {
  280.                 if ($Config->getLogMode() == Config::LOG_MODE_ALL) {
  281.                     log_info('いつも reCAPTCHA 認証ログ', [$resp->toArray()]);
  282.                 }
  283.                 return;
  284.             }
  285.             $session->set(Security::AUTHENTICATION_ERROR, new VerifyFailException('Google reCAPTCHA verification is failed. Try again later or contact the site administrator.'));
  286.             log_warning('いつも reCAPTCHA 認証ログ', [$resp->toArray()]);
  287.         }
  288.         $session->set(Security::LAST_USERNAME$userName);
  289.         //to prevent request to call next event
  290.         $event->setResponse(new RedirectResponse($event->getRequest()->getRequestUri()));
  291.     }
  292. }