Skip to content

Commit

Permalink
Replace escapeshellarg to avoid locale problems
Browse files Browse the repository at this point in the history
  • Loading branch information
johnstevenson committed Aug 14, 2018
1 parent c317d26 commit cc39aaa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Args.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class Args
public static function escape($arg, $meta = true, $module = false)
{
if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
return escapeshellarg($arg);
// Escape single-quotes and enclose in single-quotes
return "'".str_replace("'", "'\\''", $arg)."'";
}

// Check for whitespace or an empty value
Expand All @@ -48,7 +49,7 @@ public static function escape($arg, $meta = true, $module = false)
$meta = $dquotes || preg_match('/%[^%]+%/', $arg);

if (!$meta) {
// Check for characters that can be escaped in quotes
// Check for characters that can be escaped in double-quotes
$quote = $quote || strpbrk($arg, '^&|<>()') !== false;

} elseif ($module && !$dquotes && $quote) {
Expand All @@ -60,8 +61,7 @@ public static function escape($arg, $meta = true, $module = false)

if ($quote) {
// Double-up trailing backslashes and enclose in double-quotes
$arg = preg_replace('/(\\\\*)$/', '$1$1', $arg);
$arg = '"'.$arg.'"';
$arg = '"'.preg_replace('/(\\\\*)$/', '$1$1', $arg).'"';
}

if ($meta) {
Expand Down
3 changes: 3 additions & 0 deletions tests/ArgsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public function dataArguments()
// empty argument - must be quoted
'empty' => array('', '""', "''", 0),

// unix single-quote must be escaped
'unix-sq' => array("a'bc", "a'bc", "'a'\\''b'", 0),

// whitespace <space> must be quoted
'ws space' => array('a b c', '"a b c"', "'a b c'", 0),

Expand Down

0 comments on commit cc39aaa

Please sign in to comment.