vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/Controller/RedirectController.php line 60

  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\WebsiteBundle\Controller;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Exception\HttpException;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Component\Routing\RouterInterface;
  16. /**
  17.  * Contains redirect actions.
  18.  */
  19. class RedirectController
  20. {
  21.     /**
  22.      * @var RouterInterface
  23.      */
  24.     protected $router;
  25.     public function __construct(RouterInterface $router)
  26.     {
  27.         $this->router $router;
  28.     }
  29.     /**
  30.      * Creates a redirect for configured webspaces.
  31.      *
  32.      * @return RedirectResponse
  33.      *
  34.      * @deprecated since 1.6 will be removed with 2.0. Replaced by ExceptionListener::redirectPartialMatch
  35.      */
  36.     public function redirectWebspaceAction(Request $request)
  37.     {
  38.         @trigger_deprecation('sulu/sulu''1.6'__METHOD__ '() is deprecated and will be removed in 2.0. Replaced by ExceptionListener::redirectPartialMatch.');
  39.         $url $this->resolveRedirectUrl(
  40.             $request->get('redirect'),
  41.             $request->getUri(),
  42.             $request->get('_sulu')->getAttribute('resourceLocatorPrefix')
  43.         );
  44.         return new RedirectResponse($url301, ['Cache-Control' => 'private']);
  45.     }
  46.     /**
  47.      * Creates a redirect for *.html to * (without html).
  48.      *
  49.      * @return RedirectResponse
  50.      */
  51.     public function redirectAction(Request $request)
  52.     {
  53.         return new RedirectResponse($request->get('url'), 301, ['Cache-Control' => 'private']);
  54.     }
  55.     /**
  56.      * Create a redirect response which uses a route to generate redirect.
  57.      *
  58.      * @param string $route
  59.      * @param bool $permanent
  60.      *
  61.      * @return RedirectResponse
  62.      */
  63.     public function redirectToRouteAction(Request $request$route$permanent false)
  64.     {
  65.         if ('' === $route) {
  66.             throw new HttpException($permanent 410 404);
  67.         }
  68.         $attributes \array_merge($request->attributes->get('_route_params'), $request->query->all());
  69.         unset($attributes['route'], $attributes['permanent']);
  70.         return new RedirectResponse(
  71.             $this->router->generate($route$attributesUrlGeneratorInterface::ABSOLUTE_URL),
  72.             $permanent 301 302,
  73.             ['Cache-Control' => 'private']
  74.         );
  75.     }
  76.     /**
  77.      * Resolve the redirect URL, appending any additional path data.
  78.      *
  79.      * @param string $redirectUrl Redirect webspace URI
  80.      * @param string $requestUri The actual incoming request URI
  81.      * @param string $resourceLocatorPrefix The prefix of the actual portal
  82.      *
  83.      * @return string URL to redirect to
  84.      */
  85.     private function resolveRedirectUrl($redirectUrl$requestUri$resourceLocatorPrefix)
  86.     {
  87.         $redirectInfo $this->parseUrl($redirectUrl);
  88.         $requestInfo $this->parseUrl($requestUri);
  89.         $url \sprintf('%s://%s'$requestInfo['scheme'], $requestInfo['host']);
  90.         if (isset($redirectInfo['host'])) {
  91.             $url \sprintf('%s://%s'$requestInfo['scheme'], $redirectInfo['host']);
  92.         }
  93.         if (isset($requestInfo['port'])) {
  94.             $url .= ':' $requestInfo['port'];
  95.         }
  96.         if (isset($redirectInfo['path'])
  97.             && (
  98.                 // if requested url not starting with redirectUrl it need to be added
  99.                 !isset($requestInfo['path'])
  100.                 || !== \strpos($requestInfo['path'], $redirectInfo['path'] . '/')
  101.             )
  102.         ) {
  103.             $url .= $redirectInfo['path'];
  104.         }
  105.         if (isset($requestInfo['path']) && $resourceLocatorPrefix !== $requestInfo['path']) {
  106.             $path $requestInfo['path'];
  107.             if (=== \strpos($path$resourceLocatorPrefix)) {
  108.                 $path \substr($path\strlen($resourceLocatorPrefix));
  109.             }
  110.             $url .= $path;
  111.             $url \rtrim($url'/');
  112.         }
  113.         if (isset($requestInfo['query'])) {
  114.             $url .= '?' $requestInfo['query'];
  115.         }
  116.         if (isset($requestInfo['fragment'])) {
  117.             $url .= '#' $requestInfo['fragment'];
  118.         }
  119.         return $url;
  120.     }
  121.     /**
  122.      * Prefix http to the URL if it is missing and
  123.      * then parse the string using parse_url.
  124.      *
  125.      * @param string $url
  126.      *
  127.      * @return string
  128.      */
  129.     private function parseUrl($url)
  130.     {
  131.         if (!\preg_match('{^https?://}'$url)) {
  132.             $url 'http://' $url;
  133.         }
  134.         return \parse_url($url);
  135.     }
  136. }