[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Messaging / AbstractStandaloneMessage.php
1 <?php
2 namespace TYPO3\CMS\Core\Messaging;
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 * Abstract class as base for standalone messages (error pages etc.)
18 *
19 * @author Steffen Gebert <steffen.gebert@typo3.org>
20 */
21 abstract class AbstractStandaloneMessage extends \TYPO3\CMS\Core\Messaging\AbstractMessage {
22
23 /**
24 * Path to the HTML template file, relative to PATH_site
25 *
26 * @var string
27 */
28 protected $htmlTemplate;
29
30 /**
31 * Default markers
32 *
33 * @var array
34 */
35 protected $defaultMarkers = array();
36
37 /**
38 * Markers in template to be filled
39 *
40 * @var array
41 */
42 protected $markers = array();
43
44 /**
45 * Constructor
46 *
47 * @param string $message Message
48 * @param string $title Title
49 * @param integer $severity Severity, see class constants of AbstractMessage
50 */
51 public function __construct($message = '', $title = '', $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR) {
52 if (!empty($message)) {
53 $this->setMessage($message);
54 }
55 $this->setTitle(!empty($title) ? $title : 'Error!');
56 $this->setSeverity($severity);
57 }
58
59 /**
60 * Sets the markers of the templates, which have to be replaced with the specified contents.
61 * The marker array passed, will be merged with already present markers.
62 *
63 * @param array $markers Array containing the markers and values (e.g. ###MARKERNAME### => value)
64 * @return void
65 */
66 public function setMarkers(array $markers) {
67 $this->markers = array_merge($this->markers, $markers);
68 }
69
70 /**
71 * Returns the default markers like title and message, which exist for every standalone message
72 *
73 * @return array
74 */
75 protected function getDefaultMarkers() {
76 $classes = array(
77 self::NOTICE => 'notice',
78 self::INFO => 'information',
79 self::OK => 'ok',
80 self::WARNING => 'warning',
81 self::ERROR => 'error'
82 );
83 $defaultMarkers = array(
84 '###CSS_CLASS###' => $classes[$this->severity],
85 '###TITLE###' => $this->title,
86 '###MESSAGE###' => $this->message,
87 // Avoid calling TYPO3_SITE_URL here to get the base URL as it might be that we output an exception message with
88 // invalid trusted host, which would lead to a nested exception! See: #30377
89 // Instead we calculate the relative path to the document root without involving HTTP request parameters.
90 '###BASEURL###' => substr(PATH_site, strlen(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_DOCUMENT_ROOT'))),
91 '###TYPO3_mainDir###' => TYPO3_mainDir,
92 '###TYPO3_copyright_year###' => TYPO3_copyright_year
93 );
94 return $defaultMarkers;
95 }
96
97 /**
98 * Gets the filename of the HTML template.
99 *
100 * @return string The filename of the HTML template.
101 */
102 public function getHtmlTemplate() {
103 if (!$this->htmlTemplate) {
104 throw new \RuntimeException('No HTML template file has been defined, yet', 1314390127);
105 }
106 return $this->htmlTemplate;
107 }
108
109 /**
110 * Sets the filename to the HTML template
111 *
112 * @param string $htmlTemplate The filename of the HTML template, relative to PATH_site
113 * @return void
114 */
115 public function setHtmlTemplate($htmlTemplate) {
116 $this->htmlTemplate = PATH_site . $htmlTemplate;
117 if (!file_exists($this->htmlTemplate)) {
118 throw new \RuntimeException('Template file "' . $this->htmlTemplate . '" not found', 1312830504);
119 }
120 }
121
122 /**
123 * Renders the message.
124 *
125 * @return string The message as HTML.
126 */
127 public function render() {
128 $markers = array_merge($this->getDefaultMarkers(), $this->markers);
129 $content = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($this->htmlTemplate);
130 $content = \TYPO3\CMS\Core\Html\HtmlParser::substituteMarkerArray($content, $markers, '', FALSE, TRUE);
131 return $content;
132 }
133
134 /**
135 * Renders the message and echoes it.
136 *
137 * @return void
138 */
139 public function output() {
140 $content = $this->render();
141 echo $content;
142 }
143
144 }