7987b935f0fd6a044f5440da8bb9866f65b9a7be
[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\Routing\PageArguments;
24 use TYPO3\CMS\Core\TimeTracker\TimeTracker;
25 use TYPO3\CMS\Core\Utility\ArrayUtility;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
28
29 /**
30 * Initialization of TypoScriptFrontendController
31 *
32 * Do all necessary preparation steps for rendering
33 *
34 * @internal this middleware might get removed in TYPO3 v10.0.
35 */
36 class PrepareTypoScriptFrontendRendering implements MiddlewareInterface
37 {
38 /**
39 * @var TypoScriptFrontendController
40 */
41 protected $controller;
42
43 /**
44 * @var TimeTracker
45 */
46 protected $timeTracker;
47
48 public function __construct(TypoScriptFrontendController $controller = null, TimeTracker $timeTracker = null)
49 {
50 $this->controller = $controller ?: $GLOBALS['TSFE'];
51 $this->timeTracker = $timeTracker ?: GeneralUtility::makeInstance(TimeTracker::class);
52 }
53
54 /**
55 * Initialize TypoScriptFrontendController to the point right before rendering of the page is triggered
56 *
57 * @param ServerRequestInterface $request
58 * @param PsrRequestHandlerInterface $handler
59 * @return ResponseInterface
60 */
61 public function process(ServerRequestInterface $request, PsrRequestHandlerInterface $handler): ResponseInterface
62 {
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
72 // Merge Query Parameters with config.defaultGetVars
73 // This is done in getConfigArray as well, but does not override the current middleware request object
74 // Since we want to stay in sync with this, the option needs to be set as well.
75 $pageArguments = $request->getAttribute('routing');
76 if (!empty($this->controller->config['config']['defaultGetVars.'] ?? null)) {
77 $modifiedGetVars = GeneralUtility::removeDotsFromTS($this->controller->config['config']['defaultGetVars.']);
78 if ($pageArguments instanceof PageArguments) {
79 $pageArguments = $pageArguments->withQueryArguments($modifiedGetVars);
80 $request = $request->withAttribute('routing', $pageArguments);
81 }
82 if (!empty($request->getQueryParams())) {
83 ArrayUtility::mergeRecursiveWithOverrule($modifiedGetVars, $request->getQueryParams());
84 }
85 $request = $request->withQueryParams($modifiedGetVars);
86 $GLOBALS['TYPO3_REQUEST'] = $request;
87 }
88 // Populate internal route query arguments to super global $_GET
89 if ($pageArguments instanceof PageArguments) {
90 $_GET = $pageArguments->getArguments();
91 $GLOBALS['HTTP_GET_VARS'] = $pageArguments->getArguments();
92 $this->controller->setPageArguments($pageArguments);
93 }
94
95 // Setting language and locale
96 $this->timeTracker->push('Setting language and locale');
97 $this->controller->settingLanguage();
98 $this->controller->settingLocale();
99 $this->timeTracker->pull();
100
101 // Convert POST data to utf-8 for internal processing if metaCharset is different
102 if ($this->controller->metaCharset !== 'utf-8' && is_array($_POST) && !empty($_POST)) {
103 $this->convertCharsetRecursivelyToUtf8($_POST, $this->controller->metaCharset);
104 $GLOBALS['HTTP_POST_VARS'] = $_POST;
105 $parsedBody = $request->getParsedBody();
106 $this->convertCharsetRecursivelyToUtf8($parsedBody, $this->controller->metaCharset);
107 $request = $request->withParsedBody($parsedBody);
108 $GLOBALS['TYPO3_REQUEST'] = $request;
109 }
110
111 // @deprecated since TYPO3 v9.3, will be removed in TYPO3 v10.0
112 $this->controller->initializeRedirectUrlHandlers(true);
113
114 // Hook for processing data submission to extensions
115 // This is done at this point, because we need the config values
116 if (!empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['checkDataSubmission'])) {
117 trigger_error('The "checkDataSubmission" hook will be removed in TYPO3 v10.0 in favor of PSR-15. Use a middleware instead.', E_USER_DEPRECATED);
118 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['checkDataSubmission'] as $className) {
119 GeneralUtility::makeInstance($className)->checkDataSubmission($this->controller);
120 }
121 }
122
123 return $handler->handle($request);
124 }
125
126 /**
127 * Small helper function to convert charsets for arrays to UTF-8
128 *
129 * @param mixed $data given by reference (string/array usually)
130 * @param string $fromCharset convert FROM this charset
131 */
132 protected function convertCharsetRecursivelyToUtf8(&$data, string $fromCharset)
133 {
134 foreach ($data as $key => $value) {
135 if (is_array($data[$key])) {
136 $this->convertCharsetRecursivelyToUtf8($data[$key], $fromCharset);
137 } elseif (is_string($data[$key])) {
138 $data[$key] = mb_convert_encoding($data[$key], 'utf-8', $fromCharset);
139 }
140 }
141 }
142 }