[FEATURE] Render Error Pages via Fluid
[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 * This class is deprecated since TYPO3 v8 in favor of the ErrorPageController, and will be removed in TYPO3 v9
23 */
24 abstract class AbstractStandaloneMessage extends AbstractMessage
25 {
26 /**
27 * Path to the HTML template file, relative to PATH_site
28 *
29 * @var string
30 */
31 protected $htmlTemplate;
32
33 /**
34 * Default markers
35 *
36 * @var array
37 */
38 protected $defaultMarkers = array();
39
40 /**
41 * Markers in template to be filled
42 *
43 * @var array
44 */
45 protected $markers = array();
46
47 /**
48 * Constructor
49 *
50 * @param string $message Message
51 * @param string $title Title
52 * @param int $severity Severity, see class constants of AbstractMessage
53 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9, use the ErrorPageController instead
54 */
55 public function __construct($message = '', $title = '', $severity = AbstractMessage::ERROR)
56 {
57 GeneralUtility::logDeprecatedFunction();
58 if (!empty($message)) {
59 $this->setMessage($message);
60 }
61 $this->setTitle(!empty($title) ? $title : 'Error!');
62 $this->setSeverity($severity);
63 }
64
65 /**
66 * Sets the markers of the templates, which have to be replaced with the specified contents.
67 * The marker array passed, will be merged with already present markers.
68 *
69 * @param array $markers Array containing the markers and values (e.g. ###MARKERNAME### => value)
70 * @return void
71 */
72 public function setMarkers(array $markers)
73 {
74 $this->markers = array_merge($this->markers, $markers);
75 }
76
77 /**
78 * Returns the default markers like title and message, which exist for every standalone message
79 *
80 * @return array
81 */
82 protected function getDefaultMarkers()
83 {
84 $classes = array(
85 self::NOTICE => 'notice',
86 self::INFO => 'information',
87 self::OK => 'ok',
88 self::WARNING => 'warning',
89 self::ERROR => 'error'
90 );
91 $defaultMarkers = array(
92 '###CSS_CLASS###' => $classes[$this->severity],
93 '###TITLE###' => $this->title,
94 '###MESSAGE###' => $this->message,
95 // Avoid calling TYPO3_SITE_URL here to get the base URL as it might be that we output an exception message with
96 // invalid trusted host, which would lead to a nested exception! See: #30377
97 // Instead we calculate the relative path to the document root without involving HTTP request parameters.
98 '###BASEURL###' => substr(PATH_site, strlen(GeneralUtility::getIndpEnv('TYPO3_DOCUMENT_ROOT'))),
99 '###TYPO3_mainDir###' => TYPO3_mainDir,
100 '###TYPO3_copyright_year###' => TYPO3_copyright_year
101 );
102 return $defaultMarkers;
103 }
104
105 /**
106 * Gets the filename of the HTML template.
107 *
108 * @return string The filename of the HTML template.
109 */
110 public function getHtmlTemplate()
111 {
112 if (!$this->htmlTemplate) {
113 throw new \RuntimeException('No HTML template file has been defined, yet', 1314390127);
114 }
115 return $this->htmlTemplate;
116 }
117
118 /**
119 * Sets the filename to the HTML template
120 *
121 * @param string $htmlTemplate The filename of the HTML template, relative to PATH_site
122 * @return void
123 */
124 public function setHtmlTemplate($htmlTemplate)
125 {
126 $this->htmlTemplate = PATH_site . $htmlTemplate;
127 if (!file_exists($this->htmlTemplate)) {
128 throw new \RuntimeException('Template file "' . $this->htmlTemplate . '" not found', 1312830504);
129 }
130 }
131
132 /**
133 * Renders the message.
134 *
135 * @return string The message as HTML.
136 */
137 public function render()
138 {
139 $markers = array_merge($this->getDefaultMarkers(), $this->markers);
140 $content = file_get_contents($this->htmlTemplate);
141 $templateService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
142 $content = $templateService->substituteMarkerArray($content, $markers, '', false, true);
143 return $content;
144 }
145
146 /**
147 * Renders the message and echoes it.
148 *
149 * @return void
150 */
151 public function output()
152 {
153 $content = $this->render();
154 echo $content;
155 }
156 }