[TASK] Deprecate various TSFE methods
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Middleware / PrepareTypoScriptFrontendRendering.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Frontend\Middleware;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
21 use Psr\Http\Server\MiddlewareInterface;
22 use Psr\Http\Server\RequestHandlerInterface as PsrRequestHandlerInterface;
23 use TYPO3\CMS\Core\TimeTracker\TimeTracker;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
26
27 /**
28 * Initialization of TypoScriptFrontendController
29 *
30 * Do all necessary preparation steps for rendering
31 */
32 class PrepareTypoScriptFrontendRendering implements MiddlewareInterface
33 {
34 /**
35 * @var TypoScriptFrontendController
36 */
37 protected $controller;
38
39 /**
40 * @var TimeTracker
41 */
42 protected $timeTracker;
43
44 public function __construct(TypoScriptFrontendController $controller = null, TimeTracker $timeTracker = null)
45 {
46 $this->controller = $controller ?: $GLOBALS['TSFE'];
47 $this->timeTracker = $timeTracker ?: GeneralUtility::makeInstance(TimeTracker::class);
48 }
49
50 /**
51 * Initialize TypoScriptFrontendController to the point right before rendering of the page is triggered
52 *
53 * @param ServerRequestInterface $request
54 * @param PsrRequestHandlerInterface $handler
55 * @return ResponseInterface
56 */
57 public function process(ServerRequestInterface $request, PsrRequestHandlerInterface $handler): ResponseInterface
58 {
59 // Starts the template
60 $this->timeTracker->push('Start Template');
61 $this->controller->initTemplate();
62 $this->timeTracker->pull();
63 // Get from cache
64 $this->timeTracker->push('Get Page from cache');
65 // Locks may be acquired here
66 $this->controller->getFromCache();
67 $this->timeTracker->pull();
68 // Get config if not already gotten
69 // After this, we should have a valid config-array ready
70 $this->controller->getConfigArray();
71 // Setting language and locale
72 $this->timeTracker->push('Setting language and locale');
73 $this->controller->settingLanguage();
74 $this->controller->settingLocale();
75 $this->timeTracker->pull();
76
77 // Convert POST data to utf-8 for internal processing if metaCharset is different
78 if ($this->controller->metaCharset !== 'utf-8' && is_array($_POST) && !empty($_POST)) {
79 $this->convertCharsetRecursivelyToUtf8($_POST, $this->controller->metaCharset);
80 $GLOBALS['HTTP_POST_VARS'] = $_POST;
81 $parsedBody = $request->getParsedBody();
82 $this->convertCharsetRecursivelyToUtf8($parsedBody, $this->controller->metaCharset);
83 $request = $request->withParsedBody($parsedBody);
84 $GLOBALS['TYPO3_REQUEST'] = $request;
85 }
86
87 $this->controller->initializeRedirectUrlHandlers();
88
89 // Hook for processing data submission to extensions
90 // This is done at this point, because we need the config values
91 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['checkDataSubmission'] ?? [] as $className) {
92 GeneralUtility::makeInstance($className)->checkDataSubmission($this->controller);
93 }
94
95 return $handler->handle($request);
96 }
97
98 /**
99 * Small helper function to convert charsets for arrays to UTF-8
100 *
101 * @param mixed $data given by reference (string/array usually)
102 * @param string $fromCharset convert FROM this charset
103 */
104 protected function convertCharsetRecursivelyToUtf8(&$data, string $fromCharset)
105 {
106 foreach ($data as $key => $value) {
107 if (is_array($data[$key])) {
108 $this->convertCharsetRecursivelyToUtf8($data[$key], $fromCharset);
109 } elseif (is_string($data[$key])) {
110 $data[$key] = mb_convert_encoding($data[$key], 'utf-8', $fromCharset);
111 }
112 }
113 }
114 }