forked from radgeek/feedwordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeedwordpress_parser.class.php
More file actions
529 lines (500 loc) · 14.5 KB
/
feedwordpress_parser.class.php
File metadata and controls
529 lines (500 loc) · 14.5 KB
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<?php
class FeedWordPress_Parser extends SimplePie_Parser {
function reset_parser (&$xml) {
// reset members
$this->namespace = array('');
$this->element = array('');
$this->xml_base = array('');
$this->xml_base_explicit = array(false);
$this->xml_lang = array('');
$this->data = array();
$this->datas = array(array());
$this->current_xhtml_construct = -1;
$this->xmlns_stack = array();
$this->xmlns_current = array();
// reset libxml parser
xml_parser_free($xml);
$xml = xml_parser_create_ns($this->encoding, $this->separator);
xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
xml_set_object($xml, $this);
xml_set_character_data_handler($xml, 'cdata');
xml_set_element_handler($xml, 'tag_open', 'tag_close');
xml_set_start_namespace_decl_handler($xml, 'start_xmlns');
}
public function parse (&$data, $encoding) {
$data = apply_filters('feedwordpress_parser_parse', $data, $encoding, $this);
// Use UTF-8 if we get passed US-ASCII, as every US-ASCII character is a UTF-8 character
if (strtoupper($encoding) === 'US-ASCII')
{
$this->encoding = 'UTF-8';
}
else
{
$this->encoding = $encoding;
}
// Strip BOM:
// UTF-32 Big Endian BOM
if (substr($data, 0, 4) === "\x00\x00\xFE\xFF")
{
$data = substr($data, 4);
}
// UTF-32 Little Endian BOM
elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00")
{
$data = substr($data, 4);
}
// UTF-16 Big Endian BOM
elseif (substr($data, 0, 2) === "\xFE\xFF")
{
$data = substr($data, 2);
}
// UTF-16 Little Endian BOM
elseif (substr($data, 0, 2) === "\xFF\xFE")
{
$data = substr($data, 2);
}
// UTF-8 BOM
elseif (substr($data, 0, 3) === "\xEF\xBB\xBF")
{
$data = substr($data, 3);
}
if (substr($data, 0, 5) === '<?xml' && strspn(substr($data, 5, 1), "\x09\x0A\x0D\x20") && ($pos = strpos($data, '?>')) !== false)
{
$declaration = new SimplePie_XML_Declaration_Parser(substr($data, 5, $pos - 5));
if ($declaration->parse())
{
$data = substr($data, $pos + 2);
$data = '<?xml version="' . $declaration->version . '" encoding="' . $encoding . '" standalone="' . (($declaration->standalone) ? 'yes' : 'no') . '"?>' . $data;
}
else
{
$this->error_string = 'SimplePie bug! Please report this!';
return false;
}
}
$return = true;
static $xml_is_sane = null;
if ($xml_is_sane === null)
{
$parser_check = xml_parser_create();
xml_parse_into_struct($parser_check, '<foo>&</foo>', $values);
xml_parser_free($parser_check);
$xml_is_sane = isset($values[0]['value']);
}
// Create the parser
if ($xml_is_sane)
{
$xml = xml_parser_create_ns($this->encoding, $this->separator);
xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
xml_set_object($xml, $this);
xml_set_character_data_handler($xml, 'cdata');
xml_set_element_handler($xml, 'tag_open', 'tag_close');
xml_set_start_namespace_decl_handler($xml, 'start_xmlns');
// Parse!
$parseResults = xml_parse($xml, $data, true);
$endOfJunk = strpos($data, '<?xml');
if (!$parseResults and $endOfJunk > 0) :
// There is some junk before the feed prolog. Try to get rid of it.
$data = substr($data, $endOfJunk);
$data = trim($data);
$this->reset_parser($xml);
$parseResults = xml_parse($xml, $data, true);
endif;
$badEntity = (xml_get_error_code($xml) == 26);
if (!$parseResults and $badEntity) :
// There was an entity that libxml couldn't understand.
// Chances are, it was a stray HTML entity. So let's try
// converting all the named HTML entities to numeric XML
// entities and starting over.
$data = $this->html_convert_entities($data);
$this->reset_parser($xml);
$parseResults = xml_parse($xml, $data, true);
endif;
if (!$parseResults) {
$this->error_code = xml_get_error_code($xml);
$this->error_string = xml_error_string($this->error_code);
$return = false;
}
$this->current_line = xml_get_current_line_number($xml);
$this->current_column = xml_get_current_column_number($xml);
$this->current_byte = xml_get_current_byte_index($xml);
xml_parser_free($xml);
return $return;
}
else
{
libxml_clear_errors();
$xml = new XMLReader();
$xml->xml($data);
while (@$xml->read())
{
switch ($xml->nodeType)
{
case constant('XMLReader::END_ELEMENT'):
if ($xml->namespaceURI !== '')
{
$tagName = "{$xml->namespaceURI}{$this->separator}{$xml->localName}";
}
else
{
$tagName = $xml->localName;
}
$this->tag_close(null, $tagName);
break;
case constant('XMLReader::ELEMENT'):
$empty = $xml->isEmptyElement;
if ($xml->namespaceURI !== '')
{
$tagName = "{$xml->namespaceURI}{$this->separator}{$xml->localName}";
}
else
{
$tagName = $xml->localName;
}
$attributes = array();
while ($xml->moveToNextAttribute())
{
if ($xml->namespaceURI !== '')
{
$attrName = "{$xml->namespaceURI}{$this->separator}{$xml->localName}";
}
else
{
$attrName = $xml->localName;
}
$attributes[$attrName] = $xml->value;
}
foreach ($attributes as $attr => $value) :
list($ns, $local) = $this->split_ns($attr);
if ($ns=='http://www.w3.org/2000/xmlns/') :
if ('xmlns' == $local) : $local = false; endif;
$this->start_xmlns(null, $local, $value);
endif;
endforeach;
$this->tag_open(null, $tagName, $attributes);
if ($empty)
{
$this->tag_close(null, $tagName);
}
break;
case constant('XMLReader::TEXT'):
case constant('XMLReader::CDATA'):
$this->cdata(null, $xml->value);
break;
}
}
if ($error = libxml_get_last_error())
{
$this->error_code = $error->code;
$this->error_string = $error->message;
$this->current_line = $error->line;
$this->current_column = $error->column;
return false;
}
else
{
return true;
}
}
} /* FeedWordPress_Parser::parse() */
var $xmlns_stack = array();
var $xmlns_current = array();
function tag_open ($parser, $tag, $attributes) {
$ret = parent::tag_open($parser, $tag, $attributes);
if ($this->current_xhtml_construct < 0) :
$this->data['xmlns'] = $this->xmlns_current;
$this->xmlns_stack[] = $this->xmlns_current;
endif;
return $ret;
}
function tag_close($parser, $tag) {
if ($this->current_xhtml_construct < 0) :
$this->xmlns_current = array_pop($this->xmlns_stack);
endif;
$ret = parent::tag_close($parser, $tag);
return $ret;
}
function start_xmlns ($parser, $prefix, $uri) {
if (!$prefix) :
$prefix = '';
endif;
if ($this->current_xhtml_construct < 0) :
$this->xmlns_current[$prefix] = $uri;
endif;
return true;
} /* FeedWordPress_Parser::start_xmlns() */
/* html_convert_entities($string) -- convert named HTML entities to
* XML-compatible numeric entities. Adapted from code by @inanimatt:
* https://gist.github.com/inanimatt/879249
*/
public function html_convert_entities($string) {
return preg_replace_callback('/&([a-zA-Z][a-zA-Z0-9]+);/S',
array($this, 'convert_entity'), $string);
}
/* Swap HTML named entity with its numeric equivalent. If the entity
* isn't in the lookup table, this function returns a blank, which
* destroys the character in the output - this is probably the
* desired behaviour when producing XML. Adapted from code by @inanimatt:
* https://gist.github.com/inanimatt/879249
*/
public function convert_entity($matches) {
static $table = array(
'quot' => '"',
'amp' => '&',
'lt' => '<',
'gt' => '>',
'OElig' => 'Œ',
'oelig' => 'œ',
'Scaron' => 'Š',
'scaron' => 'š',
'Yuml' => 'Ÿ',
'circ' => 'ˆ',
'tilde' => '˜',
'ensp' => ' ',
'emsp' => ' ',
'thinsp' => ' ',
'zwnj' => '‌',
'zwj' => '‍',
'lrm' => '‎',
'rlm' => '‏',
'ndash' => '–',
'mdash' => '—',
'lsquo' => '‘',
'rsquo' => '’',
'sbquo' => '‚',
'ldquo' => '“',
'rdquo' => '”',
'bdquo' => '„',
'dagger' => '†',
'Dagger' => '‡',
'permil' => '‰',
'lsaquo' => '‹',
'rsaquo' => '›',
'euro' => '€',
'fnof' => 'ƒ',
'Alpha' => 'Α',
'Beta' => 'Β',
'Gamma' => 'Γ',
'Delta' => 'Δ',
'Epsilon' => 'Ε',
'Zeta' => 'Ζ',
'Eta' => 'Η',
'Theta' => 'Θ',
'Iota' => 'Ι',
'Kappa' => 'Κ',
'Lambda' => 'Λ',
'Mu' => 'Μ',
'Nu' => 'Ν',
'Xi' => 'Ξ',
'Omicron' => 'Ο',
'Pi' => 'Π',
'Rho' => 'Ρ',
'Sigma' => 'Σ',
'Tau' => 'Τ',
'Upsilon' => 'Υ',
'Phi' => 'Φ',
'Chi' => 'Χ',
'Psi' => 'Ψ',
'Omega' => 'Ω',
'alpha' => 'α',
'beta' => 'β',
'gamma' => 'γ',
'delta' => 'δ',
'epsilon' => 'ε',
'zeta' => 'ζ',
'eta' => 'η',
'theta' => 'θ',
'iota' => 'ι',
'kappa' => 'κ',
'lambda' => 'λ',
'mu' => 'μ',
'nu' => 'ν',
'xi' => 'ξ',
'omicron' => 'ο',
'pi' => 'π',
'rho' => 'ρ',
'sigmaf' => 'ς',
'sigma' => 'σ',
'tau' => 'τ',
'upsilon' => 'υ',
'phi' => 'φ',
'chi' => 'χ',
'psi' => 'ψ',
'omega' => 'ω',
'thetasym' => 'ϑ',
'upsih' => 'ϒ',
'piv' => 'ϖ',
'bull' => '•',
'hellip' => '…',
'prime' => '′',
'Prime' => '″',
'oline' => '‾',
'frasl' => '⁄',
'weierp' => '℘',
'image' => 'ℑ',
'real' => 'ℜ',
'trade' => '™',
'alefsym' => 'ℵ',
'larr' => '←',
'uarr' => '↑',
'rarr' => '→',
'darr' => '↓',
'harr' => '↔',
'crarr' => '↵',
'lArr' => '⇐',
'uArr' => '⇑',
'rArr' => '⇒',
'dArr' => '⇓',
'hArr' => '⇔',
'forall' => '∀',
'part' => '∂',
'exist' => '∃',
'empty' => '∅',
'nabla' => '∇',
'isin' => '∈',
'notin' => '∉',
'ni' => '∋',
'prod' => '∏',
'sum' => '∑',
'minus' => '−',
'lowast' => '∗',
'radic' => '√',
'prop' => '∝',
'infin' => '∞',
'ang' => '∠',
'and' => '∧',
'or' => '∨',
'cap' => '∩',
'cup' => '∪',
'int' => '∫',
'there4' => '∴',
'sim' => '∼',
'cong' => '≅',
'asymp' => '≈',
'ne' => '≠',
'equiv' => '≡',
'le' => '≤',
'ge' => '≥',
'sub' => '⊂',
'sup' => '⊃',
'nsub' => '⊄',
'sube' => '⊆',
'supe' => '⊇',
'oplus' => '⊕',
'otimes' => '⊗',
'perp' => '⊥',
'sdot' => '⋅',
'lceil' => '⌈',
'rceil' => '⌉',
'lfloor' => '⌊',
'rfloor' => '⌋',
'lang' => '〈',
'rang' => '〉',
'loz' => '◊',
'spades' => '♠',
'clubs' => '♣',
'hearts' => '♥',
'diams' => '♦',
'nbsp' => ' ',
'iexcl' => '¡',
'cent' => '¢',
'pound' => '£',
'curren' => '¤',
'yen' => '¥',
'brvbar' => '¦',
'sect' => '§',
'uml' => '¨',
'copy' => '©',
'ordf' => 'ª',
'laquo' => '«',
'not' => '¬',
'shy' => '­',
'reg' => '®',
'macr' => '¯',
'deg' => '°',
'plusmn' => '±',
'sup2' => '²',
'sup3' => '³',
'acute' => '´',
'micro' => 'µ',
'para' => '¶',
'middot' => '·',
'cedil' => '¸',
'sup1' => '¹',
'ordm' => 'º',
'raquo' => '»',
'frac14' => '¼',
'frac12' => '½',
'frac34' => '¾',
'iquest' => '¿',
'Agrave' => 'À',
'Aacute' => 'Á',
'Acirc' => 'Â',
'Atilde' => 'Ã',
'Auml' => 'Ä',
'Aring' => 'Å',
'AElig' => 'Æ',
'Ccedil' => 'Ç',
'Egrave' => 'È',
'Eacute' => 'É',
'Ecirc' => 'Ê',
'Euml' => 'Ë',
'Igrave' => 'Ì',
'Iacute' => 'Í',
'Icirc' => 'Î',
'Iuml' => 'Ï',
'ETH' => 'Ð',
'Ntilde' => 'Ñ',
'Ograve' => 'Ò',
'Oacute' => 'Ó',
'Ocirc' => 'Ô',
'Otilde' => 'Õ',
'Ouml' => 'Ö',
'times' => '×',
'Oslash' => 'Ø',
'Ugrave' => 'Ù',
'Uacute' => 'Ú',
'Ucirc' => 'Û',
'Uuml' => 'Ü',
'Yacute' => 'Ý',
'THORN' => 'Þ',
'szlig' => 'ß',
'agrave' => 'à',
'aacute' => 'á',
'acirc' => 'â',
'atilde' => 'ã',
'auml' => 'ä',
'aring' => 'å',
'aelig' => 'æ',
'ccedil' => 'ç',
'egrave' => 'è',
'eacute' => 'é',
'ecirc' => 'ê',
'euml' => 'ë',
'igrave' => 'ì',
'iacute' => 'í',
'icirc' => 'î',
'iuml' => 'ï',
'eth' => 'ð',
'ntilde' => 'ñ',
'ograve' => 'ò',
'oacute' => 'ó',
'ocirc' => 'ô',
'otilde' => 'õ',
'ouml' => 'ö',
'divide' => '÷',
'oslash' => 'ø',
'ugrave' => 'ù',
'uacute' => 'ú',
'ucirc' => 'û',
'uuml' => 'ü',
'yacute' => 'ý',
'thorn' => 'þ',
'yuml' => 'ÿ'
);
// Entity not found? Destroy it.
return isset($table[$matches[1]]) ? $table[$matches[1]] : '';
} /* FeedWordPress_Parser::convert_entity() */
} /* class FeedWordPress_Parser */