2013-01-18 12:53:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Process;
|
|
|
|
|
2014-05-17 18:27:19 +01:00
|
|
|
use Symfony\Component\Process\Exception\InvalidArgumentException;
|
|
|
|
|
2013-01-18 12:53:35 +00:00
|
|
|
/**
|
|
|
|
* ProcessUtils is a bunch of utility methods.
|
|
|
|
*
|
|
|
|
* This class contains static methods only and is not meant to be instantiated.
|
|
|
|
*
|
|
|
|
* @author Martin Hasoň <martin.hason@gmail.com>
|
|
|
|
*/
|
|
|
|
class ProcessUtils
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* This class should not be instantiated
|
|
|
|
*/
|
|
|
|
private function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escapes a string to be used as a shell argument.
|
|
|
|
*
|
|
|
|
* @param string $argument The argument that will be escaped
|
|
|
|
*
|
|
|
|
* @return string The escaped argument
|
|
|
|
*/
|
|
|
|
public static function escapeArgument($argument)
|
|
|
|
{
|
|
|
|
//Fix for PHP bug #43784 escapeshellarg removes % from given string
|
2013-12-27 15:08:19 +00:00
|
|
|
//Fix for PHP bug #49446 escapeshellarg doesn't work on Windows
|
2013-01-18 12:53:35 +00:00
|
|
|
//@see https://bugs.php.net/bug.php?id=43784
|
|
|
|
//@see https://bugs.php.net/bug.php?id=49446
|
|
|
|
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
|
2013-08-07 23:27:53 +01:00
|
|
|
if ('' === $argument) {
|
|
|
|
return escapeshellarg($argument);
|
|
|
|
}
|
|
|
|
|
2013-01-18 12:53:35 +00:00
|
|
|
$escapedArgument = '';
|
2013-09-09 21:28:39 +01:00
|
|
|
$quote = false;
|
2014-03-18 13:37:22 +00:00
|
|
|
foreach (preg_split('/(")/i', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
|
2013-08-07 23:27:53 +01:00
|
|
|
if ('"' === $part) {
|
2013-01-18 12:53:35 +00:00
|
|
|
$escapedArgument .= '\\"';
|
2014-03-18 13:37:22 +00:00
|
|
|
} elseif (self::isSurroundedBy($part, '%')) {
|
|
|
|
// Avoid environment variable expansion
|
|
|
|
$escapedArgument .= '^%"'.substr($part, 1, -1).'"^%';
|
2013-01-18 12:53:35 +00:00
|
|
|
} else {
|
2014-03-18 13:37:22 +00:00
|
|
|
// escape trailing backslash
|
2014-03-12 14:41:28 +00:00
|
|
|
if ('\\' === substr($part, -1)) {
|
|
|
|
$part .= '\\';
|
|
|
|
}
|
2014-03-18 13:37:22 +00:00
|
|
|
$quote = true;
|
2013-09-09 21:28:39 +01:00
|
|
|
$escapedArgument .= $part;
|
2013-01-18 12:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-09 21:28:39 +01:00
|
|
|
if ($quote) {
|
2014-03-14 09:10:07 +00:00
|
|
|
$escapedArgument = '"'.$escapedArgument.'"';
|
2013-09-09 21:28:39 +01:00
|
|
|
}
|
2013-01-18 12:53:35 +00:00
|
|
|
|
|
|
|
return $escapedArgument;
|
|
|
|
}
|
|
|
|
|
|
|
|
return escapeshellarg($argument);
|
|
|
|
}
|
2014-03-18 13:37:22 +00:00
|
|
|
|
2014-05-17 18:27:19 +01:00
|
|
|
/**
|
2014-05-17 18:36:35 +01:00
|
|
|
* Validates and normalizes a Process input
|
2014-05-17 18:27:19 +01:00
|
|
|
*
|
|
|
|
* @param string $caller The name of method call that validates the input
|
|
|
|
* @param mixed $input The input to validate
|
|
|
|
*
|
|
|
|
* @return string The validated input
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException In case the input is not valid
|
|
|
|
*/
|
|
|
|
public static function validateInput($caller, $input)
|
|
|
|
{
|
|
|
|
if (null !== $input) {
|
2014-05-17 18:36:35 +01:00
|
|
|
if (is_scalar($input)) {
|
|
|
|
return (string) $input;
|
|
|
|
}
|
|
|
|
// deprecated as of Symfony 2.5, to be removed in 3.0
|
|
|
|
if (is_object($input) && method_exists($input, '__toString')) {
|
2014-05-17 18:27:19 +01:00
|
|
|
return (string) $input;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new InvalidArgumentException(sprintf('%s only accepts strings.', $caller));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
2014-03-18 13:37:22 +00:00
|
|
|
private static function isSurroundedBy($arg, $char)
|
|
|
|
{
|
|
|
|
return 2 < strlen($arg) && $char === $arg[0] && $char === $arg[strlen($arg) - 1];
|
|
|
|
}
|
2013-01-18 12:53:35 +00:00
|
|
|
}
|