[TASK] Bootstrap: Load TCA configuration on every request
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Http / RequestHandler.php
1 <?php
2 namespace TYPO3\CMS\Backend\Http;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Routing\Exception\InvalidRequestTokenException;
20 use TYPO3\CMS\Core\Core\Bootstrap;
21 use TYPO3\CMS\Core\Http\RequestHandlerInterface;
22 use TYPO3\CMS\Core\Http\Response;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * General RequestHandler for the TYPO3 Backend. This is used for all Backend requests except for CLI
27 * or AJAX calls.
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 * Instance of the current TYPO3 bootstrap
40 * @var Bootstrap
41 */
42 protected $bootstrap;
43
44 /**
45 * Constructor handing over the bootstrap and the original request
46 *
47 * @param Bootstrap $bootstrap
48 */
49 public function __construct(Bootstrap $bootstrap)
50 {
51 $this->bootstrap = $bootstrap;
52 }
53
54 /**
55 * Handles any backend request
56 *
57 * @param ServerRequestInterface $request
58 * @return ResponseInterface
59 */
60 public function handleRequest(ServerRequestInterface $request)
61 {
62 // Allow the login page to be displayed if routing is not used and on index.php
63 $pathToRoute = (string)$request->getQueryParams()['route'] ?: '/login';
64 $request = $request->withAttribute('routePath', $pathToRoute);
65
66 // skip the BE user check on the login page
67 // should be handled differently in the future by checking the Bootstrap directly
68 $this->boot($pathToRoute === '/login');
69
70 // Check if the router has the available route and dispatch.
71 try {
72 return $this->dispatch($request);
73
74 // When token was invalid redirect to login
75 } catch (InvalidRequestTokenException $e) {
76 $url = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . TYPO3_mainDir;
77 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($url);
78 }
79 }
80
81 /**
82 * Does the main work for setting up the backend environment for any Backend request
83 *
84 * @param bool $proceedIfNoUserIsLoggedIn option to allow to render the request even if no user is logged in
85 */
86 protected function boot($proceedIfNoUserIsLoggedIn)
87 {
88 $this->bootstrap
89 ->checkLockedBackendAndRedirectOrDie()
90 ->checkBackendIpOrDie()
91 ->checkSslBackendAndRedirectIfNeeded()
92 ->initializeBackendRouter()
93 ->loadExtTables()
94 ->initializeBackendUser()
95 ->initializeBackendAuthentication($proceedIfNoUserIsLoggedIn)
96 ->initializeLanguageObject()
97 ->initializeBackendTemplate()
98 ->endOutputBufferingAndCleanPreviousOutput()
99 ->initializeOutputCompression()
100 ->sendHttpHeaders();
101 }
102
103 /**
104 * This request handler can handle any backend request (but not CLI).
105 *
106 * @param ServerRequestInterface $request
107 * @return bool If the request is not a CLI script, TRUE otherwise FALSE
108 */
109 public function canHandleRequest(ServerRequestInterface $request)
110 {
111 return TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_BE && !(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI);
112 }
113
114 /**
115 * Returns the priority - how eager the handler is to actually handle the
116 * request.
117 *
118 * @return int The priority of the request handler.
119 */
120 public function getPriority()
121 {
122 return 50;
123 }
124
125 /**
126 * Dispatch the request to the appropriate controller through the Backend Dispatcher which resolves the routing
127 *
128 * @param ServerRequestInterface $request
129 * @return ResponseInterface
130 * @throws InvalidRequestTokenException if the request could not be verified
131 * @throws \InvalidArgumentException when a route is found but the target of the route cannot be called
132 */
133 protected function dispatch($request)
134 {
135 /** @var Response $response */
136 $response = GeneralUtility::makeInstance(Response::class);
137 /** @var RouteDispatcher $dispatcher */
138 $dispatcher = GeneralUtility::makeInstance(RouteDispatcher::class);
139 return $dispatcher->dispatch($request, $response);
140 }
141 }