Process.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. if ($meta) {
  38. $meta = $dquotes || Preg::isMatch('/%[^%]+%/', $arg);
  39. if (!$meta) {
  40. $quote = $quote || strpbrk($arg, '^&|<>()') !== false;
  41. } elseif ($module && !$dquotes && $quote) {
  42. $meta = false;
  43. }
  44. }
  45. if ($quote) {
  46. $arg = '"'.(Preg::replace('/(\\\\*)$/', '$1$1', $arg)).'"';
  47. }
  48. if ($meta) {
  49. $arg = Preg::replace('/(["^&|<>()%])/', '^$1', $arg);
  50. }
  51. return $arg;
  52. }
  53. /**
  54. * Escapes an array of arguments that make up a shell command
  55. *
  56. * @param string[] $args Argument list, with the module name first
  57. */
  58. public static function escapeShellCommand(array $args): string
  59. {
  60. $command = '';
  61. $module = array_shift($args);
  62. if ($module !== null) {
  63. $command = self::escape($module, true, true);
  64. foreach ($args as $arg) {
  65. $command .= ' '.self::escape($arg);
  66. }
  67. }
  68. return $command;
  69. }
  70. /**
  71. * Makes putenv environment changes available in $_SERVER and $_ENV
  72. *
  73. * @param string $name
  74. * @param ?string $value A null value unsets the variable
  75. */
  76. public static function setEnv(string $name, ?string $value = null): bool
  77. {
  78. $unset = null === $value;
  79. if (!putenv($unset ? $name : $name.'='.$value)) {
  80. return false;
  81. }
  82. if ($unset) {
  83. unset($_SERVER[$name]);
  84. } else {
  85. $_SERVER[$name] = $value;
  86. }
  87. // Update $_ENV if it is being used
  88. if (false !== stripos((string) ini_get('variables_order'), 'E')) {
  89. if ($unset) {
  90. unset($_ENV[$name]);
  91. } else {
  92. $_ENV[$name] = $value;
  93. }
  94. }
  95. return true;
  96. }
  97. }