BackendConfigurationManager.php 9.93 KB
Newer Older
1
<?php
Thomas Maroschik's avatar
Thomas Maroschik committed
2
3
namespace TYPO3\CMS\Extbase\Configuration;

4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * 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.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
16

17
18
19
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
20
use TYPO3\CMS\Core\Database\QueryGenerator;
21
use TYPO3\CMS\Core\Type\Bitmask\Permission;
22
23
use TYPO3\CMS\Core\TypoScript\TemplateService;
use TYPO3\CMS\Core\Utility\ArrayUtility;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26
27
use TYPO3\CMS\Extbase\Mvc\Web\BackendRequestHandler;
use TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler;
use TYPO3\CMS\Frontend\Page\PageRepository;
28

29
30
31
/**
 * A general purpose configuration manager used in backend mode.
 */
32
class BackendConfigurationManager extends AbstractConfigurationManager
33
34
35
36
{
    /**
     * @var array
     */
37
    protected $typoScriptSetupCache = [];
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

    /**
     * stores the current page ID
     * @var int
     */
    protected $currentPageId;

    /**
     * Returns TypoScript Setup array from current Environment.
     *
     * @return array the raw TypoScript setup
     */
    public function getTypoScriptSetup()
    {
        $pageId = $this->getCurrentPageId();

        if (!array_key_exists($pageId, $this->typoScriptSetupCache)) {
55
56
            /** @var $template TemplateService */
            $template = GeneralUtility::makeInstance(TemplateService::class);
57
58
59
60
61
62
            // do not log time-performance information
            $template->tt_track = 0;
            // Explicitly trigger processing of extension static files
            $template->setProcessExtensionStatics(true);
            $template->init();
            // Get the root line
63
            $rootline = [];
64
            if ($pageId > 0) {
65
66
                /** @var $sysPage PageRepository */
                $sysPage = GeneralUtility::makeInstance(PageRepository::class);
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
                // Get the rootline for the current page
                $rootline = $sysPage->getRootLine($pageId, '', true);
            }
            // This generates the constants/config + hierarchy info for the template.
            $template->runThroughTemplates($rootline, 0);
            $template->generateConfig();
            $this->typoScriptSetupCache[$pageId] = $template->setup;
        }
        return $this->typoScriptSetupCache[$pageId];
    }

    /**
     * Returns the TypoScript configuration found in module.tx_yourextension_yourmodule
     * merged with the global configuration of your extension from module.tx_yourextension
     *
     * @param string $extensionName
     * @param string $pluginName in BE mode this is actually the module signature. But we're using it just like the plugin name in FE
     * @return array
     */
    protected function getPluginConfiguration($extensionName, $pluginName = null)
    {
        $setup = $this->getTypoScriptSetup();
89
        $pluginConfiguration = [];
90
        if (is_array($setup['module.']['tx_' . strtolower($extensionName) . '.'] ?? false)) {
91
92
93
94
            $pluginConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['module.']['tx_' . strtolower($extensionName) . '.']);
        }
        if ($pluginName !== null) {
            $pluginSignature = strtolower($extensionName . '_' . $pluginName);
95
            if (is_array($setup['module.']['tx_' . $pluginSignature . '.'] ?? false)) {
96
                $overruleConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['module.']['tx_' . $pluginSignature . '.']);
97
                ArrayUtility::mergeRecursiveWithOverrule($pluginConfiguration, $overruleConfiguration);
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
            }
        }
        return $pluginConfiguration;
    }

    /**
     * Returns the configured controller/action pairs of the specified module in the format
     * array(
     * 'Controller1' => array('action1', 'action2'),
     * 'Controller2' => array('action3', 'action4')
     * )
     *
     * @param string $extensionName
     * @param string $pluginName in BE mode this is actually the module signature. But we're using it just like the plugin name in FE
     * @return array
     */
    protected function getSwitchableControllerActions($extensionName, $pluginName)
    {
116
        $switchableControllerActions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$pluginName]['controllers'] ?? false;
