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