[!!!][TASK] Refactor client-side IRRE
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Page / PageRenderer.php
1 <?php
2 namespace TYPO3\CMS\Core\Page;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Routing\Router;
18 use TYPO3\CMS\Backend\Routing\UriBuilder;
19 use TYPO3\CMS\Backend\Template\DocumentTemplate;
20 use TYPO3\CMS\Core\Cache\CacheManager;
21 use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException;
22 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
23 use TYPO3\CMS\Core\Core\Environment;
24 use TYPO3\CMS\Core\Localization\LocalizationFactory;
25 use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;
26 use TYPO3\CMS\Core\Service\MarkerBasedTemplateService;
27 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29 use TYPO3\CMS\Core\Utility\PathUtility;
30 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
31
32 /**
33 * TYPO3 pageRender class
34 * This class render the HTML of a webpage, usable for BE and FE
35 */
36 class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface
37 {
38 // Constants for the part to be rendered
39 const PART_COMPLETE = 0;
40 const PART_HEADER = 1;
41 const PART_FOOTER = 2;
42
43 const REQUIREJS_SCOPE_CONFIG = 'config';
44 const REQUIREJS_SCOPE_RESOLVE = 'resolve';
45
46 /**
47 * @var bool
48 */
49 protected $compressJavascript = false;
50
51 /**
52 * @var bool
53 */
54 protected $compressCss = false;
55
56 /**
57 * @var bool
58 */
59 protected $removeLineBreaksFromTemplate = false;
60
61 /**
62 * @var bool
63 */
64 protected $concatenateJavascript = false;
65
66 /**
67 * @var bool
68 */
69 protected $concatenateCss = false;
70
71 /**
72 * @var bool
73 */
74 protected $moveJsFromHeaderToFooter = false;
75
76 /**
77 * @var \TYPO3\CMS\Core\Localization\Locales
78 */
79 protected $locales;
80
81 /**
82 * The language key
83 * Two character string or 'default'
84 *
85 * @var string
86 */
87 protected $lang;
88
89 /**
90 * List of language dependencies for actual language. This is used for local variants of a language
91 * that depend on their "main" language, like Brazilian Portuguese or Canadian French.
92 *
93 * @var array
94 */
95 protected $languageDependencies = [];
96
97 /**
98 * @var \TYPO3\CMS\Core\Resource\ResourceCompressor
99 */
100 protected $compressor;
101
102 // Arrays containing associative array for the included files
103 /**
104 * @var array
105 */
106 protected $jsFiles = [];
107
108 /**
109 * @var array
110 */
111 protected $jsFooterFiles = [];
112
113 /**
114 * @var array
115 */
116 protected $jsLibs = [];
117
118 /**
119 * @var array
120 */
121 protected $jsFooterLibs = [];
122
123 /**
124 * @var array
125 */
126 protected $cssFiles = [];
127
128 /**
129 * @var array
130 */
131 protected $cssLibs = [];
132
133 /**
134 * The title of the page
135 *
136 * @var string
137 */
138 protected $title;
139
140 /**
141 * Charset for the rendering
142 *
143 * @var string
144 */
145 protected $charSet;
146
147 /**
148 * @var string
149 */
150 protected $favIcon;
151
152 /**
153 * @var string
154 */
155 protected $baseUrl;
156
157 /**
158 * @var bool
159 */
160 protected $renderXhtml = true;
161
162 // Static header blocks
163 /**
164 * @var string
165 */
166 protected $xmlPrologAndDocType = '';
167
168 /**
169 * @var array
170 */
171 protected $metaTags = [];
172
173 /**
174 * META Tags added via the API
175 *
176 * @var array
177 */
178 protected $metaTagsByAPI = [];
179
180 /**
181 * @var array
182 */
183 protected $inlineComments = [];
184
185 /**
186 * @var array
187 */
188 protected $headerData = [];
189
190 /**
191 * @var array
192 */
193 protected $footerData = [];
194
195 /**
196 * @var string
197 */
198 protected $titleTag = '<title>|</title>';
199
200 /**
201 * @var string
202 */
203 protected $metaCharsetTag = '<meta http-equiv="Content-Type" content="text/html; charset=|" />';
204
205 /**
206 * @var string
207 */
208 protected $htmlTag = '<html>';
209
210 /**
211 * @var string
212 */
213 protected $headTag = '<head>';
214
215 /**
216 * @var string
217 */
218 protected $baseUrlTag = '<base href="|" />';
219
220 /**
221 * @var string
222 */
223 protected $iconMimeType = '';
224
225 /**
226 * @var string
227 */
228 protected $shortcutTag = '<link rel="shortcut icon" href="%1$s"%2$s />';
229
230 // Static inline code blocks
231 /**
232 * @var array
233 */
234 protected $jsInline = [];
235
236 /**
237 * @var array
238 */
239 protected $jsFooterInline = [];
240
241 /**
242 * @var array
243 */
244 protected $cssInline = [];
245
246 /**
247 * @var string
248 */
249 protected $bodyContent;
250
251 /**
252 * @var string
253 */
254 protected $templateFile;
255
256 // Paths to contributed libraries
257
258 /**
259 * default path to the requireJS library, relative to the typo3/ directory
260 * @var string
261 */
262 protected $requireJsPath = 'EXT:core/Resources/Public/JavaScript/Contrib/';
263
264 // Internal flags for JS-libraries
265 /**
266 * if set, the requireJS library is included
267 * @var bool
268 */
269 protected $addRequireJs = false;
270
271 /**
272 * Inline configuration for requireJS (internal)
273 * @var array
274 */
275 protected $requireJsConfig = [];
276
277 /**
278 * Module names of internal requireJS 'paths'
279 * @var array
280 */
281 protected $internalRequireJsPathModuleNames = [];
282
283 /**
284 * Inline configuration for requireJS (public)
285 * @var array
286 */
287 protected $publicRequireJsConfig = [];
288
289 /**
290 * @var array
291 */
292 protected $inlineLanguageLabels = [];
293
294 /**
295 * @var array
296 */
297 protected $inlineLanguageLabelFiles = [];
298
299 /**
300 * @var array
301 */
302 protected $inlineSettings = [];
303
304 /**
305 * @var array
306 */
307 protected $inlineJavascriptWrap = [];
308
309 /**
310 * @var array
311 */
312 protected $inlineCssWrap = [];
313
314 /**
315 * Saves error messages generated during compression
316 *
317 * @var string
318 */
319 protected $compressError = '';
320
321 /**
322 * Is empty string for HTML and ' /' for XHTML rendering
323 *
324 * @var string
325 */
326 protected $endingSlash = '';
327
328 /**
329 * @var MetaTagManagerRegistry
330 */
331 protected $metaTagRegistry;
332
333 /**
334 * @var FrontendInterface
335 */
336 protected static $cache = null;
337
338 /**
339 * @param string $templateFile Declare the used template file. Omit this parameter will use default template
340 */
341 public function __construct($templateFile = '')
342 {
343 $this->reset();
344 $this->locales = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\Locales::class);
345 if ($templateFile !== '') {
346 $this->templateFile = $templateFile;
347 }
348 $this->inlineJavascriptWrap = [
349 '<script type="text/javascript">' . LF . '/*<![CDATA[*/' . LF,
350 '/*]]>*/' . LF . '</script>' . LF
351 ];
352 $this->inlineCssWrap = [
353 '<style type="text/css">' . LF . '/*<![CDATA[*/' . LF . '<!-- ' . LF,
354 '-->' . LF . '/*]]>*/' . LF . '</style>' . LF
355 ];
356
357 $this->metaTagRegistry = GeneralUtility::makeInstance(MetaTagManagerRegistry::class);
358 $this->setMetaTag('name', 'generator', 'TYPO3 CMS');
359 }
360
361 /**
362 * @param FrontendInterface $cache
363 */
364 public static function setCache(FrontendInterface $cache)
365 {
366 static::$cache = $cache;
367 }
368
369 /**
370 * Reset all vars to initial values
371 */
372 protected function reset()
373 {
374 $this->templateFile = 'EXT:core/Resources/Private/Templates/PageRenderer.html';
375 $this->jsFiles = [];
376 $this->jsFooterFiles = [];
377 $this->jsInline = [];
378 $this->jsFooterInline = [];
379 $this->jsLibs = [];
380 $this->cssFiles = [];
381 $this->cssInline = [];
382 $this->metaTags = [];
383 $this->metaTagsByAPI = [];
384 $this->inlineComments = [];
385 $this->headerData = [];
386 $this->footerData = [];
387 }
388
389 /*****************************************************/
390 /* */
391 /* Public Setters */
392 /* */
393 /* */
394 /*****************************************************/
395 /**
396 * Sets the title
397 *
398 * @param string $title title of webpage
399 */
400 public function setTitle($title)
401 {
402 $this->title = $title;
403 }
404
405 /**
406 * Enables/disables rendering of XHTML code
407 *
408 * @param bool $enable Enable XHTML
409 */
410 public function setRenderXhtml($enable)
411 {
412 $this->renderXhtml = $enable;
413 }
414
415 /**
416 * Sets xml prolog and docType
417 *
418 * @param string $xmlPrologAndDocType Complete tags for xml prolog and docType
419 */
420 public function setXmlPrologAndDocType($xmlPrologAndDocType)
421 {
422 $this->xmlPrologAndDocType = $xmlPrologAndDocType;
423 }
424
425 /**
426 * Sets meta charset
427 *
428 * @param string $charSet Used charset
429 */
430 public function setCharSet($charSet)
431 {
432 $this->charSet = $charSet;
433 }
434
435 /**
436 * Sets language
437 *
438 * @param string $lang Used language
439 */
440 public function setLanguage($lang)
441 {
442 $this->lang = $lang;
443 $this->languageDependencies = [];
444
445 // Language is found. Configure it:
446 if (in_array($this->lang, $this->locales->getLocales())) {
447 $this->languageDependencies[] = $this->lang;
448 foreach ($this->locales->getLocaleDependencies($this->lang) as $language) {
449 $this->languageDependencies[] = $language;
450 }
451 }
452 }
453
454 /**
455 * Set the meta charset tag
456 *
457 * @param string $metaCharsetTag
458 */
459 public function setMetaCharsetTag($metaCharsetTag)
460 {
461 $this->metaCharsetTag = $metaCharsetTag;
462 }
463
464 /**
465 * Sets html tag
466 *
467 * @param string $htmlTag Html tag
468 */
469 public function setHtmlTag($htmlTag)
470 {
471 $this->htmlTag = $htmlTag;
472 }
473
474 /**
475 * Sets HTML head tag
476 *
477 * @param string $headTag HTML head tag
478 */
479 public function setHeadTag($headTag)
480 {
481 $this->headTag = $headTag;
482 }
483
484 /**
485 * Sets favicon
486 *
487 * @param string $favIcon
488 */
489 public function setFavIcon($favIcon)
490 {
491 $this->favIcon = $favIcon;
492 }
493
494 /**
495 * Sets icon mime type
496 *
497 * @param string $iconMimeType
498 */
499 public function setIconMimeType($iconMimeType)
500 {
501 $this->iconMimeType = $iconMimeType;
502 }
503
504 /**
505 * Sets HTML base URL
506 *
507 * @param string $baseUrl HTML base URL
508 */
509 public function setBaseUrl($baseUrl)
510 {
511 $this->baseUrl = $baseUrl;
512 }
513
514 /**
515 * Sets template file
516 *
517 * @param string $file
518 */
519 public function setTemplateFile($file)
520 {
521 $this->templateFile = $file;
522 }
523
524 /**
525 * Sets Content for Body
526 *
527 * @param string $content
528 */
529 public function setBodyContent($content)
530 {
531 $this->bodyContent = $content;
532 }
533
534 /**
535 * Sets path to requireJS library (relative to typo3 directory)
536 *
537 * @param string $path Path to requireJS library
538 */
539 public function setRequireJsPath($path)
540 {
541 $this->requireJsPath = $path;
542 }
543
544 /**
545 * @param string $scope
546 * @return array
547 */
548 public function getRequireJsConfig(string $scope = null): array
549 {
550 // return basic RequireJS configuration without shim, paths and packages
551 if ($scope === static::REQUIREJS_SCOPE_CONFIG) {
552 return array_replace_recursive(
553 $this->publicRequireJsConfig,
554 $this->filterArrayKeys(
555 $this->requireJsConfig,
556 ['shim', 'paths', 'packages'],
557 false
558 )
559 );
560 }
561 // return RequireJS configuration for resolving only shim, paths and packages
562 if ($scope === static::REQUIREJS_SCOPE_RESOLVE) {
563 return $this->filterArrayKeys(
564 $this->requireJsConfig,
565 ['shim', 'paths', 'packages'],
566 true
567 );
568 }
569 return [];
570 }
571
572 /*****************************************************/
573 /* */
574 /* Public Enablers / Disablers */
575 /* */
576 /* */
577 /*****************************************************/
578 /**
579 * Enables MoveJsFromHeaderToFooter
580 */
581 public function enableMoveJsFromHeaderToFooter()
582 {
583 $this->moveJsFromHeaderToFooter = true;
584 }
585
586 /**
587 * Disables MoveJsFromHeaderToFooter
588 */
589 public function disableMoveJsFromHeaderToFooter()
590 {
591 $this->moveJsFromHeaderToFooter = false;
592 }
593
594 /**
595 * Enables compression of javascript
596 */
597 public function enableCompressJavascript()
598 {
599 $this->compressJavascript = true;
600 }
601
602 /**
603 * Disables compression of javascript
604 */
605 public function disableCompressJavascript()
606 {
607 $this->compressJavascript = false;
608 }
609
610 /**
611 * Enables compression of css
612 */
613 public function enableCompressCss()
614 {
615 $this->compressCss = true;
616 }
617
618 /**
619 * Disables compression of css
620 */
621 public function disableCompressCss()
622 {
623 $this->compressCss = false;
624 }
625
626 /**
627 * Enables concatenation of js files
628 */
629 public function enableConcatenateJavascript()
630 {
631 $this->concatenateJavascript = true;
632 }
633
634 /**
635 * Disables concatenation of js files
636 */
637 public function disableConcatenateJavascript()
638 {
639 $this->concatenateJavascript = false;
640 }
641
642 /**
643 * Enables concatenation of css files
644 */
645 public function enableConcatenateCss()
646 {
647 $this->concatenateCss = true;
648 }
649
650 /**
651 * Disables concatenation of css files
652 */
653 public function disableConcatenateCss()
654 {
655 $this->concatenateCss = false;
656 }
657
658 /**
659 * Sets removal of all line breaks in template
660 */
661 public function enableRemoveLineBreaksFromTemplate()
662 {
663 $this->removeLineBreaksFromTemplate = true;
664 }
665
666 /**
667 * Unsets removal of all line breaks in template
668 */
669 public function disableRemoveLineBreaksFromTemplate()
670 {
671 $this->removeLineBreaksFromTemplate = false;
672 }
673
674 /**
675 * Enables Debug Mode
676 * This is a shortcut to switch off all compress/concatenate features to enable easier debug
677 */
678 public function enableDebugMode()
679 {
680 $this->compressJavascript = false;
681 $this->compressCss = false;
682 $this->concatenateCss = false;
683 $this->concatenateJavascript = false;
684 $this->removeLineBreaksFromTemplate = false;
685 }
686
687 /*****************************************************/
688 /* */
689 /* Public Getters */
690 /* */
691 /* */
692 /*****************************************************/
693 /**
694 * Gets the title
695 *
696 * @return string $title Title of webpage
697 */
698 public function getTitle()
699 {
700 return $this->title;
701 }
702
703 /**
704 * Gets the charSet
705 *
706 * @return string $charSet
707 */
708 public function getCharSet()
709 {
710 return $this->charSet;
711 }
712
713 /**
714 * Gets the language
715 *
716 * @return string $lang
717 */
718 public function getLanguage()
719 {
720 return $this->lang;
721 }
722
723 /**
724 * Returns rendering mode XHTML or HTML
725 *
726 * @return bool TRUE if XHTML, FALSE if HTML
727 */
728 public function getRenderXhtml()
729 {
730 return $this->renderXhtml;
731 }
732
733 /**
734 * Gets html tag
735 *
736 * @return string $htmlTag Html tag
737 */
738 public function getHtmlTag()
739 {
740 return $this->htmlTag;
741 }
742
743 /**
744 * Get meta charset
745 *
746 * @return string
747 */
748 public function getMetaCharsetTag()
749 {
750 return $this->metaCharsetTag;
751 }
752
753 /**
754 * Gets head tag
755 *
756 * @return string $tag Head tag
757 */
758 public function getHeadTag()
759 {
760 return $this->headTag;
761 }
762
763 /**
764 * Gets favicon
765 *
766 * @return string $favIcon
767 */
768 public function getFavIcon()
769 {
770 return $this->favIcon;
771 }
772
773 /**
774 * Gets icon mime type
775 *
776 * @return string $iconMimeType
777 */
778 public function getIconMimeType()
779 {
780 return $this->iconMimeType;
781 }
782
783 /**
784 * Gets HTML base URL
785 *
786 * @return string $url
787 */
788 public function getBaseUrl()
789 {
790 return $this->baseUrl;
791 }
792
793 /**
794 * Gets template file
795 *
796 * @return string
797 */
798 public function getTemplateFile()
799 {
800 return $this->templateFile;
801 }
802
803 /**
804 * Gets MoveJsFromHeaderToFooter
805 *
806 * @return bool
807 */
808 public function getMoveJsFromHeaderToFooter()
809 {
810 return $this->moveJsFromHeaderToFooter;
811 }
812
813 /**
814 * Gets compress of javascript
815 *
816 * @return bool
817 */
818 public function getCompressJavascript()
819 {
820 return $this->compressJavascript;
821 }
822
823 /**
824 * Gets compress of css
825 *
826 * @return bool
827 */
828 public function getCompressCss()
829 {
830 return $this->compressCss;
831 }
832
833 /**
834 * Gets concatenate of js files
835 *
836 * @return bool
837 */
838 public function getConcatenateJavascript()
839 {
840 return $this->concatenateJavascript;
841 }
842
843 /**
844 * Gets concatenate of css files
845 *
846 * @return bool
847 */
848 public function getConcatenateCss()
849 {
850 return $this->concatenateCss;
851 }
852
853 /**
854 * Gets remove of empty lines from template
855 *
856 * @return bool
857 */
858 public function getRemoveLineBreaksFromTemplate()
859 {
860 return $this->removeLineBreaksFromTemplate;
861 }
862
863 /**
864 * Gets content for body
865 *
866 * @return string
867 */
868 public function getBodyContent()
869 {
870 return $this->bodyContent;
871 }
872
873 /**
874 * Gets the inline language labels.
875 *
876 * @return array The inline language labels
877 */
878 public function getInlineLanguageLabels()
879 {
880 return $this->inlineLanguageLabels;
881 }
882
883 /**
884 * Gets the inline language files
885 *
886 * @return array
887 */
888 public function getInlineLanguageLabelFiles()
889 {
890 return $this->inlineLanguageLabelFiles;
891 }
892
893 /*****************************************************/
894 /* */
895 /* Public Functions to add Data */
896 /* */
897 /* */
898 /*****************************************************/
899
900 /**
901 * Sets a given meta tag
902 *
903 * @param string $type The type of the meta tag. Allowed values are property, name or http-equiv
904 * @param string $name The name of the property to add
905 * @param string $content The content of the meta tag
906 * @param array $subProperties Subproperties of the meta tag (like e.g. og:image:width)
907 * @param bool $replace Replace earlier set meta tag
908 * @throws \InvalidArgumentException
909 */
910 public function setMetaTag(string $type, string $name, string $content, array $subProperties = [], $replace = true)
911 {
912 // Lowercase all the things
913 $type = strtolower($type);
914 $name = strtolower($name);
915 if (!in_array($type, ['property', 'name', 'http-equiv'], true)) {
916 throw new \InvalidArgumentException(
917 'When setting a meta tag the only types allowed are property, name or http-equiv. "' . $type . '" given.',
918 1496402460
919 );
920 }
921
922 $manager = $this->metaTagRegistry->getManagerForProperty($name);
923 $manager->addProperty($name, $content, $subProperties, $replace, $type);
924 }
925
926 /**
927 * Returns the requested meta tag
928 *
929 * @param string $type
930 * @param string $name
931 *
932 * @return array
933 */
934 public function getMetaTag(string $type, string $name): array
935 {
936 // Lowercase all the things
937 $type = strtolower($type);
938 $name = strtolower($name);
939
940 $manager = $this->metaTagRegistry->getManagerForProperty($name);
941 $propertyContent = $manager->getProperty($name, $type);
942
943 if (!empty($propertyContent[0])) {
944 return [
945 'type' => $type,
946 'name' => $name,
947 'content' => $propertyContent[0]['content']
948 ];
949 }
950 return [];
951 }
952
953 /**
954 * Unset the requested meta tag
955 *
956 * @param string $type
957 * @param string $name
958 */
959 public function removeMetaTag(string $type, string $name)
960 {
961 // Lowercase all the things
962 $type = strtolower($type);
963 $name = strtolower($name);
964
965 $manager = $this->metaTagRegistry->getManagerForProperty($name);
966 $manager->removeProperty($name, $type);
967 }
968
969 /**
970 * Adds inline HTML comment
971 *
972 * @param string $comment
973 */
974 public function addInlineComment($comment)
975 {
976 if (!in_array($comment, $this->inlineComments)) {
977 $this->inlineComments[] = $comment;
978 }
979 }
980
981 /**
982 * Adds header data
983 *
984 * @param string $data Free header data for HTML header
985 */
986 public function addHeaderData($data)
987 {
988 if (!in_array($data, $this->headerData)) {
989 $this->headerData[] = $data;
990 }
991 }
992
993 /**
994 * Adds footer data
995 *
996 * @param string $data Free header data for HTML header
997 */
998 public function addFooterData($data)
999 {
1000 if (!in_array($data, $this->footerData)) {
1001 $this->footerData[] = $data;
1002 }
1003 }
1004
1005 /**
1006 * Adds JS Library. JS Library block is rendered on top of the JS files.
1007 *
1008 * @param string $name Arbitrary identifier
1009 * @param string $file File name
1010 * @param string $type Content Type
1011 * @param bool $compress Flag if library should be compressed
1012 * @param bool $forceOnTop Flag if added library should be inserted at begin of this block
1013 * @param string $allWrap
1014 * @param bool $excludeFromConcatenation
1015 * @param string $splitChar The char used to split the allWrap value, default is "|"
1016 * @param bool $async Flag if property 'async="async"' should be added to JavaScript tags
1017 * @param string $integrity Subresource Integrity (SRI)
1018 * @param bool $defer Flag if property 'defer="defer"' should be added to JavaScript tags
1019 * @param string $crossorigin CORS settings attribute
1020 */
1021 public function addJsLibrary($name, $file, $type = 'text/javascript', $compress = false, $forceOnTop = false, $allWrap = '', $excludeFromConcatenation = false, $splitChar = '|', $async = false, $integrity = '', $defer = false, $crossorigin = '')
1022 {
1023 if (!$type) {
1024 $type = 'text/javascript';
1025 }
1026 if (!in_array(strtolower($name), $this->jsLibs)) {
1027 $this->jsLibs[strtolower($name)] = [
1028 'file' => $file,
1029 'type' => $type,
1030 'section' => self::PART_HEADER,
1031 'compress' => $compress,
1032 'forceOnTop' => $forceOnTop,
1033 'allWrap' => $allWrap,
1034 'excludeFromConcatenation' => $excludeFromConcatenation,
1035 'splitChar' => $splitChar,
1036 'async' => $async,
1037 'integrity' => $integrity,
1038 'defer' => $defer,
1039 'crossorigin' => $crossorigin,
1040 ];
1041 }
1042 }
1043
1044 /**
1045 * Adds JS Library to Footer. JS Library block is rendered on top of the Footer JS files.
1046 *
1047 * @param string $name Arbitrary identifier
1048 * @param string $file File name
1049 * @param string $type Content Type
1050 * @param bool $compress Flag if library should be compressed
1051 * @param bool $forceOnTop Flag if added library should be inserted at begin of this block
1052 * @param string $allWrap
1053 * @param bool $excludeFromConcatenation
1054 * @param string $splitChar The char used to split the allWrap value, default is "|"
1055 * @param bool $async Flag if property 'async="async"' should be added to JavaScript tags
1056 * @param string $integrity Subresource Integrity (SRI)
1057 * @param bool $defer Flag if property 'defer="defer"' should be added to JavaScript tags
1058 * @param string $crossorigin CORS settings attribute
1059 */
1060 public function addJsFooterLibrary($name, $file, $type = 'text/javascript', $compress = false, $forceOnTop = false, $allWrap = '', $excludeFromConcatenation = false, $splitChar = '|', $async = false, $integrity = '', $defer = false, $crossorigin = '')
1061 {
1062 if (!$type) {
1063 $type = 'text/javascript';
1064 }
1065 $name .= '_jsFooterLibrary';
1066 if (!in_array(strtolower($name), $this->jsLibs)) {
1067 $this->jsLibs[strtolower($name)] = [
1068 'file' => $file,
1069 'type' => $type,
1070 'section' => self::PART_FOOTER,
1071 'compress' => $compress,
1072 'forceOnTop' => $forceOnTop,
1073 'allWrap' => $allWrap,
1074 'excludeFromConcatenation' => $excludeFromConcatenation,
1075 'splitChar' => $splitChar,
1076 'async' => $async,
1077 'integrity' => $integrity,
1078 'defer' => $defer,
1079 'crossorigin' => $crossorigin,
1080 ];
1081 }
1082 }
1083
1084 /**
1085 * Adds JS file
1086 *
1087 * @param string $file File name
1088 * @param string $type Content Type
1089 * @param bool $compress
1090 * @param bool $forceOnTop
1091 * @param string $allWrap
1092 * @param bool $excludeFromConcatenation
1093 * @param string $splitChar The char used to split the allWrap value, default is "|"
1094 * @param bool $async Flag if property 'async="async"' should be added to JavaScript tags
1095 * @param string $integrity Subresource Integrity (SRI)
1096 * @param bool $defer Flag if property 'defer="defer"' should be added to JavaScript tags
1097 * @param string $crossorigin CORS settings attribute
1098 */
1099 public function addJsFile($file, $type = 'text/javascript', $compress = true, $forceOnTop = false, $allWrap = '', $excludeFromConcatenation = false, $splitChar = '|', $async = false, $integrity = '', $defer = false, $crossorigin = '')
1100 {
1101 if (!$type) {
1102 $type = 'text/javascript';
1103 }
1104 if (!isset($this->jsFiles[$file])) {
1105 $this->jsFiles[$file] = [
1106 'file' => $file,
1107 'type' => $type,
1108 'section' => self::PART_HEADER,
1109 'compress' => $compress,
1110 'forceOnTop' => $forceOnTop,
1111 'allWrap' => $allWrap,
1112 'excludeFromConcatenation' => $excludeFromConcatenation,
1113 'splitChar' => $splitChar,
1114 'async' => $async,
1115 'integrity' => $integrity,
1116 'defer' => $defer,
1117 'crossorigin' => $crossorigin,
1118 ];
1119 }
1120 }
1121
1122 /**
1123 * Adds JS file to footer
1124 *
1125 * @param string $file File name
1126 * @param string $type Content Type
1127 * @param bool $compress
1128 * @param bool $forceOnTop
1129 * @param string $allWrap
1130 * @param bool $excludeFromConcatenation
1131 * @param string $splitChar The char used to split the allWrap value, default is "|"
1132 * @param bool $async Flag if property 'async="async"' should be added to JavaScript tags
1133 * @param string $integrity Subresource Integrity (SRI)
1134 * @param bool $defer Flag if property 'defer="defer"' should be added to JavaScript tags
1135 * @param string $crossorigin CORS settings attribute
1136 */
1137 public function addJsFooterFile($file, $type = 'text/javascript', $compress = true, $forceOnTop = false, $allWrap = '', $excludeFromConcatenation = false, $splitChar = '|', $async = false, $integrity = '', $defer = false, $crossorigin = '')
1138 {
1139 if (!$type) {
1140 $type = 'text/javascript';
1141 }
1142 if (!isset($this->jsFiles[$file])) {
1143 $this->jsFiles[$file] = [
1144 'file' => $file,
1145 'type' => $type,
1146 'section' => self::PART_FOOTER,
1147 'compress' => $compress,
1148 'forceOnTop' => $forceOnTop,
1149 'allWrap' => $allWrap,
1150 'excludeFromConcatenation' => $excludeFromConcatenation,
1151 'splitChar' => $splitChar,
1152 'async' => $async,
1153 'integrity' => $integrity,
1154 'defer' => $defer,
1155 'crossorigin' => $crossorigin,
1156 ];
1157 }
1158 }
1159
1160 /**
1161 * Adds JS inline code
1162 *
1163 * @param string $name
1164 * @param string $block
1165 * @param bool $compress
1166 * @param bool $forceOnTop
1167 */
1168 public function addJsInlineCode($name, $block, $compress = true, $forceOnTop = false)
1169 {
1170 if (!isset($this->jsInline[$name]) && !empty($block)) {
1171 $this->jsInline[$name] = [
1172 'code' => $block . LF,
1173 'section' => self::PART_HEADER,
1174 'compress' => $compress,
1175 'forceOnTop' => $forceOnTop
1176 ];
1177 }
1178 }
1179
1180 /**
1181 * Adds JS inline code to footer
1182 *
1183 * @param string $name
1184 * @param string $block
1185 * @param bool $compress
1186 * @param bool $forceOnTop
1187 */
1188 public function addJsFooterInlineCode($name, $block, $compress = true, $forceOnTop = false)
1189 {
1190 if (!isset($this->jsInline[$name]) && !empty($block)) {
1191 $this->jsInline[$name] = [
1192 'code' => $block . LF,
1193 'section' => self::PART_FOOTER,
1194 'compress' => $compress,
1195 'forceOnTop' => $forceOnTop
1196 ];
1197 }
1198 }
1199
1200 /**
1201 * Adds CSS file
1202 *
1203 * @param string $file
1204 * @param string $rel
1205 * @param string $media
1206 * @param string $title
1207 * @param bool $compress
1208 * @param bool $forceOnTop
1209 * @param string $allWrap
1210 * @param bool $excludeFromConcatenation
1211 * @param string $splitChar The char used to split the allWrap value, default is "|"
1212 * @param bool $inline
1213 */
1214 public function addCssFile($file, $rel = 'stylesheet', $media = 'all', $title = '', $compress = true, $forceOnTop = false, $allWrap = '', $excludeFromConcatenation = false, $splitChar = '|', $inline = false)
1215 {
1216 if (!isset($this->cssFiles[$file])) {
1217 $this->cssFiles[$file] = [
1218 'file' => $file,
1219 'rel' => $rel,
1220 'media' => $media,
1221 'title' => $title,
1222 'compress' => $compress,
1223 'forceOnTop' => $forceOnTop,
1224 'allWrap' => $allWrap,
1225 'excludeFromConcatenation' => $excludeFromConcatenation,
1226 'splitChar' => $splitChar,
1227 'inline' => $inline
1228 ];
1229 }
1230 }
1231
1232 /**
1233 * Adds CSS file
1234 *
1235 * @param string $file
1236 * @param string $rel
1237 * @param string $media
1238 * @param string $title
1239 * @param bool $compress
1240 * @param bool $forceOnTop
1241 * @param string $allWrap
1242 * @param bool $excludeFromConcatenation
1243 * @param string $splitChar The char used to split the allWrap value, default is "|"
1244 * @param bool $inline
1245 */
1246 public function addCssLibrary($file, $rel = 'stylesheet', $media = 'all', $title = '', $compress = true, $forceOnTop = false, $allWrap = '', $excludeFromConcatenation = false, $splitChar = '|', $inline = false)
1247 {
1248 if (!isset($this->cssLibs[$file])) {
1249 $this->cssLibs[$file] = [
1250 'file' => $file,
1251 'rel' => $rel,
1252 'media' => $media,
1253 'title' => $title,
1254 'compress' => $compress,
1255 'forceOnTop' => $forceOnTop,
1256 'allWrap' => $allWrap,
1257 'excludeFromConcatenation' => $excludeFromConcatenation,
1258 'splitChar' => $splitChar,
1259 'inline' => $inline
1260 ];
1261 }
1262 }
1263
1264 /**
1265 * Adds CSS inline code
1266 *
1267 * @param string $name
1268 * @param string $block
1269 * @param bool $compress
1270 * @param bool $forceOnTop
1271 */
1272 public function addCssInlineBlock($name, $block, $compress = false, $forceOnTop = false)
1273 {
1274 if (!isset($this->cssInline[$name]) && !empty($block)) {
1275 $this->cssInline[$name] = [
1276 'code' => $block,
1277 'compress' => $compress,
1278 'forceOnTop' => $forceOnTop
1279 ];
1280 }
1281 }
1282
1283 /**
1284 * Call function if you need the requireJS library
1285 * this automatically adds the JavaScript path of all loaded extensions in the requireJS path option
1286 * so it resolves names like TYPO3/CMS/MyExtension/MyJsFile to EXT:MyExtension/Resources/Public/JavaScript/MyJsFile.js
1287 * when using requireJS
1288 */
1289 public function loadRequireJs()
1290 {
1291 $this->addRequireJs = true;
1292 if (!empty($this->requireJsConfig) && !empty($this->publicRequireJsConfig)) {
1293 return;
1294 }
1295
1296 $loadedExtensions = ExtensionManagementUtility::getLoadedExtensionListArray();
1297 $isDevelopment = GeneralUtility::getApplicationContext()->isDevelopment();
1298 $cacheIdentifier = 'requireJS_' . md5(implode(',', $loadedExtensions) . ($isDevelopment ? ':dev' : '') . GeneralUtility::getIndpEnv('TYPO3_REQUEST_SCRIPT'));
1299 /** @var FrontendInterface $cache */
1300 $cache = static::$cache ?? GeneralUtility::makeInstance(CacheManager::class)->getCache('assets');
1301 $requireJsConfig = $cache->get($cacheIdentifier);
1302
1303 // if we did not get a configuration from the cache, compute and store it in the cache
1304 if (!isset($requireJsConfig['internal']) || !isset($requireJsConfig['public'])) {
1305 $requireJsConfig = $this->computeRequireJsConfig($isDevelopment, $loadedExtensions);
1306 $cache->set($cacheIdentifier, $requireJsConfig);
1307 }
1308
1309 $this->requireJsConfig = $requireJsConfig['internal'];
1310 $this->publicRequireJsConfig = $requireJsConfig['public'];
1311 $this->internalRequireJsPathModuleNames = $requireJsConfig['internalNames'];
1312 }
1313
1314 /**
1315 * Computes the RequireJS configuration, mainly consisting of the paths to the core and all extension JavaScript
1316 * resource folders plus some additional generic configuration.
1317 *
1318 * @param bool $isDevelopment
1319 * @param array $loadedExtensions
1320 * @return array The RequireJS configuration
1321 */
1322 protected function computeRequireJsConfig($isDevelopment, array $loadedExtensions)
1323 {
1324 // load all paths to map to package names / namespaces
1325 $requireJsConfig = [
1326 'public' => [],
1327 'internal' => [],
1328 'internalNames' => [],
1329 ];
1330
1331 // In order to avoid browser caching of JS files, adding a GET parameter to the files loaded via requireJS
1332 if ($isDevelopment) {
1333 $requireJsConfig['public']['urlArgs'] = 'bust=' . $GLOBALS['EXEC_TIME'];
1334 } else {
1335 $requireJsConfig['public']['urlArgs'] = 'bust=' . GeneralUtility::hmac(TYPO3_version . Environment::getProjectPath());
1336 }
1337 $corePath = ExtensionManagementUtility::extPath('core', 'Resources/Public/JavaScript/Contrib/');
1338 $corePath = PathUtility::getAbsoluteWebPath($corePath);
1339 // first, load all paths for the namespaces, and configure contrib libs.
1340 $requireJsConfig['public']['paths'] = [
1341 'jquery' => $corePath . '/jquery/jquery',
1342 'jquery-ui' => $corePath . 'jquery-ui',
1343 'datatables' => $corePath . 'jquery.dataTables',
1344 'nprogress' => $corePath . 'nprogress',
1345 'moment' => $corePath . 'moment',
1346 'cropper' => $corePath . 'cropper.min',
1347 'imagesloaded' => $corePath . 'imagesloaded.pkgd.min',
1348 'bootstrap' => $corePath . 'bootstrap/bootstrap',
1349 'twbs/bootstrap-datetimepicker' => $corePath . 'bootstrap-datetimepicker',
1350 'autosize' => $corePath . 'autosize',
1351 'taboverride' => $corePath . 'taboverride.min',
1352 'twbs/bootstrap-slider' => $corePath . 'bootstrap-slider.min',
1353 'jquery/autocomplete' => $corePath . 'jquery.autocomplete',
1354 'd3' => $corePath . 'd3/d3',
1355 'Sortable' => $corePath . 'Sortable.min',
1356 ];
1357 $requireJsConfig['public']['waitSeconds'] = 30;
1358 $requireJsConfig['public']['typo3BaseUrl'] = false;
1359 $publicPackageNames = ['core', 'frontend', 'backend'];
1360 foreach ($loadedExtensions as $packageName) {
1361 $jsPath = 'EXT:' . $packageName . '/Resources/Public/JavaScript/';
1362 $absoluteJsPath = GeneralUtility::getFileAbsFileName($jsPath);
1363 $fullJsPath = PathUtility::getAbsoluteWebPath($absoluteJsPath);
1364 $fullJsPath = rtrim($fullJsPath, '/');
1365 if (!empty($fullJsPath) && file_exists($absoluteJsPath)) {
1366 $type = in_array($packageName, $publicPackageNames, true) ? 'public' : 'internal';
1367 $requireJsConfig[$type]['paths']['TYPO3/CMS/' . GeneralUtility::underscoredToUpperCamelCase($packageName)] = $fullJsPath;
1368 }
1369 }
1370 // sanitize module names in internal 'paths'
1371 $internalPathModuleNames = array_keys($requireJsConfig['internal']['paths'] ?? []);
1372 $sanitizedInternalPathModuleNames = array_map(
1373 function ($moduleName) {
1374 // trim spaces and slashes & add ending slash
1375 return trim($moduleName, ' /') . '/';
1376 },
1377 $internalPathModuleNames
1378 );
1379 $requireJsConfig['internalNames'] = array_combine(
1380 $sanitizedInternalPathModuleNames,
1381 $internalPathModuleNames
1382 );
1383
1384 // check if additional AMD modules need to be loaded if a single AMD module is initialized
1385 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['RequireJS']['postInitializationModules'] ?? false)) {
1386 $this->addInlineSettingArray(
1387 'RequireJS.PostInitializationModules',
1388 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['RequireJS']['postInitializationModules']
1389 );
1390 }
1391
1392 return $requireJsConfig;
1393 }
1394
1395 /**
1396 * Add additional configuration to require js.
1397 *
1398 * Configuration will be merged recursive with overrule.
1399 *
1400 * To add another path mapping deliver the following configuration:
1401 * 'paths' => array(
1402 * 'EXTERN/mybootstrapjs' => 'sysext/.../twbs/bootstrap.min',
1403 * ),
1404 *
1405 * @param array $configuration The configuration that will be merged with existing one.
1406 */
1407 public function addRequireJsConfiguration(array $configuration)
1408 {
1409 if (TYPO3_MODE === 'BE') {
1410 // Load RequireJS in backend context at first. Doing this in FE could break the output
1411 $this->loadRequireJs();
1412 }
1413 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($this->requireJsConfig, $configuration);
1414 }
1415
1416 /**
1417 * Generates RequireJS loader HTML markup.
1418 *
1419 * @return string
1420 * @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
1421 */
1422 protected function getRequireJsLoader(): string
1423 {
1424 $html = '';
1425 $backendRequest = TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_BE;
1426 $backendUserLoggedIn = !empty($GLOBALS['BE_USER']->user['uid']);
1427
1428 // no backend request - basically frontend
1429 if (!$backendRequest) {
1430 $requireJsConfig = $this->getRequireJsConfig(static::REQUIREJS_SCOPE_CONFIG);
1431 $requireJsConfig['typo3BaseUrl'] = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH') . '?eID=requirejs';
1432 // backend request, but no backend user logged in
1433 } elseif (!$backendUserLoggedIn) {
1434 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
1435 $requireJsConfig = $this->getRequireJsConfig(static::REQUIREJS_SCOPE_CONFIG);
1436 $requireJsConfig['typo3BaseUrl'] = (string)$uriBuilder->buildUriFromRoute('ajax_core_requirejs');
1437 // backend request, having backend user logged in
1438 } else {
1439 $requireJsConfig = array_replace_recursive(
1440 $this->publicRequireJsConfig,
1441 $this->requireJsConfig
1442 );
1443 }
1444
1445 // add (probably filtered) RequireJS configuration
1446 $html .= GeneralUtility::wrapJS('var require = ' . json_encode($requireJsConfig)) . LF;
1447 // directly after that, include the require.js file
1448 $html .= '<script src="'
1449 . $this->processJsFile($this->requireJsPath . 'require.js')
1450 . '" type="text/javascript"></script>' . LF;
1451
1452 if (!empty($requireJsConfig['typo3BaseUrl'])) {
1453 $html .= '<script src="'
1454 . $this->processJsFile(
1455 'EXT:core/Resources/Public/JavaScript/requirejs-loader.js'
1456 )
1457 . '" type="text/javascript"></script>' . LF;
1458 }
1459
1460 return $html;
1461 }
1462
1463 /**
1464 * @param array $array
1465 * @param string[] $keys
1466 * @param bool $keep
1467 * @return array
1468 */
1469 protected function filterArrayKeys(array $array, array $keys, bool $keep = true): array
1470 {
1471 return array_filter(
1472 $array,
1473 function (string $key) use ($keys, $keep) {
1474 return in_array($key, $keys, true) === $keep;
1475 },
1476 ARRAY_FILTER_USE_KEY
1477 );
1478 }
1479
1480 /**
1481 * includes an AMD-compatible JS file by resolving the ModuleName, and then requires the file via a requireJS request,
1482 * additionally allowing to execute JavaScript code afterwards
1483 *
1484 * this function only works for AMD-ready JS modules, used like "define('TYPO3/CMS/Backend/FormEngine..."
1485 * in the JS file
1486 *
1487 * TYPO3/CMS/Backend/FormEngine =>
1488 * "TYPO3": Vendor Name
1489 * "CMS": Product Name
1490 * "Backend": Extension Name
1491 * "FormEngine": FileName in the Resources/Public/JavaScript folder
1492 *
1493 * @param string $mainModuleName Must be in the form of "TYPO3/CMS/PackageName/ModuleName" e.g. "TYPO3/CMS/Backend/FormEngine"
1494 * @param string $callBackFunction loaded right after the requireJS loading, must be wrapped in function() {}
1495 */
1496 public function loadRequireJsModule($mainModuleName, $callBackFunction = null)
1497 {
1498 $inlineCodeKey = $mainModuleName;
1499 // make sure requireJS is initialized
1500 $this->loadRequireJs();
1501 // move internal module path definition to public module definition
1502 // (since loading a module ends up disclosing the existence anyway)
1503 $baseModuleName = $this->findRequireJsBaseModuleName($mainModuleName);
1504 if ($baseModuleName !== null && isset($this->requireJsConfig['paths'][$baseModuleName])) {
1505 $this->publicRequireJsConfig['paths'][$baseModuleName] = $this->requireJsConfig['paths'][$baseModuleName];
1506 unset($this->requireJsConfig['paths'][$baseModuleName]);
1507 }
1508 // execute the main module, and load a possible callback function
1509 $javaScriptCode = 'require(["' . $mainModuleName . '"]';
1510 if ($callBackFunction !== null) {
1511 $inlineCodeKey .= sha1($callBackFunction);
1512 $javaScriptCode .= ', ' . $callBackFunction;
1513 }
1514 $javaScriptCode .= ');';
1515 $this->addJsInlineCode('RequireJS-Module-' . $inlineCodeKey, $javaScriptCode);
1516 }
1517
1518 /**
1519 * Determines requireJS base module name (if defined).
1520 *
1521 * @param string $moduleName
1522 * @return string|null
1523 */
1524 protected function findRequireJsBaseModuleName(string $moduleName)
1525 {
1526 // trim spaces and slashes & add ending slash
1527 $sanitizedModuleName = trim($moduleName, ' /') . '/';
1528 foreach ($this->internalRequireJsPathModuleNames as $sanitizedBaseModuleName => $baseModuleName) {
1529 if (strpos($sanitizedModuleName, $sanitizedBaseModuleName) === 0) {
1530 return $baseModuleName;
1531 }
1532 }
1533 return null;
1534 }
1535
1536 /**
1537 * Adds Javascript Inline Label. This will occur in TYPO3.lang - object
1538 * The label can be used in scripts with TYPO3.lang.<key>
1539 *
1540 * @param string $key
1541 * @param string $value
1542 */
1543 public function addInlineLanguageLabel($key, $value)
1544 {
1545 $this->inlineLanguageLabels[$key] = $value;
1546 }
1547
1548 /**
1549 * Adds Javascript Inline Label Array. This will occur in TYPO3.lang - object
1550 * The label can be used in scripts with TYPO3.lang.<key>
1551 * Array will be merged with existing array.
1552 *
1553 * @param array $array
1554 */
1555 public function addInlineLanguageLabelArray(array $array)
1556 {
1557 $this->inlineLanguageLabels = array_merge($this->inlineLanguageLabels, $array);
1558 }
1559
1560 /**
1561 * Gets labels to be used in JavaScript fetched from a locallang file.
1562 *
1563 * @param string $fileRef Input is a file-reference (see GeneralUtility::getFileAbsFileName). That file is expected to be a 'locallang.xlf' file containing a valid XML TYPO3 language structure.
1564 * @param string $selectionPrefix Prefix to select the correct labels (default: '')
1565 * @param string $stripFromSelectionName String to be removed from the label names in the output. (default: '')
1566 */
1567 public function addInlineLanguageLabelFile($fileRef, $selectionPrefix = '', $stripFromSelectionName = '')
1568 {
1569 $index = md5($fileRef . $selectionPrefix . $stripFromSelectionName);
1570 if ($fileRef && !isset($this->inlineLanguageLabelFiles[$index])) {
1571 $this->inlineLanguageLabelFiles[$index] = [
1572 'fileRef' => $fileRef,
1573 'selectionPrefix' => $selectionPrefix,
1574 'stripFromSelectionName' => $stripFromSelectionName
1575 ];
1576 }
1577 }
1578
1579 /**
1580 * Adds Javascript Inline Setting. This will occur in TYPO3.settings - object
1581 * The label can be used in scripts with TYPO3.setting.<key>
1582 *
1583 * @param string $namespace
1584 * @param string $key
1585 * @param mixed $value
1586 */
1587 public function addInlineSetting($namespace, $key, $value)
1588 {
1589 if ($namespace) {
1590 if (strpos($namespace, '.')) {
1591 $parts = explode('.', $namespace);
1592 $a = &$this->inlineSettings;
1593 foreach ($parts as $part) {
1594 $a = &$a[$part];
1595 }
1596 $a[$key] = $value;
1597 } else {
1598 $this->inlineSettings[$namespace][$key] = $value;
1599 }
1600 } else {
1601 $this->inlineSettings[$key] = $value;
1602 }
1603 }
1604
1605 /**
1606 * Adds Javascript Inline Setting. This will occur in TYPO3.settings - object
1607 * The label can be used in scripts with TYPO3.setting.<key>
1608 * Array will be merged with existing array.
1609 *
1610 * @param string $namespace
1611 * @param array $array
1612 */
1613 public function addInlineSettingArray($namespace, array $array)
1614 {
1615 if ($namespace) {
1616 if (strpos($namespace, '.')) {
1617 $parts = explode('.', $namespace);
1618 $a = &$this->inlineSettings;
1619 foreach ($parts as $part) {
1620 $a = &$a[$part];
1621 }
1622 $a = array_merge((array)$a, $array);
1623 } else {
1624 $this->inlineSettings[$namespace] = array_merge((array)$this->inlineSettings[$namespace], $array);
1625 }
1626 } else {
1627 $this->inlineSettings = array_merge($this->inlineSettings, $array);
1628 }
1629 }
1630
1631 /**
1632 * Adds content to body content
1633 *
1634 * @param string $content
1635 */
1636 public function addBodyContent($content)
1637 {
1638 $this->bodyContent .= $content;
1639 }
1640
1641 /*****************************************************/
1642 /* */
1643 /* Render Functions */
1644 /* */
1645 /*****************************************************/
1646 /**
1647 * Render the section (Header or Footer)
1648 *
1649 * @param int $part Section which should be rendered: self::PART_COMPLETE, self::PART_HEADER or self::PART_FOOTER
1650 * @return string Content of rendered section
1651 */
1652 public function render($part = self::PART_COMPLETE)
1653 {
1654 $this->prepareRendering();
1655 list($jsLibs, $jsFiles, $jsFooterFiles, $cssLibs, $cssFiles, $jsInline, $cssInline, $jsFooterInline, $jsFooterLibs) = $this->renderJavaScriptAndCss();
1656 $metaTags = implode(LF, array_merge($this->metaTags, $this->renderMetaTagsFromAPI()));
1657 $markerArray = $this->getPreparedMarkerArray($jsLibs, $jsFiles, $jsFooterFiles, $cssLibs, $cssFiles, $jsInline, $cssInline, $jsFooterInline, $jsFooterLibs, $metaTags);
1658 $template = $this->getTemplateForPart($part);
1659
1660 // The page renderer needs a full reset, even when only rendering one part of the page
1661 // This means that you can only register footer files *after* the header has been already rendered.
1662 // In case you render the footer part first, header files can only be added *after* the footer has been rendered
1663 $this->reset();
1664 $templateService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
1665 return trim($templateService->substituteMarkerArray($template, $markerArray, '###|###'));
1666 }
1667
1668 /**
1669 * Renders metaTags based on tags added via the API
1670 *
1671 * @return array
1672 */
1673 protected function renderMetaTagsFromAPI()
1674 {
1675 $metaTags = [];
1676 $metaTagManagers = $this->metaTagRegistry->getAllManagers();
1677 try {
1678 $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('pages');
1679 } catch (NoSuchCacheException $e) {
1680 $cache = null;
1681 }
1682
1683 foreach ($metaTagManagers as $manager => $managerObject) {
1684 $cacheIdentifier = $this->getTypoScriptFrontendController()->newHash . '-metatag-' . $manager;
1685
1686 $existingCacheEntry = false;
1687 if ($cache instanceof FrontendInterface && $properties = $cache->get($cacheIdentifier)) {
1688 $existingCacheEntry = true;
1689 } else {
1690 $properties = $managerObject->renderAllProperties();
1691 }
1692
1693 if (!empty($properties)) {
1694 $metaTags[] = $properties;
1695
1696 if ($cache instanceof FrontendInterface && !$existingCacheEntry && ($this->getTypoScriptFrontendController()->page['uid'] ?? false)) {
1697 $cache->set(
1698 $cacheIdentifier,
1699 $properties,
1700 ['pageId_' . $this->getTypoScriptFrontendController()->page['uid']],
1701 $this->getTypoScriptFrontendController()->get_cache_timeout()
1702 );
1703 }
1704 }
1705 }
1706 return $metaTags;
1707 }
1708
1709 /**
1710 * Render the page but not the JavaScript and CSS Files
1711 *
1712 * @param string $substituteHash The hash that is used for the placehoder markers
1713 * @internal
1714 * @return string Content of rendered section
1715 */
1716 public function renderPageWithUncachedObjects($substituteHash)
1717 {
1718 $this->prepareRendering();
1719 $markerArray = $this->getPreparedMarkerArrayForPageWithUncachedObjects($substituteHash);
1720 $template = $this->getTemplateForPart(self::PART_COMPLETE);
1721 $templateService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
1722 return trim($templateService->substituteMarkerArray($template, $markerArray, '###|###'));
1723 }
1724
1725 /**
1726 * Renders the JavaScript and CSS files that have been added during processing
1727 * of uncached content objects (USER_INT, COA_INT)
1728 *
1729 * @param string $cachedPageContent
1730 * @param string $substituteHash The hash that is used for the placehoder markers
1731 * @internal
1732 * @return string
1733 */
1734 public function renderJavaScriptAndCssForProcessingOfUncachedContentObjects($cachedPageContent, $substituteHash)
1735 {
1736 $this->prepareRendering();
1737 list($jsLibs, $jsFiles, $jsFooterFiles, $cssLibs, $cssFiles, $jsInline, $cssInline, $jsFooterInline, $jsFooterLibs) = $this->renderJavaScriptAndCss();
1738 $title = $this->title ? str_replace('|', htmlspecialchars($this->title), $this->titleTag) : '';
1739 $markerArray = [
1740 '<!-- ###TITLE' . $substituteHash . '### -->' => $title,
1741 '<!-- ###CSS_LIBS' . $substituteHash . '### -->' => $cssLibs,
1742 '<!-- ###CSS_INCLUDE' . $substituteHash . '### -->' => $cssFiles,
1743 '<!-- ###CSS_INLINE' . $substituteHash . '### -->' => $cssInline,
1744 '<!-- ###JS_INLINE' . $substituteHash . '### -->' => $jsInline,
1745 '<!-- ###JS_INCLUDE' . $substituteHash . '### -->' => $jsFiles,
1746 '<!-- ###JS_LIBS' . $substituteHash . '### -->' => $jsLibs,
1747 '<!-- ###META' . $substituteHash . '### -->' => implode(LF, array_merge($this->metaTags, $this->renderMetaTagsFromAPI())),
1748 '<!-- ###HEADERDATA' . $substituteHash . '### -->' => implode(LF, $this->headerData),
1749 '<!-- ###FOOTERDATA' . $substituteHash . '### -->' => implode(LF, $this->footerData),
1750 '<!-- ###JS_LIBS_FOOTER' . $substituteHash . '### -->' => $jsFooterLibs,
1751 '<!-- ###JS_INCLUDE_FOOTER' . $substituteHash . '### -->' => $jsFooterFiles,
1752 '<!-- ###JS_INLINE_FOOTER' . $substituteHash . '### -->' => $jsFooterInline
1753 ];
1754 foreach ($markerArray as $placeHolder => $content) {
1755 $cachedPageContent = str_replace($placeHolder, $content, $cachedPageContent);
1756 }
1757 $this->reset();
1758 return $cachedPageContent;
1759 }
1760
1761 /**
1762 * Remove ending slashes from static header block
1763 * if the page is being rendered as html (not xhtml)
1764 * and define property $this->endingSlash for further use
1765 */
1766 protected function prepareRendering()
1767 {
1768 if ($this->getRenderXhtml()) {
1769 $this->endingSlash = ' /';
1770 } else {
1771 $this->metaCharsetTag = str_replace(' />', '>', $this->metaCharsetTag);
1772 $this->baseUrlTag = str_replace(' />', '>', $this->baseUrlTag);
1773 $this->shortcutTag = str_replace(' />', '>', $this->shortcutTag);
1774 $this->endingSlash = '';
1775 }
1776 }
1777
1778 /**
1779 * Renders all JavaScript and CSS
1780 *
1781 * @return array<string>
1782 */
1783 protected function renderJavaScriptAndCss()
1784 {
1785 $this->executePreRenderHook();
1786 $mainJsLibs = $this->renderMainJavaScriptLibraries();
1787 if ($this->concatenateJavascript || $this->concatenateCss) {
1788 // Do the file concatenation
1789 $this->doConcatenate();
1790 }
1791 if ($this->compressCss || $this->compressJavascript) {
1792 // Do the file compression
1793 $this->doCompress();
1794 }
1795 $this->executeRenderPostTransformHook();
1796 $cssLibs = $this->renderCssLibraries();
1797 $cssFiles = $this->renderCssFiles();
1798 $cssInline = $this->renderCssInline();
1799 list($jsLibs, $jsFooterLibs) = $this->renderAdditionalJavaScriptLibraries();
1800 list($jsFiles, $jsFooterFiles) = $this->renderJavaScriptFiles();
1801 list($jsInline, $jsFooterInline) = $this->renderInlineJavaScript();
1802 $jsLibs = $mainJsLibs . $jsLibs;
1803 if ($this->moveJsFromHeaderToFooter) {
1804 $jsFooterLibs = $jsLibs . LF . $jsFooterLibs;
1805 $jsLibs = '';
1806 $jsFooterFiles = $jsFiles . LF . $jsFooterFiles;
1807 $jsFiles = '';
1808 $jsFooterInline = $jsInline . LF . $jsFooterInline;
1809 $jsInline = '';
1810 }
1811 $this->executePostRenderHook($jsLibs, $jsFiles, $jsFooterFiles, $cssLibs, $cssFiles, $jsInline, $cssInline, $jsFooterInline, $jsFooterLibs);
1812 return [$jsLibs, $jsFiles, $jsFooterFiles, $cssLibs, $cssFiles, $jsInline, $cssInline, $jsFooterInline, $jsFooterLibs];
1813 }
1814
1815 /**
1816 * Fills the marker array with the given strings and trims each value
1817 *
1818 * @param string $jsLibs
1819 * @param string $jsFiles
1820 * @param string $jsFooterFiles
1821 * @param string $cssLibs
1822 * @param string $cssFiles
1823 * @param string $jsInline
1824 * @param string $cssInline
1825 * @param string $jsFooterInline
1826 * @param string $jsFooterLibs
1827 * @param string $metaTags
1828 * @return array Marker array
1829 */
1830 protected function getPreparedMarkerArray($jsLibs, $jsFiles, $jsFooterFiles, $cssLibs, $cssFiles, $jsInline, $cssInline, $jsFooterInline, $jsFooterLibs, $metaTags)
1831 {
1832 $markerArray = [
1833 'XMLPROLOG_DOCTYPE' => $this->xmlPrologAndDocType,
1834 'HTMLTAG' => $this->htmlTag,
1835 'HEADTAG' => $this->headTag,
1836 'METACHARSET' => $this->charSet ? str_replace('|', htmlspecialchars($this->charSet), $this->metaCharsetTag) : '',
1837 'INLINECOMMENT' => $this->inlineComments ? LF . LF . '<!-- ' . LF . implode(LF, $this->inlineComments) . '-->' . LF . LF : '',
1838 'BASEURL' => $this->baseUrl ? str_replace('|', $this->baseUrl, $this->baseUrlTag) : '',
1839 'SHORTCUT' => $this->favIcon ? sprintf($this->shortcutTag, htmlspecialchars($this->favIcon), $this->iconMimeType) : '',
1840 'CSS_LIBS' => $cssLibs,
1841 'CSS_INCLUDE' => $cssFiles,
1842 'CSS_INLINE' => $cssInline,
1843 'JS_INLINE' => $jsInline,
1844 'JS_INCLUDE' => $jsFiles,
1845 'JS_LIBS' => $jsLibs,
1846 'TITLE' => $this->title ? str_replace('|', htmlspecialchars($this->title), $this->titleTag) : '',
1847 'META' => $metaTags,
1848 'HEADERDATA' => $this->headerData ? implode(LF, $this->headerData) : '',
1849 'FOOTERDATA' => $this->footerData ? implode(LF, $this->footerData) : '',
1850 'JS_LIBS_FOOTER' => $jsFooterLibs,
1851 'JS_INCLUDE_FOOTER' => $jsFooterFiles,
1852 'JS_INLINE_FOOTER' => $jsFooterInline,
1853 'BODY' => $this->bodyContent
1854 ];
1855 $markerArray = array_map('trim', $markerArray);
1856 return $markerArray;
1857 }
1858
1859 /**
1860 * Fills the marker array with the given strings and trims each value
1861 *
1862 * @param string $substituteHash The hash that is used for the placehoder markers
1863 * @return array Marker array
1864 */
1865 protected function getPreparedMarkerArrayForPageWithUncachedObjects($substituteHash)
1866 {
1867 $markerArray = [
1868 'XMLPROLOG_DOCTYPE' => $this->xmlPrologAndDocType,
1869 'HTMLTAG' => $this->htmlTag,
1870 'HEADTAG' => $this->headTag,
1871 'METACHARSET' => $this->charSet ? str_replace('|', htmlspecialchars($this->charSet), $this->metaCharsetTag) : '',
1872 'INLINECOMMENT' => $this->inlineComments ? LF . LF . '<!-- ' . LF . implode(LF, $this->inlineComments) . '-->' . LF . LF : '',
1873 'BASEURL' => $this->baseUrl ? str_replace('|', $this->baseUrl, $this->baseUrlTag) : '',
1874 'SHORTCUT' => $this->favIcon ? sprintf($this->shortcutTag, htmlspecialchars($this->favIcon), $this->iconMimeType) : '',
1875 'META' => '<!-- ###META' . $substituteHash . '### -->',
1876 'BODY' => $this->bodyContent,
1877 'TITLE' => '<!-- ###TITLE' . $substituteHash . '### -->',
1878 'CSS_LIBS' => '<!-- ###CSS_LIBS' . $substituteHash . '### -->',
1879 'CSS_INCLUDE' => '<!-- ###CSS_INCLUDE' . $substituteHash . '### -->',
1880 'CSS_INLINE' => '<!-- ###CSS_INLINE' . $substituteHash . '### -->',
1881 'JS_INLINE' => '<!-- ###JS_INLINE' . $substituteHash . '### -->',
1882 'JS_INCLUDE' => '<!-- ###JS_INCLUDE' . $substituteHash . '### -->',
1883 'JS_LIBS' => '<!-- ###JS_LIBS' . $substituteHash . '### -->',
1884 'HEADERDATA' => '<!-- ###HEADERDATA' . $substituteHash . '### -->',
1885 'FOOTERDATA' => '<!-- ###FOOTERDATA' . $substituteHash . '### -->',
1886 'JS_LIBS_FOOTER' => '<!-- ###JS_LIBS_FOOTER' . $substituteHash . '### -->',
1887 'JS_INCLUDE_FOOTER' => '<!-- ###JS_INCLUDE_FOOTER' . $substituteHash . '### -->',
1888 'JS_INLINE_FOOTER' => '<!-- ###JS_INLINE_FOOTER' . $substituteHash . '### -->'
1889 ];
1890 $markerArray = array_map('trim', $markerArray);
1891 return $markerArray;
1892 }
1893
1894 /**
1895 * Reads the template file and returns the requested part as string
1896 *
1897 * @param int $part
1898 * @return string
1899 */
1900 protected function getTemplateForPart($part)
1901 {
1902 $templateFile = GeneralUtility::getFileAbsFileName($this->templateFile);
1903 if (is_file($templateFile)) {
1904 $template = file_get_contents($templateFile);
1905 if ($this->removeLineBreaksFromTemplate) {
1906 $template = strtr($template, [LF => '', CR => '']);
1907 }
1908 if ($part !== self::PART_COMPLETE) {
1909 $templatePart = explode('###BODY###', $template);
1910 $template = $templatePart[$part - 1];
1911 }
1912 } else {
1913 $template = '';
1914 }
1915 return $template;
1916 }
1917
1918 /**
1919 * Helper function for render the main JavaScript libraries,
1920 * currently: RequireJS
1921 *
1922 * @return string Content with JavaScript libraries
1923 */
1924 protected function renderMainJavaScriptLibraries()
1925 {
1926 $out = '';
1927
1928 // Include RequireJS
1929 if ($this->addRequireJs) {
1930 $out .= $this->getRequireJsLoader();
1931 }
1932
1933 $this->loadJavaScriptLanguageStrings();
1934 if (TYPO3_MODE === 'BE') {
1935 $noBackendUserLoggedIn = empty($GLOBALS['BE_USER']->user['uid']);
1936 $this->addAjaxUrlsToInlineSettings($noBackendUserLoggedIn);
1937 }
1938 $inlineSettings = '';
1939 $languageLabels = $this->parseLanguageLabelsForJavaScript();
1940 if (!empty($languageLabels)) {
1941 $inlineSettings .= 'TYPO3.lang = ' . json_encode($languageLabels) . ';';
1942 }
1943 $inlineSettings .= $this->inlineSettings ? 'TYPO3.settings = ' . json_encode($this->inlineSettings) . ';' : '';
1944
1945 if ($inlineSettings !== '') {
1946 // make sure the global TYPO3 is available
1947 $inlineSettings = 'var TYPO3 = TYPO3 || {};' . CRLF . $inlineSettings;
1948 $out .= $this->inlineJavascriptWrap[0] . $inlineSettings . $this->inlineJavascriptWrap[1];
1949 }
1950
1951 return $out;
1952 }
1953
1954 /**
1955 * Converts the language labels for usage in JavaScript
1956 *
1957 * @return array
1958 */
1959 protected function parseLanguageLabelsForJavaScript(): array
1960 {
1961 if (empty($this->inlineLanguageLabels)) {
1962 return [];
1963 }
1964
1965 $labels = [];
1966 foreach ($this->inlineLanguageLabels as $key => $translationUnit) {
1967 if (is_array($translationUnit)) {
1968 $translationUnit = current($translationUnit);
1969 $labels[$key] = $translationUnit['target'] ?? $translationUnit['source'];
1970 } else {
1971 $labels[$key] = $translationUnit;
1972 }
1973 }
1974
1975 return $labels;
1976 }
1977
1978 /**
1979 * Load the language strings into JavaScript
1980 */
1981 protected function loadJavaScriptLanguageStrings()
1982 {
1983 if (!empty($this->inlineLanguageLabelFiles)) {
1984 foreach ($this->inlineLanguageLabelFiles as $languageLabelFile) {
1985 $this->includeLanguageFileForInline($languageLabelFile['fileRef'], $languageLabelFile['selectionPrefix'], $languageLabelFile['stripFromSelectionName']);
1986 }
1987 }
1988 $this->inlineLanguageLabelFiles = [];
1989 // Convert settings back to UTF-8 since json_encode() only works with UTF-8:
1990 if ($this->getCharSet() && $this->getCharSet() !== 'utf-8' && is_array($this->inlineSettings)) {
1991 $this->convertCharsetRecursivelyToUtf8($this->inlineSettings, $this->getCharSet());
1992 }
1993 }
1994
1995 /**
1996 * Small helper function to convert charsets for arrays into utf-8
1997 *
1998 * @param mixed $data given by reference (string/array usually)
1999 * @param string $fromCharset convert FROM this charset
2000 */
2001 protected function convertCharsetRecursivelyToUtf8(&$data, string $fromCharset)
2002 {
2003 foreach ($data as $key => $value) {
2004 if (is_array($data[$key])) {
2005 $this->convertCharsetRecursivelyToUtf8($data[$key], $fromCharset);
2006 } elseif (is_string($data[$key])) {
2007 $data[$key] = mb_convert_encoding($data[$key], 'utf-8', $fromCharset);
2008 }
2009 }
2010 }
2011
2012 /**
2013 * Make URLs to all backend ajax handlers available as inline setting.
2014 *
2015 * @param bool $publicRoutesOnly
2016 */
2017 protected function addAjaxUrlsToInlineSettings(bool $publicRoutesOnly = false)
2018 {
2019 $ajaxUrls = [];
2020 // Add the ajax-based routes
2021 /** @var UriBuilder $uriBuilder */
2022 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
2023 /** @var Router $router */
2024 $router = GeneralUtility::makeInstance(Router::class);
2025 $routes = $router->getRoutes();
2026 foreach ($routes as $routeIdentifier => $route) {
2027 if ($publicRoutesOnly && $route->getOption('access') !== 'public') {
2028 continue;
2029 }
2030 if ($route->getOption('ajax')) {
2031 $uri = (string)$uriBuilder->buildUriFromRoute($routeIdentifier);
2032 // use the shortened value in order to use this in JavaScript
2033 $routeIdentifier = str_replace('ajax_', '', $routeIdentifier);
2034 $ajaxUrls[$routeIdentifier] = $uri;
2035 }
2036 }
2037
2038 $this->inlineSettings['ajaxUrls'] = $ajaxUrls;
2039 }
2040
2041 /**
2042 * Render CSS library files
2043 *
2044 * @return string
2045 */
2046 protected function renderCssLibraries()
2047 {
2048 $cssFiles = '';
2049 if (!empty($this->cssLibs)) {
2050 foreach ($this->cssLibs as $file => $properties) {
2051 $tag = $this->createCssTag($properties, $file);
2052 if ($properties['forceOnTop']) {
2053 $cssFiles = $tag . $cssFiles;
2054 } else {
2055 $cssFiles .= $tag;
2056 }
2057 }
2058 }
2059 return $cssFiles;
2060 }
2061
2062 /**
2063 * Render CSS files
2064 *
2065 * @return string
2066 */
2067 protected function renderCssFiles()
2068 {
2069 $cssFiles = '';
2070 if (!empty($this->cssFiles)) {
2071 foreach ($this->cssFiles as $file => $properties) {
2072 $tag = $this->createCssTag($properties, $file);
2073 if ($properties['forceOnTop']) {
2074 $cssFiles = $tag . $cssFiles;
2075 } else {
2076 $cssFiles .= $tag;
2077 }
2078 }
2079 }
2080 return $cssFiles;
2081 }
2082
2083 /**
2084 * Create link (inline=0) or style (inline=1) tag
2085 *
2086 * @param array $properties
2087 * @param string $file
2088 * @return string
2089 */
2090 private function createCssTag(array $properties, string $file): string
2091 {
2092 if ($properties['inline'] && @is_file($file)) {
2093 $tag = $this->createInlineCssTagFromFile($file, $properties);
2094 } else {
2095 $href = $this->getStreamlinedFileName($file);
2096 $tag = '<link rel="' . htmlspecialchars($properties['rel'])
2097 . '" type="text/css" href="' . htmlspecialchars($href)
2098 . '" media="' . htmlspecialchars($properties['media']) . '"'
2099 . ($properties['title'] ? ' title="' . htmlspecialchars($properties['title']) . '"' : '')
2100 . $this->endingSlash . '>';
2101 }
2102 if ($properties['allWrap']) {
2103 $wrapArr = explode($properties['splitChar'] ?: '|', $properties['allWrap'], 2);
2104 $tag = $wrapArr[0] . $tag . $wrapArr[1];
2105 }
2106 $tag .= LF;
2107
2108 return $tag;
2109 }
2110
2111 /**
2112 * Render inline CSS
2113 *
2114 * @return string
2115 */
2116 protected function renderCssInline()
2117 {
2118 $cssInline = '';
2119 if (!empty($this->cssInline)) {
2120 foreach ($this->cssInline as $name => $properties) {
2121 $cssCode = '/*' . htmlspecialchars($name) . '*/' . LF . $properties['code'] . LF;
2122 if ($properties['forceOnTop']) {
2123 $cssInline = $cssCode . $cssInline;
2124 } else {
2125 $cssInline .= $cssCode;
2126 }
2127 }
2128 $cssInline = $this->inlineCssWrap[0] . $cssInline . $this->inlineCssWrap[1];
2129 }
2130 return $cssInline;
2131 }
2132
2133 /**
2134 * Render JavaScipt libraries
2135 *
2136 * @return array<string> jsLibs and jsFooterLibs strings
2137 */
2138 protected function renderAdditionalJavaScriptLibraries()
2139 {
2140 $jsLibs = '';
2141 $jsFooterLibs = '';
2142 if (!empty($this->jsLibs)) {
2143 foreach ($this->jsLibs as $properties) {
2144 $properties['file'] = $this->getStreamlinedFileName($properties['file']);
2145 $async = $properties['async'] ? ' async="async"' : '';
2146 $defer = $properties['defer'] ? ' defer="defer"' : '';
2147 $integrity = $properties['integrity'] ? ' integrity="' . htmlspecialchars($properties['integrity']) . '"' : '';
2148 $crossorigin = $properties['crossorigin'] ? ' crossorigin="' . htmlspecialchars($properties['crossorigin']) . '"' : '';
2149 $tag = '<script src="' . htmlspecialchars($properties['file']) . '" type="' . htmlspecialchars($properties['type']) . '"' . $async . $defer . $integrity . $crossorigin . '></script>';
2150 if ($properties['allWrap']) {
2151 $wrapArr = explode($properties['splitChar'] ?: '|', $properties['allWrap'], 2);
2152 $tag = $wrapArr[0] . $tag . $wrapArr[1];
2153 }
2154 $tag .= LF;
2155 if ($properties['forceOnTop']) {
2156 if ($properties['section'] === self::PART_HEADER) {
2157 $jsLibs = $tag . $jsLibs;
2158 } else {
2159 $jsFooterLibs = $tag . $jsFooterLibs;
2160 }
2161 } else {
2162 if ($properties['section'] === self::PART_HEADER) {
2163 $jsLibs .= $tag;
2164 } else {
2165 $jsFooterLibs .= $tag;
2166 }
2167 }
2168 }
2169 }
2170 if ($this->moveJsFromHeaderToFooter) {
2171 $jsFooterLibs = $jsLibs . LF . $jsFooterLibs;
2172 $jsLibs = '';
2173 }
2174 return [$jsLibs, $jsFooterLibs];
2175 }
2176
2177 /**
2178 * Render JavaScript files
2179 *
2180 * @return array<string> jsFiles and jsFooterFiles strings
2181 */
2182 protected function renderJavaScriptFiles()
2183 {
2184 $jsFiles = '';
2185 $jsFooterFiles = '';
2186 if (!empty($this->jsFiles)) {
2187 foreach ($this->jsFiles as $file => $properties) {
2188 $file = $this->getStreamlinedFileName($file);
2189 $async = $properties['async'] ? ' async="async"' : '';
2190 $defer = $properties['defer'] ? ' defer="defer"' : '';
2191 $integrity = $properties['integrity'] ? ' integrity="' . htmlspecialchars($properties['integrity']) . '"' : '';
2192 $crossorigin = $properties['crossorigin'] ? ' crossorigin="' . htmlspecialchars($properties['crossorigin']) . '"' : '';
2193 $tag = '<script src="' . htmlspecialchars($file) . '" type="' . htmlspecialchars($properties['type']) . '"' . $async . $defer . $integrity . $crossorigin . '></script>';
2194 if ($properties['allWrap']) {
2195 $wrapArr = explode($properties['splitChar'] ?: '|', $properties['allWrap'], 2);
2196 $tag = $wrapArr[0] . $tag . $wrapArr[1];
2197 }
2198 $tag .= LF;
2199 if ($properties['forceOnTop']) {
2200 if ($properties['section'] === self::PART_HEADER) {
2201 $jsFiles = $tag . $jsFiles;
2202 } else {
2203 $jsFooterFiles = $tag . $jsFooterFiles;
2204 }
2205 } else {
2206 if ($properties['section'] === self::PART_HEADER) {
2207 $jsFiles .= $tag;
2208 } else {
2209 $jsFooterFiles .= $tag;
2210 }
2211 }
2212 }
2213 }
2214 if ($this->moveJsFromHeaderToFooter) {
2215 $jsFooterFiles = $jsFiles . $jsFooterFiles;
2216 $jsFiles = '';
2217 }
2218 return [$jsFiles, $jsFooterFiles];
2219 }
2220
2221 /**
2222 * Render inline JavaScript
2223 *
2224 * @return array<string> jsInline and jsFooterInline string
2225 */
2226 protected function renderInlineJavaScript()
2227 {
2228 $jsInline = '';
2229 $jsFooterInline = '';
2230 if (!empty($this->jsInline)) {
2231 foreach ($this->jsInline as $name => $properties) {
2232 $jsCode = '/*' . htmlspecialchars($name) . '*/' . LF . $properties['code'] . LF;
2233 if ($properties['forceOnTop']) {
2234 if ($properties['section'] === self::PART_HEADER) {
2235 $jsInline = $jsCode . $jsInline;
2236 } else {
2237 $jsFooterInline = $jsCode . $jsFooterInline;
2238 }
2239 } else {
2240 if ($properties['section'] === self::PART_HEADER) {
2241 $jsInline .= $jsCode;
2242 } else {
2243 $jsFooterInline .= $jsCode;
2244 }
2245 }
2246 }
2247 }
2248 if ($jsInline) {
2249 $jsInline = $this->inlineJavascriptWrap[0] . $jsInline . $this->inlineJavascriptWrap[1];
2250 }
2251 if ($jsFooterInline) {
2252 $jsFooterInline = $this->inlineJavascriptWrap[0] . $jsFooterInline . $this->inlineJavascriptWrap[1];
2253 }
2254 if ($this->moveJsFromHeaderToFooter) {
2255 $jsFooterInline = $jsInline . $jsFooterInline;
2256 $jsInline = '';
2257 }
2258 return [$jsInline, $jsFooterInline];
2259 }
2260
2261 /**
2262 * Include language file for inline usage
2263 *
2264 * @param string $fileRef
2265 * @param string $selectionPrefix
2266 * @param string $stripFromSelectionName
2267 * @throws \RuntimeException
2268 */
2269 protected function includeLanguageFileForInline($fileRef, $selectionPrefix = '', $stripFromSelectionName = '')
2270 {
2271 if (!isset($this->lang) || !isset($this->charSet)) {
2272 throw new \RuntimeException('Language and character encoding are not set.', 1284906026);
2273 }
2274 $labelsFromFile = [];
2275 $allLabels = $this->readLLfile($fileRef);
2276 if ($allLabels !== false) {
2277 // Merge language specific translations:
2278 if ($this->lang !== 'default' && isset($allLabels[$this->lang])) {
2279 $labels = array_merge($allLabels['default'], $allLabels[$this->lang]);
2280 } else {
2281 $labels = $allLabels['default'];
2282 }
2283 // Iterate through all locallang labels:
2284 foreach ($labels as $label => $value) {
2285 // If $selectionPrefix is set, only respect labels that start with $selectionPrefix
2286 if ($selectionPrefix === '' || strpos($label, $selectionPrefix) === 0) {
2287 // Remove substring $stripFromSelectionName from label
2288 $label = str_replace($stripFromSelectionName, '', $label);
2289 $labelsFromFile[$label] = $value;
2290 }
2291 }
2292 $this->inlineLanguageLabels = array_merge($this->inlineLanguageLabels, $labelsFromFile);
2293 }
2294 }
2295
2296 /**
2297 * Reads a locallang file.
2298 *
2299 * @param string $fileRef Reference to a relative filename to include.
2300 * @return array Returns the $LOCAL_LANG array found in the file. If no array found, returns empty array.
2301 */
2302 protected function readLLfile($fileRef)
2303 {
2304 /** @var LocalizationFactory $languageFactory */
2305 $languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
2306
2307 if ($this->lang !== 'default') {
2308 $languages = array_reverse($this->languageDependencies);
2309 // At least we need to have English
2310 if (empty($languages)) {
2311 $languages[] = 'default';
2312 }
2313 } else {
2314 $languages = ['default'];
2315 }
2316
2317 $localLanguage = [];
2318 foreach ($languages as $language) {
2319 $tempLL = $languageFactory->getParsedData($fileRef, $language);
2320
2321 $localLanguage['default'] = $tempLL['default'];
2322 if (!isset($localLanguage[$this->lang])) {
2323 $localLanguage[$this->lang] = $localLanguage['default'];
2324 }
2325 if ($this->lang !== 'default' && isset($tempLL[$language])) {
2326 // Merge current language labels onto labels from previous language
2327 // This way we have a labels with fall back applied
2328 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($localLanguage[$this->lang], $tempLL[$language], true, false);
2329 }
2330 }
2331
2332 return $localLanguage;
2333 }
2334
2335 /*****************************************************/
2336 /* */
2337 /* Tools */
2338 /* */
2339 /*****************************************************/
2340 /**
2341 * Concatenate files into one file
2342 * registered handler
2343 */
2344 protected function doConcatenate()
2345 {
2346 $this->doConcatenateCss();
2347 $this->doConcatenateJavaScript();
2348 }
2349
2350 /**
2351 * Concatenate JavaScript files according to the configuration.
2352 */
2353 protected function doConcatenateJavaScript()
2354 {
2355 if ($this->concatenateJavascript) {
2356 if (!empty($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['jsConcatenateHandler'])) {
2357 // use external concatenation routine
2358 $params = [
2359 'jsLibs' => &$this->jsLibs,
2360 'jsFiles' => &$this->jsFiles,
2361 'jsFooterFiles' => &$this->jsFooterFiles,
2362 'headerData' => &$this->headerData,
2363 'footerData' => &$this->footerData
2364 ];
2365 GeneralUtility::callUserFunction($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['jsConcatenateHandler'], $params, $this);
2366 } else {
2367 $this->jsLibs = $this->getCompressor()->concatenateJsFiles($this->jsLibs);
2368 $this->jsFiles = $this->getCompressor()->concatenateJsFiles($this->jsFiles);
2369 $this->jsFooterFiles = $this->getCompressor()->concatenateJsFiles($this->jsFooterFiles);
2370 }
2371 }
2372 }
2373
2374 /**
2375 * Concatenate CSS files according to configuration.
2376 */
2377 protected function doConcatenateCss()
2378 {
2379 if ($this->concatenateCss) {
2380 if (!empty($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['cssConcatenateHandler'])) {
2381 // use external concatenation routine
2382 $params = [
2383 'cssFiles' => &$this->cssFiles,
2384 'cssLibs' => &$this->cssLibs,
2385 'headerData' => &$this->headerData,
2386 'footerData' => &$this->footerData
2387 ];
2388 GeneralUtility::callUserFunction($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['cssConcatenateHandler'], $params, $this);
2389 } else {
2390 $cssOptions = [];
2391 if (TYPO3_MODE === 'BE') {
2392 $cssOptions = ['baseDirectories' => GeneralUtility::makeInstance(DocumentTemplate::class)->getSkinStylesheetDirectories()];
2393 }
2394 $this->cssLibs = $this->getCompressor()->concatenateCssFiles($this->cssLibs, $cssOptions);
2395 $this->cssFiles = $this->getCompressor()->concatenateCssFiles($this->cssFiles, $cssOptions);
2396 }
2397 }
2398 }
2399
2400 /**
2401 * Compresses inline code
2402 */
2403 protected function doCompress()
2404 {
2405 $this->doCompressJavaScript();
2406 $this->doCompressCss();
2407 }
2408
2409 /**
2410 * Compresses CSS according to configuration.
2411 */
2412 protected function doCompressCss()
2413 {
2414 if ($this->compressCss) {
2415 if (!empty($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['cssCompressHandler'])) {
2416 // Use external compression routine
2417 $params = [
2418 'cssInline' => &$this->cssInline,
2419 'cssFiles' => &$this->cssFiles,
2420 'cssLibs' => &$this->cssLibs,
2421 'headerData' => &$this->headerData,
2422 'footerData' => &$this->footerData
2423 ];
2424 GeneralUtility::callUserFunction($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['cssCompressHandler'], $params, $this);
2425 } else {
2426 $this->cssLibs = $this->getCompressor()->compressCssFiles($this->cssLibs);
2427 $this->cssFiles = $this->getCompressor()->compressCssFiles($this->cssFiles);
2428 }
2429 }
2430 }
2431
2432 /**
2433 * Compresses JavaScript according to configuration.
2434 */
2435 protected function doCompressJavaScript()
2436 {
2437 if ($this->compressJavascript) {
2438 if (!empty($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['jsCompressHandler'])) {
2439 // Use external compression routine
2440 $params = [
2441 'jsInline' => &$this->jsInline,
2442 'jsFooterInline' => &$this->jsFooterInline,
2443 'jsLibs' => &$this->jsLibs,
2444 'jsFiles' => &$this->jsFiles,
2445 'jsFooterFiles' => &$this->jsFooterFiles,
2446 'headerData' => &$this->headerData,
2447 'footerData' => &$this->footerData
2448 ];
2449 GeneralUtility::callUserFunction($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['jsCompressHandler'], $params, $this);
2450 } else {
2451 // Traverse the arrays, compress files
2452 if (!empty($this->jsInline)) {
2453 foreach ($this->jsInline as $name => $properties) {
2454 if ($properties['compress']) {
2455 $error = '';
2456 $this->jsInline[$name]['code'] = GeneralUtility::minifyJavaScript($properties['code'], $error);
2457 if ($error) {
2458 $this->compressError .= 'Error with minify JS Inline Block "' . $name . '": ' . $error . LF;
2459 }
2460 }
2461 }
2462 }
2463 $this->jsLibs = $this->getCompressor()->compressJsFiles($this->jsLibs);
2464 $this->jsFiles = $this->getCompressor()->compressJsFiles($this->jsFiles);
2465 $this->jsFooterFiles = $this->getCompressor()->compressJsFiles($this->jsFooterFiles);
2466 }
2467 }
2468 }
2469
2470 /**
2471 * Returns instance of \TYPO3\CMS\Core\Resource\ResourceCompressor
2472 *
2473 * @return \TYPO3\CMS\Core\Resource\ResourceCompressor
2474 */
2475 protected function getCompressor()
2476 {
2477 if ($this->compressor === null) {
2478 $this->compressor = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceCompressor::class);
2479 }
2480 return $this->compressor;
2481 }
2482
2483 /**
2484 * Processes a Javascript file dependent on the current context
2485 *
2486 * Adds the version number for Frontend, compresses the file for Backend
2487 *
2488 * @param string $filename Filename
2489 * @return string New filename
2490 */
2491 protected function processJsFile($filename)
2492 {
2493 $filename = $this->getStreamlinedFileName($filename, false);
2494 if ($this->compressJavascript) {
2495 $filename = $this->getCompressor()->compressJsFile($filename);
2496 } elseif (TYPO3_MODE === 'FE') {
2497 $filename = GeneralUtility::createVersionNumberedFilename($filename);
2498 }
2499 return $this->getAbsoluteWebPath($filename);
2500 }
2501
2502 /**
2503 * This function acts as a wrapper to allow relative and paths starting with EXT: to be dealt with
2504 * in this very case to always return the absolute web path to be included directly before output.
2505 *
2506 * This is mainly added so the EXT: syntax can be resolved for PageRenderer in one central place,
2507 * and hopefully removed in the future by one standard API call.
2508 *
2509 * @param string $file the filename to process
2510 * @param bool $prepareForOutput whether the file should be prepared as version numbered file and prefixed as absolute webpath
2511 * @return string
2512 * @internal
2513 */
2514 protected function getStreamlinedFileName($file, $prepareForOutput = true)
2515 {
2516 if (strpos($file, 'EXT:') === 0) {
2517 $file = GeneralUtility::getFileAbsFileName($file);
2518 // as the path is now absolute, make it "relative" to the current script to stay compatible
2519 $file = PathUtility::getRelativePathTo($file);
2520 $file = rtrim($file, '/');
2521 } else {
2522 $file = GeneralUtility::resolveBackPath($file);
2523 }
2524 if ($prepareForOutput) {
2525 $file = GeneralUtility::createVersionNumberedFilename($file);
2526 $file = $this->getAbsoluteWebPath($file);
2527 }
2528 return $file;
2529 }
2530
2531 /**
2532 * Gets absolute web path of filename for backend disposal.
2533 * Resolving the absolute path in the frontend with conflict with
2534 * applying config.absRefPrefix in frontend rendering process.
2535 *
2536 * @param string $file
2537 * @return string
2538 * @see TypoScriptFrontendController::setAbsRefPrefix()
2539 */
2540 protected function getAbsoluteWebPath(string $file): string
2541 {
2542 if (TYPO3_MODE === 'FE') {
2543 return $file;
2544 }
2545 return PathUtility::getAbsoluteWebPath($file);
2546 }
2547
2548 /**
2549 * Returns global frontend controller
2550 *
2551 * @return TypoScriptFrontendController
2552 */
2553 protected function getTypoScriptFrontendController()
2554 {
2555 return $GLOBALS['TSFE'];
2556 }
2557
2558 /*****************************************************/
2559 /* */
2560 /* Hooks */
2561 /* */
2562 /*****************************************************/
2563 /**
2564 * Execute PreRenderHook for possible manipulation
2565 */
2566 protected function executePreRenderHook()
2567 {
2568 $hooks = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_pagerenderer.php']['render-preProcess'] ?? false;
2569 if (!$hooks) {
2570 return;
2571 }
2572 $params = [
2573 'jsLibs' => &$this->jsLibs,
2574 'jsFooterLibs' => &$this->jsFooterLibs,
2575 'jsFiles' => &$this->jsFiles,
2576 'jsFooterFiles' => &$this->jsFooterFiles,
2577 'cssLibs' => &$this->cssLibs,
2578 'cssFiles' => &$this->cssFiles,
2579 'headerData' => &$this->headerData,
2580 'footerData' => &$this->footerData,
2581 'jsInline' => &$this->jsInline,
2582 'jsFooterInline' => &$this->jsFooterInline,
2583 'cssInline' => &$this->cssInline
2584 ];
2585 foreach ($hooks as $hook) {
2586 GeneralUtility::callUserFunction($hook, $params, $this);
2587 }
2588 }
2589
2590 /**
2591 * PostTransform for possible manipulation of concatenated and compressed files
2592 */
2593 protected function executeRenderPostTransformHook()
2594 {
2595 $hooks = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_pagerenderer.php']['render-postTransform'] ?? false;
2596 if (!$hooks) {
2597 return;
2598 }
2599 $params = [
2600 'jsLibs' => &$this->jsLibs,
2601 'jsFooterLibs' => &$this->jsFooterLibs,
2602 'jsFiles' => &$this->jsFiles,
2603 'jsFooterFiles' => &$this->jsFooterFiles,
2604 'cssLibs' => &$this->cssLibs,
2605 'cssFiles' => &$this->cssFiles,
2606 'headerData' => &$this->headerData,
2607 'footerData' => &$this->footerData,
2608 'jsInline' => &$this->jsInline,
2609 'jsFooterInline' => &$this->jsFooterInline,
2610 'cssInline' => &$this->cssInline
2611 ];
2612 foreach ($hooks as $hook) {
2613 GeneralUtility::callUserFunction($hook, $params, $this);
2614 }
2615 }
2616
2617 /**
2618 * Execute postRenderHook for possible manipulation
2619 *
2620 * @param string $jsLibs
2621 * @param string $jsFiles
2622 * @param string $jsFooterFiles
2623 * @param string $cssLibs
2624 * @param string $cssFiles
2625 * @param string $jsInline
2626 * @param string $cssInline
2627 * @param string $jsFooterInline
2628 * @param string $jsFooterLibs
2629 */
2630 protected function executePostRenderHook(&$jsLibs, &$jsFiles, &$jsFooterFiles, &$cssLibs, &$cssFiles, &$jsInline, &$cssInline, &$jsFooterInline, &$jsFooterLibs)
2631 {
2632 $hooks = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_pagerenderer.php']['render-postProcess'] ?? false;
2633 if (!$hooks) {
2634 return;
2635 }
2636 $params = [
2637 'jsLibs' => &$jsLibs,
2638 'jsFiles' => &$jsFiles,
2639 'jsFooterFiles' => &$jsFooterFiles,
2640 'cssLibs' => &$cssLibs,
2641 'cssFiles' => &$cssFiles,
2642 'headerData' => &$this->headerData,
2643 'footerData' => &$this->footerData,
2644 'jsInline' => &$jsInline,
2645 'cssInline' => &$cssInline,
2646 'xmlPrologAndDocType' => &$this->xmlPrologAndDocType,
2647 'htmlTag' => &$this->htmlTag,
2648 'headTag' => &$this->headTag,
2649 'charSet' => &$this->charSet,
2650 'metaCharsetTag' => &$this->metaCharsetTag,
2651 'shortcutTag' => &$this->shortcutTag,
2652 'inlineComments' => &$this->inlineComments,
2653 'baseUrl' => &$this->baseUrl,
2654 'baseUrlTag' => &$this->baseUrlTag,
2655 'favIcon' => &$this->favIcon,
2656 'iconMimeType' => &$this->iconMimeType,
2657 'titleTag' => &$this->titleTag,
2658 'title' => &$this->title,
2659 'metaTags' => &$this->metaTags,
2660 'jsFooterInline' => &$jsFooterInline,
2661 'jsFooterLibs' => &$jsFooterLibs,
2662 'bodyContent' => &$this->bodyContent
2663 ];
2664 foreach ($hooks as $hook) {
2665 GeneralUtility::callUserFunction($hook, $params, $this);
2666 }
2667 }
2668
2669 /**
2670 * Creates an CSS inline tag
2671 *
2672 * @param string $file the filename to process
2673 * @param array $properties
2674 * @return string
2675 */
2676 protected function createInlineCssTagFromFile(string $file, array $properties): string
2677 {
2678 $cssInline = file_get_contents($file);
2679
2680 return '<style type="text/css"'
2681 . ' media="' . htmlspecialchars($properties['media']) . '"'
2682 . ($properties['title'] ? ' title="' . htmlspecialchars($properties['title']) . '"' : '')
2683 . '>' . LF
2684 . '/*<![CDATA[*/' . LF . '<!-- ' . LF
2685 . $cssInline
2686 . '-->' . LF . '/*]]>*/' . LF . '</style>' . LF;
2687 }
2688 }