Cleanup: Updated copyright comments
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_loadmodules.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2010 Kasper Skaarhoj (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 * $Id$
31 * Modifications by Rene Fritz, 2001
32 * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
33 *
34 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
35 * @internal
36 */
37 /**
38 * [CLASS/FUNCTION INDEX of SCRIPT]
39 *
40 *
41 *
42 * 79: class t3lib_loadModules
43 * 99: function load($modulesArray,$BE_USER='')
44 * 370: function checkExtensionModule($name)
45 * 389: function checkMod($name, $fullpath)
46 * 471: function checkModAccess($name,$MCONF)
47 * 495: function checkModWorkspace($name,$MCONF)
48 * 519: function parseModulesArray($arr)
49 * 548: function cleanName ($str)
50 * 559: function getRelativePath($baseDir,$destDir)
51 *
52 * TOTAL FUNCTIONS: 8
53 * (This index is automatically created/updated by the extension "extdeveval")
54 *
55 */
56
57
58
59
60
61
62
63
64
65
66
67
68 /**
69 * Load Backend Interface modules
70 *
71 * Typically instantiated like this:
72 * $this->loadModules = t3lib_div::makeInstance('t3lib_loadModules');
73 * $this->loadModules->load($TBE_MODULES);
74 *
75 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
76 * @package TYPO3
77 * @subpackage t3lib
78 */
79 class t3lib_loadModules {
80 var $modules = Array(); // After the init() function this array will contain the structure of available modules for the backend user.
81 var $absPathArray = array(); // Array with paths pointing to the location of modules from extensions
82
83 var $modListGroup = Array(); // this array will hold the elements that should go into the select-list of modules for groups...
84 var $modListUser = Array(); // this array will hold the elements that should go into the select-list of modules for users...
85
86 /**
87 * The backend user for use internally
88 *
89 * @var t3lib_beUserAuth
90 */
91 var $BE_USER;
92 var $observeWorkspaces = FALSE; // If set true, workspace "permissions" will be observed so non-allowed modules will not be included in the array of modules.
93
94
95 /**
96 * Init.
97 * The outcome of the load() function will be a $this->modules array populated with the backend module structure available to the BE_USER
98 * Further the global var $LANG will have labels and images for the modules loaded in an internal array.
99 *
100 * @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)
101 * @param object Optional backend user object to use. If not set, the global BE_USER object is used.
102 * @return void
103 */
104 function load($modulesArray,$BE_USER='') {
105 // Setting the backend user for use internally
106 if (is_object($BE_USER)) {
107 $this->BE_USER = $BE_USER;
108 } else {
109 $this->BE_USER = $GLOBALS['BE_USER'];
110 }
111
112 /*
113
114 $modulesArray might look like this when entering this function.
115 Notice the two modules added by extensions - they have a path attached
116
117 Array
118 (
119 [web] => list,info,perm,func
120 [file] => list
121 [user] =>
122 [tools] => em,install,txphpmyadmin
123 [help] => about
124 [_PATHS] => Array
125 (
126 [tools_install] => /www/htdocs/typo3/32/coreinstall/typo3/ext/install/mod/
127 [tools_txphpmyadmin] => /www/htdocs/typo3/32/coreinstall/typo3/ext/phpmyadmin/modsub/
128 )
129
130 )
131
132 */
133 //
134 $this->absPathArray = $modulesArray['_PATHS'];
135 unset($modulesArray['_PATHS']);
136 // unset the array for calling external backend module dispatchers in typo3/mod.php
137 unset($modulesArray['_dispatcher']);
138 // unset the array for calling backend modules based on external backend module dispatchers in typo3/mod.php
139 unset($modulesArray['_configuration']);
140
141 /*
142 With the above data for modules the result of this function call will be:
143
144 Array
145 (
146 [web] => Array
147 (
148 [0] => list
149 [1] => info
150 [2] => perm
151 [3] => func
152 )
153
154 [file] => Array
155 (
156 [0] => list
157 )
158
159 [user] => 1
160 [tools] => Array
161 (
162 [0] => em
163 [1] => install
164 [2] => txphpmyadmin
165 )
166
167 [help] => Array
168 (
169 [0] => about
170 )
171
172 )
173 */
174 $theMods = $this->parseModulesArray($modulesArray);
175
176 /*
177 Originally modules were found in typo3/mod/
178 User defined modules were found in ../typo3conf/
179
180 Today almost all modules reside in extensions and they are found by the _PATHS array of the incoming $TBE_MODULES array
181 */
182 // Setting paths for 1) core modules (old concept from mod/) and 2) user-defined modules (from ../typo3conf)
183 $paths = array();
184 $paths['defMods'] = PATH_typo3.'mod/'; // Path of static modules
185 $paths['userMods'] = PATH_typo3.'../typo3conf/'; // local modules (maybe frontend specific)
186
187 // Traverses the module setup and creates the internal array $this->modules
188 foreach($theMods as $mods => $subMod) {
189 $path = NULL;
190
191 $extModRelPath = $this->checkExtensionModule($mods);
192 if ($extModRelPath) { // EXTENSION module:
193 $theMainMod = $this->checkMod($mods,PATH_site.$extModRelPath);
194 if (is_array($theMainMod) || $theMainMod!='notFound') {
195 $path = 1; // ... just so it goes on... submodules cannot be within this path!
196 }
197 } else { // 'CLASSIC' module
198 // Checking for typo3/mod/ module existence...
199 $theMainMod = $this->checkMod($mods,$paths['defMods'].$mods);
200 if (is_array($theMainMod) || $theMainMod!='notFound') {
201 $path = $paths['defMods'];
202 } else {
203 // If not typo3/mod/ then it could be user-defined in typo3conf/ ...?
204 $theMainMod = $this->checkMod($mods,$paths['userMods'].$mods);
205 if (is_array($theMainMod) || $theMainMod!='notFound') {
206 $path = $paths['userMods'];
207 }
208 }
209 }
210
211 // if $theMainMod is not set (false) there is no access to the module !(?)
212 if ($theMainMod && !is_null($path)) {
213 $this->modules[$mods] = $theMainMod;
214
215 // SUBMODULES - if any - are loaded
216 if (is_array($subMod)) {
217 foreach($subMod as $valsub) {
218 $extModRelPath = $this->checkExtensionModule($mods.'_'.$valsub);
219 if ($extModRelPath) { // EXTENSION submodule:
220 $theTempSubMod = $this->checkMod($mods.'_'.$valsub,PATH_site.$extModRelPath);
221 if (is_array($theTempSubMod)) { // default sub-module in either main-module-path, be it the default or the userdefined.
222 $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
223 }
224 } else { // 'CLASSIC' submodule
225 // Checking for typo3/mod/xxx/ module existence...
226 // FIXME what about $path = 1; from above and using $path as string here?
227 $theTempSubMod = $this->checkMod($mods.'_'.$valsub,$path.$mods.'/'.$valsub);
228 if (is_array($theTempSubMod)) { // default sub-module in either main-module-path, be it the default or the userdefined.
229 $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
230 } 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!
231 $theTempSubMod = $this->checkMod($mods.'_'.$valsub,$paths['userMods'].$mods.'/'.$valsub);
232 if (is_array($theTempSubMod)) {
233 $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
234 }
235 }
236 }
237 }
238 }
239 } else { // This must be done in order to fill out the select-lists for modules correctly!!
240 if (is_array($subMod)) {
241 foreach($subMod as $valsub) {
242 // FIXME path can only be NULL here, or not?
243 $this->checkMod($mods.'_'.$valsub,$path.$mods.'/'.$valsub);
244 }
245 }
246 }
247 }
248 /*
249 At this point $this->modules should look like this:
250 Only modules which were accessible to the $BE_USER is listed in this array.
251
252 Array
253 (
254 [web] => Array
255 (
256 [name] => web
257 [script] => dummy.php
258 [defaultMod] => list
259 [sub] => Array
260 (
261 [list] => Array
262 (
263 [name] => web_list
264 [script] => mod/web/list/../../../db_list.php
265 )
266
267 [info] => Array
268 (
269 [name] => web_info
270 [script] => mod/web/info/index.php
271 )
272
273 [perm] => Array
274 (
275 [name] => web_perm
276 [script] => mod/web/perm/index.php
277 )
278
279 [func] => Array
280 (
281 [name] => web_func
282 [script] => mod/web/func/index.php
283 )
284
285 )
286
287 )
288
289 [file] => Array
290 (
291 [name] => file
292 [script] => dummy.php
293 [sub] => Array
294 (
295 [list] => Array
296 (
297 [name] => file_list
298 [script] => mod/file/list/../../../file_list.php
299 )
300
301 )
302
303 )
304
305 [user] => Array
306 (
307 [name] => user
308 [script] => dummy.php
309 [defaultMod] => task
310 )
311
312 [tools] => Array
313 (
314 [name] => tools
315 [script] => dummy.php
316 [sub] => Array
317 (
318 [em] => Array
319 (
320 [name] => tools_em
321 [script] => mod/tools/em/index.php
322 )
323
324 [install] => Array
325 (
326 [name] => tools_install
327 [script] => ext/install/mod/../../../install/index.php
328 )
329
330 [txphpmyadmin] => Array
331 (
332 [name] => tools_txphpmyadmin
333 [script] => ext/phpmyadmin/modsub/index.php
334 )
335
336 )
337
338 )
339
340 [help] => Array
341 (
342 [name] => help
343 [script] => dummy.php
344 [defaultMod] => welcome
345 [sub] => Array
346 (
347 [about] => Array
348 (
349 [name] => help_about
350 [script] => mod/help/about/index.php
351 )
352
353 )
354
355 )
356
357 )
358
359 */
360
361 #debug($this->modules);
362 #debug($GLOBALS['LANG']->moduleLabels);
363 }
364
365 /**
366 * 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
367 *
368 * @param string Module name
369 * @return string If found, the relative path from PATH_site
370 */
371 function checkExtensionModule($name) {
372 global $TYPO3_LOADED_EXT;
373
374 if (isset($this->absPathArray[$name])) {
375 return rtrim(substr($this->absPathArray[$name],strlen(PATH_site)), '/');
376 }
377 }
378
379 /**
380 * Here we check for the module.
381 * Return values:
382 * 'notFound': If the module was not found in the path (no "conf.php" file)
383 * false: If no access to the module (access check failed)
384 * array(): Configuration array, in case a valid module where access IS granted exists.
385 *
386 * @param string Module name
387 * @param string Absolute path to module
388 * @return mixed See description of function
389 */
390 function checkMod($name, $fullpath) {
391 if ($name == 'user_ws' && !t3lib_extMgm::isLoaded('version')) {
392 return FALSE;
393 }
394
395 // Check for own way of configuring module
396 if (is_array($GLOBALS['TBE_MODULES']['_configuration'][$name]['configureModuleFunction'])) {
397 $obj = $GLOBALS['TBE_MODULES']['_configuration'][$name]['configureModuleFunction'];
398 if (is_callable($obj)) {
399 return call_user_func($obj, $name, $fullpath);
400 }
401 }
402
403 $modconf = array();
404 $path = preg_replace('/\/[^\/.]+\/\.\.\//', '/', $fullpath); // because 'path/../path' does not work
405 if (@is_dir($path) && file_exists($path.'/conf.php')) {
406 $MCONF = array();
407 $MLANG = array();
408 include($path.'/conf.php'); // The conf-file is included. This must be valid PHP.
409 if (!$MCONF['shy'] && $this->checkModAccess($name,$MCONF) && $this->checkModWorkspace($name,$MCONF)) {
410 $modconf['name']=$name;
411 // language processing. This will add module labels and image reference to the internal ->moduleLabels array of the LANG object.
412 if (is_object($GLOBALS['LANG'])) {
413 // $MLANG['default']['tabs_images']['tab'] is for modules the reference to the module icon.
414 // Here the path is transformed to an absolute reference.
415 if ($MLANG['default']['tabs_images']['tab']) {
416
417 // Initializing search for alternative icon:
418 $altIconKey = 'MOD:'.$name.'/'.$MLANG['default']['tabs_images']['tab']; // Alternative icon key (might have an alternative set in $TBE_STYLES['skinImg']
419 $altIconAbsPath = is_array($GLOBALS['TBE_STYLES']['skinImg'][$altIconKey]) ? t3lib_div::resolveBackPath(PATH_typo3.$GLOBALS['TBE_STYLES']['skinImg'][$altIconKey][0]) : '';
420
421 // Setting icon, either default or alternative:
422 if ($altIconAbsPath && @is_file($altIconAbsPath)) {
423 $MLANG['default']['tabs_images']['tab']=$this->getRelativePath(PATH_typo3,$altIconAbsPath);
424 } else {
425 // Setting default icon:
426 $MLANG['default']['tabs_images']['tab']=$this->getRelativePath(PATH_typo3,$fullpath.'/'.$MLANG['default']['tabs_images']['tab']);
427 }
428
429 // Finally, setting the icon with correct path:
430 if (substr($MLANG['default']['tabs_images']['tab'],0,3)=='../') {
431 $MLANG['default']['tabs_images']['tab'] = PATH_site.substr($MLANG['default']['tabs_images']['tab'],3);
432 } else {
433 $MLANG['default']['tabs_images']['tab'] = PATH_typo3.$MLANG['default']['tabs_images']['tab'];
434 }
435 }
436
437 // If LOCAL_LANG references are used for labels of the module:
438 if ($MLANG['default']['ll_ref']) {
439 // Now the 'default' key is loaded with the CURRENT language - not the english translation...
440 $MLANG['default']['labels']['tablabel'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'].':mlang_labels_tablabel');
441 $MLANG['default']['labels']['tabdescr'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'].':mlang_labels_tabdescr');
442 $MLANG['default']['tabs']['tab'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'].':mlang_tabs_tab');
443 $GLOBALS['LANG']->addModuleLabels($MLANG['default'],$name.'_');
444 } else { // ... otherwise use the old way:
445 $GLOBALS['LANG']->addModuleLabels($MLANG['default'],$name.'_');
446 $GLOBALS['LANG']->addModuleLabels($MLANG[$GLOBALS['LANG']->lang],$name.'_');
447 }
448 }
449
450 // Default script setup
451 if ($MCONF['script']==='_DISPATCH') {
452 if ($MCONF['extbase']) {
453 $modconf['script'] = 'mod.php?M=Tx_' . rawurlencode($name);
454 } else {
455 $modconf['script'] = 'mod.php?M=' . rawurlencode($name);
456 }
457 } elseif ($MCONF['script'] && file_exists($path.'/'.$MCONF['script'])) {
458 $modconf['script'] = $this->getRelativePath(PATH_typo3,$fullpath.'/'.$MCONF['script']);
459 } else {
460 $modconf['script'] = 'dummy.php';
461 }
462 // Default tab setting
463 if ($MCONF['defaultMod']) {
464 $modconf['defaultMod'] = $MCONF['defaultMod'];
465 }
466 // Navigation Frame Script (GET params could be added)
467 if ($MCONF['navFrameScript']) {
468 $navFrameScript = explode('?', $MCONF['navFrameScript']);
469 $navFrameScript = $navFrameScript[0];
470 if (file_exists($path.'/'.$navFrameScript)) {
471 $modconf['navFrameScript'] = $this->getRelativePath(PATH_typo3,$fullpath.'/'.$MCONF['navFrameScript']);
472 }
473 }
474 // additional params for Navigation Frame Script: "&anyParam=value&moreParam=1"
475 if ($MCONF['navFrameScriptParam']) {
476 $modconf['navFrameScriptParam'] = $MCONF['navFrameScriptParam'];
477 }
478 } else return false;
479 } else $modconf = 'notFound';
480 return $modconf;
481 }
482
483 /**
484 * Returns true if the internal BE_USER has access to the module $name with $MCONF (based on security level set for that module)
485 *
486 * @param string Module name
487 * @param array MCONF array (module configuration array) from the modules conf.php file (contains settings about what access level the module has)
488 * @return boolean True if access is granted for $this->BE_USER
489 */
490 function checkModAccess($name,$MCONF) {
491 if ($MCONF['access']) {
492 $access=strtolower($MCONF['access']);
493 // Checking if admin-access is required
494 if (strstr($access,'admin')) { // If admin-permissions is required then return true if user is admin
495 if ($this->BE_USER->isAdmin()) {return true;}
496 }
497 // This will add modules to the select-lists of user and groups
498 if (strstr($access,'user')) { $this->modListUser[]=$name; }
499 if (strstr($access,'group')) { $this->modListGroup[]=$name; }
500 // This checks if a user is permitted to access the module
501 if ($this->BE_USER->isAdmin() || $this->BE_USER->check('modules',$name)) {return true;} // If admin you can always access a module
502
503 } else return true; // If conf[access] is not set, then permission IS granted!
504 }
505
506 /**
507 * Check if a module is allowed inside the current workspace for be user
508 * Processing happens only if $this->observeWorkspaces is TRUE
509 *
510 * @param string Module name
511 * @param array MCONF array (module configuration array) from the modules conf.php file (contains settings about workspace restrictions)
512 * @return boolean True if access is granted for $this->BE_USER
513 */
514 function checkModWorkspace($name,$MCONF) {
515 if ($this->observeWorkspaces) {
516 $status = TRUE;
517 if ($MCONF['workspaces']) {
518 $status = FALSE;
519 if (($this->BE_USER->workspace===0 && t3lib_div::inList($MCONF['workspaces'],'online')) ||
520 ($this->BE_USER->workspace===-1 && t3lib_div::inList($MCONF['workspaces'],'offline')) ||
521 ($this->BE_USER->workspace>0 && t3lib_div::inList($MCONF['workspaces'],'custom'))) {
522 $status = TRUE;
523 }
524 } elseif ($this->BE_USER->workspace===-99) {
525 $status = FALSE;
526 }
527 return $status;
528 } else return TRUE;
529 }
530
531 /**
532 * Parses the moduleArray ($TBE_MODULES) into a internally useful structure.
533 * Returns an array where the keys are names of the module and the values may be true (only module) or an array (of submodules)
534 *
535 * @param array moduleArray ($TBE_MODULES)
536 * @return array Output structure with available modules
537 */
538 function parseModulesArray($arr) {
539 $theMods = Array();
540 if (is_array($arr)) {
541 foreach($arr as $mod => $subs) {
542 $mod = $this->cleanName($mod); // clean module name to alphanum
543 if ($mod) {
544 if ($subs) {
545 $subsArr = t3lib_div::trimExplode(',', $subs);
546 foreach($subsArr as $subMod) {
547 $subMod = $this->cleanName($subMod);
548 if ($subMod) {
549 $theMods[$mod][] = $subMod;
550 }
551 }
552 } else {
553 $theMods[$mod] = 1;
554 }
555 }
556 }
557 }
558 return $theMods;
559 }
560
561 /**
562 * The $str is cleaned so that it contains alphanumerical characters only. Modules must only consist of these characters
563 *
564 * @param string String to clean up
565 * @return string
566 */
567 function cleanName ($str) {
568 return preg_replace('/[^a-z0-9]/i','',$str);
569 }
570
571 /**
572 * Get relative path for $destDir compared to $baseDir
573 *
574 * @param string Base directory
575 * @param string Destination directory
576 * @return string The relative path of destination compared to base.
577 */
578 function getRelativePath($baseDir,$destDir){
579 // By Rene Fritz
580 // a special case , the dirs are equals
581 if ($baseDir == $destDir){
582 return './';
583 }
584
585 $baseDir = ltrim($baseDir, '/'); // remove beginning
586 $destDir = ltrim($destDir, '/');
587
588 $found = true;
589 $slash_pos=0;
590
591 do {
592 $slash_pos = strpos ($destDir, '/');
593 if (substr($destDir, 0, $slash_pos) == substr($baseDir, 0, $slash_pos)){
594 $baseDir = substr($baseDir, $slash_pos+1);
595 $destDir = substr($destDir, $slash_pos+1);
596 } else {
597 $found = false;
598 }
599 } while($found == true);
600
601 $slashes = strlen ($baseDir) - strlen (str_replace('/', '', $baseDir));
602 for($i=0;$i < $slashes;$i++) {
603 $destDir = '../'.$destDir;
604 }
605 return $destDir;
606 }
607 }
608
609
610 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_loadmodules.php']) {
611 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_loadmodules.php']);
612 }
613
614 ?>