ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ProcessUtils.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
15 
24 {
28  private function __construct()
29  {
30  }
31 
39  public static function escapeArgument($argument)
40  {
41  //Fix for PHP bug #43784 escapeshellarg removes % from given string
42  //Fix for PHP bug #49446 escapeshellarg doesn't work on Windows
43  //@see https://bugs.php.net/bug.php?id=43784
44  //@see https://bugs.php.net/bug.php?id=49446
45  if ('\\' === DIRECTORY_SEPARATOR) {
46  if ('' === $argument) {
47  return escapeshellarg($argument);
48  }
49 
50  $escapedArgument = '';
51  $quote = false;
52  foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
53  if ('"' === $part) {
54  $escapedArgument .= '\\"';
55  } elseif (self::isSurroundedBy($part, '%')) {
56  // Avoid environment variable expansion
57  $escapedArgument .= '^%"'.substr($part, 1, -1).'"^%';
58  } else {
59  // escape trailing backslash
60  if ('\\' === substr($part, -1)) {
61  $part .= '\\';
62  }
63  $quote = true;
64  $escapedArgument .= $part;
65  }
66  }
67  if ($quote) {
68  $escapedArgument = '"'.$escapedArgument.'"';
69  }
70 
71  return $escapedArgument;
72  }
73 
74  return escapeshellarg($argument);
75  }
76 
87  public static function validateInput($caller, $input)
88  {
89  if (null !== $input) {
90  if (is_resource($input)) {
91  return $input;
92  }
93  if (is_string($input)) {
94  return $input;
95  }
96  if (is_scalar($input)) {
97  return (string) $input;
98  }
99  if ($input instanceof Process) {
100  return $input->getIterator($input::ITER_SKIP_ERR);
101  }
102  if ($input instanceof \Iterator) {
103  return $input;
104  }
105  if ($input instanceof \Traversable) {
106  return new \IteratorIterator($input);
107  }
108 
109  throw new InvalidArgumentException(sprintf('%s only accepts strings, Traversable objects or stream resources.', $caller));
110  }
111 
112  return $input;
113  }
114 
115  private static function isSurroundedBy($arg, $char)
116  {
117  return 2 < strlen($arg) && $char === $arg[0] && $char === $arg[strlen($arg) - 1];
118  }
119 }
static validateInput($caller, $input)
Validates and normalizes a Process input.
ProcessUtils is a bunch of utility methods.
static escapeArgument($argument)
Escapes a string to be used as a shell argument.
Process is a thin wrapper around proc_* functions to easily start independent PHP processes...
Definition: Process.php:30
__construct()
This class should not be instantiated.