feedparser.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. $xpath->registerNamespace('thread', 'http://purl.org/syndication/thread/1.0');
  81. $this->xpath = $xpath;
  82. $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)");
  83. if ($root && $root->length > 0) {
  84. $root = $root->item(0);
  85. if ($root) {
  86. switch (mb_strtolower($root->tagName)) {
  87. case "rdf:rdf":
  88. $this->type = $this::FEED_RDF;
  89. break;
  90. case "channel":
  91. $this->type = $this::FEED_RSS;
  92. break;
  93. case "feed":
  94. case "atom:feed":
  95. $this->type = $this::FEED_ATOM;
  96. break;
  97. default:
  98. if( !isset($this->error) ){
  99. $this->error = "Unknown/unsupported feed type";
  100. }
  101. return;
  102. }
  103. }
  104. switch ($this->type) {
  105. case $this::FEED_ATOM:
  106. $title = $xpath->query("//atom:feed/atom:title")->item(0);
  107. if (!$title)
  108. $title = $xpath->query("//atom03:feed/atom03:title")->item(0);
  109. if ($title) {
  110. $this->title = $title->nodeValue;
  111. }
  112. $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0);
  113. if (!$link)
  114. $link = $xpath->query("//atom:feed/atom:link[@rel='alternate']")->item(0);
  115. if (!$link)
  116. $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0);
  117. if (!$link)
  118. $link = $xpath->query("//atom03:feed/atom03:link[@rel='alternate']")->item(0);
  119. if ($link && $link->hasAttributes()) {
  120. $this->link = $link->getAttribute("href");
  121. }
  122. $articles = $xpath->query("//atom:entry");
  123. if (!$articles || $articles->length == 0)
  124. $articles = $xpath->query("//atom03:entry");
  125. foreach ($articles as $article) {
  126. array_push($this->items, new FeedItem_Atom($article, $this->doc, $this->xpath));
  127. }
  128. break;
  129. case $this::FEED_RSS:
  130. $title = $xpath->query("//channel/title")->item(0);
  131. if ($title) {
  132. $this->title = $title->nodeValue;
  133. }
  134. $link = $xpath->query("//channel/link")->item(0);
  135. if ($link) {
  136. if ($link->getAttribute("href"))
  137. $this->link = $link->getAttribute("href");
  138. else if ($link->nodeValue)
  139. $this->link = $link->nodeValue;
  140. }
  141. $articles = $xpath->query("//channel/item");
  142. foreach ($articles as $article) {
  143. array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
  144. }
  145. break;
  146. case $this::FEED_RDF:
  147. $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/');
  148. $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0);
  149. if ($title) {
  150. $this->title = $title->nodeValue;
  151. }
  152. $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0);
  153. if ($link) {
  154. $this->link = $link->nodeValue;
  155. }
  156. $articles = $xpath->query("//rssfake:item");
  157. foreach ($articles as $article) {
  158. array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath));
  159. }
  160. break;
  161. }
  162. if ($this->title) $this->title = trim($this->title);
  163. if ($this->link) $this->link = trim($this->link);
  164. } else {
  165. if( !isset($this->error) ){
  166. $this->error = "Unknown/unsupported feed type";
  167. }
  168. return;
  169. }
  170. }
  171. function format_error($error) {
  172. if ($error) {
  173. return sprintf("LibXML error %s at line %d (column %d): %s",
  174. $error->code, $error->line, $error->column,
  175. $error->message);
  176. } else {
  177. return "";
  178. }
  179. }
  180. function error() {
  181. return $this->error;
  182. }
  183. function errors() {
  184. return $this->libxml_errors;
  185. }
  186. function get_link() {
  187. return $this->link;
  188. }
  189. function get_title() {
  190. return $this->title;
  191. }
  192. function get_items() {
  193. return $this->items;
  194. }
  195. function get_links($rel) {
  196. $rv = array();
  197. switch ($this->type) {
  198. case $this::FEED_ATOM:
  199. $links = $this->xpath->query("//atom:feed/atom:link");
  200. foreach ($links as $link) {
  201. if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
  202. array_push($rv, trim($link->getAttribute('href')));
  203. }
  204. }
  205. break;
  206. case $this::FEED_RSS:
  207. $links = $this->xpath->query("//atom:link");
  208. foreach ($links as $link) {
  209. if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
  210. array_push($rv, trim($link->getAttribute('href')));
  211. }
  212. }
  213. break;
  214. }
  215. return $rv;
  216. }
  217. } ?>