|
1 | 1 | <?php |
2 | | -declare (strict_types = 1); |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | namespace Divinity76\quoteshellarg; |
4 | 6 |
|
5 | 7 | /** |
6 | 8 | * quotes shell arguments |
7 | 9 | * (doing a better job than escapeshellarg) |
8 | 10 | * |
9 | | - * @param string $arg |
10 | | - * @throws UnexpectedValueException if $arg contains null bytes |
| 11 | + * @param string|int|float $arg |
| 12 | + * @throws \UnexpectedValueException if $arg contains null bytes |
11 | 13 | * @return string |
12 | 14 | */ |
13 | 15 |
|
14 | | -function quoteshellarg(string $arg): string |
| 16 | +function quoteshellarg($arg): string |
15 | 17 | { |
| 18 | + if (\is_float($arg)) { |
| 19 | + // 17: >The 53-bit significand precision gives from 15 to 17 significant decimal digits precision. |
| 20 | + return \escapeshellarg(\sprintf('%.17g', $arg)); |
| 21 | + } |
| 22 | + if (\is_int($arg)) { |
| 23 | + return \escapeshellarg((string) $arg); |
| 24 | + } |
| 25 | + if (!\is_string($arg)) { |
| 26 | + throw new \InvalidArgumentException('Argument #1 ($arg) must be of type string|int|float, ' . (is_object($arg) ? get_class($arg) : gettype($arg)) . ' given'); |
| 27 | + } |
16 | 28 | static $isUnix = null; |
17 | 29 | if ($isUnix === null) { |
18 | | - $isUnix = in_array(PHP_OS_FAMILY, array('Linux', 'BSD', 'Darwin', 'Solaris'), true) || PHP_OS === 'CYGWIN'; |
| 30 | + $isUnix = \in_array(PHP_OS_FAMILY, array('Linux', 'BSD', 'Darwin', 'Solaris'), true) || PHP_OS === 'CYGWIN'; |
19 | 31 | } |
20 | 32 | if ($isUnix) { |
21 | 33 | // PHP's built-in escapeshellarg() for unix is kindof garbage: https://3v4l.org/Hkv7h |
22 | 34 | // corrupting-or-stripping UTF-8 unicode characters like "æøå" and non-printable characters like "\x01", |
23 | 35 | // both of which are fully legal in unix shell arguments. |
24 | 36 | // 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!'); |
| 37 | + if (false !== \strpos($arg, "\x00")) { |
| 38 | + throw new \UnexpectedValueException('unix shell arguments cannot contain null bytes!'); |
27 | 39 | } |
28 | | - return "'" . strtr($arg, array("'" => "'\\''")) . "'"; |
| 40 | + return "'" . \strtr($arg, array("'" => "'\\''")) . "'"; |
29 | 41 | } |
30 | 42 | // 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 | 43 | // fallback to php's builtin escapeshellarg |
32 | | - return escapeshellarg($arg); |
| 44 | + return \escapeshellarg($arg); |
33 | 45 | } |
0 commit comments