[TASK] Use younger selenium/standalone-chrome
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Middleware / PrepareTypoScriptFrontendRendering.php
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
18 namespace TYPO3\CMS\Frontend\Middleware;
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;
24 use TYPO3\CMS\Core\TimeTracker\TimeTracker;
25 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
26
27 /**
28 * Initialization of TypoScriptFrontendController
29 *
30 * Do all necessary preparation steps for rendering
31 *
32 * @internal this middleware might get removed in TYPO3 v10.x.
33 */
34 class PrepareTypoScriptFrontendRendering implements MiddlewareInterface
35 {
36 /**
37 * @var TypoScriptFrontendController
38 */
39 protected $controller;
40
41 /**
42 * @var TimeTracker
43 */
44 protected $timeTracker;
45
46 public function __construct(TypoScriptFrontendController $controller, TimeTracker $timeTracker)
47 {
48 $this->controller = $controller;
49 $this->timeTracker = $timeTracker;
50 }
51
52 /**
53 * Initialize TypoScriptFrontendController to the point right before rendering of the page is triggered
54 *
55 * @param ServerRequestInterface $request
56 * @param RequestHandlerInterface $handler
57 * @return ResponseInterface
58 */
59 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
60 {
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;
64 // Get from cache
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);
72
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);
79 }
80 }
81 return $handler->handle($request);
82 }
83
84 /**
85 * Small helper function to convert charsets for arrays to UTF-8
86 *
87 * @param mixed $data given by reference (string/array usually)
88 * @param string $fromCharset convert FROM this charset
89 */
90 protected function convertCharsetRecursivelyToUtf8(&$data, string $fromCharset)
91 {
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);
97 }
98 }
99 }
100 }