Permalink
Browse files

Handle DOMDocument errors nicely when sanitizing

Fixes #241 for 1.4. Also introduces SimplePie::enable_exceptions() for better
error handling via progessive enhancement.
  • Loading branch information...
1 parent 6daad6d commit 16b8e8ecca06cf0401a46fd078e788154062b93d @rmccue rmccue committed Dec 2, 2012
Showing with 29 additions and 1 deletion.
  1. +25 −1 library/SimplePie.php
  2. +4 −0 library/SimplePie/Sanitize.php
View
@@ -1196,6 +1196,16 @@ public function set_item_limit($limit = 0)
}
/**
+ * Enable throwing exceptions
+ *
+ * @param boolean $enable Should we throw exceptions, or use the old-style error property?
+ */
+ public function enable_exceptions($enable = true)
+ {
+ $this->enable_exceptions = $enable;
+ }
+
+ /**
* Initialize the feed object
*
* This is what makes everything happen. Period. This is where all of the
@@ -1963,7 +1973,21 @@ public function get_base($element = array())
*/
public function sanitize($data, $type, $base = '')
{
- return $this->sanitize->sanitize($data, $type, $base);
+ try
+ {
+ return $this->sanitize->sanitize($data, $type, $base);
+ }
+ catch (SimplePie_Exception $e)
+ {
+ if (!$this->enable_exceptions)
+ {
+ $this->error = $e->getMessage();
+ $this->registry->call('Misc', 'error', array($this->error, E_USER_WARNING, $e->getFile(), $e->getLine()));
+ return '';
+ }
+
+ throw $e;
+ }
}
/**
@@ -247,6 +247,10 @@ public function sanitize($data, $type, $base = '')
if ($type & (SIMPLEPIE_CONSTRUCT_HTML | SIMPLEPIE_CONSTRUCT_XHTML))
{
+ if (!class_exists('DOMDocument'))
+ {
+ throw new SimplePie_Exception('DOMDocument not found, unable to use sanitizer');
+ }
$document = new DOMDocument();
$document->encoding = 'UTF-8';
$data = $this->preprocess($data, $type);

0 comments on commit 16b8e8e

Please sign in to comment.