SystemInformationToolbarItem.php 9.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
namespace TYPO3\CMS\Backend\Backend\ToolbarItems;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
use TYPO3\CMS\Backend\Toolbar\Enumeration\InformationStatus;
use TYPO3\CMS\Backend\Utility\BackendUtility;
20
21
use TYPO3\CMS\Core\Imaging\Icon;
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
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
28
use TYPO3\CMS\Fluid\View\StandaloneView;
29
30
31
32

/**
 * Render system info toolbar item
 */
33
34
35
36
37
38
class SystemInformationToolbarItem implements ToolbarItemInterface {

	/**
	 * @var StandaloneView
	 */
	protected $standaloneView = NULL;
39
40

	/**
41
	 * Template file for the dropdown menu
42
	 */
43
	const TOOLBAR_MENU_TEMPLATE = 'SystemInformation.html';
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

	/**
	 * Number displayed as badge on the dropdown trigger
	 *
	 * @var int
	 */
	protected $totalCount = 0;

	/**
	 * Holds the highest severity
	 *
	 * @var string
	 */
	protected $highestSeverity = '';

59
60
61
62
63
64
65
	/**
	 * The CSS class for the badge
	 *
	 * @var string
	 */
	protected $severityBadgeClass = '';

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
	/**
	 * @var array
	 */
	protected $systemInformation = array();

	/**
	 * @var array
	 */
	protected $systemMessages = array();

	/**
	 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
	 */
	protected $signalSlotDispatcher = NULL;

	/**
	 * Constructor
	 */
	public function __construct() {
		if (!$this->checkAccess()) {
			return;
		}

89
90
91
92
		$extPath = ExtensionManagementUtility::extPath('backend');
		/* @var $view StandaloneView */
		$this->standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
		$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
99
100
	/**
	 * Collect the information for the menu
	 */
	protected function collectInformation() {
101
		$this->getWebServer();
102
103
104
105
106
107
108
109
		$this->getPhpVersion();
		$this->getDatabase();
		$this->getApplicationContext();
		$this->getGitRevision();
		$this->getOperatingSystem();

		$this->emitGetSystemInformation();
		$this->emitLoadMessages();
110
111
112
113
114
115
116
117
118
119
120
121
122

		$this->severityBadgeClass = $this->highestSeverity !== InformationStatus::STATUS_NOTICE ? 'badge-' . $this->highestSeverity : '';
	}

	/**
	 * Renders the menu for AJAX calls
	 *
	 * @param array $params
	 * @param AjaxRequestHandler $ajaxObj
	 */
	public function renderAjax($params = array(), $ajaxObj) {
		$this->collectInformation();
		$ajaxObj->addContent('systemInformationMenu', $this->getDropDown());
123
124
125
126
	}

	/**
	 * Gets the PHP version
127
128
	 *
	 * @return void
129
130
131
132
133
134
135
136
137
138
139
	 */
	protected function getPhpVersion() {
		$this->systemInformation[] = array(
			'title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.sysinfo.phpversion', TRUE),
			'value' => PHP_VERSION,
			'icon' => '<span class="fa fa-code"></span>'
		);
	}

	/**
	 * Get the database info
140
141
	 *
	 * @return void
142
143
144
145
146
147
148
149
150
151
152
	 */
	protected function getDatabase() {
		$this->systemInformation[] = array(
			'title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.sysinfo.database', TRUE),
			'value' => $this->getDatabaseConnection()->getServerVersion(),
			'icon' => '<span class="fa fa-database"></span>'
		);
	}

	/**
	 * Gets the application context
153
154
	 *
	 * @return void
155
156
157
158
159
160
	 */
	protected function getApplicationContext() {
		$applicationContext = GeneralUtility::getApplicationContext();
		$this->systemInformation[] = array(
			'title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.sysinfo.applicationcontext', TRUE),
			'value' => (string)$applicationContext,
161
			'status' => $applicationContext->isProduction() ? InformationStatus::STATUS_OK : InformationStatus::STATUS_WARNING,
162
163
164
165
166
167
			'icon' => '<span class="fa fa-tasks"></span>'
		);
	}

	/**
	 * Gets the current GIT revision and branch
168
169
	 *
	 * @return void
170
171
172
	 */
	protected function getGitRevision() {
		if (!StringUtility::endsWith(TYPO3_version, '-dev') || \TYPO3\CMS\Core\Core\SystemEnvironmentBuilder::isFunctionDisabled('exec')) {
173
			return;
174
175
176
177
178
		}
		// check if git exists
		CommandUtility::exec('git --version', $_, $returnCode);
		if ((int)$returnCode !== 0) {
			// git is not available
179
			return;
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
		}

		$revision = trim(CommandUtility::exec('git rev-parse --short HEAD'));
		$branch = trim(CommandUtility::exec('git rev-parse --abbrev-ref HEAD'));
		if (!empty($revision) && !empty($branch)) {
			$this->systemInformation[] = array(
				'title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.sysinfo.gitrevision', TRUE),
				'value' => sprintf('%s [%s]', $revision, $branch),
				'icon' => '<span class="fa fa-git"></span>'
			);
		}
	}

	/**
	 * Gets the system kernel and version
195
196
	 *
	 * @return void
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
	 */
	protected function getOperatingSystem() {
		$kernelName = php_uname('s');
		switch (strtolower($kernelName)) {
			case 'linux':
				$icon = 'linux';
				break;
			case 'darwin':
				$icon = 'apple';
				break;
			default:
				$icon = 'windows';
		}
		$this->systemInformation[] = array(
			'title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.sysinfo.operatingsystem', TRUE),
			'value' => $kernelName . ' ' . php_uname('r'),
			'icon' => '<span class="fa fa-' . htmlspecialchars($icon) . '"></span>'
		);
	}

