feedparser.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. class FeedParser {
  3. private $doc;
  4. private $error;
  5. private $libxml_errors = array();
  6. private $items;
  7. private $link;
  8. private $title;
  9. private $type;
  10. private $xpath;
  11. const FEED_RDF = 0;
  12. const FEED_RSS = 1;
  13. const FEED_ATOM = 2;
  14. function normalize_encoding($data) {
  15. if (preg_match('/^(<\?xml[\t\n\r ].*?encoding[\t\n\r ]*=[\t\n\r ]*["\'])(.+?)(["\'].*?\?>)/s', $data, $matches) === 1) {
  16. $data = mb_convert_encoding($data, 'UTF-8', $matches[2]);
  17. $data = preg_replace('/^<\?xml[\t\n\r ].*?\?>/s', $matches[1] . "UTF-8" . $matches[3] , $data);
  18. }
  19. return $data;
  20. }
  21. function __construct($data) {
  22. libxml_use_internal_errors(true);
  23. libxml_clear_errors();
  24. $this->doc = new DOMDocument();
  25. $this->doc->loadXML($data);
  26. mb_substitute_character("none");
  27. $error = libxml_get_last_error();
  28. // libxml compiled without iconv?
  29. if ($error && $error->code == 32) {
  30. $data = $this->normalize_encoding($data);
  31. if ($data) {
  32. libxml_clear_errors();
  33. $this->doc = new DOMDocument();
  34. $this->doc->loadXML($data);
  35. $error = libxml_get_last_error();
  36. }
  37. }
  38. // some terrible invalid unicode entity?
  39. if ($error) {
  40. foreach (libxml_get_errors() as $err) {
  41. if ($err->code == 9) {
  42. // if the source feed is not in utf8, next conversion will fail
  43. $data = $this->normalize_encoding($data);
  44. // remove dangling bytes
  45. $data = mb_convert_encoding($data, 'UTF-8', 'UTF-8');
  46. // apparently not all UTF-8 characters are valid for XML
  47. $data = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $data);
  48. if ($data) {
  49. libxml_clear_errors();
  50. $this->doc = new DOMDocument();
  51. $this->doc->loadXML($data);
  52. $error = libxml_get_last_error();
  53. }
  54. break;
  55. }
  56. }
  57. }
  58. if ($error) {
  59. foreach (libxml_get_errors() as $error) {
  60. if ($error->level == LIBXML_ERR_FATAL) {
  61. if(!isset($this->error)) //currently only the first error is reported
  62. $this->error = $this->format_error($error);
  63. $this->libxml_errors [] = $this->format_error($error);
  64. }
  65. }
  66. }
  67. libxml_clear_errors();
  68. $this->items = array();
  69. }
  70. function init() {
  71. $root = $this->doc->firstChild;
  72. $xpath = new DOMXPath($this->doc);
  73. $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
  74. $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#');
  75. $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
  76. $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
  77. $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
  78. $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
  79. $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
  80. $this->xpath = $xpath;
  81. $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
  82. if ($root && $root->length > 0) {
  83. $root = $root->item(0);
  84. if ($root) {
  85. switch (mb_strtolower($root->tagName)) {
  86. case "rdf:rdf":
  87. $this->type = $this::FEED_RDF;
  88. break;
  89. case "channel":
  90. $this->type = $this::FEED_RSS;
  91. break;
  92. case "feed":
  93. $this->type = $this::FEED_ATOM;
  94. break;
  95. default:
  96. if( !isset($this->error) ){
  97. $this->error = "Unknown/unsupported feed type";
  98. }
  99. return;
  100. }
  101. }
  102. switch ($this->type) {
  103. case $this::FEED_ATOM:
  104. $title = $xpath->query("//atom:feed/atom:title")->item(0);
  105. if (!$title)
  106. $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
  107. if ($title) {
  108. $this->title = $title->nodeValue;
  109. }
  110. $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
  111. if (!$link)
  112. $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
  113. if ($link && $link->hasAttributes()) {
  114. $this->link = $link->getAttribute("href");
  115. }
  116. $articles = $xpath->query("//atom:entry");
  117. if (!$articles || $articles->length == 0)
  118. $articles = $xpath->query("//atom03:entry");
  119. foreach ($articles as $article) {
  120. array_push($this->items, new FeedItem_Atom($article, $this->doc, $this->xpath));
  121. }
  122. break;
  123. case $this::FEED_RSS:
  124. $title = $xpath->query("//channel/title")->item(0);
  125. if ($title) {
  126. $this->title = $title->nodeValue;
  127. }
  128. $link = $xpath->query("//channel/link")->item(0);
  129. if ($link) {
  130. if ($link->getAttribute("href"))
  131. $this->link = $link->getAttribute("href");
  132. else if ($link->nodeValue)
  133. $this->link = $link->nodeValue;
  134. }
  135. $articles = $xpath->query("//channel/item");
  136. foreach ($articles as $article) {
  137. array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
  138. }
  139. break;
  140. case $this::FEED_RDF:
  141. $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
  142. $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
  143. if ($title) {
  144. $this->title = $title->nodeValue;
  145. }
  146. $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
  147. if ($link) {
  148. $this->link = $link->nodeValue;
  149. }
  150. $articles = $xpath->query("//rssfake:item");
  151. foreach ($articles as $article) {
  152. array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
  153. }
  154. break;
  155. }
  156. } else {
  157. if( !isset($this->error) ){
  158. $this->error = "Unknown/unsupported feed type";
  159. }
  160. return;
  161. }
  162. }
  163. function format_error($error) {
  164. if ($error) {
  165. return sprintf("LibXML error %s at line %d (column %d): %s",
  166. $error->code, $error->line, $error->column,
  167. $error->message);
  168. } else {
  169. return "";
  170. }
  171. }
  172. function error() {
  173. return $this->error;
  174. }
  175. function errors() {
  176. return $this->libxml_errors;
  177. }
  178. function get_link() {
  179. return $this->link;
  180. }
  181. function get_title() {
  182. return $this->title;
  183. }
  184. function get_items() {
  185. return $this->items;
  186. }
  187. function get_links($rel) {
  188. $rv = array();
  189. switch ($this->type) {
  190. case $this::FEED_ATOM:
  191. $links = $this->xpath->query("//atom:feed/atom:link");
  192. foreach ($links as $link) {
  193. if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
  194. array_push($rv, $link->getAttribute('href'));
  195. }
  196. }
  197. break;
  198. case $this::FEED_RSS:
  199. $links = $this->xpath->query("//atom:link");
  200. foreach ($links as $link) {
  201. if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
  202. array_push($rv, $link->getAttribute('href'));
  203. }
  204. }
  205. break;
  206. }
  207. return $rv;
  208. }
  209. } ?>