[TASK] Bootstrap: Load TCA configuration on every request
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Http / RequestHandler.php
1 <?php
2 namespace TYPO3\CMS\Frontend\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\Backend\FrontendBackendUserAuthentication;
18 use TYPO3\CMS\Core\Core\Bootstrap;
19 use TYPO3\CMS\Core\FrontendEditing\FrontendEditingController;
20 use TYPO3\CMS\Core\Http\RequestHandlerInterface;
21 use TYPO3\CMS\Core\TimeTracker\TimeTracker;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Core\Utility\MathUtility;
24 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
25 use TYPO3\CMS\Frontend\Page\PageGenerator;
26 use TYPO3\CMS\Frontend\Utility\CompressionUtility;
27 use TYPO3\CMS\Frontend\View\AdminPanelView;
28
29 /**
30 * This is the main entry point of the TypoScript driven standard front-end
31 *
32 * Basically put, this is the script which all requests for TYPO3 delivered pages goes to in the
33 * frontend (the website). The script instantiates a $TSFE object, includes libraries and does a little logic here
34 * and there in order to instantiate the right classes to create the webpage.
35 * Previously, this was called index_ts.php and also included the logic for the lightweight "eID" concept,
36 * which is now handled in a separate request handler (EidRequestHandler).
37 */
38 class RequestHandler implements RequestHandlerInterface
39 {
40 /**
41 * Instance of the current TYPO3 bootstrap
42 * @var Bootstrap
43 */
44 protected $bootstrap;
45
46 /**
47 * Instance of the timetracker
48 * @var TimeTracker
49 */
50 protected $timeTracker;
51
52 /**
53 * Instance of the TSFE object
54 * @var TypoScriptFrontendController
55 */
56 protected $controller;
57
58 /**
59 * The request handed over
60 * @var \Psr\Http\Message\ServerRequestInterface
61 */
62 protected $request;
63
64 /**
65 * Constructor handing over the bootstrap and the original request
66 *
67 * @param Bootstrap $bootstrap
68 */
69 public function __construct(Bootstrap $bootstrap)
70 {
71 $this->bootstrap = $bootstrap;
72 }
73
74 /**
75 * Handles a frontend request
76 *
77 * @param \Psr\Http\Message\ServerRequestInterface $request
78 * @return NULL|\Psr\Http\Message\ResponseInterface
79 */
80 public function handleRequest(\Psr\Http\Message\ServerRequestInterface $request)
81 {
82 $response = null;
83 $this->request = $request;
84 $this->initializeTimeTracker();
85
86 // Hook to preprocess the current request:
87 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/index_ts.php']['preprocessRequest'])) {
88 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/index_ts.php']['preprocessRequest'] as $hookFunction) {
89 $hookParameters = [];
90 GeneralUtility::callUserFunction($hookFunction, $hookParameters, $hookParameters);
91 }
92 unset($hookFunction);
93 unset($hookParameters);
94 }
95
96 $this->initializeController();
97
98 if ($GLOBALS['TYPO3_CONF_VARS']['FE']['pageUnavailable_force']
99 && !GeneralUtility::cmpIP(
100 GeneralUtility::getIndpEnv('REMOTE_ADDR'),
101 $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask'])
102 ) {
103 $this->controller->pageUnavailableAndExit('This page is temporarily unavailable.');
104 }
105
106 $this->controller->connectToDB();
107 $this->controller->sendRedirect();
108
109 // Output compression
110 // Remove any output produced until now
111 $this->bootstrap->endOutputBufferingAndCleanPreviousOutput();
112 $this->initializeOutputCompression();
113
114 // Initializing the Frontend User
115 $this->timeTracker->push('Front End user initialized', '');
116 $this->controller->initFEuser();
117 $this->timeTracker->pull();
118
119 // Initializing a possible logged-in Backend User
120 /** @var $GLOBALS['BE_USER'] \TYPO3\CMS\Backend\FrontendBackendUserAuthentication */
121 $GLOBALS['BE_USER'] = $this->controller->initializeBackendUser();
122
123 // Process the ID, type and other parameters.
124 // After this point we have an array, $page in TSFE, which is the page-record
125 // of the current page, $id.
126 $this->timeTracker->push('Process ID', '');
127 // Initialize admin panel since simulation settings are required here:
128 if ($this->controller->isBackendUserLoggedIn()) {
129 $GLOBALS['BE_USER']->initializeAdminPanel();
130 $this->bootstrap
131 ->initializeBackendRouter()
132 ->loadExtTables();
133 }
134 $this->controller->checkAlternativeIdMethods();
135 $this->controller->clear_preview();
136 $this->controller->determineId();
137
138 // Now, if there is a backend user logged in and he has NO access to this page,
139 // then re-evaluate the id shown! _GP('ADMCMD_noBeUser') is placed here because
140 // \TYPO3\CMS\Version\Hook\PreviewHook might need to know if a backend user is logged in.
141 if (
142 $this->controller->isBackendUserLoggedIn()
143 && (!$GLOBALS['BE_USER']->extPageReadAccess($this->controller->page) || GeneralUtility::_GP('ADMCMD_noBeUser'))
144 ) {
145 // Remove user
146 unset($GLOBALS['BE_USER']);
147 $this->controller->beUserLogin = false;
148 // Re-evaluate the page-id.
149 $this->controller->checkAlternativeIdMethods();
150 $this->controller->clear_preview();
151 $this->controller->determineId();
152 }
153
154 $this->controller->makeCacheHash();
155 $this->timeTracker->pull();
156
157 // Admin Panel & Frontend editing
158 if ($this->controller->isBackendUserLoggedIn()) {
159 $GLOBALS['BE_USER']->initializeFrontendEdit();
160 if ($GLOBALS['BE_USER']->adminPanel instanceof AdminPanelView) {
161 $this->bootstrap->initializeLanguageObject();
162 }
163 if ($GLOBALS['BE_USER']->frontendEdit instanceof FrontendEditingController) {
164 $GLOBALS['BE_USER']->frontendEdit->initConfigOptions();
165 }
166 }
167
168 // Starts the template
169 $this->timeTracker->push('Start Template', '');
170 $this->controller->initTemplate();
171 $this->timeTracker->pull();
172 // Get from cache
173 $this->timeTracker->push('Get Page from cache', '');
174 $this->controller->getFromCache();
175 $this->timeTracker->pull();
176 // Get config if not already gotten
177 // After this, we should have a valid config-array ready
178 $this->controller->getConfigArray();
179 // Setting language and locale
180 $this->timeTracker->push('Setting language and locale', '');
181 $this->controller->settingLanguage();
182 $this->controller->settingLocale();
183 $this->timeTracker->pull();
184
185 // Convert POST data to utf-8 for internal processing if metaCharset is different
186 $this->controller->convPOSTCharset();
187
188 $this->controller->initializeRedirectUrlHandlers();
189
190 $this->controller->handleDataSubmission();
191
192 // Check for shortcut page and redirect
193 $this->controller->checkPageForShortcutRedirect();
194 $this->controller->checkPageForMountpointRedirect();
195
196 // Generate page
197 $this->controller->setUrlIdToken();
198 $this->timeTracker->push('Page generation', '');
199 if ($this->controller->isGeneratePage()) {
200 $this->controller->generatePage_preProcessing();
201 $this->controller->preparePageContentGeneration();
202 // Content generation
203 if (!$this->controller->isINTincScript()) {
204 PageGenerator::renderContent();
205 $this->controller->setAbsRefPrefix();
206 }
207 $this->controller->generatePage_postProcessing();
208 } elseif ($this->controller->isINTincScript()) {
209 $this->controller->preparePageContentGeneration();
210 }
211 $this->controller->releaseLocks();
212 $this->timeTracker->pull();
213
214 // Render non-cached parts
215 if ($this->controller->isINTincScript()) {
216 $this->timeTracker->push('Non-cached objects', '');
217 $this->controller->INTincScript();
218 $this->timeTracker->pull();
219 }
220
221 // Output content
222 $sendTSFEContent = false;
223 if ($this->controller->isOutputting()) {
224 $this->timeTracker->push('Print Content', '');
225 $this->controller->processOutput();
226 $sendTSFEContent = true;
227 $this->timeTracker->pull();
228 }
229 // Store session data for fe_users
230 $this->controller->storeSessionData();
231 // Statistics
232 $GLOBALS['TYPO3_MISC']['microtime_end'] = microtime(true);
233 if ($this->controller->isOutputting()) {
234 if (isset($this->controller->config['config']['debug'])) {
235 $debugParseTime = (bool)$this->controller->config['config']['debug'];
236 } else {
237 $debugParseTime = !empty($GLOBALS['TYPO3_CONF_VARS']['FE']['debug']);
238 }
239 if ($debugParseTime) {
240 $this->controller->content .= LF . '<!-- Parsetime: ' . $this->timeTracker->getParseTime() . 'ms -->';
241 }
242 }
243 $this->controller->redirectToExternalUrl();
244 // Preview info
245 $this->controller->previewInfo();
246 // Hook for end-of-frontend
247 $this->controller->hook_eofe();
248 // Finish timetracking
249 $this->timeTracker->pull();
250
251 // Admin panel
252 if ($this->controller->isBackendUserLoggedIn() && $GLOBALS['BE_USER'] instanceof FrontendBackendUserAuthentication) {
253 if ($GLOBALS['BE_USER']->isAdminPanelVisible()) {
254 $this->controller->content = str_ireplace('</body>', $GLOBALS['BE_USER']->displayAdminPanel() . '</body>', $this->controller->content);
255 }
256 }
257
258 if ($sendTSFEContent) {
259 /** @var \TYPO3\CMS\Core\Http\Response $response */
260 $response = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Http\Response::class);
261 $response->getBody()->write($this->controller->content);
262 }
263 // Debugging Output
264 if (isset($GLOBALS['error']) && is_object($GLOBALS['error']) && @is_callable([$GLOBALS['error'], 'debugOutput'])) {
265 $GLOBALS['error']->debugOutput();
266 }
267 if (TYPO3_DLOG) {
268 GeneralUtility::devLog('END of FRONTEND session', 'cms', 0, ['_FLUSH' => true]);
269 }
270 return $response;
271 }
272
273 /**
274 * This request handler can handle any frontend request.
275 *
276 * @param \Psr\Http\Message\ServerRequestInterface $request
277 * @return bool If the request is not an eID request, TRUE otherwise FALSE
278 */
279 public function canHandleRequest(\Psr\Http\Message\ServerRequestInterface $request)
280 {
281 return $request->getQueryParams()['eID'] || $request->getParsedBody()['eID'] ? false : true;
282 }
283
284 /**
285 * Returns the priority - how eager the handler is to actually handle the
286 * request.
287 *
288 * @return int The priority of the request handler.
289 */
290 public function getPriority()
291 {
292 return 50;
293 }
294
295 /**
296 * Initializes output compression when enabled, could be split up and put into Bootstrap
297 * at a later point
298 */
299 protected function initializeOutputCompression()
300 {
301 if ($GLOBALS['TYPO3_CONF_VARS']['FE']['compressionLevel'] && extension_loaded('zlib')) {
302 if (MathUtility::canBeInterpretedAsInteger($GLOBALS['TYPO3_CONF_VARS']['FE']['compressionLevel'])) {
303 @ini_set('zlib.output_compression_level', $GLOBALS['TYPO3_CONF_VARS']['FE']['compressionLevel']);
304 }
305 ob_start([GeneralUtility::makeInstance(CompressionUtility::class), 'compressionOutputHandler']);
306 }
307 }
308
309 /**
310 * Timetracking started depending if a Backend User is logged in
311 */
312 protected function initializeTimeTracker()
313 {
314 $configuredCookieName = trim($GLOBALS['TYPO3_CONF_VARS']['BE']['cookieName']) ?: 'be_typo_user';
315
316 /** @var TimeTracker timeTracker */
317 $this->timeTracker = GeneralUtility::makeInstance(TimeTracker::class, ($this->request->getCookieParams()[$configuredCookieName] ? true : false));
318 $this->timeTracker->start();
319 }
320
321 /**
322 * Creates an instance of TSFE and sets it as a global variable
323 */
324 protected function initializeController()
325 {
326 $this->controller = GeneralUtility::makeInstance(
327 TypoScriptFrontendController::class,
328 null,
329 GeneralUtility::_GP('id'),
330 GeneralUtility::_GP('type'),
331 GeneralUtility::_GP('no_cache'),
332 GeneralUtility::_GP('cHash'),
333 null,
334 GeneralUtility::_GP('MP'),
335 GeneralUtility::_GP('RDCT')
336 );
337 // setting the global variable for the controller
338 // We have to define this as reference here, because there is code around
339 // which exchanges the TSFE object in the global variable. The reference ensures
340 // that the $controller member always works on the same object as the global variable.
341 // This is a dirty workaround and bypasses the protected access modifier of the controller member.
342 $GLOBALS['TSFE'] = &$this->controller;
343 }
344 }