redaxscript /
redaxscript
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace Redaxscript\Console; |
||
| 3 | |||
| 4 | use Redaxscript\Request; |
||
| 5 | use function array_filter; |
||
| 6 | use function array_key_exists; |
||
| 7 | use function array_shift; |
||
| 8 | use function explode; |
||
| 9 | use function is_array; |
||
| 10 | use function next; |
||
| 11 | use function strpos; |
||
| 12 | use function substr; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * parent class to parse the command line interface |
||
| 16 | * |
||
| 17 | * @since 3.0.0 |
||
| 18 | * |
||
| 19 | * @package Redaxscript |
||
| 20 | * @category Console |
||
| 21 | * @author Henry Ruhs |
||
| 22 | */ |
||
| 23 | |||
| 24 | class Parser |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * array of parsed arguments |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | |||
| 32 | protected array $_argumentArray = []; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 33 | |||
| 34 | /** |
||
| 35 | * array of parsed options |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | |||
| 40 | protected array $_optionArray = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * constructor of the class |
||
| 44 | * |
||
| 45 | * @since 3.0.0 |
||
| 46 | * |
||
| 47 | * @param Request $_request instance of the request class |
||
| 48 | */ |
||
| 49 | |||
| 50 | public function __construct(protected Request $_request) |
||
| 51 | { |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * init the class |
||
| 56 | * |
||
| 57 | 13 | * @param string $mode name of the mode |
|
| 58 | * |
||
| 59 | 13 | * @since 3.0.0 |
|
| 60 | 13 | */ |
|
| 61 | |||
| 62 | public function init(string $mode = null) : void |
||
| 63 | { |
||
| 64 | $argumentArray = []; |
||
| 65 | if ($mode === 'cli') |
||
| 66 | { |
||
| 67 | $argumentArray = $this->_request->getServer('argv'); |
||
| 68 | if (is_array($argumentArray) && array_key_exists(0, $argumentArray)) |
||
| 69 | { |
||
| 70 | 11 | array_shift($argumentArray); |
|
| 71 | } |
||
| 72 | 11 | } |
|
| 73 | if ($mode === 'template') |
||
| 74 | 11 | { |
|
| 75 | 11 | $argumentArray = array_filter(explode(' ', $this->_request->getStream('argv'))); |
|
| 76 | } |
||
| 77 | 11 | $this->_parseArgument($argumentArray); |
|
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | 11 | * get the value from arguments |
|
| 82 | 11 | * |
|
| 83 | * @since 3.0.0 |
||
| 84 | * |
||
| 85 | * @param string $key key of the item |
||
| 86 | * |
||
| 87 | * @return string|null |
||
| 88 | */ |
||
| 89 | |||
| 90 | public function getArgument(string $key = null) : ?string |
||
| 91 | { |
||
| 92 | if (is_array($this->_argumentArray) && array_key_exists($key, $this->_argumentArray)) |
||
| 93 | { |
||
| 94 | 2 | return $this->_argumentArray[$key]; |
|
| 95 | } |
||
| 96 | 2 | return null; |
|
| 97 | } |
||
| 98 | 1 | ||
| 99 | /** |
||
| 100 | 1 | * get the array from arguments |
|
| 101 | * |
||
| 102 | * @since 4.0.0 |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | |||
| 107 | public function getArgumentArray() : array |
||
| 108 | { |
||
| 109 | return $this->_argumentArray; |
||
| 110 | } |
||
| 111 | 5 | ||
| 112 | /** |
||
| 113 | 5 | * get the value from options |
|
| 114 | * |
||
| 115 | * @since 3.0.0 |
||
| 116 | * |
||
| 117 | * @param string $key key of the item |
||
| 118 | * |
||
| 119 | * @return string|null |
||
| 120 | */ |
||
| 121 | |||
| 122 | public function getOption(string $key = null) : ?string |
||
| 123 | { |
||
| 124 | if (is_array($this->_optionArray) && array_key_exists($key, $this->_optionArray)) |
||
| 125 | { |
||
| 126 | 2 | return $this->_optionArray[$key]; |
|
| 127 | } |
||
| 128 | 2 | return null; |
|
| 129 | } |
||
| 130 | 1 | ||
| 131 | /** |
||
| 132 | 1 | * get the array from options |
|
| 133 | * |
||
| 134 | * @since 4.0.0 |
||
| 135 | * |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | |||
| 139 | public function getOptionArray() : array |
||
| 140 | { |
||
| 141 | return $this->_optionArray; |
||
| 142 | } |
||
| 143 | 4 | ||
| 144 | /** |
||
| 145 | 4 | * set the value to arguments |
|
| 146 | * |
||
| 147 | * @since 3.0.0 |
||
| 148 | * |
||
| 149 | * @param string $key key of the item |
||
| 150 | * @param string|int $value value of the item |
||
| 151 | */ |
||
| 152 | |||
| 153 | public function setArgument(string $key = null, $value = null) : void |
||
| 154 | { |
||
| 155 | $this->_argumentArray[$key] = $value; |
||
| 156 | } |
||
| 157 | 7 | ||
| 158 | /** |
||
| 159 | 7 | * set the value to options |
|
| 160 | 7 | * |
|
| 161 | * @since 3.0.0 |
||
| 162 | * |
||
| 163 | * @param string $key key of the item |
||
| 164 | * @param string|int $value value of the item |
||
| 165 | */ |
||
| 166 | |||
| 167 | public function setOption(string $key = null, $value = null) : void |
||
| 168 | { |
||
| 169 | $this->_optionArray[$key] = $value; |
||
| 170 | } |
||
| 171 | 5 | ||
| 172 | /** |
||
| 173 | 5 | * parse raw argument |
|
| 174 | 5 | * |
|
| 175 | * @since 3.0.0 |
||
| 176 | * |
||
| 177 | * @param array|null $argumentArray raw argument to be parsed |
||
| 178 | */ |
||
| 179 | |||
| 180 | protected function _parseArgument(?array $argumentArray = []) : void |
||
| 181 | { |
||
| 182 | $doSkip = false; |
||
| 183 | $argumentKey = 0; |
||
| 184 | 11 | if (is_array($argumentArray)) |
|
| 185 | { |
||
| 186 | 11 | foreach ($argumentArray as $value) |
|
| 187 | 11 | { |
|
| 188 | 11 | $next = next($argumentArray); |
|
| 189 | if (strpos($value, '-') === 0) |
||
| 190 | 8 | { |
|
| 191 | $offset = strpos($value, '--') === 0 ? 2 : 1; |
||
| 192 | 8 | $optionArray = $this->_parseOption($value, $next, $offset); |
|
| 193 | 8 | $doSkip = $optionArray['value'] === $next; |
|
| 194 | $this->setOption($optionArray['key'], $optionArray['value']); |
||
| 195 | 4 | } |
|
| 196 | 4 | else if ($value && !$doSkip) |
|
| 197 | 4 | { |
|
| 198 | 4 | $this->setArgument($argumentKey++, $value); |
|
| 199 | } |
||
| 200 | 8 | } |
|
| 201 | } |
||
| 202 | 6 | } |
|
| 203 | |||
| 204 | /** |
||
| 205 | * parse raw option |
||
| 206 | 11 | * |
|
| 207 | * @since 3.0.0 |
||
| 208 | * |
||
| 209 | * @param string $option raw option to be parsed |
||
| 210 | * @param string $next raw next to be parsed |
||
| 211 | * @param int $offset offset of the raw option |
||
| 212 | * |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | |||
| 216 | protected function _parseOption(string $option = null, string $next = null, int $offset = null) : array |
||
| 217 | { |
||
| 218 | $equalPosition = strpos($option, '='); |
||
| 219 | if ($equalPosition) |
||
| 220 | 4 | { |
|
| 221 | return |
||
| 222 | 4 | [ |
|
| 223 | 4 | 'key' => substr($option, $offset, $equalPosition - $offset), |
|
| 224 | 'value' => substr($option, $equalPosition + 1) |
||
| 225 | ]; |
||
| 226 | } |
||
| 227 | 4 | return |
|
| 228 | 4 | [ |
|
| 229 | 'key' => substr($option, $offset), |
||
| 230 | 'value' => !$next || strpos($next, '-') === 0 ? true : $next |
||
| 231 | ]; |
||
| 232 | } |
||
| 233 | } |
||
| 234 |