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