]> git.wh0rd.org - tt-rss.git/blobdiff - classes/feedparser.php
Merge pull request #405 from Trottel/patch-2
[tt-rss.git] / classes / feedparser.php
index 1c97e496b25b09bd4fd69b572f5c47530128bd8c..239fdb7a69b0c14cb17b4dbd40b96da755291850 100644 (file)
@@ -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 = $this->normalize_encoding($data);
 
-                               $data = preg_replace('/^<\?xml[\t\n\r ].*?\?>/s', $matches[1] . "UTF-8" . $matches[3] , $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');
 
@@ -88,6 +97,7 @@ class FeedParser {
                $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/');
                $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
                $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
+               $xpath->registerNamespace('thread', 'http://purl.org/syndication/thread/1.0');
 
                $this->xpath = $xpath;
 
@@ -195,6 +205,10 @@ class FeedParser {
                                break;
 
                        }
+
+                       if ($this->title) $this->title = trim($this->title);
+                       if ($this->link) $this->link = trim($this->link);
+
                } else {
                        if( !isset($this->error) ){
                                $this->error = "Unknown/unsupported feed type";
@@ -242,7 +256,7 @@ class FeedParser {
 
                        foreach ($links as $link) {
                                if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
-                                       array_push($rv, $link->getAttribute('href'));
+                                       array_push($rv, trim($link->getAttribute('href')));
                                }
                        }
                        break;
@@ -251,7 +265,7 @@ class FeedParser {
 
                        foreach ($links as $link) {
                                if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) {
-                                       array_push($rv, $link->getAttribute('href'));
+                                       array_push($rv, trim($link->getAttribute('href')));
                                }
                        }
                        break;