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.         $redirects $this->redirectRepository->getallRedirects();
  73.         //TODO. Убрать после переноса
  74.         if (Env::site() == Env::OPT_MIX && $event->getRequest()->getHost() == 'opt.mixform.pl') {
  75.             $event->setResponse(new RedirectResponse("https://mixformwhole.pl".$event->getRequest()->getRequestUri(), 301));
  76.         }
  77.         if (Env::site() == Env::DOM) {
  78.             $request $event->getRequest();
  79.             $host $request->getHost();
  80.             if (!str_starts_with($host'www.')) {
  81.                 $uri $request->getSchemeAndHttpHost();
  82.                 $uri preg_replace('/^https?:\/\/' preg_quote($host'/') . '/i'$request->getScheme() . '://www.' $host$request->getUri());
  83.                 $event->setResponse(new RedirectResponse($uri301));
  84.             }
  85.         }
  86.         $request $event->getRequest();
  87.         
  88.         if (in_array($request->getRequestUri(), array_keys($redirects))) {
  89.             $event->setResponse(new RedirectResponse($redirects[$request->getRequestUri()], 301));
  90.             return;
  91.         }
  92.         // 1. Не выполнять редирект в окружении разработки ('dev'), чтобы не мешать локальной работе
  93.         // if ($this->environment === 'dev') {
  94.         //     return;
  95.         // }
  96.         // 2. Не выполнять редирект, если это не основной запрос (например, внутренний ESI или sub-request)
  97.         if (!$event->isMainRequest()) {
  98.             return;
  99.         }
  100.         if (Env::site() == Env::MIX || Env::site() == Env::OPT_MIX) {
  101.             if (strstr($request->getHttpHost(), 'www')) {
  102.                 $url Env::host() . $request->getRequestUri();
  103.                 $response = new RedirectResponse($url301);
  104.                 $event->setResponse($response);
  105.                 return;
  106.             }
  107.         }
  108.         // 3. Не выполнять редирект, если запрос УЖЕ защищен (уже HTTPS)
  109.         // Метод isSecure() умный и учитывает заголовки от прокси-серверов (см. ниже)
  110.         if ($request->getScheme() == 'https') {
  111.             return;
  112.         }
  113.         
  114.         // --- Логика редиректа ---
  115.         $url 'https://' $request->getHttpHost() . $request->getRequestUri();
  116.         $response = new RedirectResponse($url301);
  117.         $event->setResponse($response);
  118.     }
  119.     private function redirectMixFormOpt(Request $request)
  120.     {        
  121.         if (!$request->getLocale()) {
  122.             $request->setLocale($request->getDefaultLocale());
  123.         }
  124.     }
  125.     public static function getSubscribedEvents(): array
  126.     {
  127.         return [
  128.             'kernel.request' => 'onRequest',
  129.         ];
  130.     }
  131. }