‪TYPO3CMS  ‪main
SystemInformationToolbarItem.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\EventDispatcher\EventDispatcherInterface;
21 use Psr\Http\Message\ServerRequestInterface;
23 use ‪TYPO3\CMS\Backend\Toolbar\Enumeration\InformationStatus as DeprectaedInformationStatus;
35 
41 {
42  private ServerRequestInterface ‪$request;
43  protected array ‪$systemInformation = [];
45  protected string ‪$severityBadgeClass = '';
46  protected array ‪$systemMessages = [];
47  protected int ‪$systemMessageTotalCount = 0;
48 
49  public function ‪__construct(
50  private readonly EventDispatcherInterface $eventDispatcher,
51  private readonly ‪Typo3Version $typo3Version,
52  private readonly ‪BackendViewFactory $backendViewFactory,
53  ) {
54  $this->highestSeverity = InformationStatus::INFO;
55  }
56 
57  public function ‪setRequest(ServerRequestInterface ‪$request): void
58  {
59  $this->request = ‪$request;
60  }
61 
72  public function ‪addSystemMessage($text, $status = InformationStatus::OK, $count = 0, $module = '', $params = ''): void
73  {
74  $this->systemMessageTotalCount += $count;
75 
76  if (is_string($status)) {
77  trigger_error(
78  'Using a string of the non-native enumeration ' . DeprectaedInformationStatus::class . ' for $status '
79  . 'in SystemInformationToolbarItem->addSystemMessage() has been deprecated and will stop working in '
80  . 'TYPO3 v14.0. Use the native ' . InformationStatus::class . ' instead.',
81  E_USER_DEPRECATED
82  );
83  $messageSeverity = InformationStatus::tryFrom($status) ?? InformationStatus::OK;
84  } elseif ($status instanceof ‪InformationStatus) {
85  $messageSeverity = $status;
86  } else {
87  $messageSeverity = InformationStatus::OK;
88  }
89 
90  // define the severity for the badge
91  if ($messageSeverity->isGreaterThan($this->highestSeverity)) {
92  $this->highestSeverity = $messageSeverity;
93  }
94 
95  $this->systemMessages[] = [
96  'module' => $module,
97  'params' => $params,
98  'count' => $count,
99  'status' => $messageSeverity->value,
100  'text' => $text,
101  ];
102  }
103 
113  public function ‪addSystemInformation($title, $value, $iconIdentifier, $status = InformationStatus::NOTICE): void
114  {
115  if (is_string($status)) {
116  trigger_error(
117  'Using a string of the non-native enumeration ' . DeprectaedInformationStatus::class . ' for $status '
118  . 'in SystemInformationToolbarItem->addSystemInformation() has been deprecated and will stop working in '
119  . 'TYPO3 v14.0. Use the native ' . InformationStatus::class . ' instead.',
120  E_USER_DEPRECATED
121  );
122  $status = InformationStatus::tryFrom($status) ?? InformationStatus::NOTICE;
123  } elseif (!$status instanceof ‪InformationStatus) {
124  $status = InformationStatus::OK;
125  }
126 
127  $this->systemInformation[] = [
128  'title' => $title,
129  'value' => $value,
130  'iconIdentifier' => $iconIdentifier,
131  'status' => $status->value,
132  ];
133  }
134 
138  public function ‪checkAccess(): bool
139  {
140  return $this->‪getBackendUserAuthentication()->isAdmin();
141  }
142 
146  public function ‪getItem(): string
147  {
148  $view = $this->backendViewFactory->create($this->request);
149  return $view->render('ToolbarItems/SystemInformationToolbarItem');
150  }
151 
155  public function ‪getDropDown(): string
156  {
157  if (!$this->‪checkAccess()) {
158  return '';
159  }
160  $this->‪collectInformation();
161  $view = $this->backendViewFactory->create($this->request);
162  $view->assignMultiple([
163  'messages' => $this->systemMessages,
164  'count' => $this->systemMessageTotalCount > 99 ? '99+' : $this->systemMessageTotalCount,
165  'severityBadgeClass' => $this->severityBadgeClass,
166  'systemInformation' => $this->systemInformation,
167  ]);
168  return $view->render('ToolbarItems/SystemInformationDropDown');
169  }
170 
174  public function ‪getAdditionalAttributes(): array
175  {
176  return [];
177  }
178 
182  public function ‪hasDropDown(): bool
183  {
184  return true;
185  }
186 
190  public function ‪getIndex(): int
191  {
192  return 75;
193  }
194 
198  protected function ‪collectInformation(): void
199  {
200  $this->‪addTypo3Version();
201  $this->‪addWebServer();
202  $this->‪addPhpVersion();
203  $this->‪addDebugger();
204  $this->‪addDatabase();
205  $this->‪addApplicationContext();
206  $this->‪addComposerMode();
207  $this->‪addGitRevision();
208  $this->‪addOperatingSystem();
209  $this->eventDispatcher->dispatch(new ‪SystemInformationToolbarCollectorEvent($this));
210  $this->severityBadgeClass = $this->highestSeverity !== InformationStatus::NOTICE ? 'badge-' . $this->highestSeverity->value : '';
211  }
212 
213  protected function ‪addTypo3Version(): void
214  {
215  $this->systemInformation[] = [
216  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.typo3-version',
217  'value' => $this->typo3Version->getVersion(),
218  'iconIdentifier' => 'information-typo3-version',
219  ];
220  }
221 
222  protected function ‪addWebServer(): void
223  {
224  $this->systemInformation[] = [
225  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.webserver',
226  'value' => ‪$_SERVER['SERVER_SOFTWARE'] ?? '',
227  'iconIdentifier' => 'information-webserver',
228  ];
229  }
230 
231  protected function ‪addPhpVersion(): void
232  {
233  $this->systemInformation[] = [
234  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.phpversion',
235  'value' => PHP_VERSION,
236  'iconIdentifier' => 'information-php-version',
237  ];
238  }
239 
240  protected function ‪addDebugger(): void
241  {
242  $knownDebuggers = ['xdebug', 'Zend Debugger'];
243  foreach ($knownDebuggers as $debugger) {
244  if (extension_loaded($debugger)) {
245  $debuggerVersion = phpversion($debugger) ?: '';
246  $this->systemInformation[] = [
247  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.debugger',
248  'value' => sprintf('%s %s', $debugger, $debuggerVersion),
249  'iconIdentifier' => 'information-debugger',
250  ];
251  }
252  }
253  }
254 
255  protected function ‪addDatabase(): void
256  {
257  foreach (GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionNames() as $connectionName) {
258  $serverVersion = '[' . $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.database.offline') . ']';
259  $success = true;
260  try {
261  $serverVersion = GeneralUtility::makeInstance(ConnectionPool::class)
262  ->getConnectionByName($connectionName)
263  ->getPlatformServerVersion();
264  } catch (\‪Exception $exception) {
265  $success = false;
266  }
267  $this->systemInformation[] = [
268  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.database',
269  'titleAddition' => $connectionName,
270  'value' => $serverVersion,
271  'status' => $success ?: InformationStatus::WARNING->value,
272  'iconIdentifier' => 'information-database',
273  ];
274  }
275  }
276 
277  protected function ‪addApplicationContext(): void
278  {
279  $applicationContext = ‪Environment::getContext();
280  $this->systemInformation[] = [
281  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.applicationcontext',
282  'value' => (string)$applicationContext,
283  'status' => $applicationContext->isProduction() ? InformationStatus::OK->value : InformationStatus::WARNING->value,
284  'iconIdentifier' => 'information-application-context',
285  ];
286  }
287 
291  protected function ‪addComposerMode(): void
292  {
294  return;
295  }
296  $this->systemInformation[] = [
297  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.composerMode',
298  'value' => $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.enabled'),
299  'iconIdentifier' => 'information-composer-mode',
300  ];
301  }
302 
306  protected function ‪addGitRevision(): void
307  {
308  if (!str_ends_with($this->typo3Version->getVersion(), '-dev') || $this->isFunctionDisabled('exec')) {
309  return;
310  }
311  // check if git exists
312  $returnCode = 0;
313  ‪CommandUtility::exec('git --version', $_, $returnCode);
314  if ($returnCode !== 0) {
315  // git is not available
316  return;
317  }
318 
319  $revision = trim(‪CommandUtility::exec('git rev-parse --short HEAD'));
320  $branch = trim(‪CommandUtility::exec('git rev-parse --abbrev-ref HEAD'));
321  if (!empty($revision) && !empty($branch)) {
322  $this->systemInformation[] = [
323  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.gitrevision',
324  'value' => sprintf('%s [%s]', $revision, $branch),
325  'iconIdentifier' => 'information-git',
326  ];
327  }
328  }
329 
333  protected function ‪addOperatingSystem(): void
334  {
335  switch (PHP_OS_FAMILY) {
336  case 'Linux':
337  $icon = 'linux';
338  break;
339  case 'Darwin':
340  $icon = 'apple';
341  break;
342  case 'Windows':
343  $icon = 'windows';
344  break;
345  default:
346  $icon = 'unknown';
347  }
348  $this->systemInformation[] = [
349  'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.operatingsystem',
350  'value' => PHP_OS . ' ' . php_uname('r'),
351  'iconIdentifier' => 'information-os-' . $icon,
352  ];
353  }
354 
358  protected function ‪isFunctionDisabled(string $functionName): bool
359  {
360  $disabledFunctions = ‪GeneralUtility::trimExplode(',', (string)ini_get('disable_functions'));
361  if (!empty($disabledFunctions)) {
362  return in_array($functionName, $disabledFunctions, true);
363  }
364  return false;
365  }
366 
368  {
369  return ‪$GLOBALS['BE_USER'];
370  }
371 
373  {
374  return ‪$GLOBALS['LANG'];
375  }
376 }
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\getIndex
‪getIndex()
Definition: SystemInformationToolbarItem.php:190
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\$highestSeverity
‪InformationStatus $highestSeverity
Definition: SystemInformationToolbarItem.php:44
‪TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface
Definition: ToolbarItemInterface.php:22
‪TYPO3\CMS\Core\Information\Typo3Version
Definition: Typo3Version.php:21
‪TYPO3\CMS\Backend\View\BackendViewFactory
Definition: BackendViewFactory.php:35
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\addDebugger
‪addDebugger()
Definition: SystemInformationToolbarItem.php:240
‪TYPO3\CMS\Backend\Backend\Event\SystemInformationToolbarCollectorEvent
Definition: SystemInformationToolbarCollectorEvent.php:27
‪TYPO3\CMS\Core\Core\Environment\isComposerMode
‪static isComposerMode()
Definition: Environment.php:137
‪TYPO3\CMS\Backend\Exception
Definition: Exception.php:23
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\getItem
‪getItem()
Definition: SystemInformationToolbarItem.php:146
‪TYPO3\CMS\Backend\Toolbar\InformationStatus
‪InformationStatus
Definition: InformationStatus.php:24
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\addTypo3Version
‪addTypo3Version()
Definition: SystemInformationToolbarItem.php:213
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\addPhpVersion
‪addPhpVersion()
Definition: SystemInformationToolbarItem.php:231
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\isFunctionDisabled
‪isFunctionDisabled(string $functionName)
Definition: SystemInformationToolbarItem.php:358
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\addWebServer
‪addWebServer()
Definition: SystemInformationToolbarItem.php:222
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\$systemMessages
‪array $systemMessages
Definition: SystemInformationToolbarItem.php:46
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\$severityBadgeClass
‪string $severityBadgeClass
Definition: SystemInformationToolbarItem.php:45
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\getBackendUserAuthentication
‪getBackendUserAuthentication()
Definition: SystemInformationToolbarItem.php:367
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\getLanguageService
‪getLanguageService()
Definition: SystemInformationToolbarItem.php:372
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\addComposerMode
‪addComposerMode()
Definition: SystemInformationToolbarItem.php:291
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\getDropDown
‪getDropDown()
Definition: SystemInformationToolbarItem.php:155
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\addGitRevision
‪addGitRevision()
Definition: SystemInformationToolbarItem.php:306
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\collectInformation
‪collectInformation()
Definition: SystemInformationToolbarItem.php:198
‪TYPO3\CMS\Backend\Backend\ToolbarItems
Definition: ClearCacheToolbarItem.php:18
‪TYPO3\CMS\Core\Utility\CommandUtility\exec
‪static exec(string $command, ?array &$output=null, int &$returnValue=0)
Definition: CommandUtility.php:85
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem
Definition: SystemInformationToolbarItem.php:41
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\$systemMessageTotalCount
‪int $systemMessageTotalCount
Definition: SystemInformationToolbarItem.php:47
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\addOperatingSystem
‪addOperatingSystem()
Definition: SystemInformationToolbarItem.php:333
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\__construct
‪__construct(private readonly EventDispatcherInterface $eventDispatcher, private readonly Typo3Version $typo3Version, private readonly BackendViewFactory $backendViewFactory,)
Definition: SystemInformationToolbarItem.php:49
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\$systemInformation
‪array $systemInformation
Definition: SystemInformationToolbarItem.php:43
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪$_SERVER
‪$_SERVER['TYPO3_DEPRECATED_ENTRYPOINT']
Definition: legacy-backend.php:20
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\hasDropDown
‪hasDropDown()
Definition: SystemInformationToolbarItem.php:182
‪TYPO3\CMS\Backend\Toolbar\Enumeration\InformationStatus
Definition: InformationStatus.php:25
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\addSystemMessage
‪addSystemMessage($text, $status=InformationStatus::OK, $count=0, $module='', $params='')
Definition: SystemInformationToolbarItem.php:72
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\addSystemInformation
‪addSystemInformation($title, $value, $iconIdentifier, $status=InformationStatus::NOTICE)
Definition: SystemInformationToolbarItem.php:113
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\addDatabase
‪addDatabase()
Definition: SystemInformationToolbarItem.php:255
‪TYPO3\CMS\Backend\Toolbar\RequestAwareToolbarItemInterface
Definition: RequestAwareToolbarItemInterface.php:27
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\addApplicationContext
‪addApplicationContext()
Definition: SystemInformationToolbarItem.php:277
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\setRequest
‪setRequest(ServerRequestInterface $request)
Definition: SystemInformationToolbarItem.php:57
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\$request
‪ServerRequestInterface $request
Definition: SystemInformationToolbarItem.php:42
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\getAdditionalAttributes
‪getAdditionalAttributes()
Definition: SystemInformationToolbarItem.php:174
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\CommandUtility
Definition: CommandUtility.php:54
‪TYPO3\CMS\Backend\Backend\ToolbarItems\SystemInformationToolbarItem\checkAccess
‪checkAccess()
Definition: SystemInformationToolbarItem.php:138
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static getContext()
Definition: Environment.php:128
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822