217
218
219
220
221
222
223
224
225
226
227
	/**
	 * Gets the webserver software
	 */
	protected function getWebServer() {
		$this->systemInformation[] = array(
			'title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.sysinfo.webserver', TRUE),
			'value' => htmlspecialchars($_SERVER['SERVER_SOFTWARE']),
			'icon' => '<span class="fa fa-server"></span>'
		);
	}

228
229
	/**
	 * Emits the "getSystemInformation" signal
230
231
	 *
	 * @return void
232
233
234
235
236
237
238
239
240
241
	 */
	protected function emitGetSystemInformation() {
		list($systemInformation) = $this->getSignalSlotDispatcher()->dispatch(__CLASS__, 'getSystemInformation', array(array()));
		if (!empty($systemInformation)) {
			$this->systemInformation[] = $systemInformation;
		}
	}

	/**
	 * Emits the "loadMessages" signal
242
243
	 *
	 * @return void
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
	 */
	protected function emitLoadMessages() {
		list($message) = $this->getSignalSlotDispatcher()->dispatch(__CLASS__, 'loadMessages', array(array()));
		if (empty($message)) {
			return;
		}

		// increase counter
		if (isset($message['count'])) {
			$this->totalCount += (int)$message['count'];
		}

		// define the severity for the badge
		if (InformationStatus::mapStatusToInt($message['status']) > InformationStatus::mapStatusToInt($this->highestSeverity)) {
			$this->highestSeverity = $message['status'];
		}

		$this->systemMessages[] = $message;
	}

	/**
	 * Checks whether the user has access to this toolbar item
	 *
	 * @return bool TRUE if user has access, FALSE if not
	 */
	public function checkAccess() {
		return $this->getBackendUserAuthentication()->isAdmin();
	}

	/**
	 * Render system information dropdown
	 *
	 * @return string Icon HTML
	 */
	public function getItem() {
279
		$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
280
		$title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:toolbarItems.sysinfo', TRUE);
281
282
		$icon = $iconFactory->getIcon('actions-system-list-open', Icon::SIZE_SMALL);
		return '<span title="' . $title . '">' . $icon . '<span id="t3js-systeminformation-counter" class="badge"></span></span>';
283
284
285
286
287
288
289
290
291
292
293
294
	}

	/**
	 * Render drop down
	 *
	 * @return string Drop down HTML
	 */
	public function getDropDown() {
		if (!$this->checkAccess()) {
			return '';
		}

295
296
297
		$request = $this->standaloneView->getRequest();
		$request->setControllerExtensionName('backend');
		$this->standaloneView->assignMultiple(array(
298
299
			'installToolUrl' => BackendUtility::getModuleUrl('system_InstallInstall'),
			'messages' => $this->systemMessages,
300
301
			'count' => $this->totalCount,
			'severityBadgeClass' => $this->severityBadgeClass,
302
303
			'systemInformation' => $this->systemInformation
		));
304
		return $this->standaloneView->render();
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
	}

	/**
	 * No additional attributes needed.
	 *
	 * @return array
	 */
	public function getAdditionalAttributes() {
		return array();
	}

	/**
	 * This item has a drop down
	 *
	 * @return bool
	 */
	public function hasDropDown() {
		return TRUE;
	}

	/**
	 * Position relative to others
	 *
	 * @return int
	 */
	public function getIndex() {
		return 75;
	}

	/**
	 * Returns the current BE user.
	 *
	 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
	 */
	protected function getBackendUserAuthentication() {
		return $GLOBALS['BE_USER'];
	}

	/**
	 * Returns DatabaseConnection
	 *
	 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
	 */
	protected function getDatabaseConnection() {
		return $GLOBALS['TYPO3_DB'];
	}

	/**
	 * Returns current PageRenderer
	 *
355
	 * @return PageRenderer
356
357
	 */
	protected function getPageRenderer() {
358
		return GeneralUtility::makeInstance(PageRenderer::class);
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
	}

	/**
	 * Returns LanguageService
	 *
	 * @return \TYPO3\CMS\Lang\LanguageService
	 */
	protected function getLanguageService() {
		return $GLOBALS['LANG'];
	}

	/**
	 * Get the SignalSlot dispatcher
	 *
	 * @return \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
	 */
	protected function getSignalSlotDispatcher() {
		if (!isset($this->signalSlotDispatcher)) {
			$this->signalSlotDispatcher = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class)
				->get(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
		}
		return $this->signalSlotDispatcher;
	}
382
}