summaryrefslogtreecommitdiff
path: root/classes/feedparser.php
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2013-09-25 03:25:34 -0700
committerAndrew Dolgov <[email protected]>2013-09-25 03:25:34 -0700
commitf227d0af28e2734587fccb0b5fc5dfa7fe639593 (patch)
tree290f41212805c1ae85b8102c5c4961659b7b85da /classes/feedparser.php
parentc6e089b969c108f3dc7589956c74076ba9cf37dd (diff)
parentd3305ff8de6ca9881021646221827d27240dba38 (diff)
Merge pull request #304 from wltb/feedparser_xml9
Feedparser: Change error handling a bit
Diffstat (limited to 'classes/feedparser.php')
-rw-r--r--classes/feedparser.php49
1 files changed, 30 insertions, 19 deletions
diff --git a/classes/feedparser.php b/classes/feedparser.php
index 4a2c6c2da..07de73a9d 100644
--- a/classes/feedparser.php
+++ b/classes/feedparser.php
@@ -23,18 +23,12 @@ class FeedParser {
$error = libxml_get_last_error();
// libxml compiled without iconv?
- if ($error && ($error->code == 32 || $error->code == 9)) {
- if (preg_match('/^(<\?xml[\t\n\r ].*?encoding=["\'])(.+?)(["\'].*?\?>)/s', $data, $matches) === 1) {
- $enc = $matches[2];
-
- $data = mb_convert_encoding($data, 'UTF-8', $enc);
+ if ($error && $error->code == 32) {
+ if (preg_match('/^(<\?xml[\t\n\r ].*?encoding[\t\n\r ]*=[\t\n\r ]*["\'])(.+?)(["\'].*?\?>)/s', $data, $matches) === 1) {
+ $data = mb_convert_encoding($data, 'UTF-8', $matches[2]);
$data = preg_replace('/^<\?xml[\t\n\r ].*?\?>/s', $matches[1] . "UTF-8" . $matches[3] , $data);
-
- // apparently not all UTF-8 characters are valid for XML
- $data = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $data);
-
if ($data) {
libxml_clear_errors();
@@ -43,24 +37,41 @@ class FeedParser {
$error = libxml_get_last_error();
}
- }
+ }
}
// some terrible invalid unicode entity?
- if ($error && $error->code == 9) {
- $data = mb_convert_encoding($data, 'UTF-8', 'UTF-8');
+ if ($error) {
+ foreach(libxml_get_errors() as $err) {
+ if ($err->code == 9) {
+ // remove dangling bytes
+ $data = mb_convert_encoding($data, 'UTF-8', 'UTF-8');
+
+ // apparently not all UTF-8 characters are valid for XML
+ $data = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $data);
- if ($data) {
- libxml_clear_errors();
+ if ($data) {
+ libxml_clear_errors();
- $this->doc = new DOMDocument();
- $this->doc->loadXML($data);
+ $this->doc = new DOMDocument();
+ $this->doc->loadXML($data);
- $error = libxml_get_last_error();
+ $error = libxml_get_last_error();
+ }
+ break;
+ }
}
}
-
- $this->error = $this->format_error($error);
+
+ $this->error = "";
+ if($error) {
+ foreach(libxml_get_errors() as $error) {
+ if($error->level == LIBXML_ERR_FATAL) {
+ $this->error = $this->format_error($error);
+ break; //break here because currently we only show one error
+ }
+ }
+ }
libxml_clear_errors();
$this->items = array();