2 namespace TYPO3\CMS\Core\Messaging
;
4 use TYPO3\CMS\Core\Service\MarkerBasedTemplateService
;
5 use TYPO3\CMS\Core\Utility\GeneralUtility
;
8 * This file is part of the TYPO3 CMS project.
10 * It is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License, either version 2
12 * of the License, or any later version.
14 * For the full copyright and license information, please read the
15 * LICENSE.txt file that was distributed with this source code.
17 * The TYPO3 project - inspiring people to share!
21 * Abstract class as base for standalone messages (error pages etc.)
23 abstract class AbstractStandaloneMessage
extends AbstractMessage
26 * Path to the HTML template file, relative to PATH_site
30 protected $htmlTemplate;
37 protected $defaultMarkers = array();
40 * Markers in template to be filled
44 protected $markers = array();
49 * @param string $message Message
50 * @param string $title Title
51 * @param int $severity Severity, see class constants of AbstractMessage
53 public function __construct($message = '', $title = '', $severity = AbstractMessage
::ERROR
)
55 if (!empty($message)) {
56 $this->setMessage($message);
58 $this->setTitle(!empty($title) ?
$title : 'Error!');
59 $this->setSeverity($severity);
63 * Sets the markers of the templates, which have to be replaced with the specified contents.
64 * The marker array passed, will be merged with already present markers.
66 * @param array $markers Array containing the markers and values (e.g. ###MARKERNAME### => value)
69 public function setMarkers(array $markers)
71 $this->markers
= array_merge($this->markers
, $markers);
75 * Returns the default markers like title and message, which exist for every standalone message
79 protected function getDefaultMarkers()
82 self
::NOTICE
=> 'notice',
83 self
::INFO
=> 'information',
85 self
::WARNING
=> 'warning',
86 self
::ERROR
=> 'error'
88 $defaultMarkers = array(
89 '###CSS_CLASS###' => $classes[$this->severity
],
90 '###TITLE###' => $this->title
,
91 '###MESSAGE###' => $this->message
,
92 // Avoid calling TYPO3_SITE_URL here to get the base URL as it might be that we output an exception message with
93 // invalid trusted host, which would lead to a nested exception! See: #30377
94 // Instead we calculate the relative path to the document root without involving HTTP request parameters.
95 '###BASEURL###' => substr(PATH_site
, strlen(GeneralUtility
::getIndpEnv('TYPO3_DOCUMENT_ROOT'))),
96 '###TYPO3_mainDir###' => TYPO3_mainDir
,
97 '###TYPO3_copyright_year###' => TYPO3_copyright_year
99 return $defaultMarkers;
103 * Gets the filename of the HTML template.
105 * @return string The filename of the HTML template.
107 public function getHtmlTemplate()
109 if (!$this->htmlTemplate
) {
110 throw new \
RuntimeException('No HTML template file has been defined, yet', 1314390127);
112 return $this->htmlTemplate
;
116 * Sets the filename to the HTML template
118 * @param string $htmlTemplate The filename of the HTML template, relative to PATH_site
121 public function setHtmlTemplate($htmlTemplate)
123 $this->htmlTemplate
= PATH_site
. $htmlTemplate;
124 if (!file_exists($this->htmlTemplate
)) {
125 throw new \
RuntimeException('Template file "' . $this->htmlTemplate
. '" not found', 1312830504);
130 * Renders the message.
132 * @return string The message as HTML.
134 public function render()
136 $markers = array_merge($this->getDefaultMarkers(), $this->markers
);
137 $content = file_get_contents($this->htmlTemplate
);
138 $templateService = GeneralUtility
::makeInstance(MarkerBasedTemplateService
::class);
139 $content = $templateService->substituteMarkerArray($content, $markers, '', false, true);
144 * Renders the message and echoes it.
148 public function output()
150 $content = $this->render();