TYPO3 CMS  TYPO3_6-2
CommandUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3 
48 
54  protected static $initialized = FALSE;
55 
65  protected static $applications = array();
66 
72  protected static $paths = NULL;
73 
84  static public function exec($command, &$output = NULL, &$returnValue = 0) {
85  $lastLine = exec($command, $output, $returnValue);
86  return $lastLine;
87  }
88 
97  static public function imageMagickCommand($command, $parameters, $path = '') {
98  $gfxConf = $GLOBALS['TYPO3_CONF_VARS']['GFX'];
99  $isExt = TYPO3_OS == 'WIN' ? '.exe' : '';
100  $switchCompositeParameters = FALSE;
101  if (!$path) {
102  $path = $gfxConf['im_path'];
103  }
105  $im_version = strtolower($gfxConf['im_version_5']);
106  // This is only used internally, has no effect outside
107  if ($command === 'combine') {
108  $command = 'composite';
109  }
110  // Compile the path & command
111  if ($im_version === 'gm') {
112  $switchCompositeParameters = TRUE;
113  $path = escapeshellarg(($path . 'gm' . $isExt)) . ' ' . $command;
114  } else {
115  if ($im_version === 'im6') {
116  $switchCompositeParameters = TRUE;
117  }
118  $path = escapeshellarg($path . ($command == 'composite' ? 'composite' : $command) . $isExt);
119  }
120  // strip profile information for thumbnails and reduce their size
121  if ($parameters && $command != 'identify' && $gfxConf['im_useStripProfileByDefault'] && $gfxConf['im_stripProfileCommand'] != '') {
122  if (strpos($parameters, $gfxConf['im_stripProfileCommand']) === FALSE) {
123  // Determine whether the strip profile action has be disabled by TypoScript:
124  if ($parameters !== '-version' && strpos($parameters, '###SkipStripProfile###') === FALSE) {
125  $parameters = $gfxConf['im_stripProfileCommand'] . ' ' . $parameters;
126  } else {
127  $parameters = str_replace('###SkipStripProfile###', '', $parameters);
128  }
129  }
130  }
131  $cmdLine = $path . ' ' . $parameters;
132  // Because of some weird incompatibilities between ImageMagick 4 and 6 (plus GraphicsMagick),
133  // it is needed to change the parameters order under some preconditions
134  if ($command == 'composite' && $switchCompositeParameters) {
136  // The mask image has been specified => swap the parameters
137  if (count($paramsArr) > 5) {
138  $tmp = $paramsArr[count($paramsArr) - 3];
139  $paramsArr[count($paramsArr) - 3] = $paramsArr[count($paramsArr) - 4];
140  $paramsArr[count($paramsArr) - 4] = $tmp;
141  }
142  $cmdLine = $path . ' ' . implode(' ', $paramsArr);
143  }
144  return $cmdLine;
145  }
146 
154  public static function checkCommand($cmd, $handler = '') {
155  if (!self::init()) {
156  return FALSE;
157  }
158 
159  if ($handler && !self::checkCommand($handler)) {
160  return -1;
161  }
162  // Already checked and valid
163  if (self::$applications[$cmd]['valid']) {
164  return TRUE;
165  }
166  // Is set but was (above) not TRUE
167  if (isset(self::$applications[$cmd]['valid'])) {
168  return FALSE;
169  }
170 
171  foreach (self::$paths as $path => $validPath) {
172  // Ignore invalid (FALSE) paths
173  if ($validPath) {
174  if (TYPO3_OS == 'WIN') {
175  // Windows OS
176  // TODO Why is_executable() is not called here?
177  if (@is_file($path . $cmd)) {
178  self::$applications[$cmd]['app'] = $cmd;
179  self::$applications[$cmd]['path'] = $path;
180  self::$applications[$cmd]['valid'] = TRUE;
181  return TRUE;
182  }
183  if (@is_file($path . $cmd . '.exe')) {
184  self::$applications[$cmd]['app'] = $cmd . '.exe';
185  self::$applications[$cmd]['path'] = $path;
186  self::$applications[$cmd]['valid'] = TRUE;
187  return TRUE;
188  }
189  } else {
190  // Unix-like OS
191  $filePath = realpath($path . $cmd);
192  if ($filePath && @is_executable($filePath)) {
193  self::$applications[$cmd]['app'] = $cmd;
194  self::$applications[$cmd]['path'] = $path;
195  self::$applications[$cmd]['valid'] = TRUE;
196  return TRUE;
197  }
198  }
199  }
200  }
201 
202  // Try to get the executable with the command 'which'.
203  // It does the same like already done, but maybe on other paths
204  if (TYPO3_OS != 'WIN') {
205  $cmd = @self::exec('which ' . $cmd);
206  if (@is_executable($cmd)) {
207  self::$applications[$cmd]['app'] = $cmd;
208  self::$applications[$cmd]['path'] = dirname($cmd) . '/';
209  self::$applications[$cmd]['valid'] = TRUE;
210  return TRUE;
211  }
212  }
213 
214  return FALSE;
215  }
216 
225  public static function getCommand($cmd, $handler = '', $handlerOpt = '') {
226  if (!self::init()) {
227  return FALSE;
228  }
229 
230  // Handler
231  if ($handler) {
232  $handler = self::getCommand($handler);
233 
234  if (!$handler) {
235  return -1;
236  }
237  $handler .= ' ' . $handlerOpt . ' ';
238  }
239 
240  // Command
241  if (!self::checkCommand($cmd)) {
242  return FALSE;
243  }
244  $cmd = self::$applications[$cmd]['path'] . self::$applications[$cmd]['app'] . ' ';
245 
246  return trim($handler . $cmd);
247  }
248 
255  public static function addPaths($paths) {
256  self::initPaths($paths);
257  }
258 
265  public static function getPaths($addInvalid = FALSE) {
266  if (!self::init()) {
267  return array();
268  }
269 
270  $paths = self::$paths;
271 
272  if (!$addInvalid) {
273  foreach ($paths as $path => $validPath) {
274  if (!$validPath) {
275  unset($paths[$path]);
276  }
277  }
278  }
279  return $paths;
280  }
281 
287  protected static function init() {
288  if ($GLOBALS['TYPO3_CONF_VARS']['BE']['disable_exec_function']) {
289  return FALSE;
290  }
291  if (!self::$initialized) {
292  self::initPaths();
293  self::$applications = self::getConfiguredApps();
294  self::$initialized = TRUE;
295  }
296  return TRUE;
297  }
298 
305  protected static function initPaths($paths = '') {
306  $doCheck = FALSE;
307 
308  // Init global paths array if not already done
309  if (!is_array(self::$paths)) {
310  self::$paths = self::getPathsInternal();
311  $doCheck = TRUE;
312  }
313  // Merge the submitted paths array to the global
314  if ($paths) {
316  if (is_array($paths)) {
317  foreach ($paths as $path) {
318  // Make absolute path of relative
319  if (!preg_match('#^/#', $path)) {
320  $path = PATH_site . $path;
321  }
322  if (!isset(self::$paths[$path])) {
323  if (@is_dir($path)) {
324  self::$paths[$path] = $path;
325  } else {
326  self::$paths[$path] = FALSE;
327  }
328  }
329  }
330  }
331  }
332  // Check if new paths are invalid
333  if ($doCheck) {
334  foreach (self::$paths as $path => $valid) {
335  // Ignore invalid (FALSE) paths
336  if ($valid AND !@is_dir($path)) {
337  self::$paths[$path] = FALSE;
338  }
339  }
340  }
341  }
342 
348  protected static function getConfiguredApps() {
349  $cmdArr = array();
350 
351  if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['binSetup']) {
352  $binSetup = str_replace(array('\'.chr(10).\'', '\' . LF . \''), LF, $GLOBALS['TYPO3_CONF_VARS']['SYS']['binSetup']);
353  $pathSetup = preg_split('/[\n,]+/', $binSetup);
354  foreach ($pathSetup as $val) {
355  if (trim($val) === '') {
356  continue;
357  }
358  list($cmd, $cmdPath) = GeneralUtility::trimExplode('=', $val, TRUE, 2);
359  $cmdArr[$cmd]['app'] = basename($cmdPath);
360  $cmdArr[$cmd]['path'] = dirname($cmdPath) . '/';
361  $cmdArr[$cmd]['valid'] = TRUE;
362  }
363  }
364 
365  return $cmdArr;
366  }
367 
373  protected static function getPathsInternal() {
374 
375  $pathsArr = array();
376  $sysPathArr = array();
377 
378  // Image magick paths first
379  // im_path_lzw take precedence over im_path
380  if (($imPath = ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw'] ?: $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path']))) {
381  $imPath = self::fixPath($imPath);
382  $pathsArr[$imPath] = $imPath;
383  }
384 
385  // Add configured paths
386  if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['binPath']) {
387  $sysPath = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['binPath'], TRUE);
388  foreach ($sysPath as $val) {
389  $val = self::fixPath($val);
390  $sysPathArr[$val] = $val;
391  }
392  }
393 
394  // Add path from environment
395  // TODO: how does this work for WIN
396  if ($GLOBALS['_SERVER']['PATH']) {
397  $sep = (TYPO3_OS == 'WIN' ? ';' : ':');
398  $envPath = GeneralUtility::trimExplode($sep, $GLOBALS['_SERVER']['PATH'], TRUE);
399  foreach ($envPath as $val) {
400  $val = self::fixPath($val);
401  $sysPathArr[$val] = $val;
402  }
403  }
404 
405  // Set common paths for Unix (only)
406  if (TYPO3_OS !== 'WIN') {
407  $sysPathArr = array_merge($sysPathArr, array(
408  '/usr/bin/' => '/usr/bin/',
409  '/usr/local/bin/' => '/usr/local/bin/',
410  ));
411  }
412 
413  $pathsArr = array_merge($pathsArr, $sysPathArr);
414 
415  return $pathsArr;
416  }
417 
418 
425  protected static function fixPath($path) {
426  return str_replace('//', '/', $path . '/');
427  }
428 
429 }
static checkCommand($cmd, $handler='')
static getCommand($cmd, $handler='', $handlerOpt='')
$parameters
Definition: FileDumpEID.php:15
$sep
Definition: server.php:42
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
static unQuoteFilenames($parameters, $unQuote=FALSE)
static imageMagickCommand($command, $parameters, $path='')
static getPaths($addInvalid=FALSE)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
static exec($command, &$output=NULL, &$returnValue=0)