summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Data/Filter/FilterQueryString.php
blob: 8535df53ac0bcf589c4b6e1881edbb2490f79ca2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Data\Filter;

class FilterQueryString
{
    protected $string;

    protected $pos;

    protected $debug = array();

    protected $reportDebug = false;

    protected $length;

    protected function __construct()
    {
    }

    protected function debug($msg, $level = 0, $op = null)
    {
        if ($op === null) {
            $op = 'NULL';
        }
        $this->debug[] = sprintf(
            '%s[%d=%s] (%s): %s',
            str_repeat('* ', $level),
            $this->pos,
            $this->string[$this->pos - 1],
            $op,
            $msg
        );
    }

    public static function parse($string)
    {
        $parser = new static();
        return $parser->parseQueryString($string);
    }

    protected function readNextKey()
    {
        $str = $this->readUnlessSpecialChar();

        if ($str === false) {
            return $str;
        }
        return rawurldecode($str);
    }

    protected function readNextValue()
    {
        if ($this->nextChar() === '(') {
            $this->readChar();
            $var = preg_split('~\|~', $this->readUnless(')'));
            if ($this->readChar() !== ')') {
                $this->parseError(null, 'Expected ")"');
            }
        } else {
            $var = rawurldecode($this->readUnless(array(')', '&', '|', '>', '<')));
        }
        return $var;
    }

    protected function readNextExpression()
    {
        if ('' === ($key = $this->readNextKey())) {
            return false;
        }

        foreach (array('<', '>') as $sign) {
            if (false !== ($pos = strpos($key, $sign))) {
                if ($this->nextChar() === '=') {
                    break;
                }
                $var = substr($key, $pos + 1);
                $key = substr($key, 0, $pos);

                if (ctype_digit($var)) {
                    $var = (float) $var;
                }

                return Filter::expression($key, $sign, $var);
            }
        }
        if (in_array($this->nextChar(), array('=', '>', '<', '!'))) {
            $sign = $this->readChar();
        } else {
            $sign = false;
        }
        if ($sign === false) {
            return Filter::expression($key, '=', true);
        }

        $toFloat = false;
        if ($sign === '=') {
            $last = substr($key, -1);
            if ($last === '>' || $last === '<') {
                $sign = $last . $sign;
                $key = substr($key, 0, -1);
                $toFloat = true;
            }
        // TODO: Same as above for unescaped <> - do we really need this?
        } elseif ($sign === '>' || $sign === '<' || $sign === '!') {
            $toFloat = $sign === '>' || $sign === '<';
            if ($this->nextChar() === '=') {
                $sign .= $this->readChar();
            }
        }

        $var = $this->readNextValue();
        if ($toFloat && ctype_digit($var)) {
            $var = (float) $var;
        }

        return Filter::expression($key, $sign, $var);
    }

    protected function parseError($char = null, $extraMsg = null)
    {
        if ($extraMsg === null) {
            $extra = '';
        } else {
            $extra = ': ' . $extraMsg;
        }
        if ($char === null) {
            $char = $this->string[$this->pos];
        }
        if ($this->reportDebug) {
            $extra .= "\n" . implode("\n", $this->debug);
        }

        throw new FilterParseException(
            'Invalid filter "%s", unexpected %s at pos %d%s',
            $this->string,
            $char,
            $this->pos,
            $extra
        );
    }

