d33829a4ec3a9f32eaafbbedef2df3ac7edfec42
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Module / ModuleLoader.php
1 <?php
2 namespace TYPO3\CMS\Backend\Module;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32 /**
33 * This document provides a class that loads the modules for the TYPO3 interface.
34 *
35 * Load Backend Interface modules
36 *
37 * Typically instantiated like this:
38 * $this->loadModules = GeneralUtility::makeInstance('TYPO3\CMS\Backend\Module\ModuleLoader');
39 * $this->loadModules->load($TBE_MODULES);
40 *
41 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
42 * @internal
43 */
44 class ModuleLoader {
45
46 // After the init() function this array will contain the structure of available modules for the backend user.
47 /**
48 * @todo Define visibility
49 */
50 public $modules = array();
51
52 // Array with paths pointing to the location of modules from extensions
53 /**
54 * @todo Define visibility
55 */
56 public $absPathArray = array();
57
58 // This array will hold the elements that should go into the select-list of modules for groups...
59 /**
60 * @todo Define visibility
61 */
62 public $modListGroup = array();
63
64 // This array will hold the elements that should go into the select-list of modules for users...
65 /**
66 * @todo Define visibility
67 */
68 public $modListUser = array();
69
70 /**
71 * The backend user for use internally
72 *
73 * @var \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
74 * @todo Define visibility
75 */
76 public $BE_USER;
77
78 // If set TRUE, workspace "permissions" will be observed so non-allowed modules will not be included in the array of modules.
79 /**
80 * @todo Define visibility
81 */
82 public $observeWorkspaces = FALSE;
83
84 /**
85 * Contains the registered navigation components
86 *
87 * @var array
88 */
89 protected $navigationComponents = array();
90
91 /**
92 * Init.
93 * The outcome of the load() function will be a $this->modules array populated with the backend module structure available to the BE_USER
94 * Further the global var $LANG will have labels and images for the modules loaded in an internal array.
95 *
96 * @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)
97 * @param object $BE_USER Optional backend user object to use. If not set, the global BE_USER object is used.
98 * @return void
99 * @todo Define visibility
100 */
101 public function load($modulesArray, $BE_USER = '') {
102 // Setting the backend user for use internally
103 if (is_object($BE_USER)) {
104 $this->BE_USER = $BE_USER;
105 } else {
106 $this->BE_USER = $GLOBALS['BE_USER'];
107 }
108 /*$modulesArray might look like this when entering this function.
109 Notice the two modules added by extensions - they have a path attachedArray
110 (
111 [web] => list,info,perm,func
112 [file] => list
113 [user] =>
114 [tools] => em,install,txphpmyadmin
115 [help] => about
116 [_PATHS] => Array
117 (
118 [system_install] => /www/htdocs/typo3/32/coreinstall/typo3/ext/install/mod/
119 [tools_txphpmyadmin] => /www/htdocs/typo3/32/coreinstall/typo3/ext/phpmyadmin/modsub/
120 ))
121 */
122 $this->absPathArray = $modulesArray['_PATHS'];
123 unset($modulesArray['_PATHS']);
124 // Unset the array for calling external backend module dispatchers in typo3/mod.php
125 unset($modulesArray['_dispatcher']);
126 // Unset the array for calling backend modules based on external backend module dispatchers in typo3/mod.php
127 unset($modulesArray['_configuration']);
128 $this->navigationComponents = $modulesArray['_navigationComponents'];
129 unset($modulesArray['_navigationComponents']);
130 $theMods = $this->parseModulesArray($modulesArray);
131 // Originally modules were found in typo3/mod/
132 // User defined modules were found in ../typo3conf/
133 // Today almost all modules reside in extensions and they are found by the _PATHS array of the incoming $TBE_MODULES array
134 // Setting paths for 1) core modules (old concept from mod/) and 2) user-defined modules (from ../typo3conf)
135 $paths = array();
136 // Path of static modules
137 $paths['defMods'] = PATH_typo3 . 'mod/';
138 // Local modules (maybe frontend specific)
139 $paths['userMods'] = PATH_typo3 . '../typo3conf/';
140 // Traverses the module setup and creates the internal array $this->modules
141 foreach ($theMods as $mods => $subMod) {
142 $path = NULL;
143 $extModRelPath = $this->checkExtensionModule($mods);
144 // EXTENSION module:
145 if ($extModRelPath) {
146 $theMainMod = $this->checkMod($mods, PATH_site . $extModRelPath);
147 if (is_array($theMainMod) || $theMainMod != 'notFound') {
148 // ... just so it goes on... submodules cannot be within this path!
149 $path = 1;
150 }
151 } else {
152 // 'CLASSIC' module
153 // Checking for typo3/mod/ module existence...
154 $theMainMod = $this->checkMod($mods, $paths['defMods'] . $mods);
155 if (is_array($theMainMod) || $theMainMod != 'notFound') {
156 $path = $paths['defMods'];
157 } else {
158 // If not typo3/mod/ then it could be user-defined in typo3conf/ ...?
159 $theMainMod = $this->checkMod($mods, $paths['userMods'] . $mods);
160 if (is_array($theMainMod) || $theMainMod != 'notFound') {
161 $path = $paths['userMods'];
162 }
163 }
164 }
165 // If $theMainMod is not set (FALSE) there is no access to the module !(?)
166 if ($theMainMod && !is_null($path)) {
167 $this->modules[$mods] = $theMainMod;
168 // SUBMODULES - if any - are loaded
169 if (is_array($subMod)) {
170 foreach ($subMod as $valsub) {
171 $extModRelPath = $this->checkExtensionModule($mods . '_' . $valsub);
172 if ($extModRelPath) {
173 // EXTENSION submodule:
174 $theTempSubMod = $this->checkMod($mods . '_' . $valsub, PATH_site . $extModRelPath);
175 // Default sub-module in either main-module-path, be it the default or the userdefined.
176 if (is_array($theTempSubMod)) {
177 $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
178 }
179 } else {
180 // 'CLASSIC' submodule
181 // Checking for typo3/mod/xxx/ module existence...
182 // FIXME what about $path = 1; from above and using $path as string here?
183 $theTempSubMod = $this->checkMod($mods . '_' . $valsub, $path . $mods . '/' . $valsub);
184 // Default sub-module in either main-module-path, be it the default or the userdefined.
185 if (is_array($theTempSubMod)) {
186 $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
187 } elseif ($path == $paths['defMods']) {
188 // If the submodule did not exist in the default module path, then check if there is a submodule in the submodule path!
189 $theTempSubMod = $this->checkMod($mods . '_' . $valsub, $paths['userMods'] . $mods . '/' . $valsub);
190 if (is_array($theTempSubMod)) {
191 $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
192 }
193 }
194 }
195 }
196 }
197 } else {
198 // This must be done in order to fill out the select-lists for modules correctly!!
199 if (is_array($subMod)) {
200 foreach ($subMod as $valsub) {
201 // FIXME path can only be NULL here, or not?
202 $this->checkMod($mods . '_' . $valsub, $path . $mods . '/' . $valsub);
203 }
204 }
205 }
206 }
207 }
208
209 /**
210 * If the module name ($name) is a module from an extension (has path in $this->absPathArray) then that path is returned relative to PATH_site
211 *
212 * @param string $name Module name
213 * @return string If found, the relative path from PATH_site
214 * @todo Define visibility
215 */
216 public function checkExtensionModule($name) {
217 if (isset($this->absPathArray[$name])) {
218 return rtrim(substr($this->absPathArray[$name], strlen(PATH_site)), '/');
219 }
220 }
221
222 /**
223 * Here we check for the module.
224 * Return values:
225 * 'notFound': If the module was not found in the path (no "conf.php" file)
226 * FALSE: If no access to the module (access check failed)
227 * array(): Configuration array, in case a valid module where access IS granted exists.
228 *
229 * @param string $name Module name
230 * @param string $fullpath Absolute path to module
231 * @return mixed See description of function
232 * @todo Define visibility
233 */
234 public function checkMod($name, $fullpath) {
235 if ($name == 'user_ws' && !\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('version')) {
236 return FALSE;
237 }
238 // Check for own way of configuring module
239 if (is_array($GLOBALS['TBE_MODULES']['_configuration'][$name]['configureModuleFunction'])) {
240 $obj = $GLOBALS['TBE_MODULES']['_configuration'][$name]['configureModuleFunction'];
241 if (is_callable($obj)) {
242 $MCONF = call_user_func($obj, $name, $fullpath);
243 if ($this->checkModAccess($name, $MCONF) !== TRUE) {
244 return FALSE;
245 }
246 return $MCONF;
247 }
248 }
249 // Check if this is a submodule
250 if (strpos($name, '_') !== FALSE) {
251 list($mainModule, ) = explode('_', $name, 2);
252 }
253 $modconf = array();
254 // Because 'path/../path' does not work
255 $path = preg_replace('/\\/[^\\/.]+\\/\\.\\.\\//', '/', $fullpath);
256 if (@is_dir($path) && file_exists($path . '/conf.php')) {
257 $MCONF = array();
258 $MLANG = array();
259 // The conf-file is included. This must be valid PHP.
260 include $path . '/conf.php';
261 if (!$MCONF['shy'] && $this->checkModAccess($name, $MCONF) && $this->checkModWorkspace($name, $MCONF)) {
262 $modconf['name'] = $name;
263 // Language processing. This will add module labels and image reference to the internal ->moduleLabels array of the LANG object.
264 if (is_object($GLOBALS['LANG'])) {
265 // $MLANG['default']['tabs_images']['tab'] is for modules the reference to the module icon.
266 // Here the path is transformed to an absolute reference.
267 if ($MLANG['default']['tabs_images']['tab']) {
268 // Initializing search for alternative icon:
269 // Alternative icon key (might have an alternative set in $TBE_STYLES['skinImg']
270 $altIconKey = 'MOD:' . $name . '/' . $MLANG['default']['tabs_images']['tab'];
271 $altIconAbsPath = is_array($GLOBALS['TBE_STYLES']['skinImg'][$altIconKey]) ? GeneralUtility::resolveBackPath(PATH_typo3 . $GLOBALS['TBE_STYLES']['skinImg'][$altIconKey][0]) : '';
272 // Setting icon, either default or alternative:
273 if ($altIconAbsPath && @is_file($altIconAbsPath)) {
274 $MLANG['default']['tabs_images']['tab'] = $this->getRelativePath(PATH_typo3, $altIconAbsPath);
275 } else {
276 // Setting default icon:
277 $MLANG['default']['tabs_images']['tab'] = $this->getRelativePath(PATH_typo3, $fullpath . '/' . $MLANG['default']['tabs_images']['tab']);
278 }
279 // Finally, setting the icon with correct path:
280 if (substr($MLANG['default']['tabs_images']['tab'], 0, 3) == '../') {
281 $MLANG['default']['tabs_images']['tab'] = PATH_site . substr($MLANG['default']['tabs_images']['tab'], 3);
282 } else {
283 $MLANG['default']['tabs_images']['tab'] = PATH_typo3 . $MLANG['default']['tabs_images']['tab'];
284 }
285 }
286 // If LOCAL_LANG references are used for labels of the module:
287 if ($MLANG['default']['ll_ref']) {
288 // Now the 'default' key is loaded with the CURRENT language - not the english translation...
289 $MLANG['default']['labels']['tablabel'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'] . ':mlang_labels_tablabel');
290 $MLANG['default']['labels']['tabdescr'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'] . ':mlang_labels_tabdescr');
291 $MLANG['default']['tabs']['tab'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'] . ':mlang_tabs_tab');
292 $GLOBALS['LANG']->addModuleLabels($MLANG['default'], $name . '_');
293 } else {
294 // ... otherwise use the old way:
295 $GLOBALS['LANG']->addModuleLabels($MLANG['default'], $name . '_');
296 $GLOBALS['LANG']->addModuleLabels($MLANG[$GLOBALS['LANG']->lang], $name . '_');
297 }
298 }
299 // Default script setup
300 if ($MCONF['script'] === '_DISPATCH') {
301 if ($MCONF['extbase']) {
302 $modconf['script'] = 'mod.php?M=Tx_' . rawurlencode($name);
303 } else {
304 $modconf['script'] = 'mod.php?M=' . rawurlencode($name);
305 }
306 } elseif ($MCONF['script'] && file_exists($path . '/' . $MCONF['script'])) {
307 $modconf['script'] = $this->getRelativePath(PATH_typo3, $fullpath . '/' . $MCONF['script']);
308 } else {
309 $modconf['script'] = 'dummy.php';
310 }
311 // Default tab setting
312 if ($MCONF['defaultMod']) {
313 $modconf['defaultMod'] = $MCONF['defaultMod'];
314 }
315 // Navigation Frame Script (GET params could be added)
316 if ($MCONF['navFrameScript']) {
317 $navFrameScript = explode('?', $MCONF['navFrameScript']);
318 $navFrameScript = $navFrameScript[0];
319 if (file_exists($path . '/' . $navFrameScript)) {
320 $modconf['navFrameScript'] = $this->getRelativePath(PATH_typo3, $fullpath . '/' . $MCONF['navFrameScript']);
321 }
322 }
323 // additional params for Navigation Frame Script: "&anyParam=value&moreParam=1"
324 if ($MCONF['navFrameScriptParam']) {
325 $modconf['navFrameScriptParam'] = $MCONF['navFrameScriptParam'];
326 }
327 // check if there is a navigation component (like the pagetree)
328 if (is_array($this->navigationComponents[$name])) {
329 $modconf['navigationComponentId'] = $this->navigationComponents[$name]['componentId'];
330 } elseif ($mainModule && is_array($this->navigationComponents[$mainModule])) {
331 $modconf['navigationComponentId'] = $this->navigationComponents[$mainModule]['componentId'];
332 }
333 } else {
334 return FALSE;
335 }
336 } else {
337 $modconf = 'notFound';
338 }
339 return $modconf;
340 }
341
342 /**
343 * Returns TRUE if the internal BE_USER has access to the module $name with $MCONF (based on security level set for that module)
344 *
345 * @param string $name Module name
346 * @param array $MCONF MCONF array (module configuration array) from the modules conf.php file (contains settings about what access level the module has)
347 * @return boolean TRUE if access is granted for $this->BE_USER
348 * @todo Define visibility
349 */
350 public function checkModAccess($name, $MCONF) {
351 if ($MCONF['access']) {
352 $access = strtolower($MCONF['access']);
353 // Checking if admin-access is required
354 // If admin-permissions is required then return TRUE if user is admin
355 if (strstr($access, 'admin')) {
356 if ($this->BE_USER->isAdmin()) {
357 return TRUE;
358 }
359 }
360 // This will add modules to the select-lists of user and groups
361 if (strstr($access, 'user')) {
362 $this->modListUser[] = $name;
363 }
364 if (strstr($access, 'group')) {
365 $this->modListGroup[] = $name;
366 }
367 // This checks if a user is permitted to access the module
368 if ($this->BE_USER->isAdmin() || $this->BE_USER->check('modules', $name)) {
369 return TRUE;
370 }
371 } else {
372 return TRUE;
373 }
374 }
375
376 /**
377 * Check if a module is allowed inside the current workspace for be user
378 * Processing happens only if $this->observeWorkspaces is TRUE
379 *
380 * @param string $name Module name
381 * @param array $MCONF MCONF array (module configuration array) from the modules conf.php file (contains settings about workspace restrictions)
382 * @return boolean TRUE if access is granted for $this->BE_USER
383 * @todo Define visibility
384 */
385 public function checkModWorkspace($name, $MCONF) {
386 if ($this->observeWorkspaces) {
387 $status = TRUE;
388 if ($MCONF['workspaces']) {
389 $status = FALSE;
390 if ($this->BE_USER->workspace === 0 && GeneralUtility::inList($MCONF['workspaces'], 'online') || $this->BE_USER->workspace === -1 && GeneralUtility::inList($MCONF['workspaces'], 'offline') || $this->BE_USER->workspace > 0 && GeneralUtility::inList($MCONF['workspaces'], 'custom')) {
391 $status = TRUE;
392 }
393 } elseif ($this->BE_USER->workspace === -99) {
394 $status = FALSE;
395 }
396 return $status;
397 } else {
398 return TRUE;
399 }
400 }
401
402 /**
403 * Parses the moduleArray ($TBE_MODULES) into a internally useful structure.
404 * Returns an array where the keys are names of the module and the values may be TRUE (only module) or an array (of submodules)
405 *
406 * @param array $arr ModuleArray ($TBE_MODULES)
407 * @return array Output structure with available modules
408 * @todo Define visibility
409 */
410 public function parseModulesArray($arr) {
411 $theMods = array();
412 if (is_array($arr)) {
413 foreach ($arr as $mod => $subs) {
414 // Clean module name to alphanum
415 $mod = $this->cleanName($mod);
416 if ($mod) {
417 if ($subs) {
418 $subsArr = GeneralUtility::trimExplode(',', $subs);
419 foreach ($subsArr as $subMod) {
420 $subMod = $this->cleanName($subMod);
421 if ($subMod) {
422 $theMods[$mod][] = $subMod;
423 }
424 }
425 } else {
426 $theMods[$mod] = 1;
427 }
428 }
429 }
430 }
431 return $theMods;
432 }
433
434 /**
435 * The $str is cleaned so that it contains alphanumerical characters only. Modules must only consist of these characters
436 *
437 * @param string $str String to clean up
438 * @return string
439 * @todo Define visibility
440 */
441 public function cleanName($str) {
442 return preg_replace('/[^a-z0-9]/i', '', $str);
443 }
444
445 /**
446 * Get relative path for $destDir compared to $baseDir
447 *
448 * @param string $baseDir Base directory
449 * @param string $destDir Destination directory
450 * @return string The relative path of destination compared to base.
451 * @todo Define visibility
452 */
453 public function getRelativePath($baseDir, $destDir) {
454 // A special case , the dirs are equals
455 if ($baseDir == $destDir) {
456 return './';
457 }
458 // Remove beginning
459 $baseDir = ltrim($baseDir, '/');
460 $destDir = ltrim($destDir, '/');
461 $found = TRUE;
462 $slash_pos = 0;
463 do {
464 $slash_pos = strpos($destDir, '/');
465 if (substr($destDir, 0, $slash_pos) == substr($baseDir, 0, $slash_pos)) {
466 $baseDir = substr($baseDir, $slash_pos + 1);
467 $destDir = substr($destDir, $slash_pos + 1);
468 } else {
469 $found = FALSE;
470 }
471 } while ($found == TRUE);
472 $slashes = strlen($baseDir) - strlen(str_replace('/', '', $baseDir));
473 for ($i = 0; $i < $slashes; $i++) {
474 $destDir = '../' . $destDir;
475 }
476 return GeneralUtility::resolveBackPath($destDir);
477 }
478
479 }
480
481
482 ?>