‪TYPO3CMS  11.5
PrepareTypoScriptFrontendRendering.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
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.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
22 use Psr\Http\Server\MiddlewareInterface;
23 use Psr\Http\Server\RequestHandlerInterface;
26 
34 class ‪PrepareTypoScriptFrontendRendering implements MiddlewareInterface
35 {
39  protected ‪$timeTracker;
40 
42  {
43  $this->timeTracker = ‪$timeTracker;
44  }
45 
53  public function ‪process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
54  {
56  $controller = $request->getAttribute('frontend.controller');
57 
58  // as long as TSFE throws errors with the global object, this needs to be set, but
59  // should be removed later-on once TypoScript Condition Matcher is built with the current request object.
60  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
61  // Get from cache
62  $this->timeTracker->push('Get Page from cache');
63  // Locks may be acquired here
64  $controller->getFromCache($request);
65  $this->timeTracker->pull();
66  // Get config if not already gotten
67  // After this, we should have a valid config-array ready
68  $controller->getConfigArray($request);
69 
70  // Convert POST data to utf-8 for internal processing if metaCharset is different
71  if ($controller->metaCharset !== 'utf-8' && $request->getMethod() === 'POST') {
72  $parsedBody = $request->getParsedBody();
73  if (is_array($parsedBody) && !empty($parsedBody)) {
74  $this->‪convertCharsetRecursivelyToUtf8($parsedBody, $controller->metaCharset);
75  $request = $request->withParsedBody($parsedBody);
76  }
77  }
78  $response = $handler->handle($request);
79 
86  $controller->releaseLocks();
87 
88  return $response;
89  }
90 
97  protected function ‪convertCharsetRecursivelyToUtf8(&$data, string $fromCharset)
98  {
99  foreach ($data as $key => $value) {
100  if (is_array($data[$key])) {
101  $this->‪convertCharsetRecursivelyToUtf8($data[$key], $fromCharset);
102  } elseif (is_string($data[$key])) {
103  $data[$key] = mb_convert_encoding($data[$key], 'utf-8', $fromCharset);
104  }
105  }
106  }
107 }
‪TYPO3\CMS\Frontend\Middleware\PrepareTypoScriptFrontendRendering
Definition: PrepareTypoScriptFrontendRendering.php:35
‪TYPO3\CMS\Frontend\Middleware\PrepareTypoScriptFrontendRendering\$timeTracker
‪TimeTracker $timeTracker
Definition: PrepareTypoScriptFrontendRendering.php:38
‪TYPO3\CMS\Frontend\Middleware\PrepareTypoScriptFrontendRendering\process
‪ResponseInterface process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: PrepareTypoScriptFrontendRendering.php:52
‪TYPO3\CMS\Frontend\Middleware
Definition: BackendUserAuthenticator.php:18
‪TYPO3\CMS\Frontend\Middleware\PrepareTypoScriptFrontendRendering\convertCharsetRecursivelyToUtf8
‪convertCharsetRecursivelyToUtf8(&$data, string $fromCharset)
Definition: PrepareTypoScriptFrontendRendering.php:96
‪TYPO3\CMS\Frontend\Middleware\PrepareTypoScriptFrontendRendering\__construct
‪__construct(TimeTracker $timeTracker)
Definition: PrepareTypoScriptFrontendRendering.php:40
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:104
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:31