vendor/store.shopware.com/bxk_fancyprotect/src/Subscriber/BXK_FancyProtectSubscriber.php line 33

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace BXK_FancyProtect\Subscriber;
  3. // use BXK_FancyProtect\Storefront\Controller\BXK_FancyProtectController as Controller;
  4. use Shopware\Administration\Controller\AdministrationController;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Storefront\Event\StorefrontRenderEvent;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. use Shopware\Core\Framework\Event\BeforeSendResponseEvent;
  12. use Symfony\Component\Routing\RouterInterface;
  13. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  14. class BXK_FancyProtectSubscriber implements EventSubscriberInterface {
  15.     private $config;
  16.     private $router;
  17.     public function __construct(RouterInterface $routerSystemConfigService $systemConfigService) {
  18.         $this->config $systemConfigService;
  19.         $this->router $router;
  20.     }
  21.     public static function getSubscribedEvents(): array {
  22.         return [
  23.             BeforeSendResponseEvent::class => 'fancyResponseCheck'
  24.         ];
  25.     }
  26.     public function fancyResponseCheck(BeforeSendResponseEvent $event){
  27.         $request $event->getRequest();
  28.         $targetUrl $this->router->generate('frontend.bxk_fancyprotect.entry');
  29.         $baseUrl $request->attributes->get('sw-sales-channel-base-url');
  30.         $protectionUri $this->router->generate("frontend.bxk_fancyprotect.entry");
  31.         $maintenanceUri $this->router->generate("frontend.maintenance.page");
  32.         $key $this->getProtectionPassword($request);
  33.         if (!isset($key) || $key == "") {
  34.             return; // no key setted -> allow!
  35.         }
  36.         $releaseDate $this->getConfigValue('unlockTime'$request->attributes->get('sw-sales-channel-id'));
  37.         if (isset($releaseDate)) {
  38.             if (strtotime($releaseDate) <= strtotime('now')) {
  39.                 return; // release date -> allow request
  40.             }
  41.         }  
  42.         $cookie $event->getRequest()->cookies->get("entry");
  43.         if(!is_null($cookie)){
  44.             $md5Key $event->getRequest()->cookies->get("entry");
  45.             if (md5($this->getProtectionPassword($request)) == $md5Key) {
  46.                 // Password in cookies is correct -> allow request
  47.                 return;
  48.             }
  49.         }
  50.         $attributes $request->attributes;
  51.         if(!$attributes->count()){
  52.             // not in storefront -> allow request
  53.             return;
  54.         }
  55.         $isProtectionPage $event->getRequest()->getRequestUri() == $protectionUri;
  56.         $isMaintenancePage $event->getRequest()->getRequestUri() == $maintenanceUri;
  57.         if($request->isXmlHttpRequest() || !$attributes->get("_is_sales_channel") || $isMaintenancePage || $isProtectionPage) {
  58.             // Ajax / XmlHttpRequest request (eg newsletter) or on protection page -> allow request
  59.             return;
  60.         }
  61.         $event->setResponse(
  62.             new RedirectResponse($baseUrl $targetUrl)
  63.         );
  64.     }
  65.     private function getProtectionPassword($request) {
  66.         return $this->getConfigValue('protectionPwd'$request->attributes->get('sw-sales-channel-id'));
  67.     }
  68.     private function getConfigValue($key$default null) {
  69.         $configKey 'BXK_FancyProtect.config.' $key;
  70.         return $this->config->get($configKey$default);
  71.     }
  72. }