Passed
Push — TYPO3V10 ( 8eb37a...799e46 )
by Felix
11:02
created

Loader::hasBackendUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
namespace Aoe\Restler\System\TYPO3;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2021 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use TYPO3\CMS\Core\Context\Context;
29
use TYPO3\CMS\Core\Core\Bootstrap;
30
use TYPO3\CMS\Core\SingletonInterface;
31
use TYPO3\CMS\Core\Site\Entity\NullSite;
32
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
33
use TYPO3\CMS\Core\TypoScript\TemplateService;
34
use TYPO3\CMS\Core\Utility\GeneralUtility;
35
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
36
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
37
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
38
use TYPO3\CMS\Frontend\Page\PageRepository;
39
use TYPO3\CMS\Frontend\Utility\EidUtility;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Frontend\Utility\EidUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
use LogicException;
41
42
/**
43
 * @package Restler
44
 */
45
class Loader implements SingletonInterface
46
{
47
    /**
48
     * defines, if usage of backend-user is enabled
49
     *
50
     * @var boolean
51
     */
52
    private $isBackendUserInitialized = false;
53
    /**
54
     * defines, if usage of frontend-user is enabled (this is needed, if the eID-script must determine the frontend-user)
55
     *
56
     * @var boolean
57
     */
58
    private $isFrontendUserInitialized = false;
59
    /**
60
     * defines, if frontend-rendering is enabled (this is needed, if the eID-script must render some content-elements or RTE-fields)
61
     *
62
     * @var boolean
63
     */
64
    private $isFrontendRenderingInitialized = false;
65
66
    /**
67
     * @return BackendUserAuthentication
68
     * @throws LogicException
69
     */
70
    public function getBackendUser()
71
    {
72
        if ($this->isBackendUserInitialized === false) {
73
            throw new LogicException('be-user is not initialized - initialize with BE-user with method \'initializeBackendUser\'');
74
        }
75
        return $GLOBALS['BE_USER'];
76
    }
77
78
    /**
79
     * @return FrontendUserAuthentication
80
     * @throws LogicException
81
     */
82
    public function getFrontendUser()
83
    {
84
        if ($this->isFrontendUserInitialized === false) {
85
            throw new LogicException('fe-user is not initialized - initialize with FE-user with method \'initializeFrontendUser\'');
86
        }
87
        return $GLOBALS['TSFE']->fe_user;
88
    }
89
90
    /**
91
     * enable the usage of backend-user
92
     */
93
    public function initializeBackendUser()
94
    {
95
        if ($this->isBackendUserInitialized === true) {
96
            return;
97
        }
98
99
        if (!class_exists(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class)) {
100
            $bootstrapObj = Bootstrap::getInstance();
0 ignored issues
show
Bug introduced by
The method getInstance() does not exist on TYPO3\CMS\Core\Core\Bootstrap. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
            /** @scrutinizer ignore-call */ 
101
            $bootstrapObj = Bootstrap::getInstance();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
            $bootstrapObj->loadExtensionTables(true);
102
            $bootstrapObj->initializeBackendUser();
103
            $bootstrapObj->initializeBackendAuthentication(true);
104
            $bootstrapObj->initializeLanguageObject();
105
106
            $this->isBackendUserInitialized = true;
107
108
        } else if ($this->hasBackendUser()) {
109
            $this->isBackendUserInitialized = true;
110
        }
111
    }
112
113
    /**
114
     * enable the usage of frontend-user
115
     *
116
     * @param integer $pageId
117
     * @param integer $type
118
     */
119
    public function initializeFrontendUser($pageId = 0, $type = 0)
120
    {
121
        if (!class_exists(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class)) {
122
            if ($this->hasActiveFrontendUserSession()) {
123
                // FE-user is already initialized - this can happen when we use/call internal REST-endpoints inside of a normal TYPO3-page
124
                $this->isFrontendUserInitialized = true;
125
            }
126
            if ($this->isFrontendUserInitialized === true) {
127
                return;
128
            }
129
130
            $tsfe = $this->getTsfe($pageId, $type);
131
            $tsfe->initFEUser();
0 ignored issues
show
Bug introduced by
The method initFEUser() does not exist on TYPO3\CMS\Frontend\Contr...criptFrontendController. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
            $tsfe->/** @scrutinizer ignore-call */ 
132
                   initFEUser();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
        }
133
134
        $this->isFrontendUserInitialized = true;
135
    }
136
137
    /**
138
     * enable the frontend-rendering
139
     *
140
     * @param integer $pageId
141
     * @param integer $type
142
     *
143
     * @return void
144
     */
145
    public function initializeFrontendRendering($pageId = 0, $type = 0)
146
    {
147
        if ($this->isFrontendInitialized()) {
148
            // FE is already initialized - this can happen when we use/call internal REST-endpoints inside of a normal TYPO3-page
149
            $this->isFrontendRenderingInitialized = true;
150
        }
151
        if ($this->isFrontendRenderingInitialized === true) {
152
            return;
153
        }
154
155
        if (class_exists(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class)) {
156
            $this->getTsfe($pageId, $type);
157
        } else {
158
            $GLOBALS['TT'] = GeneralUtility::makeInstance(TimeTracker::class);
159
160
            if ($this->isFrontendUserInitialized === false) {
161
                $this->initializeFrontendUser($pageId, $type);
162
            }
163
164
            EidUtility::initTCA();
165
166
            $tsfe = $this->getTsfe($pageId, $type);
167
            $tsfe->determineId();
168
            $tsfe->initTemplate();
0 ignored issues
show
Bug introduced by
The method initTemplate() does not exist on TYPO3\CMS\Frontend\Contr...criptFrontendController. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

168
            $tsfe->/** @scrutinizer ignore-call */ 
169
                   initTemplate();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
169
            $tsfe->getConfigArray();
170
            $tsfe->newCObj();
171
            $tsfe->calculateLinkVars();
0 ignored issues
show
Bug introduced by
The call to TYPO3\CMS\Frontend\Contr...er::calculateLinkVars() has too few arguments starting with queryParams. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

171
            $tsfe->/** @scrutinizer ignore-call */ 
172
                   calculateLinkVars();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
172
        }
173
174
        $this->isFrontendRenderingInitialized = true;
175
    }
176
177
    /**
178
     * Checks if a frontend user is logged in and the session is active.
179
     *
180
     * @return bool
181
     */
182
    protected function isFrontendInitialized()
183
    {
184
        return ($GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController &&
185
            $GLOBALS['TSFE']->tmpl instanceof TemplateService;
186
    }
187
188
    /**
189
     * Checks if a frontend user is logged in and the session is active.
190
     *
191
     * @return bool
192
     */
193
    protected function hasActiveFrontendUserSession()
194
    {
195
        return ($GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController &&
196
            $GLOBALS['TSFE']->fe_user instanceof FrontendUserAuthentication &&
197
            isset($GLOBALS['TSFE']->fe_user->user['uid']);
198
    }
199
200
    /**
201
     * Checks if a backend user is logged in.
202
     *
203
     * @return bool
204
     */
205
    protected function hasBackendUser()
206
    {
207
        return ($GLOBALS['BE_USER'] ?? null) instanceof BackendUserAuthentication;
208
    }
209
210
    /**
211
     * @param integer $pageId
212
     * @param integer $type
213
     * @return TypoScriptFrontendController
214
     */
215
    private function getTsfe($pageId = 0, $type = 0)
216
    {
217
        if (false === array_key_exists('TSFE', $GLOBALS) && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController) {
218
            return $GLOBALS['TSFE'];
219
        }
220
221
        if (class_exists(\TYPO3\CMS\Core\Site\Entity\NullSite::class)) {
222
            $context = GeneralUtility::makeInstance(Context::class);
223
            $nullSite = new NullSite();
224
            $GLOBALS['TSFE'] = GeneralUtility::makeInstance(
225
                TypoScriptFrontendController::class,
226
                $context,
227
                $nullSite,
228
                $nullSite->getDefaultLanguage()
229
            );
230
            $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance(PageRepository::class, $context);
231
            $GLOBALS['TSFE']->tmpl = GeneralUtility::makeInstance(TemplateService::class);
232
        } else {
233
            if ($type > 0) {
234
                $_GET['type'] = $type;
235
            }
236
            $GLOBALS['TSFE'] = GeneralUtility::makeInstance(
237
                TypoScriptFrontendController::class,
238
                $GLOBALS['TYPO3_CONF_VARS'],
239
                $pageId,
240
                $type
241
            );
242
            $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance(PageRepository::class);
243
            $GLOBALS['TSFE']->tmpl = GeneralUtility::makeInstance(TemplateService::class);
244
        }
245
246
        return $GLOBALS['TSFE'];
247
    }
248
}
249