TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
SystemInformationToolbarItem.php
Go to the documentation of this file.
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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
33 
38 {
42  protected $standaloneView = null;
43 
47  const TOOLBAR_MENU_TEMPLATE = 'SystemInformation.html';
48 
54  protected $totalCount = 0;
55 
61  protected $highestSeverity;
62 
68  protected $severityBadgeClass = '';
69 
73  protected $systemInformation = [];
74 
78  protected $systemMessages = [];
79 
83  protected $signalSlotDispatcher = null;
84 
88  protected $iconFactory;
89 
93  protected $maximumCountInBadge = 99;
94 
98  public function __construct()
99  {
100  if (!$this->checkAccess()) {
101  return;
102  }
103  $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
104 
105  $extPath = ExtensionManagementUtility::extPath('backend');
106  /* @var $view StandaloneView */
107  $this->standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
108  $this->standaloneView->setTemplatePathAndFilename($extPath . 'Resources/Private/Templates/ToolbarMenu/' . static::TOOLBAR_MENU_TEMPLATE);
109 
110  $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Toolbar/SystemInformationMenu');
111 
113  }
114 
118  protected function collectInformation()
119  {
120  $this->getTypo3Version();
121  $this->getWebServer();
122  $this->getPhpVersion();
123  $this->getDatabase();
124  $this->getApplicationContext();
125  $this->getComposerMode();
126  $this->getGitRevision();
127  $this->getOperatingSystem();
128 
129  $this->emitGetSystemInformation();
130  $this->emitLoadMessages();
131 
132  $this->severityBadgeClass = !$this->highestSeverity->equals(InformationStatus::STATUS_NOTICE) ? 'badge-' . (string)$this->highestSeverity : '';
133  }
134 
142  public function renderMenuAction(ServerRequestInterface $request, ResponseInterface $response)
143  {
144  $this->collectInformation();
145 
146  $response->getBody()->write($this->getDropDown());
147  $response = $response->withHeader('Content-Type', 'text/html; charset=utf-8');
148  return $response;
149  }
150 
156  protected function getPhpVersion()
157  {
158  $this->systemInformation[] = [
159  'title' => htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.phpversion')),
160  'value' => PHP_VERSION,
161  'icon' => $this->iconFactory->getIcon('sysinfo-php-version', Icon::SIZE_SMALL)->render()
162  ];
163  }
164 
170  protected function getDatabase()
171  {
172  foreach (GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionNames() as $connectionName) {
173  $this->systemInformation[] = [
174  'title' => $this->getLanguageService()
175  ->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.database')
176  . ' (' . htmlspecialchars($connectionName) . ')',
177  'value' => GeneralUtility::makeInstance(ConnectionPool::class)
178  ->getConnectionByName($connectionName)
179  ->getServerVersion(),
180  'icon' => $this->iconFactory->getIcon('sysinfo-database', Icon::SIZE_SMALL)->render()
181  ];
182  }
183  }
184 
190  protected function getApplicationContext()
191  {
192  $applicationContext = GeneralUtility::getApplicationContext();
193  $this->systemInformation[] = [
194  'title' => htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.applicationcontext')),
195  'value' => (string)$applicationContext,
196  'status' => $applicationContext->isProduction() ? InformationStatus::STATUS_OK : InformationStatus::STATUS_WARNING,
197  'icon' => $this->iconFactory->getIcon('sysinfo-application-context', Icon::SIZE_SMALL)->render()
198  ];
199  }
200 
204  protected function getComposerMode()
205  {
207  return;
208  }
209 
210  $languageService = $this->getLanguageService();
211  $this->systemInformation[] = [
212  'title' => htmlspecialchars($languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.composerMode')),
213  'value' => htmlspecialchars($languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.enabled')),
214  'icon' => $this->iconFactory->getIcon('sysinfo-composer-mode', Icon::SIZE_SMALL)->render()
215  ];
216  }
217 
223  protected function getGitRevision()
224  {
225  if (!StringUtility::endsWith(TYPO3_version, '-dev') || \TYPO3\CMS\Core\Core\SystemEnvironmentBuilder::isFunctionDisabled('exec')) {
226  return;
227  }
228  // check if git exists
229  CommandUtility::exec('git --version', $_, $returnCode);
230  if ((int)$returnCode !== 0) {
231  // git is not available
232  return;
233  }
234 
235  $revision = trim(CommandUtility::exec('git rev-parse --short HEAD'));
236  $branch = trim(CommandUtility::exec('git rev-parse --abbrev-ref HEAD'));
237  if (!empty($revision) && !empty($branch)) {
238  $this->systemInformation[] = [
239  'title' => htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.gitrevision')),
240  'value' => sprintf('%s [%s]', $revision, $branch),
241  'icon' => $this->iconFactory->getIcon('sysinfo-git', Icon::SIZE_SMALL)->render()
242  ];
243  }
244  }
245 
251  protected function getOperatingSystem()
252  {
253  $kernelName = php_uname('s');
254  switch (strtolower($kernelName)) {
255  case 'linux':
256  $icon = 'linux';
257  break;
258  case 'darwin':
259  $icon = 'apple';
260  break;
261  default:
262  $icon = 'windows';
263  }
264  $this->systemInformation[] = [
265  'title' => htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.operatingsystem')),
266  'value' => $kernelName . ' ' . php_uname('r'),
267  'icon' => $this->iconFactory->getIcon('sysinfo-os-' . $icon, Icon::SIZE_SMALL)->render()
268  ];
269  }
270 
274  protected function getWebServer()
275  {
276  $this->systemInformation[] = [
277  'title' => htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.webserver')),
278  'value' => htmlspecialchars($_SERVER['SERVER_SOFTWARE']),
279  'icon' => $this->iconFactory->getIcon('sysinfo-webserver', Icon::SIZE_SMALL)->render()
280  ];
281  }
282 
286  protected function getTypo3Version()
287  {
288  $this->systemInformation[] = [
289  'title' => htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.typo3-version')),
290  'value' => htmlspecialchars(VersionNumberUtility::getCurrentTypo3Version()),
291  'icon' => $this->iconFactory->getIcon('sysinfo-typo3-version', Icon::SIZE_SMALL)->render()
292  ];
293  }
294 
300  protected function emitGetSystemInformation()
301  {
302  $this->getSignalSlotDispatcher()->dispatch(__CLASS__, 'getSystemInformation', [$this]);
303  }
304 
310  protected function emitLoadMessages()
311  {
312  $this->getSignalSlotDispatcher()->dispatch(__CLASS__, 'loadMessages', [$this]);
313  }
314 
324  public function addSystemMessage($text, $status = InformationStatus::STATUS_OK, $count = 0, $module = '')
325  {
326  $this->totalCount += (int)$count;
327 
329  $messageSeverity = InformationStatus::cast($status);
330  // define the severity for the badge
331  if ($messageSeverity->isGreaterThan($this->highestSeverity)) {
332  $this->highestSeverity = $messageSeverity;
333  }
334 
335  $this->systemMessages[] = [
336  'module' => $module,
337  'count' => (int)$count,
338  'status' => $messageSeverity,
339  'text' => $text
340  ];
341  }
342 
352  public function addSystemInformation($title, $value, $icon, $status = InformationStatus::STATUS_NOTICE)
353  {
354  $this->systemInformation[] = [
355  'title' => $title,
356  'value' => $value,
357  'icon' => $icon,
358  'status' => $status
359  ];
360  }
361 
367  public function checkAccess()
368  {
369  return $this->getBackendUserAuthentication()->isAdmin();
370  }
371 
377  public function getItem()
378  {
379  $title = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo'));
380  $icon = $this->iconFactory->getIcon('actions-system-list-open', Icon::SIZE_SMALL)->render('inline');
381  return '
382  <span class="toolbar-item-icon" title="' . $title . '">' . $icon . '</span>
383  <span class="toolbar-item-title">' . $title . '</span>
384  <span class="toolbar-item-badge t3js-systeminformation-counter badge"></span>
385  ';
386  }
387 
393  public function getDropDown()
394  {
395  if (!$this->checkAccess()) {
396  return '';
397  }
398 
399  $request = $this->standaloneView->getRequest();
400  $request->setControllerExtensionName('backend');
401  $this->standaloneView->assignMultiple([
402  'installToolUrl' => BackendUtility::getModuleUrl('system_extinstall'),
403  'messages' => $this->systemMessages,
404  'count' => $this->totalCount > $this->maximumCountInBadge ? $this->maximumCountInBadge . '+' : $this->totalCount,
405  'severityBadgeClass' => $this->severityBadgeClass,
406  'systemInformation' => $this->systemInformation
407  ]);
408  return $this->standaloneView->render();
409  }
410 
416  public function getAdditionalAttributes()
417  {
418  return [];
419  }
420 
426  public function hasDropDown()
427  {
428  return true;
429  }
430 
436  public function getIndex()
437  {
438  return 75;
439  }
440 
446  protected function getBackendUserAuthentication()
447  {
448  return $GLOBALS['BE_USER'];
449  }
450 
456  protected function getPageRenderer()
457  {
458  return GeneralUtility::makeInstance(PageRenderer::class);
459  }
460 
466  protected function getLanguageService()
467  {
468  return $GLOBALS['LANG'];
469  }
470 
476  protected function getSignalSlotDispatcher()
477  {
478  if (!isset($this->signalSlotDispatcher)) {
479  $this->signalSlotDispatcher = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class)
480  ->get(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
481  }
483  }
484 }
static endsWith($haystack, $needle)
renderMenuAction(ServerRequestInterface $request, ResponseInterface $response)
addSystemInformation($title, $value, $icon, $status=InformationStatus::STATUS_NOTICE)
static exec($command, &$output=null, &$returnValue=0)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static makeInstance($className,...$constructorArguments)