[TASK] Deprecate PageRenderer->loadJQuery()
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Template / DocumentTemplate.php
1 <?php
2 namespace TYPO3\CMS\Backend\Template;
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 Psr\Log\LoggerAwareInterface;
18 use Psr\Log\LoggerAwareTrait;
19 use TYPO3\CMS\Backend\Backend\Shortcut\ShortcutRepository;
20 use TYPO3\CMS\Backend\Routing\UriBuilder;
21 use TYPO3\CMS\Backend\Utility\BackendUtility;
22 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
23 use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
24 use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
25 use TYPO3\CMS\Core\Imaging\Icon;
26 use TYPO3\CMS\Core\Imaging\IconFactory;
27 use TYPO3\CMS\Core\Page\PageRenderer;
28 use TYPO3\CMS\Core\Service\MarkerBasedTemplateService;
29 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
30 use TYPO3\CMS\Core\Utility\GeneralUtility;
31 use TYPO3\CMS\Core\Utility\PathUtility;
32
33 /**
34 * TYPO3 Backend Template Class
35 *
36 * This class contains functions for starting and ending the HTML of backend modules
37 * It also contains methods for outputting sections of content.
38 * Further there are functions for making icons, links, setting form-field widths etc.
39 * Color scheme and stylesheet definitions are also available here.
40 * Finally this file includes the language class for TYPO3's backend.
41 *
42 * After this file $LANG and $TBE_TEMPLATE are global variables / instances of their respective classes.
43 *
44 * Please refer to Inside TYPO3 for a discussion of how to use this API.
45 */
46 class DocumentTemplate implements LoggerAwareInterface
47 {
48 use LoggerAwareTrait;
49 use PublicPropertyDeprecationTrait;
50
51 /**
52 * @var array
53 */
54 protected $deprecatedPublicProperties = [
55 'hasDocheader' => 'Using $hasDocheader of class DocumentTemplate is discouraged. The property is not evaluated in the TYPO3 core anymore and will be removed in TYPO3 v10.'
56 ];
57
58 // Vars you typically might want to/should set from outside after making instance of this class:
59 /**
60 * This can be set to the HTML-code for a formtag.
61 * Useful when you need a form to span the whole page; Inserted exactly after the body-tag.
62 *
63 * @var string
64 */
65 public $form = '';
66
67 /**
68 * Additional header code (eg. a JavaScript section) could be accommulated in this var. It will be directly outputted in the header.
69 *
70 * @var string
71 */
72 public $JScode = '';
73
74 /**
75 * Similar to $JScode but for use as array with associative keys to prevent double inclusion of JS code. a <script> tag is automatically wrapped around.
76 *
77 * @var array
78 */
79 public $JScodeArray = ['jumpToUrl' => '
80 function jumpToUrl(URL) {
81 window.location.href = URL;
82 return false;
83 }
84 '];
85
86 /**
87 * Additional 'page-end' code could be accumulated in this var. It will be outputted at the end of page before </body> and some other internal page-end code.
88 *
89 * @var string
90 */
91 public $postCode = '';
92
93 /**
94 * HTML template with markers for module
95 *
96 * @var string
97 */
98 public $moduleTemplate = '';
99
100 /**
101 * The base file (not overlaid by TBE_STYLES) for the current module, useful for hooks when finding out which modules is rendered currently
102 *
103 * @var string
104 */
105 protected $moduleTemplateFilename = '';
106
107 /**
108 * Script ID
109 *
110 * @var string
111 */
112 public $scriptID = '';
113
114 /**
115 * Id which can be set for the body tag. Default value is based on script ID
116 *
117 * @var string
118 */
119 public $bodyTagId = '';
120
121 /**
122 * You can add additional attributes to the body-tag through this variable.
123 *
124 * @var string
125 */
126 public $bodyTagAdditions = '';
127
128 /**
129 * Additional CSS styles which will be added to the <style> section in the header
130 * used as array with associative keys to prevent double inclusion of CSS code
131 *
132 * @var array
133 */
134 public $inDocStylesArray = [];
135
136 /**
137 * Filename of stylesheet
138 *
139 * @var string
140 */
141 public $styleSheetFile = '';
142
143 /**
144 * Filename of stylesheet #2 - linked to right after the $this->styleSheetFile script
145 *
146 * @var string
147 */
148 public $styleSheetFile2 = '';
149
150 /**
151 * Filename of a post-stylesheet - included right after all inline styles.
152 *
153 * @var string
154 */
155 public $styleSheetFile_post = '';
156
157 /**
158 * Whether to use the X-UA-Compatible meta tag
159 *
160 * @var bool
161 */
162 protected $useCompatibilityTag = true;
163
164 /**
165 * X-Ua-Compatible version output in meta tag
166 *
167 * @var string
168 */
169 protected $xUaCompatibilityVersion = 'IE=edge';
170
171 // Skinning
172 /**
173 * Include these CSS directories from skins by default
174 *
175 * @var array
176 */
177 protected $stylesheetsSkins = [
178 'structure' => 'Resources/Public/Css/structure/',
179 'visual' => 'Resources/Public/Css/visual/'
180 ];
181
182 /**
183 * JavaScript files loaded for every page in the Backend
184 *
185 * @var array
186 */
187 protected $jsFiles = [];
188
189 /**
190 * JavaScript files loaded for every page in the Backend, but explicitly excluded from concatenation (useful for libraries etc.)
191 *
192 * @var array
193 */
194 protected $jsFilesNoConcatenation = [];
195
196 /**
197 * Indicates if a <div>-output section is open
198 *
199 * @var int
200 * @internal will be removed in TYPO3 v9
201 */
202 public $sectionFlag = 0;
203
204 /**
205 * (Default) Class for wrapping <DIV>-tag of page. Is set in class extensions.
206 *
207 * @var string
208 */
209 public $divClass = '';
210
211 /**
212 * @var string
213 */
214 public $pageHeaderBlock = '';
215
216 /**
217 * @var string
218 */
219 public $endOfPageJsBlock = '';
220
221 /**
222 * @var bool
223 * @deprecated since TYPO3 v9.4, will be removed in TYPO3 v10
224 */
225 protected $hasDocheader = true;
226
227 /**
228 * @var PageRenderer
229 */
230 protected $pageRenderer;
231
232 /**
233 * Alternative template file
234 *
235 * @var string
236 */
237 protected $pageHeaderFooterTemplateFile = '';
238
239 /**
240 * Whether flashmessages should be rendered or not
241 *
242 * @var bool $showFlashMessages
243 */
244 public $showFlashMessages = true;
245
246 /**
247 * @var IconFactory
248 */
249 protected $iconFactory;
250
251 /**
252 * @var MarkerBasedTemplateService
253 */
254 protected $templateService;
255
256 /**
257 * Constructor
258 */
259 public function __construct()
260 {
261 // Initializes the page rendering object:
262 $this->initPageRenderer();
263
264 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
265
266 // initialize Marker Support
267 $this->templateService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
268
269 // Setting default scriptID, trim forward slash from route
270 $this->scriptID = ltrim(GeneralUtility::_GET('route'), '/');
271 $this->bodyTagId = preg_replace('/[^A-Za-z0-9-]/', '-', $this->scriptID);
272 // Individual configuration per script? If so, make a recursive merge of the arrays:
273 if (is_array($GLOBALS['TBE_STYLES']['scriptIDindex'][$this->scriptID] ?? false)) {
274 // Make copy
275 $ovr = $GLOBALS['TBE_STYLES']['scriptIDindex'][$this->scriptID];
276 // merge styles.
277 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($GLOBALS['TBE_STYLES'], $ovr);
278 // Have to unset - otherwise the second instantiation will do it again!
279 unset($GLOBALS['TBE_STYLES']['scriptIDindex'][$this->scriptID]);
280 }
281 // Main Stylesheets:
282 if (!empty($GLOBALS['TBE_STYLES']['stylesheet'])) {
283 $this->styleSheetFile = $GLOBALS['TBE_STYLES']['stylesheet'];
284 }
285 if (!empty($GLOBALS['TBE_STYLES']['stylesheet2'])) {
286 $this->styleSheetFile2 = $GLOBALS['TBE_STYLES']['stylesheet2'];
287 }
288 if (!empty($GLOBALS['TBE_STYLES']['styleSheetFile_post'])) {
289 $this->styleSheetFile_post = $GLOBALS['TBE_STYLES']['styleSheetFile_post'];
290 }
291 if (!empty($GLOBALS['TBE_STYLES']['inDocStyles_TBEstyle'])) {
292 $this->inDocStylesArray['TBEstyle'] = $GLOBALS['TBE_STYLES']['inDocStyles_TBEstyle'];
293 }
294 // include all stylesheets
295 foreach ($this->getSkinStylesheetDirectories() as $stylesheetDirectory) {
296 $this->addStyleSheetDirectory($stylesheetDirectory);
297 }
298 }
299
300 /**
301 * Initializes the page renderer object
302 */
303 protected function initPageRenderer()
304 {
305 if ($this->pageRenderer !== null) {
306 return;
307 }
308 $this->pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
309 $this->pageRenderer->setLanguage($GLOBALS['LANG']->lang);
310 $this->pageRenderer->enableConcatenateCss();
311 $this->pageRenderer->enableConcatenateJavascript();
312 $this->pageRenderer->enableCompressCss();
313 $this->pageRenderer->enableCompressJavascript();
314 // Add all JavaScript files defined in $this->jsFiles to the PageRenderer
315 foreach ($this->jsFilesNoConcatenation as $file) {
316 $this->pageRenderer->addJsFile(
317 $file,
318 'text/javascript',
319 true,
320 false,
321 '',
322 true
323 );
324 }
325 // Add all JavaScript files defined in $this->jsFiles to the PageRenderer
326 foreach ($this->jsFiles as $file) {
327 $this->pageRenderer->addJsFile($file);
328 }
329 if ((int)$GLOBALS['TYPO3_CONF_VARS']['BE']['debug'] === 1) {
330 $this->pageRenderer->enableDebugMode();
331 }
332 }
333
334 /*****************************************
335 *
336 * EVALUATION FUNCTIONS
337 * Various centralized processing
338 *
339 *****************************************/
340
341 /**
342 * Returns a linked shortcut-icon which will call the shortcut frame and set a shortcut there back to the calling page/module
343 *
344 * @param string $gvList Is the list of GET variables to store (if any)
345 * @param string $setList Is the list of SET[] variables to store (if any) - SET[] variables a stored in $GLOBALS["SOBE"]->MOD_SETTINGS for backend modules
346 * @param string $modName Module name string
347 * @param string|int $motherModName Is used to enter the "parent module name" if the module is a submodule under eg. Web>* or File>*. You can also set this value to 1 in which case the currentLoadedModule is sent to the shortcut script (so - not a fixed value!) - that is used in file_edit and wizard_rte modules where those are really running as a part of another module.
348 * @param string $classes
349 * @return string HTML content
350 */
351 public function makeShortcutIcon($gvList, $setList, $modName, $motherModName = '', $classes = '')
352 {
353 $gvList = 'route,' . $gvList;
354 $storeUrl = $this->makeShortcutUrl($gvList, $setList);
355 $pathInfo = parse_url(GeneralUtility::getIndpEnv('REQUEST_URI'));
356 // Fallback for alt_mod. We still pass in the old xMOD... stuff, but TBE_MODULES only knows about "record_edit".
357 // We still need to pass the xMOD name to createShortcut below, since this is used for icons.
358 $moduleName = $modName === 'xMOD_alt_doc.php' ? 'record_edit' : $modName;
359 // Add the module identifier automatically if typo3/index.php is used:
360 if (GeneralUtility::_GET('M') !== null) {
361 $storeUrl = '&M=' . $moduleName . $storeUrl;
362 }
363 if ((int)$motherModName === 1) {
364 $motherModule = 'top.currentModuleLoaded';
365 } elseif ($motherModName) {
366 $motherModule = GeneralUtility::quoteJSvalue($motherModName);
367 } else {
368 $motherModule = '\'\'';
369 }
370 $confirmationText = GeneralUtility::quoteJSvalue($GLOBALS['LANG']->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.makeBookmark'));
371
372 $shortcutUrl = $pathInfo['path'] . '?' . $storeUrl;
373 $shortcutRepository = GeneralUtility::makeInstance(ShortcutRepository::class);
374 $shortcutExist = $shortcutRepository->shortcutExists($shortcutUrl);
375
376 if ($shortcutExist) {
377 return '<a class="active ' . htmlspecialchars($classes) . '" title="">' .
378 $this->iconFactory->getIcon('actions-system-shortcut-active', Icon::SIZE_SMALL)->render() . '</a>';
379 }
380 $url = GeneralUtility::quoteJSvalue(rawurlencode($shortcutUrl));
381 $onClick = 'top.TYPO3.ShortcutMenu.createShortcut(' . GeneralUtility::quoteJSvalue(rawurlencode($modName)) .
382 ', ' . $url . ', ' . $confirmationText . ', ' . $motherModule . ', this);return false;';
383
384 return '<a href="#" class="' . htmlspecialchars($classes) . '" onclick="' . htmlspecialchars($onClick) . '" title="' .
385 htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.makeBookmark')) . '">' .
386 $this->iconFactory->getIcon('actions-system-shortcut-new', Icon::SIZE_SMALL)->render() . '</a>';
387 }
388
389 /**
390 * MAKE url for storing
391 * Internal func
392 *
393 * @param string $gvList Is the list of GET variables to store (if any)
394 * @param string $setList Is the list of SET[] variables to store (if any) - SET[] variables a stored in $GLOBALS["SOBE"]->MOD_SETTINGS for backend modules
395 * @return string
396 * @internal
397 * @see makeShortcutIcon()
398 */
399 public function makeShortcutUrl($gvList, $setList)
400 {
401 $GET = GeneralUtility::_GET();
402 $storeArray = array_merge(GeneralUtility::compileSelectedGetVarsFromArray($gvList, $GET), ['SET' => GeneralUtility::compileSelectedGetVarsFromArray($setList, (array)$GLOBALS['SOBE']->MOD_SETTINGS)]);
403 return GeneralUtility::implodeArrayForUrl('', $storeArray);
404 }
405
406 /**
407 * Returns <input> attributes to set the width of an text-type input field.
408 * For client browsers with no CSS support the cols/size attribute is returned.
409 * For CSS compliant browsers (recommended) a ' style="width: ...px;"' is returned.
410 *
411 * @param int $size A relative number which multiplied with approx. 10 will lead to the width in pixels
412 * @param bool $textarea A flag you can set for textareas - DEPRECATED as there is no difference any more between the two
413 * @param string $styleOverride A string which will be returned as attribute-value for style="" instead of the calculated width (if CSS is enabled)
414 * @return string Tag attributes for an <input> tag (regarding width)
415 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.
416 */
417 public function formWidth($size = 48, $textarea = false, $styleOverride = '')
418 {
419 trigger_error('This method will be removed in TYPO3 10 - use responsive code or direct inline styles to format your input fields instead.', E_USER_DEPRECATED);
420 return ' style="' . ($styleOverride ?: 'width:' . ceil($size * 9.58) . 'px;') . '"';
421 }
422
423 /**
424 * Returns JavaScript variables setting the returnUrl and thisScript location for use by JavaScript on the page.
425 * Used in fx. db_list.php (Web>List)
426 *
427 * @param string $thisLocation URL to "this location" / current script
428 * @return string Urls are returned as JavaScript variables T3_RETURN_URL and T3_THIS_LOCATION
429 * @see typo3/db_list.php
430 */
431 public function redirectUrls($thisLocation = '')
432 {
433 $thisLocation = $thisLocation ? $thisLocation : GeneralUtility::linkThisScript([
434 'CB' => '',
435 'SET' => '',
436 'cmd' => '',
437 'popViewId' => ''
438 ]);
439 $out = '
440 var T3_RETURN_URL = ' . GeneralUtility::quoteJSvalue(str_replace('%20', '', rawurlencode(GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'))))) . ';
441 var T3_THIS_LOCATION = ' . GeneralUtility::quoteJSvalue(str_replace('%20', '', rawurlencode($thisLocation))) . '
442 ';
443 return $out;
444 }
445
446 /**
447 * Defines whether to use the X-UA-Compatible meta tag.
448 *
449 * @param bool $useCompatibilityTag Whether to use the tag
450 */
451 public function useCompatibilityTag($useCompatibilityTag = true)
452 {
453 $this->useCompatibilityTag = (bool)$useCompatibilityTag;
454 }
455
456 /*****************************************
457 *
458 * PAGE BUILDING FUNCTIONS.
459 * Use this to build the HTML of your backend modules
460 *
461 *****************************************/
462 /**
463 * Returns page start
464 * This includes the proper header with charset, title, meta tag and beginning body-tag.
465 *
466 * @param string $title HTML Page title for the header
467 * @return string Returns the whole header section of a HTML-document based on settings in internal variables (like styles, javascript code, charset, generator and docType)
468 * @see endPage()
469 */
470 public function startPage($title)
471 {
472 // hook pre start page
473 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/template.php']['preStartPageHook'] ?? [] as $hookFunction) {
474 $hookParameters = [
475 'title' => &$title
476 ];
477 GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
478 }
479 // alternative template for Header and Footer
480 if ($this->pageHeaderFooterTemplateFile) {
481 $file = GeneralUtility::getFileAbsFileName($this->pageHeaderFooterTemplateFile);
482 if ($file) {
483 $this->pageRenderer->setTemplateFile($file);
484 }
485 }
486
487 // Disable rendering of XHTML tags
488 $this->pageRenderer->setRenderXhtml(false);
489
490 $languageCode = $this->pageRenderer->getLanguage() === 'default' ? 'en' : $this->pageRenderer->getLanguage();
491 $this->pageRenderer->setHtmlTag('<html lang="' . $languageCode . '">');
492
493 $headerStart = '<!DOCTYPE html>';
494 $this->pageRenderer->setXmlPrologAndDocType($headerStart);
495 $this->pageRenderer->setHeadTag('<head>' . LF . '<!-- TYPO3 Script ID: ' . htmlspecialchars($this->scriptID) . ' -->');
496 header('Content-Type:text/html;charset=utf-8');
497 $this->pageRenderer->setCharSet('utf-8');
498 $this->pageRenderer->setMetaTag('name', 'generator', $this->generator());
499 $this->pageRenderer->setMetaTag('name', 'robots', 'noindex,follow');
500 $this->pageRenderer->setMetaTag('name', 'viewport', 'width=device-width, initial-scale=1');
501 $this->pageRenderer->setFavIcon($this->getBackendFavicon());
502 if ($this->useCompatibilityTag) {
503 $this->pageRenderer->setMetaTag('http-equiv', 'X-UA-Compatible', $this->xUaCompatibilityVersion);
504 }
505 $this->pageRenderer->setTitle($title);
506 // add docstyles
507 $this->docStyle();
508 $this->pageRenderer->addHeaderData($this->JScode);
509 foreach ($this->JScodeArray as $name => $code) {
510 $this->pageRenderer->addJsInlineCode($name, $code, false);
511 }
512
513 // Note: please do not reference "bootstrap" outside of the TYPO3 Core (not in your own extensions)
514 // as this is preliminary as long as Twitter bootstrap does not support AMD modules
515 // this logic will be changed once Twitter bootstrap 4 is included
516 // @todo
517 $this->pageRenderer->addJsFile('EXT:core/Resources/Public/JavaScript/Contrib/bootstrap/bootstrap.js');
518
519 // csh manual require js module & moduleUrl
520 if (TYPO3_MODE === 'BE' && $this->getBackendUser() && !empty($this->getBackendUser()->user)) {
521 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
522 $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/ContextHelp');
523 $this->pageRenderer->addInlineSetting(
524 'ContextHelp',
525 'moduleUrl',
526 (string)$uriBuilder->buildUriFromRoute('help_cshmanual', ['action' => 'detail'])
527 );
528 }
529
530 // hook for additional headerData
531 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/template.php']['preHeaderRenderHook'] ?? [] as $hookFunction) {
532 $hookParameters = [
533 'pageRenderer' => &$this->pageRenderer
534 ];
535 GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
536 }
537 // Construct page header.
538 $str = $this->pageRenderer->render(PageRenderer::PART_HEADER);
539 $this->JScode = '';
540 $this->JScodeArray = [];
541 $this->endOfPageJsBlock = $this->pageRenderer->render(PageRenderer::PART_FOOTER);
542 $str .= $this->docBodyTagBegin() . ($this->divClass ? '
543
544 <!-- Wrapping DIV-section for whole page BEGIN -->
545 <div class="' . $this->divClass . '">
546 ' : '') . trim($this->form);
547 return $str;
548 }
549
550 /**
551 * Returns page end; This includes finishing form, div, body and html tags.
552 *
553 * @return string The HTML end of a page
554 * @see startPage()
555 */
556 public function endPage()
557 {
558 $str = $this->postCode . GeneralUtility::wrapJS(BackendUtility::getUpdateSignalCode()) . ($this->form ? '
559 </form>' : '');
560 // If something is in buffer like debug, put it to end of page
561 if (ob_get_contents()) {
562 $str .= ob_get_clean();
563 if (!headers_sent()) {
564 header('Content-Encoding: None');
565 }
566 }
567 $str .= ($this->divClass ? '
568
569 <!-- Wrapping DIV-section for whole page END -->
570 </div>' : '') . $this->endOfPageJsBlock;
571
572 // Logging: Can't find better place to put it:
573 $this->logger->debug('END of BACKEND session', ['_FLUSH' => true]);
574 return $str;
575 }
576
577 /**
578 * Shortcut for render the complete page of a module
579 *
580 * @param string $title page title
581 * @param string $content page content
582 * @return string complete page
583 */
584 public function render($title, $content)
585 {
586 $pageContent = $this->startPage($title);
587 $pageContent .= $content;
588 $pageContent .= $this->endPage();
589 return $this->insertStylesAndJS($pageContent);
590 }
591
592 /**
593 * Creates the bodyTag.
594 * You can add to the bodyTag by $this->bodyTagAdditions
595 *
596 * @return string HTML body tag
597 */
598 public function docBodyTagBegin()
599 {
600 return '<body ' . trim($this->bodyTagAdditions . ($this->bodyTagId ? ' id="' . $this->bodyTagId . '"' : '')) . '>';
601 }
602
603 /**
604 * Outputting document style
605 */
606 public function docStyle()
607 {
608 // Implode it all:
609 $inDocStyles = implode(LF, $this->inDocStylesArray);
610
611 // Reset styles so they won't be added again in insertStylesAndJS()
612 $this->inDocStylesArray = [];
613
614 if ($this->styleSheetFile) {
615 $this->pageRenderer->addCssFile($this->styleSheetFile);
616 }
617 if ($this->styleSheetFile2) {
618 $this->pageRenderer->addCssFile($this->styleSheetFile2);
619 }
620
621 if ($inDocStyles !== '') {
622 $this->pageRenderer->addCssInlineBlock('inDocStyles', $inDocStyles . LF . '/*###POSTCSSMARKER###*/');
623 }
624
625 if ($this->styleSheetFile_post) {
626 $this->pageRenderer->addCssFile($this->styleSheetFile_post);
627 }
628 }
629
630 /**
631 * Insert additional style sheet link
632 *
633 * @param string $key some key identifying the style sheet
634 * @param string $href uri to the style sheet file
635 * @param string $title value for the title attribute of the link element
636 * @param string $relation value for the rel attribute of the link element
637 * @deprecated since TYPO3 v9.4, will be removed in TYPO3 v10
638 * @see PageRenderer::addCssFile()
639 */
640 public function addStyleSheet($key, $href, $title = '', $relation = 'stylesheet')
641 {
642 trigger_error('The method `DocumentTemplate::addStyleSheet has been deprecated, this method will be removed in TYPO3 v10.0', E_USER_DEPRECATED);
643 $this->pageRenderer->addCssFile($href, $relation, 'screen', $title);
644 }
645
646 /**
647 * Add all *.css files of the directory $path to the stylesheets
648 *
649 * @param string $path directory to add
650 */
651 public function addStyleSheetDirectory($path)
652 {
653 $path = GeneralUtility::getFileAbsFileName($path);
654 // Read all files in directory and sort them alphabetically
655 $cssFiles = GeneralUtility::getFilesInDir($path, 'css');
656 foreach ($cssFiles as $cssFile) {
657 $this->pageRenderer->addCssFile(PathUtility::getRelativePathTo($path) . $cssFile);
658 }
659 }
660
661 /**
662 * Insert post rendering document style into already rendered content
663 * This is needed for extobjbase
664 *
665 * @param string $content style-content to insert.
666 * @return string content with inserted styles
667 */
668 public function insertStylesAndJS($content)
669 {
670 $styles = LF . implode(LF, $this->inDocStylesArray);
671 $content = str_replace('/*###POSTCSSMARKER###*/', $styles, $content);
672
673 // Insert accumulated JS
674 $jscode = $this->JScode . LF . GeneralUtility::wrapJS(implode(LF, $this->JScodeArray));
675 $content = str_replace('<!--###POSTJSMARKER###-->', $jscode, $content);
676 return $content;
677 }
678
679 /**
680 * Returns an array of all stylesheet directories belonging to core and skins
681 *
682 * @return array Stylesheet directories
683 */
684 public function getSkinStylesheetDirectories()
685 {
686 $stylesheetDirectories = [];
687 // Stylesheets from skins
688 // merge default css directories ($this->stylesheetsSkin) with additional ones and include them
689 if (is_array($GLOBALS['TBE_STYLES']['skins'])) {
690 // loop over all registered skins
691 foreach ($GLOBALS['TBE_STYLES']['skins'] as $skinExtKey => $skin) {
692 $skinStylesheetDirs = $this->stylesheetsSkins;
693 // Skins can add custom stylesheetDirectories using
694 // $GLOBALS['TBE_STYLES']['skins'][$_EXTKEY]['stylesheetDirectories']
695 if (is_array($skin['stylesheetDirectories'])) {
696 $skinStylesheetDirs = array_merge($skinStylesheetDirs, $skin['stylesheetDirectories']);
697 }
698 // Add all registered directories
699 foreach ($skinStylesheetDirs as $stylesheetDir) {
700 // for EXT:myskin/stylesheets/ syntax
701 if (strpos($stylesheetDir, 'EXT:') === 0) {
702 list($extKey, $path) = explode('/', substr($stylesheetDir, 4), 2);
703 if (!empty($extKey) && ExtensionManagementUtility::isLoaded($extKey) && !empty($path)) {
704 $stylesheetDirectories[] = ExtensionManagementUtility::extPath($extKey) . $path;
705 }
706 } else {
707 // For relative paths
708 $stylesheetDirectories[] = ExtensionManagementUtility::extPath($skinExtKey) . $stylesheetDir;
709 }
710 }
711 }
712 }
713 return $stylesheetDirectories;
714 }
715
716 /**
717 * Returns generator meta tag
718 *
719 * @return string <meta> tag with name "generator
720 */
721 public function generator()
722 {
723 return 'TYPO3 CMS, ' . TYPO3_URL_GENERAL . ', &#169; Kasper Sk&#229;rh&#248;j ' . TYPO3_copyright_year . ', extensions are copyright of their respective owners.';
724 }
725
726 /**
727 * Returns X-UA-Compatible meta tag
728 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.
729 *
730 * @param string $content Content of the compatible tag (default: IE-8)
731 * @return string <meta http-equiv="X-UA-Compatible" content="???" />
732 */
733 public function xUaCompatible($content = 'IE=8')
734 {
735 trigger_error('Method DocumentTemplate->xUaCompatible is deprecated and will be removed with v10. Use pageRenderer->setMetaTag instead.', E_USER_DEPRECATED);
736 return '<meta http-equiv="X-UA-Compatible" content="' . $content . '" />';
737 }
738
739 /*****************************************
740 *
741 * OTHER ELEMENTS
742 * Tables, buttons, formatting dimmed/red strings
743 *
744 ******************************************/
745
746 /**
747 * Function to load a HTML template file with markers.
748 * When calling from own extension, use syntax getHtmlTemplate('EXT:extkey/template.html')
749 *
750 * @param string $filename tmpl name, usually in the typo3/template/ directory
751 * @return string HTML of template
752 */
753 public function getHtmlTemplate($filename)
754 {
755 // setting the name of the original HTML template
756 $this->moduleTemplateFilename = $filename;
757 if ($GLOBALS['TBE_STYLES']['htmlTemplates'][$filename]) {
758 $filename = $GLOBALS['TBE_STYLES']['htmlTemplates'][$filename];
759 }
760 $filename = GeneralUtility::getFileAbsFileName($filename);
761 return $filename !== '' ? file_get_contents($filename) : '';
762 }
763
764 /**
765 * Define the template for the module
766 *
767 * @param string $filename filename
768 */
769 public function setModuleTemplate($filename)
770 {
771 $this->moduleTemplate = $this->getHtmlTemplate($filename);
772 }
773
774 /**
775 * Put together the various elements for the module <body> using a static HTML
776 * template
777 *
778 * @param array $pageRecord Record of the current page, used for page path and info
779 * @param array $buttons HTML for all buttons
780 * @param array $markerArray HTML for all other markers
781 * @param array $subpartArray HTML for the subparts
782 * @return string Composite HTML
783 */
784 public function moduleBody($pageRecord = [], $buttons = [], $markerArray = [], $subpartArray = [])
785 {
786 // Get the HTML template for the module
787 $moduleBody = $this->templateService->getSubpart($this->moduleTemplate, '###FULLDOC###');
788 // Add CSS
789 $this->inDocStylesArray[] = 'html { overflow: hidden; }';
790 // Get the page path for the docheader
791 $markerArray['PAGEPATH'] = $this->getPagePath($pageRecord);
792 // Get the page info for the docheader
793 $markerArray['PAGEINFO'] = $this->getPageInfo($pageRecord);
794 // Get all the buttons for the docheader
795 $docHeaderButtons = $this->getDocHeaderButtons($buttons);
796 // Merge docheader buttons with the marker array
797 $markerArray = array_merge($markerArray, $docHeaderButtons);
798 // replacing subparts
799 foreach ($subpartArray as $marker => $content) {
800 $moduleBody = $this->templateService->substituteSubpart($moduleBody, $marker, $content);
801 }
802 // adding flash messages
803 if ($this->showFlashMessages) {
804 $flashMessages = $this->getFlashMessages();
805 if (!empty($flashMessages)) {
806 $markerArray['FLASHMESSAGES'] = $flashMessages;
807 // If there is no dedicated marker for the messages present
808 // then force them to appear before the content
809 if (strpos($moduleBody, '###FLASHMESSAGES###') === false) {
810 $moduleBody = str_replace('###CONTENT###', '###FLASHMESSAGES######CONTENT###', $moduleBody);
811 }
812 }
813 }
814 // Hook for adding more markers/content to the page, like the version selector
815 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/template.php']['moduleBodyPostProcess'] ?? [] as $funcRef) {
816 $params = [
817 'moduleTemplateFilename' => &$this->moduleTemplateFilename,
818 'moduleTemplate' => &$this->moduleTemplate,
819 'moduleBody' => &$moduleBody,
820 'markers' => &$markerArray,
821 'parentObject' => &$this
822 ];
823 GeneralUtility::callUserFunction($funcRef, $params, $this);
824 }
825 // Replacing all markers with the finished markers and return the HTML content
826 return $this->templateService->substituteMarkerArray($moduleBody, $markerArray, '###|###');
827 }
828
829 /**
830 * Get the default rendered FlashMessages from queue
831 *
832 * @return string
833 */
834 public function getFlashMessages()
835 {
836 /** @var \TYPO3\CMS\Core\Messaging\FlashMessageService $flashMessageService */
837 $flashMessageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessageService::class);
838 /** @var \TYPO3\CMS\Core\Messaging\FlashMessageQueue $defaultFlashMessageQueue */
839 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
840 return $defaultFlashMessageQueue->renderFlashMessages();
841 }
842
843 /**
844 * Fill the button lists with the defined HTML
845 *
846 * @param array $buttons HTML for all buttons
847 * @return array Containing HTML for both buttonlists
848 */
849 protected function getDocHeaderButtons($buttons)
850 {
851 $markers = [];
852 // Fill buttons for left and right float
853 $floats = ['left', 'right'];
854 foreach ($floats as $key) {
855 // Get the template for each float
856 $buttonTemplate = $this->templateService->getSubpart($this->moduleTemplate, '###BUTTON_GROUPS_' . strtoupper($key) . '###');
857 // Fill the button markers in this float
858 $buttonTemplate = $this->templateService->substituteMarkerArray($buttonTemplate, $buttons, '###|###', true);
859 // getting the wrap for each group
860 $buttonWrap = $this->templateService->getSubpart($this->moduleTemplate, '###BUTTON_GROUP_WRAP###');
861 // looping through the groups (max 6) and remove the empty groups
862 for ($groupNumber = 1; $groupNumber < 6; $groupNumber++) {
863 $buttonMarker = '###BUTTON_GROUP' . $groupNumber . '###';
864 $buttonGroup = $this->templateService->getSubpart($buttonTemplate, $buttonMarker);
865 if (trim($buttonGroup)) {
866 if ($buttonWrap) {
867 $buttonGroup = $this->templateService->substituteMarker($buttonWrap, '###BUTTONS###', $buttonGroup);
868 }
869 $buttonTemplate = $this->templateService->substituteSubpart($buttonTemplate, $buttonMarker, trim($buttonGroup));
870 }
871 }
872 // Replace the marker with the template and remove all line breaks (for IE compat)
873 $markers['BUTTONLIST_' . strtoupper($key)] = str_replace(LF, '', $buttonTemplate);
874 }
875 // Hook for manipulating docHeaderButtons
876 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/template.php']['docHeaderButtonsHook'] ?? [] as $funcRef) {
877 $params = [
878 'buttons' => $buttons,
879 'markers' => &$markers,
880 'pObj' => &$this
881 ];
882 GeneralUtility::callUserFunction($funcRef, $params, $this);
883 }
884 return $markers;
885 }
886
887 /**
888 * Generate the page path for docheader
889 *
890 * @param array $pageRecord Current page
891 * @return string Page path
892 */
893 protected function getPagePath($pageRecord)
894 {
895 // Is this a real page
896 if (is_array($pageRecord) && $pageRecord['uid']) {
897 $title = substr($pageRecord['_thePathFull'], 0, -1);
898 // Remove current page title
899 $pos = strrpos($title, $pageRecord['title']);
900 if ($pos !== false) {
901 $title = substr($title, 0, $pos);
902 }
903 } else {
904 $title = '';
905 }
906 // Setting the path of the page
907 $pagePath = htmlspecialchars($GLOBALS['LANG']->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.path')) . ': <span class="typo3-docheader-pagePath">';
908 // crop the title to title limit (or 50, if not defined)
909 $cropLength = empty($GLOBALS['BE_USER']->uc['titleLen']) ? 50 : $GLOBALS['BE_USER']->uc['titleLen'];
910 $croppedTitle = GeneralUtility::fixed_lgd_cs($title, -$cropLength);
911 if ($croppedTitle !== $title) {
912 $pagePath .= '<abbr title="' . htmlspecialchars($title) . '">' . htmlspecialchars($croppedTitle) . '</abbr>';
913 } else {
914 $pagePath .= htmlspecialchars($title);
915 }
916 $pagePath .= '</span>';
917 return $pagePath;
918 }
919
920 /**
921 * Setting page icon with context menu + uid for docheader
922 *
923 * @param array $pageRecord Current page
924 * @return string Page info
925 */
926 protected function getPageInfo($pageRecord)
927 {
928 // Add icon with context menu, etc:
929 // If there IS a real page
930 if (is_array($pageRecord) && $pageRecord['uid']) {
931 $alttext = BackendUtility::getRecordIconAltText($pageRecord, 'pages');
932 $iconImg = '<span title="' . htmlspecialchars($alttext) . '">' . $this->iconFactory->getIconForRecord('pages', $pageRecord, Icon::SIZE_SMALL)->render() . '</span>';
933 // Make Icon:
934 $theIcon = BackendUtility::wrapClickMenuOnIcon($iconImg, 'pages', $pageRecord['uid']);
935 $uid = $pageRecord['uid'];
936 $title = BackendUtility::getRecordTitle('pages', $pageRecord);
937 } else {
938 // On root-level of page tree
939 // Make Icon
940 $iconImg = '<span title="' . htmlspecialchars($GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']) . '">' . $this->iconFactory->getIcon('apps-pagetree-root', Icon::SIZE_SMALL)->render() . '</span>';
941 if ($GLOBALS['BE_USER']->isAdmin()) {
942 $theIcon = BackendUtility::wrapClickMenuOnIcon($iconImg, 'pages');
943 } else {
944 $theIcon = $iconImg;
945 }
946 $uid = '0';
947 $title = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
948 }
949 // Setting icon with context menu + uid
950 $pageInfo = $theIcon . '<strong>' . htmlspecialchars($title) . '&nbsp;[' . $uid . ']</strong>';
951 return $pageInfo;
952 }
953
954 /**
955 * Retrieves configured favicon for backend (with fallback)
956 *
957 * @return string
958 */
959 protected function getBackendFavicon()
960 {
961 $backendFavicon = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('backend', 'backendFavicon');
962 if (!empty($backendFavicon)) {
963 $path = $this->getUriForFileName($backendFavicon);
964 } else {
965 $path = ExtensionManagementUtility::extPath('backend') . 'Resources/Public/Icons/favicon.ico';
966 }
967 return PathUtility::getAbsoluteWebPath($path);
968 }
969
970 /**
971 * Returns the uri of a relative reference, resolves the "EXT:" prefix
972 * (way of referring to files inside extensions) and checks that the file is inside
973 * the project root of the TYPO3 installation
974 *
975 * @param string $filename The input filename/filepath to evaluate
976 * @return string Returns the filename of $filename if valid, otherwise blank string.
977 */
978 protected function getUriForFileName($filename)
979 {
980 if (strpos($filename, '://')) {
981 return $filename;
982 }
983 $urlPrefix = '';
984 if (strpos($filename, 'EXT:') === 0) {
985 $absoluteFilename = GeneralUtility::getFileAbsFileName($filename);
986 $filename = '';
987 if ($absoluteFilename !== '') {
988 $filename = PathUtility::getAbsoluteWebPath($absoluteFilename);
989 }
990 } elseif (strpos($filename, '/') !== 0) {
991 $urlPrefix = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
992 }
993 return $urlPrefix . $filename;
994 }
995
996 /**
997 * @return BackendUserAuthentication|null
998 */
999 protected function getBackendUser()
1000 {
1001 return $GLOBALS['BE_USER'] ?? null;
1002 }
1003 }