[FEATURE] Add symfony dependency injection for core and extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Http / RequestHandler.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Http;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use Psr\Http\Server\RequestHandlerInterface;
21 use TYPO3\CMS\Backend\Routing\Exception\InvalidRequestTokenException;
22 use TYPO3\CMS\Backend\Routing\UriBuilder;
23 use TYPO3\CMS\Core\Http\RedirectResponse;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 /**
27 * General RequestHandler for the TYPO3 Backend. This is used for all Backend requests, including AJAX routes.
28 *
29 * If a get/post parameter "route" is set, the Backend Routing is called and searches for a
30 * matching route inside the Router. The corresponding controller / action is called then which returns the response.
31 *
32 * The following get/post parameters are evaluated here:
33 * - route
34 * - token
35 */
36 class RequestHandler implements RequestHandlerInterface
37 {
38 /**
39 * @var RouteDispatcher
40 */
41 protected $dispatcher;
42
43 /**
44 * @param RouteDispatcher $dispatcher
45 */
46 public function __construct(RouteDispatcher $dispatcher)
47 {
48 $this->dispatcher = $dispatcher;
49 }
50
51 /**
52 * Sets the global GET and POST to the values, so if people access $_GET and $_POST
53 * Within hooks starting NOW (e.g. cObject), they get the "enriched" data from query params.
54 *
55 * This needs to be run after the request object has been enriched with modified GET/POST variables.
56 *
57 * @param ServerRequestInterface $request
58 * @internal this safety net will be removed in TYPO3 v10.0.
59 */
60 protected function resetGlobalsToCurrentRequest(ServerRequestInterface $request)
61 {
62 if ($request->getQueryParams() !== $_GET) {
63 $queryParams = $request->getQueryParams();
64 $_GET = $queryParams;
65 $GLOBALS['HTTP_GET_VARS'] = $_GET;
66 }
67 if ($request->getMethod() === 'POST') {
68 $parsedBody = $request->getParsedBody();
69 if (is_array($parsedBody) && $parsedBody !== $_POST) {
70 $_POST = $parsedBody;
71 $GLOBALS['HTTP_POST_VARS'] = $_POST;
72 }
73 }
74 $GLOBALS['TYPO3_REQUEST'] = $request;
75 }
76
77 /**
78 * Handles a backend request, after finishing running middlewares
79 * Dispatch the request to the appropriate controller through the
80 * Backend Dispatcher which resolves the routing
81 *
82 * @param ServerRequestInterface $request
83 * @return ResponseInterface
84 */
85 public function handle(ServerRequestInterface $request): ResponseInterface
86 {
87 // safety net to have the fully-added request object globally available as long as
88 // there are Core classes that need the Request object but do not get it handed in
89 $this->resetGlobalsToCurrentRequest($request);
90 try {
91 // Check if the router has the available route and dispatch.
92 return $this->dispatcher->dispatch($request);
93 } catch (InvalidRequestTokenException $e) {
94 // When token was invalid redirect to login
95 $loginPage = GeneralUtility::makeInstance(UriBuilder::class)->buildUriFromRoute('login');
96 return new RedirectResponse((string)$loginPage);
97 }
98 }
99 }