5 private $libxml_errors = array();
16 function normalize_encoding($data) {
17 if (preg_match('/^(<\?xml[\t\n\r ].*?encoding[\t\n\r ]*=[\t\n\r ]*["\'])(.+?)(["\'].*?\?>)/s', $data, $matches) === 1) {
18 $data = mb_convert_encoding($data, 'UTF-8', $matches[2]);
20 $data = preg_replace('/^<\?xml[\t\n\r ].*?\?>/s', $matches[1] . "UTF-8" . $matches[3] , $data);
26 function __construct($data) {
27 libxml_use_internal_errors(true);
28 libxml_clear_errors();
29 $this->doc = new DOMDocument();
30 $this->doc->loadXML($data);
32 mb_substitute_character("none");
34 $error = libxml_get_last_error();
36 // libxml compiled without iconv?
37 if ($error && $error->code == 32) {
38 $data = $this->normalize_encoding($data);
41 libxml_clear_errors();
43 $this->doc = new DOMDocument();
44 $this->doc->loadXML($data);
46 $error = libxml_get_last_error();
50 // some terrible invalid unicode entity?
52 foreach (libxml_get_errors() as $err) {
53 if ($err->code == 9) {
54 // if the source feed is not in utf8, next conversion will fail
55 $data = $this->normalize_encoding($data);
57 // remove dangling bytes
58 $data = mb_convert_encoding($data, 'UTF-8', 'UTF-8');
60 // apparently not all UTF-8 characters are valid for XML
61 $data = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $data);
64 libxml_clear_errors();
66 $this->doc = new DOMDocument();
67 $this->doc->loadXML($data);
69 $error = libxml_get_last_error();
77 foreach (libxml_get_errors() as $error) {
78 if ($error->level == LIBXML_ERR_FATAL) {
79 if(!isset($this->error)) //currently only the first error is reported
80 $this->error = $this->format_error($error);
81 $this->libxml_errors [] = $this->format_error($error);
85 libxml_clear_errors();
87 $this->items = array();
91 $root = $this->doc->firstChild;
92 $xpath = new DOMXPath($this->doc);
93 $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
94 $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
95 $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
96 $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
97 $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
98 $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
99 $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
100 $xpath->registerNamespace('thread', 'http://purl.org/syndication/thread/1.0');
102 $this->xpath = $xpath;
104 $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
106 if ($root && $root->length > 0) {
107 $root = $root->item(0);
110 switch (mb_strtolower($root->tagName)) {
112 $this->type = $this::FEED_RDF;
115 $this->type = $this::FEED_RSS;
118 $this->type = $this::FEED_ATOM;
121 if( !isset($this->error) ){
122 $this->error = "Unknown/unsupported feed type";
128 switch ($this->type) {
129 case $this::FEED_ATOM:
131 $title = $xpath->query("//atom:feed/atom:title")->item(0);
134 $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
138 $this->title = $title->nodeValue;
141 $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
144 $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
147 if ($link && $link->hasAttributes()) {
148 $this->link = $link->getAttribute("href");
151 $articles = $xpath->query("//atom:entry");
153 if (!$articles || $articles->length == 0)
154 $articles = $xpath->query("//atom03:entry");
156 foreach ($articles as $article) {
157 array_push($this->items, new FeedItem_Atom($article, $this->doc, $this->xpath));
161 case $this::FEED_RSS:
162 $title = $xpath->query("//channel/title")->item(0);
165 $this->title = $title->nodeValue;
168 $link = $xpath->query("//channel/link")->item(0);
171 if ($link->getAttribute("href"))
172 $this->link = $link->getAttribute("href");
173 else if ($link->nodeValue)
174 $this->link = $link->nodeValue;
177 $articles = $xpath->query("//channel/item");
179 foreach ($articles as $article) {
180 array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
184 case $this::FEED_RDF:
185 $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
187 $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
190 $this->title = $title->nodeValue;
193 $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
196 $this->link = $link->nodeValue;
199 $articles = $xpath->query("//rssfake:item");
201 foreach ($articles as $article) {
202 array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
209 if ($this->title) $this->title = trim($this->title);
210 if ($this->link) $this->link = trim($this->link);
213 if( !isset($this->error) ){
214 $this->error = "Unknown/unsupported feed type";
220 function format_error($error) {
222 return sprintf("LibXML error %s at line %d (column %d): %s",
223 $error->code, $error->line, $error->column,
235 return $this->libxml_errors;
238 function get_link() {
242 function get_title() {
246 function get_items() {
250 function get_links($rel) {
253 switch ($this->type) {
254 case $this::FEED_ATOM:
255 $links = $this->xpath->query("//atom:feed/atom:link");
257 foreach ($links as $link) {
258 if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
259 array_push($rv, trim($link->getAttribute('href')));
263 case $this::FEED_RSS:
264 $links = $this->xpath->query("//atom:link");
266 foreach ($links as $link) {
267 if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
268 array_push($rv, trim($link->getAttribute('href')));