4bafdbbb28a63eb550a47ee396c720e78720569c
[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. Unlike all other RequestHandlers in the TYPO3 CMS Core, the actual logic for choosing
28 * the controller is still done inside places like each single file.
29 * This RequestHandler here serves solely to check and set up all requirements needed for a TYPO3 Backend.
30 * This class might be changed in the future.
31 *
32 * At first, this request handler serves as a replacement to typo3/init.php. It is called but does not exit
33 * so any typical script that is not dispatched, is just running through the handleRequest() method and then
34 * calls its own code.
35 *
36 * However, if a get/post parameter "route" is set, the unified Backend Routing is called and searches for a
37 * matching route inside the Router. The corresponding controller / action is called then which returns content.
38 *
39 * The following get/post parameters are evaluated here:
40 * - route
41 * - token
42 */
43 class RequestHandler implements RequestHandlerInterface
44 {
45 /**
46 * Instance of the current TYPO3 bootstrap
47 * @var Bootstrap
48 */
49 protected $bootstrap;
50
51 /**
52 * Constructor handing over the bootstrap and the original request
53 *
54 * @param Bootstrap $bootstrap
55 */
56 public function __construct(Bootstrap $bootstrap)
57 {
58 $this->bootstrap = $bootstrap;
59 }
60
61 /**
62 * Handles any backend request
63 *
64 * @param ServerRequestInterface $request
65 * @return NULL|ResponseInterface
66 */
67 public function handleRequest(ServerRequestInterface $request)
68 {
69 // enable dispatching via Request/Response logic only for typo3/index.php
70 // This fallback will be removed in TYPO3 CMS 8, as only index.php will be allowed
71 $path = substr($request->getUri()->getPath(), strlen(GeneralUtility::getIndpEnv('TYPO3_SITE_PATH')));
72 $routingEnabled = ($path === TYPO3_mainDir . 'index.php' || $path === TYPO3_mainDir);
73 $proceedIfNoUserIsLoggedIn = false;
74
75 if ($routingEnabled) {
76 $pathToRoute = (string)$request->getQueryParams()['route'];
77 // Allow the login page to be displayed if routing is not used and on index.php
78 if (empty($pathToRoute)) {
79 $pathToRoute = '/login';
80 }
81 $request = $request->withAttribute('routePath', $pathToRoute);
82
83 // Evaluate the constant for skipping the BE user check for the bootstrap
84 // should be handled differently in the future by checking the Bootstrap directly
85 if ($pathToRoute === '/login') {
86 $proceedIfNoUserIsLoggedIn = true;
87 }
88 }
89
90 $this->boot($proceedIfNoUserIsLoggedIn);
91
92 // Check if the router has the available route and dispatch.
93 if ($routingEnabled) {
94 return $this->dispatch($request);
95 }
96
97 // No route found, so the system proceeds in called entrypoint as fallback.
98 return null;
99 }
100
101 /**
102 * Does the main work for setting up the backend environment for any Backend request
103 *
104 * @param bool $proceedIfNoUserIsLoggedIn option to allow to render the request even if no user is logged in
105 * @return void
106 */
107 protected function boot($proceedIfNoUserIsLoggedIn)
108 {
109 $this->bootstrap
110 ->checkLockedBackendAndRedirectOrDie()
111 ->checkBackendIpOrDie()
112 ->checkSslBackendAndRedirectIfNeeded()
113 ->initializeBackendRouter()
114 ->loadExtensionTables(true)
115 ->initializeSpriteManager()
116 ->initializeBackendUser()
117 ->initializeBackendAuthentication($proceedIfNoUserIsLoggedIn)
118 ->initializeLanguageObject()
119 ->initializeBackendTemplate()
120 ->endOutputBufferingAndCleanPreviousOutput()
121 ->initializeOutputCompression()
122 ->sendHttpHeaders();
123 }
124
125 /**
126 * This request handler can handle any backend request (but not CLI).
127 *
128 * @param ServerRequestInterface $request
129 * @return bool If the request is not a CLI script, TRUE otherwise FALSE
130 */
131 public function canHandleRequest(ServerRequestInterface $request)
132 {
133 return (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_BE && !(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI));
134 }
135
136 /**
137 * Returns the priority - how eager the handler is to actually handle the
138 * request.
139 *
140 * @return int The priority of the request handler.
141 */
142 public function getPriority()
143 {
144 return 50;
145 }
146
147 /**
148 * Dispatch the request to the appropriate controller through the Backend Dispatcher which resolves the routing
149 *
150 * @param ServerRequestInterface $request
151 * @return ResponseInterface
152 * @throws RouteNotFoundException when no route is registered
153 * @throws \InvalidArgumentException when a route is found but the target of the route cannot be called
154 */
155 protected function dispatch($request)
156 {
157 /** @var Response $response */
158 $response = GeneralUtility::makeInstance(Response::class);
159 /** @var RouteDispatcher $dispatcher */
160 $dispatcher = GeneralUtility::makeInstance(RouteDispatcher::class);
161 return $dispatcher->dispatch($request, $response);
162 }
163 }