PluginProbe
FeedWordPress / 2014.0805
FeedWordPress v2014.0805
trunk 0.8 0.9 0.91 0.95 0.96 0.97 0.98 0.981 0.99 0.991 0.992 0.993 2008.1030 2008.1101 2008.1105 2008.1214 2009.0612 2009.0613 2009.0618 2009.0707 2009.1111 2009.1112 2010.0127 2010.0528 All 65 releases
feedwordpress / feedwordpress_parser.class.php

feedwordpress_parser.class.php in FeedWordPress 2014.0805, at feedwordpress_parser.class.php

245 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 class FeedWordPress_Parser extends SimplePie_Parser {
3 function reset_parser (&$xml) {
4 xml_parser_free($xml);
5
6 $xml = xml_parser_create_ns($this->encoding, $this->separator);
7 xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);
8 xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
9 xml_set_object($xml, $this);
10 xml_set_character_data_handler($xml, 'cdata');
11 xml_set_element_handler($xml, 'tag_open', 'tag_close');
12 xml_set_start_namespace_decl_handler($xml, 'start_xmlns');
13 }
14
15 function parse (&$data, $encoding) {
16 $data = apply_filters('feedwordpress_parser_parse', $data, $encoding, $this);
17
18 // Use UTF-8 if we get passed US-ASCII, as every US-ASCII character is a UTF-8 character
19 if (strtoupper($encoding) === 'US-ASCII')
20 {
21 $this->encoding = 'UTF-8';
22 }
23 else
24 {
25 $this->encoding = $encoding;
26 }
27
28 // Strip BOM:
29 // UTF-32 Big Endian BOM
30 if (substr($data, 0, 4) === "\x00\x00\xFE\xFF")
31 {
32 $data = substr($data, 4);
33 }
34 // UTF-32 Little Endian BOM
35 elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00")
36 {
37 $data = substr($data, 4);
38 }
39 // UTF-16 Big Endian BOM
40 elseif (substr($data, 0, 2) === "\xFE\xFF")
41 {
42 $data = substr($data, 2);
43 }
44 // UTF-16 Little Endian BOM
45 elseif (substr($data, 0, 2) === "\xFF\xFE")
46 {
47 $data = substr($data, 2);
48 }
49 // UTF-8 BOM
50 elseif (substr($data, 0, 3) === "\xEF\xBB\xBF")
51 {
52 $data = substr($data, 3);
53 }
54
55 if (substr($data, 0, 5) === '<?xml' && strspn(substr($data, 5, 1), "\x09\x0A\x0D\x20") && ($pos = strpos($data, '?>')) !== false)
56 {
57 $declaration = new SimplePie_XML_Declaration_Parser(substr($data, 5, $pos - 5));
58 if ($declaration->parse())
59 {
60 $data = substr($data, $pos + 2);
61 $data = '<?xml version="' . $declaration->version . '" encoding="' . $encoding . '" standalone="' . (($declaration->standalone) ? 'yes' : 'no') . '"?>' . $data;
62 }
63 else
64 {
65 $this->error_string = 'SimplePie bug! Please report this!';
66 return false;
67 }
68 }
69
70 $return = true;
71
72 static $xml_is_sane = null;
73 if ($xml_is_sane === null)
74 {
75 $parser_check = xml_parser_create();
76 xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
77 xml_parser_free($parser_check);
78 $xml_is_sane = isset($values[0]['value']);
79 }
80
81 // Create the parser
82 if ($xml_is_sane)
83 {
84 $xml = xml_parser_create_ns($this->encoding, $this->separator);
85 xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);
86 xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
87 xml_set_object($xml, $this);
88 xml_set_character_data_handler($xml, 'cdata');
89 xml_set_element_handler($xml, 'tag_open', 'tag_close');
90 xml_set_start_namespace_decl_handler($xml, 'start_xmlns');
91
92 // Parse!
93 $parseResults = xml_parse($xml, $data, true);
94
95 $endOfJunk = strpos($data, '<?xml');
96 if (!$parseResults and $endOfJunk > 0) :
97 // There is some junk before the feed prolog. Try to get rid of it.
98 $newData = substr($data, $endOfJunk);
99 $newData = trim($newData);
100 $this->reset_parser($xml);
101
102 $parseResults = xml_parse($xml, $newData, true);
103 endif;
104
105 if (!$parseResults)
106 {
107 if (class_exists('DOMDocument')) :
108 libxml_use_internal_errors(true);
109 $doc = new DOMDocument;
110 $doc->loadHTML('<html>'.$data.'</html>');
111 ///*DBG*/ echo "<plaintext>";
112 ///*DBG*/ $dd = $doc->getElementsByTagName('html');
113 ///*DBG*/ for ($i = 0; $i < $dd->length; $i++) :
114 ///*DBG*/ var_dump($dd->item($i)->nodeName);
115 ///*DBG*/ endfor;
116 ///*DBG*/ var_dump($doc);
117 libxml_use_internal_errors(false);
118 ///*DBG*/ die;
119 endif;
120
121 $this->error_code = xml_get_error_code($xml);
122 $this->error_string = xml_error_string($this->error_code);
123 ///*DBG*/ echo "WOOGA WOOGA"; var_dump($this->error_string); die;
124 $return = false;
125 }
126
127 $this->current_line = xml_get_current_line_number($xml);
128 $this->current_column = xml_get_current_column_number($xml);
129 $this->current_byte = xml_get_current_byte_index($xml);
130 xml_parser_free($xml);
131
132 return $return;
133 }
134 else
135 {
136 libxml_clear_errors();
137 $xml = new XMLReader();
138 $xml->xml($data);
139 while (@$xml->read())
140 {
141 switch ($xml->nodeType)
142 {
143
144 case constant('XMLReader::END_ELEMENT'):
145 if ($xml->namespaceURI !== '')
146 {
147 $tagName = "{$xml->namespaceURI}{$this->separator}{$xml->localName}";
148 }
149 else
150 {
151 $tagName = $xml->localName;
152 }
153 $this->tag_close(null, $tagName);
154 break;
155 case constant('XMLReader::ELEMENT'):
156 $empty = $xml->isEmptyElement;
157 if ($xml->namespaceURI !== '')
158 {
159 $tagName = "{$xml->namespaceURI}{$this->separator}{$xml->localName}";
160 }
161 else
162 {
163 $tagName = $xml->localName;
164 }
165 $attributes = array();
166 while ($xml->moveToNextAttribute())
167 {
168 if ($xml->namespaceURI !== '')
169 {
170 $attrName = "{$xml->namespaceURI}{$this->separator}{$xml->localName}";
171 }
172 else
173 {
174 $attrName = $xml->localName;
175 }
176 $attributes[$attrName] = $xml->value;
177 }
178
179 foreach ($attributes as $attr => $value) :
180 list($ns, $local) = $this->split_ns($attr);
181 if ($ns=='http://www.w3.org/2000/xmlns/') :
182 if ('xmlns' == $local) : $local = false; endif;
183 $this->start_xmlns(null, $local, $value);
184 endif;
185 endforeach;
186
187 $this->tag_open(null, $tagName, $attributes);
188 if ($empty)
189 {
190 $this->tag_close(null, $tagName);
191 }
192 break;
193 case constant('XMLReader::TEXT'):
194
195 case constant('XMLReader::CDATA'):
196 $this->cdata(null, $xml->value);
197 break;
198 }
199 }
200 if ($error = libxml_get_last_error())
201 {
202 $this->error_code = $error->code;
203 $this->error_string = $error->message;
204 $this->current_line = $error->line;
205 $this->current_column = $error->column;
206 return false;
207 }
208 else
209 {
210 return true;
211 }
212 }
213 } /* FeedWordPress_Parser::parse() */
214
215 var $xmlns_stack = array();
216 var $xmlns_current = array();
217 function tag_open ($parser, $tag, $attributes) {
218 $ret = parent::tag_open($parser, $tag, $attributes);
219 if ($this->current_xhtml_construct < 0) :
220 $this->data['xmlns'] = $this->xmlns_current;
221 $this->xmlns_stack[] = $this->xmlns_current;
222 endif;
223 return $ret;
224 }
225
226 function tag_close($parser, $tag) {
227 if ($this->current_xhtml_construct < 0) :
228 $this->xmlns_current = array_pop($this->xmlns_stack);
229 endif;
230 $ret = parent::tag_close($parser, $tag);
231 return $ret;
232 }
233
234 function start_xmlns ($parser, $prefix, $uri) {
235 if (!$prefix) :
236 $prefix = '';
237 endif;
238 if ($this->current_xhtml_construct < 0) :
239 $this->xmlns_current[$prefix] = $uri;
240 endif;
241 return true;
242 } /* FeedWordPress_Parser::start_xmlns() */
243 }
244
245