Project

General

Profile

Bug #19468 » bug_9581.patch

Administrator Admin, 2008-10-16 16:59

View differences:

t3lib/class.t3lib_exec.php (working copy)
* 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:
......
*/
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
......
* @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']) {
if ($this->applications[$cmd]['valid']) {
return true;
}
// is set but was (above) not true
if (isset($T3_VAR['t3lib_exec']['apps'][$cmd]['valid'])) {
if (isset($this->applications[$cmd]['valid'])) {
return false;
}
foreach($T3_VAR['t3lib_exec']['paths'] as $path => $validPath) {
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 (@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;
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);
$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;
}
}
......
* @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);
}
......
* @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) {
......
/**
* 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;
}
......
* @return void
* @internal
*/
function _initPaths($paths='') {
global $T3_VAR;
protected static function _initPaths($paths='') {
$doCheck = false;
$doCeck=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;
if (!is_array($this->paths)) {
$this->paths = self::_getPaths();
$doCheck = true;
}
// merge the submitted paths array to the global
if ($paths) {
$paths = t3lib_div::trimExplode(',',$paths,1);
$paths = t3lib_div::trimExplode(',', $paths, 1);
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;
}
}
}
......
* @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'], 1));
$pathSetup = t3lib_div::trimExplode("\n", $pathSetup, 1);
foreach($pathSetup as $val) {
list($cmd, $cmdPath) = t3lib_div::trimExplode('=',$val,1);
list($cmd, $cmdPath) = t3lib_div::trimExplode('=', $val, 1);
$cmdArr[$cmd]['app'] = basename($cmdPath);
$cmdArr[$cmd]['path'] = dirname($cmdPath).'/';
$cmdArr[$cmd]['path'] = dirname($cmdPath) . '/';
$cmdArr[$cmd]['valid'] = true;
}
}
......
* @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);
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);
if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['binPath']) {
$sysPath = t3lib_div::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['binPath'], 1);
foreach($sysPath as $val) {
$val = t3lib_exec::_fixPath($val);
$sysPathArr[$val]=$val;
$val = self::_fixPath($val);
$sysPathArr[$val] = $val;
}
}
// 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'], 1);
foreach($envPath as $val) {
$val = t3lib_exec::_fixPath($val);
$sysPathArr[$val]=$val;
$val = self::_fixPath($val);
$sysPathArr[$val] = $val;
}
}
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
$sysPathArr = array_merge($sysPathArr, array (
......
));
}
$pathsArr = array_merge($pathsArr, $sysPathArr);
return $pathsArr;
......
* @return string Output path
* @internal
*/
function _fixPath($path) {
return str_replace ('//','/',$path.'/');
protected static function _fixPath($path) {
return str_replace ('//', '/', $path . '/');
}
}
(1-1/3)