SystemInformationToolbarItem.php 14.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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!
 */

17
18
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
19
use TYPO3\CMS\Backend\Toolbar\Enumeration\InformationStatus;
20
use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
21
use TYPO3\CMS\Backend\Utility\BackendUtility;
22
use TYPO3\CMS\Core\Core\Bootstrap;
23
use TYPO3\CMS\Core\Database\ConnectionPool;
24
25
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
26
use TYPO3\CMS\Core\Page\PageRenderer;
27
use TYPO3\CMS\Core\Utility\CommandUtility;
28
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
29
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
31
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
32
use TYPO3\CMS\Fluid\View\StandaloneView;
33
34
35
36

/**
 * Render system info toolbar item
 */
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class SystemInformationToolbarItem implements ToolbarItemInterface
{
    /**
     * @var StandaloneView
     */
    protected $standaloneView = null;

    /**
     * Template file for the dropdown menu
     */
    const TOOLBAR_MENU_TEMPLATE = 'SystemInformation.html';

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

    /**
     * Holds the highest severity
     *
     * @var InformationStatus
     */
    protected $highestSeverity;

    /**
     * The CSS class for the badge
     *
     * @var string
     */
    protected $severityBadgeClass = '';

    /**
     * @var array
     */
73
    protected $systemInformation = [];
74
75
76
77

    /**
     * @var array
     */
78
    protected $systemMessages = [];
79
80
81
82
83
84
85
86
87
88
89

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

    /**
     * @var IconFactory
     */
    protected $iconFactory;

90
91
92
93
94
    /**
     * @var int
     */
    protected $maximumCountInBadge = 99;

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    /**
     * Constructor
     */
    public function __construct()
    {
        if (!$this->checkAccess()) {
            return;
        }
        $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);

        $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);

        $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Toolbar/SystemInformationMenu');

        $this->highestSeverity = InformationStatus::cast(InformationStatus::STATUS_INFO);
    }

    /**
     * Collect the information for the menu
     */
    protected function collectInformation()
    {
120
        $this->getTypo3Version();
121
122
123
124
        $this->getWebServer();
        $this->getPhpVersion();
        $this->getDatabase();
        $this->getApplicationContext();
125
        $this->getComposerMode();
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
        $this->getGitRevision();
        $this->getOperatingSystem();

        $this->emitGetSystemInformation();
        $this->emitLoadMessages();

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

    /**
     * Renders the menu for AJAX calls
     *
     * @param ServerRequestInterface $request
     * @param ResponseInterface $response
     * @return ResponseInterface
     */
    public function renderMenuAction(ServerRequestInterface $request, ResponseInterface $response)
    {
        $this->collectInformation();

        $response->getBody()->write($this->getDropDown());
        $response = $response->withHeader('Content-Type', 'text/html; charset=utf-8');
        return $response;
    }

    /**
     * Gets the PHP version
     *
     * @return void
     */
    protected function getPhpVersion()
    {
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
165
166
167
168
169
170
171
    }

    /**
     * Get the database info
     *
     * @return void
     */
    protected function getDatabase()
    {
172
173
174
        foreach (GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionNames() as $connectionName) {
            $this->systemInformation[] = [
                'title' => $this->getLanguageService()
175
                    ->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.database')
176
177
178
179
180
181
182
                    . ' (' . htmlspecialchars($connectionName) . ')',
                'value' => GeneralUtility::makeInstance(ConnectionPool::class)
                    ->getConnectionByName($connectionName)
                    ->getServerVersion(),
                'icon' => $this->iconFactory->getIcon('sysinfo-database', Icon::SIZE_SMALL)->render()
            ];
        }
183
184
185
186
187
188
189
190
191
192
    }

    /**
     * Gets the application context
     *
     * @return void
     */
    protected function getApplicationContext()
    {
        $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
196
            'value' => (string)$applicationContext,
            'status' => $applicationContext->isProduction() ? InformationStatus::STATUS_OK : InformationStatus::STATUS_WARNING,
197
            'icon' => $this->iconFactory->getIcon('sysinfo-application-context', Icon::SIZE_SMALL)->render()
198
        ];
199
200
    }

201
    /**
202
     * Adds the information if the Composer mode is enabled or disabled to the displayed system information
203
     */
204
    protected function getComposerMode()
