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