[CLEANUP] Use Permission constants consistently
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Configuration / BackendConfigurationManager.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Configuration;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
19 use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
20 use TYPO3\CMS\Core\Database\QueryGenerator;
21 use TYPO3\CMS\Core\Type\Bitmask\Permission;
22 use TYPO3\CMS\Core\TypoScript\TemplateService;
23 use TYPO3\CMS\Core\Utility\ArrayUtility;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Extbase\Mvc\Web\BackendRequestHandler;
26 use TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler;
27 use TYPO3\CMS\Frontend\Page\PageRepository;
28
29 /**
30 * A general purpose configuration manager used in backend mode.
31 */
32 class BackendConfigurationManager extends AbstractConfigurationManager
33 {
34 /**
35 * @var array
36 */
37 protected $typoScriptSetupCache = [];
38
39 /**
40 * stores the current page ID
41 * @var int
42 */
43 protected $currentPageId;
44
45 /**
46 * Returns TypoScript Setup array from current Environment.
47 *
48 * @return array the raw TypoScript setup
49 */
50 public function getTypoScriptSetup()
51 {
52 $pageId = $this->getCurrentPageId();
53
54 if (!array_key_exists($pageId, $this->typoScriptSetupCache)) {
55 /** @var $template TemplateService */
56 $template = GeneralUtility::makeInstance(TemplateService::class);
57 // do not log time-performance information
58 $template->tt_track = 0;
59 // Explicitly trigger processing of extension static files
60 $template->setProcessExtensionStatics(true);
61 $template->init();
62 // Get the root line
63 $rootline = [];
64 if ($pageId > 0) {
65 /** @var $sysPage PageRepository */
66 $sysPage = GeneralUtility::makeInstance(PageRepository::class);
67 // Get the rootline for the current page
68 $rootline = $sysPage->getRootLine($pageId, '', true);
69 }
70 // This generates the constants/config + hierarchy info for the template.
71 $template->runThroughTemplates($rootline, 0);
72 $template->generateConfig();
73 $this->typoScriptSetupCache[$pageId] = $template->setup;
74 }
75 return $this->typoScriptSetupCache[$pageId];
76 }
77
78 /**
79 * Returns the TypoScript configuration found in module.tx_yourextension_yourmodule
80 * merged with the global configuration of your extension from module.tx_yourextension
81 *
82 * @param string $extensionName
83 * @param string $pluginName in BE mode this is actually the module signature. But we're using it just like the plugin name in FE
84 * @return array
85 */
86 protected function getPluginConfiguration($extensionName, $pluginName = null)
87 {
88 $setup = $this->getTypoScriptSetup();
89 $pluginConfiguration = [];
90 if (is_array($setup['module.']['tx_' . strtolower($extensionName) . '.'] ?? false)) {
91 $pluginConfiguration = $this->typoScriptService->convertTypoScriptArrayToPlainArray($setup['module.']['tx_' . strtolower($extensionName) . '.']);
92 }
93 if ($pluginName !== null) {
94 $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 return $pluginConfiguration;
101 }
102
103 /**
104 * Returns the configured controller/action pairs of the specified module in the format
105 * array(
106 * 'Controller1' => array('action1', 'action2'),
107 * 'Controller2' => array('action3', 'action4')
108 * )
109 *
110 * @param string $extensionName
111 * @param string $pluginName in BE mode this is actually the module signature. But we're using it just like the plugin name in FE
112 * @return array
113 */
114 protected function getSwitchableControllerActions($extensionName, $pluginName)
115 {
116 $switchableControllerActions = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['modules'][$pluginName]['controllers'] ?? false;
117 if (!is_array($switchableControllerActions)) {
118 $switchableControllerActions = [];
119 }
120 return $switchableControllerActions;
121 }
122
123 /**
124 * Returns the page uid of the current page.
125 * If no page is selected, we'll return the uid of the first root page.
126 *
127 * @return int current page id. If no page is selected current root page id is returned
128 */
129 protected function getCurrentPageId()
130 {
131 if ($this->currentPageId !== null) {
132 return $this->currentPageId;
133 }
134
135 $this->currentPageId = $this->getCurrentPageIdFromGetPostData() ?: $this->getCurrentPageIdFromCurrentSiteRoot();
136 $this->currentPageId = $this->currentPageId ?: $this->getCurrentPageIdFromRootTemplate();
137 $this->currentPageId = $this->currentPageId ?: self::DEFAULT_BACKEND_STORAGE_PID;
138
139 return $this->currentPageId;
140 }
141
142 /**
143 * Gets the current page ID from the GET/POST data.
144 *
145 * @return int the page UID, will be 0 if none has been set
146 */
147 protected function getCurrentPageIdFromGetPostData()
148 {
149 return (int)GeneralUtility::_GP('id');
150 }
151
152 /**
153 * Gets the current page ID from the first site root in tree.
154 *
155 * @return int the page UID, will be 0 if none has been set
156 */
157 protected function getCurrentPageIdFromCurrentSiteRoot()
158 {
159 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
160 ->getQueryBuilderForTable('pages');
161
162 $queryBuilder
163 ->getRestrictions()
164 ->removeAll()
165 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
166 ->add(GeneralUtility::makeInstance(HiddenRestriction::class));
167
168 $rootPage = $queryBuilder
169 ->select('uid')
170 ->from('pages')
171 ->where(
172 $queryBuilder->expr()->eq('is_siteroot', $queryBuilder->createNamedParameter(1, \PDO::PARAM_INT))
173 )
174 ->orderBy('sorting')
175 ->execute()
176 ->fetch();
177
178 if (empty($rootPage)) {
179 return 0;
180 }
181
182 return (int)$rootPage['uid'];
183 }
184
185 /**
186 * Gets the current page ID from the first created root template.
187 *
188 * @return int the page UID, will be 0 if none has been set
189 */
190 protected function getCurrentPageIdFromRootTemplate()
191 {
192 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
193 ->getQueryBuilderForTable('sys_template');
194
195 $queryBuilder
196 ->getRestrictions()
197 ->removeAll()
198 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
199 ->add(GeneralUtility::makeInstance(HiddenRestriction::class));
200
201 $rootTemplate = $queryBuilder
202 ->select('pid')
203 ->from('sys_template')
204 ->where(
205 $queryBuilder->expr()->eq('root', $queryBuilder->createNamedParameter(1, \PDO::PARAM_INT))
206 )
207 ->orderBy('crdate')
208 ->execute()
209 ->fetch();
210
211 if (empty($rootTemplate)) {
212 return 0;
213 }
214
215 return (int)$rootTemplate['pid'];
216 }
217
218 /**
219 * Returns the default backend storage pid
220 *
221 * @return string
222 */
223 public function getDefaultBackendStoragePid()
224 {
225 return $this->getCurrentPageId();
226 }
227
228 /**
229 * We need to set some default request handler if the framework configuration
230 * could not be loaded; to make sure Extbase also works in Backend modules
231 * in all contexts.
232 *
233 * @param array $frameworkConfiguration
234 * @return array
235 */
236 protected function getContextSpecificFrameworkConfiguration(array $frameworkConfiguration)
237 {
238 if (!isset($frameworkConfiguration['mvc']['requestHandlers'])) {
239 $frameworkConfiguration['mvc']['requestHandlers'] = [
240 FrontendRequestHandler::class => FrontendRequestHandler::class,
241 BackendRequestHandler::class => BackendRequestHandler::class
242 ];
243 }
244 return $frameworkConfiguration;
245 }
246
247 /**
248 * Returns a comma separated list of storagePid that are below a certain storage pid.
249 *
250 * @param string $storagePid Storage PID to start at; multiple PIDs possible as comma-separated list
251 * @param int $recursionDepth Maximum number of levels to search, 0 to disable recursive lookup
252 * @return string storage PIDs
253 */
254 protected function getRecursiveStoragePids($storagePid, $recursionDepth = 0)
255 {
256 if ($recursionDepth <= 0) {
257 return $storagePid;
258 }
259
260 $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 if ((string)$pids !== '') {
267 $recursiveStoragePids .= $pids . ',';
268 }
269 }
270
271 return rtrim($recursiveStoragePids, ',');
272 }
273
274 /**
275 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
276 */
277 protected function getBackendUser()
278 {
279 return $GLOBALS['BE_USER'];
280 }
281 }