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