Completed
Branch master (d17104)
by Christian
21:20
created

GeneralInformation::getDataToStore()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 19
nc 1
nop 1
dl 0
loc 24
rs 9.6333
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace TYPO3\CMS\Adminpanel\Modules\Info;
5
6
/*
7
 * This file is part of the TYPO3 CMS project.
8
 *
9
 * It is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU General Public License, either version 2
11
 * of the License, or any later version.
12
 *
13
 * For the full copyright and license information, please read the
14
 * LICENSE.txt file that was distributed with this source code.
15
 *
16
 * The TYPO3 project - inspiring people to share!
17
 */
18
19
use Psr\Http\Message\ServerRequestInterface;
20
use TYPO3\CMS\Adminpanel\ModuleApi\AbstractSubModule;
21
use TYPO3\CMS\Adminpanel\ModuleApi\ContentProviderInterface;
22
use TYPO3\CMS\Adminpanel\ModuleApi\DataProviderInterface;
23
use TYPO3\CMS\Adminpanel\ModuleApi\ModuleData;
24
use TYPO3\CMS\Core\Context\Context;
25
use TYPO3\CMS\Core\Context\UserAspect;
26
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
use TYPO3\CMS\Fluid\View\StandaloneView;
29
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
30
31
/**
32
 * General information module displaying info about the current
33
 * request
34
 *
35
 * @internal
36
 */
37
class GeneralInformation extends AbstractSubModule implements DataProviderInterface, ContentProviderInterface
38
{
39
    /**
40
     * @inheritdoc
41
     */
42
    public function getDataToStore(ServerRequestInterface $request): ModuleData
43
    {
44
        /** @var UserAspect $frontendUserAspect */
45
        $frontendUserAspect = GeneralUtility::makeInstance(Context::class)->getAspect('frontend.user');
46
        $tsfe = $this->getTypoScriptFrontendController();
47
        return new ModuleData(
48
            [
49
                'post' => $_POST,
50
                'get' => $_GET,
51
                'cookie' => $_COOKIE,
52
                'server' => $_SERVER,
53
                'info' => [
54
                    'pageUid' => $tsfe->id,
55
                    'pageType' => $tsfe->type,
56
                    'groupList' => implode(',', $frontendUserAspect->getGroupIds()),
57
                    'noCache' => $this->isNoCacheEnabled(),
58
                    'countUserInt' => count($tsfe->config['INTincScript'] ?? []),
59
                    'totalParsetime' => $this->getTimeTracker()->getParseTime(),
60
                    'feUser' => [
61
                        'uid' => $frontendUserAspect->get('id') ?: 0,
62
                        'username' => $frontendUserAspect->get('username') ?: '',
63
                    ],
64
                    'imagesOnPage' => $this->collectImagesOnPage(),
65
                    'documentSize' => $this->collectDocumentSize(),
66
                ],
67
            ]
68
        );
69
    }
70
71
    /**
72
     * Creates the content for the "info" section ("module") of the Admin Panel
73
     *
74
     * @param \TYPO3\CMS\Adminpanel\ModuleApi\ModuleData $data
75
     * @return string HTML content for the section. Consists of a string with table-rows with four columns.
76
     * @see display()
77
     */
78
    public function getContent(ModuleData $data): string
79
    {
80
        $view = GeneralUtility::makeInstance(StandaloneView::class);
81
        $templateNameAndPath = 'EXT:adminpanel/Resources/Private/Templates/Modules/Info/General.html';
82
        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templateNameAndPath));
83
        $view->setPartialRootPaths(['EXT:adminpanel/Resources/Private/Partials']);
84
85
        $view->assignMultiple($data->getArrayCopy());
86
87
        return $view->render();
88
    }
89
90
    /**
91
     * Identifier for this Sub-module,
92
     * for example "preview" or "cache"
93
     *
94
     * @return string
95
     */
96
    public function getIdentifier(): string
97
    {
98
        return 'info_general';
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104
    public function getLabel(): string
105
    {
106
        return $this->getLanguageService()->sL(
107
            'LLL:EXT:adminpanel/Resources/Private/Language/locallang_info.xlf:sub.general.label'
108
        );
109
    }
110
111
    /**
112
     * Collects images from TypoScriptFrontendController and calculates the total size.
113
     * Returns human readable image sizes for fluid template output
114
     *
115
     * @return array
116
     */
117
    protected function collectImagesOnPage(): array
118
    {
119
        $imagesOnPage = [
120
            'files' => [],
121
            'total' => 0,
122
            'totalSize' => 0,
123
            'totalSizeHuman' => GeneralUtility::formatSize(0),
124
        ];
125
126
        if ($this->isNoCacheEnabled() === false) {
127
            return $imagesOnPage;
128
        }
129
130
        $count = 0;
131
        $totalImageSize = 0;
132
        if (!empty($this->getTypoScriptFrontendController()->imagesOnPage)) {
133
            foreach ($this->getTypoScriptFrontendController()->imagesOnPage as $file) {
134
                $fileSize = @filesize($file);
135
                $imagesOnPage['files'][] = [
136
                    'name' => $file,
137
                    'size' => $fileSize,
138
                    'sizeHuman' => GeneralUtility::formatSize($fileSize),
139
                ];
140
                $totalImageSize += $fileSize;
141
                $count++;
142
            }
143
        }
144
        $imagesOnPage['totalSize'] = GeneralUtility::formatSize($totalImageSize);
145
        $imagesOnPage['total'] = $count;
146
        return $imagesOnPage;
147
    }
148
149
    /**
150
     * Gets the document size from the current page in a human readable format
151
     *
152
     * @return string
153
     */
154
    protected function collectDocumentSize(): string
155
    {
156
        $documentSize = 0;
157
        if ($this->isNoCacheEnabled() === true) {
158
            $documentSize = mb_strlen($this->getTypoScriptFrontendController()->content, 'UTF-8');
159
        }
160
161
        return GeneralUtility::formatSize($documentSize);
162
    }
163
164
    /**
165
     * @return bool
166
     */
167
    protected function isNoCacheEnabled(): bool
168
    {
169
        return (bool)$this->getTypoScriptFrontendController()->no_cache;
170
    }
171
172
    /**
173
     * @return TypoScriptFrontendController
174
     */
175
    protected function getTypoScriptFrontendController(): TypoScriptFrontendController
176
    {
177
        return $GLOBALS['TSFE'];
178
    }
179
180
    /**
181
     * @return TimeTracker
182
     */
183
    protected function getTimeTracker(): TimeTracker
184
    {
185
        return GeneralUtility::makeInstance(TimeTracker::class);
186
    }
187
}
188