summaryrefslogtreecommitdiff
path: root/classes/feedparser.php
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2013-10-29 12:15:26 +0400
committerAndrew Dolgov <[email protected]>2013-10-29 12:15:26 +0400
commit4ad04ee227dd7d704f417aaf9d6762f5cfdf4c1f (patch)
tree0aeb03dc1fe1d04cf372de8e8263416f893d292c /classes/feedparser.php
parent88edaa9344dec86ab1e09dd2113b003c99d0d048 (diff)
report all libxml errors in updater debug output
force utf8 encoding if devforceupdate is on parser: try to convert non-unicode feeds with specified encoding to utf8 before trying to remove dangling utf8 characters in case of utf8-related libxml errors because doing so produces garbage content
Diffstat (limited to 'classes/feedparser.php')
-rw-r--r--classes/feedparser.php29
1 files changed, 19 insertions, 10 deletions
diff --git a/classes/feedparser.php b/classes/feedparser.php
index 1c97e496b..de6c56542 100644
--- a/classes/feedparser.php
+++ b/classes/feedparser.php
@@ -13,6 +13,16 @@ class FeedParser {
const FEED_RSS = 1;
const FEED_ATOM = 2;
+ function normalize_encoding($data) {
+ 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);
+ }
+
+ return $data;
+ }
+
function __construct($data) {
libxml_use_internal_errors(true);
libxml_clear_errors();
@@ -25,19 +35,15 @@ class FeedParser {
// libxml compiled without iconv?
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);
+ $data = $this->normalize_encoding($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();
}
}
@@ -45,6 +51,9 @@ class FeedParser {
if ($error) {
foreach (libxml_get_errors() as $err) {
if ($err->code == 9) {
+ // if the source feed is not in utf8, next conversion will fail
+ $data = $this->normalize_encoding($data);
+
// remove dangling bytes
$data = mb_convert_encoding($data, 'UTF-8', 'UTF-8');