268aa47b1bcff1e1b7cabc7192171040d0cf74a6
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Messaging / AbstractStandaloneMessage.php
1 <?php
2 namespace TYPO3\CMS\Core\Messaging;
3
4 use TYPO3\CMS\Core\Service\MarkerBasedTemplateService;
5 use TYPO3\CMS\Core\Utility\GeneralUtility;
6
7 /*
8 * This file is part of the TYPO3 CMS project.
9 *
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.
13 *
14 * For the full copyright and license information, please read the
15 * LICENSE.txt file that was distributed with this source code.
16 *
17 * The TYPO3 project - inspiring people to share!
18 */
19
20 /**
21 * Abstract class as base for standalone messages (error pages etc.)
22 */
23 abstract class AbstractStandaloneMessage extends AbstractMessage
24 {
25 /**
26 * Path to the HTML template file, relative to PATH_site
27 *
28 * @var string
29 */
30 protected $htmlTemplate;
31
32 /**
33 * Default markers
34 *
35 * @var array
36 */
37 protected $defaultMarkers = array();
38
39 /**
40 * Markers in template to be filled
41 *
42 * @var array
43 */
44 protected $markers = array();
45
46 /**
47 * Constructor
48 *
49 * @param string $message Message
50 * @param string $title Title
51 * @param int $severity Severity, see class constants of AbstractMessage
52 */
53 public function __construct($message = '', $title = '', $severity = AbstractMessage::ERROR)
54 {
55 if (!empty($message)) {
56 $this->setMessage($message);
57 }
58 $this->setTitle(!empty($title) ? $title : 'Error!');
59 $this->setSeverity($severity);
60 }
61
62 /**
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.
65 *
66 * @param array $markers Array containing the markers and values (e.g. ###MARKERNAME### => value)
67 * @return void
68 */
69 public function setMarkers(array $markers)
70 {
71 $this->markers = array_merge($this->markers, $markers);
72 }
73
74 /**
75 * Returns the default markers like title and message, which exist for every standalone message
76 *
77 * @return array
78 */
79 protected function getDefaultMarkers()
80 {
81 $classes = array(
82 self::NOTICE => 'notice',
83 self::INFO => 'information',
84 self::OK => 'ok',
85 self::WARNING => 'warning',
86 self::ERROR => 'error'
87 );
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
98 );
99 return $defaultMarkers;
100 }
101
102 /**
103 * Gets the filename of the HTML template.
104 *
105 * @return string The filename of the HTML template.
106 */
107 public function getHtmlTemplate()
108 {
109 if (!$this->htmlTemplate) {
110 throw new \RuntimeException('No HTML template file has been defined, yet', 1314390127);
111 }
112 return $this->htmlTemplate;
113 }
114
115 /**
116 * Sets the filename to the HTML template
117 *
118 * @param string $htmlTemplate The filename of the HTML template, relative to PATH_site
119 * @return void
120 */
121 public function setHtmlTemplate($htmlTemplate)
122 {
123 $this->htmlTemplate = PATH_site . $htmlTemplate;
124 if (!file_exists($this->htmlTemplate)) {
125 throw new \RuntimeException('Template file "' . $this->htmlTemplate . '" not found', 1312830504);
126 }
127 }
128
129 /**
130 * Renders the message.
131 *
132 * @return string The message as HTML.
133 */
134 public function render()
135 {
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);
140 return $content;
141 }
142
143 /**
144 * Renders the message and echoes it.
145 *
146 * @return void
147 */
148 public function output()
149 {
150 $content = $this->render();
151 echo $content;
152 }
153 }