[FEATURE] Add HTTPS security check to reports module
[Packages/TYPO3.CMS.git] / typo3 / sysext / reports / Classes / Report / Status / Status.php
1 <?php
2 namespace TYPO3\CMS\Reports\Report\Status;
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 use Psr\Http\Message\ServerRequestInterface;
18 use TYPO3\CMS\Core\Localization\LanguageService;
19 use TYPO3\CMS\Core\Registry;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Reports\ExtendedStatusProviderInterface;
22 use TYPO3\CMS\Reports\RequestAwareReportInterface;
23 use TYPO3\CMS\Reports\RequestAwareStatusProviderInterface;
24 use TYPO3\CMS\Reports\Status as ReportStatus;
25 use TYPO3\CMS\Reports\StatusProviderInterface;
26
27 /**
28 * The status report
29 */
30 class Status implements RequestAwareReportInterface
31 {
32 /**
33 * @var StatusProviderInterface[][]
34 */
35 protected $statusProviders = [];
36
37 /**
38 * Constructor for class tx_reports_report_Status
39 */
40 public function __construct()
41 {
42 $this->getLanguageService()->includeLLFile('EXT:reports/Resources/Private/Language/locallang_reports.xlf');
43 }
44
45 /**
46 * Takes care of creating / rendering the status report
47 *
48 * @param ServerRequestInterface|null $request the currently handled request
49 * @return string The status report as HTML
50 */
51 public function getReport(ServerRequestInterface $request = null)
52 {
53 $this->getStatusProviders();
54
55 $content = '';
56 $status = $this->getSystemStatus($request);
57 $highestSeverity = $this->getHighestSeverity($status);
58 // Updating the registry
59 $registry = GeneralUtility::makeInstance(Registry::class);
60 $registry->set('tx_reports', 'status.highestSeverity', $highestSeverity);
61 $content .= '<p class="lead">' . $this->getLanguageService()->getLL('status_report_explanation') . '</p>';
62 return $content . $this->renderStatus($status);
63 }
64
65 /**
66 * Gets all registered status providers and creates instances of them.
67 */
68 protected function getStatusProviders()
69 {
70 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']['tx_reports']['status']['providers'] as $key => $statusProvidersList) {
71 $this->statusProviders[$key] = [];
72 foreach ($statusProvidersList as $statusProvider) {
73 $statusProviderInstance = GeneralUtility::makeInstance($statusProvider);
74 if ($statusProviderInstance instanceof StatusProviderInterface) {
75 $this->statusProviders[$key][] = $statusProviderInstance;
76 }
77 }
78 }
79 }
80
81 /**
82 * Runs through all status providers and returns all statuses collected.
83 *
84 * @param ServerRequestInterface $request
85 * @return \TYPO3\CMS\Reports\Status[]
86 */
87 public function getSystemStatus(ServerRequestInterface $request = null)
88 {
89 $status = [];
90 foreach ($this->statusProviders as $statusProviderId => $statusProviderList) {
91 $status[$statusProviderId] = [];
92 foreach ($statusProviderList as $statusProvider) {
93 if ($statusProvider instanceof RequestAwareStatusProviderInterface) {
94 $statuses = $statusProvider->getStatus($request);
95 } else {
96 $statuses = $statusProvider->getStatus();
97 }
98 $status[$statusProviderId] = array_merge($status[$statusProviderId], $statuses);
99 }
100 }
101 return $status;
102 }
103
104 /**
105 * Runs through all status providers and returns all statuses collected, which are detailed.
106 *
107 * @return \TYPO3\CMS\Reports\Status[]
108 */
109 public function getDetailedSystemStatus()
110 {
111 $status = [];
112 foreach ($this->statusProviders as $statusProviderId => $statusProviderList) {
113 $status[$statusProviderId] = [];
114 foreach ($statusProviderList as $statusProvider) {
115 if ($statusProvider instanceof ExtendedStatusProviderInterface) {
116 $statuses = $statusProvider->getDetailedStatus();
117 $status[$statusProviderId] = array_merge($status[$statusProviderId], $statuses);
118 }
119 }
120 }
121 return $status;
122 }
123
124 /**
125 * Determines the highest severity from the given statuses.
126 *
127 * @param array $statusCollection An array of \TYPO3\CMS\Reports\Status objects.
128 * @return int The highest severity found from the statuses.
129 */
130 public function getHighestSeverity(array $statusCollection)
131 {
132 $highestSeverity = ReportStatus::NOTICE;
133 foreach ($statusCollection as $statusProvider => $providerStatuses) {
134 /** @var ReportStatus $status */
135 foreach ($providerStatuses as $status) {
136 if ($status->getSeverity() > $highestSeverity) {
137 $highestSeverity = $status->getSeverity();
138 }
139 // Reached the highest severity level, no need to go on
140 if ($highestSeverity == ReportStatus::ERROR) {
141 break;
142 }
143 }
144 }
145 return $highestSeverity;
146 }
147
148 /**
149 * Renders the system's status
150 *
151 * @param array $statusCollection An array of statuses as returned by the available status providers
152 * @return string The system status as an HTML table
153 */
154 protected function renderStatus(array $statusCollection)
155 {
156 $content = '';
157 $template = '
158 <tr>
159 <td class="###CLASS### col-xs-6">###HEADER###</td>
160 <td class="###CLASS### col-xs-6">###STATUS###<br>###CONTENT###</td>
161 </tr>
162 ';
163 $statuses = $this->sortStatusProviders($statusCollection);
164 $id = 0;
165 foreach ($statuses as $provider => $providerStatus) {
166 $providerState = $this->sortStatuses($providerStatus);
167 $id++;
168 $classes = [
169 ReportStatus::NOTICE => 'notice',
170 ReportStatus::INFO => 'info',
171 ReportStatus::OK => 'success',
172 ReportStatus::WARNING => 'warning',
173 ReportStatus::ERROR => 'danger'
174 ];
175 $messages = '';
176 /** @var ReportStatus $status */
177 foreach ($providerState as $status) {
178 $severity = $status->getSeverity();
179 $messages .= strtr($template, [
180 '###CLASS###' => $classes[$severity],
181 '###HEADER###' => $status->getTitle(),
182 '###STATUS###' => $status->getValue(),
183 '###CONTENT###' => $status->getMessage()
184 ]);
185 }
186 $header = '<h2>' . $provider . '</h2>';
187 $table = '<table class="table table-striped table-hover">';
188 $table .= '<tbody>' . $messages . '</tbody>';
189 $table .= '</table>';
190
191 $content .= $header . $table;
192 }
193 return $content;
194 }
195
196 /**
197 * Sorts the status providers (alphabetically and puts primary status providers at the beginning)
198 *
199 * @param array $statusCollection A collection of statuses (with providers)
200 * @return array The collection of statuses sorted by provider (beginning with provider "_install")
201 */
202 protected function sortStatusProviders(array $statusCollection)
203 {
204 // Extract the primary status collections, i.e. the status groups
205 // that must appear on top of the status report
206 // Change their keys to localized collection titles
207 $primaryStatuses = [
208 $this->getLanguageService()->getLL('status_typo3') => $statusCollection['typo3'],
209 $this->getLanguageService()->getLL('status_system') => $statusCollection['system'],
210 $this->getLanguageService()->getLL('status_security') => $statusCollection['security'],
211 $this->getLanguageService()->getLL('status_configuration') => $statusCollection['configuration']
212 ];
213 unset($statusCollection['typo3'], $statusCollection['system'], $statusCollection['security'], $statusCollection['configuration']);
214 // Assemble list of secondary status collections with left-over collections
215 // Change their keys using localized labels if available
216 // @todo extract into getLabel() method
217 $secondaryStatuses = [];
218 foreach ($statusCollection as $statusProviderId => $collection) {
219 $label = '';
220 if (strpos($statusProviderId, 'LLL:') === 0) {
221 // Label provided by extension
222 $label = $this->getLanguageService()->sL($statusProviderId);
223 } else {
224 // Generic label
225 $label = $this->getLanguageService()->getLL('status_' . $statusProviderId);
226 }
227 $providerLabel = empty($label) ? $statusProviderId : $label;
228 $secondaryStatuses[$providerLabel] = $collection;
229 }
230 // Sort the secondary status collections alphabetically
231 ksort($secondaryStatuses);
232 $orderedStatusCollection = array_merge($primaryStatuses, $secondaryStatuses);
233 return $orderedStatusCollection;
234 }
235
236 /**
237 * Sorts the statuses by severity
238 *
239 * @param array $statusCollection A collection of statuses per provider
240 * @return array The collection of statuses sorted by severity
241 */
242 protected function sortStatuses(array $statusCollection)
243 {
244 $statuses = [];
245 $sortTitle = [];
246 $header = null;
247 /** @var ReportStatus $status */
248 foreach ($statusCollection as $status) {
249 if ($status->getTitle() === 'TYPO3') {
250 $header = $status;
251 continue;
252 }
253 $statuses[] = $status;
254 $sortTitle[] = $status->getSeverity();
255 }
256 array_multisort($sortTitle, SORT_DESC, $statuses);
257 // Making sure that the core version information is always on the top
258 if (is_object($header)) {
259 array_unshift($statuses, $header);
260 }
261 return $statuses;
262 }
263
264 /**
265 * @return LanguageService
266 */
267 protected function getLanguageService()
268 {
269 return $GLOBALS['LANG'];
270 }
271 }