SystemInformationToolbarItem.php 12.8 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
use TYPO3\CMS\Backend\Routing\UriBuilder;
18
use TYPO3\CMS\Backend\Toolbar\Enumeration\InformationStatus;
19
use TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface;
20
use TYPO3\CMS\Core\Core\Environment;
21
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
22
use TYPO3\CMS\Core\Database\ConnectionPool;
23
use TYPO3\CMS\Core\Page\PageRenderer;
24
25
26
use TYPO3\CMS\Core\Utility\CommandUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
27
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
28
29
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
30
use TYPO3\CMS\Fluid\View\StandaloneView;
31
32
33
34

/**
 * Render system info toolbar item
 */
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class SystemInformationToolbarItem implements ToolbarItemInterface
{
    /**
     * 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
     */
61
    protected $systemInformation = [];
62
63
64
65

    /**
     * @var array
     */
66
    protected $systemMessages = [];
67
68

    /**
69
     * @var Dispatcher
70
71
72
     */
    protected $signalSlotDispatcher = null;

73
74
75
76
77
    /**
     * @var int
     */
    protected $maximumCountInBadge = 99;

78
79
80
81
82
83
84
85
86
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Toolbar/SystemInformationMenu');
        $this->highestSeverity = InformationStatus::cast(InformationStatus::STATUS_INFO);
    }

87
88
89
90
91
92
93
94
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
120
121
122
123
124
125
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    /**
     * 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
     * @param string $params Query string with additional parameters
     */
    public function addSystemMessage($text, $status = InformationStatus::STATUS_OK, $count = 0, $module = '', $params = '')
    {
        $this->totalCount += (int)$count;

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

        $this->systemMessages[] = [
            'module' => $module,
            'params' => $params,
            'count' => (int)$count,
            'status' => $messageSeverity,
            'text' => $text
        ];
    }

    /**
     * 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 $iconIdentifier The icon identifier
     * @param string $status The status of this system information
     */
    public function addSystemInformation($title, $value, $iconIdentifier, $status = InformationStatus::STATUS_NOTICE)
    {
        $this->systemInformation[] = [
            'title' => $title,
            'value' => $value,
            'iconIdentifier' => $iconIdentifier,
            'status' => $status
        ];
    }

    /**
     * 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()
    {
        return $this->getFluidTemplateObject('SystemInformationToolbarItem.html')->render();
    }

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

        $this->collectInformation();

        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
        $view = $this->getFluidTemplateObject('SystemInformationDropDown.html');
        $view->assignMultiple([
            'environmentToolUrl' => (string)$uriBuilder->buildUriFromRoute('tools_toolsenvironment'),
            'messages' => $this->systemMessages,
            'count' => $this->totalCount > $this->maximumCountInBadge ? $this->maximumCountInBadge . '+' : $this->totalCount,
            'severityBadgeClass' => $this->severityBadgeClass,
            'systemInformation' => $this->systemInformation
        ]);
        return $view->render();
    }

    /**
     * No additional attributes needed.
     *
     * @return array
     */
    public function getAdditionalAttributes()
    {
        return [];
    }

    /**
     * This item has a drop down
     *
     * @return bool
     */
    public function hasDropDown()
    {
        return true;
    }

    /**
     * Position relative to others
     *
     * @return int
     */
    public function getIndex()
    {
        return 75;
    }