    protected function readFilters($nestingLevel = 0, $op = null)
    {
        $filters = array();
        while ($this->pos < $this->length) {
            if ($op === '!' && count($filters) === 1) {
                break;
            }
            $filter = $this->readNextExpression();
            $next = $this->readChar();


            if ($filter === false) {
                $this->debug('Got no next expression, next is ' . $next, $nestingLevel, $op);
                if ($next === '!') {
                    $not = $this->readFilters($nestingLevel + 1, '!');
                    $filters[] = $not;
                    if (in_array($this->nextChar(), array('|', '&', ')'))) {
                        $next = $this->readChar();
                        $this->debug('Got NOT, next is now: ' . $next, $nestingLevel, $op);
                    } else {
                        $this->debug('Breaking after NOT: ' . $not, $nestingLevel, $op);
                        break;
                    }
                }

                if ($op === null && count($filters) > 0 && ($next === '&' || $next === '|')) {
                    $op = $next;
                    continue;
                }

                if ($next === false) {
                    // Nothing more to read
                    break;
                }

                if ($next === ')') {
                    if ($nestingLevel > 0) {
                        $this->debug('Closing without filter: ' . $next, $nestingLevel, $op);
                        break;
                    }
                    $this->parseError($next);
                }
                if ($next === '(') {
                    $filters[] = $this->readFilters($nestingLevel + 1, null);
                    continue;
                }
                if ($next === $op) {
                    continue;
                }
                $this->parseError($next, "$op level $nestingLevel");
            } else {
                $this->debug('Got new expression: ' . $filter, $nestingLevel, $op);

                $filters[] = $filter;

                if ($next === false) {
                    $this->debug('Next is false, nothing to read but got filter', $nestingLevel, $op);
                    // Got filter, nothing more to read
                    break;
                }

                if ($op === '!') {
                    $this->pos--;
                    break;
                }
                if ($next === $op) {
                    $this->debug('Next matches operator', $nestingLevel, $op);
                    continue; // Break??
                }

                if ($next === ')') {
                    if ($nestingLevel > 0) {
                        $this->debug('Closing with filter: ' . $next, $nestingLevel, $op);
                        break;
                    }
                    $this->parseError($next);
                }
                if ($op === null && in_array($next, array('&', '|'))) {
                    $this->debug('Setting op to ' . $next, $nestingLevel, $op);
                    $op = $next;
                    continue;
                }
                $this->parseError($next);
            }
        }

        if ($nestingLevel === 0 && $this->pos < $this->length) {
            $this->parseError($op, 'Did not read full filter');
        }

        if ($nestingLevel === 0 && count($filters) === 1 && $op !== '!') {
            // There is only one filter expression, no chain
            $this->debug('Returning first filter only: ' . $filters[0], $nestingLevel, $op);
            return $filters[0];
        }

        if ($op === null && count($filters) === 1) {
            $this->debug('No op, single filter, setting AND', $nestingLevel, $op);
            $op = '&';
        }
        $this->debug(sprintf('Got %d filters, returning', count($filters)), $nestingLevel, $op);

        switch ($op) {
            case '&':
                return Filter::matchAll($filters);
            case '|':
                return Filter::matchAny($filters);
            case '!':
                return Filter::not($filters);
            case null:
                return Filter::matchAll();
            default:
                $this->parseError($op);
        }
    }

    protected function parseQueryString($string)
    {
        $this->pos = 0;

        $this->string = $string;

        $this->length = $string ? strlen($string) : 0;

        if ($this->length === 0) {
            return Filter::matchAll();
        }
        return $this->readFilters();
    }

    protected function readUnless($char)
    {
        $buffer = '';
        while (false !== ($c = $this->readChar())) {
            if (is_array($char)) {
                if (in_array($c, $char)) {
                    $this->pos--;
                    break;
                }
            } else {
                if ($c === $char) {
                    $this->pos--;
                    break;
                }
            }
            $buffer .= $c;
        }

        return $buffer;
    }

    protected function readUnlessSpecialChar()
    {
        return $this->readUnless(array('=', '(', ')', '&', '|', '>', '<', '!'));
    }

    protected function readExpressionOperator()
    {
        return $this->readUnless(array('=', '>', '<', '!'));
    }

    protected function readChar()
    {
        if ($this->length > $this->pos) {
            return $this->string[$this->pos++];
        }
        return false;
    }

    protected function nextChar()
    {
        if ($this->length > $this->pos) {
            return $this->string[$this->pos];
        }
        return false;
    }
}