<?php
namespace App\Utils;
use App\Entity\Gos\PortalSettings;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class PortalSettingsService
{
private $request;
private $em;
public function __construct(RequestStack $requestStack, EntityManagerInterface $entityManager)
{
$this->request = $requestStack->getCurrentRequest();
$this->em = $entityManager;
}
public function getPortalSettingsHashForIframe(Request $request): ?string
{
// backdoor for portal settings for netflixes
if ($portalId = $request->query->get('portal'))
{
$portalSettings = $this->em->getRepository(PortalSettings::class)->find($portalId);
if ($portalSettings instanceof PortalSettings)
{
$request->getSession()->set('portalSettingsHash', $portalSettings->getHash());
return $portalSettings->getHash();
}
}
$hash = $this->getPortalSettingsHash($request);
if ($hash)
{
return $hash;
}
$domain = UrlParser::getDomain($request->headers->get('referer'));
if (strpos($request->headers->get('referer'), 'gos') === false)
{
$portalSettings = $this->em->getRepository(PortalSettings::class)->findOneByDomain($domain);
if ($portalSettings !== null)
{
return $portalSettings->getHash();
}
}
return null;
}
public function getPortalSettingsForIframe(Request $request): ?PortalSettings
{
$hash = $this->getPortalSettingsHashForIframe($request);
if ($hash !== null) {
return $this->em->getRepository(PortalSettings::class)->findOneByHash($hash);
}
return null;
}
public function getPortalSettingsHash(Request $request)
{
$sessionHash = $request->getSession()->get('portalSettingsHash');
$cookiesHash = $request->cookies->get('gos_portal_settings');
$requestHash = $request->get('portalSettingsHash') ?? $request->request->get('hash');
$hash = false;
if (!empty($sessionHash))
{
$hash = $sessionHash;
}
elseif (!empty($cookiesHash))
{
$hash = $cookiesHash;
}
elseif (!empty($requestHash))
{
$hash = $requestHash;
}
return $hash;
}
public function selectPortalSettings()
{
$selectPsId = $this->request->request->get('selectPortalSettings');
if (is_null($selectPsId))
{
$selectPsId = $this->request->getSession()->get('selectPsId');
}
else
{
if ($selectPsId != 0)
{
$selectPs = $this->em->getRepository(PortalSettings::class)->find($selectPsId);
if (empty($selectPs))
{
$this->request->getSession()->getFlashBag()->add('danger', 'Not found portal settings');
$selectPsId = 0;
}
}
$this->request->getSession()->set('selectPsId', $selectPsId);
}
if (empty($selectPsId))
{
$selectPsId = 0;
$this->request->getSession()->set('selectPsId', $selectPsId);
}
return $selectPsId;
}
public function getPortalSettings() : ?PortalSettings
{
$hash = $this->getPortalSettingsHash($this->request);
if ($hash)
{
return $this->em->getRepository(PortalSettings::class)->findOneByHash($hash);
}
return null;
}
public function createGosPortalSettingsCookie($hash): Cookie
{
return new Cookie(
'gos_portal_settings',
$hash,
strtotime('now + 30 minutes'),
'/',
null,
$_ENV['APP_ENV'] === 'prod',
true,
false,
$_ENV['APP_ENV'] === 'prod' ? 'None' : null
);
}
private function getHashFromSubdomain(string $subdomain): ?string
{
$pattern = '/(?:gos|zamowienia)\.([a-zA-Z0-9-]+\.[a-zA-Z]{2,})/';
if (preg_match($pattern, $subdomain, $matches)) {
$portalSettings = $this->em->getRepository(PortalSettings::class)->findMatching($matches[1]);
if ($portalSettings !== null) {
return $portalSettings->getHash();
}
}
return null;
}
}