ezsystems /
ezpublish-kernel
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 | |||
| 3 | /** |
||
| 4 | * @copyright Copyright (C) eZ Systems AS. All rights reserved. |
||
| 5 | * @license For full copyright and license information view LICENSE file distributed with this source code. |
||
| 6 | */ |
||
| 7 | declare(strict_types=1); |
||
| 8 | |||
| 9 | namespace eZ\Publish\Core\QueryType\BuiltIn\SortSpec; |
||
| 10 | |||
| 11 | final class SortSpecLexer implements SortSpecLexerInterface |
||
| 12 | { |
||
| 13 | private const K_ASC = 'asc'; |
||
| 14 | private const K_DESC = 'desc'; |
||
| 15 | |||
| 16 | private const ID_PATTERN = '[a-zA-Z_][a-zA-Z0-9_]*'; |
||
| 17 | private const FLOAT_PATTERN = '-?[0-9]+\.[0-9]+'; |
||
| 18 | private const INT_PATTERN = '-?[0-9]+'; |
||
| 19 | |||
| 20 | /** @var string */ |
||
| 21 | private $input; |
||
| 22 | |||
| 23 | /** @var \eZ\Publish\Core\QueryType\BuiltIn\SortSpec\Token[] */ |
||
| 24 | private $tokens = []; |
||
| 25 | |||
| 26 | /** @var int|null */ |
||
| 27 | private $position; |
||
| 28 | |||
| 29 | /** @var \eZ\Publish\Core\QueryType\BuiltIn\SortSpec\Token|null */ |
||
| 30 | private $current; |
||
| 31 | |||
| 32 | /** @var \eZ\Publish\Core\QueryType\BuiltIn\SortSpec\Token|null */ |
||
| 33 | private $next; |
||
| 34 | |||
| 35 | public function getAll(): iterable |
||
| 36 | { |
||
| 37 | return $this->tokens; |
||
| 38 | } |
||
| 39 | |||
| 40 | public function consume(): Token |
||
| 41 | { |
||
| 42 | $this->current = $this->next; |
||
| 43 | $this->next = $this->tokens[++$this->position] ?? null; |
||
| 44 | |||
| 45 | return $this->current; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function isEOF(): bool |
||
| 49 | { |
||
| 50 | return $this->next === null || $this->next->isA(Token::TYPE_EOF); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function peek(): ?Token |
||
| 54 | { |
||
| 55 | return $this->next; |
||
| 56 | } |
||
| 57 | |||
| 58 | public function getInput(): string |
||
| 59 | { |
||
| 60 | return $this->input; |
||
| 61 | } |
||
| 62 | |||
| 63 | public function tokenize(string $input): void |
||
| 64 | { |
||
| 65 | $this->reset(); |
||
| 66 | |||
| 67 | $this->input = $input; |
||
| 68 | $this->tokens = []; |
||
| 69 | foreach ($this->split($input) as $match) { |
||
| 70 | [$value, $position] = $match; |
||
| 71 | $value = trim($value); |
||
| 72 | |||
| 73 | if ($value === '') { |
||
| 74 | // Skip whitespaces |
||
| 75 | continue; |
||
| 76 | } |
||
| 77 | |||
| 78 | $this->tokens[] = new Token( |
||
| 79 | $this->getTokenType($value), |
||
| 80 | $value, |
||
| 81 | $position |
||
| 82 | ); |
||
| 83 | } |
||
| 84 | |||
| 85 | $this->tokens[] = new Token(Token::TYPE_EOF); |
||
| 86 | $this->next = $this->tokens[0] ?? null; |
||
| 87 | } |
||
| 88 | |||
| 89 | private function reset(): void |
||
| 90 | { |
||
| 91 | $this->position = 0; |
||
| 92 | $this->next = null; |
||
| 93 | $this->current = null; |
||
| 94 | } |
||
| 95 | |||
| 96 | private function split(string $input): array |
||
| 97 | { |
||
| 98 | $regexp = sprintf( |
||
| 99 | '/^(asc)|(desc)|(\\.)|(,)|(%s)|\s+$/iu', |
||
| 100 | implode(')|(', [ |
||
| 101 | self::FLOAT_PATTERN, |
||
| 102 | self::INT_PATTERN, |
||
| 103 | self::ID_PATTERN, |
||
| 104 | ]), |
||
| 105 | ); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 106 | |||
| 107 | $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE; |
||
| 108 | |||
| 109 | return preg_split($regexp, $input, -1, $flags); |
||
| 110 | } |
||
| 111 | |||
| 112 | private function getTokenType(string $value): string |
||
| 113 | { |
||
| 114 | switch ($value) { |
||
| 115 | case self::K_ASC: |
||
| 116 | return Token::TYPE_ASC; |
||
| 117 | case self::K_DESC: |
||
| 118 | return Token::TYPE_DESC; |
||
| 119 | case '.': |
||
| 120 | return Token::TYPE_DOT; |
||
| 121 | case ',': |
||
| 122 | return Token::TYPE_COMMA; |
||
| 123 | } |
||
| 124 | |||
| 125 | if ($this->isInt($value)) { |
||
| 126 | return Token::TYPE_INT; |
||
| 127 | } |
||
| 128 | |||
| 129 | if ($this->isFloat($value)) { |
||
| 130 | return Token::TYPE_FLOAT; |
||
| 131 | } |
||
| 132 | |||
| 133 | if ($this->isID($value)) { |
||
| 134 | return Token::TYPE_ID; |
||
| 135 | } |
||
| 136 | |||
| 137 | return Token::TYPE_NONE; |
||
| 138 | } |
||
| 139 | |||
| 140 | private function isInt(string $value): bool |
||
| 141 | { |
||
| 142 | return preg_match('/^' . self::INT_PATTERN . '$/', $value) === 1; |
||
| 143 | } |
||
| 144 | |||
| 145 | private function isFloat(string $value): bool |
||
| 146 | { |
||
| 147 | return preg_match('/^' . self::FLOAT_PATTERN . '$/', $value) === 1; |
||
| 148 | } |
||
| 149 | |||
| 150 | private function isID(string $value): bool |
||
| 151 | { |
||
| 152 | return preg_match('/^' . self::ID_PATTERN . '$/', $value) === 1; |
||
| 153 | } |
||
| 154 | } |
||
| 155 |