feedparser.php 6.2 KB

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