Issues (153)

Classes/Base/Context.php (3 issues)

Labels
1
<?php
2
3
/**
4
 * @license GPLv3, http://www.gnu.org/copyleft/gpl.html
5
 * @copyright Aimeos (aimeos.org), 2019
6
 * @package TYPO3
7
 */
8
9
10
namespace Aimeos\Aimeos\Base;
11
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
0 ignored issues
show
The type TYPO3\CMS\Core\Utility\GeneralUtility 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...
13
use Psr\Http\Message\ServerRequestInterface;
14
15
16
/**
17
 * Aimeos context class
18
 *
19
 * @package TYPO3
20
 */
21
class Context
22
{
23
    private static $context;
24
25
26
    /**
27
     * Returns the current context
28
     *
29
     * @param \Aimeos\Base\Config\Iface Configuration object
0 ignored issues
show
The type Aimeos\Aimeos\Base\Configuration 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...
30
     * @return \Aimeos\MShop\ContextIface Context object
31
     */
32
    public static function get(\Aimeos\Base\Config\Iface $config) : \Aimeos\MShop\ContextIface
33
    {
34
        if (self::$context === null) {
35
            // TYPO3 specifc context with password hasher
36
            $context = new \Aimeos\MShop\Context\Item\Typo3();
37
            $context->setConfig($config);
38
39
            self::addDataBaseManager($context);
40
            self::addFilesystemManager($context);
41
            self::addMessageQueueManager($context);
42
            self::addLogger($context);
43
            self::addCache($context);
44
            self::addMailer($context);
45
            self::addNonce($context);
46
            self::addProcess($context);
47
            self::addSession($context);
48
            self::addHasher($context);
49
            self::addToken($context);
50
            self::addUser($context);
51
            self::addGroups($context);
52
            self::addDateTime($context);
53
54
            self::$context = $context;
55
        }
56
57
        // Use local TS configuration from plugins
58
        self::$context->setConfig($config);
59
60
        return self::$context;
61
    }
62
63
64
    /**
65
     * Adds the cache object to the context
66
     *
67
     * @param \Aimeos\MShop\ContextIface $context Context object including config
68
     * @return \Aimeos\MShop\ContextIface Modified context object
69
     */
70
    protected static function addCache(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface
71
    {
72
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_cache'])
73
            && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_cache']))
74
        ) {
75
            return $fcn($context);
76
        }
77
78
        $cacheName = \Aimeos\Aimeos\Base::getExtConfig('cacheName', 'Typo3');
79
80
        switch ($cacheName) {
81
            case 'None':
82
                $context->config()->set('client/html/basket/cache/enable', false);
83
                $cache = \Aimeos\Base\Cache\Factory::create('None');
84
                break;
85
86
            case 'Typo3':
87
                $manager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class);
88
                $cache = new \Aimeos\Base\Cache\Typo3($manager->getCache('aimeos'));
89
                break;
90
91
            default:
92
                $cache = (new \Aimeos\MAdmin\Cache\Manager\Standard($context))->getCache();
93
        }
94
95
        return $context->setCache($cache);
96
    }
97
98
99
    /**
100
     * Adds the database manager object to the context
101
     *
102
     * @param \Aimeos\MShop\ContextIface $context Context object
103
     * @return \Aimeos\MShop\ContextIface Modified context object
104
     */
105
    protected static function addDatabaseManager(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface
106
    {
107
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_dbm'])
108
            && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_dbm']))
109
        ) {
110
            return $fcn($context);
111
        }
112
113
        $dbm = new \Aimeos\Base\DB\Manager\Standard($context->config()->get('resource', []), 'DBAL');
114
        return $context->setDatabaseManager($dbm);
115
    }
116
117
118
    /**
119
     * Adds the filesystem manager object to the context
120
     *
121
     * @param \Aimeos\MShop\ContextIface $context Context object
122
     * @return \Aimeos\MShop\ContextIface Modified context object
123
     */
124
    protected static function addFilesystemManager(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface
125
    {
126
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_fsm'])
127
            && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_fsm']))
128
        ) {
129
            return $fcn($context);
130
        }
131
132
        $fsm = new \Aimeos\Base\Filesystem\Manager\Standard($context->config()->get('resource', []));
133
        return $context->setFilesystemManager($fsm);
134
    }
135
136
137
    /**
138
     * Adds the password hasher object to the context
139
     *
140
     * @param \Aimeos\MShop\ContextIface $context Context object
141
     * @return \Aimeos\MShop\ContextIface Modified context object
142
     */
