-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from SamarRizvi/master
Fix Bug: % symbol stripped from ffmpeg command
- Loading branch information
Showing
2 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace PHPVideoToolkit; | ||
|
||
/** | ||
* ProcessUtils is a bunch of utility methods taken from Symphony. | ||
* | ||
* 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 | ||
//Fix for PHP bug #49446 escapeshellarg doesn't work on Windows | ||
//@see https://bugs.php.net/bug.php?id=43784 | ||
//@see https://bugs.php.net/bug.php?id=49446 | ||
if ('\\' === DIRECTORY_SEPARATOR) { | ||
if ('' === $argument) { | ||
return escapeshellarg($argument); | ||
} | ||
|
||
$escapedArgument = ''; | ||
$quote = false; | ||
foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) { | ||
if ('"' === $part) { | ||
$escapedArgument .= '\\"'; | ||
} elseif (self::isSurroundedBy($part, '%')) { | ||
// Avoid environment variable expansion | ||
$escapedArgument .= '^%"'.substr($part, 1, -1).'"^%'; | ||
} else { | ||
// escape trailing backslash | ||
if ('\\' === substr($part, -1)) { | ||
$part .= '\\'; | ||
} | ||
$quote = true; | ||
$escapedArgument .= $part; | ||
} | ||
} | ||
if ($quote) { | ||
$escapedArgument = '"'.$escapedArgument.'"'; | ||
} | ||
|
||
return $escapedArgument; | ||
} | ||
|
||
return escapeshellarg($argument); | ||
} | ||
|
||
private static function isSurroundedBy($arg, $char) | ||
{ | ||
return 2 < strlen($arg) && $char === $arg[0] && $char === $arg[strlen($arg) - 1]; | ||
} | ||
} |