custom/plugins/ScopPlatformRedirecter/src/Subscriber/RequestSubscriber.php line 70

Open in your IDE?
  1. <?php
  2. /**
  3.  * Implemented by scope01 GmbH team https://scope01.com
  4.  *
  5.  * @copyright scope01 GmbH https://scope01.com
  6.  * @license MIT License
  7.  * @link https://scope01.com
  8.  */
  9. declare(strict_types=1);
  10. /**
  11.  * Implemented by scope01 GmbH team https://scope01.com
  12.  *
  13.  * @copyright scope01 GmbH https://scope01.com
  14.  * @license MIT
  15.  * @link https://scope01.com
  16.  */
  17. namespace Scop\PlatformRedirecter\Subscriber;
  18. use Shopware\Core\Framework\Context;
  19. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  22. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  23. use Shopware\Core\Framework\Event\BeforeSendRedirectResponseEvent;
  24. use Shopware\Core\Framework\Event\BeforeSendResponseEvent;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. use Symfony\Component\HttpFoundation\RedirectResponse;
  27. class RequestSubscriber implements EventSubscriberInterface
  28. {
  29.     /**
  30.      * @var EntityRepositoryInterface
  31.      */
  32.     private $repository;
  33.     /**
  34.      * @var EntityRepositoryInterface
  35.      */
  36.     private $seoUrlRepository;
  37.     /**
  38.      * @param EntityRepositoryInterface $redirectRepository
  39.      */
  40.     public function __construct(EntityRepositoryInterface $redirectRepositoryEntityRepositoryInterface $seoUrlRepository)
  41.     {
  42.         /** @var EntityRepositoryInterface $repository */
  43.         $this->repository $redirectRepository;
  44.         $this->seoUrlRepository $seoUrlRepository;
  45.     }
  46.     /**
  47.      *
  48.      * @return array
  49.      */
  50.     public static function getSubscribedEvents(): array
  51.     {
  52.         return [
  53.             BeforeSendResponseEvent::class => 'redirectBeforeSendResponse',
  54.             BeforeSendRedirectResponseEvent::class => 'redirectBeforeSendResponse'
  55.         ];
  56.     }
  57.     /**
  58.      * Redirect to the new url if found in redirects
  59.      * Otherwise do nothing
  60.      * Modules like admin, api or widgets are excluded from redirects
  61.      *
  62.      * @param BeforeSendResponseEvent $event
  63.      */
  64.     public function redirectBeforeSendResponse(BeforeSendResponseEvent $event): void
  65.     {
  66.         $requestUri = (string)$event->getRequest()->get("sw-original-request-uri");
  67.         // "sw-original-request-uri" is not present in shopware versions below 6.4.0.0.
  68.         // In this case, an older method must be used for redirecting, which doesn't redirects correctly in some edge cases.
  69.         if ($requestUri === "") {
  70.             $this->oldRedirectBeforeSendResponse($event);
  71.             return;
  72.         }
  73.         $storefrontUri $event->getRequest()->get('sw-sales-channel-absolute-base-url');
  74.         $requestBase $event->getRequest()->getPathInfo();
  75.         $requestBaseUrl $event->getRequest()->getBaseUrl();
  76.         // Block overriding /admin and /api and widgets, so you can't lock out of the administration.
  77.         if (\strpos($requestBase"/admin") === 0) {
  78.             return;
  79.         }
  80.         if (\strpos($requestBase"/api") === 0) {
  81.             return;
  82.         }
  83.         if (\strpos($requestBase"/widgets") === 0) {
  84.             return;
  85.         }
  86.         if (\strpos($requestBase"/store-api") === 0) {
  87.             return;
  88.         }
  89.         if (\strpos($requestBase"/_profiler") === 0) {
  90.             return;
  91.         }
  92.         $context Context::createDefaultContext();
  93.         $search = [
  94.             $requestBaseUrl '/' $requestUri// relative url with shopware 6 in sub folder: /public/Ergonomic-Concrete-Cough-Machine/48314803f1244f609a2ce907bfb48f53
  95.             $requestBaseUrl $requestUri// relative url with shopware 6 in sub folder url is not shopware seo url: /public/test
  96.             $storefrontUri $requestUri// absolute url with shopware 6 in sub folder, full url with host: http://shopware-platform.local/public/test1
  97.             $storefrontUri '/' $requestUri// absolute url with shopware 6 in sub folder, full url with host and slash at the end: http://shopware-platform.local/public/Freizeit-Elektro/Telefone/
  98.             $requestUri// relative url domain configured in public folder: /Ergonomic-Concrete-Cough-Machine/48314803f1244f609a2ce907bfb48f53 or /test4
  99.             '/' $requestUri// absolute url domain configured in public folder: http://shopware-platform.local/Shoes-Baby/
  100.             \substr($requestUri1), // e.g. "test"
  101.         ];
  102.         // search for the redirect in the database
  103.         $redirects $this->repository->search((new Criteria())->addFilter(new EqualsAnyFilter('sourceURL'$search))->addFilter(new EqualsFilter('enabled'true))
  104.             ->setLimit(1), $context);
  105.         // No Redirect found for this URL, do nothing
  106.         if ($redirects->count() === 0) {
  107.             return;
  108.         }
  109.         $redirect $redirects->first();
  110.         $targetURL $redirect->getTargetURL();
  111.         $code $redirect->getHttpCode();
  112.         // Prevent endless redirecting when target url and source url have only different capitalisation
  113.         if (in_array($targetURL$searchtrue)) {
  114.             return;
  115.         }
  116.         /*
  117.          *  checks if $targetURL is a full url or path and redirects accordingly
  118.          */
  119.         if (!(\strpos($targetURL"http:") === || \strpos($targetURL"https:") === 0)) {
  120.             if (\strpos($targetURL"www.") === 0) {
  121.                 $targetURL "http://" $targetURL;
  122.             } else {
  123.                 if (\strpos($targetURL"/") !== 0) {
  124.                     $targetURL "/" $targetURL;
  125.                 }
  126.             }
  127.         }
  128.         $event->setResponse(new RedirectResponse($targetURL$code));
  129.     }
  130.     /**
  131.      * Method for shopware versions below 6.4.0.0
  132.      *
  133.      * Redirect to the new url if found in redirects
  134.      * Otherwise do nothing
  135.      * Modules like admin, api or widgets are excluded from redirects
  136.      *
  137.      * @param BeforeSendResponseEvent $event
  138.      */
  139.     private function oldRedirectBeforeSendResponse(BeforeSendResponseEvent $event): void
  140.     {
  141.         $requestUri = (string)$event->getRequest()->get('resolved-uri');
  142.         $storefrontUri $event->getRequest()->get('sw-storefront-url');
  143.         $requestBase $event->getRequest()->getPathInfo();
  144.         $requestBaseUrl $event->getRequest()->getBaseUrl();
  145.         $queryString = (string)$event->getRequest()->getQueryString();
  146.         $search = [];
  147.         // Block overriding /admin and /api and widgets, so you can't lock out of the administration.
  148.         if (\strpos($requestBase"/admin") === 0) {
  149.             return;
  150.         }
  151.         if (\strpos($requestBase"/api") === 0) {
  152.             return;
  153.         }
  154.         if (\strpos($requestBase"/widgets") === 0) {
  155.             return;
  156.         }
  157.         if (\strpos($requestBase"/store-api") === 0) {
  158.             return;
  159.         }
  160.         if (\strpos($requestBase"/_profiler") === 0) {
  161.             return;
  162.         }
  163.         if ($queryString !== '') {
  164.             $queryString urldecode($queryString);
  165.             $requestUri .= '?' $queryString;
  166.         }
  167.         // try to load the seo route
  168.         $context Context::createDefaultContext();
  169.         $redirects $this->seoUrlRepository->search((new Criteria())
  170.             ->addFilter(new EqualsAnyFilter('pathInfo', [$requestUri])), $context);
  171.         // if found overwrite search term with the seo route
  172.         if ($redirects->count() !== 0) {
  173.             foreach ($redirects as $redirect) {
  174.                 $requestBase $redirect->getSeoPathInfo();
  175.                 // Search for Redirect
  176.                 $search[] = [
  177.                     $requestBaseUrl '/' $requestBase// relative url with shopware 6 in sub folder: /public/Ergonomic-Concrete-Cough-Machine/48314803f1244f609a2ce907bfb48f53
  178.                     $requestBaseUrl $requestBase// relative url with shopware 6 in sub folder url is not shopware seo url: /public/test
  179.                     $storefrontUri $requestBase// absolute url with shopware 6 in sub folder, full url with host: http://shopware-platform.local/public/test1
  180.                     $storefrontUri '/' $requestBase// absolute url with shopware 6 in sub folder, full url with host and slash at the end: http://shopware-platform.local/public/Freizeit-Elektro/Telefone/
  181.                     $requestBase// relative url domain configured in public folder: /Ergonomic-Concrete-Cough-Machine/48314803f1244f609a2ce907bfb48f53 or /test4
  182.                     '/' $requestBase// absolute url domain configured in public folder: http://shopware-platform.local/Shoes-Baby/
  183.                     \substr($requestBase1), // e.g. "test"
  184.                 ];
  185.             }
  186.         }
  187.         if (!empty($search)) {
  188.             $search array_merge(...$search);
  189.         } else {
  190.             $search = [
  191.                 $requestBaseUrl '/' $requestUri// relative url with shopware 6 in sub folder: /public/Ergonomic-Concrete-Cough-Machine/48314803f1244f609a2ce907bfb48f53
  192.                 $requestBaseUrl $requestUri// relative url with shopware 6 in sub folder url is not shopware seo url: /public/test
  193.                 $storefrontUri $requestUri// absolute url with shopware 6 in sub folder, full url with host: http://shopware-platform.local/public/test1
  194.                 $storefrontUri '/' $requestUri// absolute url with shopware 6 in sub folder, full url with host and slash at the end: http://shopware-platform.local/public/Freizeit-Elektro/Telefone/
  195.                 $requestUri// relative url domain configured in public folder: /Ergonomic-Concrete-Cough-Machine/48314803f1244f609a2ce907bfb48f53 or /test4
  196.                 '/' $requestUri// absolute url domain configured in public folder: http://shopware-platform.local/Shoes-Baby/
  197.                 \substr($requestUri1), // e.g. "test"
  198.             ];
  199.         }
  200.         // search for the redirect in the database
  201.         $redirects $this->repository->search((new Criteria())->addFilter(new EqualsAnyFilter('sourceURL'$search))->addFilter(new EqualsFilter('enabled'true))
  202.             ->setLimit(1), $context);
  203.         // No Redirect found for this URL, do nothing
  204.         if ($redirects->count() === 0) {
  205.             return;
  206.         }
  207.         $redirect $redirects->first();
  208.         $targetURL $redirect->getTargetURL();
  209.         $code $redirect->getHttpCode();
  210.         // Prevent endless redirecting when target url belongs to the same seo url like the source url or when target url and source url have only different capitalisation
  211.         if (in_array($targetURL$searchtrue)) {
  212.             return;
  213.         }
  214.         /*
  215.          *  checks if $targetURL is a full url or path and redirects accordingly
  216.          */
  217.         if (!(\strpos($targetURL"http:") === || \strpos($targetURL"https:") === 0)) {
  218.             if (\strpos($targetURL"www.") === 0) {
  219.                 $targetURL "http://" $targetURL;
  220.             } else {
  221.                 if (\strpos($targetURL"/") !== 0) {
  222.                     $targetURL "/" $targetURL;
  223.                 }
  224.             }
  225.         }
  226.         $event->setResponse(new RedirectResponse($targetURL$code));
  227.     }
  228. }