3 declare(strict_types
=1);
6 * This file is part of the TYPO3 CMS project.
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
15 * The TYPO3 project - inspiring people to share!
18 namespace TYPO3\CMS\Frontend\Middleware
;
20 use Psr\Http\Message\ResponseInterface
;
21 use Psr\Http\Message\ServerRequestInterface
;
22 use Psr\Http\Server\MiddlewareInterface
;
23 use Psr\Http\Server\RequestHandlerInterface
;
24 use TYPO3\CMS\Core\TimeTracker\TimeTracker
;
25 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
;
28 * Initialization of TypoScriptFrontendController
30 * Do all necessary preparation steps for rendering
32 * @internal this middleware might get removed in TYPO3 v10.x.
34 class PrepareTypoScriptFrontendRendering
implements MiddlewareInterface
37 * @var TypoScriptFrontendController
39 protected $controller;
44 protected $timeTracker;
46 public function __construct(TypoScriptFrontendController
$controller, TimeTracker
$timeTracker)
48 $this->controller
= $controller;
49 $this->timeTracker
= $timeTracker;
53 * Initialize TypoScriptFrontendController to the point right before rendering of the page is triggered
55 * @param ServerRequestInterface $request
56 * @param RequestHandlerInterface $handler
57 * @return ResponseInterface
59 public function process(ServerRequestInterface
$request, RequestHandlerInterface
$handler): ResponseInterface
61 // as long as TSFE throws errors with the global object, this needs to be set, but
62 // should be removed later-on once TypoScript Condition Matcher is built with the current request object.
63 $GLOBALS['TYPO3_REQUEST'] = $request;
65 $this->timeTracker
->push('Get Page from cache');
66 // Locks may be acquired here
67 $this->controller
->getFromCache($request);
68 $this->timeTracker
->pull();
69 // Get config if not already gotten
70 // After this, we should have a valid config-array ready
71 $this->controller
->getConfigArray($request);
73 // Convert POST data to utf-8 for internal processing if metaCharset is different
74 if ($this->controller
->metaCharset
!== 'utf-8' && $request->getMethod() === 'POST') {
75 $parsedBody = $request->getParsedBody();
76 if (is_array($parsedBody) && !empty($parsedBody)) {
77 $this->convertCharsetRecursivelyToUtf8($parsedBody, $this->controller
->metaCharset
);
78 $request = $request->withParsedBody($parsedBody);
81 return $handler->handle($request);
85 * Small helper function to convert charsets for arrays to UTF-8
87 * @param mixed $data given by reference (string/array usually)
88 * @param string $fromCharset convert FROM this charset
90 protected function convertCharsetRecursivelyToUtf8(&$data, string $fromCharset)
92 foreach ($data as $key => $value) {
93 if (is_array($data[$key])) {
94 $this->convertCharsetRecursivelyToUtf8($data[$key], $fromCharset);
95 } elseif (is_string($data[$key])) {
96 $data[$key] = mb_convert_encoding($data[$key], 'utf-8', $fromCharset);