custom/plugins/BXK_FancyProtect/src/Controller/BXK_FancyProtectController.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace BXK_FancyProtect\Controller;
  3. use Shopware\Storefront\Controller\StorefrontController;
  4. use Shopware\Core\Framework\Context;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\HttpFoundation\Session\Session;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  11. use Symfony\Component\HttpFoundation\Cookie;
  12. class BXK_FancyProtectController extends StorefrontController {
  13.     private $config;
  14.     private $session;
  15.     public function __construct(Session $sessionSystemConfigService $systemConfigService) {
  16.         $this->session $session;
  17.         $this->config $systemConfigService;
  18.     }
  19.     /**
  20.      * Fancy Protect Page - GET
  21.      *
  22.      * @RouteScope(scopes={"storefront"})
  23.      * @Route("/entry", name="frontend.bxk_fancyprotect.entry", methods={"GET"})
  24.      */
  25.     public function index(Request $requestContext $context) {
  26.         $md5Key $request->cookies->get("entry"null);
  27.         $isProtected true;
  28.         if ($md5Key) {
  29.             $isProtected md5($this->getProtectionPassword($request)) != $md5Key;
  30.         }
  31.         $releaseDate $this->getConfigValue('unlockTime'$request->attributes->get('sw-sales-channel-id'));
  32.         if (isset($releaseDate)) {
  33.             if ($this->getProtectionPassword($request) == "" || strtotime($releaseDate) <= strtotime('now')) {
  34.                 $isProtected false;
  35.             }
  36.         }
  37.         if ($isProtected) {
  38.             // get options for frontend
  39.             $params $this->getFancyParams($request);
  40.             return $this->renderStorefront('@BXK_FancyProtect/plugins/BXK_FancyProtect/index.html.twig'$params);
  41.         } else {
  42.             return $this->redirectToRoute("frontend.home.page");
  43.         }
  44.     }
  45.     public function getFancyParams($request) {
  46.         $salesChannelId $request->attributes->get('sw-sales-channel-id');
  47.         $title = ($this->getConfigValue('title'$salesChannelId) == "") ? "COMING SOON" $this->getConfigValue('title'$salesChannelId);
  48.         $content = ($this->getConfigValue('content'$salesChannelId) == "") ? "Stay tuned, we working on our website!" $this->getConfigValue('content'$salesChannelId);
  49.         return [
  50.             'title' => $title,
  51.             'content' =>  $content,
  52.             'urlBackground' =>  $this->getConfigValue('urlBackground'$salesChannelId),
  53.             'unlockTime' =>  $this->getConfigValue('unlockTime'$salesChannelId),
  54.             'showNewsletter' =>  $this->getConfigValue('showNewsletter'$salesChannelId),
  55.             'socialFacebook' =>  $this->getConfigValue('socialFacebook'$salesChannelId),
  56.             'socialInstagram' =>  $this->getConfigValue('socialInstagram'$salesChannelId),
  57.             'socialTwitter' =>  $this->getConfigValue('socialTwitter'$salesChannelId),
  58.         ];
  59.     }
  60.     /**
  61.      * Fancy Protect Early Access Check - POST
  62.      *
  63.      * @RouteScope(scopes={"storefront"})
  64.      * @Route("/entry", name="frontend.bxkfancy.entry.login", methods={"POST"})
  65.      */
  66.     public function login(Request $requestContext $context) {
  67.         // get password
  68.         $key $request->get('fancyprotect'null);
  69.         // get other options for frontend
  70.         $params $this->getFancyParams($request);
  71.         if (isset($key) && is_string($key)) {
  72.             // Check key
  73.             if (md5($key) === md5($this->getProtectionPassword($request))) {
  74.                 // Forward to homepage
  75.                 $response = new Response(''302);
  76.                 $response->headers->set("location"$this->generateUrl("frontend.home.page"));
  77.                 $response->headers->setCookie(new Cookie('entry'md5($key),0'/'nullfalsefalse));
  78.                 return $response;
  79.             }
  80.             $params['fancyprotect_error'] = 'fancyprotect.wrongPassword';
  81.         }
  82.         return $this->renderStorefront('@BXK_FancyProtect/plugins/BXK_FancyProtect/index.html.twig',$params);
  83.     }
  84.     private function getProtectionPassword($request) {
  85.         return $this->getConfigValue('protectionPwd'$request->attributes->get('sw-sales-channel-id'));
  86.     }
  87.     private function getConfigValue($key$default null) {
  88.         $configKey 'BXK_FancyProtect.config.' $key;
  89.         return $this->config->get($configKey$default);
  90.     }
  91. }