Fixed bug #13675: Performance improvement: Use array_walk() for t3lib_div::intExplode
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_loadmodules.php
index 2a6862a..c712590 100644 (file)
@@ -2,7 +2,7 @@
 /***************************************************************
 *  Copyright notice
 *
-*  (c) 1999-2004 Kasper Skaarhoj (kasper@typo3.com)
+*  (c) 1999-2009 Kasper Skaarhoj (kasperYYYY@typo3.com)
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -31,7 +31,7 @@
  * Modifications by Rene Fritz, 2001
  * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
  *
- * @author     Kasper Skaarhoj <kasper@typo3.com>
+ * @author     Kasper Skaarhoj <kasperYYYY@typo3.com>
  * @internal
  */
 /**
  *
  *
  *
- *   78: class t3lib_loadModules
- *   97:     function load($modulesArray,$BE_USER='')
- *  366:     function checkExtensionModule($name)
- *  385:     function checkMod($name, $fullpath)
- *  458:     function checkModAccess($name,$MCONF)
- *  481:     function parseModulesArray ($arr)
- *  511:     function cleanName ($str)
- *  522:     function getRelativePath($baseDir,$destDir)
+ *   79: class t3lib_loadModules
+ *   99:     function load($modulesArray,$BE_USER='')
+ *  370:     function checkExtensionModule($name)
+ *  389:     function checkMod($name, $fullpath)
+ *  471:     function checkModAccess($name,$MCONF)
+ *  495:     function checkModWorkspace($name,$MCONF)
+ *  519:     function parseModulesArray($arr)
+ *  548:     function cleanName ($str)
+ *  559:     function getRelativePath($baseDir,$destDir)
  *
- * TOTAL FUNCTIONS: 7
+ * TOTAL FUNCTIONS: 8
  * (This index is automatically created/updated by the extension "extdeveval")
  *
  */
  *             $this->loadModules = t3lib_div::makeInstance('t3lib_loadModules');
  *             $this->loadModules->load($TBE_MODULES);
  *
