TYPO3 CMS  TYPO3_8-7
SystemInformationToolbarItem.php
Go to the documentation of this file.
1 <?php
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 
31 
36 {
42  protected $totalCount = 0;
43 
49  protected $highestSeverity;
50 
56  protected $severityBadgeClass = '';
57 
61  protected $systemInformation = [];
62 
66  protected $systemMessages = [];
67 
71  protected $signalSlotDispatcher = null;
72 
76  protected $maximumCountInBadge = 99;
77 
81  public function __construct()
82  {
83  $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Toolbar/SystemInformationMenu');
85  }
86 
90  protected function collectInformation()
91  {
92  $this->getTypo3Version();
93  $this->getWebServer();
94  $this->getPhpVersion();
95  $this->getDatabase();
96  $this->getApplicationContext();
97  $this->getComposerMode();
98  $this->getGitRevision();
99  $this->getOperatingSystem();
100 
101  $this->emitGetSystemInformation();
102  $this->emitLoadMessages();
103 
104  $this->severityBadgeClass = !$this->highestSeverity->equals(InformationStatus::STATUS_NOTICE) ? 'badge-' . (string)$this->highestSeverity : '';
105  }
106 
114  public function renderMenuAction(ServerRequestInterface $request, ResponseInterface $response)
115  {
116  $this->collectInformation();
117  $response->getBody()->write($this->getDropDown());
118  return $response->withHeader('Content-Type', 'text/html; charset=utf-8');
119  }
120 
124  protected function getPhpVersion()
125  {
126  $this->systemInformation[] = [
127  'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.phpversion',
128  'value' => PHP_VERSION,
129  'iconIdentifier' => 'sysinfo-php-version'
130  ];
131  }
132 
136  protected function getDatabase()
137  {
138  foreach (GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionNames() as $connectionName) {
139  $this->systemInformation[] = [
140  'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.database',
141  'titleAddition' => $connectionName,
142  'value' => GeneralUtility::makeInstance(ConnectionPool::class)
143  ->getConnectionByName($connectionName)
144  ->getServerVersion(),
145  'iconIdentifier' => 'sysinfo-database'
146  ];
147  }
148  }
149 
153  protected function getApplicationContext()
154  {
155  $applicationContext = GeneralUtility::getApplicationContext();
156  $this->systemInformation[] = [
157  'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.applicationcontext',
158  'value' => (string)$applicationContext,
159  'status' => $applicationContext->isProduction() ? InformationStatus::STATUS_OK : InformationStatus::STATUS_WARNING,
160  'iconIdentifier' => 'sysinfo-application-context'
161  ];
162  }
163 
167  protected function getComposerMode()
168  {
170  return;
171  }
172 
173  $this->systemInformation[] = [
174  'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.composerMode',
175  'value' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.enabled'),
176  'iconIdentifier' => 'sysinfo-composer-mode'
177  ];
178  }
179 
183  protected function getGitRevision()
184  {
185  if (!StringUtility::endsWith(TYPO3_version, '-dev') || SystemEnvironmentBuilder::isFunctionDisabled('exec')) {
186  return;
187  }
188  // check if git exists
189  CommandUtility::exec('git --version', $_, $returnCode);
190  if ((int)$returnCode !== 0) {
191  // git is not available
192  return;
193  }
194 
195  $revision = trim(CommandUtility::exec('git rev-parse --short HEAD'));
196  $branch = trim(CommandUtility::exec('git rev-parse --abbrev-ref HEAD'));
197  if (!empty($revision) && !empty($branch)) {
198  $this->systemInformation[] = [
199  'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.gitrevision',
200  'value' => sprintf('%s [%s]', $revision, $branch),
201  'iconIdentifier' => 'sysinfo-git'
202  ];
203  }
204  }
205 
209  protected function getOperatingSystem()
210  {
211  $kernelName = PHP_OS;
212  switch (strtolower($kernelName)) {
213  case 'linux':
214  $icon = 'linux';
215  break;
216  case 'darwin':
217  $icon = 'apple';
218  break;
219  case StringUtility::beginsWith($kernelName, 'win'):
220  $icon = 'windows';
221  break;
222  default:
223  $icon = 'unknown';
224  }
225  $this->systemInformation[] = [
226  'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.operatingsystem',
227  'value' => $kernelName . ' ' . php_uname('r'),
228  'iconIdentifier' => 'sysinfo-os-' . $icon
229  ];
230  }
231 
235  protected function getWebServer()
236  {
237  $this->systemInformation[] = [
238  'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.webserver',
239  'value' => $_SERVER['SERVER_SOFTWARE'],
240  'iconIdentifier' => 'sysinfo-webserver'
241  ];
242  }
243 
247  protected function getTypo3Version()
248  {
249  $this->systemInformation[] = [
250  'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.typo3-version',
252  'iconIdentifier' => 'sysinfo-typo3-version'
253  ];
254  }
255 
259  protected function emitGetSystemInformation()
260  {
261  $this->getSignalSlotDispatcher()->dispatch(__CLASS__, 'getSystemInformation', [$this]);
262  }
263 
267  protected function emitLoadMessages()
268  {
269  $this->getSignalSlotDispatcher()->dispatch(__CLASS__, 'loadMessages', [$this]);
270  }
271 
281  public function addSystemMessage($text, $status = InformationStatus::STATUS_OK, $count = 0, $module = '')
282  {
283  $this->totalCount += (int)$count;
284 
286  $messageSeverity = InformationStatus::cast($status);
287  // define the severity for the badge
288  if ($messageSeverity->isGreaterThan($this->highestSeverity)) {
289  $this->highestSeverity = $messageSeverity;
290  }
291 
292  $this->systemMessages[] = [
293  'module' => $module,
294  'count' => (int)$count,
295  'status' => $messageSeverity,
296  'text' => $text
297  ];
298  }
299 
309  public function addSystemInformation($title, $value, $iconIdentifier, $status = InformationStatus::STATUS_NOTICE)
310  {
311  $this->systemInformation[] = [
312  'title' => $title,
313  'value' => $value,
314  'iconIdentifier' => $iconIdentifier,
315  'status' => $status
316  ];
317  }
318 
324  public function checkAccess()
325  {
326  return $this->getBackendUserAuthentication()->isAdmin();
327  }
328 
334  public function getItem()
335  {
336  return $this->getFluidTemplateObject('SystemInformationToolbarItem.html')->render();
337  }
338 
344  public function getDropDown()
345  {
346  if (!$this->checkAccess()) {
347  return '';
348  }
349 
350  $view = $this->getFluidTemplateObject('SystemInformationDropDown.html');
351  $view->assignMultiple([
352  'installToolUrl' => BackendUtility::getModuleUrl('system_extinstall'),
353  'messages' => $this->systemMessages,
354  'count' => $this->totalCount > $this->maximumCountInBadge ? $this->maximumCountInBadge . '+' : $this->totalCount,
355  'severityBadgeClass' => $this->severityBadgeClass,
356  'systemInformation' => $this->systemInformation
357  ]);
358  return $view->render();
359  }
360 
366  public function getAdditionalAttributes()
367  {
368  return [];
369  }
370 
376  public function hasDropDown()
377  {
378  return true;
379  }
380 
386  public function getIndex()
387  {
388  return 75;
389  }
390 
396  protected function getBackendUserAuthentication()
397  {
398  return $GLOBALS['BE_USER'];
399  }
400 
406  protected function getPageRenderer()
407  {
408  return GeneralUtility::makeInstance(PageRenderer::class);
409  }
410 
416  protected function getSignalSlotDispatcher()
417  {
418  if (!isset($this->signalSlotDispatcher)) {
419  $this->signalSlotDispatcher = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class)
420  ->get(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
421  }
423  }
424 
431  protected function getFluidTemplateObject(string $filename): StandaloneView
432  {
433  $view = GeneralUtility::makeInstance(StandaloneView::class);
434  $view->setLayoutRootPaths(['EXT:backend/Resources/Private/Layouts']);
435  $view->setPartialRootPaths(['EXT:backend/Resources/Private/Partials/ToolbarItems']);
436  $view->setTemplateRootPaths(['EXT:backend/Resources/Private/Templates/ToolbarItems']);
437 
438  $view->setTemplate($filename);
439 
440  $view->getRequest()->setControllerExtensionName('Backend');
441  return $view;
442  }
443 }
renderMenuAction(ServerRequestInterface $request, ResponseInterface $response)
static exec($command, &$output=null, &$returnValue=0)
addSystemInformation($title, $value, $iconIdentifier, $status=InformationStatus::STATUS_NOTICE)
static makeInstance($className,... $constructorArguments)
static beginsWith($haystack, $needle)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static endsWith($haystack, $needle)