]>
Commit | Line | Data |
---|---|---|
cd07592c AD |
1 | <?php |
2 | class FeedParser { | |
3 | private $doc; | |
4 | private $error; | |
54f23d38 | 5 | private $libxml_errors = array(); |
cd07592c AD |
6 | private $items; |
7 | private $link; | |
8 | private $title; | |
9 | private $type; | |
b9eee80e | 10 | private $xpath; |
cd07592c AD |
11 | |
12 | const FEED_RDF = 0; | |
13 | const FEED_RSS = 1; | |
14 | const FEED_ATOM = 2; | |
15 | ||
4ad04ee2 AD |
16 | function normalize_encoding($data) { |
17 | if (preg_match('/^(<\?xml[\t\n\r ].*?encoding[\t\n\r ]*=[\t\n\r ]*["\'])(.+?)(["\'].*?\?>)/s', $data, $matches) === 1) { | |
cde82722 AD |
18 | |
19 | $encoding = strtolower($matches[2]); | |
20 | ||
e4071d25 | 21 | if (in_array($encoding, array_map('strtolower', mb_list_encodings()))) |
cde82722 | 22 | $data = mb_convert_encoding($data, 'UTF-8', $encoding); |
4ad04ee2 AD |
23 | |
24 | $data = preg_replace('/^<\?xml[\t\n\r ].*?\?>/s', $matches[1] . "UTF-8" . $matches[3] , $data); | |
25 | } | |
26 | ||
27 | return $data; | |
28 | } | |
29 | ||
cd07592c AD |
30 | function __construct($data) { |
31 | libxml_use_internal_errors(true); | |
32 | libxml_clear_errors(); | |
33 | $this->doc = new DOMDocument(); | |
34 | $this->doc->loadXML($data); | |
d1f3fa97 | 35 | |
f612dbe8 AD |
36 | mb_substitute_character("none"); |
37 | ||
d1f3fa97 AD |
38 | $error = libxml_get_last_error(); |
39 | ||
4f00f55c | 40 | // libxml compiled without iconv? |
5fa36a64 | 41 | if ($error && $error->code == 32) { |
4ad04ee2 | 42 | $data = $this->normalize_encoding($data); |
4f00f55c | 43 | |
4ad04ee2 AD |
44 | if ($data) { |
45 | libxml_clear_errors(); | |
f612dbe8 | 46 | |
4ad04ee2 AD |
47 | $this->doc = new DOMDocument(); |
48 | $this->doc->loadXML($data); | |
f612dbe8 | 49 | |
4ad04ee2 | 50 | $error = libxml_get_last_error(); |
5fa36a64 | 51 | } |
4f00f55c AD |
52 | } |
53 | ||
54 | // some terrible invalid unicode entity? | |
5fa36a64 | 55 | if ($error) { |
4d49863f | 56 | foreach (libxml_get_errors() as $err) { |
5fa36a64 | 57 | if ($err->code == 9) { |
4ad04ee2 AD |
58 | // if the source feed is not in utf8, next conversion will fail |
59 | $data = $this->normalize_encoding($data); | |
60 | ||
5fa36a64 | 61 | // remove dangling bytes |
62 | $data = mb_convert_encoding($data, 'UTF-8', 'UTF-8'); | |
4d49863f | 63 | |
5fa36a64 | 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 | } | |
f612dbe8 | 77 | } |
d1f3fa97 | 78 | } |
4d49863f | 79 | |
4d49863f AD |
80 | if ($error) { |
81 | foreach (libxml_get_errors() as $error) { | |
82 | if ($error->level == LIBXML_ERR_FATAL) { | |
54f23d38 | 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); | |
d3305ff8 | 86 | } |
4d49863f | 87 | } |
d3305ff8 | 88 | } |
cd07592c AD |
89 | libxml_clear_errors(); |
90 | ||
91 | $this->items = array(); | |
92 | } | |
93 | ||
94 | function init() { | |
95 | $root = $this->doc->firstChild; | |
04d2f9c8 AD |
96 | $xpath = new DOMXPath($this->doc); |
97 | $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom'); | |
a3b9fd12 | 98 | $xpath->registerNamespace('atom03', 'http://purl.org/atom/ns#'); |
4c00e15b | 99 | $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/'); |
852d4ac8 | 100 | $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'); |
d4992d6b AD |
101 | $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/'); |
102 | $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/'); | |
8a95d630 | 103 | $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/'); |
d71ac5d3 | 104 | $xpath->registerNamespace('thread', 'http://purl.org/syndication/thread/1.0'); |
d4992d6b | 105 | |
b9eee80e | 106 | $this->xpath = $xpath; |
04d2f9c8 | 107 | |
a9000b03 | 108 | $root = $xpath->query("(//atom03:feed|//atom:feed|//channel|//rdf:rdf|//rdf:RDF)"); |
cd07592c | 109 | |
2c6f3c2e | 110 | if ($root && $root->length > 0) { |
a9000b03 AD |
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": | |
4834ad56 | 122 | case "atom:feed": |
a9000b03 AD |
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; | |
349c4229 | 130 | } |
cd07592c AD |
131 | } |
132 | ||
cd07592c AD |
133 | switch ($this->type) { |
134 | case $this::FEED_ATOM: | |
cd07592c AD |
135 | |
136 | $title = $xpath->query("//atom:feed/atom:title")->item(0); | |
137 | ||
a3b9fd12 AD |
138 | if (!$title) |
139 | $title = $xpath->query("//atom03:feed/atom03:title")->item(0); | |
140 | ||
141 | ||
cd07592c AD |
142 | if ($title) { |
143 | $this->title = $title->nodeValue; | |
144 | } | |
145 | ||
146 | $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0); | |
147 | ||
e55a5ec6 AD |
148 | if (!$link) |
149 | $link = $xpath->query("//atom:feed/atom:link[@rel='alternate']")->item(0); | |
150 | ||
a3b9fd12 AD |
151 | if (!$link) |
152 | $link = $xpath->query("//atom03:feed/atom03:link[not(@rel)]")->item(0); | |
153 | ||
e55a5ec6 AD |
154 | if (!$link) |
155 | $link = $xpath->query("//atom03:feed/atom03:link[@rel='alternate']")->item(0); | |
a3b9fd12 | 156 | |
cd07592c AD |
157 | if ($link && $link->hasAttributes()) { |
158 | $this->link = $link->getAttribute("href"); | |
159 | } | |
160 | ||
161 | $articles = $xpath->query("//atom:entry"); | |
162 | ||
a3b9fd12 AD |
163 | if (!$articles || $articles->length == 0) |
164 | $articles = $xpath->query("//atom03:entry"); | |
165 | ||
cd07592c | 166 | foreach ($articles as $article) { |
4c00e15b | 167 | array_push($this->items, new FeedItem_Atom($article, $this->doc, $this->xpath)); |
cd07592c AD |
168 | } |
169 | ||
cd07592c AD |
170 | break; |
171 | case $this::FEED_RSS: | |
04d2f9c8 AD |
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 | ||
1874c8d6 AD |
180 | if ($link) { |
181 | if ($link->getAttribute("href")) | |
182 | $this->link = $link->getAttribute("href"); | |
183 | else if ($link->nodeValue) | |
184 | $this->link = $link->nodeValue; | |
04d2f9c8 AD |
185 | } |
186 | ||
187 | $articles = $xpath->query("//channel/item"); | |
188 | ||
189 | foreach ($articles as $article) { | |
4c00e15b | 190 | array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath)); |
04d2f9c8 AD |
191 | } |
192 | ||
cd07592c | 193 | break; |
852d4ac8 AD |
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 | ||
cd07592c | 217 | } |
31bd6f76 AD |
218 | |
219 | if ($this->title) $this->title = trim($this->title); | |
220 | if ($this->link) $this->link = trim($this->link); | |
221 | ||
852d4ac8 | 222 | } else { |
349c4229 | 223 | if( !isset($this->error) ){ |
224 | $this->error = "Unknown/unsupported feed type"; | |
225 | } | |
852d4ac8 | 226 | return; |
cd07592c AD |
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 | ||
54f23d38 | 244 | function errors() { |
245 | return $this->libxml_errors; | |
246 | } | |
247 | ||
cd07592c AD |
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 | ||
b9eee80e AD |
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) { | |
31bd6f76 | 269 | array_push($rv, trim($link->getAttribute('href'))); |
b9eee80e AD |
270 | } |
271 | } | |
272 | break; | |
273 | case $this::FEED_RSS: | |
f17c3ee2 AD |
274 | $links = $this->xpath->query("//atom:link"); |
275 | ||
3c8060ac AD |
276 | foreach ($links as $link) { |
277 | if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) { | |
31bd6f76 | 278 | array_push($rv, trim($link->getAttribute('href'))); |
3c8060ac | 279 | } |
b9eee80e AD |
280 | } |
281 | break; | |
282 | } | |
283 | ||
284 | return $rv; | |
285 | } | |
22a866ed | 286 | } |