Main Page | Directories | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages | Examples

class.t3lib_loadmodules.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2004 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 ***************************************************************/
00078 class t3lib_loadModules {
00079    var $modules = Array();    // After the init() function this array will contain the structure of available modules for the backend user.
00080    var $absPathArray = array();  // Array with paths pointing to the location of modules from extensions
00081 
00082    var $modListGroup = Array();     // this array will hold the elements that should go into the select-list of modules for groups...
00083    var $modListUser = Array();      // this array will hold the elements that should go into the select-list of modules for users...
00084 
00085    var $BE_USER = '';   // The backend user for use internally
00086 
00087 
00097    function load($modulesArray,$BE_USER='')  {
00098          // Setting the backend user for use internally
00099       if (is_object($BE_USER))   {
00100          $this->BE_USER = $BE_USER;
00101       } else {
00102          $this->BE_USER = $GLOBALS['BE_USER'];
00103       }
00104 
00105          /*
00106 
00107           $modulesArray might look like this when entering this function.
00108           Notice the two modules added by extensions - they have a path attached
00109 
00110          Array
00111          (
00112              [web] => list,info,perm,func
00113              [file] => list
00114              [doc] =>
00115              [user] =>
00116              [tools] => em,install,txphpmyadmin
00117              [help] => about
00118              [_PATHS] => Array
00119                  (
00120                      [tools_install] => /www/htdocs/typo3/32/coreinstall/typo3/ext/install/mod/
00121                      [tools_txphpmyadmin] => /www/htdocs/typo3/32/coreinstall/typo3/ext/phpmyadmin/modsub/
00122                  )
00123 
00124          )
00125 
00126           */
00127          //
00128       $this->absPathArray = $modulesArray['_PATHS'];
00129       unset($modulesArray['_PATHS']);
00130 
00131          /*
00132             With the above data for modules the result of this function call will be:
00133 
00134             Array
00135             (
00136                 [web] => Array
00137                     (
00138                         [0] => list
00139                         [1] => info
00140                         [2] => perm
00141                         [3] => func
00142                     )
00143 
00144                 [file] => Array
00145                     (
00146                         [0] => list
00147                     )
00148 
00149                 [doc] => 1
00150                 [user] => 1
00151                 [tools] => Array
00152                     (
00153                         [0] => em
00154                         [1] => install
00155                         [2] => txphpmyadmin
00156                     )
00157 
00158                 [help] => Array
00159                     (
00160                         [0] => about
00161                     )
00162 
00163             )
00164          */
00165       $theMods = $this->parseModulesArray($modulesArray);
00166 
00167          /*
00168             Originally modules were found in typo3/mod/
00169             User defined modules were found in ../typo3conf/
00170 
00171             Today almost all modules reside in extensions and they are found by the _PATHS array of the incoming $TBE_MODULES array
00172          */
00173          // Setting paths for 1) core modules (old concept from mod/) and 2) user-defined modules (from ../typo3conf)
00174       $paths = array();
00175       $paths['defMods'] = PATH_typo3.'mod/'; // Path of static modules
00176       $paths['userMods'] = PATH_typo3.'../typo3conf/';  // local modules (maybe frontend specific)
00177 
00178          // Traverses the module setup and creates the internal array $this->modules
00179       foreach($theMods as $mods => $subMod)  {
00180          unset ($path);
00181 
00182          $extModRelPath = $this->checkExtensionModule($mods);
00183          if ($extModRelPath)  {  // EXTENSION module:
00184             $theMainMod = $this->checkMod($mods,PATH_site.$extModRelPath);
00185             if (is_array($theMainMod) || $theMainMod!='notFound') {
00186                $path = 1;  // ... just so it goes on... submodules cannot be within this path!
00187             }
00188          } else { // 'CLASSIC' module
00189                // Checking for typo3/mod/ module existence...
00190             $theMainMod = $this->checkMod($mods,$paths['defMods'].$mods);
00191             if (is_array($theMainMod) || $theMainMod!='notFound') {
00192                $path = $paths['defMods'];
00193             } else {
00194                   // If not typo3/mod/ then it could be user-defined in typo3conf/ ...?
00195                $theMainMod = $this->checkMod($mods,$paths['userMods'].$mods);
00196                if (is_array($theMainMod) || $theMainMod!='notFound') {
00197                   $path = $paths['userMods'];
00198                }
00199             }
00200          }
00201 
00202             // if $theMainMod is not set (false) there is no access to the module !(?)
00203          if ($theMainMod && isset($path)) {
00204             $this->modules[$mods] = $theMainMod;
00205 
00206                // SUBMODULES - if any - are loaded (The 'doc' module cannot have submodules...)
00207             if ($mods!='doc' && is_array($subMod)) {
00208                foreach($subMod as $valsub)   {
00209                   $extModRelPath = $this->checkExtensionModule($mods.'_'.$valsub);
00210                   if ($extModRelPath)  {  // EXTENSION submodule:
00211                      $theTempSubMod = $this->checkMod($mods.'_'.$valsub,PATH_site.$extModRelPath);
00212                      if (is_array($theTempSubMod)) {  // default sub-module in either main-module-path, be it the default or the userdefined.
00213                         $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
00214                      }
00215                   } else { // 'CLASSIC' submodule
00216                         // Checking for typo3/mod/xxx/ module existence...
00217                      $theTempSubMod = $this->checkMod($mods.'_'.$valsub,$path.$mods.'/'.$valsub);
00218                      if (is_array($theTempSubMod)) {  // default sub-module in either main-module-path, be it the default or the userdefined.
00219                         $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
00220                      } 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!
00221                         $theTempSubMod = $this->checkMod($mods.'_'.$valsub,$paths['userMods'].$mods.'/'.$valsub);
00222                         if (is_array($theTempSubMod)) {
00223                            $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
00224                         }
00225                      }
00226                   }
00227                }
00228             }
00229          } else { // This must be done in order to fill out the select-lists for modules correctly!!
00230             if ($mods!='doc' && is_array($subMod)) {
00231                foreach($subMod as $valsub)   {
00232                   $this->checkMod($mods.'_'.$valsub,$path.$mods.'/'.$valsub);
00233                }
00234             }
00235          }
00236       }
00237    /*
00238       At this point $this->modules should look like this:
00239       Only modules which were accessible to the $BE_USER is listed in this array.
00240 
00241       Array
00242       (
00243           [web] => Array
00244               (
00245                   [name] => web
00246                   [script] => dummy.php
00247                   [defaultMod] => list
00248                   [sub] => Array
00249                       (
00250                           [list] => Array
00251                               (
00252                                   [name] => web_list
00253                                   [script] => mod/web/list/../../../db_list.php
00254                               )
00255 
00256                           [info] => Array
00257                               (
00258                                   [name] => web_info
00259                                   [script] => mod/web/info/index.php
00260                               )
00261 
00262                           [perm] => Array
00263                               (
00264                                   [name] => web_perm
00265                                   [script] => mod/web/perm/index.php
00266                               )
00267 
00268                           [func] => Array
00269                               (
00270                                   [name] => web_func
00271                                   [script] => mod/web/func/index.php
00272                               )
00273 
00274                       )
00275 
00276               )
00277 
00278           [file] => Array
00279               (
00280                   [name] => file
00281                   [script] => dummy.php
00282                   [sub] => Array
00283                       (
00284                           [list] => Array
00285                               (
00286                                   [name] => file_list
00287                                   [script] => mod/file/list/../../../file_list.php
00288                               )
00289 
00290                       )
00291 
00292               )
00293 
00294           [doc] => Array
00295               (
00296                   [name] => doc
00297                   [script] => mod/doc/../../alt_doc.php
00298               )
00299 
00300           [user] => Array
00301               (
00302                   [name] => user
00303                   [script] => dummy.php
00304                   [defaultMod] => task
00305               )
00306 
00307           [tools] => Array
00308               (
00309                   [name] => tools
00310                   [script] => dummy.php
00311                   [sub] => Array
00312                       (
00313                           [em] => Array
00314                               (
00315                                   [name] => tools_em
00316                                   [script] => mod/tools/em/index.php
00317                               )
00318 
00319                           [install] => Array
00320                               (
00321                                   [name] => tools_install
00322                                   [script] => ext/install/mod/../../../install/index.php
00323                               )
00324 
00325                           [txphpmyadmin] => Array
00326                               (
00327                                   [name] => tools_txphpmyadmin
00328                                   [script] => ext/phpmyadmin/modsub/index.php
00329                               )
00330 
00331                       )
00332 
00333               )
00334 
00335           [help] => Array
00336               (
00337                   [name] => help
00338                   [script] => dummy.php
00339                   [defaultMod] => welcome
00340                   [sub] => Array
00341                       (
00342                           [about] => Array
00343                               (
00344                                   [name] => help_about
00345                                   [script] => mod/help/about/index.php
00346                               )
00347 
00348                       )
00349 
00350               )
00351 
00352       )
00353 
00354    */
00355 
00356 #debug($this->modules);
00357 #debug($GLOBALS['LANG']->moduleLabels);
00358    }
00359 
00366    function checkExtensionModule($name)   {
00367       global $TYPO3_LOADED_EXT;
00368 
00369       if (isset($this->absPathArray[$name])) {
00370          return ereg_replace ('\/$', '', substr($this->absPathArray[$name],strlen(PATH_site)));
00371       }
00372    }
00373 
00385    function checkMod($name, $fullpath) {
00386       $modconf=Array();
00387       $path = ereg_replace ('/[^/.]+/\.\./', '/', $fullpath); // because 'path/../path' does not work
00388       if (@is_dir($path) && @file_exists($path.'/conf.php'))   {
00389          include($path.'/conf.php');   // The conf-file is included. This must be valid PHP.
00390          if (!$MCONF['shy'] && $this->checkModAccess($name,$MCONF))  {
00391             $modconf['name']=$name;
00392                // language processing. This will add module labels and image reference to the internal ->moduleLabels array of the LANG object.
00393             if (is_object($GLOBALS['LANG'])) {
00394                   // $MLANG['default']['tabs_images']['tab'] is for modules the reference to the module icon.
00395                   // Here the path is transformed to an absolute reference.
00396                if ($MLANG['default']['tabs_images']['tab']) {
00397 
00398                      // Initializing search for alternative icon:
00399                   $altIconKey = 'MOD:'.$name.'/'.$MLANG['default']['tabs_images']['tab'];    // Alternative icon key (might have an alternative set in $TBE_STYLES['skinImg']
00400                   $altIconAbsPath = is_array($GLOBALS['TBE_STYLES']['skinImg'][$altIconKey]) ? t3lib_div::resolveBackPath(PATH_typo3.$GLOBALS['TBE_STYLES']['skinImg'][$altIconKey][0]) : '';
00401 
00402                      // Setting icon, either default or alternative:
00403                   if ($altIconAbsPath && @is_file($altIconAbsPath))  {
00404                      $MLANG['default']['tabs_images']['tab']=$this->getRelativePath(PATH_typo3,$altIconAbsPath);
00405                   } else {
00406                         // Setting default icon:
00407                      $MLANG['default']['tabs_images']['tab']=$this->getRelativePath(PATH_typo3,$fullpath.'/'.$MLANG['default']['tabs_images']['tab']);
00408                   }
00409 
00410                      // Finally, setting the icon with correct path:
00411                   if (substr($MLANG['default']['tabs_images']['tab'],0,3)=='../')   {
00412                      $MLANG['default']['tabs_images']['tab'] = PATH_site.substr($MLANG['default']['tabs_images']['tab'],3);
00413                   } else {
00414                      $MLANG['default']['tabs_images']['tab'] = PATH_typo3.$MLANG['default']['tabs_images']['tab'];
00415                   }
00416                }
00417 
00418                   // If LOCAL_LANG references are used for labels of the module:
00419                if ($MLANG['default']['ll_ref']) {
00420                      // Now the 'default' key is loaded with the CURRENT language - not the english translation...
00421                   $MLANG['default']['labels']['tablabel'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'].':mlang_labels_tablabel');
00422                   $MLANG['default']['labels']['tabdescr'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'].':mlang_labels_tabdescr');
00423                   $MLANG['default']['tabs']['tab'] = $GLOBALS['LANG']->sL($MLANG['default']['ll_ref'].':mlang_tabs_tab');
00424                   $GLOBALS['LANG']->addModuleLabels($MLANG['default'],$name.'_');
00425                } else { // ... otherwise use the old way:
00426                   $GLOBALS['LANG']->addModuleLabels($MLANG['default'],$name.'_');
00427                   $GLOBALS['LANG']->addModuleLabels($MLANG[$GLOBALS['LANG']->lang],$name.'_');
00428                }
00429             }
00430 
00431                // Default script setup
00432             if ($MCONF['script'] && @file_exists($path.'/'.$MCONF['script'])) {
00433                $modconf['script'] = $this->getRelativePath(PATH_typo3,$fullpath.'/'.$MCONF['script']);
00434             } else {
00435                $modconf['script'] = 'dummy.php';
00436             }
00437                // Default tab setting
00438             if ($MCONF['defaultMod'])  {
00439                $modconf['defaultMod'] = $MCONF['defaultMod'];
00440             }
00441                // Navigation Frame Script (GET params could be added)
00442             if ($MCONF['navFrameScript']) {
00443                $navFrameScript = explode('?', $MCONF['navFrameScript']);
00444                $navFrameScript = $navFrameScript[0];
00445                if (@file_exists($path.'/'.$navFrameScript)) {
00446                   $modconf['navFrameScript'] = $this->getRelativePath(PATH_typo3,$fullpath.'/'.$MCONF['navFrameScript']);
00447                }
00448             }
00449                // additional params for Navigation Frame Script: "&anyParam=value&moreParam=1"
00450             if ($MCONF['navFrameScriptParam']) {
00451                $modconf['navFrameScriptParam'] = $MCONF['navFrameScriptParam'];
00452             }
00453          } else return false;
00454       } else $modconf = 'notFound';
00455       return $modconf;
00456    }
00457 
00465    function checkModAccess($name,$MCONF)  {
00466       if ($MCONF['access'])   {
00467          $access=strtolower($MCONF['access']);
00468             // Checking if admin-access is required
00469          if (strstr($access,'admin'))  {  // If admin-permissions is required then return true if user is admin
00470             if ($this->BE_USER->isAdmin())   {return true;}
00471          }
00472             // This will add modules to the select-lists of user and groups
00473          if (strstr($access,'user'))   {  $this->modListUser[]=$name;      }
00474          if (strstr($access,'group'))  {  $this->modListGroup[]=$name;  }
00475             // This checks if a user is permitted to access the module
00476          if ($this->BE_USER->isAdmin() || $this->BE_USER->check('modules',$name))   {return true;} // If admin you can always access a module
00477 
00478       } else return true;  // If conf[access] is not set, then permission IS granted!
00479    }
00480 
00488    function parseModulesArray($arr) {
00489       $theMods = Array();
00490       if (is_array($arr))  {
00491          foreach($arr as $mod => $subs)   {
00492             $mod = $this->cleanName($mod);      // clean module name to alphanum
00493             if ($mod)   {
00494                if ($subs)  {
00495                   $subsArr = t3lib_div::trimExplode(',', $subs);
00496                   foreach($subsArr as $subMod)  {
00497                      $subMod = $this->cleanName($subMod);
00498                      if ($subMod)   {
00499                         $theMods[$mod][] = $subMod;
00500                      }
00501                   }
00502                } else {
00503                   $theMods[$mod] = 1;
00504                }
00505             }
00506          }
00507       }
00508       return $theMods;
00509    }
00510 
00517    function cleanName ($str)  {
00518       return ereg_replace('[^A-Za-z0-9]*','',$str);
00519    }
00520 
00528    function getRelativePath($baseDir,$destDir){
00529          // By Rene Fritz
00530          // a special case , the dirs are equals
00531       if ($baseDir == $destDir){
00532          return './';
00533       }
00534 
00535       $baseDir = ereg_replace ('^/', '', $baseDir);   // remove beginning
00536       $destDir = ereg_replace ('^/', '', $destDir);
00537 
00538       $found = true;
00539       $slash_pos=0;
00540 
00541       do {
00542          $slash_pos = strpos ($destDir, '/');
00543          if (substr($destDir, 0, $slash_pos) == substr($baseDir, 0, $slash_pos)){
00544             $baseDir = substr($baseDir, $slash_pos+1);
00545             $destDir = substr($destDir, $slash_pos+1);
00546          } else {
00547             $found = false;
00548          }
00549       } while($found == true);
00550 
00551       $slashes = strlen ($baseDir) - strlen (str_replace('/', '', $baseDir));
00552       for($i=0;$i < $slashes;$i++)  {
00553          $destDir = '../'.$destDir;
00554       }
00555       return $destDir;
00556    }
00557 }
00558 
00559 
00560 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_loadmodules.php'])   {
00561    include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_loadmodules.php']);
00562 }
00563 ?>

Generated on Sun Oct 3 01:05:48 2004 for TYPO3core 3.7.0 dev by  doxygen 1.3.8-20040913