Process.php 3.1 KB

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