‪TYPO3CMS  9.5
PrepareTypoScriptFrontendRendering.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
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;
28 
36 class ‪PrepareTypoScriptFrontendRendering implements MiddlewareInterface
37 {
41  protected ‪$controller;
42 
46  protected ‪$timeTracker;
47 
49  {
50  $this->controller = ‪$controller ?: ‪$GLOBALS['TSFE'];
51  $this->timeTracker = ‪$timeTracker ?: GeneralUtility::makeInstance(TimeTracker::class);
52  }
53 
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  $this->controller->setPageArguments($pageArguments);
81  $request = $request->withAttribute('routing', $pageArguments);
82  }
83  if (!empty($request->getQueryParams())) {
84  ‪ArrayUtility::mergeRecursiveWithOverrule($modifiedGetVars, $request->getQueryParams());
85  }
86  $request = $request->withQueryParams($modifiedGetVars);
87  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
88  }
89 
90  // Setting language and locale
91  $this->timeTracker->push('Setting language and locale');
92  $this->controller->settingLanguage();
93  $this->controller->settingLocale();
94  $this->timeTracker->pull();
95 
96  // Convert POST data to utf-8 for internal processing if metaCharset is different
97  if ($this->controller->metaCharset !== 'utf-8' && is_array($_POST) && !empty($_POST)) {
98  $this->‪convertCharsetRecursivelyToUtf8($_POST, $this->controller->metaCharset);
99  ‪$GLOBALS['HTTP_POST_VARS'] = $_POST;
100  $parsedBody = $request->getParsedBody();
101  $this->‪convertCharsetRecursivelyToUtf8($parsedBody, $this->controller->metaCharset);
102  $request = $request->withParsedBody($parsedBody);
103  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
104  }
105 
106  // @deprecated since TYPO3 v9.3, will be removed in TYPO3 v10.0
107  $this->controller->initializeRedirectUrlHandlers(true);
108 
109  // Hook for processing data submission to extensions
110  // This is done at this point, because we need the config values
111  if (!empty(‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['checkDataSubmission'])) {
112  trigger_error('The "checkDataSubmission" hook will be removed in TYPO3 v10.0 in favor of PSR-15. Use a middleware instead.', E_USER_DEPRECATED);
113  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['checkDataSubmission'] as $className) {
114  GeneralUtility::makeInstance($className)->checkDataSubmission($this->controller);
115  }
116  }
117 
118  return $handler->handle($request);
119  }
120 
127  protected function ‪convertCharsetRecursivelyToUtf8(&$data, string $fromCharset)
128  {
129  foreach ($data as $key => $value) {
130  if (is_array($data[$key])) {
131  $this->‪convertCharsetRecursivelyToUtf8($data[$key], $fromCharset);
132  } elseif (is_string($data[$key])) {
133  $data[$key] = mb_convert_encoding($data[$key], 'utf-8', $fromCharset);
134  }
135  }
136  }
137 }
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:25
‪TYPO3\CMS\Frontend\Middleware\PrepareTypoScriptFrontendRendering\__construct
‪__construct(TypoScriptFrontendController $controller=null, TimeTracker $timeTracker=null)
Definition: PrepareTypoScriptFrontendRendering.php:46
‪TYPO3\CMS\Frontend\Middleware\PrepareTypoScriptFrontendRendering
Definition: PrepareTypoScriptFrontendRendering.php:37
‪TYPO3\CMS\Frontend\Middleware\PrepareTypoScriptFrontendRendering\$timeTracker
‪TimeTracker $timeTracker
Definition: PrepareTypoScriptFrontendRendering.php:44
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:614
‪TYPO3\CMS\Frontend\Middleware\PrepareTypoScriptFrontendRendering\$controller
‪TypoScriptFrontendController $controller
Definition: PrepareTypoScriptFrontendRendering.php:40
‪TYPO3\CMS\Frontend\Middleware
Definition: BackendUserAuthenticator.php:4
‪TYPO3\CMS\Frontend\Middleware\PrepareTypoScriptFrontendRendering\convertCharsetRecursivelyToUtf8
‪convertCharsetRecursivelyToUtf8(&$data, string $fromCharset)
Definition: PrepareTypoScriptFrontendRendering.php:125
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:97
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪TYPO3\CMS\Frontend\Middleware\PrepareTypoScriptFrontendRendering\process
‪ResponseInterface process(ServerRequestInterface $request, PsrRequestHandlerInterface $handler)
Definition: PrepareTypoScriptFrontendRendering.php:59
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:27