[BUGFIX] Disable file compression for external urls in js and css includes
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Http / RequestHandler.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Frontend\Http;
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\RequestHandlerInterface as PsrRequestHandlerInterface;
22 use TYPO3\CMS\Core\Core\Environment;
23 use TYPO3\CMS\Core\Http\NullResponse;
24 use TYPO3\CMS\Core\Http\RequestHandlerInterface;
25 use TYPO3\CMS\Core\Http\Response;
26 use TYPO3\CMS\Core\Page\PageRenderer;
27 use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
28 use TYPO3\CMS\Core\TimeTracker\TimeTracker;
29 use TYPO3\CMS\Core\Type\File\ImageInfo;
30 use TYPO3\CMS\Core\TypoScript\TypoScriptService;
31 use TYPO3\CMS\Core\Utility\ArrayUtility;
32 use TYPO3\CMS\Core\Utility\GeneralUtility;
33 use TYPO3\CMS\Core\Utility\PathUtility;
34 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
35 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
36 use TYPO3\CMS\Frontend\Resource\FilePathSanitizer;
37
38 /**
39 * This is the main entry point of the TypoScript driven standard front-end.
40 *
41 * "handle()" is called when all PSR-15 middlewares have been set up the PSR-7 ServerRequest object and the following
42 * things have been evaluated
43 * - correct page ID, page type (typeNum), rootline, MP etc.
44 * - info if is cached content already available
45 * - proper language
46 * - proper TypoScript which should be processed.
47 *
48 * Then, this class is able to render the actual HTTP body part built via TypoScript. Here this is split into two parts:
49 * - Everything included in <body>, done via page.10, page.20 etc.
50 * - Everything around.
51 *
52 * If the content has been built together within the cache (cache_pages), it is fetched directly, and
53 * any so-called "uncached" content is generated again.
54 *
55 * Some further hooks allow to post-processing the content.
56 *
57 * Then the right HTTP response headers are compiled together and sent as well.
58 */
59 class RequestHandler implements RequestHandlerInterface, PsrRequestHandlerInterface
60 {
61 /**
62 * Instance of the timetracker
63 * @var TimeTracker
64 */
65 protected $timeTracker;
66
67 /**
68 * Handles a frontend request
69 *
70 * @param ServerRequestInterface $request
71 * @return ResponseInterface
72 */
73 public function handleRequest(ServerRequestInterface $request): ResponseInterface
74 {
75 return $this->handle($request);
76 }
77
78 /**
79 * Puts parameters that have been added or removed from the global _GET or _POST arrays
80 * into the given request (however, the PSR-7 request information takes precedence).
81 *
82 * @param ServerRequestInterface $request
83 * @return ServerRequestInterface
84 */
85 protected function addModifiedGlobalsToIncomingRequest(ServerRequestInterface $request): ServerRequestInterface
86 {
87 $originalGetParameters = $request->getAttribute('_originalGetParameters', null);
88 if ($originalGetParameters !== null && !empty($_GET) && $_GET !== $originalGetParameters) {
89 // Find out what has been changed.
90 $modifiedGetParameters = ArrayUtility::arrayDiffAssocRecursive($_GET ?? [], $originalGetParameters);
91 if (!empty($modifiedGetParameters)) {
92 $queryParams = array_replace_recursive($modifiedGetParameters, $request->getQueryParams());
93 $request = $request->withQueryParams($queryParams);
94 $GLOBALS['TYPO3_REQUEST'] = $request;
95 $this->timeTracker->setTSlogMessage('GET parameters have been modified during Request building in a hook.');
96 }
97 }
98 // do same for $_POST if the request is a POST request
99 $originalPostParameters = $request->getAttribute('_originalPostParameters', null);
100 if ($request->getMethod() === 'POST' && $originalPostParameters !== null && !empty($_POST) && $_POST !== $originalPostParameters) {
101 // Find out what has been changed
102 $modifiedPostParameters = ArrayUtility::arrayDiffAssocRecursive($_POST ?? [], $originalPostParameters);
103 if (!empty($modifiedPostParameters)) {
104 $parsedBody = array_replace_recursive($modifiedPostParameters, $request->getParsedBody());
105 $request = $request->withParsedBody($parsedBody);
106 $GLOBALS['TYPO3_REQUEST'] = $request;
107 $this->timeTracker->setTSlogMessage('POST parameters have been modified during Request building in a hook.');
108 }
109 }
110 return $request;
111 }
112
113 /**
114 * Sets the global GET and POST to the values, so if people access $_GET and $_POST
115 * Within hooks starting NOW (e.g. cObject), they get the "enriched" data from query params.
116 *
117 * This needs to be run after the request object has been enriched with modified GET/POST variables.
118 *
119 * @param ServerRequestInterface $request
120 * @internal this safety net will be removed in TYPO3 v10.0.
121 */
122 protected function resetGlobalsToCurrentRequest(ServerRequestInterface $request)
123 {
124 if ($request->getQueryParams() !== $_GET) {
125 $queryParams = $request->getQueryParams();
126 $_GET = $queryParams;
127 $GLOBALS['HTTP_GET_VARS'] = $_GET;
128 }
129 if ($request->getMethod() === 'POST') {
130 $parsedBody = $request->getParsedBody();
131 if (is_array($parsedBody) && $parsedBody !== $_POST) {
132 $_POST = $parsedBody;
133 $GLOBALS['HTTP_POST_VARS'] = $_POST;
134 }
135 }
136 }
137 /**
138 * Handles a frontend request, after finishing running middlewares
139 *
140 * @param ServerRequestInterface $request
141 * @return ResponseInterface|null
142 */
143 public function handle(ServerRequestInterface $request): ResponseInterface
144 {
145 // Fetch the initialized time tracker object
146 $this->timeTracker = GeneralUtility::makeInstance(TimeTracker::class);
147 /** @var TypoScriptFrontendController $controller */
148 $controller = $GLOBALS['TSFE'];
149
150 // safety net, will be removed in TYPO3 v10.0. Aligns $_GET/$_POST to the incoming request.
151 $request = $this->addModifiedGlobalsToIncomingRequest($request);
152 $this->resetGlobalsToCurrentRequest($request);
153
154 // Generate page
155 if ($controller->isGeneratePage()) {
156 $this->timeTracker->push('Page generation');
157 $controller->generatePage_preProcessing();
158 $controller->preparePageContentGeneration($request);
159
160 // Content generation
161 $this->timeTracker->incStackPointer();
162 $this->timeTracker->push($controller->sPre, 'PAGE');
163
164 // If 'disableAllHeaderCode' is set, all the header-code is discarded
165 if ($controller->config['config']['disableAllHeaderCode'] ?? false) {
166 $controller->content = $this->generatePageContent($controller);
167 } else {
168 $controller->content = $this->generatePageContentWithHeader($controller, $request->getAttribute('language', null));
169 }
170
171 $this->timeTracker->pull($this->timeTracker->LR ? $controller->content : '');
172 $this->timeTracker->decStackPointer();
173
174 $controller->setAbsRefPrefix();
175 $controller->generatePage_postProcessing();
176 $this->timeTracker->pull();
177 }
178 $controller->releaseLocks();
179
180 // Render non-cached page parts by replacing placeholders which are taken from cache or added during page generation
181 if ($controller->isINTincScript()) {
182 if (!$controller->isGeneratePage()) {
183 // When page was generated, this was already called. Avoid calling this twice.
184 $controller->preparePageContentGeneration($request);
185 }
186 $this->timeTracker->push('Non-cached objects');
187 $controller->INTincScript();
188 $this->timeTracker->pull();
189 }
190
191 // Create a Response object when sending content
192 $response = new Response();
193
194 // Output content
195 $isOutputting = $controller->isOutputting();
196 if ($isOutputting) {
197 $this->timeTracker->push('Print Content');
198 $response = $controller->applyHttpHeadersToResponse($response);
199 $controller->processContentForOutput();
200 $this->timeTracker->pull();
201 }
202 // Store session data for fe_users
203 $controller->fe_user->storeSessionData();
204
205 // @deprecated since TYPO3 v9.3, will be removed in TYPO3 v10.0.
206 $redirectResponse = $controller->redirectToExternalUrl(true);
207 if ($redirectResponse instanceof ResponseInterface) {
208 $controller->sendHttpHeadersDirectly();
209 return $redirectResponse;
210 }
211
212 // Statistics
213 $GLOBALS['TYPO3_MISC']['microtime_end'] = microtime(true);
214 if ($isOutputting && ($controller->config['config']['debug'] ?? !empty($GLOBALS['TYPO3_CONF_VARS']['FE']['debug']))) {
215 $response = $response->withHeader('X-TYPO3-Parsetime', $this->timeTracker->getParseTime() . 'ms');
216 }
217
218 // Preview info
219 // @deprecated since TYPO3 v9.4, will be removed in TYPO3 v10.0.
220 $controller->previewInfo(true);
221
222 // Hook for "end-of-frontend"
223 $_params = ['pObj' => &$controller];
224 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['hook_eofe'] ?? [] as $_funcRef) {
225 GeneralUtility::callUserFunction($_funcRef, $_params, $controller);
226 }
227
228 // Finish time tracking (started in TYPO3\CMS\Frontend\Middleware\TimeTrackerInitialization)
229 $this->timeTracker->pull();
230
231 if ($isOutputting) {
232 $response->getBody()->write($controller->content);
233 // if any code set a response code that is not 200 clear the cache's content
234 // if we fail to do so we would deliver cache content with a wrong header, which causes big mess.
235 if (http_response_code() !== 200 || $response->getStatusCode() !== 200) {
236 $controller->clearPageCacheContent();
237 }
238 }
239
240 return $isOutputting ? $response : new NullResponse();
241 }
242
243 /**
244 * Generates the main content part within <body> tags (except JS files/CSS files).
245 *
246 * @param TypoScriptFrontendController $controller
247 * @return string
248 */
249 protected function generatePageContent(TypoScriptFrontendController $controller): string
250 {
251 $pageContent = $controller->cObj->cObjGet($controller->pSetup) ?: '';
252 if ($controller->pSetup['wrap'] ?? false) {
253 $pageContent = $controller->cObj->wrap($pageContent, $controller->pSetup['wrap']);
254 }
255 if ($controller->pSetup['stdWrap.'] ?? false) {
256 $pageContent = $controller->cObj->stdWrap($pageContent, $controller->pSetup['stdWrap.']);
257 }
258 return $pageContent;
259 }
260
261 /**
262 * Rendering normal HTML-page with header by wrapping the generated content ($pageContent) in body-tags and setting the header accordingly.
263 * Render HTML page with header parts (<head> tag content and wrap around <body> tag) - this is done
264 * after the "main" page Content, since some JS may be inserted at that point.
265 *
266 * @param TypoScriptFrontendController $controller
267 * @param SiteLanguage|null $siteLanguage
268 * @return string
269 */
270 protected function generatePageContentWithHeader(TypoScriptFrontendController $controller, ?SiteLanguage $siteLanguage): string
271 {
272 // Generate the page content, this has to be first, as some additional TSFE-related code could have been written
273 $pageContent = $this->generatePageContent($controller);
274 $pageRenderer = $this->getPageRenderer();
275 if ($controller->config['config']['moveJsFromHeaderToFooter'] ?? false) {
276 $pageRenderer->enableMoveJsFromHeaderToFooter();
277 }
278 if ($controller->config['config']['pageRendererTemplateFile'] ?? false) {
279 try {
280 $file = GeneralUtility::makeInstance(FilePathSanitizer::class)->sanitize($controller->config['config']['pageRendererTemplateFile']);
281 $pageRenderer->setTemplateFile($file);
282 } catch (\TYPO3\CMS\Core\Resource\Exception $e) {
283 // do nothing
284 }
285 }
286 $headerComment = trim($controller->config['config']['headerComment'] ?? '');
287 if ($headerComment) {
288 $pageRenderer->addInlineComment("\t" . str_replace(LF, LF . "\t", $headerComment) . LF);
289 }
290 // Setting charset:
291 $theCharset = $controller->metaCharset;
292 // Reset the content variables:
293 $controller->content = '';
294 $htmlTagAttributes = [];
295 $htmlLang = $controller->config['config']['htmlTag_langKey'] ?? ($controller->sys_language_isocode ?: 'en');
296 $direction = $controller->config['config']['htmlTag_dir'] ?? null;
297 if ($siteLanguage !== null) {
298 $direction = $siteLanguage->getDirection();
299 $htmlLang = $siteLanguage->getTwoLetterIsoCode();
300 }
301
302 if ($direction) {
303 $htmlTagAttributes['dir'] = htmlspecialchars($direction);
304 }
305 // Setting document type:
306 $docTypeParts = [];
307 $xmlDocument = true;
308 // Part 1: XML prologue
309 switch ((string)($controller->config['config']['xmlprologue'] ?? '')) {
310 case 'none':
311 $xmlDocument = false;
312 break;
313 case 'xml_10':
314 $docTypeParts[] = '<?xml version="1.0" encoding="' . $theCharset . '"?>';
315 break;
316 case 'xml_11':
317 $docTypeParts[] = '<?xml version="1.1" encoding="' . $theCharset . '"?>';
318 break;
319 case '':
320 if ($controller->xhtmlVersion) {
321 $docTypeParts[] = '<?xml version="1.0" encoding="' . $theCharset . '"?>';
322 } else {
323 $xmlDocument = false;
324 }
325 break;
326 default:
327 $docTypeParts[] = $controller->config['config']['xmlprologue'];
328 }
329 // Part 2: DTD
330 $doctype = $controller->config['config']['doctype'] ?? null;
331 if ($doctype) {
332 switch ($doctype) {
333 case 'xhtml_trans':
334 $docTypeParts[] = '<!DOCTYPE html
335 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
336 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
337 break;
338 case 'xhtml_strict':
339 $docTypeParts[] = '<!DOCTYPE html
340 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
341 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
342 break;
343 case 'xhtml_basic':
344 $docTypeParts[] = '<!DOCTYPE html
345 PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN"
346 "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">';
347 break;
348 case 'xhtml_11':
349 $docTypeParts[] = '<!DOCTYPE html
350 PUBLIC "-//W3C//DTD XHTML 1.1//EN"
351 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
352 break;
353 case 'xhtml+rdfa_10':
354 $docTypeParts[] = '<!DOCTYPE html
355 PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"
356 "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">';
357 break;
358 case 'html5':
359 $docTypeParts[] = '<!DOCTYPE html>';
360 if ($xmlDocument) {
361 $pageRenderer->setMetaCharsetTag('<meta charset="|" />');
362 } else {
363 $pageRenderer->setMetaCharsetTag('<meta charset="|">');
364 }
365 break;
366 case 'none':
367 break;
368 default:
369 $docTypeParts[] = $doctype;
370 }
371 } else {
372 $docTypeParts[] = '<!DOCTYPE html>';
373 if ($xmlDocument) {
374 $pageRenderer->setMetaCharsetTag('<meta charset="|" />');
375 } else {
376 $pageRenderer->setMetaCharsetTag('<meta charset="|">');
377 }
378 }
379 if ($controller->xhtmlVersion) {
380 $htmlTagAttributes['xml:lang'] = $htmlLang;
381 }
382 if ($controller->xhtmlVersion < 110 || $doctype === 'html5') {
383 $htmlTagAttributes['lang'] = $htmlLang;
384 }
385 if ($controller->xhtmlVersion || $doctype === 'html5' && $xmlDocument) {
386 // We add this to HTML5 to achieve a slightly better backwards compatibility
387 $htmlTagAttributes['xmlns'] = 'http://www.w3.org/1999/xhtml';
388 if (is_array($controller->config['config']['namespaces.'])) {
389 foreach ($controller->config['config']['namespaces.'] as $prefix => $uri) {
390 // $uri gets htmlspecialchared later
391 $htmlTagAttributes['xmlns:' . htmlspecialchars($prefix)] = $uri;
392 }
393 }
394 }
395 // Swap XML and doctype order around (for MSIE / Opera standards compliance)
396 if ($controller->config['config']['doctypeSwitch'] ?? false) {
397 $docTypeParts = array_reverse($docTypeParts);
398 }
399 // Adding doctype parts:
400 if (!empty($docTypeParts)) {
401 $pageRenderer->setXmlPrologAndDocType(implode(LF, $docTypeParts));
402 }
403 // Begin header section:
404 $htmlTag = $this->generateHtmlTag($htmlTagAttributes, $controller->config['config'] ?? [], $controller->cObj);
405 $pageRenderer->setHtmlTag($htmlTag);
406 // Head tag:
407 $headTag = $controller->pSetup['headTag'] ?? '<head>';
408 if (isset($controller->pSetup['headTag.'])) {
409 $headTag = $controller->cObj->stdWrap($headTag, $controller->pSetup['headTag.']);
410 }
411 $pageRenderer->setHeadTag($headTag);
412 // Setting charset meta tag:
413 $pageRenderer->setCharSet($theCharset);
414 $pageRenderer->addInlineComment(' This website is powered by TYPO3 - inspiring people to share!
415 TYPO3 is a free open source Content Management Framework initially created by Kasper Skaarhoj and licensed under GNU/GPL.
416 TYPO3 is copyright ' . TYPO3_copyright_year . ' of Kasper Skaarhoj. Extensions are copyright of their respective owners.
417 Information and contribution at ' . TYPO3_URL_GENERAL . '
418 ');
419 if ($controller->baseUrl) {
420 $pageRenderer->setBaseUrl($controller->baseUrl);
421 }
422 if ($controller->pSetup['shortcutIcon'] ?? false) {
423 try {
424 $favIcon = GeneralUtility::makeInstance(FilePathSanitizer::class)->sanitize($controller->pSetup['shortcutIcon']);
425 $iconFileInfo = GeneralUtility::makeInstance(ImageInfo::class, Environment::getPublicPath() . '/' . $favIcon);
426 if ($iconFileInfo->isFile()) {
427 $iconMimeType = $iconFileInfo->getMimeType();
428 if ($iconMimeType) {
429 $iconMimeType = ' type="' . $iconMimeType . '"';
430 $pageRenderer->setIconMimeType($iconMimeType);
431 }
432 $pageRenderer->setFavIcon(PathUtility::getAbsoluteWebPath($controller->absRefPrefix . $favIcon));
433 }
434 } catch (\TYPO3\CMS\Core\Resource\Exception $e) {
435 // do nothing
436 }
437 }
438 // Including CSS files
439 if (isset($controller->tmpl->setup['plugin.']) && is_array($controller->tmpl->setup['plugin.'])) {
440 $stylesFromPlugins = '';
441 foreach ($controller->tmpl->setup['plugin.'] as $key => $iCSScode) {
442 if (is_array($iCSScode)) {
443 if ($iCSScode['_CSS_DEFAULT_STYLE'] && empty($controller->config['config']['removeDefaultCss'])) {
444 if (isset($iCSScode['_CSS_DEFAULT_STYLE.'])) {
445 $cssDefaultStyle = $controller->cObj->stdWrap($iCSScode['_CSS_DEFAULT_STYLE'], $iCSScode['_CSS_DEFAULT_STYLE.']);
446 } else {
447 $cssDefaultStyle = $iCSScode['_CSS_DEFAULT_STYLE'];
448 }
449 $stylesFromPlugins .= '/* default styles for extension "' . substr($key, 0, -1) . '" */' . LF . $cssDefaultStyle . LF;
450 }
451 if ($iCSScode['_CSS_PAGE_STYLE'] && empty($controller->config['config']['removePageCss'])) {
452 $cssPageStyle = implode(LF, $iCSScode['_CSS_PAGE_STYLE']);
453 if (isset($iCSScode['_CSS_PAGE_STYLE.'])) {
454 $cssPageStyle = $controller->cObj->stdWrap($cssPageStyle, $iCSScode['_CSS_PAGE_STYLE.']);
455 }
456 $cssPageStyle = '/* specific page styles for extension "' . substr($key, 0, -1) . '" */' . LF . $cssPageStyle;
457 $this->addCssToPageRenderer($controller, $cssPageStyle, true, 'InlinePageCss');
458 }
459 }
460 }
461 if (!empty($stylesFromPlugins)) {
462 $this->addCssToPageRenderer($controller, $stylesFromPlugins, false, 'InlineDefaultCss');
463 }
464 }
465 /**********************************************************************/
466 /* config.includeCSS / config.includeCSSLibs
467 /**********************************************************************/
468 if (isset($controller->pSetup['includeCSS.']) && is_array($controller->pSetup['includeCSS.'])) {
469 foreach ($controller->pSetup['includeCSS.'] as $key => $CSSfile) {
470 if (!is_array($CSSfile)) {
471 $cssFileConfig = &$controller->pSetup['includeCSS.'][$key . '.'];
472 if (isset($cssFileConfig['if.']) && !$controller->cObj->checkIf($cssFileConfig['if.'])) {
473 continue;
474 }
475 if ($cssFileConfig['external']) {
476 $ss = $CSSfile;
477 } else {
478 try {
479 $ss = GeneralUtility::makeInstance(FilePathSanitizer::class)->sanitize($CSSfile);
480 } catch (\TYPO3\CMS\Core\Resource\Exception $e) {
481 $ss = null;
482 }
483 }
484 if ($ss) {
485 if ($cssFileConfig['import']) {
486 if (!$cssFileConfig['external'] && $ss[0] !== '/') {
487 // To fix MSIE 6 that cannot handle these as relative paths (according to Ben v Ende)
488 $ss = GeneralUtility::dirname(GeneralUtility::getIndpEnv('SCRIPT_NAME')) . '/' . $ss;
489 }
490 $pageRenderer->addCssInlineBlock('import_' . $key, '@import url("' . htmlspecialchars($ss) . '") ' . htmlspecialchars($cssFileConfig['media']) . ';', empty($cssFileConfig['disableCompression']), (bool)$cssFileConfig['forceOnTop']);
491 } else {
492 $pageRenderer->addCssFile(
493 $ss,
494 $cssFileConfig['alternate'] ? 'alternate stylesheet' : 'stylesheet',
495 $cssFileConfig['media'] ?: 'all',
496 $cssFileConfig['title'] ?: '',
497 $cssFileConfig['external'] ? false : empty($cssFileConfig['disableCompression']),
498 (bool)$cssFileConfig['forceOnTop'],
499 $cssFileConfig['allWrap'],
500 (bool)$cssFileConfig['excludeFromConcatenation'] || (bool)$cssFileConfig['inline'],
501 $cssFileConfig['allWrap.']['splitChar'],
502 $cssFileConfig['inline']
503 );
504 unset($cssFileConfig);
505 }
506 }
507 }
508 }
509 }
510 if (isset($controller->pSetup['includeCSSLibs.']) && is_array($controller->pSetup['includeCSSLibs.'])) {
511 foreach ($controller->pSetup['includeCSSLibs.'] as $key => $CSSfile) {
512 if (!is_array($CSSfile)) {
513 $cssFileConfig = &$controller->pSetup['includeCSSLibs.'][$key . '.'];
514 if (isset($cssFileConfig['if.']) && !$controller->cObj->checkIf($cssFileConfig['if.'])) {
515 continue;
516 }
517 if ($cssFileConfig['external']) {
518 $ss = $CSSfile;
519 } else {
520 try {
521 $ss = GeneralUtility::makeInstance(FilePathSanitizer::class)->sanitize($CSSfile);
522 } catch (\TYPO3\CMS\Core\Resource\Exception $e) {
523 $ss = null;
524 }
525 }
526 if ($ss) {
527 if ($cssFileConfig['import']) {
528 if (!$cssFileConfig['external'] && $ss[0] !== '/') {
529 // To fix MSIE 6 that cannot handle these as relative paths (according to Ben v Ende)
530 $ss = GeneralUtility::dirname(GeneralUtility::getIndpEnv('SCRIPT_NAME')) . '/' . $ss;
531 }
532 $pageRenderer->addCssInlineBlock('import_' . $key, '@import url("' . htmlspecialchars($ss) . '") ' . htmlspecialchars($cssFileConfig['media']) . ';', empty($cssFileConfig['disableCompression']), (bool)$cssFileConfig['forceOnTop']);
533 } else {
534 $pageRenderer->addCssLibrary(
535 $ss,
536 $cssFileConfig['alternate'] ? 'alternate stylesheet' : 'stylesheet',
537 $cssFileConfig['media'] ?: 'all',
538 $cssFileConfig['title'] ?: '',
539 $cssFileConfig['external'] ? false : empty($cssFileConfig['disableCompression']),
540 (bool)$cssFileConfig['forceOnTop'],
541 $cssFileConfig['allWrap'],
542 (bool)$cssFileConfig['excludeFromConcatenation'] || (bool)$cssFileConfig['inline'],
543 $cssFileConfig['allWrap.']['splitChar'],
544 $cssFileConfig['inline']
545 );
546 unset($cssFileConfig);
547 }
548 }
549 }
550 }
551 }
552
553 // CSS_inlineStyle from TS
554 $style = trim($controller->pSetup['CSS_inlineStyle'] ?? '');
555 $style .= $controller->cObj->cObjGet($controller->pSetup['cssInline.'] ?? null, 'cssInline.');
556 if (trim($style)) {
557 $this->addCssToPageRenderer($controller, $style, true, 'additionalTSFEInlineStyle');
558 }
559 // Javascript Libraries
560 if (isset($controller->pSetup['javascriptLibs.']) && is_array($controller->pSetup['javascriptLibs.'])) {
561 // @deprecated since TYPO3 v9, will be removed in TYPO3 v10.0, the setting page.javascriptLibs has been deprecated and will be removed in TYPO3 v10.0.
562 trigger_error('The setting page.javascriptLibs will be removed in TYPO3 v10.0.', E_USER_DEPRECATED);
563
564 // Include jQuery into the page renderer
565 if (!empty($controller->pSetup['javascriptLibs.']['jQuery'])) {
566 // @deprecated since TYPO3 v9, will be removed in TYPO3 v10.0, the setting page.javascriptLibs.jQuery has been deprecated and will be removed in TYPO3 v10.0.
567 trigger_error('The setting page.javascriptLibs.jQuery will be removed in TYPO3 v10.0.', E_USER_DEPRECATED);
568
569 $jQueryTS = $controller->pSetup['javascriptLibs.']['jQuery.'];
570 // Check if version / source is set, if not set variable to "NULL" to use the default of the page renderer
571 $version = $jQueryTS['version'] ?? null;
572 $source = $jQueryTS['source'] ?? null;
573 // When "noConflict" is not set or "1" enable the default jQuery noConflict mode, otherwise disable the namespace
574 if (!isset($jQueryTS['noConflict']) || !empty($jQueryTS['noConflict'])) {
575 $namespace = 'noConflict';
576 } else {
577 $namespace = PageRenderer::JQUERY_NAMESPACE_NONE;
578 }
579 $pageRenderer->loadJquery($version, $source, $namespace, true);
580 }
581 }
582 // JavaScript library files
583 if (isset($controller->pSetup['includeJSLibs.']) && is_array($controller->pSetup['includeJSLibs.'])) {
584 foreach ($controller->pSetup['includeJSLibs.'] as $key => $JSfile) {
585 if (!is_array($JSfile)) {
586 if (isset($controller->pSetup['includeJSLibs.'][$key . '.']['if.']) && !$controller->cObj->checkIf($controller->pSetup['includeJSLibs.'][$key . '.']['if.'])) {
587 continue;
588 }
589 if ($controller->pSetup['includeJSLibs.'][$key . '.']['external']) {
590 $ss = $JSfile;
591 } else {
592 try {
593 $ss = GeneralUtility::makeInstance(FilePathSanitizer::class)->sanitize($JSfile);
594 } catch (\TYPO3\CMS\Core\Resource\Exception $e) {
595 $ss = null;
596 }
597 }
598 if ($ss) {
599 $jsFileConfig = &$controller->pSetup['includeJSLibs.'][$key . '.'];
600 $type = $jsFileConfig['type'];
601 if (!$type) {
602 $type = 'text/javascript';
603 }
604 $crossOrigin = $jsFileConfig['crossorigin'];
605 if (!$crossOrigin && $jsFileConfig['integrity'] && $jsFileConfig['external']) {
606 $crossOrigin = 'anonymous';
607 }
608 $pageRenderer->addJsLibrary(
609 $key,
610 $ss,
611 $type,
612 $jsFileConfig['external'] ? false : empty($jsFileConfig['disableCompression']),
613 (bool)$jsFileConfig['forceOnTop'],
614 $jsFileConfig['allWrap'],
615 (bool)$jsFileConfig['excludeFromConcatenation'],
616 $jsFileConfig['allWrap.']['splitChar'],
617 (bool)$jsFileConfig['async'],
618 $jsFileConfig['integrity'],
619 (bool)$jsFileConfig['defer'],
620 $crossOrigin
621 );
622 unset($jsFileConfig);
623 }
624 }
625 }
626 }
627 if (isset($controller->pSetup['includeJSFooterlibs.']) && is_array($controller->pSetup['includeJSFooterlibs.'])) {
628 foreach ($controller->pSetup['includeJSFooterlibs.'] as $key => $JSfile) {
629 if (!is_array($JSfile)) {
630 if (isset($controller->pSetup['includeJSFooterlibs.'][$key . '.']['if.']) && !$controller->cObj->checkIf($controller->pSetup['includeJSFooterlibs.'][$key . '.']['if.'])) {
631 continue;
632 }
633 if ($controller->pSetup['includeJSFooterlibs.'][$key . '.']['external']) {
634 $ss = $JSfile;
635 } else {
636 try {
637 $ss = GeneralUtility::makeInstance(FilePathSanitizer::class)->sanitize($JSfile);
638 } catch (\TYPO3\CMS\Core\Resource\Exception $e) {
639 $ss = null;
640 }
641 }
642 if ($ss) {
643 $jsFileConfig = &$controller->pSetup['includeJSFooterlibs.'][$key . '.'];
644 $type = $jsFileConfig['type'];
645 if (!$type) {
646 $type = 'text/javascript';
647 }
648 $crossorigin = $jsFileConfig['crossorigin'];
649 if (!$crossorigin && $jsFileConfig['integrity'] && $jsFileConfig['external']) {
650 $crossorigin = 'anonymous';
651 }
652 $pageRenderer->addJsFooterLibrary(
653 $key,
654 $ss,
655 $type,
656 $jsFileConfig['external'] ? false : empty($jsFileConfig['disableCompression']),
657 (bool)$jsFileConfig['forceOnTop'],
658 $jsFileConfig['allWrap'],
659 (bool)$jsFileConfig['excludeFromConcatenation'],
660 $jsFileConfig['allWrap.']['splitChar'],
661 (bool)$jsFileConfig['async'],
662 $jsFileConfig['integrity'],
663 (bool)$jsFileConfig['defer'],
664 $crossorigin
665 );
666 unset($jsFileConfig);
667 }
668 }
669 }
670 }
671 // JavaScript files
672 if (isset($controller->pSetup['includeJS.']) && is_array($controller->pSetup['includeJS.'])) {
673 foreach ($controller->pSetup['includeJS.'] as $key => $JSfile) {
674 if (!is_array($JSfile)) {
675 if (isset($controller->pSetup['includeJS.'][$key . '.']['if.']) && !$controller->cObj->checkIf($controller->pSetup['includeJS.'][$key . '.']['if.'])) {
676 continue;
677 }
678 if ($controller->pSetup['includeJS.'][$key . '.']['external']) {
679 $ss = $JSfile;
680 } else {
681 try {
682 $ss = GeneralUtility::makeInstance(FilePathSanitizer::class)->sanitize($JSfile);
683 } catch (\TYPO3\CMS\Core\Resource\Exception $e) {
684 $ss = null;
685 }
686 }
687 if ($ss) {
688 $jsConfig = &$controller->pSetup['includeJS.'][$key . '.'];
689 $type = $jsConfig['type'];
690 if (!$type) {
691 $type = 'text/javascript';
692 }
693 $crossorigin = $jsConfig['crossorigin'];
694 if (!$crossorigin && $jsConfig['integrity'] && $jsConfig['external']) {
695 $crossorigin = 'anonymous';
696 }
697 $pageRenderer->addJsFile(
698 $ss,
699 $type,
700 $jsConfig['external'] ? false : empty($jsConfig['disableCompression']),
701 (bool)$jsConfig['forceOnTop'],
702 $jsConfig['allWrap'],
703 (bool)$jsConfig['excludeFromConcatenation'],
704 $jsConfig['allWrap.']['splitChar'],
705 (bool)$jsConfig['async'],
706 $jsConfig['integrity'],
707 (bool)$jsConfig['defer'],
708 $crossorigin
709 );
710 unset($jsConfig);
711 }
712 }
713 }
714 }
715 if (isset($controller->pSetup['includeJSFooter.']) && is_array($controller->pSetup['includeJSFooter.'])) {
716 foreach ($controller->pSetup['includeJSFooter.'] as $key => $JSfile) {
717 if (!is_array($JSfile)) {
718 if (isset($controller->pSetup['includeJSFooter.'][$key . '.']['if.']) && !$controller->cObj->checkIf($controller->pSetup['includeJSFooter.'][$key . '.']['if.'])) {
719 continue;
720 }
721 if ($controller->pSetup['includeJSFooter.'][$key . '.']['external']) {
722 $ss = $JSfile;
723 } else {
724 try {
725 $ss = GeneralUtility::makeInstance(FilePathSanitizer::class)->sanitize($JSfile);
726 } catch (\TYPO3\CMS\Core\Resource\Exception $e) {
727 $ss = null;
728 }
729 }
730 if ($ss) {
731 $jsConfig = &$controller->pSetup['includeJSFooter.'][$key . '.'];
732 $type = $jsConfig['type'];
733 if (!$type) {
734 $type = 'text/javascript';
735 }
736 $crossorigin = $jsConfig['crossorigin'];
737 if (!$crossorigin && $jsConfig['integrity'] && $jsConfig['external']) {
738 $crossorigin = 'anonymous';
739 }
740 $pageRenderer->addJsFooterFile(
741 $ss,
742 $type,
743 $jsConfig['external'] ? false : empty($jsConfig['disableCompression']),
744 (bool)$jsConfig['forceOnTop'],
745 $jsConfig['allWrap'],
746 (bool)$jsConfig['excludeFromConcatenation'],
747 $jsConfig['allWrap.']['splitChar'],
748 (bool)$jsConfig['async'],
749 $jsConfig['integrity'],
750 (bool)$jsConfig['defer'],
751 $crossorigin
752 );
753 unset($jsConfig);
754 }
755 }
756 }
757 }
758 // Headerdata
759 if (isset($controller->pSetup['headerData.']) && is_array($controller->pSetup['headerData.'])) {
760 $pageRenderer->addHeaderData($controller->cObj->cObjGet($controller->pSetup['headerData.'], 'headerData.'));
761 }
762 // Footerdata
763 if (isset($controller->pSetup['footerData.']) && is_array($controller->pSetup['footerData.'])) {
764 $pageRenderer->addFooterData($controller->cObj->cObjGet($controller->pSetup['footerData.'], 'footerData.'));
765 }
766 $controller->generatePageTitle();
767
768 // @internal hook for EXT:seo, will be gone soon, do not use it in your own extensions
769 $_params = ['page' => $controller->page];
770 $_ref = '';
771 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['TYPO3\CMS\Frontend\Page\PageGenerator']['generateMetaTags'] ?? [] as $_funcRef) {
772 GeneralUtility::callUserFunction($_funcRef, $_params, $_ref);
773 }
774
775 $this->generateMetaTagHtml(
776 $controller->pSetup['meta.'] ?? [],
777 $controller->cObj
778 );
779
780 unset($controller->additionalHeaderData['JSCode']);
781 if (isset($controller->config['INTincScript']) && is_array($controller->config['INTincScript'])) {
782 $controller->additionalHeaderData['JSCode'] = $controller->JSCode;
783 // Storing the JSCode vars...
784 $controller->config['INTincScript_ext']['divKey'] = $controller->uniqueHash();
785 $controller->config['INTincScript_ext']['additionalHeaderData'] = $controller->additionalHeaderData;
786 // Storing the header-data array
787 $controller->config['INTincScript_ext']['additionalFooterData'] = $controller->additionalFooterData;
788 // Storing the footer-data array
789 $controller->config['INTincScript_ext']['additionalJavaScript'] = $controller->additionalJavaScript;
790 // Storing the JS-data array
791 $controller->config['INTincScript_ext']['additionalCSS'] = $controller->additionalCSS;
792 // Storing the Style-data array
793 $controller->additionalHeaderData = ['<!--HD_' . $controller->config['INTincScript_ext']['divKey'] . '-->'];
794 // Clearing the array
795 $controller->additionalFooterData = ['<!--FD_' . $controller->config['INTincScript_ext']['divKey'] . '-->'];
796 // Clearing the array
797 $controller->divSection .= '<!--TDS_' . $controller->config['INTincScript_ext']['divKey'] . '-->';
798 } else {
799 $controller->INTincScript_loadJSCode();
800 }
801 $scriptJsCode = '';
802
803 if ($controller->spamProtectEmailAddresses && $controller->spamProtectEmailAddresses !== 'ascii') {
804 $scriptJsCode = '
805 // decrypt helper function
806 function decryptCharcode(n,start,end,offset) {
807 n = n + offset;
808 if (offset > 0 && n > end) {
809 n = start + (n - end - 1);
810 } else if (offset < 0 && n < start) {
811 n = end - (start - n - 1);
812 }
813 return String.fromCharCode(n);
814 }
815 // decrypt string
816 function decryptString(enc,offset) {
817 var dec = "";
818 var len = enc.length;
819 for(var i=0; i < len; i++) {
820 var n = enc.charCodeAt(i);
821 if (n >= 0x2B && n <= 0x3A) {
822 dec += decryptCharcode(n,0x2B,0x3A,offset); // 0-9 . , - + / :
823 } else if (n >= 0x40 && n <= 0x5A) {
824 dec += decryptCharcode(n,0x40,0x5A,offset); // A-Z @
825 } else if (n >= 0x61 && n <= 0x7A) {
826 dec += decryptCharcode(n,0x61,0x7A,offset); // a-z
827 } else {
828 dec += enc.charAt(i);
829 }
830 }
831 return dec;
832 }
833 // decrypt spam-protected emails
834 function linkTo_UnCryptMailto(s) {
835 location.href = decryptString(s,' . $controller->spamProtectEmailAddresses * -1 . ');
836 }
837 ';
838 }
839 // Add inline JS
840 $inlineJS = '';
841 // defined in php
842 if (is_array($controller->inlineJS)) {
843 foreach ($controller->inlineJS as $key => $val) {
844 if (!is_array($val)) {
845 $inlineJS .= LF . $val . LF;
846 }
847 }
848 }
849 // defined in TS with page.inlineJS
850 // Javascript inline code
851 $inline = $controller->cObj->cObjGet($controller->pSetup['jsInline.'] ?? null, 'jsInline.');
852 if ($inline) {
853 $inlineJS .= LF . $inline . LF;
854 }
855 // Javascript inline code for Footer
856 $inlineFooterJs = $controller->cObj->cObjGet($controller->pSetup['jsFooterInline.'] ?? null, 'jsFooterInline.');
857 // Should minify?
858 if ($controller->config['config']['compressJs'] ?? false) {
859 $pageRenderer->enableCompressJavascript();
860 $minifyErrorScript = ($minifyErrorInline = '');
861 $scriptJsCode = GeneralUtility::minifyJavaScript($scriptJsCode, $minifyErrorScript);
862 if ($minifyErrorScript) {
863 $this->timeTracker->setTSlogMessage($minifyErrorScript, 3);
864 }
865 if ($inlineJS) {
866 $inlineJS = GeneralUtility::minifyJavaScript($inlineJS, $minifyErrorInline);
867 if ($minifyErrorInline) {
868 $this->timeTracker->setTSlogMessage($minifyErrorInline, 3);
869 }
870 }
871 if ($inlineFooterJs) {
872 $inlineFooterJs = GeneralUtility::minifyJavaScript($inlineFooterJs, $minifyErrorInline);
873 if ($minifyErrorInline) {
874 $this->timeTracker->setTSlogMessage($minifyErrorInline, 3);
875 }
876 }
877 }
878 if (!isset($controller->config['config']['removeDefaultJS']) || !$controller->config['config']['removeDefaultJS']) {
879 // include default and inlineJS
880 if ($scriptJsCode) {
881 $pageRenderer->addJsInlineCode('_scriptCode', $scriptJsCode, $controller->config['config']['compressJs']);
882 }
883 if ($inlineJS) {
884 $pageRenderer->addJsInlineCode('TS_inlineJS', $inlineJS, $controller->config['config']['compressJs']);
885 }
886 if ($inlineFooterJs) {
887 $pageRenderer->addJsFooterInlineCode('TS_inlineFooter', $inlineFooterJs, $controller->config['config']['compressJs']);
888 }
889 } elseif ($controller->config['config']['removeDefaultJS'] === 'external') {
890 /*
891 * This keeps inlineJS from *_INT Objects from being moved to external files.
892 * At this point in frontend rendering *_INT Objects only have placeholders instead
893 * of actual content so moving these placeholders to external files would
894 * a) break the JS file (syntax errors due to the placeholders)
895 * b) the needed JS would never get included to the page
896 * Therefore inlineJS from *_INT Objects must not be moved to external files but
897 * kept internal.
898 */
899 $inlineJSint = '';
900 $this->stripIntObjectPlaceholder($inlineJS, $inlineJSint);
901 if ($inlineJSint) {
902 $pageRenderer->addJsInlineCode('TS_inlineJSint', $inlineJSint, $controller->config['config']['compressJs']);
903 }
904 if (trim($scriptJsCode . $inlineJS)) {
905 $pageRenderer->addJsFile(GeneralUtility::writeJavaScriptContentToTemporaryFile($scriptJsCode . $inlineJS), 'text/javascript', $controller->config['config']['compressJs']);
906 }
907 if ($inlineFooterJs) {
908 $inlineFooterJSint = '';
909 $this->stripIntObjectPlaceholder($inlineFooterJs, $inlineFooterJSint);
910 if ($inlineFooterJSint) {
911 $pageRenderer->addJsFooterInlineCode('TS_inlineFooterJSint', $inlineFooterJSint, $controller->config['config']['compressJs']);
912 }
913 $pageRenderer->addJsFooterFile(GeneralUtility::writeJavaScriptContentToTemporaryFile($inlineFooterJs), 'text/javascript', $controller->config['config']['compressJs']);
914 }
915 } else {
916 // Include only inlineJS
917 if ($inlineJS) {
918 $pageRenderer->addJsInlineCode('TS_inlineJS', $inlineJS, $controller->config['config']['compressJs']);
919 }
920 if ($inlineFooterJs) {
921 $pageRenderer->addJsFooterInlineCode('TS_inlineFooter', $inlineFooterJs, $controller->config['config']['compressJs']);
922 }
923 }
924 if (isset($controller->pSetup['inlineLanguageLabelFiles.']) && is_array($controller->pSetup['inlineLanguageLabelFiles.'])) {
925 foreach ($controller->pSetup['inlineLanguageLabelFiles.'] as $key => $languageFile) {
926 if (is_array($languageFile)) {
927 continue;
928 }
929 $languageFileConfig = &$controller->pSetup['inlineLanguageLabelFiles.'][$key . '.'];
930 if (isset($languageFileConfig['if.']) && !$controller->cObj->checkIf($languageFileConfig['if.'])) {
931 continue;
932 }
933 $pageRenderer->addInlineLanguageLabelFile(
934 $languageFile,
935 $languageFileConfig['selectionPrefix'] ?: '',
936 $languageFileConfig['stripFromSelectionName'] ?: ''
937 );
938 }
939 }
940 if (isset($controller->pSetup['inlineSettings.']) && is_array($controller->pSetup['inlineSettings.'])) {
941 $pageRenderer->addInlineSettingArray('TS', $controller->pSetup['inlineSettings.']);
942 }
943 // Compression and concatenate settings
944 if ($controller->config['config']['compressCss'] ?? false) {
945 $pageRenderer->enableCompressCss();
946 }
947 if ($controller->config['config']['compressJs'] ?? false) {
948 $pageRenderer->enableCompressJavascript();
949 }
950 if ($controller->config['config']['concatenateCss'] ?? false) {
951 $pageRenderer->enableConcatenateCss();
952 }
953 if ($controller->config['config']['concatenateJs'] ?? false) {
954 $pageRenderer->enableConcatenateJavascript();
955 }
956 // Backward compatibility for old configuration
957 // @deprecated - remove this option in TYPO3 v10.0.
958 if ($controller->config['config']['concatenateJsAndCss'] ?? false) {
959 trigger_error('Setting config.concatenateJsAndCss is deprecated in favor of config.concatenateJs and config.concatenateCss, and will have no effect anymore in TYPO3 v10.0.', E_USER_DEPRECATED);
960 $pageRenderer->enableConcatenateCss();
961 $pageRenderer->enableConcatenateJavascript();
962 }
963 // Add header data block
964 if ($controller->additionalHeaderData) {
965 $pageRenderer->addHeaderData(implode(LF, $controller->additionalHeaderData));
966 }
967 // Add footer data block
968 if ($controller->additionalFooterData) {
969 $pageRenderer->addFooterData(implode(LF, $controller->additionalFooterData));
970 }
971 // Header complete, now add content
972 // Bodytag:
973 if ($controller->config['config']['disableBodyTag'] ?? false) {
974 $bodyTag = '';
975 } else {
976 $defBT = (isset($controller->pSetup['bodyTagCObject']) && $controller->pSetup['bodyTagCObject'])
977 ? $controller->cObj->cObjGetSingle($controller->pSetup['bodyTagCObject'], $controller->pSetup['bodyTagCObject.'], 'bodyTagCObject')
978 : '<body>';
979 $bodyTag = (isset($controller->pSetup['bodyTag']) && $controller->pSetup['bodyTag'])
980 ? $controller->pSetup['bodyTag']
981 : $defBT;
982 if (trim($controller->pSetup['bodyTagAdd'] ?? '')) {
983 $bodyTag = preg_replace('/>$/', '', trim($bodyTag)) . ' ' . trim($controller->pSetup['bodyTagAdd']) . '>';
984 }
985 }
986 $pageRenderer->addBodyContent(LF . $bodyTag);
987 // Div-sections
988 if ($controller->divSection) {
989 $pageRenderer->addBodyContent(LF . $controller->divSection);
990 }
991 // Page content
992 $pageRenderer->addBodyContent(LF . $pageContent);
993 if (!empty($controller->config['INTincScript']) && is_array($controller->config['INTincScript'])) {
994 // Store the serialized pageRenderer in configuration
995 $controller->config['INTincScript_ext']['pageRenderer'] = serialize($pageRenderer);
996 // Render complete page, keep placeholders for JavaScript and CSS
997 $pageContent = $pageRenderer->renderPageWithUncachedObjects($controller->config['INTincScript_ext']['divKey']);
998 } else {
999 // Render complete page
1000 $pageContent = $pageRenderer->render();
1001 }
1002 return $pageContent ?: '';
1003 }
1004
1005 /*************************
1006 *
1007 * Helper functions
1008 *
1009 *************************/
1010
1011 /**
1012 * Searches for placeholder created from *_INT cObjects, removes them from
1013 * $searchString and merges them to $intObjects
1014 *
1015 * @param string $searchString The String which should be cleaned from int-object markers
1016 * @param string $intObjects The String the found int-placeholders are moved to (for further processing)
1017 */
1018 protected function stripIntObjectPlaceholder(&$searchString, &$intObjects)
1019 {
1020 $tempArray = [];
1021 preg_match_all('/\\<\\!--INT_SCRIPT.[a-z0-9]*--\\>/', $searchString, $tempArray);
1022 $searchString = preg_replace('/\\<\\!--INT_SCRIPT.[a-z0-9]*--\\>/', '', $searchString);
1023 $intObjects = implode('', $tempArray[0]);
1024 }
1025
1026 /**
1027 * Generate meta tags from meta tag TypoScript
1028 *
1029 * @param array $metaTagTypoScript TypoScript configuration for meta tags (e.g. $GLOBALS['TSFE']->pSetup['meta.'])
1030 * @param ContentObjectRenderer $cObj
1031 */
1032 protected function generateMetaTagHtml(array $metaTagTypoScript, ContentObjectRenderer $cObj)
1033 {
1034 $pageRenderer = $this->getPageRenderer();
1035
1036 $typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
1037 $conf = $typoScriptService->convertTypoScriptArrayToPlainArray($metaTagTypoScript);
1038 foreach ($conf as $key => $properties) {
1039 $replace = false;
1040 if (is_array($properties)) {
1041 $nodeValue = $properties['_typoScriptNodeValue'] ?? '';
1042 $value = trim($cObj->stdWrap($nodeValue, $metaTagTypoScript[$key . '.']) ?? '');
1043 if ($value === '' && !empty($properties['value'])) {
1044 $value = $properties['value'];
1045 $replace = false;
1046 }
1047 } else {
1048 $value = $properties;
1049 }
1050
1051 $attribute = 'name';
1052 if ((is_array($properties) && !empty($properties['httpEquivalent'])) || strtolower($key) === 'refresh') {
1053 $attribute = 'http-equiv';
1054 }
1055 if (is_array($properties) && !empty($properties['attribute'])) {
1056 $attribute = $properties['attribute'];
1057 }
1058 if (is_array($properties) && !empty($properties['replace'])) {
1059 $replace = true;
1060 }
1061
1062 if (!is_array($value)) {
1063 $value = (array)$value;
1064 }
1065 foreach ($value as $subValue) {
1066 if (trim($subValue ?? '') !== '') {
1067 $pageRenderer->setMetaTag($attribute, $key, $subValue, [], $replace);
1068 }
1069 }
1070 }
1071 }
1072
1073 /**
1074 * @return PageRenderer
1075 */
1076 protected function getPageRenderer(): PageRenderer
1077 {
1078 return GeneralUtility::makeInstance(PageRenderer::class);
1079 }
1080
1081 /**
1082 * Adds inline CSS code, by respecting the inlineStyle2TempFile option
1083 *
1084 * @param TypoScriptFrontendController $controller
1085 * @param string $cssStyles the inline CSS styling
1086 * @param bool $excludeFromConcatenation option to see if it should be concatenated
1087 * @param string $inlineBlockName the block name to add it
1088 */
1089 protected function addCssToPageRenderer(TypoScriptFrontendController $controller, string $cssStyles, bool $excludeFromConcatenation, string $inlineBlockName)
1090 {
1091 if (empty($controller->config['config']['inlineStyle2TempFile'] ?? false)) {
1092 $this->getPageRenderer()->addCssInlineBlock($inlineBlockName, $cssStyles, !empty($controller->config['config']['compressCss'] ?? false));
1093 } else {
1094 $this->getPageRenderer()->addCssFile(
1095 GeneralUtility::writeStyleSheetContentToTemporaryFile($cssStyles),
1096 'stylesheet',
1097 'all',
1098 '',
1099 (bool)($controller->config['config']['compressCss'] ?? false),
1100 false,
1101 '',
1102 $excludeFromConcatenation
1103 );
1104 }
1105 }
1106
1107 /**
1108 * Generates the <html> tag by evaluting TypoScript configuration, usually found via:
1109 *
1110 * - Adding extra attributes in addition to pre-generated ones (e.g. "dir")
1111 * config.htmlTag.attributes.no-js = 1
1112 * config.htmlTag.attributes.empty-attribute =
1113 *
1114 * - Adding one full string (no stdWrap!) to the "<html $htmlTagAttributes {config.htmlTag_setParams}>" tag
1115 * config.htmlTag_setParams = string|"none"
1116 *
1117 * If config.htmlTag_setParams = none is set, even the pre-generated values are not added at all anymore.
1118 *
1119 * - "config.htmlTag_stdWrap" always applies over the whole compiled tag.
1120 *
1121 * @param array $htmlTagAttributes pre-generated attributes by doctype/direction etc. values.
1122 * @param array $configuration the TypoScript configuration "config." array
1123 * @param ContentObjectRenderer $cObj
1124 * @return string the full <html> tag as string
1125 */
1126 protected function generateHtmlTag(array $htmlTagAttributes, array $configuration, ContentObjectRenderer $cObj): string
1127 {
1128 if (is_array($configuration['htmlTag.']['attributes.'] ?? null)) {
1129 $attributeString = '';
1130 foreach ($configuration['htmlTag.']['attributes.'] as $attributeName => $value) {
1131 $attributeString .= ' ' . htmlspecialchars($attributeName) . ($value !== '' ? '="' . htmlspecialchars((string)$value) . '"' : '');
1132 // If e.g. "htmlTag.attributes.dir" is set, make sure it is not added again with "implodeAttributes()"
1133 if (isset($htmlTagAttributes[$attributeName])) {
1134 unset($htmlTagAttributes[$attributeName]);
1135 }
1136 }
1137 $attributeString = ltrim(GeneralUtility::implodeAttributes($htmlTagAttributes) . $attributeString);
1138 } elseif (($configuration['htmlTag_setParams'] ?? '') === 'none') {
1139 $attributeString = '';
1140 } elseif (isset($configuration['htmlTag_setParams'])) {
1141 $attributeString = $configuration['htmlTag_setParams'];
1142 } else {
1143 $attributeString = GeneralUtility::implodeAttributes($htmlTagAttributes);
1144 }
1145 $htmlTag = '<html' . ($attributeString ? ' ' . $attributeString : '') . '>';
1146 if (isset($configuration['htmlTag_stdWrap.'])) {
1147 $htmlTag = $cObj->stdWrap($htmlTag, $configuration['htmlTag_stdWrap.']);
1148 }
1149 return $htmlTag;
1150 }
1151
1152 /**
1153 * This request handler can handle any frontend request.
1154 *
1155 * @param ServerRequestInterface $request
1156 * @return bool If the request is not an eID request, TRUE otherwise FALSE
1157 */
1158 public function canHandleRequest(ServerRequestInterface $request): bool
1159 {
1160 return true;
1161 }
1162
1163 /**
1164 * Returns the priority - how eager the handler is to actually handle the
1165 * request.
1166 *
1167 * @return int The priority of the request handler.
1168 */
1169 public function getPriority(): int
1170 {
1171 return 50;
1172 }
1173 }