Index: t3lib/class.t3lib_exec.php =================================================================== --- t3lib/class.t3lib_exec.php (revision 5645) +++ t3lib/class.t3lib_exec.php (working copy) @@ -2,7 +2,7 @@ /*************************************************************** * Copyright notice * -* (c) 2002-2009 RenČ Fritz (r.fritz@colorcube.de) +* (c) 2002-2009 René Fritz (r.fritz@colorcube.de) * All rights reserved * * This script is part of the Typo3 project. The Typo3 project is @@ -26,7 +26,7 @@ * * $Id$ * - * @author RenČ Fritz + * @author René Fritz */ /** * [CLASS/FUNCTION INDEX of SCRIPT] @@ -60,7 +60,7 @@ * This class is meant to be used without instance: * $cmd = t3lib_exec::getCommand ('awstats','perl'); * - * The data of this class is hold in a global variable. Doing it this way the setup is cached. + * The data of this class is cached. * That means if a program is found once it don't have to be searched again. * * user functions: @@ -78,12 +78,18 @@ * binaries can be preconfigured with * $TYPO3_CONF_VARS['SYS']['binSetup'] * - * @author RenČ Fritz + * @author René Fritz * @package TYPO3 * @subpackage t3lib */ class t3lib_exec { + protected static $initialized = false; + + protected static $applications = array(); + + protected static $paths = NULL; + /** * Checks if a command is valid or not * updates global vars @@ -92,61 +98,59 @@ * @param string executer for the command. eg: "perl" * @return boolean false if cmd is not found, or -1 if the handler is not found */ - function checkCommand($cmd, $handler='') { - global $T3_VAR; - - if (!t3lib_exec::_init()) { + public static function checkCommand($cmd, $handler = '') { + if (!self::_init()) { return false; } - - if ($handler && !t3lib_exec::checkCommand($handler)) { + if ($handler && !self::checkCommand($handler)) { return -1; } - // already checked and valid - if ($T3_VAR['t3lib_exec']['apps'][$cmd]['valid']) { + // already checked and valid + if ($this->applications[$cmd]['valid']) { return true; } - // is set but was (above) not true - if (isset($T3_VAR['t3lib_exec']['apps'][$cmd]['valid'])) { + // is set but was (above) not true + if (isset($this->applications[$cmd]['valid'])) { return false; } - foreach($T3_VAR['t3lib_exec']['paths'] as $path => $validPath) { - // ignore invalid (false) paths + foreach($this->paths as $path => $validPath) { + // ignore invalid (false) paths if ($validPath) { - if (TYPO3_OS=='WIN') { - if (@is_file($path.$cmd)) { - $T3_VAR['t3lib_exec']['apps'][$cmd]['app'] = $cmd; - $T3_VAR['t3lib_exec']['apps'][$cmd]['path'] = $path; - $T3_VAR['t3lib_exec']['apps'][$cmd]['valid'] = true; + if (TYPO3_OS == 'WIN') { + if (@is_file($path . $cmd)) { + $this->applications[$cmd]['app'] = $cmd; + $this->applications[$cmd]['path'] = $path; + $this->applications[$cmd]['valid'] = true; return true; } - if (@is_file($path.$cmd.'.exe')) { - $T3_VAR['t3lib_exec']['apps'][$cmd]['app'] = $cmd.'.exe'; - $T3_VAR['t3lib_exec']['apps'][$cmd]['path'] = $path; - $T3_VAR['t3lib_exec']['apps'][$cmd]['valid'] = true; + if (@is_file($path . $cmd . '.exe')) { + $this->applications[$cmd]['app'] = $cmd . '.exe'; + $this->applications[$cmd]['path'] = $path; + $this->applications[$cmd]['valid'] = true; return true; } - } else { // UNIX - if (@is_executable($path.$cmd)) { - $T3_VAR['t3lib_exec']['apps'][$cmd]['app'] = $cmd; - $T3_VAR['t3lib_exec']['apps'][$cmd]['path'] = $path; - $T3_VAR['t3lib_exec']['apps'][$cmd]['valid'] = true; + // UNIX + } else { + if (@is_executable($path . $cmd)) { + $this->applications[$cmd]['app'] = $cmd; + $this->applications[$cmd]['path'] = $path; + $this->applications[$cmd]['valid'] = true; return true; } } } } - // try to get the executable with the command 'which'. It do the same like already done, but maybe on other paths?? - if (TYPO3_OS!='WIN') { - $cmd = @exec ('which '.$cmd); + // try to get the executable with the command 'which'. It do the same like already done, but maybe on other paths?? + if (TYPO3_OS != 'WIN') { + $cmd = @exec ('which ' . $cmd); if (@is_executable($cmd)) { - $T3_VAR['t3lib_exec']['apps'][$cmd]['app'] = $cmd; - $T3_VAR['t3lib_exec']['apps'][$cmd]['path'] = dirname($cmd).'/'; - $T3_VAR['t3lib_exec']['apps'][$cmd]['valid'] = true; + $this->applications[$cmd]['app'] = $cmd; + $this->applications[$cmd]['path'] = dirname($cmd) . '/'; + $this->applications[$cmd]['valid'] = true; return true; } } @@ -163,41 +167,39 @@ * @param string options for the handler, like '-w' for "perl" * @return mixed returns command string, or false if cmd is not found, or -1 if the handler is not found */ - function getCommand($cmd, $handler='', $handlerOpt='') { - global $T3_VAR; - - if (!t3lib_exec::_init()) { + public static function getCommand($cmd, $handler = '', $handlerOpt = '') { + if (!self::_init()) { return false; } // handler if ($handler) { - $handler = t3lib_exec::getCommand($handler); + $handler = self::getCommand($handler); if (!$handler) { return -1; } - $handler .= ' '.$handlerOpt.' '; + $handler .= ' ' . $handlerOpt . ' '; } // command - if (!t3lib_exec::checkCommand($cmd)) { + if (!self::checkCommand($cmd)) { return false; } - $cmd = $T3_VAR['t3lib_exec']['apps'][$cmd]['path'].$T3_VAR['t3lib_exec']['apps'][$cmd]['app'].' '; + $cmd = $this->applications[$cmd]['path'] . $this->applications[$cmd]['app'] . ' '; - return trim($handler.$cmd); + return trim($handler . $cmd); } /** * Extend the preset paths. This way an extension can install an executable and provide the path to t3lib_exec. * - * @param string comma seperated list of extra paths where a command should be searched. Relative paths (without leading "/") are prepend with site root path (PATH_site). + * @param string comma separated list of extra paths where a command should be searched. Relative paths (without leading "/") are prepend with site root path (PATH_site). * @return void */ - function addPaths($paths) { - t3lib_exec::_initPaths($paths); + public static function addPaths($paths) { + self::_initPaths($paths); } @@ -208,14 +210,12 @@ * @param boolean If set the array contains invalid path too. Then the key is the path and the value is empty * @return array Array of search paths (empty if exec is disabled) */ - function getPaths($addInvalid=false) { - global $T3_VAR; - - if (!t3lib_exec::_init()) { + public static function getPaths($addInvalid = false) { + if (!self::_init()) { return array(); } - $paths = $T3_VAR['t3lib_exec']['paths']; + $paths = $this->paths; if(!$addInvalid) { foreach($paths as $path => $validPath) { @@ -229,21 +229,19 @@ /** - * Initialization, internal + * Initialization * * @return void * @internal */ - function _init() { - global $T3_VAR, $TYPO3_CONF_VARS; - - if ($TYPO3_CONF_VARS['BE']['disable_exec_function']) { + protected static function _init() { + if ($GLOBALS['TYPO3_CONF_VARS']['BE']['disable_exec_function']) { return false; } - if (!$T3_VAR['t3lib_exec']['init']) { - t3lib_exec::_initPaths(); - $T3_VAR['t3lib_exec']['apps'] = t3lib_exec::_getConfiguredApps();; - $T3_VAR['t3lib_exec']['init'] = true; + if (!$this->initialized) { + self::_initPaths(); + $this->applications = self::_getConfiguredApps(); + $this->initialized = true; } return true; } @@ -256,47 +254,45 @@ * @return void * @internal */ - function _initPaths($paths='') { - global $T3_VAR; - - $doCeck=false; + protected static function _initPaths($paths = '') { + $doCheck = false; - // init global paths array if not already done - if (!is_array($T3_VAR['t3lib_exec']['paths'])) { - $T3_VAR['t3lib_exec']['paths'] = t3lib_exec::_getPaths(); - $doCeck=true; + // init global paths array if not already done + if (!is_array($this->paths)) { + $this->paths = self::_getPaths(); + $doCheck = true; } - // merge the submitted paths array to the global + // merge the submitted paths array to the global if ($paths) { - $paths = t3lib_div::trimExplode(',',$paths,1); + $paths = t3lib_div::trimExplode(',', $paths, true); if (is_array($paths)) { foreach($paths as $path) { // make absolute path of relative - if (!preg_match('#^/#',$path)) { - $path = PATH_site.$path; + if (!preg_match('#^/#', $path)) { + $path = PATH_site . $path; } - if (!isset($T3_VAR['t3lib_exec']['paths'][$path])) { + if (!isset($this->paths[$path])) { if (@is_dir($path)) { - $T3_VAR['t3lib_exec']['paths'][$path] = $path; - $T3_VAR['t3lib_exec']['allPaths'].=','.$path; + $this->paths[$path] = $path; + $this->allPaths .= ',' . $path; // $doCeck=true; just done } else { - $T3_VAR['t3lib_exec']['paths'][$path] = false; + $this->paths[$path] = false; } } } } } // check if new paths are invalid - if ($doCeck) { - $T3_VAR['t3lib_exec']['allPaths']=''; - foreach($T3_VAR['t3lib_exec']['paths'] as $path => $valid) { + if ($doCheck) { + $this->allPaths = ''; + foreach($this->paths as $path => $valid) { // ignore invalid (false) paths if ($valid AND !@is_dir($path)) { - $T3_VAR['t3lib_exec']['paths'][$path] = false; + $this->paths[$path] = false; } - if ($path = $T3_VAR['t3lib_exec']['paths'][$path]) { - $T3_VAR['t3lib_exec']['allPaths'].=','.$path; + if ($path = $this->paths[$path]) { + $this->allPaths .= ',' . $path; } } } @@ -309,19 +305,17 @@ * @return array Array of commands and path * @internal */ - function _getConfiguredApps() { - global $TYPO3_CONF_VARS; - + protected static function _getConfiguredApps() { $cmdArr = array(); - if ($TYPO3_CONF_VARS['SYS']['binSetup']) { - $pathSetup = implode("\n", t3lib_div::trimExplode(',',$TYPO3_CONF_VARS['SYS']['binSetup'],1)); - $pathSetup = t3lib_div::trimExplode("\n",$pathSetup,1); + if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['binSetup']) { + $pathSetup = implode("\n", t3lib_div::trimExplode(',', $GLOBALS['SYS']['binSetup'], true)); + $pathSetup = t3lib_div::trimExplode("\n", $pathSetup, true); foreach($pathSetup as $val) { - list($cmd, $cmdPath) = t3lib_div::trimExplode('=',$val,1); + list($cmd, $cmdPath) = t3lib_div::trimExplode('=', $val, true); $cmdArr[$cmd]['app'] = basename($cmdPath); - $cmdArr[$cmd]['path'] = dirname($cmdPath).'/'; + $cmdArr[$cmd]['path'] = dirname($cmdPath) . '/'; $cmdArr[$cmd]['valid'] = true; } } @@ -336,54 +330,51 @@ * @return array Array of absolute paths (keys and values are equal) * @internal */ - function _getPaths() { - global $T3_VAR, $TYPO3_CONF_VARS; - + protected static function _getPaths() { $pathsArr = array(); $sysPathArr = array(); - // image magick paths first - // im_path_lzw take precedence over im_path - if ($imPath = ($TYPO3_CONF_VARS['GFX']['im_path_lzw'] ? $TYPO3_CONF_VARS['GFX']['im_path_lzw'] : $TYPO3_CONF_VARS['GFX']['im_path'])) { - $imPath = t3lib_exec::_fixPath($imPath); + // image magick paths first + // im_path_lzw take precedence over im_path + if ($imPath = ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw'] ? $TYPO3_CONF_VARS['GFX']['im_path_lzw'] : $TYPO3_CONF_VARS['GFX']['im_path'])) { + $imPath = self::_fixPath($imPath); $pathsArr[$imPath] = $imPath; } - // add configured paths - if ($TYPO3_CONF_VARS['SYS']['binPath']) { - $sysPath = t3lib_div::trimExplode(',',$TYPO3_CONF_VARS['SYS']['binPath'],1); + // add configured paths + if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['binPath']) { + $sysPath = t3lib_div::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['binPath'], true); foreach($sysPath as $val) { - $val = t3lib_exec::_fixPath($val); - $sysPathArr[$val]=$val; + $val = self::_fixPath($val); + $sysPathArr[$val] = $val; } } - - // add path from environment + // add path from environment // TODO: how does this work for WIN if ($GLOBALS['_SERVER']['PATH']) { $sep = (TYPO3_OS=='WIN') ? ';' : ':'; - $envPath = t3lib_div::trimExplode($sep,$GLOBALS['_SERVER']['PATH'],1); + $envPath = t3lib_div::trimExplode($sep, $GLOBALS['_SERVER']['PATH'], true); foreach($envPath as $val) { - $val = t3lib_exec::_fixPath($val); - $sysPathArr[$val]=$val; + $val = self::_fixPath($val); + $sysPathArr[$val] = $val; } } - if (TYPO3_OS=='WIN') { + if (TYPO3_OS == 'WIN') { // TODO: add the most common paths for WIN $sysPathArr = array_merge($sysPathArr, array ( - '/usr/bin/' => '/usr/bin/', - '/perl/bin/' => '/perl/bin/', + 'C:/Programs' => 'C:/Programs', + 'C:/Perl/bin' => 'C:/Perl/bin', )); - } else { // UNIX + // UNIX + } else { $sysPathArr = array_merge($sysPathArr, array ( '/usr/bin/' => '/usr/bin/', '/usr/local/bin/' => '/usr/local/bin/', )); } - $pathsArr = array_merge($pathsArr, $sysPathArr); return $pathsArr; @@ -397,12 +388,12 @@ * @return string Output path * @internal */ - function _fixPath($path) { - return str_replace ('//','/',$path.'/'); + protected static function _fixPath($path) { + return str_replace ('//', '/', $path . '/'); } } if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_exec.php']) { include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_exec.php']); } -?> \ No newline at end of file +?>