143
    protected static function addHasher(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface
144
    {
145
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_hasher'])
146
            && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_hasher']))
147
        ) {
148
            return $fcn($context);
149
        }
150
151
        $factory = GeneralUtility::makeInstance('TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory');
152
        return $context->setPassword(new \Aimeos\Base\Password\Typo3($factory->getDefaultHashInstance('FE')));
153
    }
154
155
156
    /**
157
     * Adds the logger object to the context
158
     *
159
     * @param \Aimeos\MShop\ContextIface $context Context object
160
     * @return \Aimeos\MShop\ContextIface Modified context object
161
     */
162
    protected static function addLogger(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface
163
    {
164
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_logger'])
165
            && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_logger']))
166
        ) {
167
            return $fcn($context);
168
        }
169
170
        return $context->setLogger(\Aimeos\MAdmin::create($context, 'log'));
0 ignored issues
show
Aimeos\MAdmin::create($context, 'log') of type Aimeos\MShop\Common\Manager\Iface is incompatible with the type Aimeos\Base\Logger\Iface expected by parameter $logger of Aimeos\MShop\ContextIface::setLogger(). ( Ignorable by Annotation )

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

170
        return $context->setLogger(/** @scrutinizer ignore-type */ \Aimeos\MAdmin::create($context, 'log'));
Loading history...
171
    }
172
173
174
    /**
175
     * Adds the mailer object to the context
176
     *
177
     * @param \Aimeos\MShop\ContextIface $context Context object
178
     * @return \Aimeos\MShop\ContextIface Modified context object
179
     */
180
    protected static function addMailer(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface
181
    {
182
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_mailer'])
183
            && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_mailer']))
184
        ) {
185
            return $fcn($context);
186
        }
187
188
        return $context->setMail(new \Aimeos\Base\Mail\Typo3(function() {
189
            return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);
190
        }));
191
    }
192
193
194
    /**
195
     * Adds the message queue manager object to the context
196
     *
197
     * @param \Aimeos\MShop\ContextIface $context Context object
198
     * @return \Aimeos\MShop\ContextIface Modified context object
199
     */
200
    protected static function addMessageQueueManager(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface
201
    {
202
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_mqueue'])
203
            && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_mqueue']))
204
        ) {
205
            return $fcn($context);
206
        }
207
208
        $mqm = new \Aimeos\Base\MQueue\Manager\Standard($context->config()->get('resource', []));
209
        return $context->setMessageQueueManager($mqm);
210
    }
211
212
213
    /**
214
     * Adds the nonce value for inline JS to the context
215
     *
216
     * @param \Aimeos\MShop\ContextIface $context Context object
217
     * @return \Aimeos\MShop\ContextIface Modified context object
218
     */
219
    protected static function addNonce(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface
220
    {
221
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_nounce'])
222
            && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_nounce']))
223
        ) {
224
            return $fcn($context);
225
        }
226
227
        return $context->setNonce(base64_encode(random_bytes(16)));
228
    }
229
230
231
    /**
232
     * Adds the process object to the context
233
     *
234
     * @param \Aimeos\MShop\ContextIface $context Context object
235
     * @return \Aimeos\MShop\ContextIface Modified context object
236
     */
237
    protected static function addProcess(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface
238
    {
239
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_process'])
240
            && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_process']))
241
        ) {
242
            return $fcn($context);
243
        }
244
245
        $process = new \Aimeos\Base\Process\Pcntl(\Aimeos\Aimeos\Base::getExtConfig('pcntlMax', 4));
246
247
        // Reset before child processes are spawned to avoid lost DB connections afterwards
248
        if (method_exists('\TYPO3\CMS\Core\Database\ConnectionPool', 'resetConnections') === false
249
            || $process->isAvailable() === false
250
        ) {
251
            $process = new \Aimeos\Base\Process\None();
252
        }
253
254
        return $context->setProcess($process);
255
    }
256
257
258
    /**
259
     * Adds the session object to the context
260
     *
261
     * @param \Aimeos\MShop\ContextIface $context Context object
262
     * @return \Aimeos\MShop\ContextIface Modified context object
263
     */
264
    protected static function addSession(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface
265
    {
266
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_session'])
267
            && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_session']))
268
        ) {
269
            return $fcn($context);
270
        }
271
272
        $class = \TYPO3\CMS\Core\Authentication\CommandLineUserAuthentication::class;
273
274
        if (isset($GLOBALS['TSFE']->fe_user)) {
275
            $session = new \Aimeos\Base\Session\Typo3($GLOBALS['TSFE']->fe_user);
276
        } elseif (isset($GLOBALS['BE_USER']) && !($GLOBALS['BE_USER'] instanceof $class)) {
277
            $session = new \Aimeos\Base\Session\Typo3($GLOBALS['BE_USER']);
278
        } else {
279
            $session = new \Aimeos\Base\Session\None();
280
        }
281
282
        return $context->setSession($session);
283
    }
