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
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2014 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/**
* Class is used to validate and parse item keys.
*
* Example of usage:
* $itemKey = new CItemKey('test.key[a, b, c]');
* echo $itemKey->isValid(); // true
* echo $itemKey->getKeyId(); // test.key
* print_r($itemKey->parameters()); // array('a', 'b', 'c')
*/
class CItemKey {
const STATE_NEW = 0;
const STATE_END = 1;
const STATE_UNQUOTED = 2;
const STATE_QUOTED = 3;
private $keyId = ''; // main part of the key (for 'key[1, 2, 3]' key id would be 'key')
private $parameters = array();
private $isValid = true;
private $error = '';
/**
* Parse key and determine if it is valid.
*
* @param string $key
*/
public function __construct($key) {
$this->parseKey($key);
}
/**
* Returns an error message depending on input parameters.
*
* @param string $key
* @param int $pos
*
* @return string
*/
private function errorMessage($key, $pos) {
if (!isset($key[$pos])) {
return ($pos == 0) ? _('key is empty') : _('unexpected end of key');
}
for ($i = $pos, $chunk = '', $maxChunkSize = 50; isset($key[$i]); $i++) {
if (0x80 != (0xc0 & ord($key[$i])) && $maxChunkSize-- == 0) {
break;
}
$chunk .= $key[$i];
}
if (isset($key[$i])) {
$chunk .= ' ...';
}
return _s('incorrect syntax near "%1$s"', $chunk);
}
/**
* Parse key and parameters and put them into $this->parameters array.
*
* @param string $key
*/
private function parseKey($key) {
$pos = 0;
// checking every byte, one by one, until first 'not key_id' char is reached
while (isset($key[$pos])) {
if (!isKeyIdChar($key[$pos])) {
break; // $pos now points to the first 'not a key name' char
}
$this->keyId .= $key[$pos++];
}
// checking if key is empty
if ($pos == 0) {
$this->isValid = false;
$this->error = $this->errorMessage($key, $pos);
return;
}
// invalid symbol instead of '[', which would be the beginning of params
if (isset($key[$pos]) && $key[$pos] != '[') {
$this->isValid = false;
$this->error = $this->errorMessage($key, $pos);
return;
}
$state = self::STATE_END;
$level = 0;
$num = 0;
while (isset($key[$pos])) {
if ($level == 0) {
// first square bracket + Zapcat compatibility
if ($state == self::STATE_END && $key[$pos] == '[') {
$state = self::STATE_NEW;
}
else {
break;
}
}
switch ($state) {
// a new parameter started
case self::STATE_NEW:
switch ($key[$pos]) {
case ' ':
break;
case ',':
if ($level == 1) {
if (!isset($this->parameters[$num])) {
$this->parameters[$num] = '';
}
$num++;
}
break;
case '[':
$level++;
if ($level == 2) {
$l = $pos;
}
break;
case ']':
if ($level == 1) {
if (!isset($this->parameters[$num])) {
$this->parameters[$num] = '';
}
$num++;
}
elseif ($level == 2) {
$this->parameters[$num] = '';
for ($l++; $l < $pos; $l++) {
$this->parameters[$num] .= $key[$l];
}
}
$level--;
$state = self::STATE_END;
break;
case '"':
$state = self::STATE_QUOTED;
if ($level == 1) {
$l = $pos;
}
break;
default:
$state = self::STATE_UNQUOTED;
if ($level == 1) {
$l = $pos;
}
}
break;
// end of parameter
case self::STATE_END:
switch ($key[$pos]) {
case ' ':
break;
case ',':
$state = self::STATE_NEW;
if ($level == 1) {
if (!isset($this->parameters[$num])) {
$this->parameters[$num] = '';
}
$num++;
}
break;
case ']':
if ($level == 1) {
if (!isset($this->parameters[$num])) {
$this->parameters[$num] = '';
}
$num++;
}
elseif ($level == 2) {
$this->parameters[$num] = '';
for ($l++; $l < $pos; $l++) {
$this->parameters[$num] .= $key[$l];
}
}
$level--;
break;
default:
break 3;
}
break;
// an unquoted parameter
case self::STATE_UNQUOTED:
if ($key[$pos] == ']' || $key[$pos] == ',') {
if ($level == 1) {
$this->parameters[$num] = '';
for (; $l < $pos; $l++) {
$this->parameters[$num] .= $key[$l];
}
}
$pos--;
$state = self::STATE_END;
}
break;
// a quoted parameter
case self::STATE_QUOTED:
if ($key[$pos] == '"' && $key[$pos - 1] != '\\') {
if ($level == 1) {
$this->parameters[$num] = '';
for ($l++; $l < $pos; $l++) {
if ($key[$l] != '\\' || $key[$l + 1] != '"') {
$this->parameters[$num] .= $key[$l];
}
}
}
$state = self::STATE_END;
}
break;
}
$pos++;
}
if ($pos == 0 || isset($key[$pos]) || $level != 0) {
$this->isValid = false;
$this->error = $this->errorMessage($key, $pos);
}
}
/**
* Returns the result of validation.
*
* @return bool
*/
public function isValid() {
return $this->isValid;
}
/**
* Returns the error message if key is invalid.
*
* @return string
*/
public function getError() {
return $this->error;
}
/**
* Returns the left part of key without parameters.
*
* @return string
*/
public function getKeyId() {
return $this->keyId;
}
/**
* Returns the list of key parameters.
*
* @return array
*/
public function getParameters() {
return $this->parameters;
}
}
|