/src/typo3_src-4.2.0alpha1/t3lib/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 
00091         var $BE_USER = '';
00092         var $observeWorkspaces = FALSE;         // If set true, workspace "permissions" will be observed so non-allowed modules will not be included in the array of modules.
00093 
00094 
00104         function load($modulesArray,$BE_USER='')        {
00105                         // Setting the backend user for use internally
00106                 if (is_object($BE_USER))        {
00107                         $this->BE_USER = $BE_USER;
00108                 } else {
00109                         $this->BE_USER = $GLOBALS['BE_USER'];
00110                 }
00111 
00112                         /*
00113 
00114                          $modulesArray might look like this when entering this function.
00115                          Notice the two modules added by extensions - they have a path attached
00116 
00117                         Array
00118                         (
00119                             [web] => list,info,perm,func
00120                             [file] => list
00121                             [doc] =>
00122                             [user] =>
00123                             [tools] => em,install,txphpmyadmin
00124                             [help] => about
00125                             [_PATHS] => Array
00126                                 (
00127                                     [tools_install] => /www/htdocs/typo3/32/coreinstall/typo3/ext/install/mod/
00128                                     [tools_txphpmyadmin] => /www/htdocs/typo3/32/coreinstall/typo3/ext/phpmyadmin/modsub/
00129                                 )
00130 
00131                         )
00132 
00133                          */
00134                         //
00135                 $this->absPathArray = $modulesArray['_PATHS'];
00136                 unset($modulesArray['_PATHS']);
00137 
00138                         /*
00139                                 With the above data for modules the result of this function call will be:
00140 
00141                                 Array
00142                                 (
00143                                     [web] => Array
00144                                         (
00145                                             [0] => list
00146                                             [1] => info
00147                                             [2] => perm
00148                                             [3] => func
00149                                         )
00150 
00151                                     [file] => Array
00152                                         (
00153                                             [0] => list
00154                                         )
00155 
00156                                     [doc] => 1
00157                                     [user] => 1
00158                                     [tools] => Array
00159                                         (
00160                                             [0] => em
00161                                             [1] => install
00162                                             [2] => txphpmyadmin
00163                                         )
00164 
00165                                     [help] => Array
00166                                         (
00167                                             [0] => about
00168                                         )
00169 
00170                                 )
00171                         */
00172                 $theMods = $this->parseModulesArray($modulesArray);
00173 
00174                         /*
00175                                 Originally modules were found in typo3/mod/
00176                                 User defined modules were found in ../typo3conf/
00177 
00178                                 Today almost all modules reside in extensions and they are found by the _PATHS array of the incoming $TBE_MODULES array
00179                         */
00180                         // Setting paths for 1) core modules (old concept from mod/) and 2) user-defined modules (from ../typo3conf)
00181                 $paths = array();
00182                 $paths['defMods'] = PATH_typo3.'mod/';  // Path of static modules
00183                 $paths['userMods'] = PATH_typo3.'../typo3conf/';  // local modules (maybe frontend specific)
00184 
00185                         // Traverses the module setup and creates the internal array $this->modules
00186                 foreach($theMods as $mods => $subMod)   {
00187                         $path = NULL;
00188 
00189                         $extModRelPath = $this->checkExtensionModule($mods);
00190                         if ($extModRelPath)     {       // EXTENSION module:
00191                                 $theMainMod = $this->checkMod($mods,PATH_site.$extModRelPath);
00192                                 if (is_array($theMainMod) || $theMainMod!='notFound')   {
00193                                         $path = 1;      // ... just so it goes on... submodules cannot be within this path!
00194                                 }
00195                         } else {        // 'CLASSIC' module
00196                                         // Checking for typo3/mod/ module existence...
00197                                 $theMainMod = $this->checkMod($mods,$paths['defMods'].$mods);
00198                                 if (is_array($theMainMod) || $theMainMod!='notFound')   {
00199                                         $path = $paths['defMods'];
00200                                 } else {
00201                                                 // If not typo3/mod/ then it could be user-defined in typo3conf/ ...?
00202                                         $theMainMod = $this->checkMod($mods,$paths['userMods'].$mods);
00203                                         if (is_array($theMainMod) || $theMainMod!='notFound')   {
00204                                                 $path = $paths['userMods'];
00205                                         }
00206                                 }
00207                         }
00208 
00209                                 // if $theMainMod is not set (false) there is no access to the module !(?)
00210                         if ($theMainMod && !is_null($path))     {
00211                                 $this->modules[$mods] = $theMainMod;
00212 
00213                                         // SUBMODULES - if any - are loaded (The 'doc' module cannot have submodules...)
00214                                 if ($mods!='doc' && is_array($subMod))  {
00215                                         foreach($subMod as $valsub)     {
00216                                                 $extModRelPath = $this->checkExtensionModule($mods.'_'.$valsub);
00217                                                 if ($extModRelPath)     {       // EXTENSION submodule:
00218                                                         $theTempSubMod = $this->checkMod($mods.'_'.$valsub,PATH_site.$extModRelPath);
00219                                                         if (is_array($theTempSubMod))   {       // default sub-module in either main-module-path, be it the default or the userdefined.
00220                                                                 $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
00221                                                         }
00222                                                 } else {        // 'CLASSIC' submodule
00223                                                                 // Checking for typo3/mod/xxx/ module existence...
00224 // FIXME what about $path = 1; from above and using $path as string here?
00225                                                         $theTempSubMod = $this->checkMod($mods.'_'.$valsub,$path.$mods.'/'.$valsub);
00226                                                         if (is_array($theTempSubMod))   {       // default sub-module in either main-module-path, be it the default or the userdefined.
00227                                                                 $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
00228                                                         } 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!
00229                                                                 $theTempSubMod = $this->checkMod($mods.'_'.$valsub,$paths['userMods'].$mods.'/'.$valsub);
00230                                                                 if (is_array($theTempSubMod))   {
00231                                                                         $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
00232                                                                 }
00233                                                         }
00234                                                 }
00235                                         }
00236                                 }
00237                         } else {        // This must be done in order to fill out the select-lists for modules correctly!!
00238                                 if ($mods!='doc' && is_array($subMod))  {
00239                                         foreach($subMod as $valsub)     {
00240 // FIXME path can only be NULL here, or not?
00241                                                 $this->checkMod($mods.'_'.$valsub,$path.$mods.'/'.$valsub);
00242                                         }
00243                                 }
00244                         }
00245                 }
00246         /*
00247                 At this point $this->modules should look like this:
00248                 Only modules which were accessible to the $BE_USER is listed in this array.
00249 
00250                 Array
00251                 (
00252                     [web] => Array
00253                         (
00254                             [name] => web
00255                             [script] => dummy.php
00256                             [defaultMod] => list
00257                             [sub] => Array
00258                                 (
00259                                     [list] => Array
00260                                         (
00261                                             [name] => web_list
00262                                             [script] => mod/web/list/../../../db_list.php
00263                                         )
00264 
00265                                     [info] => Array
00266                                         (
00267                                             [name] => web_info
00268                                             [script] => mod/web/info/index.php
00269                                         )
00270 
00271                                     [perm] => Array
00272                                         (
00273                                             [name] => web_perm
00274                                             [script] => mod/web/perm/index.php
00275                                         )
00276 
00277                                     [func] => Array
00278                                         (
00279                                             [name] => web_func
00280                                             [script] => mod/web/func/index.php
00281                                         )
00282 
00283                                 )
00284 
00285                         )
00286 
00287                     [file] => Array
00288                         (
00289                             [name] => file
00290                             [script] => dummy.php
00291                             [sub] => Array
00292                                 (
00293                                     [list] => Array
00294                                         (
00295                                             [name] => file_list
00296                                             [script] => mod/file/list/../../../file_list.php
00297                                         )
00298 
00299                                 )
00300 
00301                         )
00302 
00303                     [doc] => Array
00304                         (
00305                             [name] => doc
00306                             [script] => mod/doc/../../alt_doc.php
00307                         )
00308 
00309                     [user] => Array
00310                         (
00311                             [name] => user
00312                             [script] => dummy.php
00313                             [defaultMod] => task
00314                         )
00315 
00316                     [tools] => Array
00317                         (
00318                             [name] => tools
00319                             [script] => dummy.php
00320                             [sub] => Array
00321                                 (
00322                                     [em] => Array
00323                                         (
00324                                             [name] => tools_em
00325                                             [script] => mod/tools/em/index.php
00326                                         )
00327 
00328                                     [install] => Array
00329                                         (
00330                                             [name] => tools_install
00331                                             [script] => ext/install/mod/../../../install/index.php
00332                                         )
00333 
00334                                     [txphpmyadmin] => Array
00335                                         (
00336                                             [name] => tools_txphpmyadmin
00337                                             [script] => ext/phpmyadmin/modsub/index.php
00338                                         )
00339 
00340                                 )
00341 
00342                         )
00343 
00344                     [help] => Array
00345                         (
00346                             [name] => help
00347                             [script] => dummy.php
00348                             [defaultMod] => welcome
00349                             [sub] => Array
00350                                 (
00351                                     [about] => Array
00352                                         (
00353                                             [name] => help_about
00354                                             [script] => mod/help/about/index.php
00355                                         )
00356 
00357                                 )
00358 
00359                         )
00360 
00361                 )
00362 
00363         */
00364 
00365 #debug($this->modules);
00366 #debug($GLOBALS['LANG']->moduleLabels);
00367         }
00368 
00375         function checkExtensionModule($name)    {
00376                 global $TYPO3_LOADED_EXT;
00377 
00378                 if (isset($this->absPathArray[$name]))  {
00379                         return ereg_replace ('\/$', '', substr($this->absPathArray[$name],strlen(PATH_site)));
00380                 }
00381         }
00382 
00394         function checkMod($name, $fullpath)     {
00395                 $modconf=Array();
00396                 $path = ereg_replace ('/[^/.]+/\.\./', '/', $fullpath); // because 'path/../path' does not work
00397                 if (@is_dir($path) && @file_exists($path.'/conf.php'))  {
00398                         $MCONF = array();
00399                         $MLANG = array();
00400                         include($path.'/conf.php');     // The conf-file is included. This must be valid PHP.
00401                         if (!$MCONF['shy'] && $this->checkModAccess($name,$MCONF) && $this->checkModWorkspace($name,$MCONF))    {
00402                                 $modconf['name']=$name;
00403                                         // language processing. This will add module labels and image reference to the internal ->moduleLabels array of the LANG object.
00404                                 if (is_object($GLOBALS['LANG']))        {
00405                                                 // $MLANG['default']['tabs_images']['tab'] is for modules the reference to the module icon.
00406                                                 // Here the path is transformed to an absolute reference.
00407                                         if ($MLANG['default']['tabs_images']['tab'])    {
00408 
00409                                                         // Initializing search for alternative icon:
00410                                                 $altIconKey = 'MOD:'.$name.'/'.$MLANG['default']['tabs_images']['tab'];         // Alternative icon key (might have an alternative set in $TBE_STYLES['skinImg']
00411                                                 $altIconAbsPath = is_array($GLOBALS['TBE_STYLES']['skinImg'][$altIconKey]) ? t3lib_div::resolveBackPath(PATH_typo3.$GLOBALS['TBE_STYLES']['skinImg'][$altIconKey][0]) : '';
00412 
00413                                                         // Setting icon, either default or alternative:
00414                                                 if ($altIconAbsPath && @is_file($altIconAbsPath))       {
00415                                                         $MLANG['default']['tabs_images']['tab']=$this->getRelativePath(PATH_typo3,$altIconAbsPath);
00416                                                 } else {
00417                                                                 // Setting default icon:
00418                                                         $MLANG['default']['tabs_images']['tab']=$this->getRelativePath(PATH_typo3,$fullpath.'/'.$MLANG['default']['tabs_images']['tab']);
00419                                                 }
00420 
00421                                                         // Finally, setting the icon with correct path:
00422                                                 if (substr($MLANG['default']['tabs_images']['tab'],0,3)=='../') {
00423                                                         $MLANG['default']['tabs_images']['tab'] = PATH_site.substr($MLANG['default']['tabs_images']['tab'],3);
00424                                                 } else {
00425                                                         $MLANG['default']['tabs_images']['tab'] = PATH_typo3.$MLANG['default']['tabs_images']['tab'];
00426                                                 }
00427                                         }
00428 
00429                                                 // If LOCAL_LANG references are used for labels of the module:
00430                                         if ($MLANG['default']['ll_ref'])        {
00431                                                         // Now the 'default' key is loaded with the CURRENT language - not the english translation...
00432                                                 $MLANG['default']['labels']['tablabel'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'].':mlang_labels_tablabel');
00433                                                 $MLANG['default']['labels']['tabdescr'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'].':mlang_labels_tabdescr');
00434                                                 $MLANG['default']['tabs']['tab'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'].':mlang_tabs_tab');
00435                                                 $GLOBALS['LANG']->addModuleLabels($MLANG['default'],$name.'_');
00436                                         } else {        // ... otherwise use the old way:
00437                                                 $GLOBALS['LANG']->addModuleLabels($MLANG['default'],$name.'_');
00438                                                 $GLOBALS['LANG']->addModuleLabels($MLANG[$GLOBALS['LANG']->lang],$name.'_');
00439                                         }
00440                                 }
00441 
00442                                         // Default script setup
00443                                 if ($MCONF['script']==='_DISPATCH')     {
00444                                         $modconf['script'] = 'mod.php?M='.rawurlencode($name);
00445                                 } elseif ($MCONF['script'] && @file_exists($path.'/'.$MCONF['script'])) {
00446                                         $modconf['script'] = $this->getRelativePath(PATH_typo3,$fullpath.'/'.$MCONF['script']);
00447                                 } else {
00448                                         $modconf['script'] = 'dummy.php';
00449                                 }
00450                                         // Default tab setting
00451                                 if ($MCONF['defaultMod'])       {
00452                                         $modconf['defaultMod'] = $MCONF['defaultMod'];
00453                                 }
00454                                         // Navigation Frame Script (GET params could be added)
00455                                 if ($MCONF['navFrameScript']) {
00456                                         $navFrameScript = explode('?', $MCONF['navFrameScript']);
00457                                         $navFrameScript = $navFrameScript[0];
00458                                         if (@file_exists($path.'/'.$navFrameScript))    {
00459                                                 $modconf['navFrameScript'] = $this->getRelativePath(PATH_typo3,$fullpath.'/'.$MCONF['navFrameScript']);
00460                                         }
00461                                 }
00462                                         // additional params for Navigation Frame Script: "&anyParam=value&moreParam=1"
00463                                 if ($MCONF['navFrameScriptParam']) {
00464                                         $modconf['navFrameScriptParam'] = $MCONF['navFrameScriptParam'];
00465                                 }
00466                         } else return false;
00467                 } else $modconf = 'notFound';
00468                 return $modconf;
00469         }
00470 
00478         function checkModAccess($name,$MCONF)   {
00479                 if ($MCONF['access'])   {
00480                         $access=strtolower($MCONF['access']);
00481                                 // Checking if admin-access is required
00482                         if (strstr($access,'admin'))    {       // If admin-permissions is required then return true if user is admin
00483                                 if ($this->BE_USER->isAdmin())  {return true;}
00484                         }
00485                                 // This will add modules to the select-lists of user and groups
00486                         if (strstr($access,'user'))     {       $this->modListUser[]=$name;             }
00487                         if (strstr($access,'group'))    {       $this->modListGroup[]=$name;    }
00488                                 // This checks if a user is permitted to access the module
00489                         if ($this->BE_USER->isAdmin() || $this->BE_USER->check('modules',$name))        {return true;}  // If admin you can always access a module
00490 
00491                 } else return true;     // If conf[access] is not set, then permission IS granted!
00492         }
00493 
00502         function checkModWorkspace($name,$MCONF)        {
00503                 if ($this->observeWorkspaces)   {
00504                         $status = TRUE;
00505                         if ($MCONF['workspaces'])       {
00506                                 $status = FALSE;
00507                                 if (($this->BE_USER->workspace===0 && t3lib_div::inList($MCONF['workspaces'],'online')) ||
00508                                         ($this->BE_USER->workspace===-1 && t3lib_div::inList($MCONF['workspaces'],'offline')) ||
00509                                         ($this->BE_USER->workspace>0 && t3lib_div::inList($MCONF['workspaces'],'custom')))      {
00510                                                 $status = TRUE;
00511                                 }
00512                         } elseif ($this->BE_USER->workspace===-99)      {
00513                                 $status = FALSE;
00514                         }
00515                         return $status;
00516                 } else return TRUE;
00517         }
00518 
00526         function parseModulesArray($arr)        {
00527                 $theMods = Array();
00528                 if (is_array($arr))     {
00529                         foreach($arr as $mod => $subs)  {
00530                                 $mod = $this->cleanName($mod);          // clean module name to alphanum
00531                                 if ($mod)       {
00532                                         if ($subs)      {
00533                                                 $subsArr = t3lib_div::trimExplode(',', $subs);
00534                                                 foreach($subsArr as $subMod)    {
00535                                                         $subMod = $this->cleanName($subMod);
00536                                                         if ($subMod)    {
00537                                                                 $theMods[$mod][] = $subMod;
00538                                                         }
00539                                                 }
00540                                         } else {
00541                                                 $theMods[$mod] = 1;
00542                                         }
00543                                 }
00544                         }
00545                 }
00546                 return $theMods;
00547         }
00548 
00555         function cleanName ($str)       {
00556                 return preg_replace('/[^a-z0-9]/i','',$str);
00557         }
00558 
00566         function getRelativePath($baseDir,$destDir){
00567                         // By Rene Fritz
00568                         // a special case , the dirs are equals
00569                 if ($baseDir == $destDir){
00570                         return './';
00571                 }
00572 
00573                 $baseDir = ereg_replace ('^/', '', $baseDir);   // remove beginning
00574                 $destDir = ereg_replace ('^/', '', $destDir);
00575 
00576                 $found = true;
00577                 $slash_pos=0;
00578 
00579                 do {
00580                         $slash_pos = strpos ($destDir, '/');
00581                         if (substr($destDir, 0, $slash_pos) == substr($baseDir, 0, $slash_pos)){
00582                                 $baseDir = substr($baseDir, $slash_pos+1);
00583                                 $destDir = substr($destDir, $slash_pos+1);
00584                         } else {
00585                                 $found = false;
00586                         }
00587                 } while($found == true);
00588 
00589                 $slashes = strlen ($baseDir) - strlen (str_replace('/', '', $baseDir));
00590                 for($i=0;$i < $slashes;$i++)    {
00591                         $destDir = '../'.$destDir;
00592                 }
00593                 return $destDir;
00594         }
00595 }
00596 
00597 
00598 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_loadmodules.php'])       {
00599         include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_loadmodules.php']);
00600 }
00601 ?>

This documentation has been generated automatically from TYPO3 source code using Doxygen and is provided as is by Cast Iron Coding as a courtesy to other TYPO3 developers and users. Please consider Cast Iron Coding — a full-service web development agency in Portland, Oregon specializing in TYPO3 extension development — for all of your TYPO3 development and consulting needs!