src/EventSubscriber/Redirect.php line 80

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Env;
  4. use App\Func;
  5. use App\DTO\AppDTO;
  6. use App\DTO\PageDTO;
  7. use App\Entity\Cart;
  8. use App\Entity\Sett;
  9. use App\Entity\User;
  10. use App\Entity\Block;
  11. use App\Entity\Order;
  12. use App\Entity\Timer;
  13. use Twig\Environment;
  14. use App\Entity\Banner;
  15. use App\Entity\Labels;
  16. use App\Model\Wishlist;
  17. use App\Entity\Template;
  18. use App\GlobalVars\Blocks;
  19. use App\Service\Auth\Auth;
  20. use App\Entity\Transaction;
  21. use App\GlobalVars\Templates;
  22. use App\Entity\Page as EntityPage;
  23. use App\Repository\PageRepository;
  24. use Doctrine\ORM\EntityManagerInterface;
  25. use App\GlobalVars\Sett as GlobalVarsSett;
  26. use Symfony\Contracts\Cache\ItemInterface;
  27. use App\Entity\Translation\PageTranslation;
  28. use App\Entity\Translation\SettTranslation;
  29. use Symfony\Contracts\Cache\CacheInterface;
  30. use App\Entity\Translation\TimerTranslation;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Symfony\Component\Security\Core\Security;
  33. use App\GlobalVars\Labels as GlobalVarsLabels;
  34. use Symfony\Component\HttpFoundation\Response;
  35. use App\Entity\Translation\TemplateTranslation;
  36. use App\Repository\RedirectRepository;
  37. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  38. use Symfony\Component\HttpKernel\Event\RequestEvent;
  39. use Symfony\Component\HttpFoundation\RedirectResponse;
  40. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  41. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  42. class Redirect implements EventSubscriberInterface
  43. {
  44.     private $no_cache false;
  45.     
  46.     private Environment $twig;
  47.     private EntityManagerInterface $em;
  48.     private CacheInterface $Cache;
  49.     private Auth $Auth;
  50.     private Env $Env;
  51.     private Func $Func;
  52.     private Wishlist $Wishlist;
  53.     private ?AppDTO $app;
  54.     // Temp
  55.     private $_pagename;
  56.     private RedirectRepository $redirectRepository;
  57.     public function __construct(AppDTO $appEnvironment $twigEntityManagerInterface $emTagAwareCacheInterface $appCacheAuth $AuthEnv $EnvFunc $FuncWishlist $WishlistSecurity $securityRedirectRepository $redirectRepository)
  58.     {
  59.         $this->twig $twig;
  60.         $this->em $em;
  61.         $this->app $app;
  62.         $this->Cache $appCache;
  63.         $this->Auth $Auth;
  64.         $this->Auth->setUser($security->getUser());
  65.         $this->Env $Env;
  66.         $this->Func $Func;
  67.         $this->Wishlist $Wishlist;
  68.         $this->redirectRepository $redirectRepository;
  69.     }
  70.     public function onRequest(RequestEvent $event): void
  71.     {
  72.         if (Env::is_test()) {
  73.             return;
  74.         }
  75.         
  76.         $redirects $this->redirectRepository->getallRedirects();
  77.         //TODO. Убрать после переноса
  78.         if (Env::site() == Env::OPT_MIX && $event->getRequest()->getHost() == 'opt.mixform.pl') {
  79.             $event->setResponse(new RedirectResponse("https://mixformwhole.pl".$event->getRequest()->getRequestUri(), 301));
  80.         }
  81.         if (Env::site() == Env::DOM && !Env::is_test()) {
  82.             $request $event->getRequest();
  83.             $host $request->getHost();
  84.             if (!str_starts_with($host'www.')) {
  85.                 $uri $request->getSchemeAndHttpHost();
  86.                 $uri preg_replace('/^https?:\/\/' preg_quote($host'/') . '/i'$request->getScheme() . '://www.' $host$request->getUri());
  87.                 $event->setResponse(new RedirectResponse($uri301));
  88.             }
  89.         }
  90.         $request $event->getRequest();
  91.         
  92.         if (in_array($request->getRequestUri(), array_keys($redirects))) {
  93.             $event->setResponse(new RedirectResponse($redirects[$request->getRequestUri()], 301));
  94.             return;
  95.         }
  96.         // 1. Не выполнять редирект в окружении разработки ('dev'), чтобы не мешать локальной работе
  97.         // if ($this->environment === 'dev') {
  98.         //     return;
  99.         // }
  100.         // 2. Не выполнять редирект, если это не основной запрос (например, внутренний ESI или sub-request)
  101.         if (!$event->isMainRequest()) {
  102.             return;
  103.         }
  104.         if (Env::site() == Env::MIX || Env::site() == Env::OPT_MIX) {
  105.             if (strstr($request->getHttpHost(), 'www')) {
  106.                 $url Env::host() . $request->getRequestUri();
  107.                 $response = new RedirectResponse($url301);
  108.                 $event->setResponse($response);
  109.                 return;
  110.             }
  111.         }
  112.         // 3. Не выполнять редирект, если запрос УЖЕ защищен (уже HTTPS)
  113.         // Метод isSecure() умный и учитывает заголовки от прокси-серверов (см. ниже)
  114.         if ($request->getScheme() == 'https') {
  115.             return;
  116.         }
  117.         
  118.         // --- Логика редиректа ---
  119.         $url 'https://' $request->getHttpHost() . $request->getRequestUri();
  120.         $response = new RedirectResponse($url301);
  121.         $event->setResponse($response);
  122.     }
  123.     private function redirectMixFormOpt(Request $request)
  124.     {        
  125.         if (!$request->getLocale()) {
  126.             $request->setLocale($request->getDefaultLocale());
  127.         }
  128.     }
  129.     public static function getSubscribedEvents(): array
  130.     {
  131.         return [
  132.             'kernel.request' => 'onRequest',
  133.         ];
  134.     }
  135. }