211
212
213
214
215
    /**
     * Collect the information for the menu
     */
    protected function collectInformation()
    {
216
        $this->getTypo3Version();
217
218
219
220
        $this->getWebServer();
        $this->getPhpVersion();
        $this->getDatabase();
        $this->getApplicationContext();
221
        $this->getComposerMode();
222
223
224
225
226
227
228
229
230
231
        $this->getGitRevision();
        $this->getOperatingSystem();

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

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

    /**
232
     * Gets the TYPO3 version
233
     */
234
    protected function getTypo3Version()
235
    {
236
237
238
        $this->systemInformation[] = [
            'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.typo3-version',
            'value' => VersionNumberUtility::getCurrentTypo3Version(),
239
            'iconIdentifier' => 'information-typo3-version'
240
241
242
243
244
245
246
247
248
249
250
        ];
    }

    /**
     * Gets the webserver software
     */
    protected function getWebServer()
    {
        $this->systemInformation[] = [
            'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.webserver',
            'value' => $_SERVER['SERVER_SOFTWARE'],
251
            'iconIdentifier' => 'information-webserver'
252
        ];
253
254
255
256
257
258
259
    }

    /**
     * Gets the PHP version
     */
    protected function getPhpVersion()
    {
260
        $this->systemInformation[] = [
261
            'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.phpversion',
262
            'value' => PHP_VERSION,
263
            'iconIdentifier' => 'information-php-version'
264
        ];
265
266
267
268
269
270
271
    }

    /**
     * Get the database info
     */
    protected function getDatabase()
    {
272
273
        foreach (GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionNames() as $connectionName) {
            $this->systemInformation[] = [
274
275
                'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.database',
                'titleAddition' => $connectionName,
276
277
278
                'value' => GeneralUtility::makeInstance(ConnectionPool::class)
                    ->getConnectionByName($connectionName)
                    ->getServerVersion(),
279
                'iconIdentifier' => 'information-database'
280
281
            ];
        }
282
283
284
285
286
287
288
289
    }

    /**
     * Gets the application context
     */
    protected function getApplicationContext()
    {
        $applicationContext = GeneralUtility::getApplicationContext();
290
        $this->systemInformation[] = [
291
292
            'title'  => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.applicationcontext',
            'value'  => (string)$applicationContext,
293
            'status' => $applicationContext->isProduction() ? InformationStatus::STATUS_OK : InformationStatus::STATUS_WARNING,
294
            'iconIdentifier' => 'information-application-context'
295
        ];
296
297
    }

298
    /**
299
     * Adds the information if the Composer mode is enabled or disabled to the displayed system information
300
     */
301
    protected function getComposerMode()
302
    {
303
        if (!Environment::isComposerMode()) {
304
305
306
            return;
        }

307
        $this->systemInformation[] = [
308
            'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.composerMode',
309
            'value' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.enabled'),
310
            'iconIdentifier' => 'information-composer-mode'
311
        ];
312
313
    }

314
315
316
317
318
    /**
     * Gets the current GIT revision and branch
     */
    protected function getGitRevision()
    {
319
        if (!StringUtility::endsWith(TYPO3_version, '-dev') || SystemEnvironmentBuilder::isFunctionDisabled('exec')) {
320
321
322
323
324
325
326
327
328
329
330
331
            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)) {
332
            $this->systemInformation[] = [
333
                'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.gitrevision',
334
                'value' => sprintf('%s [%s]', $revision, $branch),
335
                'iconIdentifier' => 'information-git'
336
            ];
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
        }
    }

    /**
     * Gets the system kernel and version
     */
    protected function getOperatingSystem()
    {
        $kernelName = php_uname('s');
        switch (strtolower($kernelName)) {
            case 'linux':
                $icon = 'linux';
                break;
            case 'darwin':
                $icon = 'apple';
                break;
            default:
                $icon = 'windows';
        }
356
        $this->systemInformation[] = [
357
            'title' => 'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.operatingsystem',
358
            'value' => $kernelName . ' ' . php_uname('r'),
359
            'iconIdentifier' => 'information-os-' . $icon
360
        ];
361
362
363
364
365
366
367
    }

    /**
     * Emits the "getSystemInformation" signal
     */
    protected function emitGetSystemInformation()
    {
368
        $this->getSignalSlotDispatcher()->dispatch(__CLASS__, 'getSystemInformation', [$this]);
369
370
371
372
373
374
375
    }

    /**
     * Emits the "loadMessages" signal
     */
    protected function emitLoadMessages()
    {
376
        $this->getSignalSlotDispatcher()->dispatch(__CLASS__, 'loadMessages', [$this]);
377
    }
378

379
    /**
380
     * Returns a new standalone view, shorthand function
381
     *
382
383
     * @param string $filename Which templateFile should be used.
     * @return StandaloneView
384
     */
385
    protected function getFluidTemplateObject(string $filename): StandaloneView
386
    {
387
388
389
390
        $view = GeneralUtility::makeInstance(StandaloneView::class);
        $view->setLayoutRootPaths(['EXT:backend/Resources/Private/Layouts']);
        $view->setPartialRootPaths(['EXT:backend/Resources/Private/Partials/ToolbarItems']);
        $view->setTemplateRootPaths(['EXT:backend/Resources/Private/Templates/ToolbarItems']);
391

392
        $view->setTemplate($filename);
393

394
395
        $view->getRequest()->setControllerExtensionName('Backend');
        return $view;
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
    }

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

    /**
     * Get the SignalSlot dispatcher
     *
421
     * @return Dispatcher
422
423
424
425
     */
    protected function getSignalSlotDispatcher()
    {
        if (!isset($this->signalSlotDispatcher)) {
426
            $this->signalSlotDispatcher = GeneralUtility::makeInstance(ObjectManager::class)->get(Dispatcher::class);
427
428
429
        }
        return $this->signalSlotDispatcher;
    }
430
}