00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 class t3lib_exec {
00086
00087
00088 protected static $initialized = FALSE;
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 protected static $applications = array();
00100
00101
00102
00103
00104
00105
00106 protected static $paths = NULL;
00107
00108
00109
00110
00111
00112
00113
00114
00115 public static function checkCommand($cmd, $handler = '') {
00116 if (!self::init()) {
00117 return FALSE;
00118 }
00119
00120 if ($handler && !self::checkCommand($handler)) {
00121 return -1;
00122 }
00123
00124 if (self::$applications[$cmd]['valid']) {
00125 return TRUE;
00126 }
00127
00128 if (isset(self::$applications[$cmd]['valid'])) {
00129 return FALSE;
00130 }
00131
00132 foreach (self::$paths as $path => $validPath) {
00133
00134 if ($validPath) {
00135 if (TYPO3_OS == 'WIN') {
00136
00137
00138 if (@is_file($path . $cmd)) {
00139 self::$applications[$cmd]['app'] = $cmd;
00140 self::$applications[$cmd]['path'] = $path;
00141 self::$applications[$cmd]['valid'] = TRUE;
00142 return TRUE;
00143 }
00144 if (@is_file($path . $cmd . '.exe')) {
00145 self::$applications[$cmd]['app'] = $cmd . '.exe';
00146 self::$applications[$cmd]['path'] = $path;
00147 self::$applications[$cmd]['valid'] = TRUE;
00148 return TRUE;
00149 }
00150 } else {
00151
00152 $filePath = realpath($path . $cmd);
00153 if ($filePath && @is_executable($filePath)) {
00154 self::$applications[$cmd]['app'] = $cmd;
00155 self::$applications[$cmd]['path'] = $path;
00156 self::$applications[$cmd]['valid'] = TRUE;
00157 return TRUE;
00158 }
00159 }
00160 }
00161 }
00162
00163
00164
00165 if (TYPO3_OS != 'WIN') {
00166 $cmd = @exec('which ' . $cmd);
00167 if (@is_executable($cmd)) {
00168 self::$applications[$cmd]['app'] = $cmd;
00169 self::$applications[$cmd]['path'] = dirname($cmd) . '/';
00170 self::$applications[$cmd]['valid'] = TRUE;
00171 return TRUE;
00172 }
00173 }
00174
00175 return FALSE;
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 public static function getCommand($cmd, $handler = '', $handlerOpt = '') {
00188 if (!self::init()) {
00189 return FALSE;
00190 }
00191
00192
00193 if ($handler) {
00194 $handler = self::getCommand($handler);
00195
00196 if (!$handler) {
00197 return -1;
00198 }
00199 $handler .= ' ' . $handlerOpt . ' ';
00200 }
00201
00202
00203 if (!self::checkCommand($cmd)) {
00204 return FALSE;
00205 }
00206 $cmd = self::$applications[$cmd]['path'] . self::$applications[$cmd]['app'] . ' ';
00207
00208 return trim($handler . $cmd);
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218 public static function addPaths($paths) {
00219 self::initPaths($paths);
00220 }
00221
00222
00223
00224
00225
00226
00227
00228
00229 public static function getPaths($addInvalid = FALSE) {
00230 if (!self::init()) {
00231 return array();
00232 }
00233
00234 $paths = self::$paths;
00235
00236 if (!$addInvalid) {
00237 foreach ($paths as $path => $validPath) {
00238 if (!$validPath) {
00239 unset($paths[$path]);
00240 }
00241 }
00242 }
00243 return $paths;
00244 }
00245
00246
00247
00248
00249
00250
00251
00252 protected static function init() {
00253 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['disable_exec_function']) {
00254 return FALSE;
00255 }
00256 if (!self::$initialized) {
00257 self::initPaths();
00258 self::$applications = self::getConfiguredApps();
00259 self::$initialized = TRUE;
00260 }
00261 return TRUE;
00262 }
00263
00264
00265
00266
00267
00268
00269
00270
00271 protected static function initPaths($paths = '') {
00272 $doCheck = FALSE;
00273
00274
00275 if (!is_array(self::$paths)) {
00276 self::$paths = self::getPathsInternal();
00277 $doCheck = TRUE;
00278 }
00279
00280 if ($paths) {
00281 $paths = t3lib_div::trimExplode(',', $paths, 1);
00282 if (is_array($paths)) {
00283 foreach ($paths as $path) {
00284
00285 if (!preg_match('#^/#', $path)) {
00286 $path = PATH_site . $path;
00287 }
00288 if (!isset(self::$paths[$path])) {
00289 if (@is_dir($path)) {
00290 self::$paths[$path] = $path;
00291 } else {
00292 self::$paths[$path] = FALSE;
00293 }
00294 }
00295 }
00296 }
00297 }
00298
00299 if ($doCheck) {
00300 foreach (self::$paths as $path => $valid) {
00301
00302 if ($valid AND !@is_dir($path)) {
00303 self::$paths[$path] = FALSE;
00304 }
00305 }
00306 }
00307 }
00308
00309
00310
00311
00312
00313
00314
00315 protected static function getConfiguredApps() {
00316 $cmdArr = array();
00317
00318 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['binSetup']) {
00319 $pathSetup = preg_split('/[\n,]+/', $GLOBALS['TYPO3_CONF_VARS']['SYS']['binSetup']);
00320 foreach ($pathSetup as $val) {
00321 list($cmd, $cmdPath) = t3lib_div::trimExplode('=', $val, 1);
00322 $cmdArr[$cmd]['app'] = basename($cmdPath);
00323 $cmdArr[$cmd]['path'] = dirname($cmdPath) . '/';
00324 $cmdArr[$cmd]['valid'] = TRUE;
00325 }
00326 }
00327
00328 return $cmdArr;
00329 }
00330
00331
00332
00333
00334
00335
00336
00337 protected static function getPathsInternal() {
00338
00339 $pathsArr = array();
00340 $sysPathArr = array();
00341
00342
00343
00344 if (($imPath = ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw'] ? $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw'] : $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path']))) {
00345 $imPath = self::fixPath($imPath);
00346 $pathsArr[$imPath] = $imPath;
00347 }
00348
00349
00350 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['binPath']) {
00351 $sysPath = t3lib_div::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['binPath'], 1);
00352 foreach ($sysPath as $val) {
00353 $val = self::fixPath($val);
00354 $sysPathArr[$val] = $val;
00355 }
00356 }
00357
00358
00359
00360
00361 if ($GLOBALS['_SERVER']['PATH']) {
00362 $sep = (TYPO3_OS == 'WIN' ? ';' : ':');
00363 $envPath = t3lib_div::trimExplode($sep, $GLOBALS['_SERVER']['PATH'], 1);
00364 foreach ($envPath as $val) {
00365 $val = self::fixPath($val);
00366 $sysPathArr[$val] = $val;
00367 }
00368 }
00369
00370
00371 if (TYPO3_OS !== 'WIN') {
00372 $sysPathArr = array_merge($sysPathArr, array(
00373 '/usr/bin/' => '/usr/bin/',
00374 '/usr/local/bin/' => '/usr/local/bin/',
00375 ));
00376 }
00377
00378 $pathsArr = array_merge($pathsArr, $sysPathArr);
00379
00380 return $pathsArr;
00381 }
00382
00383
00384
00385
00386
00387
00388
00389
00390 protected static function fixPath($path) {
00391 return str_replace('
00392 }
00393 }
00394
00395 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_exec.php']) {
00396 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_exec.php']);
00397 }
00398 ?>