|
6 | 6 | * quotes shell arguments |
7 | 7 | * (doing a better job than escapeshellarg) |
8 | 8 | * |
9 | | - * @param string $arg |
| 9 | + * @param string|int|float $arg |
10 | 10 | * @throws UnexpectedValueException if $arg contains null bytes |
11 | 11 | * @return string |
12 | 12 | */ |
13 | 13 |
|
14 | | -function quoteshellarg(string $arg): string |
| 14 | +function quoteshellarg(string | int | float $arg): string |
15 | 15 | { |
| 16 | + if (\is_float($arg)) { |
| 17 | + // 17: >The 53-bit significand precision gives from 15 to 17 significant decimal digits precision. |
| 18 | + return \escapeshellarg(\sprintf('%.17g', $arg)); |
| 19 | + } |
| 20 | + if (\is_int($arg)) { |
| 21 | + return \escapeshellarg((string) $arg); |
| 22 | + } |
16 | 23 | static $isUnix = null; |
17 | 24 | if ($isUnix === null) { |
18 | | - $isUnix = in_array(PHP_OS_FAMILY, array('Linux', 'BSD', 'Darwin', 'Solaris'), true) || PHP_OS === 'CYGWIN'; |
| 25 | + $isUnix = \in_array(PHP_OS_FAMILY, array('Linux', 'BSD', 'Darwin', 'Solaris'), true) || PHP_OS === 'CYGWIN'; |
19 | 26 | } |
20 | 27 | if ($isUnix) { |
21 | 28 | // PHP's built-in escapeshellarg() for unix is kindof garbage: https://3v4l.org/Hkv7h |
22 | 29 | // corrupting-or-stripping UTF-8 unicode characters like "æøå" and non-printable characters like "\x01", |
23 | 30 | // both of which are fully legal in unix shell arguments. |
24 | 31 | // In single-quoted-unix-shell-arguments there are only 2 bytes that needs special attention: \x00 and \x27 |
25 | | - if (false !== strpos($arg, "\x00")) { |
26 | | - throw new UnexpectedValueException('unix shell arguments cannot contain null bytes!'); |
| 32 | + if (false !== \strpos($arg, "\x00")) { |
| 33 | + throw new \UnexpectedValueException('unix shell arguments cannot contain null bytes!'); |
27 | 34 | } |
28 | | - return "'" . strtr($arg, array("'" => "'\\''")) . "'"; |
| 35 | + return "'" . \strtr($arg, array("'" => "'\\''")) . "'"; |
29 | 36 | } |
30 | 37 | // todo: quoteshellarg for windows? it's a nightmare though: https://docs.microsoft.com/en-us/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way |
31 | 38 | // fallback to php's builtin escapeshellarg |
32 | | - return escapeshellarg($arg); |
| 39 | + return \escapeshellarg($arg); |
33 | 40 | } |
0 commit comments