ff6388a93a1c69dbd5e44354881f014c1278a434
[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\ArrayUtility;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
27
28 /**
29 * Initialization of TypoScriptFrontendController
30 *
31 * Do all necessary preparation steps for rendering
32 */
33 class PrepareTypoScriptFrontendRendering implements MiddlewareInterface
34 {
35 /**
36 * @var TypoScriptFrontendController
37 */
38 protected $controller;
39
40 /**
41 * @var TimeTracker
42 */
43 protected $timeTracker;
44
45 public function __construct(TypoScriptFrontendController $controller = null, TimeTracker $timeTracker = null)
46 {
47 $this->controller = $controller ?: $GLOBALS['TSFE'];
48 $this->timeTracker = $timeTracker ?: GeneralUtility::makeInstance(TimeTracker::class);
49 }
50
51 /**
52 * Initialize TypoScriptFrontendController to the point right before rendering of the page is triggered
53 *
54 * @param ServerRequestInterface $request
55 * @param PsrRequestHandlerInterface $handler
56 * @return ResponseInterface
57 */
58 public function process(ServerRequestInterface $request, PsrRequestHandlerInterface $handler): ResponseInterface
59 {
60 // Get from cache
61 $this->timeTracker->push('Get Page from cache');
62 // Locks may be acquired here
63 $this->controller->getFromCache();
64 $this->timeTracker->pull();
65 // Get config if not already gotten
66 // After this, we should have a valid config-array ready
67 $this->controller->getConfigArray();
68
69 // Merge Query Parameters with config.defaultGetVars
70 // This is done in getConfigArray as well, but does not override the current middleware request object
71 // Since we want to stay in sync with this, the option needs to be set as well.
72 if (!empty($this->controller->config['config']['defaultGetVars.'] ?? null)) {
73 $modifiedGetVars = GeneralUtility::removeDotsFromTS($this->controller->config['config']['defaultGetVars.']);
74 if (!empty($request->getQueryParams())) {
75 ArrayUtility::mergeRecursiveWithOverrule($modifiedGetVars, $request->getQueryParams());
76 }
77 $request = $request->withQueryParams($modifiedGetVars);
78 $GLOBALS['TYPO3_REQUEST'] = $request;
79 }
80
81 // Setting language and locale
82 $this->timeTracker->push('Setting language and locale');
83 $this->controller->settingLanguage();
84 $this->controller->settingLocale();
85 $this->timeTracker->pull();
86
87 // Convert POST data to utf-8 for internal processing if metaCharset is different
88 if ($this->controller->metaCharset !== 'utf-8' && is_array($_POST) && !empty($_POST)) {
89 $this->convertCharsetRecursivelyToUtf8($_POST, $this->controller->metaCharset);
90 $GLOBALS['HTTP_POST_VARS'] = $_POST;
91 $parsedBody = $request->getParsedBody();
92 $this->convertCharsetRecursivelyToUtf8($parsedBody, $this->controller->metaCharset);
93 $request = $request->withParsedBody($parsedBody);
94 $GLOBALS['TYPO3_REQUEST'] = $request;
95 }
96
97 // @deprecated since TYPO3 v9.3, will be removed in TYPO3 v10.0
98 $this->controller->initializeRedirectUrlHandlers(true);
99
100 // Hook for processing data submission to extensions
101 // This is done at this point, because we need the config values
102 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['checkDataSubmission'] ?? [] as $className) {
103 GeneralUtility::makeInstance($className)->checkDataSubmission($this->controller);
104 }
105
106 return $handler->handle($request);
107 }
108
109 /**
110 * Small helper function to convert charsets for arrays to UTF-8
111 *
112 * @param mixed $data given by reference (string/array usually)
113 * @param string $fromCharset convert FROM this charset
114 */
115 protected function convertCharsetRecursivelyToUtf8(&$data, string $fromCharset)
116 {
117 foreach ($data as $key => $value) {
118 if (is_array($data[$key])) {
119 $this->convertCharsetRecursivelyToUtf8($data[$key], $fromCharset);
120 } elseif (is_string($data[$key])) {
121 $data[$key] = mb_convert_encoding($data[$key], 'utf-8', $fromCharset);
122 }
123 }
124 }
125 }