Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Zend Framework (http://framework.zend.com/)
|
Chris@0
|
4 *
|
Chris@0
|
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
|
Chris@0
|
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
Chris@0
|
7 * @license http://framework.zend.com/license/new-bsd New BSD License
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 namespace Zend\Feed\Reader;
|
Chris@0
|
11
|
Chris@0
|
12 use DOMDocument;
|
Chris@0
|
13 use DOMXPath;
|
Chris@0
|
14 use Zend\Cache\Storage\StorageInterface as CacheStorage;
|
Chris@0
|
15 use Zend\Http as ZendHttp;
|
Chris@0
|
16 use Zend\Stdlib\ErrorHandler;
|
Chris@0
|
17 use Zend\Feed\Reader\Exception\InvalidHttpClientException;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 */
|
Chris@0
|
21 class Reader implements ReaderImportInterface
|
Chris@0
|
22 {
|
Chris@0
|
23 /**
|
Chris@0
|
24 * Namespace constants
|
Chris@0
|
25 */
|
Chris@0
|
26 const NAMESPACE_ATOM_03 = 'http://purl.org/atom/ns#';
|
Chris@0
|
27 const NAMESPACE_ATOM_10 = 'http://www.w3.org/2005/Atom';
|
Chris@0
|
28 const NAMESPACE_RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
|
Chris@0
|
29 const NAMESPACE_RSS_090 = 'http://my.netscape.com/rdf/simple/0.9/';
|
Chris@0
|
30 const NAMESPACE_RSS_10 = 'http://purl.org/rss/1.0/';
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Feed type constants
|
Chris@0
|
34 */
|
Chris@0
|
35 const TYPE_ANY = 'any';
|
Chris@0
|
36 const TYPE_ATOM_03 = 'atom-03';
|
Chris@0
|
37 const TYPE_ATOM_10 = 'atom-10';
|
Chris@0
|
38 const TYPE_ATOM_10_ENTRY = 'atom-10-entry';
|
Chris@0
|
39 const TYPE_ATOM_ANY = 'atom';
|
Chris@0
|
40 const TYPE_RSS_090 = 'rss-090';
|
Chris@0
|
41 const TYPE_RSS_091 = 'rss-091';
|
Chris@0
|
42 const TYPE_RSS_091_NETSCAPE = 'rss-091n';
|
Chris@0
|
43 const TYPE_RSS_091_USERLAND = 'rss-091u';
|
Chris@0
|
44 const TYPE_RSS_092 = 'rss-092';
|
Chris@0
|
45 const TYPE_RSS_093 = 'rss-093';
|
Chris@0
|
46 const TYPE_RSS_094 = 'rss-094';
|
Chris@0
|
47 const TYPE_RSS_10 = 'rss-10';
|
Chris@0
|
48 const TYPE_RSS_20 = 'rss-20';
|
Chris@0
|
49 const TYPE_RSS_ANY = 'rss';
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * Cache instance
|
Chris@0
|
53 *
|
Chris@0
|
54 * @var CacheStorage
|
Chris@0
|
55 */
|
Chris@0
|
56 protected static $cache = null;
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * HTTP client object to use for retrieving feeds
|
Chris@0
|
60 *
|
Chris@0
|
61 * @var Http\ClientInterface
|
Chris@0
|
62 */
|
Chris@0
|
63 protected static $httpClient = null;
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * Override HTTP PUT and DELETE request methods?
|
Chris@0
|
67 *
|
Chris@0
|
68 * @var bool
|
Chris@0
|
69 */
|
Chris@0
|
70 protected static $httpMethodOverride = false;
|
Chris@0
|
71
|
Chris@0
|
72 protected static $httpConditionalGet = false;
|
Chris@0
|
73
|
Chris@0
|
74 protected static $extensionManager = null;
|
Chris@0
|
75
|
Chris@0
|
76 protected static $extensions = [
|
Chris@0
|
77 'feed' => [
|
Chris@0
|
78 'DublinCore\Feed',
|
Chris@0
|
79 'Atom\Feed'
|
Chris@0
|
80 ],
|
Chris@0
|
81 'entry' => [
|
Chris@0
|
82 'Content\Entry',
|
Chris@0
|
83 'DublinCore\Entry',
|
Chris@0
|
84 'Atom\Entry'
|
Chris@0
|
85 ],
|
Chris@0
|
86 'core' => [
|
Chris@0
|
87 'DublinCore\Feed',
|
Chris@0
|
88 'Atom\Feed',
|
Chris@0
|
89 'Content\Entry',
|
Chris@0
|
90 'DublinCore\Entry',
|
Chris@0
|
91 'Atom\Entry'
|
Chris@0
|
92 ]
|
Chris@0
|
93 ];
|
Chris@0
|
94
|
Chris@0
|
95 /**
|
Chris@0
|
96 * Get the Feed cache
|
Chris@0
|
97 *
|
Chris@0
|
98 * @return CacheStorage
|
Chris@0
|
99 */
|
Chris@0
|
100 public static function getCache()
|
Chris@0
|
101 {
|
Chris@0
|
102 return static::$cache;
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 /**
|
Chris@0
|
106 * Set the feed cache
|
Chris@0
|
107 *
|
Chris@0
|
108 * @param CacheStorage $cache
|
Chris@0
|
109 * @return void
|
Chris@0
|
110 */
|
Chris@0
|
111 public static function setCache(CacheStorage $cache)
|
Chris@0
|
112 {
|
Chris@0
|
113 static::$cache = $cache;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * Set the HTTP client instance
|
Chris@0
|
118 *
|
Chris@0
|
119 * Sets the HTTP client object to use for retrieving the feeds.
|
Chris@0
|
120 *
|
Chris@0
|
121 * @param ZendHttp\Client | Http\ClientInterface $httpClient
|
Chris@0
|
122 * @return void
|
Chris@0
|
123 */
|
Chris@0
|
124 public static function setHttpClient($httpClient)
|
Chris@0
|
125 {
|
Chris@0
|
126 if ($httpClient instanceof ZendHttp\Client) {
|
Chris@0
|
127 $httpClient = new Http\ZendHttpClientDecorator($httpClient);
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 if (! $httpClient instanceof Http\ClientInterface) {
|
Chris@0
|
131 throw new InvalidHttpClientException();
|
Chris@0
|
132 }
|
Chris@0
|
133 static::$httpClient = $httpClient;
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 /**
|
Chris@0
|
137 * Gets the HTTP client object. If none is set, a new ZendHttp\Client will be used.
|
Chris@0
|
138 *
|
Chris@0
|
139 * @return Http\ClientInterface
|
Chris@0
|
140 */
|
Chris@0
|
141 public static function getHttpClient()
|
Chris@0
|
142 {
|
Chris@0
|
143 if (! static::$httpClient) {
|
Chris@0
|
144 static::$httpClient = new Http\ZendHttpClientDecorator(new ZendHttp\Client());
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 return static::$httpClient;
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150 /**
|
Chris@0
|
151 * Toggle using POST instead of PUT and DELETE HTTP methods
|
Chris@0
|
152 *
|
Chris@0
|
153 * Some feed implementations do not accept PUT and DELETE HTTP
|
Chris@0
|
154 * methods, or they can't be used because of proxies or other
|
Chris@0
|
155 * measures. This allows turning on using POST where PUT and
|
Chris@0
|
156 * DELETE would normally be used; in addition, an
|
Chris@0
|
157 * X-Method-Override header will be sent with a value of PUT or
|
Chris@0
|
158 * DELETE as appropriate.
|
Chris@0
|
159 *
|
Chris@0
|
160 * @param bool $override Whether to override PUT and DELETE.
|
Chris@0
|
161 * @return void
|
Chris@0
|
162 */
|
Chris@0
|
163 public static function setHttpMethodOverride($override = true)
|
Chris@0
|
164 {
|
Chris@0
|
165 static::$httpMethodOverride = $override;
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 /**
|
Chris@0
|
169 * Get the HTTP override state
|
Chris@0
|
170 *
|
Chris@0
|
171 * @return bool
|
Chris@0
|
172 */
|
Chris@0
|
173 public static function getHttpMethodOverride()
|
Chris@0
|
174 {
|
Chris@0
|
175 return static::$httpMethodOverride;
|
Chris@0
|
176 }
|
Chris@0
|
177
|
Chris@0
|
178 /**
|
Chris@0
|
179 * Set the flag indicating whether or not to use HTTP conditional GET
|
Chris@0
|
180 *
|
Chris@0
|
181 * @param bool $bool
|
Chris@0
|
182 * @return void
|
Chris@0
|
183 */
|
Chris@0
|
184 public static function useHttpConditionalGet($bool = true)
|
Chris@0
|
185 {
|
Chris@0
|
186 static::$httpConditionalGet = $bool;
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@0
|
189 /**
|
Chris@0
|
190 * Import a feed by providing a URI
|
Chris@0
|
191 *
|
Chris@0
|
192 * @param string $uri The URI to the feed
|
Chris@0
|
193 * @param string $etag OPTIONAL Last received ETag for this resource
|
Chris@0
|
194 * @param string $lastModified OPTIONAL Last-Modified value for this resource
|
Chris@0
|
195 * @return Feed\FeedInterface
|
Chris@0
|
196 * @throws Exception\RuntimeException
|
Chris@0
|
197 */
|
Chris@0
|
198 public static function import($uri, $etag = null, $lastModified = null)
|
Chris@0
|
199 {
|
Chris@0
|
200 $cache = self::getCache();
|
Chris@0
|
201 $client = self::getHttpClient();
|
Chris@0
|
202 $cacheId = 'Zend_Feed_Reader_' . md5($uri);
|
Chris@0
|
203
|
Chris@0
|
204 if (static::$httpConditionalGet && $cache) {
|
Chris@0
|
205 $headers = [];
|
Chris@0
|
206 $data = $cache->getItem($cacheId);
|
Chris@0
|
207 if ($data && $client instanceof Http\HeaderAwareClientInterface) {
|
Chris@0
|
208 // Only check for ETag and last modified values in the cache
|
Chris@0
|
209 // if we have a client capable of emitting headers in the first place.
|
Chris@0
|
210 if ($etag === null) {
|
Chris@0
|
211 $etag = $cache->getItem($cacheId . '_etag');
|
Chris@0
|
212 }
|
Chris@0
|
213 if ($lastModified === null) {
|
Chris@0
|
214 $lastModified = $cache->getItem($cacheId . '_lastmodified');
|
Chris@0
|
215 }
|
Chris@0
|
216 if ($etag) {
|
Chris@0
|
217 $headers['If-None-Match'] = [$etag];
|
Chris@0
|
218 }
|
Chris@0
|
219 if ($lastModified) {
|
Chris@0
|
220 $headers['If-Modified-Since'] = [$lastModified];
|
Chris@0
|
221 }
|
Chris@0
|
222 }
|
Chris@0
|
223 $response = $client->get($uri, $headers);
|
Chris@0
|
224 if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 304) {
|
Chris@0
|
225 throw new Exception\RuntimeException('Feed failed to load, got response code ' . $response->getStatusCode());
|
Chris@0
|
226 }
|
Chris@0
|
227 if ($response->getStatusCode() == 304) {
|
Chris@0
|
228 $responseXml = $data;
|
Chris@0
|
229 } else {
|
Chris@0
|
230 $responseXml = $response->getBody();
|
Chris@0
|
231 $cache->setItem($cacheId, $responseXml);
|
Chris@0
|
232
|
Chris@0
|
233 if ($response instanceof Http\HeaderAwareResponseInterface) {
|
Chris@0
|
234 if ($response->getHeaderLine('ETag', false)) {
|
Chris@0
|
235 $cache->setItem($cacheId . '_etag', $response->getHeaderLine('ETag'));
|
Chris@0
|
236 }
|
Chris@0
|
237 if ($response->getHeaderLine('Last-Modified', false)) {
|
Chris@0
|
238 $cache->setItem($cacheId . '_lastmodified', $response->getHeaderLine('Last-Modified'));
|
Chris@0
|
239 }
|
Chris@0
|
240 }
|
Chris@0
|
241 }
|
Chris@0
|
242 return static::importString($responseXml);
|
Chris@0
|
243 } elseif ($cache) {
|
Chris@0
|
244 $data = $cache->getItem($cacheId);
|
Chris@0
|
245 if ($data) {
|
Chris@0
|
246 return static::importString($data);
|
Chris@0
|
247 }
|
Chris@0
|
248 $response = $client->get($uri);
|
Chris@0
|
249 if ((int) $response->getStatusCode() !== 200) {
|
Chris@0
|
250 throw new Exception\RuntimeException('Feed failed to load, got response code ' . $response->getStatusCode());
|
Chris@0
|
251 }
|
Chris@0
|
252 $responseXml = $response->getBody();
|
Chris@0
|
253 $cache->setItem($cacheId, $responseXml);
|
Chris@0
|
254 return static::importString($responseXml);
|
Chris@0
|
255 } else {
|
Chris@0
|
256 $response = $client->get($uri);
|
Chris@0
|
257 if ((int) $response->getStatusCode() !== 200) {
|
Chris@0
|
258 throw new Exception\RuntimeException('Feed failed to load, got response code ' . $response->getStatusCode());
|
Chris@0
|
259 }
|
Chris@0
|
260 $reader = static::importString($response->getBody());
|
Chris@0
|
261 $reader->setOriginalSourceUri($uri);
|
Chris@0
|
262 return $reader;
|
Chris@0
|
263 }
|
Chris@0
|
264 }
|
Chris@0
|
265
|
Chris@0
|
266 /**
|
Chris@0
|
267 * Import a feed from a remote URI
|
Chris@0
|
268 *
|
Chris@0
|
269 * Performs similarly to import(), except it uses the HTTP client passed to
|
Chris@0
|
270 * the method, and does not take into account cached data.
|
Chris@0
|
271 *
|
Chris@0
|
272 * Primary purpose is to make it possible to use the Reader with alternate
|
Chris@0
|
273 * HTTP client implementations.
|
Chris@0
|
274 *
|
Chris@0
|
275 * @param string $uri
|
Chris@0
|
276 * @param Http\ClientInterface $client
|
Chris@0
|
277 * @return self
|
Chris@0
|
278 * @throws Exception\RuntimeException if response is not an Http\ResponseInterface
|
Chris@0
|
279 */
|
Chris@0
|
280 public static function importRemoteFeed($uri, Http\ClientInterface $client)
|
Chris@0
|
281 {
|
Chris@0
|
282 $response = $client->get($uri);
|
Chris@0
|
283 if (! $response instanceof Http\ResponseInterface) {
|
Chris@0
|
284 throw new Exception\RuntimeException(sprintf(
|
Chris@0
|
285 'Did not receive a %s\Http\ResponseInterface from the provided HTTP client; received "%s"',
|
Chris@0
|
286 __NAMESPACE__,
|
Chris@0
|
287 (is_object($response) ? get_class($response) : gettype($response))
|
Chris@0
|
288 ));
|
Chris@0
|
289 }
|
Chris@0
|
290
|
Chris@0
|
291 if ((int) $response->getStatusCode() !== 200) {
|
Chris@0
|
292 throw new Exception\RuntimeException('Feed failed to load, got response code ' . $response->getStatusCode());
|
Chris@0
|
293 }
|
Chris@0
|
294 $reader = static::importString($response->getBody());
|
Chris@0
|
295 $reader->setOriginalSourceUri($uri);
|
Chris@0
|
296 return $reader;
|
Chris@0
|
297 }
|
Chris@0
|
298
|
Chris@0
|
299 /**
|
Chris@0
|
300 * Import a feed from a string
|
Chris@0
|
301 *
|
Chris@0
|
302 * @param string $string
|
Chris@0
|
303 * @return Feed\FeedInterface
|
Chris@0
|
304 * @throws Exception\InvalidArgumentException
|
Chris@0
|
305 * @throws Exception\RuntimeException
|
Chris@0
|
306 */
|
Chris@0
|
307 public static function importString($string)
|
Chris@0
|
308 {
|
Chris@0
|
309 $trimmed = trim($string);
|
Chris@0
|
310 if (!is_string($string) || empty($trimmed)) {
|
Chris@0
|
311 throw new Exception\InvalidArgumentException('Only non empty strings are allowed as input');
|
Chris@0
|
312 }
|
Chris@0
|
313
|
Chris@0
|
314 $libxmlErrflag = libxml_use_internal_errors(true);
|
Chris@0
|
315 $oldValue = libxml_disable_entity_loader(true);
|
Chris@0
|
316 $dom = new DOMDocument;
|
Chris@0
|
317 $status = $dom->loadXML(trim($string));
|
Chris@0
|
318 foreach ($dom->childNodes as $child) {
|
Chris@0
|
319 if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
|
Chris@0
|
320 throw new Exception\InvalidArgumentException(
|
Chris@0
|
321 'Invalid XML: Detected use of illegal DOCTYPE'
|
Chris@0
|
322 );
|
Chris@0
|
323 }
|
Chris@0
|
324 }
|
Chris@0
|
325 libxml_disable_entity_loader($oldValue);
|
Chris@0
|
326 libxml_use_internal_errors($libxmlErrflag);
|
Chris@0
|
327
|
Chris@0
|
328 if (!$status) {
|
Chris@0
|
329 // Build error message
|
Chris@0
|
330 $error = libxml_get_last_error();
|
Chris@0
|
331 if ($error && $error->message) {
|
Chris@0
|
332 $error->message = trim($error->message);
|
Chris@0
|
333 $errormsg = "DOMDocument cannot parse XML: {$error->message}";
|
Chris@0
|
334 } else {
|
Chris@0
|
335 $errormsg = "DOMDocument cannot parse XML: Please check the XML document's validity";
|
Chris@0
|
336 }
|
Chris@0
|
337 throw new Exception\RuntimeException($errormsg);
|
Chris@0
|
338 }
|
Chris@0
|
339
|
Chris@0
|
340 $type = static::detectType($dom);
|
Chris@0
|
341
|
Chris@0
|
342 static::registerCoreExtensions();
|
Chris@0
|
343
|
Chris@0
|
344 if (substr($type, 0, 3) == 'rss') {
|
Chris@0
|
345 $reader = new Feed\Rss($dom, $type);
|
Chris@0
|
346 } elseif (substr($type, 8, 5) == 'entry') {
|
Chris@0
|
347 $reader = new Entry\Atom($dom->documentElement, 0, self::TYPE_ATOM_10);
|
Chris@0
|
348 } elseif (substr($type, 0, 4) == 'atom') {
|
Chris@0
|
349 $reader = new Feed\Atom($dom, $type);
|
Chris@0
|
350 } else {
|
Chris@0
|
351 throw new Exception\RuntimeException('The URI used does not point to a '
|
Chris@0
|
352 . 'valid Atom, RSS or RDF feed that Zend\Feed\Reader can parse.');
|
Chris@0
|
353 }
|
Chris@0
|
354 return $reader;
|
Chris@0
|
355 }
|
Chris@0
|
356
|
Chris@0
|
357 /**
|
Chris@0
|
358 * Imports a feed from a file located at $filename.
|
Chris@0
|
359 *
|
Chris@0
|
360 * @param string $filename
|
Chris@0
|
361 * @throws Exception\RuntimeException
|
Chris@0
|
362 * @return Feed\FeedInterface
|
Chris@0
|
363 */
|
Chris@0
|
364 public static function importFile($filename)
|
Chris@0
|
365 {
|
Chris@0
|
366 ErrorHandler::start();
|
Chris@0
|
367 $feed = file_get_contents($filename);
|
Chris@0
|
368 $err = ErrorHandler::stop();
|
Chris@0
|
369 if ($feed === false) {
|
Chris@0
|
370 throw new Exception\RuntimeException("File '{$filename}' could not be loaded", 0, $err);
|
Chris@0
|
371 }
|
Chris@0
|
372 return static::importString($feed);
|
Chris@0
|
373 }
|
Chris@0
|
374
|
Chris@0
|
375 /**
|
Chris@0
|
376 * Find feed links
|
Chris@0
|
377 *
|
Chris@0
|
378 * @param $uri
|
Chris@0
|
379 * @return FeedSet
|
Chris@0
|
380 * @throws Exception\RuntimeException
|
Chris@0
|
381 */
|
Chris@0
|
382 public static function findFeedLinks($uri)
|
Chris@0
|
383 {
|
Chris@0
|
384 $client = static::getHttpClient();
|
Chris@0
|
385 $response = $client->get($uri);
|
Chris@0
|
386 if ($response->getStatusCode() !== 200) {
|
Chris@0
|
387 throw new Exception\RuntimeException("Failed to access $uri, got response code " . $response->getStatusCode());
|
Chris@0
|
388 }
|
Chris@0
|
389 $responseHtml = $response->getBody();
|
Chris@0
|
390 $libxmlErrflag = libxml_use_internal_errors(true);
|
Chris@0
|
391 $oldValue = libxml_disable_entity_loader(true);
|
Chris@0
|
392 $dom = new DOMDocument;
|
Chris@0
|
393 $status = $dom->loadHTML(trim($responseHtml));
|
Chris@0
|
394 libxml_disable_entity_loader($oldValue);
|
Chris@0
|
395 libxml_use_internal_errors($libxmlErrflag);
|
Chris@0
|
396 if (!$status) {
|
Chris@0
|
397 // Build error message
|
Chris@0
|
398 $error = libxml_get_last_error();
|
Chris@0
|
399 if ($error && $error->message) {
|
Chris@0
|
400 $error->message = trim($error->message);
|
Chris@0
|
401 $errormsg = "DOMDocument cannot parse HTML: {$error->message}";
|
Chris@0
|
402 } else {
|
Chris@0
|
403 $errormsg = "DOMDocument cannot parse HTML: Please check the XML document's validity";
|
Chris@0
|
404 }
|
Chris@0
|
405 throw new Exception\RuntimeException($errormsg);
|
Chris@0
|
406 }
|
Chris@0
|
407 $feedSet = new FeedSet;
|
Chris@0
|
408 $links = $dom->getElementsByTagName('link');
|
Chris@0
|
409 $feedSet->addLinks($links, $uri);
|
Chris@0
|
410 return $feedSet;
|
Chris@0
|
411 }
|
Chris@0
|
412
|
Chris@0
|
413 /**
|
Chris@0
|
414 * Detect the feed type of the provided feed
|
Chris@0
|
415 *
|
Chris@0
|
416 * @param Feed\AbstractFeed|DOMDocument|string $feed
|
Chris@0
|
417 * @param bool $specOnly
|
Chris@0
|
418 * @return string
|
Chris@0
|
419 * @throws Exception\InvalidArgumentException
|
Chris@0
|
420 * @throws Exception\RuntimeException
|
Chris@0
|
421 */
|
Chris@0
|
422 public static function detectType($feed, $specOnly = false)
|
Chris@0
|
423 {
|
Chris@0
|
424 if ($feed instanceof Feed\AbstractFeed) {
|
Chris@0
|
425 $dom = $feed->getDomDocument();
|
Chris@0
|
426 } elseif ($feed instanceof DOMDocument) {
|
Chris@0
|
427 $dom = $feed;
|
Chris@0
|
428 } elseif (is_string($feed) && !empty($feed)) {
|
Chris@0
|
429 ErrorHandler::start(E_NOTICE|E_WARNING);
|
Chris@0
|
430 ini_set('track_errors', 1);
|
Chris@0
|
431 $oldValue = libxml_disable_entity_loader(true);
|
Chris@0
|
432 $dom = new DOMDocument;
|
Chris@0
|
433 $status = $dom->loadXML($feed);
|
Chris@0
|
434 foreach ($dom->childNodes as $child) {
|
Chris@0
|
435 if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
|
Chris@0
|
436 throw new Exception\InvalidArgumentException(
|
Chris@0
|
437 'Invalid XML: Detected use of illegal DOCTYPE'
|
Chris@0
|
438 );
|
Chris@0
|
439 }
|
Chris@0
|
440 }
|
Chris@0
|
441 libxml_disable_entity_loader($oldValue);
|
Chris@0
|
442 ini_restore('track_errors');
|
Chris@0
|
443 ErrorHandler::stop();
|
Chris@0
|
444 if (!$status) {
|
Chris@0
|
445 if (!isset($phpErrormsg)) {
|
Chris@0
|
446 if (function_exists('xdebug_is_enabled')) {
|
Chris@0
|
447 $phpErrormsg = '(error message not available, when XDebug is running)';
|
Chris@0
|
448 } else {
|
Chris@0
|
449 $phpErrormsg = '(error message not available)';
|
Chris@0
|
450 }
|
Chris@0
|
451 }
|
Chris@0
|
452 throw new Exception\RuntimeException("DOMDocument cannot parse XML: $phpErrormsg");
|
Chris@0
|
453 }
|
Chris@0
|
454 } else {
|
Chris@0
|
455 throw new Exception\InvalidArgumentException('Invalid object/scalar provided: must'
|
Chris@0
|
456 . ' be of type Zend\Feed\Reader\Feed, DomDocument or string');
|
Chris@0
|
457 }
|
Chris@0
|
458 $xpath = new DOMXPath($dom);
|
Chris@0
|
459
|
Chris@0
|
460 if ($xpath->query('/rss')->length) {
|
Chris@0
|
461 $type = self::TYPE_RSS_ANY;
|
Chris@0
|
462 $version = $xpath->evaluate('string(/rss/@version)');
|
Chris@0
|
463
|
Chris@0
|
464 if (strlen($version) > 0) {
|
Chris@0
|
465 switch ($version) {
|
Chris@0
|
466 case '2.0':
|
Chris@0
|
467 $type = self::TYPE_RSS_20;
|
Chris@0
|
468 break;
|
Chris@0
|
469
|
Chris@0
|
470 case '0.94':
|
Chris@0
|
471 $type = self::TYPE_RSS_094;
|
Chris@0
|
472 break;
|
Chris@0
|
473
|
Chris@0
|
474 case '0.93':
|
Chris@0
|
475 $type = self::TYPE_RSS_093;
|
Chris@0
|
476 break;
|
Chris@0
|
477
|
Chris@0
|
478 case '0.92':
|
Chris@0
|
479 $type = self::TYPE_RSS_092;
|
Chris@0
|
480 break;
|
Chris@0
|
481
|
Chris@0
|
482 case '0.91':
|
Chris@0
|
483 $type = self::TYPE_RSS_091;
|
Chris@0
|
484 break;
|
Chris@0
|
485 }
|
Chris@0
|
486 }
|
Chris@0
|
487
|
Chris@0
|
488 return $type;
|
Chris@0
|
489 }
|
Chris@0
|
490
|
Chris@0
|
491 $xpath->registerNamespace('rdf', self::NAMESPACE_RDF);
|
Chris@0
|
492
|
Chris@0
|
493 if ($xpath->query('/rdf:RDF')->length) {
|
Chris@0
|
494 $xpath->registerNamespace('rss', self::NAMESPACE_RSS_10);
|
Chris@0
|
495
|
Chris@0
|
496 if ($xpath->query('/rdf:RDF/rss:channel')->length
|
Chris@0
|
497 || $xpath->query('/rdf:RDF/rss:image')->length
|
Chris@0
|
498 || $xpath->query('/rdf:RDF/rss:item')->length
|
Chris@0
|
499 || $xpath->query('/rdf:RDF/rss:textinput')->length
|
Chris@0
|
500 ) {
|
Chris@0
|
501 return self::TYPE_RSS_10;
|
Chris@0
|
502 }
|
Chris@0
|
503
|
Chris@0
|
504 $xpath->registerNamespace('rss', self::NAMESPACE_RSS_090);
|
Chris@0
|
505
|
Chris@0
|
506 if ($xpath->query('/rdf:RDF/rss:channel')->length
|
Chris@0
|
507 || $xpath->query('/rdf:RDF/rss:image')->length
|
Chris@0
|
508 || $xpath->query('/rdf:RDF/rss:item')->length
|
Chris@0
|
509 || $xpath->query('/rdf:RDF/rss:textinput')->length
|
Chris@0
|
510 ) {
|
Chris@0
|
511 return self::TYPE_RSS_090;
|
Chris@0
|
512 }
|
Chris@0
|
513 }
|
Chris@0
|
514
|
Chris@0
|
515 $xpath->registerNamespace('atom', self::NAMESPACE_ATOM_10);
|
Chris@0
|
516
|
Chris@0
|
517 if ($xpath->query('//atom:feed')->length) {
|
Chris@0
|
518 return self::TYPE_ATOM_10;
|
Chris@0
|
519 }
|
Chris@0
|
520
|
Chris@0
|
521 if ($xpath->query('//atom:entry')->length) {
|
Chris@0
|
522 if ($specOnly == true) {
|
Chris@0
|
523 return self::TYPE_ATOM_10;
|
Chris@0
|
524 } else {
|
Chris@0
|
525 return self::TYPE_ATOM_10_ENTRY;
|
Chris@0
|
526 }
|
Chris@0
|
527 }
|
Chris@0
|
528
|
Chris@0
|
529 $xpath->registerNamespace('atom', self::NAMESPACE_ATOM_03);
|
Chris@0
|
530
|
Chris@0
|
531 if ($xpath->query('//atom:feed')->length) {
|
Chris@0
|
532 return self::TYPE_ATOM_03;
|
Chris@0
|
533 }
|
Chris@0
|
534
|
Chris@0
|
535 return self::TYPE_ANY;
|
Chris@0
|
536 }
|
Chris@0
|
537
|
Chris@0
|
538 /**
|
Chris@0
|
539 * Set plugin manager for use with Extensions
|
Chris@0
|
540 *
|
Chris@0
|
541 * @param ExtensionManagerInterface $extensionManager
|
Chris@0
|
542 */
|
Chris@0
|
543 public static function setExtensionManager(ExtensionManagerInterface $extensionManager)
|
Chris@0
|
544 {
|
Chris@0
|
545 static::$extensionManager = $extensionManager;
|
Chris@0
|
546 }
|
Chris@0
|
547
|
Chris@0
|
548 /**
|
Chris@0
|
549 * Get plugin manager for use with Extensions
|
Chris@0
|
550 *
|
Chris@0
|
551 * @return ExtensionManagerInterface
|
Chris@0
|
552 */
|
Chris@0
|
553 public static function getExtensionManager()
|
Chris@0
|
554 {
|
Chris@0
|
555 if (!isset(static::$extensionManager)) {
|
Chris@0
|
556 static::setExtensionManager(new StandaloneExtensionManager());
|
Chris@0
|
557 }
|
Chris@0
|
558 return static::$extensionManager;
|
Chris@0
|
559 }
|
Chris@0
|
560
|
Chris@0
|
561 /**
|
Chris@0
|
562 * Register an Extension by name
|
Chris@0
|
563 *
|
Chris@0
|
564 * @param string $name
|
Chris@0
|
565 * @return void
|
Chris@0
|
566 * @throws Exception\RuntimeException if unable to resolve Extension class
|
Chris@0
|
567 */
|
Chris@0
|
568 public static function registerExtension($name)
|
Chris@0
|
569 {
|
Chris@0
|
570 $feedName = $name . '\Feed';
|
Chris@0
|
571 $entryName = $name . '\Entry';
|
Chris@0
|
572 $manager = static::getExtensionManager();
|
Chris@0
|
573 if (static::isRegistered($name)) {
|
Chris@0
|
574 if ($manager->has($feedName) || $manager->has($entryName)) {
|
Chris@0
|
575 return;
|
Chris@0
|
576 }
|
Chris@0
|
577 }
|
Chris@0
|
578
|
Chris@0
|
579 if (!$manager->has($feedName) && !$manager->has($entryName)) {
|
Chris@0
|
580 throw new Exception\RuntimeException('Could not load extension: ' . $name
|
Chris@0
|
581 . ' using Plugin Loader. Check prefix paths are configured and extension exists.');
|
Chris@0
|
582 }
|
Chris@0
|
583 if ($manager->has($feedName)) {
|
Chris@0
|
584 static::$extensions['feed'][] = $feedName;
|
Chris@0
|
585 }
|
Chris@0
|
586 if ($manager->has($entryName)) {
|
Chris@0
|
587 static::$extensions['entry'][] = $entryName;
|
Chris@0
|
588 }
|
Chris@0
|
589 }
|
Chris@0
|
590
|
Chris@0
|
591 /**
|
Chris@0
|
592 * Is a given named Extension registered?
|
Chris@0
|
593 *
|
Chris@0
|
594 * @param string $extensionName
|
Chris@0
|
595 * @return bool
|
Chris@0
|
596 */
|
Chris@0
|
597 public static function isRegistered($extensionName)
|
Chris@0
|
598 {
|
Chris@0
|
599 $feedName = $extensionName . '\Feed';
|
Chris@0
|
600 $entryName = $extensionName . '\Entry';
|
Chris@0
|
601 if (in_array($feedName, static::$extensions['feed'])
|
Chris@0
|
602 || in_array($entryName, static::$extensions['entry'])
|
Chris@0
|
603 ) {
|
Chris@0
|
604 return true;
|
Chris@0
|
605 }
|
Chris@0
|
606 return false;
|
Chris@0
|
607 }
|
Chris@0
|
608
|
Chris@0
|
609 /**
|
Chris@0
|
610 * Get a list of extensions
|
Chris@0
|
611 *
|
Chris@0
|
612 * @return array
|
Chris@0
|
613 */
|
Chris@0
|
614 public static function getExtensions()
|
Chris@0
|
615 {
|
Chris@0
|
616 return static::$extensions;
|
Chris@0
|
617 }
|
Chris@0
|
618
|
Chris@0
|
619 /**
|
Chris@0
|
620 * Reset class state to defaults
|
Chris@0
|
621 *
|
Chris@0
|
622 * @return void
|
Chris@0
|
623 */
|
Chris@0
|
624 public static function reset()
|
Chris@0
|
625 {
|
Chris@0
|
626 static::$cache = null;
|
Chris@0
|
627 static::$httpClient = null;
|
Chris@0
|
628 static::$httpMethodOverride = false;
|
Chris@0
|
629 static::$httpConditionalGet = false;
|
Chris@0
|
630 static::$extensionManager = null;
|
Chris@0
|
631 static::$extensions = [
|
Chris@0
|
632 'feed' => [
|
Chris@0
|
633 'DublinCore\Feed',
|
Chris@0
|
634 'Atom\Feed'
|
Chris@0
|
635 ],
|
Chris@0
|
636 'entry' => [
|
Chris@0
|
637 'Content\Entry',
|
Chris@0
|
638 'DublinCore\Entry',
|
Chris@0
|
639 'Atom\Entry'
|
Chris@0
|
640 ],
|
Chris@0
|
641 'core' => [
|
Chris@0
|
642 'DublinCore\Feed',
|
Chris@0
|
643 'Atom\Feed',
|
Chris@0
|
644 'Content\Entry',
|
Chris@0
|
645 'DublinCore\Entry',
|
Chris@0
|
646 'Atom\Entry'
|
Chris@0
|
647 ]
|
Chris@0
|
648 ];
|
Chris@0
|
649 }
|
Chris@0
|
650
|
Chris@0
|
651 /**
|
Chris@0
|
652 * Register core (default) extensions
|
Chris@0
|
653 *
|
Chris@0
|
654 * @return void
|
Chris@0
|
655 */
|
Chris@0
|
656 protected static function registerCoreExtensions()
|
Chris@0
|
657 {
|
Chris@0
|
658 static::registerExtension('DublinCore');
|
Chris@0
|
659 static::registerExtension('Content');
|
Chris@0
|
660 static::registerExtension('Atom');
|
Chris@0
|
661 static::registerExtension('Slash');
|
Chris@0
|
662 static::registerExtension('WellFormedWeb');
|
Chris@0
|
663 static::registerExtension('Thread');
|
Chris@0
|
664 static::registerExtension('Podcast');
|
Chris@0
|
665 }
|
Chris@0
|
666
|
Chris@0
|
667 /**
|
Chris@0
|
668 * Utility method to apply array_unique operation to a multidimensional
|
Chris@0
|
669 * array.
|
Chris@0
|
670 *
|
Chris@0
|
671 * @param array
|
Chris@0
|
672 * @return array
|
Chris@0
|
673 */
|
Chris@0
|
674 public static function arrayUnique(array $array)
|
Chris@0
|
675 {
|
Chris@0
|
676 foreach ($array as &$value) {
|
Chris@0
|
677 $value = serialize($value);
|
Chris@0
|
678 }
|
Chris@0
|
679 $array = array_unique($array);
|
Chris@0
|
680 foreach ($array as &$value) {
|
Chris@0
|
681 $value = unserialize($value);
|
Chris@0
|
682 }
|
Chris@0
|
683 return $array;
|
Chris@0
|
684 }
|
Chris@0
|
685 }
|