Issues (153)

Classes/Controller/AbstractController.php (2 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * @license GPLv3, http://www.gnu.org/copyleft/gpl.html
5
 * @copyright Metaways Infosystems GmbH, 2013
6
 * @copyright Aimeos (aimeos.org), 2014-2017
7
 * @package TYPO3
8
 */
9
10
namespace Aimeos\Aimeos\Controller;
11
12
13
use Aimeos\Aimeos\Base;
14
15
16
/**
17
 * Abstract class with common functionality for all controllers.
18
 *
19
 * @package TYPO3
20
 */
21
abstract class AbstractController
22
    extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
23
    implements \TYPO3\CMS\Extbase\Mvc\Controller\ControllerInterface
24
{
25
    private static $context;
26
    private $contextBE;
27
    private $ceUid;
28
29
30
    /**
31
     * Checks if the backend user is allowed to access the site
32
     *
33
     * @param array $sitePath List of siteid values
34
     * @param mixed $userSiteId Site ID stored in the backend user record
35
     * @return bool True if the user is allowed to access the site
36
     * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException If user doesn't have access
37
     */
38
    protected function checkSite(array $sitePath, $userSiteId) : bool
39
    {
40
        foreach (array_reverse($sitePath) as $siteid) {
41
            if ((string) $userSiteId === (string) $siteid) {
42
                return true;
43
            }
44
        }
45
46
        throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientUserPermissionsException('Access not allowed');
47
    }
48
49
50
    /**
51
     * Returns the context item for the frontend
52
     *
53
     * @param string $templatePath Path for retrieving the template paths used in the view
54
     * @param bool $withView True to add view to context object, false for no view
55
     * @return \Aimeos\MShop\ContextIface Context item
56
     */
57
    protected function context(string $templatePath = 'client/html/templates',
58
        bool $withView = true) : \Aimeos\MShop\ContextIface
59
    {
60
        $config = Base::config((array) $this->settings);
61
62
        if (!isset(self::$context)) {
63
            $context = Base::context($config);
64
            $locale = Base::locale($context, $this->request);
65
            $context->setI18n(Base::i18n([$locale->getLanguageId()], $config->get('i18n', [])));
66
            $context->setLocale($locale);
67
68
            self::$context = $context;
69
        }
70
71
        // Use plugin specific configuration
72
        self::$context->setConfig($config);
73
74
        foreach (self::$context->locale()->getSiteItem()->getConfig() as $key => $value) {
75
            $config->set($key, $value);
76
        }
77
78
        if ($withView === true) {
79
            $langid = self::$context->locale()->getLanguageId();
80
            $paths = Base::aimeos()->getTemplatePaths($templatePath);
81
            $view = Base::view(self::$context, $this->uriBuilder, $paths, $this->request, $langid);
82
83
            self::$context->setView($view);
84
        }
85
86
        return self::$context;
87
    }
88
89
90
    /**
91
     * Returns the context item for backend operations
92
     *
93
     * @param string $templatePath Path for retrieving the template paths used in the view
94
     * @param bool $withView True to add view to context object, false for no view
95
     * @return \Aimeos\MShop\ContextIface Context item
96
     */
97
    protected function contextBackend(string $templatePath = 'admin/jqadm/templates',
98
        bool $withView = true) : \Aimeos\MShop\ContextIface
99
    {
100
        if (!isset($this->contextBE)) {
101
            $lang = 'en';
102
            $site = 'default';
103
104
            $config = Base::config((array) $this->settings);
105
            $context = Base::context($config);
106
107
            if ($this->request->hasArgument('locale') && ($value = $this->request->getArgument('locale')) != '') {
108
                $lang = $value;
109
            } elseif (!in_array($GLOBALS['BE_USER']->uc['lang'] ?? '', ['', 'default'])) {
110
                $lang = $GLOBALS['BE_USER']->uc['lang'];
111
            }
112
113
            if ($this->request->hasArgument('site') && ($value = $this->request->getArgument('site')) != '') {
114
                $site = $value;
115
            } elseif (isset($GLOBALS['BE_USER']->user['siteid']) && $GLOBALS['BE_USER']->user['siteid'] != '') {
116
                $siteManager = \Aimeos\MShop::create($context, 'locale/site');
117
                $siteId = current(array_reverse(explode('.', trim($GLOBALS['BE_USER']->user['siteid'], '.'))));
118
                $site = ($siteId ? $siteManager->get($siteId)->getCode() : 'default');
119
            }
120
121
            $locale = Base::getLocaleBackend($context, $site);
122
            $context->setLocale($locale);
123
124
            if (isset($GLOBALS['BE_USER']->user['siteid']) && $GLOBALS['BE_USER']->user['siteid'] != '') {
125
                $this->checkSite($locale->getSitePath(), $GLOBALS['BE_USER']->user['siteid']);
126
            }
127
128
            $i18n = Base::i18n([$lang, 'en'], $config->get('i18n', []));
129
            $context->setI18n($i18n);
130
131
            foreach ($locale->getSiteItem()->getConfig() as $key => $value) {
132
                $config->set($key, $value);
133
            }
134
135
            if ($withView) {
136
                $paths = Base::aimeos()->getTemplatePaths($templatePath);
137
                $view = Base::view($context, $this->uriBuilder, $paths, $this->request, $lang);
138
                $context->setView($view);
139
            }
140
141
            $this->contextBE = $context;
142
        }
143
144
        return $this->contextBE;
145
    }
146
147
148
    /**
149
     * Returns the output of the client and adds the header.
150
     *
151
     * @param \Aimeos\Client\Html\Iface $client Html client object (no type hint to prevent reflection)
152
     * @return string HTML code for inserting into the HTML body
153
     */
154
    protected function getClientOutput(\Aimeos\Client\Html\Iface $client)
155
    {
156
        $uid = $this->ceUid;
157
        if ($GLOBALS['TYPO3_REQUEST'] instanceof \Psr\Http\Message\ServerRequestInterface
158
            && empty($GLOBALS['TYPO3_REQUEST']->getAttribute('routing')) === false
159
        ) {
160
            $uid .= '-' . $GLOBALS['TYPO3_REQUEST']->getAttribute('routing')->getPageType();
161
        }
162
163
        $client->setView($this->context()->view())->init();
164
        $header = (string) $client->header($uid);
165
        $html = (string) $client->body($uid);
166
167
        if (!isset($this->responseFactory)) // TYPO3 10
168
        {
169
            $this->response->addAdditionalHeaderData($header);
0 ignored issues
show
The property response does not exist on Aimeos\Aimeos\Controller\AbstractController. Did you mean responseFactory?
Loading history...
170
            return $html;
171
        }
172
173
        $renderer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Page\PageRenderer::class);
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...
174
        $renderer->addHeaderData($header);
175
176
        return $this->responseFactory->createResponse()
177
            ->withAddedHeader('Content-Type', 'text/html; charset=utf-8')
178
            ->withBody($this->streamFactory->createStream($html));
179
    }
180
181
182
    /**
183
     * Initializes the object before the real action is called.
184
     */
185
    protected function initializeAction()
186
    {
187
        $this->uriBuilder->setArgumentPrefix('ai');
188
189
        $ce = $this->configurationManager->getContentObject();
190
        $this->ceUid = $ce->data['uid'] ?? null;
191
    }
192
}
193