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