vendor/symfony/framework-bundle/Controller/RedirectController.php line 109

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\FrameworkBundle\Controller;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Exception\HttpException;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. /**
  17.  * Redirects a request to another URL.
  18.  *
  19.  * @author Fabien Potencier <fabien@symfony.com>
  20.  *
  21.  * @final
  22.  */
  23. class RedirectController
  24. {
  25.     private $router;
  26.     private $httpPort;
  27.     private $httpsPort;
  28.     public function __construct(UrlGeneratorInterface $router = null, int $httpPort = null, int $httpsPort = null)
  29.     {
  30.         $this->router = $router;
  31.         $this->httpPort = $httpPort;
  32.         $this->httpsPort = $httpsPort;
  33.     }
  34.     /**
  35.      * Redirects to another route with the given name.
  36.      *
  37.      * The response status code is 302 if the permanent parameter is false (default),
  38.      * and 301 if the redirection is permanent.
  39.      *
  40.      * In case the route name is empty, the status code will be 404 when permanent is false
  41.      * and 410 otherwise.
  42.      *
  43.      * @param string     $route             The route name to redirect to
  44.      * @param bool       $permanent         Whether the redirection is permanent
  45.      * @param bool|array $ignoreAttributes  Whether to ignore attributes or an array of attributes to ignore
  46.      * @param bool       $keepRequestMethod Whether redirect action should keep HTTP request method
  47.      *
  48.      * @throws HttpException In case the route name is empty
  49.      */
  50.     public function redirectAction(Request $request, string $route, bool $permanent = false, $ignoreAttributes = false, bool $keepRequestMethod = false, bool $keepQueryParams = false): Response
  51.     {
  52.         if ('' == $route) {
  53.             throw new HttpException($permanent ? 410 : 404);
  54.         }
  55.         $attributes = [];
  56.         if (false === $ignoreAttributes || \is_array($ignoreAttributes)) {
  57.             $attributes = $request->attributes->get('_route_params');
  58.             if ($keepQueryParams) {
  59.                 if ($query = $request->server->get('QUERY_STRING')) {
  60.                     $query = self::parseQuery($query);
  61.                 } else {
  62.                     $query = $request->query->all();
  63.                 }
  64.                 $attributes = array_merge($query, $attributes);
  65.             }
  66.             unset($attributes['route'], $attributes['permanent'], $attributes['ignoreAttributes'], $attributes['keepRequestMethod'], $attributes['keepQueryParams']);
  67.             if ($ignoreAttributes) {
  68.                 $attributes = array_diff_key($attributes, array_flip($ignoreAttributes));
  69.             }
  70.         }
  71.         if ($keepRequestMethod) {
  72.             $statusCode = $permanent ? 308 : 307;
  73.         } else {
  74.             $statusCode = $permanent ? 301 : 302;
  75.         }
  76.         return new RedirectResponse($this->router->generate($route, $attributes, UrlGeneratorInterface::ABSOLUTE_URL), $statusCode);
  77.     }
  78.     /**
  79.      * Redirects to a URL.
  80.      *
  81.      * The response status code is 302 if the permanent parameter is false (default),
  82.      * and 301 if the redirection is permanent.
  83.      *
  84.      * In case the path is empty, the status code will be 404 when permanent is false
  85.      * and 410 otherwise.
  86.      *
  87.      * @param string      $path              The absolute path or URL to redirect to
  88.      * @param bool        $permanent         Whether the redirect is permanent or not
  89.      * @param string|null $scheme            The URL scheme (null to keep the current one)
  90.      * @param int|null    $httpPort          The HTTP port (null to keep the current one for the same scheme or the default configured port)
  91.      * @param int|null    $httpsPort         The HTTPS port (null to keep the current one for the same scheme or the default configured port)
  92.      * @param bool        $keepRequestMethod Whether redirect action should keep HTTP request method
  93.      *
  94.      * @throws HttpException In case the path is empty
  95.      */
  96.     public function urlRedirectAction(Request $request, string $path, bool $permanent = false, string $scheme = null, int $httpPort = null, int $httpsPort = null, bool $keepRequestMethod = false): Response
  97.     {
  98.         if ('' == $path) {
  99.             throw new HttpException($permanent ? 410 : 404);
  100.         }
  101.         if ($keepRequestMethod) {
  102.             $statusCode = $permanent ? 308 : 307;
  103.         } else {
  104.             $statusCode = $permanent ? 301 : 302;
  105.         }
  106.         // redirect if the path is a full URL
  107.         if (parse_url($path, \PHP_URL_SCHEME)) {
  108.             return new RedirectResponse($path, $statusCode);
  109.         }
  110.         if (null === $scheme) {
  111.             $scheme = $request->getScheme();
  112.         }
  113.         if ($qs = $request->server->get('QUERY_STRING') ?: $request->getQueryString()) {
  114.             if (!str_contains($path, '?')) {
  115.                 $qs = '?'.$qs;
  116.             } else {
  117.                 $qs = '&'.$qs;
  118.             }
  119.         }
  120.         $port = '';
  121.         if ('http' === $scheme) {
  122.             if (null === $httpPort) {
  123.                 if ('http' === $request->getScheme()) {
  124.                     $httpPort = $request->getPort();
  125.                 } else {
  126.                     $httpPort = $this->httpPort;
  127.                 }
  128.             }
  129.             if (null !== $httpPort && 80 != $httpPort) {
  130.                 $port = ":$httpPort";
  131.             }
  132.         } elseif ('https' === $scheme) {
  133.             if (null === $httpsPort) {
  134.                 if ('https' === $request->getScheme()) {
  135.                     $httpsPort = $request->getPort();
  136.                 } else {
  137.                     $httpsPort = $this->httpsPort;
  138.                 }
  139.             }
  140.             if (null !== $httpsPort && 443 != $httpsPort) {
  141.                 $port = ":$httpsPort";
  142.             }
  143.         }
  144.         $url = $scheme.'://'.$request->getHost().$port.$request->getBaseUrl().$path.$qs;
  145.         return new RedirectResponse($url, $statusCode);
  146.     }
  147.     public function __invoke(Request $request): Response
  148.     {
  149.         $p = $request->attributes->get('_route_params', []);
  150.         if (\array_key_exists('route', $p)) {
  151.             if (\array_key_exists('path', $p)) {
  152.                 throw new \RuntimeException(sprintf('Ambiguous redirection settings, use the "path" or "route" parameter, not both: "%s" and "%s" found respectively in "%s" routing configuration.', $p['path'], $p['route'], $request->attributes->get('_route')));
  153.             }
  154.             return $this->redirectAction($request, $p['route'], $p['permanent'] ?? false, $p['ignoreAttributes'] ?? false, $p['keepRequestMethod'] ?? false, $p['keepQueryParams'] ?? false);
  155.         }
  156.         if (\array_key_exists('path', $p)) {
  157.             return $this->urlRedirectAction($request, $p['path'], $p['permanent'] ?? false, $p['scheme'] ?? null, $p['httpPort'] ?? null, $p['httpsPort'] ?? null, $p['keepRequestMethod'] ?? false);
  158.         }
  159.         throw new \RuntimeException(sprintf('The parameter "path" or "route" is required to configure the redirect action in "%s" routing configuration.', $request->attributes->get('_route')));
  160.     }
  161.     private static function parseQuery(string $query)
  162.     {
  163.         $q = [];
  164.         foreach (explode('&', $query) as $v) {
  165.             if (false !== $i = strpos($v, "\0")) {
  166.                 $v = substr($v, 0, $i);
  167.             }
  168.             if (false === $i = strpos($v, '=')) {
  169.                 $k = urldecode($v);
  170.                 $v = '';
  171.             } else {
  172.                 $k = urldecode(substr($v, 0, $i));
  173.                 $v = substr($v, $i);
  174.             }
  175.             if (false !== $i = strpos($k, "\0")) {
  176.                 $k = substr($k, 0, $i);
  177.             }
  178.             $k = ltrim($k, ' ');
  179.             if (false === $i = strpos($k, '[')) {
  180.                 $q[] = bin2hex($k).$v;
  181.             } else {
  182.                 $q[] = bin2hex(substr($k, 0, $i)).rawurlencode(substr($k, $i)).$v;
  183.             }
  184.         }
  185.         parse_str(implode('&', $q), $q);
  186.         $query = [];
  187.         foreach ($q as $k => $v) {
  188.             if (false !== $i = strpos($k, '_')) {
  189.                 $query[substr_replace($k, hex2bin(substr($k, 0, $i)).'[', 0, 1 + $i)] = $v;
  190.             } else {
  191.                 $query[hex2bin($k)] = $v;
  192.             }
  193.         }
  194.         return $query;
  195.     }
  196. }