"TYPO3 4.0.1: typo3_src-4.0.1/t3lib/class.t3lib_loadmodules.php Source File", "datetime" => "Sat Dec 2 19:22:18 2006", "date" => "2 Dec 2006", "doxygenversion" => "1.4.6", "projectname" => "TYPO3 4.0.1", "projectnumber" => "4.0.1" ); get_header($doxygen_vars); ?>

class.t3lib_loadmodules.php

00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2006 Kasper Skaarhoj (kasperYYYY@typo3.com)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00079 class t3lib_loadModules {
00080         var $modules = Array();         // After the init() function this array will contain the structure of available modules for the backend user.
00081         var $absPathArray = array();    // Array with paths pointing to the location of modules from extensions
00082 
00083         var $modListGroup = Array();            // this array will hold the elements that should go into the select-list of modules for groups...
00084         var $modListUser = Array();             // this array will hold the elements that should go into the select-list of modules for users...
00085 
00086         var $BE_USER = '';      // The backend user for use internally
00087         var $observeWorkspaces = FALSE;         // If set true, workspace "permissions" will be observed so non-allowed modules will not be included in the array of modules.
00088 
00089 
00099         function load($modulesArray,$BE_USER='')        {
00100                         // Setting the backend user for use internally
00101                 if (is_object($BE_USER))        {
00102                         $this->BE_USER = $BE_USER;
00103                 } else {
00104                         $this->BE_USER = $GLOBALS['BE_USER'];
00105                 }
00106 
00107                         /*
00108 
00109                          $modulesArray might look like this when entering this function.
00110                          Notice the two modules added by extensions - they have a path attached
00111 
00112                         Array
00113                         (
00114                             [web] => list,info,perm,func
00115                             [file] => list
00116                             [doc] =>
00117                             [user] =>
00118                             [tools] => em,install,txphpmyadmin
00119                             [help] => about
00120                             [_PATHS] => Array
00121                                 (
00122                                     [tools_install] => /www/htdocs/typo3/32/coreinstall/typo3/ext/install/mod/
00123                                     [tools_txphpmyadmin] => /www/htdocs/typo3/32/coreinstall/typo3/ext/phpmyadmin/modsub/
00124                                 )
00125 
00126                         )
00127 
00128                          */
00129                         //
00130                 $this->absPathArray = $modulesArray['_PATHS'];
00131                 unset($modulesArray['_PATHS']);
00132 
00133                         /*
00134                                 With the above data for modules the result of this function call will be:
00135 
00136                                 Array
00137                                 (
00138                                     [web] => Array
00139                                         (
00140                                             [0] => list
00141                                             [1] => info
00142                                             [2] => perm
00143                                             [3] => func
00144                                         )
00145 
00146                                     [file] => Array
00147                                         (
00148                                             [0] => list
00149                                         )
00150 
00151                                     [doc] => 1
00152                                     [user] => 1
00153                                     [tools] => Array
00154                                         (
00155                                             [0] => em
00156                                             [1] => install
00157                                             [2] => txphpmyadmin
00158                                         )
00159 
00160                                     [help] => Array
00161                                         (
00162                                             [0] => about
00163                                         )
00164 
00165                                 )
00166                         */
00167                 $theMods = $this->parseModulesArray($modulesArray);
00168 
00169                         /*
00170                                 Originally modules were found in typo3/mod/
00171                                 User defined modules were found in ../typo3conf/
00172 
00173                                 Today almost all modules reside in extensions and they are found by the _PATHS array of the incoming $TBE_MODULES array
00174                         */
00175                         // Setting paths for 1) core modules (old concept from mod/) and 2) user-defined modules (from ../typo3conf)
00176                 $paths = array();
00177                 $paths['defMods'] = PATH_typo3.'mod/';  // Path of static modules
00178                 $paths['userMods'] = PATH_typo3.'../typo3conf/';  // local modules (maybe frontend specific)
00179 
00180                         // Traverses the module setup and creates the internal array $this->modules
00181                 foreach($theMods as $mods => $subMod)   {
00182                         $path = NULL;
00183 
00184                         $extModRelPath = $this->checkExtensionModule($mods);
00185                         if ($extModRelPath)     {       // EXTENSION module:
00186                                 $theMainMod = $this->checkMod($mods,PATH_site.$extModRelPath);
00187                                 if (is_array($theMainMod) || $theMainMod!='notFound')   {
00188                                         $path = 1;      // ... just so it goes on... submodules cannot be within this path!
00189                                 }
00190                         } else {        // 'CLASSIC' module
00191                                         // Checking for typo3/mod/ module existence...
00192                                 $theMainMod = $this->checkMod($mods,$paths['defMods'].$mods);
00193                                 if (is_array($theMainMod) || $theMainMod!='notFound')   {
00194                                         $path = $paths['defMods'];
00195                                 } else {
00196                                                 // If not typo3/mod/ then it could be user-defined in typo3conf/ ...?
00197                                         $theMainMod = $this->checkMod($mods,$paths['userMods'].$mods);
00198                                         if (is_array($theMainMod) || $theMainMod!='notFound')   {
00199                                                 $path = $paths['userMods'];
00200                                         }
00201                                 }
00202                         }
00203 
00204                                 // if $theMainMod is not set (false) there is no access to the module !(?)
00205                         if ($theMainMod && !is_null($path))     {
00206                                 $this->modules[$mods] = $theMainMod;
00207 
00208                                         // SUBMODULES - if any - are loaded (The 'doc' module cannot have submodules...)
00209                                 if ($mods!='doc' && is_array($subMod))  {
00210                                         foreach($subMod as $valsub)     {
00211                                                 $extModRelPath = $this->checkExtensionModule($mods.'_'.$valsub);
00212                                                 if ($extModRelPath)     {       // EXTENSION submodule:
00213                                                         $theTempSubMod = $this->checkMod($mods.'_'.$valsub,PATH_site.$extModRelPath);
00214                                                         if (is_array($theTempSubMod))   {       // default sub-module in either main-module-path, be it the default or the userdefined.
00215                                                                 $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
00216                                                         }
00217                                                 } else {        // 'CLASSIC' submodule
00218                                                                 // Checking for typo3/mod/xxx/ module existence...
00219 // FIXME what about $path = 1; from above and using $path as string here?
00220                                                         $theTempSubMod = $this->checkMod($mods.'_'.$valsub,$path.$mods.'/'.$valsub);
00221                                                         if (is_array($theTempSubMod))   {       // default sub-module in either main-module-path, be it the default or the userdefined.
00222                                                                 $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
00223                                                         } 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!
00224                                                                 $theTempSubMod = $this->checkMod($mods.'_'.$valsub,$paths['userMods'].$mods.'/'.$valsub);
00225                                                                 if (is_array($theTempSubMod))   {
00226                                                                         $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
00227                                                                 }
00228                                                         }
00229                                                 }
00230                                         }
00231                                 }
00232                         } else {        // This must be done in order to fill out the select-lists for modules correctly!!
00233                                 if ($mods!='doc' && is_array($subMod))  {
00234                                         foreach($subMod as $valsub)     {
00235 // FIXME path can only be NULL here, or not?
00236                                                 $this->checkMod($mods.'_'.$valsub,$path.$mods.'/'.$valsub);
00237                                         }
00238                                 }
00239                         }
00240                 }
00241         /*
00242                 At this point $this->modules should look like this:
00243                 Only modules which were accessible to the $BE_USER is listed in this array.
00244 
00245                 Array
00246                 (
00247                     [web] => Array
00248                         (
00249                             [name] => web
00250                             [script] => dummy.php
00251                             [defaultMod] => list
00252                             [sub] => Array
00253                                 (
00254                                     [list] => Array
00255                                         (
00256                                             [name] => web_list
00257                                             [script] => mod/web/list/../../../db_list.php
00258                                         )
00259 
00260                                     [info] => Array
00261                                         (
00262                                             [name] => web_info
00263                                             [script] => mod/web/info/index.php
00264                                         )
00265 
00266                                     [perm] => Array
00267                                         (
00268                                             [name] => web_perm
00269                                             [script] => mod/web/perm/index.php
00270                                         )
00271 
00272                                     [func] => Array
00273                                         (
00274                                             [name] => web_func
00275                                             [script] => mod/web/func/index.php
00276                                         )
00277 
00278                                 )
00279 
00280                         )
00281 
00282                     [file] => Array
00283                         (
00284                             [name] => file
00285                             [script] => dummy.php
00286                             [sub] => Array
00287                                 (
00288                                     [list] => Array
00289                                         (
00290                                             [name] => file_list
00291                                             [script] => mod/file/list/../../../file_list.php
00292                                         )
00293 
00294                                 )
00295 
00296                         )
00297 
00298                     [doc] => Array
00299                         (
00300                             [name] => doc
00301                             [script] => mod/doc/../../alt_doc.php
00302                         )
00303 
00304                     [user] => Array
00305                         (
00306                             [name] => user
00307                             [script] => dummy.php
00308                             [defaultMod] => task
00309                         )
00310 
00311                     [tools] => Array
00312                         (
00313                             [name] => tools
00314                             [script] => dummy.php
00315                             [sub] => Array
00316                                 (
00317                                     [em] => Array
00318                                         (
00319                                             [name] => tools_em
00320                                             [script] => mod/tools/em/index.php
00321                                         )
00322 
00323                                     [install] => Array
00324                                         (
00325                                             [name] => tools_install
00326                                             [script] => ext/install/mod/../../../install/index.php
00327                                         )
00328 
00329                                     [txphpmyadmin] => Array
00330                                         (
00331                                             [name] => tools_txphpmyadmin
00332                                             [script] => ext/phpmyadmin/modsub/index.php
00333                                         )
00334 
00335                                 )
00336 
00337                         )
00338 
00339                     [help] => Array
00340                         (
00341                             [name] => help
00342                             [script] => dummy.php
00343                             [defaultMod] => welcome
00344                             [sub] => Array
00345                                 (
00346                                     [about] => Array
00347                                         (
00348                                             [name] => help_about
00349                                             [script] => mod/help/about/index.php
00350                                         )
00351 
00352                                 )
00353 
00354                         )
00355 
00356                 )
00357 
00358         */
00359 
00360 #debug($this->modules);
00361 #debug($GLOBALS['LANG']->moduleLabels);
00362         }
00363 
00370         function checkExtensionModule($name)    {
00371                 global $TYPO3_LOADED_EXT;
00372 
00373                 if (isset($this->absPathArray[$name]))  {
00374                         return ereg_replace ('\/$', '', substr($this->absPathArray[$name],strlen(PATH_site)));
00375                 }
00376         }
00377 
00389         function checkMod($name, $fullpath)     {
00390                 $modconf=Array();
00391                 $path = ereg_replace ('/[^/.]+/\.\./', '/', $fullpath); // because 'path/../path' does not work
00392                 if (@is_dir($path) && @file_exists($path.'/conf.php'))  {
00393                         $MCONF = array();
00394                         $MLANG = array();
00395                         include($path.'/conf.php');     // The conf-file is included. This must be valid PHP.
00396                         if (!$MCONF['shy'] && $this->checkModAccess($name,$MCONF) && $this->checkModWorkspace($name,$MCONF))    {
00397                                 $modconf['name']=$name;
00398                                         // language processing. This will add module labels and image reference to the internal ->moduleLabels array of the LANG object.
00399                                 if (is_object($GLOBALS['LANG']))        {
00400                                                 // $MLANG['default']['tabs_images']['tab'] is for modules the reference to the module icon.
00401                                                 // Here the path is transformed to an absolute reference.
00402                                         if ($MLANG['default']['tabs_images']['tab'])    {
00403 
00404                                                         // Initializing search for alternative icon:
00405                                                 $altIconKey = 'MOD:'.$name.'/'.$MLANG['default']['tabs_images']['tab'];         // Alternative icon key (might have an alternative set in $TBE_STYLES['skinImg']
00406                                                 $altIconAbsPath = is_array($GLOBALS['TBE_STYLES']['skinImg'][$altIconKey]) ? t3lib_div::resolveBackPath(PATH_typo3.$GLOBALS['TBE_STYLES']['skinImg'][$altIconKey][0]) : '';
00407 
00408                                                         // Setting icon, either default or alternative:
00409                                                 if ($altIconAbsPath && @is_file($altIconAbsPath))       {
00410                                                         $MLANG['default']['tabs_images']['tab']=$this->getRelativePath(PATH_typo3,$altIconAbsPath);
00411                                                 } else {
00412                                                                 // Setting default icon:
00413                                                         $MLANG['default']['tabs_images']['tab']=$this->getRelativePath(PATH_typo3,$fullpath.'/'.$MLANG['default']['tabs_images']['tab']);
00414                                                 }
00415 
00416                                                         // Finally, setting the icon with correct path:
00417                                                 if (substr($MLANG['default']['tabs_images']['tab'],0,3)=='../') {
00418                                                         $MLANG['default']['tabs_images']['tab'] = PATH_site.substr($MLANG['default']['tabs_images']['tab'],3);
00419                                                 } else {
00420                                                         $MLANG['default']['tabs_images']['tab'] = PATH_typo3.$MLANG['default']['tabs_images']['tab'];
00421                                                 }
00422                                         }
00423 
00424                                                 // If LOCAL_LANG references are used for labels of the module:
00425                                         if ($MLANG['default']['ll_ref'])        {
00426                                                         // Now the 'default' key is loaded with the CURRENT language - not the english translation...
00427                                                 $MLANG['default']['labels']['tablabel'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'].':mlang_labels_tablabel');
00428                                                 $MLANG['default']['labels']['tabdescr'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'].':mlang_labels_tabdescr');
00429                                                 $MLANG['default']['tabs']['tab'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'].':mlang_tabs_tab');
00430                                                 $GLOBALS['LANG']->addModuleLabels($MLANG['default'],$name.'_');
00431                                         } else {        // ... otherwise use the old way:
00432                                                 $GLOBALS['LANG']->addModuleLabels($MLANG['default'],$name.'_');
00433                                                 $GLOBALS['LANG']->addModuleLabels($MLANG[$GLOBALS['LANG']->lang],$name.'_');
00434                                         }
00435                                 }
00436 
00437                                         // Default script setup
00438                                 if ($MCONF['script'] && @file_exists($path.'/'.$MCONF['script']))       {
00439                                         $modconf['script'] = $this->getRelativePath(PATH_typo3,$fullpath.'/'.$MCONF['script']);
00440                                 } else {
00441                                         $modconf['script'] = 'dummy.php';
00442                                 }
00443                                         // Default tab setting
00444                                 if ($MCONF['defaultMod'])       {
00445                                         $modconf['defaultMod'] = $MCONF['defaultMod'];
00446                                 }
00447                                         // Navigation Frame Script (GET params could be added)
00448                                 if ($MCONF['navFrameScript']) {
00449                                         $navFrameScript = explode('?', $MCONF['navFrameScript']);
00450                                         $navFrameScript = $navFrameScript[0];
00451                                         if (@file_exists($path.'/'.$navFrameScript))    {
00452                                                 $modconf['navFrameScript'] = $this->getRelativePath(PATH_typo3,$fullpath.'/'.$MCONF['navFrameScript']);
00453                                         }
00454                                 }
00455                                         // additional params for Navigation Frame Script: "&anyParam=value&moreParam=1"
00456                                 if ($MCONF['navFrameScriptParam']) {
00457                                         $modconf['navFrameScriptParam'] = $MCONF['navFrameScriptParam'];
00458                                 }
00459                         } else return false;
00460                 } else $modconf = 'notFound';
00461                 return $modconf;
00462         }
00463 
00471         function checkModAccess($name,$MCONF)   {
00472                 if ($MCONF['access'])   {
00473                         $access=strtolower($MCONF['access']);
00474                                 // Checking if admin-access is required
00475                         if (strstr($access,'admin'))    {       // If admin-permissions is required then return true if user is admin
00476                                 if ($this->BE_USER->isAdmin())  {return true;}
00477                         }
00478                                 // This will add modules to the select-lists of user and groups
00479                         if (strstr($access,'user'))     {       $this->modListUser[]=$name;             }
00480                         if (strstr($access,'group'))    {       $this->modListGroup[]=$name;    }
00481                                 // This checks if a user is permitted to access the module
00482                         if ($this->BE_USER->isAdmin() || $this->BE_USER->check('modules',$name))        {return true;}  // If admin you can always access a module
00483 
00484                 } else return true;     // If conf[access] is not set, then permission IS granted!
00485         }
00486 
00495         function checkModWorkspace($name,$MCONF)        {
00496                 if ($this->observeWorkspaces)   {
00497                         $status = TRUE;
00498                         if ($MCONF['workspaces'])       {
00499                                 $status = FALSE;
00500                                 if (($this->BE_USER->workspace===0 && t3lib_div::inList($MCONF['workspaces'],'online')) ||
00501                                         ($this->BE_USER->workspace===-1 && t3lib_div::inList($MCONF['workspaces'],'offline')) ||
00502                                         ($this->BE_USER->workspace>0 && t3lib_div::inList($MCONF['workspaces'],'custom')))      {
00503                                                 $status = TRUE;
00504                                 }
00505                         } elseif ($this->BE_USER->workspace===-99)      {
00506                                 $status = FALSE;
00507                         }
00508                         return $status;
00509                 } else return TRUE;
00510         }
00511 
00519         function parseModulesArray($arr)        {
00520                 $theMods = Array();
00521                 if (is_array($arr))     {
00522                         foreach($arr as $mod => $subs)  {
00523                                 $mod = $this->cleanName($mod);          // clean module name to alphanum
00524                                 if ($mod)       {
00525                                         if ($subs)      {
00526                                                 $subsArr = t3lib_div::trimExplode(',', $subs);
00527                                                 foreach($subsArr as $subMod)    {
00528                                                         $subMod = $this->cleanName($subMod);
00529                                                         if ($subMod)    {
00530                                                                 $theMods[$mod][] = $subMod;
00531                                                         }
00532                                                 }
00533                                         } else {
00534                                                 $theMods[$mod] = 1;
00535                                         }
00536                                 }
00537                         }
00538                 }
00539                 return $theMods;
00540         }
00541 
00548         function cleanName ($str)       {
00549                 return preg_replace('/[^a-z0-9]/i','',$str);
00550         }
00551 
00559         function getRelativePath($baseDir,$destDir){
00560                         // By Rene Fritz
00561                         // a special case , the dirs are equals
00562                 if ($baseDir == $destDir){
00563                         return './';
00564                 }
00565 
00566                 $baseDir = ereg_replace ('^/', '', $baseDir);   // remove beginning
00567                 $destDir = ereg_replace ('^/', '', $destDir);
00568 
00569                 $found = true;
00570                 $slash_pos=0;
00571 
00572                 do {
00573                         $slash_pos = strpos ($destDir, '/');
00574                         if (substr($destDir, 0, $slash_pos) == substr($baseDir, 0, $slash_pos)){
00575                                 $baseDir = substr($baseDir, $slash_pos+1);
00576                                 $destDir = substr($destDir, $slash_pos+1);
00577                         } else {
00578                                 $found = false;
00579                         }
00580                 } while($found == true);
00581 
00582                 $slashes = strlen ($baseDir) - strlen (str_replace('/', '', $baseDir));
00583                 for($i=0;$i < $slashes;$i++)    {
00584                         $destDir = '../'.$destDir;
00585                 }
00586                 return $destDir;
00587         }
00588 }
00589 
00590 
00591 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_loadmodules.php'])       {
00592         include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_loadmodules.php']);
00593 }
00594 ?>