[BUGFIX] Remove caches for page title and meta tag
[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\Frontend\FrontendInterface;
22 use TYPO3\CMS\Core\Core\Environment;
23 use TYPO3\CMS\Core\Localization\LocalizationFactory;
24 use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;
25 use TYPO3\CMS\Core\Service\MarkerBasedTemplateService;
26 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28 use TYPO3\CMS\Core\Utility\PathUtility;
29 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
30
31 /**
32 * TYPO3 pageRender class
33 * This class render the HTML of a webpage, usable for BE and FE
34 */
35 class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface
36 {
37 // Constants for the part to be rendered
38 const PART_COMPLETE = 0;
39 const PART_HEADER = 1;
40 const PART_FOOTER = 2;
41
42 const REQUIREJS_SCOPE_CONFIG = 'config';
43 const REQUIREJS_SCOPE_RESOLVE = 'resolve';
44
45 /**
46 * @var bool
47 */
48 protected $compressJavascript = false;
49
50 /**
51 * @var bool
52 */
53 protected $compressCss = false;
54
55 /**
56 * @var bool
57 */
58 protected $removeLineBreaksFromTemplate = false;
59
60 /**
61 * @var bool
62 */
63 protected $concatenateJavascript = false;
64
65 /**
66 * @var bool
67 */
68 protected $concatenateCss = false;
69
70 /**
71 * @var bool
72 */
73 protected $moveJsFromHeaderToFooter = false;
74
75 /**
76 * @var \TYPO3\CMS\Core\Localization\Locales
77 */
78 protected $locales;
79
80 /**
81 * The language key
82 * Two character string or 'default'
83 *
84 * @var string
85 */
86 protected $lang;
87
88 /**
89 * List of language dependencies for actual language. This is used for local variants of a language
90 * that depend on their "main" language, like Brazilian Portuguese or Canadian French.
91 *
92 * @var array
93 */
94 protected $languageDependencies = [];
95
96 /**
97 * @var \TYPO3\CMS\Core\Resource\ResourceCompressor
98 */
99 protected $compressor;
100
101 // Arrays containing associative array for the included files
102 /**
103 * @var array
104 */
105 protected $jsFiles = [];
106
107 /**
108 * @var array
109 */
110 protected $jsFooterFiles = [];
111
112 /**
113 * @var array
114 */
115 protected $jsLibs = [];
116
117 /**
118 * @var array
119 */
120 protected $jsFooterLibs = [];
121
122 /**
123 * @var array
124 */
125 protected $cssFiles = [];
126
127 /**
128 * @var array
129 */
130 protected $cssLibs = [];
131
132 /**
133 * The title of the page
134 *
135 * @var string
136 */
137 protected $title;
138
139 /**
140 * Charset for the rendering
141 *
142 * @var string
143 */
144 protected $charSet;
145
146 /**
147 * @var string
148 */
149 protected $favIcon;
150
151 /**
152 * @var string
153 */
154 protected $baseUrl;
155
156 /**
157 * @var bool
158 */
159 protected $renderXhtml = true;
160
161 // Static header blocks
162 /**
163 * @var string
164 */
165 protected $xmlPrologAndDocType = '';
166
167 /**
168 * @var array
169 */
170 protected $metaTags = [];
171
172 /**
173 * @var array
174 */
175 protected $inlineComments = [];
176
177 /**
178 * @var array
179 */
180 protected $headerData = [];
181
182 /**
183 * @var array
184 */
185 protected $footerData = [];
186
187 /**
188 * @var string
189 */
190 protected $titleTag = '<title>|</title>';
191
192 /**
193 * @var string
194 */
195 protected $metaCharsetTag = '<meta http-equiv="Content-Type" content="text/html; charset=|" />';
196
197 /**
198 * @var string
199 */
200 protected $htmlTag = '<html>';
201
202 /**
203 * @var string
204 */
205 protected $headTag = '<head>';
206
207 /**
208 * @var string
209 */
210 protected $baseUrlTag = '<base href="|" />';
211
212 /**
213 * @var string
214 */
215 protected $iconMimeType = '';
216
217 /**
218 * @var string
219 */
220 protected $shortcutTag = '<link rel="shortcut icon" href="%1$s"%2$s />';
221
222 // Static inline code blocks
223 /**
224 * @var array
225 */
226 protected $jsInline = [];
227
228 /**
229 * @var array
230 */
231 protected $jsFooterInline = [];
232
233 /**
234 * @var array
235 */
236 protected $cssInline = [];
237
238 /**
239 * @var string
240 */
241 protected $bodyContent;
242
243 /**
244 * @var string
245 */
246 protected $templateFile;
247
248 // Paths to contributed libraries
249
250 /**
251 * default path to the requireJS library, relative to the typo3/ directory
252 * @var string
253 */
254 protected $requireJsPath = 'EXT:core/Resources/Public/JavaScript/Contrib/';
255
256 // Internal flags for JS-libraries
257 /**
258 * if set, the requireJS library is included
259 * @var bool
260 */
261 protected $addRequireJs = false;
262
263 /**
264 * Inline configuration for requireJS (internal)
265 * @var array
266 */
267 protected $requireJsConfig = [];
268
269 /**
270 * Module names of internal requireJS 'paths'
271 * @var array
272 */
273 protected $internalRequireJsPathModuleNames = [];
274
275 /**
276 * Inline configuration for requireJS (public)
277 * @var array
278 */
279 protected $publicRequireJsConfig = [];
280
281 /**
282 * @var array
283 */
284 protected $inlineLanguageLabels = [];
285
286 /**
287 * @var array
288 */
289 protected $inlineLanguageLabelFiles = [];
290
291 /**
292 * @var array
293 */
294 protected $inlineSettings = [];
295
296 /**
297 * @var array
298 */
299 protected $inlineJavascriptWrap = [];
300
301 /**
302 * @var array
303 */
304 protected $inlineCssWrap = [];
305
306 /**
307 * Saves error messages generated during compression
308 *
309 * @var string
310 */
311 protected $compressError = '';
312
313 /**
314 * Is empty string for HTML and ' /' for XHTML rendering
315 *
316 * @var string
317 */
318 protected $endingSlash = '';
319
320 /**
321 * @var MetaTagManagerRegistry
322 */
323 protected $metaTagRegistry;
324
325 /**
326 * @var FrontendInterface
327 */
328 protected static $cache = null;
329
330 /**
331 * @param string $templateFile Declare the used template file. Omit this parameter will use default template
332 */
333 public function __construct($templateFile = '')
334 {
335 $this->reset();
336 $this->locales = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\Locales::class);
337 if ($templateFile !== '') {
338 $this->templateFile = $templateFile;
339 }
340 $this->inlineJavascriptWrap = [
341 '<script type="text/javascript">' . LF . '/*<![CDATA[*/' . LF,
342 '/*]]>*/' . LF . '</script>' . LF
343 ];
344 $this->inlineCssWrap = [
345 '<style type="text/css">' . LF . '/*<![CDATA[*/' . LF . '<!-- ' . LF,
346 '-->' . LF . '/*]]>*/' . LF . '</style>' . LF
347 ];
348
349 $this->metaTagRegistry = GeneralUtility::makeInstance(MetaTagManagerRegistry::class);
350 $this->setMetaTag('name', 'generator', 'TYPO3 CMS');
351 }
352
353 /**
354 * Set restored meta tag managers as singletons
355 * so that uncached plugins can use them to add or remove meta tags
356 */
357 public function __wakeup()
358 {
359 GeneralUtility::setSingletonInstance(get_class($this->metaTagRegistry), $this->metaTagRegistry);
360 }
361
362 /**
363 * @param FrontendInterface $cache
364 */
365 public static function setCache(FrontendInterface $cache)
366 {
367 static::$cache = $cache;
368 }
369
370 /**
371 * Reset all vars to initial values
372 */
373 protected function reset()
374 {
375 $this->templateFile = 'EXT:core/Resources/Private/Templates/PageRenderer.html';
376 $this->jsFiles = [];
377 $this->jsFooterFiles = [];
378 $this->jsInline = [];
379 $this->jsFooterInline = [];
380 $this->jsLibs = [];
381 $this->cssFiles = [];
382 $this->cssInline = [];
383 $this->metaTags = [];
384 $this->metaTagsByAPI = [];
385 $this->inlineComments = [];
386 $this->headerData = [];
387 $this->footerData = [];
388 }
389
390 /*****************************************************/
391 /* */
392 /* Public Setters */
393 /* */
394 /* */
395 /*****************************************************/
396 /**
397 * Sets the title
398 *
399 * @param string $title title of webpage
400 */
401 public function setTitle($title)
402 {
403 $this->title = $title;
404 }
405
406 /**
407 * Enables/disables rendering of XHTML code
408 *
409 * @param bool $enable Enable XHTML
410 */
411 public function setRenderXhtml($enable)
412 {
413 $this->renderXhtml = $enable;
414 }
415
416 /**
417 * Sets xml prolog and docType
418 *
419 * @param string $xmlPrologAndDocType Complete tags for xml prolog and docType
420 */
421 public function setXmlPrologAndDocType($xmlPrologAndDocType)
422 {
423 $this->xmlPrologAndDocType = $xmlPrologAndDocType;
424 }
425
426 /**
427 * Sets meta charset
428 *
429 * @param string $charSet Used charset
430 */
431 public function setCharSet($charSet)
432 {
433 $this->charSet = $charSet;
434 }
435
436 /**
437 * Sets language
438 *
439 * @param string $lang Used language
440 */
441 public function setLanguage($lang)
442 {
443 $this->lang = $lang;
444 $this->languageDependencies = [];
445
446 // Language is found. Configure it:
447 if (in_array($this->lang, $this->locales->getLocales())) {
448 $this->languageDependencies[] = $this->lang;
449 foreach ($this->locales->getLocaleDependencies($this->lang) as $language) {
450 $this->languageDependencies[] = $language;
451 }
452 }
453 }
454
455 /**
456 * Set the meta charset tag
457 *
458 * @param string $metaCharsetTag
459 */
460 public function setMetaCharsetTag($metaCharsetTag)
461 {
462 $this->metaCharsetTag = $metaCharsetTag;
463 }
464
465 /**
466 * Sets html tag
467 *
468 * @param string $htmlTag Html tag
469 */
470 public function setHtmlTag($htmlTag)
471 {
472 $this->htmlTag = $htmlTag;
473 }
474
475 /**
476 * Sets HTML head tag
477 *
478 * @param string $headTag HTML head tag
479 */
480 public function setHeadTag($headTag)
481 {
482 $this->headTag = $headTag;
483 }
484
485 /**
486 * Sets favicon
487 *
488 * @param string $favIcon
489 */
490 public function setFavIcon($favIcon)
491 {
492 $this->favIcon = $favIcon;
493 }
494
495 /**
496 * Sets icon mime type
497 *
498 * @param string $iconMimeType
499 */
500 public function setIconMimeType($iconMimeType)
501 {
502 $this->iconMimeType = $iconMimeType;
503 }
504
505 /**
506 * Sets HTML base URL
507 *
508 * @param string $baseUrl HTML base URL
509 */
510 public function setBaseUrl($baseUrl)
511 {
512 $this->baseUrl = $baseUrl;
513 }
514
515 /**
516 * Sets template file
517 *
518 * @param string $file
519 */
520 public function setTemplateFile($file)
521 {
522 $this->templateFile = $file;
523 }
524
525 /**
526 * Sets Content for Body
527 *
528 * @param string $content
529 */
530 public function setBodyContent($content)
531 {
532 $this->bodyContent = $content;
533 }
534
535 /**
536 * Sets path to requireJS library (relative to typo3 directory)
537 *
538 * @param string $path Path to requireJS library
539 */
540 public function setRequireJsPath($path)
541 {
542 $this->requireJsPath = $path;
543 }
544
545 /**
546 * @param string $scope
547 * @return array
548 */
549 public function getRequireJsConfig(string $scope = null): array
550 {
551 // return basic RequireJS configuration without shim, paths and packages
552 if ($scope === static::REQUIREJS_SCOPE_CONFIG) {
553 return array_replace_recursive(
554 $this->publicRequireJsConfig,
555 $this->filterArrayKeys(
556 $this->requireJsConfig,
557 ['shim', 'paths', 'packages'],
558 false
559 )
560 );
561 }
562 // return RequireJS configuration for resolving only shim, paths and packages
563 if ($scope === static::REQUIREJS_SCOPE_RESOLVE) {
564 return $this->filterArrayKeys(
565 $this->requireJsConfig,
566 ['shim', 'paths', 'packages'],
567 true
568 );
569 }
570 return [];
571 }
572
573 /*****************************************************/
574 /* */
575 /* Public Enablers / Disablers */
576 /* */
577 /* */
578 /*****************************************************/
579 /**
580 * Enables MoveJsFromHeaderToFooter
581 */
582 public function enableMoveJsFromHeaderToFooter()
583 {
584 $this->moveJsFromHeaderToFooter = true;
585 }
586
587 /**
588 * Disables MoveJsFromHeaderToFooter
589 */
590 public function disableMoveJsFromHeaderToFooter()
591 {
592 $this->moveJsFromHeaderToFooter = false;
593 }
594
595 /**
596 * Enables compression of javascript
597 */
598 public function enableCompressJavascript()
599 {
600 $this->compressJavascript = true;
601 }
602
603 /**
604 * Disables compression of javascript
605 */
606 public function disableCompressJavascript()
607 {
608 $this->compressJavascript = false;
609 }
610
611 /**
612 * Enables compression of css
613 */
614 public function enableCompressCss()
615 {
616 $this->compressCss = true;
617 }
618
619 /**
620 * Disables compression of css
621 */
622 public function disableCompressCss()
623 {
624 $this->compressCss = false;
625 }
626
627 /**
628 * Enables concatenation of js files
629 */
630 public function enableConcatenateJavascript()
631 {
632 $this->concatenateJavascript = true;
633 }
634
635 /**
636 * Disables concatenation of js files
637 */
638 public function disableConcatenateJavascript()
639 {
640 $this->concatenateJavascript = false;
641 }
642
643 /**
644 * Enables concatenation of css files
645 */
646 public function enableConcatenateCss()
647 {
648 $this->concatenateCss = true;
649 }
650
651 /**
652 * Disables concatenation of css files
653 */
654 public function disableConcatenateCss()
655 {
656 $this->concatenateCss = false;
657 }
658
659 /**
660 * Sets removal of all line breaks in template
661 */
662 public function enableRemoveLineBreaksFromTemplate()
663 {
664 $this->removeLineBreaksFromTemplate = true;
665 }
666
667 /**
668 * Unsets removal of all line breaks in template
669 */
670 public function disableRemoveLineBreaksFromTemplate()
671 {
672 $this->removeLineBreaksFromTemplate = false;
673 }
674
675 /**
676 * Enables Debug Mode
677 * This is a shortcut to switch off all compress/concatenate features to enable easier debug
678 */
679 public function enableDebugMode()
680 {
681 $this->compressJavascript = false;
682 $this->compressCss = false;
683 $this->concatenateCss = false;
684 $this->concatenateJavascript = false;
685 $this->removeLineBreaksFromTemplate = false;
686 }
687
688 /*****************************************************/
689 /* */
690 /* Public Getters */
691 /* */
692 /* */
693 /*****************************************************/
694 /**
695 * Gets the title
696 *
697 * @return string $title Title of webpage
698 */
699 public function getTitle()
700 {
701 return $this->title;
702 }
703
704 /**
705 * Gets the charSet
706 *
707 * @return string $charSet
708 */
709 public function getCharSet()
710 {
711 return $this->charSet;
712 }
713
714 /**
715 * Gets the language
716 *
717 * @return string $lang
718 */
719 public function getLanguage()
720 {
721 return $this->lang;
722 }
723
724 /**
725 * Returns rendering mode XHTML or HTML
726 *
727 * @return bool TRUE if XHTML, FALSE if HTML
728 */
729 public function getRenderXhtml()
730 {
731 return $this->renderXhtml;
732 }
733
734 /**
735 * Gets html tag
736 *
737 * @return string $htmlTag Html tag
738 */
739 public function getHtmlTag()
740 {
741 return $this->htmlTag;
742 }
743
744 /**
745 * Get meta charset
746 *
747 * @return string
748 */
749 public function getMetaCharsetTag()
750 {
751 return $this->metaCharsetTag;
752 }
753
754 /**
755 * Gets head tag
756 *
757 * @return string $tag Head tag
758 */
759 public function getHeadTag()
760 {
761 return $this->headTag;
762 }
763
764 /**
765 * Gets favicon
766 *
767 * @return string $favIcon
768 */
769 public function getFavIcon()
770 {
771 return $this->favIcon;
772 }
773
774 /**
775 * Gets icon mime type
776 *
777 * @return string $iconMimeType
778 */
779 public function getIconMimeType()
780 {
781 return $this->iconMimeType;
782 }
783
784 /**
785 * Gets HTML base URL
786 *
787 * @return string $url
788 */
789 public function getBaseUrl()
790 {
791 return $this->baseUrl;
792 }
793
794 /**
795 * Gets template file
796 *
797 * @return string
798 */
799 public function getTemplateFile()
800 {
801 return $this->templateFile;
802 }
803
804 /**
805 * Gets MoveJsFromHeaderToFooter
806 *
807 * @return bool
808 */
809 public function getMoveJsFromHeaderToFooter()
810 {
811 return $this->moveJsFromHeaderToFooter;
812 }
813
814 /**
815 * Gets compress of javascript
816 *
817 * @return bool
818 */
819 public function getCompressJavascript()
820 {
821 return $this->compressJavascript;
822 }
823
824 /**
825 * Gets compress of css
826 *
827 * @return bool
828 */
829 public function getCompressCss()
830 {
831 return $this->compressCss;
832 }
833
834 /**
835 * Gets concatenate of js files
836 *
837 * @return bool
838 */
839 public function getConcatenateJavascript()
840 {
841 return $this->concatenateJavascript;
842 }
843
844 /**
845 * Gets concatenate of css files
846 *
847 * @return bool
848 */
849 public function getConcatenateCss()
850 {
851 return $this->concatenateCss;
852 }
853
854 /**
855 * Gets remove of empty lines from template
856 *
857 * @return bool
858 */
859 public function getRemoveLineBreaksFromTemplate()
860 {
861 return $this->removeLineBreaksFromTemplate;
862 }
863
864 /**
865 * Gets content for body
866 *
867 * @return string
868 */
869 public function getBodyContent()
870 {
871 return $this->bodyContent;
872 }
873
874 /**
875 * Gets the inline language labels.
876 *
877 * @return array The inline language labels
878 */
879 public function getInlineLanguageLabels()
880 {
881 return $this->inlineLanguageLabels;
882 }
883
884 /**
885 * Gets the inline language files
886 *
887 * @return array
888 */
889 public function getInlineLanguageLabelFiles()
890 {
891 return $this->inlineLanguageLabelFiles;
892 }
893
894 /*****************************************************/
895 /* */
896 /* Public Functions to add Data */
897 /* */
898 /* */
899 /*****************************************************/
900
901 /**
902 * Sets a given meta tag
903 *
904 * @param string $type The type of the meta tag. Allowed values are property, name or http-equiv
905 * @param string $name The name of the property to add
906 * @param string $content The content of the meta tag
907 * @param array $subProperties Subproperties of the meta tag (like e.g. og:image:width)
908 * @param bool $replace Replace earlier set meta tag
909 * @throws \InvalidArgumentException
910 */
911 public function setMetaTag(string $type, string $name, string $content, array $subProperties = [], $replace = true)
912 {
913 // Lowercase all the things
914 $type = strtolower($type);
915 $name = strtolower($name);
916 if (!in_array($type, ['property', 'name', 'http-equiv'], true)) {
917 throw new \InvalidArgumentException(
918 'When setting a meta tag the only types allowed are property, name or http-equiv. "' . $type . '" given.',
919 1496402460
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
1678 foreach ($metaTagManagers as $manager => $managerObject) {
1679 $properties = $managerObject->renderAllProperties();
1680 if (!empty($properties)) {
1681 $metaTags[] = $properties;
1682 }
1683 }
1684 return $metaTags;
1685 }
1686
1687 /**
1688 * Render the page but not the JavaScript and CSS Files
1689 *
1690 * @param string $substituteHash The hash that is used for the placehoder markers
1691 * @internal
1692 * @return string Content of rendered section
1693 */
1694 public function renderPageWithUncachedObjects($substituteHash)
1695 {
1696 $this->prepareRendering();
1697 $markerArray = $this->getPreparedMarkerArrayForPageWithUncachedObjects($substituteHash);
1698 $template = $this->getTemplateForPart(self::PART_COMPLETE);
1699 $templateService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
1700 return trim($templateService->substituteMarkerArray($template, $markerArray, '###|###'));
1701 }
1702
1703 /**
1704 * Renders the JavaScript and CSS files that have been added during processing
1705 * of uncached content objects (USER_INT, COA_INT)
1706 *
1707 * @param string $cachedPageContent
1708 * @param string $substituteHash The hash that is used for the placehoder markers
1709 * @internal
1710 * @return string
1711 */
1712 public function renderJavaScriptAndCssForProcessingOfUncachedContentObjects($cachedPageContent, $substituteHash)
1713 {
1714 $this->prepareRendering();
1715 list($jsLibs, $jsFiles, $jsFooterFiles, $cssLibs, $cssFiles, $jsInline, $cssInline, $jsFooterInline, $jsFooterLibs) = $this->renderJavaScriptAndCss();
1716 $title = $this->title ? str_replace('|', htmlspecialchars($this->title), $this->titleTag) : '';
1717 $markerArray = [
1718 '<!-- ###TITLE' . $substituteHash . '### -->' => $title,
1719 '<!-- ###CSS_LIBS' . $substituteHash . '### -->' => $cssLibs,
1720 '<!-- ###CSS_INCLUDE' . $substituteHash . '### -->' => $cssFiles,
1721 '<!-- ###CSS_INLINE' . $substituteHash . '### -->' => $cssInline,
1722 '<!-- ###JS_INLINE' . $substituteHash . '### -->' => $jsInline,
1723 '<!-- ###JS_INCLUDE' . $substituteHash . '### -->' => $jsFiles,
1724 '<!-- ###JS_LIBS' . $substituteHash . '### -->' => $jsLibs,
1725 '<!-- ###META' . $substituteHash . '### -->' => implode(LF, array_merge($this->metaTags, $this->renderMetaTagsFromAPI())),
1726 '<!-- ###HEADERDATA' . $substituteHash . '### -->' => implode(LF, $this->headerData),
1727 '<!-- ###FOOTERDATA' . $substituteHash . '### -->' => implode(LF, $this->footerData),
1728 '<!-- ###JS_LIBS_FOOTER' . $substituteHash . '### -->' => $jsFooterLibs,
1729 '<!-- ###JS_INCLUDE_FOOTER' . $substituteHash . '### -->' => $jsFooterFiles,
1730 '<!-- ###JS_INLINE_FOOTER' . $substituteHash . '### -->' => $jsFooterInline
1731 ];
1732 foreach ($markerArray as $placeHolder => $content) {
1733 $cachedPageContent = str_replace($placeHolder, $content, $cachedPageContent);
1734 }
1735 $this->reset();
1736 return $cachedPageContent;
1737 }
1738
1739 /**
1740 * Remove ending slashes from static header block
1741 * if the page is being rendered as html (not xhtml)
1742 * and define property $this->endingSlash for further use
1743 */
1744 protected function prepareRendering()
1745 {
1746 if ($this->getRenderXhtml()) {
1747 $this->endingSlash = ' /';
1748 } else {
1749 $this->metaCharsetTag = str_replace(' />', '>', $this->metaCharsetTag);
1750 $this->baseUrlTag = str_replace(' />', '>', $this->baseUrlTag);
1751 $this->shortcutTag = str_replace(' />', '>', $this->shortcutTag);
1752 $this->endingSlash = '';
1753 }
1754 }
1755
1756 /**
1757 * Renders all JavaScript and CSS
1758 *
1759 * @return array<string>
1760 */
1761 protected function renderJavaScriptAndCss()
1762 {
1763 $this->executePreRenderHook();
1764 $mainJsLibs = $this->renderMainJavaScriptLibraries();
1765 if ($this->concatenateJavascript || $this->concatenateCss) {
1766 // Do the file concatenation
1767 $this->doConcatenate();
1768 }
1769 if ($this->compressCss || $this->compressJavascript) {
1770 // Do the file compression
1771 $this->doCompress();
1772 }
1773 $this->executeRenderPostTransformHook();
1774 $cssLibs = $this->renderCssLibraries();
1775 $cssFiles = $this->renderCssFiles();
1776 $cssInline = $this->renderCssInline();
1777 list($jsLibs, $jsFooterLibs) = $this->renderAdditionalJavaScriptLibraries();
1778 list($jsFiles, $jsFooterFiles) = $this->renderJavaScriptFiles();
1779 list($jsInline, $jsFooterInline) = $this->renderInlineJavaScript();
1780 $jsLibs = $mainJsLibs . $jsLibs;
1781 if ($this->moveJsFromHeaderToFooter) {
1782 $jsFooterLibs = $jsLibs . LF . $jsFooterLibs;
1783 $jsLibs = '';
1784 $jsFooterFiles = $jsFiles . LF . $jsFooterFiles;
1785 $jsFiles = '';
1786 $jsFooterInline = $jsInline . LF . $jsFooterInline;
1787 $jsInline = '';
1788 }
1789 $this->executePostRenderHook($jsLibs, $jsFiles, $jsFooterFiles, $cssLibs, $cssFiles, $jsInline, $cssInline, $jsFooterInline, $jsFooterLibs);
1790 return [$jsLibs, $jsFiles, $jsFooterFiles, $cssLibs, $cssFiles, $jsInline, $cssInline, $jsFooterInline, $jsFooterLibs];
1791 }
1792
1793 /**
1794 * Fills the marker array with the given strings and trims each value
1795 *
1796 * @param string $jsLibs
1797 * @param string $jsFiles
1798 * @param string $jsFooterFiles
1799 * @param string $cssLibs
1800 * @param string $cssFiles
1801 * @param string $jsInline
1802 * @param string $cssInline
1803 * @param string $jsFooterInline
1804 * @param string $jsFooterLibs
1805 * @param string $metaTags
1806 * @return array Marker array
1807 */
1808 protected function getPreparedMarkerArray($jsLibs, $jsFiles, $jsFooterFiles, $cssLibs, $cssFiles, $jsInline, $cssInline, $jsFooterInline, $jsFooterLibs, $metaTags)
1809 {
1810 $markerArray = [
1811 'XMLPROLOG_DOCTYPE' => $this->xmlPrologAndDocType,
1812 'HTMLTAG' => $this->htmlTag,
1813 'HEADTAG' => $this->headTag,
1814 'METACHARSET' => $this->charSet ? str_replace('|', htmlspecialchars($this->charSet), $this->metaCharsetTag) : '',
1815 'INLINECOMMENT' => $this->inlineComments ? LF . LF . '<!-- ' . LF . implode(LF, $this->inlineComments) . '-->' . LF . LF : '',
1816 'BASEURL' => $this->baseUrl ? str_replace('|', $this->baseUrl, $this->baseUrlTag) : '',
1817 'SHORTCUT' => $this->favIcon ? sprintf($this->shortcutTag, htmlspecialchars($this->favIcon), $this->iconMimeType) : '',
1818 'CSS_LIBS' => $cssLibs,
1819 'CSS_INCLUDE' => $cssFiles,
1820 'CSS_INLINE' => $cssInline,
1821 'JS_INLINE' => $jsInline,
1822 'JS_INCLUDE' => $jsFiles,
1823 'JS_LIBS' => $jsLibs,
1824 'TITLE' => $this->title ? str_replace('|', htmlspecialchars($this->title), $this->titleTag) : '',
1825 'META' => $metaTags,
1826 'HEADERDATA' => $this->headerData ? implode(LF, $this->headerData) : '',
1827 'FOOTERDATA' => $this->footerData ? implode(LF, $this->footerData) : '',
1828 'JS_LIBS_FOOTER' => $jsFooterLibs,
1829 'JS_INCLUDE_FOOTER' => $jsFooterFiles,
1830 'JS_INLINE_FOOTER' => $jsFooterInline,
1831 'BODY' => $this->bodyContent
1832 ];
1833 $markerArray = array_map('trim', $markerArray);
1834 return $markerArray;
1835 }
1836
1837 /**
1838 * Fills the marker array with the given strings and trims each value
1839 *
1840 * @param string $substituteHash The hash that is used for the placehoder markers
1841 * @return array Marker array
1842 */
1843 protected function getPreparedMarkerArrayForPageWithUncachedObjects($substituteHash)
1844 {
1845 $markerArray = [
1846 'XMLPROLOG_DOCTYPE' => $this->xmlPrologAndDocType,
1847 'HTMLTAG' => $this->htmlTag,
1848 'HEADTAG' => $this->headTag,
1849 'METACHARSET' => $this->charSet ? str_replace('|', htmlspecialchars($this->charSet), $this->metaCharsetTag) : '',
1850 'INLINECOMMENT' => $this->inlineComments ? LF . LF . '<!-- ' . LF . implode(LF, $this->inlineComments) . '-->' . LF . LF : '',
1851 'BASEURL' => $this->baseUrl ? str_replace('|', $this->baseUrl, $this->baseUrlTag) : '',
1852 'SHORTCUT' => $this->favIcon ? sprintf($this->shortcutTag, htmlspecialchars($this->favIcon), $this->iconMimeType) : '',
1853 'META' => '<!-- ###META' . $substituteHash . '### -->',
1854 'BODY' => $this->bodyContent,
1855 'TITLE' => '<!-- ###TITLE' . $substituteHash . '### -->',
1856 'CSS_LIBS' => '<!-- ###CSS_LIBS' . $substituteHash . '### -->',
1857 'CSS_INCLUDE' => '<!-- ###CSS_INCLUDE' . $substituteHash . '### -->',
1858 'CSS_INLINE' => '<!-- ###CSS_INLINE' . $substituteHash . '### -->',
1859 'JS_INLINE' => '<!-- ###JS_INLINE' . $substituteHash . '### -->',
1860 'JS_INCLUDE' => '<!-- ###JS_INCLUDE' . $substituteHash . '### -->',
1861 'JS_LIBS' => '<!-- ###JS_LIBS' . $substituteHash . '### -->',
1862 'HEADERDATA' => '<!-- ###HEADERDATA' . $substituteHash . '### -->',
1863 'FOOTERDATA' => '<!-- ###FOOTERDATA' . $substituteHash . '### -->',
1864 'JS_LIBS_FOOTER' => '<!-- ###JS_LIBS_FOOTER' . $substituteHash . '### -->',
1865 'JS_INCLUDE_FOOTER' => '<!-- ###JS_INCLUDE_FOOTER' . $substituteHash . '### -->',
1866 'JS_INLINE_FOOTER' => '<!-- ###JS_INLINE_FOOTER' . $substituteHash . '### -->'
1867 ];
1868 $markerArray = array_map('trim', $markerArray);
1869 return $markerArray;
1870 }
1871
1872 /**
1873 * Reads the template file and returns the requested part as string
1874 *
1875 * @param int $part
1876 * @return string
1877 */
1878 protected function getTemplateForPart($part)
1879 {
1880 $templateFile = GeneralUtility::getFileAbsFileName($this->templateFile);
1881 if (is_file($templateFile)) {
1882 $template = file_get_contents($templateFile);
1883 if ($this->removeLineBreaksFromTemplate) {
1884 $template = strtr($template, [LF => '', CR => '']);
1885 }
1886 if ($part !== self::PART_COMPLETE) {
1887 $templatePart = explode('###BODY###', $template);
1888 $template = $templatePart[$part - 1];
1889 }
1890 } else {
1891 $template = '';
1892 }
1893 return $template;
1894 }
1895
1896 /**
1897 * Helper function for render the main JavaScript libraries,
1898 * currently: RequireJS
1899 *
1900 * @return string Content with JavaScript libraries
1901 */
1902 protected function renderMainJavaScriptLibraries()
1903 {
1904 $out = '';
1905
1906 // Include RequireJS
1907 if ($this->addRequireJs) {
1908 $out .= $this->getRequireJsLoader();
1909 }
1910
1911 $this->loadJavaScriptLanguageStrings();
1912 if (TYPO3_MODE === 'BE') {
1913 $noBackendUserLoggedIn = empty($GLOBALS['BE_USER']->user['uid']);
1914 $this->addAjaxUrlsToInlineSettings($noBackendUserLoggedIn);
1915 }
1916 $inlineSettings = '';
1917 $languageLabels = $this->parseLanguageLabelsForJavaScript();
1918 if (!empty($languageLabels)) {
1919 $inlineSettings .= 'TYPO3.lang = ' . json_encode($languageLabels) . ';';
1920 }
1921 $inlineSettings .= $this->inlineSettings ? 'TYPO3.settings = ' . json_encode($this->inlineSettings) . ';' : '';
1922
1923 if ($inlineSettings !== '') {
1924 // make sure the global TYPO3 is available
1925 $inlineSettings = 'var TYPO3 = TYPO3 || {};' . CRLF . $inlineSettings;
1926 $out .= $this->inlineJavascriptWrap[0] . $inlineSettings . $this->inlineJavascriptWrap[1];
1927 }
1928
1929 return $out;
1930 }
1931
1932 /**
1933 * Converts the language labels for usage in JavaScript
1934 *
1935 * @return array
1936 */
1937 protected function parseLanguageLabelsForJavaScript(): array
1938 {
1939 if (empty($this->inlineLanguageLabels)) {
1940 return [];
1941 }
1942
1943 $labels = [];
1944 foreach ($this->inlineLanguageLabels as $key => $translationUnit) {
1945 if (is_array($translationUnit)) {
1946 $translationUnit = current($translationUnit);
1947 $labels[$key] = $translationUnit['target'] ?? $translationUnit['source'];
1948 } else {
1949 $labels[$key] = $translationUnit;
1950 }
1951 }
1952
1953 return $labels;
1954 }
1955
1956 /**
1957 * Load the language strings into JavaScript
1958 */
1959 protected function loadJavaScriptLanguageStrings()
1960 {
1961 if (!empty($this->inlineLanguageLabelFiles)) {
1962 foreach ($this->inlineLanguageLabelFiles as $languageLabelFile) {
1963 $this->includeLanguageFileForInline($languageLabelFile['fileRef'], $languageLabelFile['selectionPrefix'], $languageLabelFile['stripFromSelectionName']);
1964 }
1965 }
1966 $this->inlineLanguageLabelFiles = [];
1967 // Convert settings back to UTF-8 since json_encode() only works with UTF-8:
1968 if ($this->getCharSet() && $this->getCharSet() !== 'utf-8' && is_array($this->inlineSettings)) {
1969 $this->convertCharsetRecursivelyToUtf8($this->inlineSettings, $this->getCharSet());
1970 }
1971 }
1972
1973 /**
1974 * Small helper function to convert charsets for arrays into utf-8
1975 *
1976 * @param mixed $data given by reference (string/array usually)
1977 * @param string $fromCharset convert FROM this charset
1978 */
1979 protected function convertCharsetRecursivelyToUtf8(&$data, string $fromCharset)
1980 {
1981 foreach ($data as $key => $value) {
1982 if (is_array($data[$key])) {
1983 $this->convertCharsetRecursivelyToUtf8($data[$key], $fromCharset);
1984 } elseif (is_string($data[$key])) {
1985 $data[$key] = mb_convert_encoding($data[$key], 'utf-8', $fromCharset);
1986 }
1987 }
1988 }
1989
1990 /**
1991 * Make URLs to all backend ajax handlers available as inline setting.
1992 *
1993 * @param bool $publicRoutesOnly
1994 */
1995 protected function addAjaxUrlsToInlineSettings(bool $publicRoutesOnly = false)
1996 {
1997 $ajaxUrls = [];
1998 // Add the ajax-based routes
1999 /** @var UriBuilder $uriBuilder */
2000 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
2001 /** @var Router $router */
2002 $router = GeneralUtility::makeInstance(Router::class);
2003 $routes = $router->getRoutes();
2004 foreach ($routes as $routeIdentifier => $route) {
2005 if ($publicRoutesOnly && $route->getOption('access') !== 'public') {
2006 continue;
2007 }
2008 if ($route->getOption('ajax')) {
2009 $uri = (string)$uriBuilder->buildUriFromRoute($routeIdentifier);
2010 // use the shortened value in order to use this in JavaScript
2011 $routeIdentifier = str_replace('ajax_', '', $routeIdentifier);
2012 $ajaxUrls[$routeIdentifier] = $uri;
2013 }
2014 }
2015
2016 $this->inlineSettings['ajaxUrls'] = $ajaxUrls;
2017 }
2018
2019 /**
2020 * Render CSS library files
2021 *
2022 * @return string
2023 */
2024 protected function renderCssLibraries()
2025 {
2026 $cssFiles = '';
2027 if (!empty($this->cssLibs)) {
2028 foreach ($this->cssLibs as $file => $properties) {
2029 $tag = $this->createCssTag($properties, $file);
2030 if ($properties['forceOnTop']) {
2031 $cssFiles = $tag . $cssFiles;
2032 } else {
2033 $cssFiles .= $tag;
2034 }
2035 }
2036 }
2037 return $cssFiles;
2038 }
2039
2040 /**
2041 * Render CSS files
2042 *
2043 * @return string
2044 */
2045 protected function renderCssFiles()
2046 {
2047 $cssFiles = '';
2048 if (!empty($this->cssFiles)) {
2049 foreach ($this->cssFiles as $file => $properties) {
2050 $tag = $this->createCssTag($properties, $file);
2051 if ($properties['forceOnTop']) {
2052 $cssFiles = $tag . $cssFiles;
2053 } else {
2054 $cssFiles .= $tag;
2055 }
2056 }
2057 }
2058 return $cssFiles;
2059 }
2060
2061 /**
2062 * Create link (inline=0) or style (inline=1) tag
2063 *
2064 * @param array $properties
2065 * @param string $file
2066 * @return string
2067 */
2068 private function createCssTag(array $properties, string $file): string
2069 {
2070 if ($properties['inline'] && @is_file($file)) {
2071 $tag = $this->createInlineCssTagFromFile($file, $properties);
2072 } else {
2073 $href = $this->getStreamlinedFileName($file);
2074 $tag = '<link rel="' . htmlspecialchars($properties['rel'])
2075 . '" type="text/css" href="' . htmlspecialchars($href)
2076 . '" media="' . htmlspecialchars($properties['media']) . '"'
2077 . ($properties['title'] ? ' title="' . htmlspecialchars($properties['title']) . '"' : '')
2078 . $this->endingSlash . '>';
2079 }
2080 if ($properties['allWrap']) {
2081 $wrapArr = explode($properties['splitChar'] ?: '|', $properties['allWrap'], 2);
2082 $tag = $wrapArr[0] . $tag . $wrapArr[1];
2083 }
2084 $tag .= LF;
2085
2086 return $tag;
2087 }
2088
2089 /**
2090 * Render inline CSS
2091 *
2092 * @return string
2093 */
2094 protected function renderCssInline()
2095 {
2096 $cssInline = '';
2097 if (!empty($this->cssInline)) {
2098 foreach ($this->cssInline as $name => $properties) {
2099 $cssCode = '/*' . htmlspecialchars($name) . '*/' . LF . $properties['code'] . LF;
2100 if ($properties['forceOnTop']) {
2101 $cssInline = $cssCode . $cssInline;
2102 } else {
2103 $cssInline .= $cssCode;
2104 }
2105 }
2106 $cssInline = $this->inlineCssWrap[0] . $cssInline . $this->inlineCssWrap[1];
2107 }
2108 return $cssInline;
2109 }
2110
2111 /**
2112 * Render JavaScipt libraries
2113 *
2114 * @return array<string> jsLibs and jsFooterLibs strings
2115 */
2116 protected function renderAdditionalJavaScriptLibraries()
2117 {
2118 $jsLibs = '';
2119 $jsFooterLibs = '';
2120 if (!empty($this->jsLibs)) {
2121 foreach ($this->jsLibs as $properties) {
2122 $properties['file'] = $this->getStreamlinedFileName($properties['file']);
2123 $async = $properties['async'] ? ' async="async"' : '';
2124 $defer = $properties['defer'] ? ' defer="defer"' : '';
2125 $integrity = $properties['integrity'] ? ' integrity="' . htmlspecialchars($properties['integrity']) . '"' : '';
2126 $crossorigin = $properties['crossorigin'] ? ' crossorigin="' . htmlspecialchars($properties['crossorigin']) . '"' : '';
2127 $tag = '<script src="' . htmlspecialchars($properties['file']) . '" type="' . htmlspecialchars($properties['type']) . '"' . $async . $defer . $integrity . $crossorigin . '></script>';
2128 if ($properties['allWrap']) {
2129 $wrapArr = explode($properties['splitChar'] ?: '|', $properties['allWrap'], 2);
2130 $tag = $wrapArr[0] . $tag . $wrapArr[1];
2131 }
2132 $tag .= LF;
2133 if ($properties['forceOnTop']) {
2134 if ($properties['section'] === self::PART_HEADER) {
2135 $jsLibs = $tag . $jsLibs;
2136 } else {
2137 $jsFooterLibs = $tag . $jsFooterLibs;
2138 }
2139 } else {
2140 if ($properties['section'] === self::PART_HEADER) {
2141 $jsLibs .= $tag;
2142 } else {
2143 $jsFooterLibs .= $tag;
2144 }
2145 }
2146 }
2147 }
2148 if ($this->moveJsFromHeaderToFooter) {
2149 $jsFooterLibs = $jsLibs . LF . $jsFooterLibs;
2150 $jsLibs = '';
2151 }
2152 return [$jsLibs, $jsFooterLibs];
2153 }
2154
2155 /**
2156 * Render JavaScript files
2157 *
2158 * @return array<string> jsFiles and jsFooterFiles strings
2159 */
2160 protected function renderJavaScriptFiles()
2161 {
2162 $jsFiles = '';
2163 $jsFooterFiles = '';
2164 if (!empty($this->jsFiles)) {
2165 foreach ($this->jsFiles as $file => $properties) {
2166 $file = $this->getStreamlinedFileName($file);
2167 $async = $properties['async'] ? ' async="async"' : '';
2168 $defer = $properties['defer'] ? ' defer="defer"' : '';
2169 $integrity = $properties['integrity'] ? ' integrity="' . htmlspecialchars($properties['integrity']) . '"' : '';
2170 $crossorigin = $properties['crossorigin'] ? ' crossorigin="' . htmlspecialchars($properties['crossorigin']) . '"' : '';
2171 $tag = '<script src="' . htmlspecialchars($file) . '" type="' . htmlspecialchars($properties['type']) . '"' . $async . $defer . $integrity . $crossorigin . '></script>';
2172 if ($properties['allWrap']) {
2173 $wrapArr = explode($properties['splitChar'] ?: '|', $properties['allWrap'], 2);
2174 $tag = $wrapArr[0] . $tag . $wrapArr[1];
2175 }
2176 $tag .= LF;
2177 if ($properties['forceOnTop']) {
2178 if ($properties['section'] === self::PART_HEADER) {
2179 $jsFiles = $tag . $jsFiles;
2180 } else {
2181 $jsFooterFiles = $tag . $jsFooterFiles;
2182 }
2183 } else {
2184 if ($properties['section'] === self::PART_HEADER) {
2185 $jsFiles .= $tag;
2186 } else {
2187 $jsFooterFiles .= $tag;
2188 }
2189 }
2190 }
2191 }
2192 if ($this->moveJsFromHeaderToFooter) {
2193 $jsFooterFiles = $jsFiles . $jsFooterFiles;
2194 $jsFiles = '';
2195 }
2196 return [$jsFiles, $jsFooterFiles];
2197 }
2198
2199 /**
2200 * Render inline JavaScript
2201 *
2202 * @return array<string> jsInline and jsFooterInline string
2203 */
2204 protected function renderInlineJavaScript()
2205 {
2206 $jsInline = '';
2207 $jsFooterInline = '';
2208 if (!empty($this->jsInline)) {
2209 foreach ($this->jsInline as $name => $properties) {
2210 $jsCode = '/*' . htmlspecialchars($name) . '*/' . LF . $properties['code'] . LF;
2211 if ($properties['forceOnTop']) {
2212 if ($properties['section'] === self::PART_HEADER) {
2213 $jsInline = $jsCode . $jsInline;
2214 } else {
2215 $jsFooterInline = $jsCode . $jsFooterInline;
2216 }
2217 } else {
2218 if ($properties['section'] === self::PART_HEADER) {
2219 $jsInline .= $jsCode;
2220 } else {
2221 $jsFooterInline .= $jsCode;
2222 }
2223 }
2224 }
2225 }
2226 if ($jsInline) {
2227 $jsInline = $this->inlineJavascriptWrap[0] . $jsInline . $this->inlineJavascriptWrap[1];
2228 }
2229 if ($jsFooterInline) {
2230 $jsFooterInline = $this->inlineJavascriptWrap[0] . $jsFooterInline . $this->inlineJavascriptWrap[1];
2231 }
2232 if ($this->moveJsFromHeaderToFooter) {
2233 $jsFooterInline = $jsInline . $jsFooterInline;
2234 $jsInline = '';
2235 }
2236 return [$jsInline, $jsFooterInline];
2237 }
2238
2239 /**
2240 * Include language file for inline usage
2241 *
2242 * @param string $fileRef
2243 * @param string $selectionPrefix
2244 * @param string $stripFromSelectionName
2245 * @throws \RuntimeException
2246 */
2247 protected function includeLanguageFileForInline($fileRef, $selectionPrefix = '', $stripFromSelectionName = '')
2248 {
2249 if (!isset($this->lang) || !isset($this->charSet)) {
2250 throw new \RuntimeException('Language and character encoding are not set.', 1284906026);
2251 }
2252 $labelsFromFile = [];
2253 $allLabels = $this->readLLfile($fileRef);
2254 if ($allLabels !== false) {
2255 // Merge language specific translations:
2256 if ($this->lang !== 'default' && isset($allLabels[$this->lang])) {
2257 $labels = array_merge($allLabels['default'], $allLabels[$this->lang]);
2258 } else {
2259 $labels = $allLabels['default'];
2260 }
2261 // Iterate through all locallang labels:
2262 foreach ($labels as $label => $value) {
2263 // If $selectionPrefix is set, only respect labels that start with $selectionPrefix
2264 if ($selectionPrefix === '' || strpos($label, $selectionPrefix) === 0) {
2265 // Remove substring $stripFromSelectionName from label
2266 $label = str_replace($stripFromSelectionName, '', $label);
2267 $labelsFromFile[$label] = $value;
2268 }
2269 }
2270 $this->inlineLanguageLabels = array_merge($this->inlineLanguageLabels, $labelsFromFile);
2271 }
2272 }
2273
2274 /**
2275 * Reads a locallang file.
2276 *
2277 * @param string $fileRef Reference to a relative filename to include.
2278 * @return array Returns the $LOCAL_LANG array found in the file. If no array found, returns empty array.
2279 */
2280 protected function readLLfile($fileRef)
2281 {
2282 /** @var LocalizationFactory $languageFactory */
2283 $languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
2284
2285 if ($this->lang !== 'default') {
2286 $languages = array_reverse($this->languageDependencies);
2287 // At least we need to have English
2288 if (empty($languages)) {
2289 $languages[] = 'default';
2290 }
2291 } else {
2292 $languages = ['default'];
2293 }
2294
2295 $localLanguage = [];
2296 foreach ($languages as $language) {
2297 $tempLL = $languageFactory->getParsedData($fileRef, $language);
2298
2299 $localLanguage['default'] = $tempLL['default'];
2300 if (!isset($localLanguage[$this->lang])) {
2301 $localLanguage[$this->lang] = $localLanguage['default'];
2302 }
2303 if ($this->lang !== 'default' && isset($tempLL[$language])) {
2304 // Merge current language labels onto labels from previous language
2305 // This way we have a labels with fall back applied
2306 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($localLanguage[$this->lang], $tempLL[$language], true, false);
2307 }
2308 }
2309
2310 return $localLanguage;
2311 }
2312
2313 /*****************************************************/
2314 /* */
2315 /* Tools */
2316 /* */
2317 /*****************************************************/
2318 /**
2319 * Concatenate files into one file
2320 * registered handler
2321 */
2322 protected function doConcatenate()
2323 {
2324 $this->doConcatenateCss();
2325 $this->doConcatenateJavaScript();
2326 }
2327
2328 /**
2329 * Concatenate JavaScript files according to the configuration.
2330 */
2331 protected function doConcatenateJavaScript()
2332 {
2333 if ($this->concatenateJavascript) {
2334 if (!empty($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['jsConcatenateHandler'])) {
2335 // use external concatenation routine
2336 $params = [
2337 'jsLibs' => &$this->jsLibs,
2338 'jsFiles' => &$this->jsFiles,
2339 'jsFooterFiles' => &$this->jsFooterFiles,
2340 'headerData' => &$this->headerData,
2341 'footerData' => &$this->footerData
2342 ];
2343 GeneralUtility::callUserFunction($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['jsConcatenateHandler'], $params, $this);
2344 } else {
2345 $this->jsLibs = $this->getCompressor()->concatenateJsFiles($this->jsLibs);
2346 $this->jsFiles = $this->getCompressor()->concatenateJsFiles($this->jsFiles);
2347 $this->jsFooterFiles = $this->getCompressor()->concatenateJsFiles($this->jsFooterFiles);
2348 }
2349 }
2350 }
2351
2352 /**
2353 * Concatenate CSS files according to configuration.
2354 */
2355 protected function doConcatenateCss()
2356 {
2357 if ($this->concatenateCss) {
2358 if (!empty($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['cssConcatenateHandler'])) {
2359 // use external concatenation routine
2360 $params = [
2361 'cssFiles' => &$this->cssFiles,
2362 'cssLibs' => &$this->cssLibs,
2363 'headerData' => &$this->headerData,
2364 'footerData' => &$this->footerData
2365 ];
2366 GeneralUtility::callUserFunction($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['cssConcatenateHandler'], $params, $this);
2367 } else {
2368 $cssOptions = [];
2369 if (TYPO3_MODE === 'BE') {
2370 $cssOptions = ['baseDirectories' => GeneralUtility::makeInstance(DocumentTemplate::class)->getSkinStylesheetDirectories()];
2371 }
2372 $this->cssLibs = $this->getCompressor()->concatenateCssFiles($this->cssLibs, $cssOptions);
2373 $this->cssFiles = $this->getCompressor()->concatenateCssFiles($this->cssFiles, $cssOptions);
2374 }
2375 }
2376 }
2377
2378 /**
2379 * Compresses inline code
2380 */
2381 protected function doCompress()
2382 {
2383 $this->doCompressJavaScript();
2384 $this->doCompressCss();
2385 }
2386
2387 /**
2388 * Compresses CSS according to configuration.
2389 */
2390 protected function doCompressCss()
2391 {
2392 if ($this->compressCss) {
2393 if (!empty($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['cssCompressHandler'])) {
2394 // Use external compression routine
2395 $params = [
2396 'cssInline' => &$this->cssInline,
2397 'cssFiles' => &$this->cssFiles,
2398 'cssLibs' => &$this->cssLibs,
2399 'headerData' => &$this->headerData,
2400 'footerData' => &$this->footerData
2401 ];
2402 GeneralUtility::callUserFunction($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['cssCompressHandler'], $params, $this);
2403 } else {
2404 $this->cssLibs = $this->getCompressor()->compressCssFiles($this->cssLibs);
2405 $this->cssFiles = $this->getCompressor()->compressCssFiles($this->cssFiles);
2406 }
2407 }
2408 }
2409
2410 /**
2411 * Compresses JavaScript according to configuration.
2412 */
2413 protected function doCompressJavaScript()
2414 {
2415 if ($this->compressJavascript) {
2416 if (!empty($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['jsCompressHandler'])) {
2417 // Use external compression routine
2418 $params = [
2419 'jsInline' => &$this->jsInline,
2420 'jsFooterInline' => &$this->jsFooterInline,
2421 'jsLibs' => &$this->jsLibs,
2422 'jsFiles' => &$this->jsFiles,
2423 'jsFooterFiles' => &$this->jsFooterFiles,
2424 'headerData' => &$this->headerData,
2425 'footerData' => &$this->footerData
2426 ];
2427 GeneralUtility::callUserFunction($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['jsCompressHandler'], $params, $this);
2428 } else {
2429 // Traverse the arrays, compress files
2430 if (!empty($this->jsInline)) {
2431 foreach ($this->jsInline as $name => $properties) {
2432 if ($properties['compress']) {
2433 $error = '';
2434 $this->jsInline[$name]['code'] = GeneralUtility::minifyJavaScript($properties['code'], $error);
2435 if ($error) {
2436 $this->compressError .= 'Error with minify JS Inline Block "' . $name . '": ' . $error . LF;
2437 }
2438 }
2439 }
2440 }
2441 $this->jsLibs = $this->getCompressor()->compressJsFiles($this->jsLibs);
2442 $this->jsFiles = $this->getCompressor()->compressJsFiles($this->jsFiles);
2443 $this->jsFooterFiles = $this->getCompressor()->compressJsFiles($this->jsFooterFiles);
2444 }
2445 }
2446 }
2447
2448 /**
2449 * Returns instance of \TYPO3\CMS\Core\Resource\ResourceCompressor
2450 *
2451 * @return \TYPO3\CMS\Core\Resource\ResourceCompressor
2452 */
2453 protected function getCompressor()
2454 {
2455 if ($this->compressor === null) {
2456 $this->compressor = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceCompressor::class);
2457 }
2458 return $this->compressor;
2459 }
2460
2461 /**
2462 * Processes a Javascript file dependent on the current context
2463 *
2464 * Adds the version number for Frontend, compresses the file for Backend
2465 *
2466 * @param string $filename Filename
2467 * @return string New filename
2468 */
2469 protected function processJsFile($filename)
2470 {
2471 $filename = $this->getStreamlinedFileName($filename, false);
2472 if ($this->compressJavascript) {
2473 $filename = $this->getCompressor()->compressJsFile($filename);
2474 } elseif (TYPO3_MODE === 'FE') {
2475 $filename = GeneralUtility::createVersionNumberedFilename($filename);
2476 }
2477 return $this->getAbsoluteWebPath($filename);
2478 }
2479
2480 /**
2481 * This function acts as a wrapper to allow relative and paths starting with EXT: to be dealt with
2482 * in this very case to always return the absolute web path to be included directly before output.
2483 *
2484 * This is mainly added so the EXT: syntax can be resolved for PageRenderer in one central place,
2485 * and hopefully removed in the future by one standard API call.
2486 *
2487 * @param string $file the filename to process
2488 * @param bool $prepareForOutput whether the file should be prepared as version numbered file and prefixed as absolute webpath
2489 * @return string
2490 * @internal
2491 */
2492 protected function getStreamlinedFileName($file, $prepareForOutput = true)
2493 {
2494 if (strpos($file, 'EXT:') === 0) {
2495 $file = GeneralUtility::getFileAbsFileName($file);
2496 // as the path is now absolute, make it "relative" to the current script to stay compatible
2497 $file = PathUtility::getRelativePathTo($file);
2498 $file = rtrim($file, '/');
2499 } else {
2500 $file = GeneralUtility::resolveBackPath($file);
2501 }
2502 if ($prepareForOutput) {
2503 $file = GeneralUtility::createVersionNumberedFilename($file);
2504 $file = $this->getAbsoluteWebPath($file);
2505 }
2506 return $file;
2507 }
2508
2509 /**
2510 * Gets absolute web path of filename for backend disposal.
2511 * Resolving the absolute path in the frontend with conflict with
2512 * applying config.absRefPrefix in frontend rendering process.
2513 *
2514 * @param string $file
2515 * @return string
2516 * @see TypoScriptFrontendController::setAbsRefPrefix()
2517 */
2518 protected function getAbsoluteWebPath(string $file): string
2519 {
2520 if (TYPO3_MODE === 'FE') {
2521 return $file;
2522 }
2523 return PathUtility::getAbsoluteWebPath($file);
2524 }
2525
2526 /**
2527 * Returns global frontend controller
2528 *
2529 * @return TypoScriptFrontendController
2530 */
2531 protected function getTypoScriptFrontendController()
2532 {
2533 return $GLOBALS['TSFE'];
2534 }
2535
2536 /*****************************************************/
2537 /* */
2538 /* Hooks */
2539 /* */
2540 /*****************************************************/
2541 /**
2542 * Execute PreRenderHook for possible manipulation
2543 */
2544 protected function executePreRenderHook()
2545 {
2546 $hooks = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_pagerenderer.php']['render-preProcess'] ?? false;
2547 if (!$hooks) {
2548 return;
2549 }
2550 $params = [
2551 'jsLibs' => &$this->jsLibs,
2552 'jsFooterLibs' => &$this->jsFooterLibs,
2553 'jsFiles' => &$this->jsFiles,
2554 'jsFooterFiles' => &$this->jsFooterFiles,
2555 'cssLibs' => &$this->cssLibs,
2556 'cssFiles' => &$this->cssFiles,
2557 'headerData' => &$this->headerData,
2558 'footerData' => &$this->footerData,
2559 'jsInline' => &$this->jsInline,
2560 'jsFooterInline' => &$this->jsFooterInline,
2561 'cssInline' => &$this->cssInline
2562 ];
2563 foreach ($hooks as $hook) {
2564 GeneralUtility::callUserFunction($hook, $params, $this);
2565 }
2566 }
2567
2568 /**
2569 * PostTransform for possible manipulation of concatenated and compressed files
2570 */
2571 protected function executeRenderPostTransformHook()
2572 {
2573 $hooks = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_pagerenderer.php']['render-postTransform'] ?? false;
2574 if (!$hooks) {
2575 return;
2576 }
2577 $params = [
2578 'jsLibs' => &$this->jsLibs,
2579 'jsFooterLibs' => &$this->jsFooterLibs,
2580 'jsFiles' => &$this->jsFiles,
2581 'jsFooterFiles' => &$this->jsFooterFiles,
2582 'cssLibs' => &$this->cssLibs,
2583 'cssFiles' => &$this->cssFiles,
2584 'headerData' => &$this->headerData,
2585 'footerData' => &$this->footerData,
2586 'jsInline' => &$this->jsInline,
2587 'jsFooterInline' => &$this->jsFooterInline,
2588 'cssInline' => &$this->cssInline
2589 ];
2590 foreach ($hooks as $hook) {
2591 GeneralUtility::callUserFunction($hook, $params, $this);
2592 }
2593 }
2594
2595 /**
2596 * Execute postRenderHook for possible manipulation
2597 *
2598 * @param string $jsLibs
2599 * @param string $jsFiles
2600 * @param string $jsFooterFiles
2601 * @param string $cssLibs
2602 * @param string $cssFiles
2603 * @param string $jsInline
2604 * @param string $cssInline
2605 * @param string $jsFooterInline
2606 * @param string $jsFooterLibs
2607 */
2608 protected function executePostRenderHook(&$jsLibs, &$jsFiles, &$jsFooterFiles, &$cssLibs, &$cssFiles, &$jsInline, &$cssInline, &$jsFooterInline, &$jsFooterLibs)
2609 {
2610 $hooks = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_pagerenderer.php']['render-postProcess'] ?? false;
2611 if (!$hooks) {
2612 return;
2613 }
2614 $params = [
2615 'jsLibs' => &$jsLibs,
2616 'jsFiles' => &$jsFiles,
2617 'jsFooterFiles' => &$jsFooterFiles,
2618 'cssLibs' => &$cssLibs,
2619 'cssFiles' => &$cssFiles,
2620 'headerData' => &$this->headerData,
2621 'footerData' => &$this->footerData,
2622 'jsInline' => &$jsInline,
2623 'cssInline' => &$cssInline,
2624 'xmlPrologAndDocType' => &$this->xmlPrologAndDocType,
2625 'htmlTag' => &$this->htmlTag,
2626 'headTag' => &$this->headTag,
2627 'charSet' => &$this->charSet,
2628 'metaCharsetTag' => &$this->metaCharsetTag,
2629 'shortcutTag' => &$this->shortcutTag,
2630 'inlineComments' => &$this->inlineComments,
2631 'baseUrl' => &$this->baseUrl,
2632 'baseUrlTag' => &$this->baseUrlTag,
2633 'favIcon' => &$this->favIcon,
2634 'iconMimeType' => &$this->iconMimeType,
2635 'titleTag' => &$this->titleTag,
2636 'title' => &$this->title,
2637 'metaTags' => &$this->metaTags,
2638 'jsFooterInline' => &$jsFooterInline,
2639 'jsFooterLibs' => &$jsFooterLibs,
2640 'bodyContent' => &$this->bodyContent
2641 ];
2642 foreach ($hooks as $hook) {
2643 GeneralUtility::callUserFunction($hook, $params, $this);
2644 }
2645 }
2646
2647 /**
2648 * Creates an CSS inline tag
2649 *
2650 * @param string $file the filename to process
2651 * @param array $properties
2652 * @return string
2653 */
2654 protected function createInlineCssTagFromFile(string $file, array $properties): string
2655 {
2656 $cssInline = file_get_contents($file);
2657
2658 return '<style type="text/css"'
2659 . ' media="' . htmlspecialchars($properties['media']) . '"'
2660 . ($properties['title'] ? ' title="' . htmlspecialchars($properties['title']) . '"' : '')
2661 . '>' . LF
2662 . '/*<![CDATA[*/' . LF . '<!-- ' . LF
2663 . $cssInline
2664 . '-->' . LF . '/*]]>*/' . LF . '</style>' . LF;
2665 }
2666 }