<?php
// src/EventListener/RequestListener.php
namespace App\EventListener;
use App\Entity\Domains\Domain;
use App\Entity\Generic\User;
use App\Entity\Setting;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Twig\Loader\FilesystemLoader;
#
#[AsEventListener(event: 'kernel.request', priority: 255)]
class RequestListener
{
private $entityManager;
private $session;
private $twigLoader;
public function __construct(EntityManagerInterface $entityManager, SessionInterface $session, FilesystemLoader $twigLoader)
{
$this->entityManager = $entityManager;
$this->session = $session;
$this->twigLoader = $twigLoader;
}
// public function onKernelRequest(RequestEvent $event)
// {
// $request = $event->getRequest();
// $subLink = $this->entityManager->getRepository(Setting::class)->findOneBy(['label' => 'SUBSCRIPTIONLINK'])->getValue();
// $currentDomain = $request->getHttpHost();
// $domain = $request->getHttpHost();
// if ($domain == $subLink &&
// (
// (!str_contains($request->getRequestUri(), 'subscription'))
// &&
// (!str_contains($request->getRequestUri(), 'tg'))
// ) && $domain !== 'buffalo724.org'
// ) {
//// echo 'این دامنه متعلق به استفاده سابسکریپشن میباشد و در صورتی که مستقیم وارد پنل بشید فیلتر میشود لذا برای جلوگیری از این امر لطفا از طریق آدرس درست وارد شوید';
//// die;
// } else {
// $appUrl = $_ENV['APP_URL'];
// $appUrl = str_replace('http://', '', $appUrl);
// $appUrl = str_replace('https://', '', $appUrl);
// if ($domain != $appUrl && $domain != '127.0.0.1' && $domain != 'tehrannetvpn.com' && $domain != 'buffalo724.org' && $domain != 'kenargozar.com' && $domain != 'www.kenargozar.com' && $domain != $subLink) {
// $targetDomain = $this->entityManager->getRepository(Domain::class)->findOneBy(['address' => $domain, 'active' => true]);
// if (!$targetDomain || (!$targetDomain->isActive())) {
//// die('دامنه منقضی شده است لطفا از دامنه پایه تلاش فرمایید');
// }
//
//
// }
// }
//
// }
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
$subLinkSetting = $this->entityManager->getRepository(Setting::class)->findOneBy(['label' => 'SUBSCRIPTIONLINK']);
if ($subLinkSetting) {
$subLink = $subLinkSetting->getValue();
$domain = $request->getHttpHost();
// Check if the request is valid for the subscription link
if ($domain === $subLink && !str_contains($request->getRequestUri(), 'subscription') && !str_contains($request->getRequestUri(), 'sms') ) {
die('دامنه منقضی شده است لطفا از دامنه پایه تلاش فرمایید');
} else {
$appUrl = trim(str_replace(['http://', 'https://'], '', $_ENV['APP_URL']));
if ($domain !== $appUrl && !$this->isAllowedDomain($domain, $subLink)) {
die('دامنه منقضی شده است لطفا از دامنه پایه تلاش فرمایید');
}
}
}
}
private function isAllowedDomain(string $domain, string $subLink): bool
{
$allowedDomains = [
'188.165.164.83',
'127.0.0.1', 'tehrannetvpn.com', 'kenargozar.com', 'www.kenargozar.com', $subLink];
foreach ($this->entityManager->getRepository(Domain::class)->findBy(['active' => 1]) as $domainObject) {
$allowedDomains[] = $domainObject->getAddress();
}
return in_array($domain, $allowedDomains);
}
}