[TASK] Replaced icon with IconFactory in SystemInformationToolbarItem
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Backend / ToolbarItems / SystemInformationToolbarItem.php
1 <?php
2 namespace TYPO3\CMS\Backend\Backend\ToolbarItems;
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 TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
18 use TYPO3\CMS\Backend\Toolbar\Enumeration\InformationStatus;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
20 use TYPO3\CMS\Core\Imaging\Icon;
21 use TYPO3\CMS\Core\Imaging\IconFactory;
22 use TYPO3\CMS\Core\Http\AjaxRequestHandler;
23 use \TYPO3\CMS\Core\Page\PageRenderer;
24 use TYPO3\CMS\Core\Utility\CommandUtility;
25 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27 use TYPO3\CMS\Core\Utility\StringUtility;
28 use TYPO3\CMS\Fluid\View\StandaloneView;
29
30 /**
31 * Render system info toolbar item
32 */
33 class SystemInformationToolbarItem implements ToolbarItemInterface {
34
35 /**
36 * @var StandaloneView
37 */
38 protected $standaloneView = NULL;
39
40 /**
41 * Template file for the dropdown menu
42 */
43 const TOOLBAR_MENU_TEMPLATE = 'SystemInformation.html';
44
45 /**
46 * Number displayed as badge on the dropdown trigger
47 *
48 * @var int
49 */
50 protected $totalCount = 0;
51
52 /**
53 * Holds the highest severity
54 *
55 * @var string
56 */
57 protected $highestSeverity = '';
58
59 /**
60 * The CSS class for the badge
61 *
62 * @var string
63 */
64 protected $severityBadgeClass = '';
65
66 /**
67 * @var array
68 */
69 protected $systemInformation = array();
70
71 /**
72 * @var array
73 */
74 protected $systemMessages = array();
75
76 /**
77 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
78 */
79 protected $signalSlotDispatcher = NULL;
80
81 /**
82 * Constructor
83 */
84 public function __construct() {
85 if (!$this->checkAccess()) {
86 return;
87 }
88
89 $extPath = ExtensionManagementUtility::extPath('backend');
90 /* @var $view StandaloneView */
91 $this->standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
92 $this->standaloneView->setTemplatePathAndFilename($extPath . 'Resources/Private/Templates/ToolbarMenu/' . static::TOOLBAR_MENU_TEMPLATE);
93
94 $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Toolbar/SystemInformationMenu');
95 }
96
97 /**
98 * Collect the information for the menu
99 */
100 protected function collectInformation() {
101 $this->getWebServer();
102 $this->getPhpVersion();
103 $this->getDatabase();
104 $this->getApplicationContext();
105 $this->getGitRevision();
106 $this->getOperatingSystem();
107
108 $this->emitGetSystemInformation();
109 $this->emitLoadMessages();
110
111 $this->severityBadgeClass = $this->highestSeverity !== InformationStatus::STATUS_NOTICE ? 'badge-' . $this->highestSeverity : '';
112 }
113
114 /**
115 * Renders the menu for AJAX calls
116 *
117 * @param array $params
118 * @param AjaxRequestHandler $ajaxObj
119 */
120 public function renderAjax($params = array(), $ajaxObj) {
121 $this->collectInformation();
122 $ajaxObj->addContent('systemInformationMenu', $this->getDropDown());
123 }
124
125 /**
126 * Gets the PHP version
127 *
128 * @return void
129 */
130 protected function getPhpVersion() {
131 $this->systemInformation[] = array(
132 'title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.sysinfo.phpversion', TRUE),
133 'value' => PHP_VERSION,
134 'icon' => '<span class="fa fa-code"></span>'
135 );
136 }
137
138 /**
139 * Get the database info
140 *
141 * @return void
142 */
143 protected function getDatabase() {
144 $this->systemInformation[] = array(
145 'title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.sysinfo.database', TRUE),
146 'value' => $this->getDatabaseConnection()->getServerVersion(),
147 'icon' => '<span class="fa fa-database"></span>'
148 );
149 }
150
151 /**
152 * Gets the application context
153 *
154 * @return void
155 */
156 protected function getApplicationContext() {
157 $applicationContext = GeneralUtility::getApplicationContext();
158 $this->systemInformation[] = array(
159 'title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.sysinfo.applicationcontext', TRUE),
160 'value' => (string)$applicationContext,
161 'status' => $applicationContext->isProduction() ? InformationStatus::STATUS_OK : InformationStatus::STATUS_WARNING,
162 'icon' => '<span class="fa fa-tasks"></span>'
163 );
164 }
165
166 /**
167 * Gets the current GIT revision and branch
168 *
169 * @return void
170 */
171 protected function getGitRevision() {
172 if (!StringUtility::endsWith(TYPO3_version, '-dev') || \TYPO3\CMS\Core\Core\SystemEnvironmentBuilder::isFunctionDisabled('exec')) {
173 return;
174 }
175 // check if git exists
176 CommandUtility::exec('git --version', $_, $returnCode);
177 if ((int)$returnCode !== 0) {
178 // git is not available
179 return;
180 }
181
182 $revision = trim(CommandUtility::exec('git rev-parse --short HEAD'));
183 $branch = trim(CommandUtility::exec('git rev-parse --abbrev-ref HEAD'));
184 if (!empty($revision) && !empty($branch)) {
185 $this->systemInformation[] = array(
186 'title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.sysinfo.gitrevision', TRUE),
187 'value' => sprintf('%s [%s]', $revision, $branch),
188 'icon' => '<span class="fa fa-git"></span>'
189 );
190 }
191 }
192
193 /**
194 * Gets the system kernel and version
195 *
196 * @return void
197 */
198 protected function getOperatingSystem() {
199 $kernelName = php_uname('s');
200 switch (strtolower($kernelName)) {
201 case 'linux':
202 $icon = 'linux';
203 break;
204 case 'darwin':
205 $icon = 'apple';
206 break;
207 default:
208 $icon = 'windows';
209 }
210 $this->systemInformation[] = array(
211 'title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.sysinfo.operatingsystem', TRUE),
212 'value' => $kernelName . ' ' . php_uname('r'),
213 'icon' => '<span class="fa fa-' . htmlspecialchars($icon) . '"></span>'
214 );
215 }
216
217 /**
218 * Gets the webserver software
219 */
220 protected function getWebServer() {
221 $this->systemInformation[] = array(
222 'title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.sysinfo.webserver', TRUE),
223 'value' => htmlspecialchars($_SERVER['SERVER_SOFTWARE']),
224 'icon' => '<span class="fa fa-server"></span>'
225 );
226 }
227
228 /**
229 * Emits the "getSystemInformation" signal
230 *
231 * @return void
232 */
233 protected function emitGetSystemInformation() {
234 list($systemInformation) = $this->getSignalSlotDispatcher()->dispatch(__CLASS__, 'getSystemInformation', array(array()));
235 if (!empty($systemInformation)) {
236 $this->systemInformation[] = $systemInformation;
237 }
238 }
239
240 /**
241 * Emits the "loadMessages" signal
242 *
243 * @return void
244 */
245 protected function emitLoadMessages() {
246 list($message) = $this->getSignalSlotDispatcher()->dispatch(__CLASS__, 'loadMessages', array(array()));
247 if (empty($message)) {
248 return;
249 }
250
251 // increase counter
252 if (isset($message['count'])) {
253 $this->totalCount += (int)$message['count'];
254 }
255
256 // define the severity for the badge
257 if (InformationStatus::mapStatusToInt($message['status']) > InformationStatus::mapStatusToInt($this->highestSeverity)) {
258 $this->highestSeverity = $message['status'];
259 }
260
261 $this->systemMessages[] = $message;
262 }
263
264 /**
265 * Checks whether the user has access to this toolbar item
266 *
267 * @return bool TRUE if user has access, FALSE if not
268 */
269 public function checkAccess() {
270 return $this->getBackendUserAuthentication()->isAdmin();
271 }
272
273 /**
274 * Render system information dropdown
275 *
276 * @return string Icon HTML
277 */
278 public function getItem() {
279 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
280 $title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.sysinfo', TRUE);
281 $icon = $iconFactory->getIcon('actions-system-list-open', Icon::SIZE_SMALL);
282 return '<span title="' . $title . '">' . $icon . '<span id="t3js-systeminformation-counter" class="badge"></span></span>';
283 }
284
285 /**
286 * Render drop down
287 *
288 * @return string Drop down HTML
289 */
290 public function getDropDown() {
291 if (!$this->checkAccess()) {
292 return '';
293 }
294
295 $request = $this->standaloneView->getRequest();
296 $request->setControllerExtensionName('backend');
297 $this->standaloneView->assignMultiple(array(
298 'installToolUrl' => BackendUtility::getModuleUrl('system_InstallInstall'),
299 'messages' => $this->systemMessages,
300 'count' => $this->totalCount,
301 'severityBadgeClass' => $this->severityBadgeClass,
302 'systemInformation' => $this->systemInformation
303 ));
304 return $this->standaloneView->render();
305 }
306
307 /**
308 * No additional attributes needed.
309 *
310 * @return array
311 */
312 public function getAdditionalAttributes() {
313 return array();
314 }
315
316 /**
317 * This item has a drop down
318 *
319 * @return bool
320 */
321 public function hasDropDown() {
322 return TRUE;
323 }
324
325 /**
326 * Position relative to others
327 *
328 * @return int
329 */
330 public function getIndex() {
331 return 75;
332 }
333
334 /**
335 * Returns the current BE user.
336 *
337 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
338 */
339 protected function getBackendUserAuthentication() {
340 return $GLOBALS['BE_USER'];
341 }
342
343 /**
344 * Returns DatabaseConnection
345 *
346 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
347 */
348 protected function getDatabaseConnection() {
349 return $GLOBALS['TYPO3_DB'];
350 }
351
352 /**
353 * Returns current PageRenderer
354 *
355 * @return PageRenderer
356 */
357 protected function getPageRenderer() {
358 return GeneralUtility::makeInstance(PageRenderer::class);
359 }
360
361 /**
362 * Returns LanguageService
363 *
364 * @return \TYPO3\CMS\Lang\LanguageService
365 */
366 protected function getLanguageService() {
367 return $GLOBALS['LANG'];
368 }
369
370 /**
371 * Get the SignalSlot dispatcher
372 *
373 * @return \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
374 */
375 protected function getSignalSlotDispatcher() {
376 if (!isset($this->signalSlotDispatcher)) {
377 $this->signalSlotDispatcher = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class)
378 ->get(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
379 }
380 return $this->signalSlotDispatcher;
381 }
382 }