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