- * @author     Kasper Skaarhoj <kasper@typo3.com>
+ * @author     Kasper Skaarhoj <kasperYYYY@typo3.com>
  * @package TYPO3
  * @subpackage t3lib
  */
 class t3lib_loadModules {
        var $modules = Array();         // After the init() function this array will contain the structure of available modules for the backend user.
-       var $absPathArray=array();      // Array with paths pointing to the location of modules from extensions
+       var $absPathArray = array();    // Array with paths pointing to the location of modules from extensions
 
-       var $modListGroup= Array();             // this array will hold the elements that should go into the select-list of modules for groups...
-       var $modListUser= Array();              // this array will hold the elements that should go into the select-list of modules for users...
+       var $modListGroup = Array();            // this array will hold the elements that should go into the select-list of modules for groups...
+       var $modListUser = Array();             // this array will hold the elements that should go into the select-list of modules for users...
 
-       var $BE_USER='';        // The backend user for use internally
+       /**
+        * The backend user for use internally
+        *
+        * @var t3lib_beUserAuth
+        */
+       var $BE_USER;
+       var $observeWorkspaces = FALSE;         // If set true, workspace "permissions" will be observed so non-allowed modules will not be included in the array of modules.
 
 
        /**
@@ -97,7 +104,7 @@ class t3lib_loadModules {
        function load($modulesArray,$BE_USER='')        {
                        // Setting the backend user for use internally
                if (is_object($BE_USER))        {
-                       $this->BE_USER=$BE_USER;
+                       $this->BE_USER = $BE_USER;
                } else {
                        $this->BE_USER = $GLOBALS['BE_USER'];
                }
@@ -111,7 +118,6 @@ class t3lib_loadModules {
                        (
                            [web] => list,info,perm,func
                            [file] => list
-                           [doc] =>
                            [user] =>
                            [tools] => em,install,txphpmyadmin
                            [help] => about
@@ -146,7 +152,6 @@ class t3lib_loadModules {
                                            [0] => list
                                        )
 
-                                   [doc] => 1
                                    [user] => 1
                                    [tools] => Array
                                        (
@@ -171,13 +176,13 @@ class t3lib_loadModules {
                                Today almost all modules reside in extensions and they are found by the _PATHS array of the incoming $TBE_MODULES array
                        */
                        // Setting paths for 1) core modules (old concept from mod/) and 2) user-defined modules (from ../typo3conf)
-               $paths=array();
+               $paths = array();
                $paths['defMods'] = PATH_typo3.'mod/';  // Path of static modules
                $paths['userMods'] = PATH_typo3.'../typo3conf/';  // local modules (maybe frontend specific)
 
                        // Traverses the module setup and creates the internal array $this->modules
-               while(list($mods,$subMod)=each($theMods))       {
-                       unset ($path);
+               foreach($theMods as $mods => $subMod)   {
+                       $path = NULL;
 
                        $extModRelPath = $this->checkExtensionModule($mods);
                        if ($extModRelPath)     {       // EXTENSION module:
@@ -200,35 +205,37 @@ class t3lib_loadModules {
                        }
 
                                // if $theMainMod is not set (false) there is no access to the module !(?)
-                       if ($theMainMod && isset($path))        {
-                               $this->modules[$mods]=$theMainMod;
+                       if ($theMainMod && !is_null($path))     {
+                               $this->modules[$mods] = $theMainMod;
 
-                                       // SUBMODULES - if any - are loaded (The 'doc' module cannot have submodules...)
-                               if ($mods!='doc' && is_array($subMod))  {
-                                       while(list(,$valsub)=each($subMod))     {
+                                       // SUBMODULES - if any - are loaded
+                               if (is_array($subMod))  {
+                                       foreach($subMod as $valsub)     {
                                                $extModRelPath = $this->checkExtensionModule($mods.'_'.$valsub);
                                                if ($extModRelPath)     {       // EXTENSION submodule:
-                                                       $theTempSubMod=$this->checkMod($mods.'_'.$valsub,PATH_site.$extModRelPath);
+                                                       $theTempSubMod = $this->checkMod($mods.'_'.$valsub,PATH_site.$extModRelPath);
                                                        if (is_array($theTempSubMod))   {       // default sub-module in either main-module-path, be it the default or the userdefined.
-                                                               $this->modules[$mods]['sub'][$valsub]=$theTempSubMod;
+                                                               $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
                                                        }
                                                } else {        // 'CLASSIC' submodule
                                                                // Checking for typo3/mod/xxx/ module existence...
-                                                       $theTempSubMod=$this->checkMod($mods.'_'.$valsub,$path.$mods.'/'.$valsub);
+// FIXME what about $path = 1; from above and using $path as string here?
+                                                       $theTempSubMod = $this->checkMod($mods.'_'.$valsub,$path.$mods.'/'.$valsub);
                                                        if (is_array($theTempSubMod))   {       // default sub-module in either main-module-path, be it the default or the userdefined.
-                                                               $this->modules[$mods]['sub'][$valsub]=$theTempSubMod;
+                                                               $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
                                                        } 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!
-                                                               $theTempSubMod=$this->checkMod($mods.'_'.$valsub,$paths['userMods'].$mods.'/'.$valsub);
+                                                               $theTempSubMod = $this->checkMod($mods.'_'.$valsub,$paths['userMods'].$mods.'/'.$valsub);
                                                                if (is_array($theTempSubMod))   {
-                                                                       $this->modules[$mods]['sub'][$valsub]=$theTempSubMod;
+                                                                       $this->modules[$mods]['sub'][$valsub] = $theTempSubMod;
                                                                }
                                                        }
                                                }
                                        }
                                }
                        } else {        // This must be done in order to fill out the select-lists for modules correctly!!
-                               if ($mods!='doc' && is_array($subMod))  {
-                                       while(list(,$valsub)=each($subMod))     {
+                               if (is_array($subMod))  {
+                                       foreach($subMod as $valsub)     {
+// FIXME path can only be NULL here, or not?
                                                $this->checkMod($mods.'_'.$valsub,$path.$mods.'/'.$valsub);
                                        }
                                }
@@ -291,12 +298,6 @@ class t3lib_loadModules {
 
                        )
 
-                   [doc] => Array
-                       (
-                           [name] => doc
-                           [script] => mod/doc/../../alt_doc.php
-                       )
-
                    [user] => Array
                        (
                            [name] => user
@@ -367,15 +368,15 @@ class t3lib_loadModules {
                global $TYPO3_LOADED_EXT;
 
                if (isset($this->absPathArray[$name]))  {
-                       return ereg_replace ('\/$', '', substr($this->absPathArray[$name],strlen(PATH_site)));
+                       return rtrim(substr($this->absPathArray[$name],strlen(PATH_site)), '/');
                }
        }
 
        /**
-        * Here we check for the module. If not a conf-file is set then it's regarded to be a spacer
+        * Here we check for the module.
         * Return values:
-        *      'notFound':     If the module was not found in the path
-        *      false:          If no access to the module
+        *      'notFound':     If the module was not found in the path (no "conf.php" file)
+        *      false:          If no access to the module (access check failed)
         *      array():        Configuration array, in case a valid module where access IS granted exists.
         *
         * @param       string          Module name
@@ -383,11 +384,25 @@ class t3lib_loadModules {
         * @return      mixed           See description of function
         */
        function checkMod($name, $fullpath)     {
-               $modconf=Array();
-               $path = ereg_replace ('/[^/.]+/\.\./', '/', $fullpath); // because 'path/../path' does not work
-               if (@is_dir($path) && @file_exists($path.'/conf.php'))  {
+               if ($name == 'user_ws' && !t3lib_extMgm::isLoaded('version')) {
+                       return FALSE;
+               }
+
+                       // Check for own way of configuring module
+               if (is_array($GLOBALS['TBE_MODULES'][$name]['configureModuleFunction'])) {
+                       $obj = $GLOBALS['TBE_MODULES'][$name]['configureModuleFunction'];
+                       if (is_callable($obj)) {
+                               return call_user_func($obj, $name, $fullpath);
+                       }
+               }
+
+               $modconf = array();
+               $path = preg_replace('/\/[^\/.]+\/\.\.\//', '/', $fullpath); // because 'path/../path' does not work
+               if (@is_dir($path) && file_exists($path.'/conf.php'))   {
+                       $MCONF = array();
+                       $MLANG = array();
                        include($path.'/conf.php');     // The conf-file is included. This must be valid PHP.
-                       if ($this->checkModAccess($name,$MCONF))        {
+                       if (!$MCONF['shy'] && $this->checkModAccess($name,$MCONF) && $this->checkModWorkspace($name,$MCONF))    {
                                $modconf['name']=$name;
                                        // language processing. This will add module labels and image reference to the internal ->moduleLabels array of the LANG object.
                                if (is_object($GLOBALS['LANG']))        {
@@ -429,30 +444,35 @@ class t3lib_loadModules {
                                }
 
                                        // Default script setup
-                               if ($MCONF['script'] && @file_exists($path.'/'.$MCONF['script']))       {
-                                       $modconf['script']= $this->getRelativePath(PATH_typo3,$fullpath.'/'.$MCONF['script']);
+                               if ($MCONF['script']==='_DISPATCH')     {
+                                       if ($MCONF['extbase']) {
+                                               $modconf['script'] = 'mod.php?M=Tx_' . rawurlencode($name);
+                                       } else {
+                                               $modconf['script'] = 'mod.php?M=' . rawurlencode($name);
+                                       }
+                               } elseif ($MCONF['script'] && file_exists($path.'/'.$MCONF['script']))  {
+                                       $modconf['script'] = $this->getRelativePath(PATH_typo3,$fullpath.'/'.$MCONF['script']);
                                } else {
-                                       $modconf['script']='dummy.php';
+                                       $modconf['script'] = 'dummy.php';
                                }
                                        // Default tab setting
                                if ($MCONF['defaultMod'])       {
-                                       $modconf['defaultMod']=$MCONF['defaultMod'];
+                                       $modconf['defaultMod'] = $MCONF['defaultMod'];
                                }
                                        // Navigation Frame Script (GET params could be added)
                                if ($MCONF['navFrameScript']) {
                                        $navFrameScript = explode('?', $MCONF['navFrameScript']);
                                        $navFrameScript = $navFrameScript[0];
-                                       if (@file_exists($path.'/'.$navFrameScript))    {
-                                               $modconf['navFrameScript']= $this->getRelativePath(PATH_typo3,$fullpath.'/'.$MCONF['navFrameScript']);
+                                       if (file_exists($path.'/'.$navFrameScript))     {
+                                               $modconf['navFrameScript'] = $this->getRelativePath(PATH_typo3,$fullpath.'/'.$MCONF['navFrameScript']);
                                        }
                                }
                                        // additional params for Navigation Frame Script: "&anyParam=value&moreParam=1"
                                if ($MCONF['navFrameScriptParam']) {
-                                       $modconf['navFrameScriptParam']=$MCONF['navFrameScriptParam'];
+                                       $modconf['navFrameScriptParam'] = $MCONF['navFrameScriptParam'];
                                }
                        } else return false;
-               } else $modconf='notFound';
-
+               } else $modconf = 'notFound';
                return $modconf;
        }
 
@@ -480,29 +500,53 @@ class t3lib_loadModules {
        }
 
        /**
+        * Check if a module is allowed inside the current workspace for be user
+        * Processing happens only if $this->observeWorkspaces is TRUE
+        *
+        * @param       string          Module name
+        * @param       array           MCONF array (module configuration array) from the modules conf.php file (contains settings about workspace restrictions)
+        * @return      boolean         True if access is granted for $this->BE_USER
+        */
+       function checkModWorkspace($name,$MCONF)        {
+               if ($this->observeWorkspaces)   {
+                       $status = TRUE;
+                       if ($MCONF['workspaces'])       {
+                               $status = FALSE;
+                               if (($this->BE_USER->workspace===0 && t3lib_div::inList($MCONF['workspaces'],'online')) ||
+                                       ($this->BE_USER->workspace===-1 && t3lib_div::inList($MCONF['workspaces'],'offline')) ||
+                                       ($this->BE_USER->workspace>0 && t3lib_div::inList($MCONF['workspaces'],'custom')))      {
+                                               $status = TRUE;
+                               }
+                       } elseif ($this->BE_USER->workspace===-99)      {
+                               $status = FALSE;
+                       }
+                       return $status;
+               } else return TRUE;
+       }
+
+       /**
         * Parses the moduleArray ($TBE_MODULES) into a internally useful structure.
         * Returns an array where the keys are names of the module and the values may be true (only module) or an array (of submodules)
         *
         * @param       array           moduleArray ($TBE_MODULES)
         * @return      array           Output structure with available modules
         */
-       function parseModulesArray ($arr)       {
+       function parseModulesArray($arr)        {
                $theMods = Array();
                if (is_array($arr))     {
-                       reset($arr);
-                       while(list($mod,$subs)=each($arr))      {
-                               $mod=$this->cleanName($mod);            // clean module name to alphanum
+                       foreach($arr as $mod => $subs)  {
+                               $mod = $this->cleanName($mod);          // clean module name to alphanum
                                if ($mod)       {
                                        if ($subs)      {
-                                               $temp=explode(',',$subs);
-                                               while(list(,$subMod)=each($temp))       {
-                                                       $subMod=$this->cleanName($subMod);
+                                               $subsArr = t3lib_div::trimExplode(',', $subs);
+                                               foreach($subsArr as $subMod)    {
+                                                       $subMod = $this->cleanName($subMod);
                                                        if ($subMod)    {
-                                                               $theMods[$mod][]=$subMod;
+                                                               $theMods[$mod][] = $subMod;
                                                        }
                                                }
                                        } else {
-                                               $theMods[$mod]=1;
+                                               $theMods[$mod] = 1;
                                        }
                                }
                        }
@@ -517,7 +561,7 @@ class t3lib_loadModules {
         * @return      string
         */
        function cleanName ($str)       {
-               return ereg_replace('[^A-Za-z0-9]*','',$str);
+               return preg_replace('/[^a-z0-9]/i','',$str);
        }
 
        /**
@@ -534,8 +578,8 @@ class t3lib_loadModules {
                        return './';
                }
 
-               $baseDir = ereg_replace ('^/', '', $baseDir);   // remove beginning
-               $destDir = ereg_replace ('^/', '', $destDir);
+               $baseDir = ltrim($baseDir, '/');        // remove beginning
+               $destDir = ltrim($destDir, '/');
 
                $found = true;
                $slash_pos=0;
@@ -562,4 +606,5 @@ class t3lib_loadModules {
 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_loadmodules.php'])      {
        include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_loadmodules.php']);
 }
+
 ?>
\ No newline at end of file