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