Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Generate an error if DOMDocument doesn't exist

Rather than trying to load the class and getting a failure, this much more
sensibly generates an error. Fixes #241

Also introduces SimplePie_Exception, which is only used internally.
  • Loading branch information...
commit e33d7445bb5e47cfa8d87bcaa03652d648dd336f 1 parent e41d2c1
@rmccue rmccue authored
View
16 library/SimplePie.php
@@ -1500,10 +1500,20 @@ protected function fetch_data(&$cache)
{
// We need to unset this so that if SimplePie::set_file() has been called that object is untouched
unset($file);
- if (!($file = $locate->find($this->autodiscovery, $this->all_discovered_feeds)))
+ try
{
- $this->error = "A feed could not be found at $this->feed_url. A feed with an invalid mime type may fall victim to this error, or " . SIMPLEPIE_NAME . " was unable to auto-discover it.. Use force_feed() if you are certain this URL is a real feed.";
- $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
+ if (!($file = $locate->find($this->autodiscovery, $this->all_discovered_feeds)))
+ {
+ $this->error = "A feed could not be found at $this->feed_url. A feed with an invalid mime type may fall victim to this error, or " . SIMPLEPIE_NAME . " was unable to auto-discover it.. Use force_feed() if you are certain this URL is a real feed.";
+ $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
+ return false;
+ }
+ }
+ catch (SimplePie_Exception $e)
+ {
+ // This is usually because DOMDocument doesn't exist
+ $this->error = $e->getMessage();
+ $this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, $e->getFile(), $e->getLine()));
return false;
}
if ($cache)
View
29 library/SimplePie/Locator.php
@@ -72,11 +72,18 @@ public function __construct(SimplePie_File $file, $timeout = 10, $useragent = nu
$this->timeout = $timeout;
$this->max_checked_feeds = $max_checked_feeds;
- $this->dom = new DOMDocument();
+ if (class_exists('DOMDocument'))
+ {
+ $this->dom = new DOMDocument();
- set_error_handler(array('SimplePie_Misc', 'silence_errors'));
- $this->dom->loadHTML($this->file->body);
- restore_error_handler();
+ set_error_handler(array('SimplePie_Misc', 'silence_errors'));
+ $this->dom->loadHTML($this->file->body);
+ restore_error_handler();
+ }
+ else
+ {
+ $this->dom = null;
+ }
}
public function set_registry(SimplePie_Registry $registry)
@@ -162,6 +169,10 @@ public function is_feed($file)
public function get_base()
{
+ if ($this->dom === null)
+ {
+ throw new SimplePie_Exception('DOMDocument not found, unable to use locator');
+ }
$this->http_base = $this->file->url;
$this->base = $this->http_base;
$elements = $this->dom->getElementsByTagName('base');
@@ -196,6 +207,11 @@ public function autodiscovery()
protected function search_elements_by_tag($name, &$done, $feeds)
{
+ if ($this->dom === null)
+ {
+ throw new SimplePie_Exception('DOMDocument not found, unable to use locator');
+ }
+
$links = $this->dom->getElementsByTagName($name);
foreach ($links as $link)
{
@@ -238,6 +254,11 @@ protected function search_elements_by_tag($name, &$done, $feeds)
public function get_links()
{
+ if ($this->dom === null)
+ {
+ throw new SimplePie_Exception('DOMDocument not found, unable to use locator');
+ }
+
$links = $this->dom->getElementsByTagName('a');
foreach ($links as $link)
{
View
31 tests/LocatorTest.php
@@ -92,6 +92,37 @@ public function testInvalidMIMEType()
$this->assertEquals($feed, null);
}
+ public function testDirectNoDOM()
+ {
+ $data = new MockSimplePie_File('http://example.com/feed.xml');
+
+ $registry = new SimplePie_Registry();
+ $locator = new SimplePie_Locator($data, 0, null, false);
+ $locator->dom = null;
+ $locator->set_registry($registry);
+
+ $this->assertTrue($locator->is_feed($data));
+ $this->assertEquals($locator->find(SIMPLEPIE_LOCATOR_ALL, $found), $data);
+ }
+
+ /**
+ * @expectedException SimplePie_Exception
+ */
+ public function testFailDiscoveryNoDOM()
+ {
+ $data = new MockSimplePie_File('http://example.com/feed.xml');
+ $data->headers['content-type'] = 'text/html';
+ $data->body = '<!DOCTYPE html><html><body>Hi!</body></html>';
+
+ $registry = new SimplePie_Registry();
+ $locator = new SimplePie_Locator($data, 0, null, false);
+ $locator->dom = null;
+ $locator->set_registry($registry);
+
+ $this->assertFalse($locator->is_feed($data));
+ $this->assertFalse($locator->find(SIMPLEPIE_LOCATOR_ALL, $found));
+ }
+
/**
* Tests from Firefox
*
Please sign in to comment.
Something went wrong with that request. Please try again.