284
285
286
    /**
287
     * Adds the session token to the context
288
     *
289
     * @param \Aimeos\MShop\ContextIface $context Context object
290
     * @return \Aimeos\MShop\ContextIface Modified context object
291
     */
292
    protected static function addToken(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface
293
    {
294
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_token'])
295
            && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_token']))
296
        ) {
297
            return $fcn($context);
298
        }
299
300
        $session = $context->session();
301
302
        if (($token = $session->get('token')) === null) {
303
            $token = $GLOBALS['TSFE']->fe_user->id ?? md5(microtime(true) . getmypid());
304
            $session->set('token', $token);
305
        }
306
307
        return $context->setToken($token);
308
    }
309
310
311
    /**
312
     * Adds the user ID and editor name to the context
313
     *
314
     * @param \Aimeos\MShop\ContextIface $context Context object
315
     * @return \Aimeos\MShop\ContextIface Modified context object
316
     */
317
    protected static function addUser(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface
318
    {
319
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_user'])
320
            && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_user']))
321
        ) {
322
            return $fcn($context);
323
        }
324
325
        $appType = null;
326
        $t3context = GeneralUtility::makeInstance('TYPO3\CMS\Core\Context\Context');
327
328
        if (($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface) {
329
            $appType = \TYPO3\CMS\Core\Http\ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST']);
330
        }
331
332
        if ($appType && $appType->isFrontend() && $t3context->getPropertyFromAspect('frontend.user', 'isLoggedIn')) {
333
            $context->setUserId($GLOBALS['TSFE']->fe_user->user[$GLOBALS['TSFE']->fe_user->userid_column]);
334
            $context->setEditor((string) $GLOBALS['TSFE']->fe_user->user['username']);
335
        } elseif ($appType && $appType->isBackend() && isset($GLOBALS['BE_USER']->user['username'])) {
336
            $context->setEditor((string) $GLOBALS['BE_USER']->user['username']);
337
        } else {
338
            $context->setEditor((string) GeneralUtility::getIndpEnv('REMOTE_ADDR'));
339
        }
340
341
        return $context;
342
    }
343
344
345
    /**
346
     * Adds the group IDs to the context
347
     *
348
     * @param \Aimeos\MShop\ContextIface $context Context object
349
     * @return \Aimeos\MShop\ContextIface Modified context object
350
     */
351
    protected static function addGroups(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface
352
    {
353
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_groups'])
354
            && is_callable(($fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context_groups']))
355
        ) {
356
            return $fcn($context);
357
        }
358
359
        $t3context = GeneralUtility::makeInstance('TYPO3\CMS\Core\Context\Context');
360
361
        $appType = null;
362
        if (($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface) {
363
            $appType = \TYPO3\CMS\Core\Http\ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST']);
364
        }
365
366
        if ($appType && $appType->isFrontend() && $t3context->getPropertyFromAspect('frontend.user', 'isLoggedIn')) {
367
            $ids = GeneralUtility::trimExplode(',', $GLOBALS['TSFE']->fe_user->user['usergroup']);
368
            $context->setGroupIds($ids);
369
        } elseif ($appType && $appType->isBackend() && $GLOBALS['BE_USER']->userGroups) {
370
            $ids = array_keys($GLOBALS['BE_USER']->userGroups);
371
            $context->setGroupIds($ids);
372
        }
373
374
        return $context;
375
    }
376
377
378
    /**
379
     * Adds the frontend date time to the context
380
     *
381
     * @param \Aimeos\MShop\ContextIface $context Context object including config
382
     * @return \Aimeos\MShop\ContextIface Modified context object
383
     */
384
    protected static function addDateTime(\Aimeos\MShop\ContextIface $context) : \Aimeos\MShop\ContextIface
385
    {
386
        $appType = null;
387
        if (($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface) {
388
            $appType = \TYPO3\CMS\Core\Http\ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST']);
389
        }
390
391
        if ($appType && $appType->isFrontend() && isset($GLOBALS['BE_USER']->adminPanel)
392
            && class_exists('TYPO3\\CMS\\Adminpanel\\Service\\ConfigurationService')) {
393
            $service = GeneralUtility::makeInstance('TYPO3\\CMS\\Adminpanel\\Service\\ConfigurationService');
394
            $tstamp = strtotime($service->getConfigurationOption('preview', 'simulateDate'));
395
396
            if (!empty($tstamp)) {
397
                $context->setDateTime(date('Y-m-d H:i:s', $tstamp));
398
            }
399
        }
400
401
        return $context;
402
    }
403
}
404