/PHP/Test/vendor/symfony/symfony/src/Symfony/Component/Security/Http/HttpUtils.php

https://bitbucket.org/AdriVanHoudt/school · PHP · 145 lines · 75 code · 17 blank · 53 comment · 14 complexity · 5f61d90654c683717dd17151a9f772e8 MD5 · raw file

  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\Matcher\UrlMatcherInterface;
  15. use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  18. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  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. /**
  29. * Constructor.
  30. *
  31. * @param UrlGeneratorInterface $urlGenerator A UrlGeneratorInterface instance
  32. * @param UrlMatcherInterface|RequestMatcherInterface $urlMatcher The Url or Request matcher
  33. */
  34. public function __construct(UrlGeneratorInterface $urlGenerator = null, $urlMatcher = null)
  35. {
  36. $this->urlGenerator = $urlGenerator;
  37. if ($urlMatcher !== null && !$urlMatcher instanceof UrlMatcherInterface && !$urlMatcher instanceof RequestMatcherInterface) {
  38. throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
  39. }
  40. $this->urlMatcher = $urlMatcher;
  41. }
  42. /**
  43. * Creates a redirect Response.
  44. *
  45. * @param Request $request A Request instance
  46. * @param string $path A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo))
  47. * @param integer $status The status code
  48. *
  49. * @return Response A RedirectResponse instance
  50. */
  51. public function createRedirectResponse(Request $request, $path, $status = 302)
  52. {
  53. return new RedirectResponse($this->generateUri($request, $path), $status);
  54. }
  55. /**
  56. * Creates a Request.
  57. *
  58. * @param Request $request The current Request instance
  59. * @param string $path A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo))
  60. *
  61. * @return Request A Request instance
  62. */
  63. public function createRequest(Request $request, $path)
  64. {
  65. $newRequest = Request::create($this->generateUri($request, $path), 'get', array(), $request->cookies->all(), array(), $request->server->all());
  66. if ($request->hasSession()) {
  67. $newRequest->setSession($request->getSession());
  68. }
  69. if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
  70. $newRequest->attributes->set(SecurityContextInterface::AUTHENTICATION_ERROR, $request->attributes->get(SecurityContextInterface::AUTHENTICATION_ERROR));
  71. }
  72. if ($request->attributes->has(SecurityContextInterface::ACCESS_DENIED_ERROR)) {
  73. $newRequest->attributes->set(SecurityContextInterface::ACCESS_DENIED_ERROR, $request->attributes->get(SecurityContextInterface::ACCESS_DENIED_ERROR));
  74. }
  75. if ($request->attributes->has(SecurityContextInterface::LAST_USERNAME)) {
  76. $newRequest->attributes->set(SecurityContextInterface::LAST_USERNAME, $request->attributes->get(SecurityContextInterface::LAST_USERNAME));
  77. }
  78. return $newRequest;
  79. }
  80. /**
  81. * Checks that a given path matches the Request.
  82. *
  83. * @param Request $request A Request instance
  84. * @param string $path A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo))
  85. *
  86. * @return Boolean true if the path is the same as the one from the Request, false otherwise
  87. */
  88. public function checkRequestPath(Request $request, $path)
  89. {
  90. if ('/' !== $path[0]) {
  91. try {
  92. // matching a request is more powerful than matching a URL path + context, so try that first
  93. if ($this->urlMatcher instanceof RequestMatcherInterface) {
  94. $parameters = $this->urlMatcher->matchRequest($request);
  95. } else {
  96. $parameters = $this->urlMatcher->match($request->getPathInfo());
  97. }
  98. return $path === $parameters['_route'];
  99. } catch (MethodNotAllowedException $e) {
  100. return false;
  101. } catch (ResourceNotFoundException $e) {
  102. return false;
  103. }
  104. }
  105. return $path === rawurldecode($request->getPathInfo());
  106. }
  107. /**
  108. * Generates a URI, based on the given path or absolute URL.
  109. *
  110. * @param Request $request A Request instance
  111. * @param string $path A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo))
  112. *
  113. * @return string An absolute URL
  114. */
  115. public function generateUri($request, $path)
  116. {
  117. if (0 === strpos($path, 'http') || !$path) {
  118. return $path;
  119. }
  120. if ('/' === $path[0]) {
  121. return $request->getUriForPath($path);
  122. }
  123. if (null === $this->urlGenerator) {
  124. throw new \LogicException('You must provide a UrlGeneratorInterface instance to be able to use routes.');
  125. }
  126. return $this->urlGenerator->generate($path, array(), UrlGeneratorInterface::ABSOLUTE_URL);
  127. }
  128. }