Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

vendor/symfony/security-http/HttpUtils.php line 42

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\Component\Security\Http;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  14. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
  17. use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
  18. use Symfony\Component\Security\Core\Security;
  19. /**
  20.  * Encapsulates the logic needed to create sub-requests, redirect the user, and match URLs.
  21.  *
  22.  * @author Fabien Potencier <fabien@symfony.com>
  23.  */
  24. class HttpUtils
  25. {
  26.     private $urlGenerator;
  27.     private $urlMatcher;
  28.     private $domainRegexp;
  29.     private $secureDomainRegexp;
  30.     /**
  31.      * @param UrlMatcherInterface|RequestMatcherInterface $urlMatcher         The URL or Request matcher
  32.      * @param string|null                                 $domainRegexp       A regexp the target of HTTP redirections must match, scheme included
  33.      * @param string|null                                 $secureDomainRegexp A regexp the target of HTTP redirections must match when the scheme is "https"
  34.      *
  35.      * @throws \InvalidArgumentException
  36.      */
  37.     public function __construct(UrlGeneratorInterface $urlGenerator null$urlMatcher nullstring $domainRegexp nullstring $secureDomainRegexp null)
  38.     {
  39.         $this->urlGenerator $urlGenerator;
  40.         if (null !== $urlMatcher && !$urlMatcher instanceof UrlMatcherInterface && !$urlMatcher instanceof RequestMatcherInterface) {
  41.             throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
  42.         }
  43.         $this->urlMatcher $urlMatcher;
  44.         $this->domainRegexp $domainRegexp;
  45.         $this->secureDomainRegexp $secureDomainRegexp;
  46.     }
  47.     /**
  48.      * Creates a redirect Response.
  49.      *
  50.      * @param string $path   A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo))
  51.      * @param int    $status The status code
  52.      *
  53.      * @return RedirectResponse
  54.      */
  55.     public function createRedirectResponse(Request $requeststring $pathint $status 302)
  56.     {
  57.         if (null !== $this->secureDomainRegexp && 'https' === $this->urlMatcher->getContext()->getScheme() && preg_match('#^https?:[/\\\\]{2,}+[^/]++#i'$path$host) && !preg_match(sprintf($this->secureDomainRegexppreg_quote($request->getHttpHost())), $host[0])) {
  58.             $path '/';
  59.         }
  60.         if (null !== $this->domainRegexp && preg_match('#^https?:[/\\\\]{2,}+[^/]++#i'$path$host) && !preg_match(sprintf($this->domainRegexppreg_quote($request->getHttpHost())), $host[0])) {
  61.             $path '/';
  62.         }
  63.         return new RedirectResponse($this->generateUri($request$path), $status);
  64.     }
  65.     /**
  66.      * Creates a Request.
  67.      *
  68.      * @param string $path A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo))
  69.      *
  70.      * @return Request
  71.      */
  72.     public function createRequest(Request $requeststring $path)
  73.     {
  74.         $newRequest Request::create($this->generateUri($request$path), 'get', [], $request->cookies->all(), [], $request->server->all());
  75.         static $setSession;
  76.         if (null === $setSession) {
  77.             $setSession \Closure::bind(static function ($newRequest$request) { $newRequest->session $request->session; }, nullRequest::class);
  78.         }
  79.         $setSession($newRequest$request);
  80.         if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
  81.             $newRequest->attributes->set(Security::AUTHENTICATION_ERROR$request->attributes->get(Security::AUTHENTICATION_ERROR));
  82.         }
  83.         if ($request->attributes->has(Security::ACCESS_DENIED_ERROR)) {
  84.             $newRequest->attributes->set(Security::ACCESS_DENIED_ERROR$request->attributes->get(Security::ACCESS_DENIED_ERROR));
  85.         }
  86.         if ($request->attributes->has(Security::LAST_USERNAME)) {
  87.             $newRequest->attributes->set(Security::LAST_USERNAME$request->attributes->get(Security::LAST_USERNAME));
  88.         }
  89.         if ($request->get('_format')) {
  90.             $newRequest->attributes->set('_format'$request->get('_format'));
  91.         }
  92.         if ($request->getDefaultLocale() !== $request->getLocale()) {
  93.             $newRequest->setLocale($request->getLocale());
  94.         }
  95.         return $newRequest;
  96.     }
  97.     /**
  98.      * Checks that a given path matches the Request.
  99.      *
  100.      * @param string $path A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo))
  101.      *
  102.      * @return bool true if the path is the same as the one from the Request, false otherwise
  103.      */
  104.     public function checkRequestPath(Request $requeststring $path)
  105.     {
  106.         if ('/' !== $path[0]) {
  107.             // Shortcut if request has already been matched before
  108.             if ($request->attributes->has('_route')) {
  109.                 return $path === $request->attributes->get('_route');
  110.             }
  111.             try {
  112.                 // matching a request is more powerful than matching a URL path + context, so try that first
  113.                 if ($this->urlMatcher instanceof RequestMatcherInterface) {
  114.                     $parameters $this->urlMatcher->matchRequest($request);
  115.                 } else {
  116.                     $parameters $this->urlMatcher->match($request->getPathInfo());
  117.                 }
  118.                 return isset($parameters['_route']) && $path === $parameters['_route'];
  119.             } catch (MethodNotAllowedException $e) {
  120.                 return false;
  121.             } catch (ResourceNotFoundException $e) {
  122.                 return false;
  123.             }
  124.         }
  125.         return $path === rawurldecode($request->getPathInfo());
  126.     }
  127.     /**
  128.      * Generates a URI, based on the given path or absolute URL.
  129.      *
  130.      * @param string $path A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo))
  131.      *
  132.      * @return string
  133.      *
  134.      * @throws \LogicException
  135.      */
  136.     public function generateUri(Request $requeststring $path)
  137.     {
  138.         $url parse_url($path);
  139.         if ('' === $path || isset($url['scheme'], $url['host'])) {
  140.             return $path;
  141.         }
  142.         if ('/' === $path[0]) {
  143.             return $request->getUriForPath($path);
  144.         }
  145.         if (null === $this->urlGenerator) {
  146.             throw new \LogicException('You must provide a UrlGeneratorInterface instance to be able to use routes.');
  147.         }
  148.         $url $this->urlGenerator->generate($path$request->attributes->all(), UrlGeneratorInterface::ABSOLUTE_URL);
  149.         // unnecessary query string parameters must be removed from URL
  150.         // (ie. query parameters that are presents in $attributes)
  151.         // fortunately, they all are, so we have to remove entire query string
  152.         $position strpos($url'?');
  153.         if (false !== $position) {
  154.             $fragment parse_url($url\PHP_URL_FRAGMENT);
  155.             $url substr($url0$position);
  156.             // fragment must be preserved
  157.             if ($fragment) {
  158.                 $url .= "#$fragment";
  159.             }
  160.         }
  161.         return $url;
  162.     }
  163. }