TYPO3 CMS  TYPO3_7-6
AbstractStandaloneMessage.php
Go to the documentation of this file.
1 <?php
3 
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 
24 {
30  protected $htmlTemplate;
31 
37  protected $defaultMarkers = [];
38 
44  protected $markers = [];
45 
54  {
55  if (!empty($message)) {
56  $this->setMessage($message);
57  }
58  $this->setTitle(!empty($title) ? $title : 'Error!');
59  $this->setSeverity($severity);
60  }
61 
69  public function setMarkers(array $markers)
70  {
71  $this->markers = array_merge($this->markers, $markers);
72  }
73 
79  protected function getDefaultMarkers()
80  {
81  $classes = [
82  self::NOTICE => 'notice',
83  self::INFO => 'information',
84  self::OK => 'ok',
85  self::WARNING => 'warning',
86  self::ERROR => 'error'
87  ];
88  $defaultMarkers = [
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 
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 
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 
134  public function render()
135  {
136  $markers = array_merge($this->getDefaultMarkers(), $this->markers);
137  $content = GeneralUtility::getUrl($this->htmlTemplate);
138  $templateService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
139  $content = $templateService->substituteMarkerArray($content, $markers, '', false, true);
140  return $content;
141  }
142 
148  public function output()
149  {
150  $content = $this->render();
151  echo $content;
152  }
153 }
__construct($message='', $title='', $severity=AbstractMessage::ERROR)
static getUrl($url, $includeHeader=0, $requestHeaders=false, &$report=null)