205
    {
206
207
208
209
        if (!Bootstrap::usesComposerClassLoading()) {
            return;
        }

210
        $languageService = $this->getLanguageService();
211
        $this->systemInformation[] = [
212
213
            'title' => htmlspecialchars($languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.composerMode')),
            '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
    }

218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    /**
     * Gets the current GIT revision and branch
     *
     * @return void
     */
    protected function getGitRevision()
    {
        if (!StringUtility::endsWith(TYPO3_version, '-dev') || \TYPO3\CMS\Core\Core\SystemEnvironmentBuilder::isFunctionDisabled('exec')) {
            return;
        }
        // check if git exists
        CommandUtility::exec('git --version', $_, $returnCode);
        if ((int)$returnCode !== 0) {
            // git is not available
            return;
        }

        $revision = trim(CommandUtility::exec('git rev-parse --short HEAD'));
        $branch = trim(CommandUtility::exec('git rev-parse --abbrev-ref HEAD'));
        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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
        }
    }

    /**
     * Gets the system kernel and version
     *
     * @return void
     */
    protected function getOperatingSystem()
    {
        $kernelName = php_uname('s');
        switch (strtolower($kernelName)) {
            case 'linux':
                $icon = 'linux';
                break;
            case 'darwin':
                $icon = 'apple';
                break;
            default:
                $icon = 'windows';
        }
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
271
272
273
274
275
    }

    /**
     * Gets the webserver software
     */
    protected function getWebServer()
    {
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
    }

283
284
285
286
287
288
    /**
     * Gets the TYPO3 version
     */
    protected function getTypo3Version()
    {
        $this->systemInformation[] = [
289
            'title' => htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.typo3-version')),
290
291
292
293
294
            'value' => htmlspecialchars(VersionNumberUtility::getCurrentTypo3Version()),
            'icon' => $this->iconFactory->getIcon('sysinfo-typo3-version', Icon::SIZE_SMALL)->render()
        ];
    }

295
296
297
298
299
300
301
    /**
     * Emits the "getSystemInformation" signal
     *
     * @return void
     */
    protected function emitGetSystemInformation()
    {
302
        $this->getSignalSlotDispatcher()->dispatch(__CLASS__, 'getSystemInformation', [$this]);
303
304
305
306
307
308
309
310
311
    }

    /**
     * Emits the "loadMessages" signal
     *
     * @return void
     */
    protected function emitLoadMessages()
    {
312
        $this->getSignalSlotDispatcher()->dispatch(__CLASS__, 'loadMessages', [$this]);
313
    }
314

315
316
317
318
319
320
321
322
323
324
325
326
    /**
     * Add a system message.
     * This is a callback method for signal receivers.
     *
     * @param string $text The text to be displayed
     * @param string $status The status of this system message
     * @param int $count Will be added to the total count
     * @param string $module The associated module
     */
    public function addSystemMessage($text, $status = InformationStatus::STATUS_OK, $count = 0, $module = '')
    {
        $this->totalCount += (int)$count;
327
328

        /** @var InformationStatus $messageSeverity */
329
        $messageSeverity = InformationStatus::cast($status);
330
331
332
333
334
        // define the severity for the badge
        if ($messageSeverity->isGreaterThan($this->highestSeverity)) {
            $this->highestSeverity = $messageSeverity;
        }

335
        $this->systemMessages[] = [
336
337
338
339
            'module' => $module,
            'count' => (int)$count,
            'status' => $messageSeverity,
            'text' => $text
340
        ];
341
342
343
344
345
346
347
348
349
    }

    /**
     * Add a system information.
     * This is a callback method for signal receivers.
     *
     * @param string $title The title of this system information
     * @param string $value The associated value
     * @param string $icon The icon html
350
     * @param string $status The status of this system information
351
     */
352
    public function addSystemInformation($title, $value, $icon, $status = InformationStatus::STATUS_NOTICE)
353
    {
354
        $this->systemInformation[] = [
355
356
            'title' => $title,
            'value' => $value,
357
358
            'icon' => $icon,
            'status' => $status
359
        ];
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
    }

    /**
     * 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()
    {
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
382
383
384
385
        return '
            <span class="toolbar-item-icon" title="' . $title . '">' . $icon . '</span>
            <span class="toolbar-item-title">' . $title . '</span>
            <span class="toolbar-item-badge t3js-systeminformation-counter badge"></span>
            ';
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
    }

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

        $request = $this->standaloneView->getRequest();
        $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
406
            'severityBadgeClass' => $this->severityBadgeClass,
            'systemInformation' => $this->systemInformation
407
        ]);
408
409
410
411
412
413
414
415
416
417
        return $this->standaloneView->render();
    }

    /**
     * No additional attributes needed.
     *
     * @return array
     */
    public function getAdditionalAttributes()
    {
418
        return [];
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
    }

    /**
     * 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 current PageRenderer
     *
     * @return PageRenderer
     */
    protected function getPageRenderer()
    {
        return GeneralUtility::makeInstance(PageRenderer::class);
    }

    /**
     * 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;
    }
484
}