[FEATURE] Introduce Request/Response based on PSR-7
[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 TYPO3\CMS\Core\Core\Bootstrap;
18 use TYPO3\CMS\Core\Core\RequestHandlerInterface;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * General RequestHandler for the TYPO3 Backend. This is used for all Backend requests except for CLI
23 * or AJAX calls. Unlike all other RequestHandlers in the TYPO3 CMS Core, the actual logic for choosing
24 * the controller is still done inside places like mod.php and each single file.
25 * This RequestHandler here serves solely to check and set up all requirements needed for a TYPO3 Backend.
26 * This class might be changed in the future.
27 */
28 class RequestHandler implements RequestHandlerInterface {
29
30 /**
31 * Instance of the current TYPO3 bootstrap
32 * @var Bootstrap
33 */
34 protected $bootstrap;
35
36 /**
37 * Constructor handing over the bootstrap and the original request
38 *
39 * @param Bootstrap $bootstrap
40 */
41 public function __construct(Bootstrap $bootstrap) {
42 $this->bootstrap = $bootstrap;
43 }
44
45 /**
46 * Handles any backend request
47 *
48 * @param \Psr\Http\Message\ServerRequestInterface $request
49 * @return NULL|\Psr\Http\Message\ResponseInterface
50 */
51 public function handleRequest(\Psr\Http\Message\ServerRequestInterface $request) {
52 // enable dispatching via Request/Response logic only for typo3/index.php currently
53 $path = substr($request->getUri()->getPath(), strlen(GeneralUtility::getIndpEnv('TYPO3_SITE_PATH')));
54 $routingEnabled = ($path === TYPO3_mainDir . 'index.php' || $path === TYPO3_mainDir);
55
56 // Evaluate the constant for skipping the BE user check for the bootstrap
57 if (defined('TYPO3_PROCEED_IF_NO_USER') && TYPO3_PROCEED_IF_NO_USER) {
58 $proceedIfNoUserIsLoggedIn = TRUE;
59 } else {
60 $proceedIfNoUserIsLoggedIn = FALSE;
61 }
62
63 $this->bootstrap
64 ->checkLockedBackendAndRedirectOrDie()
65 ->checkBackendIpOrDie()
66 ->checkSslBackendAndRedirectIfNeeded()
67 ->checkValidBrowserOrDie()
68 ->loadExtensionTables(TRUE)
69 ->initializeSpriteManager()
70 ->initializeBackendUser()
71 ->initializeBackendAuthentication($proceedIfNoUserIsLoggedIn)
72 ->initializeLanguageObject()
73 ->initializeBackendTemplate()
74 ->endOutputBufferingAndCleanPreviousOutput()
75 ->initializeOutputCompression()
76 ->sendHttpHeaders();
77
78 if ($routingEnabled) {
79 return $this->dispatch($request);
80 }
81 return NULL;
82 }
83
84 /**
85 * This request handler can handle any backend request (but not CLI).
86 *
87 * @param \Psr\Http\Message\ServerRequestInterface $request
88 * @return bool If the request is not a CLI script, TRUE otherwise FALSE
89 */
90 public function canHandleRequest(\Psr\Http\Message\ServerRequestInterface $request) {
91 return (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_BE && !(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI));
92 }
93
94 /**
95 * Returns the priority - how eager the handler is to actually handle the
96 * request.
97 *
98 * @return int The priority of the request handler.
99 */
100 public function getPriority() {
101 return 50;
102 }
103
104 /**
105 * Dispatch the request to the appropriate controller, will go to a proper dispatcher/router class in the future
106 *
107 * @internal
108 * @param \Psr\Http\Message\RequestInterface $request
109 * @return NULL|\Psr\Http\Message\ResponseInterface
110 */
111 protected function dispatch($request) {
112 $controller = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Controller\LoginController::class);
113 if ($controller instanceof \TYPO3\CMS\Core\Http\ControllerInterface) {
114 return $controller->processRequest($request);
115 }
116 return NULL;
117 }
118 }