[TASK] Use LanguageService::$moduleLabels as fallback in ModuleLoader
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Module / ModuleLoader.php
1 <?php
2 namespace TYPO3\CMS\Backend\Module;
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\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Lang\LanguageService;
21
22 /**
23 * This document provides a class that loads the modules for the TYPO3 interface.
24 *
25 * Load Backend Interface modules
26 *
27 * Typically instantiated like this:
28 * $this->loadModules = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Module\ModuleLoader::class);
29 * $this->loadModules->load($TBE_MODULES);
30 * @internal
31 */
32 class ModuleLoader
33 {
34 /**
35 * After the init() function this array will contain the structure of available modules for the backend user.
36 *
37 * @var array
38 */
39 public $modules = [];
40
41 /**
42 * This array will hold the elements that should go into the select-list of modules for groups...
43 *
44 * @var array
45 */
46 public $modListGroup = [];
47
48 /**
49 * This array will hold the elements that should go into the select-list of modules for users...
50 *
51 * @var array
52 */
53 public $modListUser = [];
54
55 /**
56 * The backend user for use internally
57 *
58 * @var \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
59 */
60 public $BE_USER;
61
62 /**
63 * If set TRUE, workspace "permissions" will be observed so non-allowed modules will not be included in the array of modules.
64 *
65 * @var bool
66 */
67 public $observeWorkspaces = false;
68
69 /**
70 * Contains the registered navigation components
71 *
72 * @var array
73 */
74 protected $navigationComponents = [];
75
76 /**
77 * Labels for the modules
78 * @var array
79 */
80 protected $moduleLabels = [];
81
82 /**
83 * Init.
84 * The outcome of the load() function will be a $this->modules array populated with the backend module structure available to the BE_USER
85 * Further the global var $LANG will have labels and images for the modules loaded in an internal array.
86 *
87 * @param array $modulesArray Should be the global var $TBE_MODULES, $BE_USER can optionally be set to an alternative Backend user object than the global var $BE_USER (which is the currently logged in user)
88 * @param BackendUserAuthentication $beUser Optional backend user object to use. If not set, the global BE_USER object is used.
89 * @return void
90 */
91 public function load($modulesArray, BackendUserAuthentication $beUser = null)
92 {
93 // Setting the backend user for use internally
94 $this->BE_USER = $beUser ?: $GLOBALS['BE_USER'];
95
96 // Unset the array for calling backend modules based on external backend module dispatchers in typo3/index.php
97 unset($modulesArray['_configuration']);
98 $this->navigationComponents = $modulesArray['_navigationComponents'];
99 unset($modulesArray['_navigationComponents']);
100 $mainModules = $this->parseModulesArray($modulesArray);
101
102 // Traverses the module setup and creates the internal array $this->modules
103 foreach ($mainModules as $mainModuleName => $subModules) {
104 $mainModuleConfiguration = $this->checkMod($mainModuleName);
105 // If $mainModuleConfiguration is not set (FALSE) there is no access to the module !(?)
106 if (is_array($mainModuleConfiguration)) {
107 $this->modules[$mainModuleName] = $mainModuleConfiguration;
108 // Load the submodules
109 if (is_array($subModules)) {
110 foreach ($subModules as $subModuleName) {
111 $subModuleConfiguration = $this->checkMod($mainModuleName . '_' . $subModuleName);
112 if (is_array($subModuleConfiguration)) {
113 $this->modules[$mainModuleName]['sub'][$subModuleName] = $subModuleConfiguration;
114 }
115 }
116 }
117 } elseif ($mainModuleConfiguration !== false) {
118 // Although the configuration was not found, still check if there are submodules
119 // This must be done in order to fill out the select-lists for modules correctly!!
120 if (is_array($subModules)) {
121 foreach ($subModules as $subModuleName) {
122 $this->checkMod($mainModuleName . '_' . $subModuleName);
123 }
124 }
125 }
126 }
127 }
128
129 /**
130 * Here we check for the module.
131 *
132 * Return values:
133 * 'notFound': If the module was not found in the path (no "conf.php" file)
134 * FALSE: If no access to the module (access check failed)
135 * array(): Configuration array, in case a valid module where access IS granted exists.
136 *
137 * @param string $name Module name
138 * @return string|bool|array See description of function
139 */
140 public function checkMod($name)
141 {
142 // Check for own way of configuring module
143 if (is_array($GLOBALS['TBE_MODULES']['_configuration'][$name]['configureModuleFunction'])) {
144 $obj = $GLOBALS['TBE_MODULES']['_configuration'][$name]['configureModuleFunction'];
145 if (is_callable($obj)) {
146 $MCONF = call_user_func($obj, $name);
147 if ($this->checkModAccess($name, $MCONF) !== true) {
148 return false;
149 }
150 $this->addLabelsForModule($name, $MCONF['labels']);
151 return $MCONF;
152 }
153 }
154
155 // merge configuration and labels into one array
156 $setupInformation = $this->getModuleSetupInformation($name);
157
158 // clean up the configuration part
159 if (empty($setupInformation['configuration'])) {
160 return 'notFound';
161 }
162 if (
163 $setupInformation['configuration']['shy']
164 || !$this->checkModAccess($name, $setupInformation['configuration'])
165 || !$this->checkModWorkspace($name, $setupInformation['configuration'])
166 ) {
167 return false;
168 }
169 $finalModuleConfiguration = $setupInformation['configuration'];
170 $finalModuleConfiguration['name'] = $name;
171 // Language processing. This will add module labels and image reference to the internal ->moduleLabels array of the LANG object.
172 $this->addLabelsForModule($name, ($finalModuleConfiguration['labels'] ?? $setupInformation['labels']));
173
174 // Default script setup
175 if ($setupInformation['configuration']['script'] === '_DISPATCH' || isset($setupInformation['configuration']['routeTarget'])) {
176 if ($setupInformation['configuration']['extbase']) {
177 $finalModuleConfiguration['script'] = BackendUtility::getModuleUrl('Tx_' . $name);
178 } else {
179 // just go through BackendModuleRequestHandler where the routeTarget is resolved
180 $finalModuleConfiguration['script'] = BackendUtility::getModuleUrl($name);
181 }
182 } else {
183 $finalModuleConfiguration['script'] = BackendUtility::getModuleUrl('dummy');
184 }
185
186 if (!empty($setupInformation['configuration']['navigationFrameModule'])) {
187 $finalModuleConfiguration['navFrameScript'] = BackendUtility::getModuleUrl(
188 $setupInformation['configuration']['navigationFrameModule'],
189 !empty($setupInformation['configuration']['navigationFrameModuleParameters'])
190 ? $setupInformation['configuration']['navigationFrameModuleParameters']
191 : []
192 );
193 }
194
195 // Check if this is a submodule
196 $mainModule = '';
197 if (strpos($name, '_') !== false) {
198 list($mainModule, ) = explode('_', $name, 2);
199 }
200
201 // check if there is a navigation component (like the pagetree)
202 if (is_array($this->navigationComponents[$name])) {
203 $finalModuleConfiguration['navigationComponentId'] = $this->navigationComponents[$name]['componentId'];
204 // navigation component can be overridden by the main module component
205 } elseif ($mainModule && is_array($this->navigationComponents[$mainModule]) && $setupInformation['configuration']['inheritNavigationComponentFromMainModule'] !== false) {
206 $finalModuleConfiguration['navigationComponentId'] = $this->navigationComponents[$mainModule]['componentId'];
207 }
208 return $finalModuleConfiguration;
209 }
210
211 /**
212 * fetches the conf.php file of a certain module, and also merges that with
213 * some additional configuration
214 *
215 * @param string $moduleName the combined name of the module, can be "web", "web_info", or "tools_log"
216 * @return array an array with subarrays, named "configuration" (aka $MCONF), "labels" (previously known as $MLANG) and the stripped path
217 */
218 protected function getModuleSetupInformation($moduleName)
219 {
220 $moduleSetupInformation = [
221 'configuration' => [],
222 'labels' => []
223 ];
224
225 $moduleConfiguration = !empty($GLOBALS['TBE_MODULES']['_configuration'][$moduleName])
226 ? $GLOBALS['TBE_MODULES']['_configuration'][$moduleName]
227 : null;
228 if ($moduleConfiguration !== null) {
229 // Overlay setup with additional labels
230 if (!empty($moduleConfiguration['labels']) && is_array($moduleConfiguration['labels'])) {
231 if (empty($moduleSetupInformation['labels']['default']) || !is_array($moduleSetupInformation['labels']['default'])) {
232 $moduleSetupInformation['labels']['default'] = $moduleConfiguration['labels'];
233 } else {
234 $moduleSetupInformation['labels']['default'] = array_replace_recursive($moduleSetupInformation['labels']['default'], $moduleConfiguration['labels']);
235 }
236 unset($moduleConfiguration['labels']);
237 }
238 // Overlay setup with additional configuration
239 if (is_array($moduleConfiguration)) {
240 $moduleSetupInformation['configuration'] = array_replace_recursive($moduleSetupInformation['configuration'], $moduleConfiguration);
241 }
242 }
243
244 // Add some default configuration
245 if (!isset($moduleSetupInformation['configuration']['inheritNavigationComponentFromMainModule'])) {
246 $moduleSetupInformation['configuration']['inheritNavigationComponentFromMainModule'] = true;
247 }
248
249 return $moduleSetupInformation;
250 }
251
252 /**
253 * Returns TRUE if the internal BE_USER has access to the module $name with $MCONF (based on security level set for that module)
254 *
255 * @param string $name Module name
256 * @param array $MCONF MCONF array (module configuration array) from the modules conf.php file (contains settings about what access level the module has)
257 * @return bool TRUE if access is granted for $this->BE_USER
258 */
259 public function checkModAccess($name, $MCONF)
260 {
261 if (empty($MCONF['access'])) {
262 return true;
263 }
264 $access = strtolower($MCONF['access']);
265 // Checking if admin-access is required
266 // If admin-permissions is required then return TRUE if user is admin
267 if (strpos($access, 'admin') !== false && $this->BE_USER->isAdmin()) {
268 return true;
269 }
270 // This will add modules to the select-lists of user and groups
271 if (strpos($access, 'user') !== false) {
272 $this->modListUser[] = $name;
273 }
274 if (strpos($access, 'group') !== false) {
275 $this->modListGroup[] = $name;
276 }
277 // This checks if a user is permitted to access the module
278 if ($this->BE_USER->isAdmin() || $this->BE_USER->check('modules', $name)) {
279 return true;
280 }
281 return false;
282 }
283
284 /**
285 * Check if a module is allowed inside the current workspace for be user
286 * Processing happens only if $this->observeWorkspaces is TRUE
287 *
288 * @param string $name Module name (unused)
289 * @param array $MCONF MCONF array (module configuration array) from the modules conf.php file (contains settings about workspace restrictions)
290 * @return bool TRUE if access is granted for $this->BE_USER
291 */
292 public function checkModWorkspace($name, $MCONF)
293 {
294 if (!$this->observeWorkspaces) {
295 return true;
296 }
297 $status = true;
298 if (!empty($MCONF['workspaces'])) {
299 $status = $this->BE_USER->workspace === 0 && GeneralUtility::inList($MCONF['workspaces'], 'online')
300 || $this->BE_USER->workspace === -1 && GeneralUtility::inList($MCONF['workspaces'], 'offline')
301 || $this->BE_USER->workspace > 0 && GeneralUtility::inList($MCONF['workspaces'], 'custom');
302 } elseif ($this->BE_USER->workspace === -99) {
303 $status = false;
304 }
305 return $status;
306 }
307
308 /**
309 * Parses the moduleArray ($TBE_MODULES) into an internally useful structure.
310 * Returns an array where the keys are names of the module and the values may be TRUE (only module) or an array (of submodules)
311 *
312 * @param array $arr ModuleArray ($TBE_MODULES)
313 * @return array Output structure with available modules
314 */
315 public function parseModulesArray($arr)
316 {
317 $theMods = [];
318 if (is_array($arr)) {
319 foreach ($arr as $mod => $subs) {
320 // Clean module name to alphanum
321 $mod = $this->cleanName($mod);
322 if ($mod) {
323 if ($subs) {
324 $subsArr = GeneralUtility::trimExplode(',', $subs);
325 foreach ($subsArr as $subMod) {
326 $subMod = $this->cleanName($subMod);
327 if ($subMod) {
328 $theMods[$mod][] = $subMod;
329 }
330 }
331 } else {
332 $theMods[$mod] = 1;
333 }
334 }
335 }
336 }
337 return $theMods;
338 }
339
340 /**
341 * The $str is cleaned so that it contains alphanumerical characters only.
342 * Module names must only consist of these characters
343 *
344 * @param string $str String to clean up
345 * @return string
346 */
347 public function cleanName($str)
348 {
349 return preg_replace('/[^a-z0-9]/i', '', $str);
350 }
351
352 /**
353 * Registers labels for a module in a unified way.
354 *
355 * Legacy info: This was previously named
356 * - labels->tablabel (now called "shortdescription")
357 * - labels->tabdescr (now called "description")
358 * - tabs->tab (now called "title")
359 *
360 * The LLL information is stored, not the actual translated string.
361 *
362 * @param string $moduleName the name of the module
363 * @param string|array $labels the information about the three labels
364 */
365 public function addLabelsForModule($moduleName, $labels)
366 {
367 // If LOCAL_LANG references are used for labels of the module:
368 if (is_string($labels)) {
369 // Extbase-based modules
370 $this->moduleLabels[$moduleName] = [
371 'shortdescription' => $labels . ':mlang_labels_tablabel',
372 'description' => $labels . ':mlang_labels_tabdescr',
373 'title' => $labels . ':mlang_tabs_tab',
374 ];
375 } elseif (isset($labels['title'])) {
376 // New way, where all labels can be LLL references
377 $this->moduleLabels[$moduleName] = $labels;
378 } elseif (isset($labels['ll_ref'])) {
379 // Classic, non-extbase module labels
380 $this->addLabelsForModule($moduleName, $labels['ll_ref']);
381 } else {
382 // Very old obsolete approach, don't use anymore, use one of the ways above.
383 if (is_object($this->getLanguageService())) {
384 $language = $this->getLanguageService()->lang;
385 } else {
386 $language = 'default';
387 }
388
389 if (empty($labels)) {
390 if (isset($this->getLanguageService()->moduleLabels['labels'][$moduleName . '_tablabel'])) {
391 $labels[$language]['labels']['tablabel'] = $this->getLanguageService()->moduleLabels['labels'][$moduleName . '_tablabel'];
392 }
393 if (isset($this->getLanguageService()->moduleLabels['labels'][$moduleName . '_tabdescr'])) {
394 $labels[$language]['labels']['tabdescr'] = $this->getLanguageService()->moduleLabels['labels'][$moduleName . '_tabdescr'];
395 }
396 if (isset($this->getLanguageService()->moduleLabels['tabs'][$moduleName . '_tab'])) {
397 $labels[$language]['tabs']['tab'] = $this->getLanguageService()->moduleLabels['tabs'][$moduleName . '_tab'];
398 }
399 }
400
401 if (isset($labels[$language]['ll_ref'])) {
402 $this->addLabelsForModule($moduleName, $labels[$language]['ll_ref']);
403 } elseif (isset($labels['default']['ll_ref'])) {
404 $this->addLabelsForModule($moduleName, $labels['default']['ll_ref']);
405 } else {
406 $this->moduleLabels[$moduleName] = [
407 'shortdescription' => isset($labels[$language]['labels']['tablabel']) ? $labels[$language]['labels']['tablabel'] : $labels['default']['labels']['tablabel'],
408 'description' => isset($labels[$language]['labels']['tabdescr']) ? $labels[$language]['labels']['tabdescr'] : $labels['default']['labels']['tabdescr'],
409 'title' => isset($labels[$language]['tabs']['tab']) ? $labels[$language]['tabs']['tab'] : $labels['default']['tabs']['tab'],
410 ];
411 }
412 }
413 }
414
415 /**
416 * Returns the labels for the given module
417 *
418 * @param string $moduleName
419 * @return array
420 */
421 public function getLabelsForModule($moduleName)
422 {
423 return isset($this->moduleLabels[$moduleName]) ? $this->moduleLabels[$moduleName] : [];
424 }
425
426 /**
427 * @return LanguageService
428 */
429 protected function getLanguageService()
430 {
431 return $GLOBALS['LANG'];
432 }
433 }