src/EventListener/RequestListener.php line 64

Open in your IDE?
  1. <?php
  2. // src/EventListener/RequestListener.php
  3. namespace App\EventListener;
  4. use App\Entity\Domains\Domain;
  5. use App\Entity\Generic\User;
  6. use App\Entity\Setting;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\HttpKernel\Event\RequestEvent;
  11. use Twig\Loader\FilesystemLoader;
  12. #
  13. #[AsEventListener(event'kernel.request'priority255)]
  14. class RequestListener
  15. {
  16.     private $entityManager;
  17.     private $session;
  18.     private $twigLoader;
  19.     public function __construct(EntityManagerInterface $entityManagerSessionInterface $sessionFilesystemLoader $twigLoader)
  20.     {
  21.         $this->entityManager $entityManager;
  22.         $this->session $session;
  23.         $this->twigLoader $twigLoader;
  24.     }
  25. //    public function onKernelRequest(RequestEvent $event)
  26. //    {
  27. //        $request = $event->getRequest();
  28. //        $subLink = $this->entityManager->getRepository(Setting::class)->findOneBy(['label' => 'SUBSCRIPTIONLINK'])->getValue();
  29. //        $currentDomain = $request->getHttpHost();
  30. //        $domain = $request->getHttpHost();
  31. //        if ($domain == $subLink &&
  32. //            (
  33. //                (!str_contains($request->getRequestUri(), 'subscription'))
  34. //                &&
  35. //                (!str_contains($request->getRequestUri(), 'tg'))
  36. //            ) && $domain !== 'buffalo724.org'
  37. //        ) {
  38. ////            echo 'این دامنه متعلق به استفاده سابسکریپشن میباشد و در صورتی که مستقیم وارد پنل بشید فیلتر میشود لذا برای جلوگیری از این امر لطفا از طریق آدرس درست وارد شوید';
  39. ////            die;
  40. //        } else {
  41. //            $appUrl = $_ENV['APP_URL'];
  42. //            $appUrl = str_replace('http://', '', $appUrl);
  43. //            $appUrl = str_replace('https://', '', $appUrl);
  44. //            if ($domain != $appUrl && $domain != '127.0.0.1' && $domain != 'tehrannetvpn.com' && $domain != 'buffalo724.org' && $domain != 'kenargozar.com' && $domain != 'www.kenargozar.com' && $domain != $subLink) {
  45. //                $targetDomain = $this->entityManager->getRepository(Domain::class)->findOneBy(['address' => $domain, 'active' => true]);
  46. //                if (!$targetDomain || (!$targetDomain->isActive())) {
  47. ////                    die('دامنه منقضی شده است لطفا از دامنه پایه تلاش فرمایید');
  48. //                }
  49. //
  50. //
  51. //            }
  52. //        }
  53. //
  54. //    }
  55.     public function onKernelRequest(RequestEvent $event)
  56.     {
  57.         $request $event->getRequest();
  58.         $subLinkSetting $this->entityManager->getRepository(Setting::class)->findOneBy(['label' => 'SUBSCRIPTIONLINK']);
  59.         if ($subLinkSetting) {
  60.             $subLink $subLinkSetting->getValue();
  61.             $domain $request->getHttpHost();
  62.             // Check if the request is valid for the subscription link
  63.             if ($domain === $subLink && !str_contains($request->getRequestUri(), 'subscription')  && !str_contains($request->getRequestUri(), 'sms') ) {
  64.                 die('دامنه منقضی شده است لطفا از دامنه پایه تلاش فرمایید');
  65.             } else {
  66.                 $appUrl trim(str_replace(['http://''https://'], ''$_ENV['APP_URL']));
  67.                 if ($domain !== $appUrl && !$this->isAllowedDomain($domain$subLink)) {
  68.                     die('دامنه منقضی شده است لطفا از دامنه پایه تلاش فرمایید');
  69.                 }
  70.             }
  71.         }
  72.     }
  73.     private function isAllowedDomain(string $domainstring $subLink): bool
  74.     {
  75.         $allowedDomains = [
  76.             '188.165.164.83',
  77.             '127.0.0.1''tehrannetvpn.com',  'kenargozar.com''www.kenargozar.com'$subLink];
  78.         foreach ($this->entityManager->getRepository(Domain::class)->findBy(['active' => 1]) as  $domainObject) {
  79.             $allowedDomains[] = $domainObject->getAddress();
  80.         }
  81.         return in_array($domain$allowedDomains);
  82.     }
  83. }