117
        if (!is_array($switchableControllerActions)) {
118
            $switchableControllerActions = [];
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
        }
        return $switchableControllerActions;
    }

    /**
     * Returns the page uid of the current page.
     * If no page is selected, we'll return the uid of the first root page.
     *
     * @return int current page id. If no page is selected current root page id is returned
     */
    protected function getCurrentPageId()
    {
        if ($this->currentPageId !== null) {
            return $this->currentPageId;
        }

        $this->currentPageId = $this->getCurrentPageIdFromGetPostData() ?: $this->getCurrentPageIdFromCurrentSiteRoot();
        $this->currentPageId = $this->currentPageId ?: $this->getCurrentPageIdFromRootTemplate();
        $this->currentPageId = $this->currentPageId ?: self::DEFAULT_BACKEND_STORAGE_PID;

        return $this->currentPageId;
    }

    /**
     * Gets the current page ID from the GET/POST data.
     *
     * @return int the page UID, will be 0 if none has been set
     */
    protected function getCurrentPageIdFromGetPostData()
    {
149
        return (int)GeneralUtility::_GP('id');
150
151
152
153
154
155
156
157
158
    }

    /**
     * Gets the current page ID from the first site root in tree.
     *
     * @return int the page UID, will be 0 if none has been set
     */
    protected function getCurrentPageIdFromCurrentSiteRoot()
    {
159
160
161
162
163
164
165
166
167
168
169
170
171
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
            ->getQueryBuilderForTable('pages');

        $queryBuilder
            ->getRestrictions()
            ->removeAll()
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
            ->add(GeneralUtility::makeInstance(HiddenRestriction::class));

        $rootPage = $queryBuilder
            ->select('uid')
            ->from('pages')
            ->where(
172
                $queryBuilder->expr()->eq('is_siteroot', $queryBuilder->createNamedParameter(1, \PDO::PARAM_INT))
173
174
175
176
177
            )
            ->orderBy('sorting')
            ->execute()
            ->fetch();

178
179
180
181
182
183
184
185
186
187
188
189
190
191
        if (empty($rootPage)) {
            return 0;
        }

        return (int)$rootPage['uid'];
    }

    /**
     * Gets the current page ID from the first created root template.
     *
     * @return int the page UID, will be 0 if none has been set
     */
    protected function getCurrentPageIdFromRootTemplate()
    {
192
193
194
195
196
197
198
199
200
201
202
203
204
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
            ->getQueryBuilderForTable('sys_template');

        $queryBuilder
            ->getRestrictions()
            ->removeAll()
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
            ->add(GeneralUtility::makeInstance(HiddenRestriction::class));

        $rootTemplate = $queryBuilder
            ->select('pid')
            ->from('sys_template')
            ->where(
205
                $queryBuilder->expr()->eq('root', $queryBuilder->createNamedParameter(1, \PDO::PARAM_INT))
206
207
208
209
210
            )
            ->orderBy('crdate')
            ->execute()
            ->fetch();

211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
        if (empty($rootTemplate)) {
            return 0;
        }

        return (int)$rootTemplate['pid'];
    }

    /**
     * Returns the default backend storage pid
     *
     * @return string
     */
    public function getDefaultBackendStoragePid()
    {
        return $this->getCurrentPageId();
    }

    /**
     * We need to set some default request handler if the framework configuration
     * could not be loaded; to make sure Extbase also works in Backend modules
     * in all contexts.
     *
     * @param array $frameworkConfiguration
     * @return array
     */
    protected function getContextSpecificFrameworkConfiguration(array $frameworkConfiguration)
    {
        if (!isset($frameworkConfiguration['mvc']['requestHandlers'])) {
239
            $frameworkConfiguration['mvc']['requestHandlers'] = [
240
241
                FrontendRequestHandler::class => FrontendRequestHandler::class,
                BackendRequestHandler::class => BackendRequestHandler::class
242
            ];
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
        }
        return $frameworkConfiguration;
    }

    /**
     * Returns a comma separated list of storagePid that are below a certain storage pid.
     *
     * @param string $storagePid Storage PID to start at; multiple PIDs possible as comma-separated list
     * @param int $recursionDepth Maximum number of levels to search, 0 to disable recursive lookup
     * @return string storage PIDs
     */
    protected function getRecursiveStoragePids($storagePid, $recursionDepth = 0)
    {
        if ($recursionDepth <= 0) {
            return $storagePid;
        }

        $recursiveStoragePids = '';
261
        $storagePids = GeneralUtility::intExplode(',', $storagePid);
262
        $permsClause = $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW);
263
        $queryGenerator = GeneralUtility::makeInstance(QueryGenerator::class);
264
        foreach ($storagePids as $startPid) {
265
            $pids = $queryGenerator->getTreeList($startPid, $recursionDepth, 0, $permsClause);
266
267
268
269
270
271
272
            if ((string)$pids !== '') {
                $recursiveStoragePids .= $pids . ',';
            }
        }

        return rtrim($recursiveStoragePids, ',');
    }
273
274
275
276
277
278
279
280

    /**
     * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
     */
    protected function getBackendUser()
    {
        return $GLOBALS['BE_USER'];
    }
281
}