HttpUtils.php 4.9 KB

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