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