src/Utils/PortalSettingsService.php line 71

Open in your IDE?
  1. <?php
  2. namespace App\Utils;
  3. use App\Entity\Gos\PortalSettings;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpFoundation\Cookie;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. class PortalSettingsService
  9. {
  10.     private $request;
  11.     private $em;
  12.     public function __construct(RequestStack $requestStackEntityManagerInterface $entityManager)
  13.     {
  14.         $this->request       $requestStack->getCurrentRequest();
  15.         $this->em            $entityManager;
  16.     }
  17.     public function getPortalSettingsHashForIframe(Request $request): ?string
  18.     {
  19.         // backdoor for portal settings for netflixes
  20.         if ($portalId $request->query->get('portal'))
  21.         {
  22.             $portalSettings $this->em->getRepository(PortalSettings::class)->find($portalId);
  23.             if ($portalSettings instanceof PortalSettings)
  24.             {
  25.                 $request->getSession()->set('portalSettingsHash'$portalSettings->getHash());
  26.                 return $portalSettings->getHash();
  27.             }
  28.         }
  29.         $hash $this->getPortalSettingsHash($request);
  30.         if ($hash)
  31.         {
  32.             return $hash;
  33.         }
  34.         $domain UrlParser::getDomain($request->headers->get('referer'));
  35.         if (strpos($request->headers->get('referer'), 'gos') === false)
  36.         {
  37.             $portalSettings $this->em->getRepository(PortalSettings::class)->findOneByDomain($domain);
  38.             if ($portalSettings !== null)
  39.             {
  40.                 return $portalSettings->getHash();
  41.             }
  42.         }
  43.         return null;
  44.     }
  45.     public function getPortalSettingsForIframe(Request $request): ?PortalSettings
  46.     {
  47.         $hash $this->getPortalSettingsHashForIframe($request);
  48.         if ($hash !== null) {
  49.             return $this->em->getRepository(PortalSettings::class)->findOneByHash($hash);
  50.         }
  51.         return null;
  52.     }
  53.     public function getPortalSettingsHash(Request $request)
  54.     {
  55.         $sessionHash $request->getSession()->get('portalSettingsHash');
  56.         $cookiesHash $request->cookies->get('gos_portal_settings');
  57.         $requestHash $request->get('portalSettingsHash') ?? $request->request->get('hash');
  58.         $hash        false;
  59.         if (!empty($sessionHash))
  60.         {
  61.             $hash $sessionHash;
  62.         }
  63.         elseif (!empty($cookiesHash))
  64.         {
  65.             $hash $cookiesHash;
  66.         }
  67.         elseif (!empty($requestHash))
  68.         {
  69.             $hash $requestHash;
  70.         }
  71.         return $hash;
  72.     }
  73.     public function selectPortalSettings()
  74.     {
  75.         $selectPsId $this->request->request->get('selectPortalSettings');
  76.         if (is_null($selectPsId))
  77.         {
  78.             $selectPsId $this->request->getSession()->get('selectPsId');
  79.         }
  80.         else
  81.         {
  82.             if ($selectPsId != 0)
  83.             {
  84.                 $selectPs $this->em->getRepository(PortalSettings::class)->find($selectPsId);
  85.                 if (empty($selectPs))
  86.                 {
  87.                     $this->request->getSession()->getFlashBag()->add('danger''Not found portal settings');
  88.                     $selectPsId 0;
  89.                 }
  90.             }
  91.             $this->request->getSession()->set('selectPsId'$selectPsId);
  92.         }
  93.         if (empty($selectPsId))
  94.         {
  95.             $selectPsId 0;
  96.             $this->request->getSession()->set('selectPsId'$selectPsId);
  97.         }
  98.         return $selectPsId;
  99.     }
  100.     public function getPortalSettings() : ?PortalSettings
  101.     {
  102.         $hash $this->getPortalSettingsHash($this->request);
  103.         if ($hash)
  104.         {
  105.             return $this->em->getRepository(PortalSettings::class)->findOneByHash($hash);
  106.         }
  107.         return null;
  108.     }
  109.     public function createGosPortalSettingsCookie($hash): Cookie
  110.     {
  111.         return new Cookie(
  112.             'gos_portal_settings',
  113.             $hash,
  114.             strtotime('now + 30 minutes'),
  115.             '/',
  116.             null,
  117.             $_ENV['APP_ENV'] === 'prod',
  118.             true,
  119.             false,
  120.             $_ENV['APP_ENV'] === 'prod' 'None' null
  121.         );
  122.     }
  123.     private function getHashFromSubdomain(string $subdomain): ?string
  124.     {
  125.         $pattern '/(?:gos|zamowienia)\.([a-zA-Z0-9-]+\.[a-zA-Z]{2,})/';
  126.         if (preg_match($pattern$subdomain$matches)) {
  127.             $portalSettings $this->em->getRepository(PortalSettings::class)->findMatching($matches[1]);
  128.             if ($portalSettings !== null) {
  129.                 return $portalSettings->getHash();
  130.             }
  131.         }
  132.         return null;
  133.     }
  134. }