| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of composer/xdebug-handler. |
| 5 |
* |
| 6 |
* (c) Composer <https://github.com/composer> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view |
| 9 |
* the LICENSE file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
declare(strict_types=1); |
| 13 |
|
| 14 |
namespace Composer\XdebugHandler; |
| 15 |
|
| 16 |
use Composer\Pcre\Preg; |
| 17 |
|
| 18 |
/** |
| 19 |
* Process utility functions |
| 20 |
* |
| 21 |
* @author John Stevenson <john-stevenson@blueyonder.co.uk> |
| 22 |
*/ |
| 23 |
class Process |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Escapes a string to be used as a shell argument. |
| 27 |
* |
| 28 |
* From https://github.com/johnstevenson/winbox-args |
| 29 |
* MIT Licensed (c) John Stevenson <john-stevenson@blueyonder.co.uk> |
| 30 |
* |
| 31 |
* @param string $arg The argument to be escaped |
| 32 |
* @param bool $meta Additionally escape cmd.exe meta characters |
| 33 |
* @param bool $module The argument is the module to invoke |
| 34 |
*/ |
| 35 |
public static function escape(string $arg, bool $meta = true, bool $module = false): string |
| 36 |
{ |
| 37 |
if (!defined('PHP_WINDOWS_VERSION_BUILD')) { |
| 38 |
return "'".str_replace("'", "'\\''", $arg)."'"; |
| 39 |
} |
| 40 |
|
| 41 |
$quote = strpbrk($arg, " \t") !== false || $arg === ''; |
| 42 |
|
| 43 |
$arg = Preg::replace('/(\\\\*)"/', '$1$1\\"', $arg, -1, $dquotes); |
| 44 |
|
| 45 |
if ($meta) { |
| 46 |
$meta = $dquotes || Preg::isMatch('/%[^%]+%/', $arg); |
| 47 |
|
| 48 |
if (!$meta) { |
| 49 |
$quote = $quote || strpbrk($arg, '^&|<>()') !== false; |
| 50 |
} elseif ($module && !$dquotes && $quote) { |
| 51 |
$meta = false; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if ($quote) { |
| 56 |
$arg = '"'.(Preg::replace('/(\\\\*)$/', '$1$1', $arg)).'"'; |
| 57 |
} |
| 58 |
|
| 59 |
if ($meta) { |
| 60 |
$arg = Preg::replace('/(["^&|<>()%])/', '^$1', $arg); |
| 61 |
} |
| 62 |
|
| 63 |
return $arg; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Escapes an array of arguments that make up a shell command |
| 68 |
* |
| 69 |
* @param string[] $args Argument list, with the module name first |
| 70 |
*/ |
| 71 |
public static function escapeShellCommand(array $args): string |
| 72 |
{ |
| 73 |
$command = ''; |
| 74 |
$module = array_shift($args); |
| 75 |
|
| 76 |
if ($module !== null) { |
| 77 |
$command = self::escape($module, true, true); |
| 78 |
|
| 79 |
foreach ($args as $arg) { |
| 80 |
$command .= ' '.self::escape($arg); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return $command; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Makes putenv environment changes available in $_SERVER and $_ENV |
| 89 |
* |
| 90 |
* @param string $name |
| 91 |
* @param ?string $value A null value unsets the variable |
| 92 |
*/ |
| 93 |
public static function setEnv(string $name, ?string $value = null): bool |
| 94 |
{ |
| 95 |
$unset = null === $value; |
| 96 |
|
| 97 |
if (!putenv($unset ? $name : $name.'='.$value)) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
if ($unset) { |
| 102 |
unset($_SERVER[$name]); |
| 103 |
} else { |
| 104 |
$_SERVER[$name] = $value; |
| 105 |
} |
| 106 |
|
| 107 |
// Update $_ENV if it is being used |
| 108 |
if (false !== stripos((string) ini_get('variables_order'), 'E')) { |
| 109 |
if ($unset) { |
| 110 |
unset($_ENV[$name]); |
| 111 |
} else { |
| 112 |
$_ENV[$name] = $value; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return true; |
| 117 |
} |
| 118 |
} |
| 119 |
|