vendor/symfony/src/Symfony/Component/Security/Http/HttpUtils.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\Security\Http;
       
    13 
       
    14 use Symfony\Component\Security\Core\SecurityContextInterface;
       
    15 
       
    16 use Symfony\Component\HttpFoundation\Request;
       
    17 use Symfony\Component\HttpFoundation\RedirectResponse;
       
    18 use Symfony\Component\Routing\RouterInterface;
       
    19 
       
    20 /**
       
    21  * Encapsulates the logic needed to create sub-requests, redirect the user, and match URLs.
       
    22  *
       
    23  * @author Fabien Potencier <fabien@symfony.com>
       
    24  */
       
    25 class HttpUtils
       
    26 {
       
    27     private $router;
       
    28 
       
    29     /**
       
    30      * Constructor.
       
    31      *
       
    32      * @param RouterInterface $router An RouterInterface instance
       
    33      */
       
    34     public function __construct(RouterInterface $router = null)
       
    35     {
       
    36         $this->router = $router;
       
    37     }
       
    38 
       
    39     /**
       
    40      * Creates a redirect Response.
       
    41      *
       
    42      * @param Request $request A Request instance
       
    43      * @param string  $path    A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo))
       
    44      * @param integer $status  The status code
       
    45      *
       
    46      * @return Response A RedirectResponse instance
       
    47      */
       
    48     public function createRedirectResponse(Request $request, $path, $status = 302)
       
    49     {
       
    50         if ('/' === $path[0]) {
       
    51             $path = $request->getUriForPath($path);
       
    52         } elseif (0 !== strpos($path, 'http')) {
       
    53             $this->resetLocale($request);
       
    54             $path = $this->generateUrl($path, true);
       
    55         }
       
    56 
       
    57         return new RedirectResponse($path, $status);
       
    58     }
       
    59 
       
    60     /**
       
    61      * Creates a Request.
       
    62      *
       
    63      * @param Request $request The current Request instance
       
    64      * @param string  $path    A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo))
       
    65      *
       
    66      * @return Request A Request instance
       
    67      */
       
    68     public function createRequest(Request $request, $path)
       
    69     {
       
    70         if ($path && '/' !== $path[0] && 0 !== strpos($path, 'http')) {
       
    71             $this->resetLocale($request);
       
    72             $path = $this->generateUrl($path, true);
       
    73         }
       
    74 
       
    75         $newRequest = Request::create($path, 'get', array(), $request->cookies->all(), array(), $request->server->all());
       
    76         if ($session = $request->getSession()) {
       
    77             $newRequest->setSession($session);
       
    78         }
       
    79 
       
    80         if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
       
    81             $newRequest->attributes->set(SecurityContextInterface::AUTHENTICATION_ERROR, $request->attributes->get(SecurityContextInterface::AUTHENTICATION_ERROR));
       
    82         }
       
    83         if ($request->attributes->has(SecurityContextInterface::ACCESS_DENIED_ERROR)) {
       
    84             $newRequest->attributes->set(SecurityContextInterface::ACCESS_DENIED_ERROR, $request->attributes->get(SecurityContextInterface::ACCESS_DENIED_ERROR));
       
    85         }
       
    86         if ($request->attributes->has(SecurityContextInterface::LAST_USERNAME)) {
       
    87             $newRequest->attributes->set(SecurityContextInterface::LAST_USERNAME, $request->attributes->get(SecurityContextInterface::LAST_USERNAME));
       
    88         }
       
    89 
       
    90         return $newRequest;
       
    91     }
       
    92 
       
    93     /**
       
    94      * Checks that a given path matches the Request.
       
    95      *
       
    96      * @param Request $request A Request instance
       
    97      * @param string  $path    A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo))
       
    98      *
       
    99      * @return Boolean true if the path is the same as the one from the Request, false otherwise
       
   100      */
       
   101     public function checkRequestPath(Request $request, $path)
       
   102     {
       
   103         if ('/' !== $path[0]) {
       
   104             try {
       
   105                 $parameters = $this->router->match($request->getPathInfo());
       
   106 
       
   107                 return $path === $parameters['_route'];
       
   108             } catch (\Exception $e) {
       
   109                 return false;
       
   110             }
       
   111         }
       
   112 
       
   113         return $path === $request->getPathInfo();
       
   114     }
       
   115 
       
   116     // hack (don't have a better solution for now)
       
   117     private function resetLocale(Request $request)
       
   118     {
       
   119         $context = $this->router->getContext();
       
   120         if ($context->getParameter('_locale')) {
       
   121             return;
       
   122         }
       
   123 
       
   124         try {
       
   125             $parameters = $this->router->match($request->getPathInfo());
       
   126 
       
   127             if (isset($parameters['_locale'])) {
       
   128                 $context->setParameter('_locale', $parameters['_locale']);
       
   129             } elseif ($session = $request->getSession()) {
       
   130                 $context->setParameter('_locale', $session->getLocale());
       
   131             }
       
   132         } catch (\Exception $e) {
       
   133             // let's hope user doesn't use the locale in the path
       
   134         }
       
   135     }
       
   136 
       
   137     private function generateUrl($route, $absolute = false)
       
   138     {
       
   139         if (null === $this->router) {
       
   140             throw new \LogicException('You must provide a RouterInterface instance to be able to use routes.');
       
   141         }
       
   142 
       
   143         return $this->router->generate($route, array(), $absolute);
       
   144     }
       
   145 }