PluginProbe
FeedWordPress / 2012.1212
FeedWordPress v2012.1212
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 2012.1212, at feedwordpress_parser.class.php

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