[TASK] Add more fixers for php-cs-fixer
[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\RouteNotFoundException;
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 return $this->dispatch($request);
72 }
73
74 /**
75 * Does the main work for setting up the backend environment for any Backend request
76 *
77 * @param bool $proceedIfNoUserIsLoggedIn option to allow to render the request even if no user is logged in
78 * @return void
79 */
80 protected function boot($proceedIfNoUserIsLoggedIn)
81 {
82 $this->bootstrap
83 ->checkLockedBackendAndRedirectOrDie()
84 ->checkBackendIpOrDie()
85 ->checkSslBackendAndRedirectIfNeeded()
86 ->initializeBackendRouter()
87 ->loadExtensionTables(true)
88 ->initializeBackendUser()
89 ->initializeBackendAuthentication($proceedIfNoUserIsLoggedIn)
90 ->initializeLanguageObject()
91 ->initializeBackendTemplate()
92 ->endOutputBufferingAndCleanPreviousOutput()
93 ->initializeOutputCompression()
94 ->sendHttpHeaders();
95 }
96
97 /**
98 * This request handler can handle any backend request (but not CLI).
99 *
100 * @param ServerRequestInterface $request
101 * @return bool If the request is not a CLI script, TRUE otherwise FALSE
102 */
103 public function canHandleRequest(ServerRequestInterface $request)
104 {
105 return TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_BE && !(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI);
106 }
107
108 /**
109 * Returns the priority - how eager the handler is to actually handle the
110 * request.
111 *
112 * @return int The priority of the request handler.
113 */
114 public function getPriority()
115 {
116 return 50;
117 }
118
119 /**
120 * Dispatch the request to the appropriate controller through the Backend Dispatcher which resolves the routing
121 *
122 * @param ServerRequestInterface $request
123 * @return ResponseInterface
124 * @throws RouteNotFoundException when no route is registered
125 * @throws \InvalidArgumentException when a route is found but the target of the route cannot be called
126 */
127 protected function dispatch($request)
128 {
129 /** @var Response $response */
130 $response = GeneralUtility::makeInstance(Response::class);
131 /** @var RouteDispatcher $dispatcher */
132 $dispatcher = GeneralUtility::makeInstance(RouteDispatcher::class);
133 return $dispatcher->dispatch($request, $response);
134 }
135 }