From 1a484ec3f58ff5e7a5745f231b68cb64ba65929d Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 1 Apr 2019 09:02:05 +0300 Subject: [PATCH] feedparser: remove horrible hacks for feeds with broken encoding --- classes/feedparser.php | 54 ------------------------------------------ 1 file changed, 54 deletions(-) diff --git a/classes/feedparser.php b/classes/feedparser.php index dc67e204e..400cc7095 100644 --- a/classes/feedparser.php +++ b/classes/feedparser.php @@ -13,20 +13,6 @@ 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) { - - $encoding = strtolower($matches[2]); - - if (in_array($encoding, array_map('strtolower', mb_list_encodings()))) - $data = mb_convert_encoding($data, 'UTF-8', $encoding); - - $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(); @@ -37,46 +23,6 @@ class FeedParser { $error = libxml_get_last_error(); - // libxml compiled without iconv? - if ($error && $error->code == 32) { - $data = $this->normalize_encoding($data); - - if ($data) { - libxml_clear_errors(); - - $this->doc = new DOMDocument(); - $this->doc->loadXML($data); - - $error = libxml_get_last_error(); - } - } - - // some terrible invalid unicode entity? - 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'); - - // 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(); - - $this->doc = new DOMDocument(); - $this->doc->loadXML($data); - - $error = libxml_get_last_error(); - } - break; - } - } - } - if ($error) { foreach (libxml_get_errors() as $error) { if ($error->level == LIBXML_ERR_FATAL) {