/
Tokenize.php
413 lines (363 loc) · 10.6 KB
/
Tokenize.php
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php
/**
* Copyright 2012-2015 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2012-2015 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Imap_Client
*/
/**
* Tokenization of an IMAP data stream.
*
* NOTE: This class is NOT intended to be accessed outside of this package.
* There is NO guarantees that the API of this class will not change across
* versions.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2012-2015 Horde LLC
* @internal
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Imap_Client
*
* @property-read boolean $eos Has the end of the stream been reached?
*/
class Horde_Imap_Client_Tokenize implements Iterator
{
/**
* Current data.
*
* @var mixed
*/
protected $_current = false;
/**
* Current key.
*
* @var integer
*/
protected $_key = false;
/**
* Sublevel.
*
* @var integer
*/
protected $_level = false;
/**
* Array of literal stream objects.
*
* @var array
*/
protected $_literals = array();
/**
* Return Horde_Stream object for literal tokens?
*
* @var boolean
*/
protected $_literalStream = false;
/**
* next() modifiers.
*
* @var array
*/
protected $_nextModify = array();
/**
* Data stream.
*
* @var Horde_Stream
*/
protected $_stream;
/**
* Constructor.
*
* @param mixed $data Data to add (string, resource, or Horde_Stream
* object).
*/
public function __construct($data = null)
{
$this->_stream = new Horde_Stream_Temp();
if (!is_null($data)) {
$this->add($data);
}
}
/**
*/
public function __clone()
{
throw new LogicException('Object can not be cloned.');
}
/**
*/
public function __get($name)
{
switch ($name) {
case 'eos':
return $this->_stream->eof();
}
}
/**
*/
public function __sleep()
{
throw new LogicException('Object can not be serialized.');
}
/**
*/
public function __toString()
{
$pos = $this->_stream->pos();
$out = $this->_current . ' ' . $this->_stream->getString();
$this->_stream->seek($pos, false);
return $out;
}
/**
* Add data to buffer.
*
* @param mixed $data Data to add (string, resource, or Horde_Stream
* object).
*/
public function add($data)
{
$this->_stream->add($data);
}
/**
* Add data to literal stream at the current position.
*
* @param mixed $data Data to add (string, resource, or Horde_Stream
* object).
*/
public function addLiteralStream($data)
{
$pos = $this->_stream->pos();
if (!isset($this->_literals[$pos])) {
$this->_literals[$pos] = new Horde_Stream_Temp();
}
$this->_literals[$pos]->add($data);
}
/**
* Flush the remaining entries left in the iterator.
*
* @param boolean $return If true, return entries. Only returns entries
* on the current level.
* @param boolean $sublevel Only flush items in current sublevel?
*
* @return array The entries if $return is true.
*/
public function flushIterator($return = true, $sublevel = true)
{
$out = array();
if ($return) {
$this->_nextModify = array(
'level' => $sublevel ? $this->_level : 0,
'out' => array()
);
$this->next();
$out = $this->_nextModify['out'];
$this->_nextModify = array();
} elseif ($sublevel && $this->_level) {
$this->_nextModify = array(
'level' => $this->_level
);
$this->next();
$this->_nextModify = array();
} else {
$this->_stream->end();
$this->_stream->getChar();
$this->_current = $this->_key = $this->_level = false;
}
return $out;
}
/**
* Return literal length data located at the end of the stream.
*
* @return mixed Null if no literal data found, or an array with these
* keys:
* - binary: (boolean) True if this is a literal8.
* - length: (integer) Length of the literal.
*/
public function getLiteralLength()
{
if ($this->_stream->substring(-1, 1) === '}') {
$literal_data = $this->_stream->getString(
$this->_stream->search('{', true) - 1
);
$literal_len = substr($literal_data, 2, -1);
if (is_numeric($literal_len)) {
return array(
'binary' => ($literal_data[0] === '~'),
'length' => intval($literal_len)
);
}
}
return null;
}
/* Iterator methods. */
/**
*/
public function current()
{
return $this->_current;
}
/**
*/
public function key()
{
return $this->_key;
}
/**
* @return mixed Either a string, boolean (true for open paren, false for
* close paren/EOS), Horde_Stream object, or null.
*/
public function next()
{
$level = isset($this->_nextModify['level'])
? $this->_nextModify['level']
: null;
/* Directly access stream here to drastically reduce the number of
* getChar() calls we would have to make. */
$stream = $this->_stream->stream;
do {
$check_len = true;
$in_quote = $text = $binary = false;
while (($c = fgetc($stream)) !== false) {
switch ($c) {
case '\\':
$text .= $in_quote
? fgetc($stream)
: $c;
break;
case '"':
if ($in_quote) {
$check_len = false;
break 2;
}
$in_quote = true;
/* Set $text to non-false (could be empty string). */
$text = '';
break;
default:
if ($in_quote) {
$text .= $c;
break;
}
switch ($c) {
case '(':
++$this->_level;
$check_len = false;
$text = true;
break 3;
case ')':
if ($text === false) {
--$this->_level;
$check_len = $text = false;
} else {
$this->_stream->seek(-1);
}
break 3;
case '~':
// Ignore binary string identifier. PHP strings are
// binary-safe. But keep it is not used as string
// identifier.
$binary = true;
$text .= $c;
continue;
case '{':
if ($binary) {
$text = substr($text, 0, -1);
}
$literal_len = intval($this->_stream->getToChar('}'));
$pos = $this->_stream->pos();
if (isset($this->_literals[$pos])) {
$text = $this->_literals[$pos];
if (!$this->_literalStream) {
$text = strval($text);
}
} elseif ($this->_literalStream) {
$text = new Horde_Stream_Temp();
while (($literal_len > 0) && !feof($stream)) {
$part = $this->_stream->substring(
0,
min($literal_len, 8192)
);
$text->add($part);
$literal_len -= strlen($part);
}
} else {
$text = $this->_stream->substring(0, $literal_len);
}
$check_len = false;
break 3;
case ' ':
if ($text !== false) {
break 3;
}
break;
default:
$text .= $c;
break;
}
break;
}
$binary = false;
}
if ($check_len) {
switch (strlen($text)) {
case 0:
$text = false;
break;
case 3:
if (strcasecmp($text, 'NIL') === 0) {
$text = null;
}
break;
}
}
if (($text === false) && feof($stream)) {
$this->_key = $this->_level = false;
break;
}
++$this->_key;
if (is_null($level) || ($level > $this->_level)) {
break;
}
if (($level === $this->_level) && !is_bool($text)) {
$this->_nextModify['out'][] = $text;
}
} while (true);
$this->_current = $text;
return $text;
}
/**
* Force return of literal data as stream, if next token.
*
* @see next()
*/
public function nextStream()
{
$changed = $this->_literalStream;
$this->_literalStream = true;
$out = $this->next();
if ($changed) {
$this->_literalStream = false;
}
return $out;
}
/**
*/
public function rewind()
{
$this->_stream->rewind();
$this->_current = false;
$this->_key = -1;
$this->_level = 0;
}
/**
*/
public function valid()
{
return ($this->_level !== false);
}
}