]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/lib/feeddiscovery.php
In very specific circumstances we can bulkDistribute 0 notices
[quix0rs-gnu-social.git] / plugins / OStatus / lib / feeddiscovery.php
index 7afb71bdc16044ad8ec763e64f2b2d442df5a0ae..e9c710bebdbbfa5010c4582cec6ff7a66c5b585e 100644 (file)
@@ -22,7 +22,9 @@
  * @maintainer Brion Vibber <brion@status.net>
  */
 
-if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
+if (!defined('STATUSNET')) {
+    exit(1);
+}
 
 class FeedSubBadURLException extends FeedSubException
 {
@@ -48,6 +50,10 @@ class FeedSubNoFeedException extends FeedSubException
 {
 }
 
+class FeedSubNoSalmonException extends FeedSubException
+{
+}
+
 class FeedSubBadXmlException extends FeedSubException
 {
 }
@@ -73,6 +79,7 @@ class FeedDiscovery
     public $uri;
     public $type;
     public $feed;
+    public $root;
 
     /** Post-initialize query helper... */
     public function getLink($rel, $type=null)
@@ -83,7 +90,17 @@ class FeedDiscovery
 
     public function getAtomLink($rel, $type=null)
     {
-        return ActivityUtils::getLink($this->feed->documentElement, $rel, $type);
+        return ActivityUtils::getLink($this->root, $rel, $type);
+    }
+
+    /**
+     * Get the referenced PuSH hub link from an Atom feed.
+     *
+     * @return mixed string or false
+     */
+    public function getHubLink()
+    {
+        return $this->getAtomLink('hub');
     }
 
     /**
@@ -103,7 +120,7 @@ class FeedDiscovery
             $response = $client->get($url);
         } catch (HTTP_Request2_Exception $e) {
             common_log(LOG_ERR, __METHOD__ . " Failure for $url - " . $e->getMessage());
-            throw new FeedSubBadURLException($e);
+            throw new FeedSubBadURLException($e->getMessage());
         }
 
         if ($htmlOk) {
@@ -117,7 +134,7 @@ class FeedDiscovery
                 return $this->discoverFromURL($target, false);
             }
         }
-        
+
         return $this->initFromResponse($response);
     }
 
@@ -129,7 +146,7 @@ class FeedDiscovery
     function initFromResponse($response)
     {
         if (!$response->isOk()) {
-            throw new FeedSubBadResponseException($response->getCode());
+            throw new FeedSubBadResponseException($response->getStatus());
         }
 
         $sourceurl = $response->getUrl();
@@ -154,9 +171,27 @@ class FeedDiscovery
             $this->uri = $sourceurl;
             $this->type = $type;
             $this->feed = $feed;
+
+            $el = $this->feed->documentElement;
+
+            // Looking for the "root" element: RSS channel or Atom feed
+
+            if ($el->tagName == 'rss') {
+                $channels = $el->getElementsByTagName('channel');
+                if ($channels->length > 0) {
+                    $this->root = $channels->item(0);
+                } else {
+                    throw new FeedSubBadXmlException($sourceurl);
+                }
+            } else if ($el->tagName == 'feed') {
+                $this->root = $el;
+            } else {
+                throw new FeedSubBadXmlException($sourceurl);
+            }
+
             return $this->uri;
         } else {
-            throw new FeedSubBadXmlException($url);
+            throw new FeedSubBadXmlException($sourceurl);
         }
     }
 
@@ -167,8 +202,9 @@ class FeedDiscovery
      */
     function discoverFromHTML($url, $body)
     {
-        // DOMDocument::loadHTML may throw warnings on unrecognized elements.
-        $old = error_reporting(error_reporting() & ~E_WARNING);
+        // DOMDocument::loadHTML may throw warnings on unrecognized elements,
+        // and notices on unrecognized namespaces.
+        $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
         $dom = new DOMDocument();
         $ok = $dom->loadHTML($body);
         error_reporting($old);
@@ -202,7 +238,7 @@ class FeedDiscovery
             'application/atom+xml' => false,
             'application/rss+xml' => false,
         );
-        
+
         $nodes = $dom->getElementsByTagName('link');
         for ($i = 0; $i < $nodes->length; $i++) {
             $node = $nodes->item($i);
@@ -211,11 +247,11 @@ class FeedDiscovery
                 $type = $node->attributes->getNamedItem('type');
                 $href = $node->attributes->getNamedItem('href');
                 if ($rel && $type && $href) {
-                    $rel = trim($rel->value);
+                    $rel = array_filter(explode(" ", $rel->value));
                     $type = trim($type->value);
                     $href = trim($href->value);
 
-                    if (trim($rel) == 'alternate' && array_key_exists($type, $feeds) && empty($feeds[$type])) {
+                    if (in_array('alternate', $rel) && array_key_exists($type, $feeds) && empty($feeds[$type])) {
                         // Save the first feed found of each type...
                         $feeds[$type] = $this->resolveURI($href, $base);
                     }