]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/classes/FeedSub.php
Code cleanup and enabling User object's etc. getUri()
[quix0rs-gnu-social.git] / plugins / OStatus / classes / FeedSub.php
index 55cf1bddbca8e7020eb0c6707e6161f861eb6540..0a6abfa1bef239346fe4c30ae6d8da4943eb643b 100644 (file)
@@ -67,7 +67,7 @@ class FeedSub extends Managed_DataObject
     // PuSH subscription data
     public $huburi;
     public $secret;
-    public $sub_state; // subscribe, active, unsubscribe, inactive
+    public $sub_state; // subscribe, active, unsubscribe, inactive, nohub
     public $sub_start;
     public $sub_end;
     public $last_update;
@@ -83,7 +83,7 @@ class FeedSub extends Managed_DataObject
                 'uri' => array('type' => 'varchar', 'not null' => true, 'length' => 255, 'description' => 'FeedSub uri'),
                 'huburi' => array('type' => 'text', 'description' => 'FeedSub hub-uri'),
                 'secret' => array('type' => 'text', 'description' => 'FeedSub stored secret'),
-                'sub_state' => array('type' => 'enum("subscribe","active","unsubscribe","inactive")', 'not null' => true, 'description' => 'subscription state'),
+                'sub_state' => array('type' => 'enum("subscribe","active","unsubscribe","inactive","nohub")', 'not null' => true, 'description' => 'subscription state'),
                 'sub_start' => array('type' => 'datetime', 'description' => 'subscription start'),
                 'sub_end' => array('type' => 'datetime', 'description' => 'subscription end'),
                 'last_update' => array('type' => 'datetime', 'not null' => true, 'description' => 'when this record was last updated'),
@@ -112,11 +112,20 @@ class FeedSub extends Managed_DataObject
      * Do we have a hub? Then we are a PuSH feed.
      * https://en.wikipedia.org/wiki/PubSubHubbub
      *
-     * NOTE: does not respect if we have a fallback_hub configured
+     * If huburi is empty, then doublecheck that we are not using
+     * a fallback hub. If there is a fallback hub, it is only if the
+     * sub_state is "nohub" that we assume it's not a PuSH feed.
      */
     public function isPuSH()
     {
-        return !empty($this->huburi);
+        if (empty($this->huburi)
+                && (!common_config('feedsub', 'fallback_hub')
+                    || $this->sub_state === 'nohub')) {
+                // Here we have no huburi set. Also, either there is no 
+                // fallback hub configured or sub_state is "nohub".
+            return false;
+        }
+        return true;
     }
 
     /**
@@ -189,7 +198,7 @@ class FeedSub extends Managed_DataObject
     public function subscribe()
     {
         if ($this->sub_state && $this->sub_state != 'inactive') {
-            common_log(LOG_WARNING, "Attempting to (re)start PuSH subscription to {$this->uri} in unexpected state {$this->sub_state}");
+            common_log(LOG_WARNING, sprintf('Attempting to (re)start PuSH subscription to %s in unexpected state %s', $this->getUri(), $this->sub_state));
         }
 
         if (!Event::handle('FeedSubscribe', array($this))) {
@@ -226,7 +235,7 @@ class FeedSub extends Managed_DataObject
      */
     public function unsubscribe() {
         if ($this->sub_state != 'active') {
-            common_log(LOG_WARNING, "Attempting to (re)end PuSH subscription to {$this->uri} in unexpected state {$this->sub_state}");
+            common_log(LOG_WARNING, sprintf('Attempting to (re)end PuSH subscription to %s in unexpected state %s', $this->getUri(), $this->sub_state));
         }
 
         if (!Event::handle('FeedUnsubscribe', array($this))) {
@@ -269,10 +278,10 @@ class FeedSub extends Managed_DataObject
             Event::handle('FeedSubSubscriberCount', array($this, &$count));
 
             if ($count) {
-                common_log(LOG_INFO, __METHOD__ . ': ok, ' . $count . ' user(s) left for ' . $this->uri);
+                common_log(LOG_INFO, __METHOD__ . ': ok, ' . $count . ' user(s) left for ' . $this->getUri());
                 return false;
             } else {
-                common_log(LOG_INFO, __METHOD__ . ': unsubscribing, no users left for ' . $this->uri);
+                common_log(LOG_INFO, __METHOD__ . ': unsubscribing, no users left for ' . $this->getUri());
                 return $this->unsubscribe();
             }
         }
@@ -322,7 +331,7 @@ class FeedSub extends Managed_DataObject
                           'hub.verify_token' => 'Deprecated-since-PuSH-0.4', // TODO: rm!
 
                           'hub.secret' => $this->secret,
-                          'hub.topic' => $this->uri);
+                          'hub.topic' => $this->getUri());
             $client = new HTTPClient();
             if ($this->huburi) {
                 $hub = $this->huburi;
@@ -350,7 +359,7 @@ class FeedSub extends Managed_DataObject
             }
         } catch (Exception $e) {
             // wtf!
-            common_log(LOG_ERR, __METHOD__ . ": error \"{$e->getMessage()}\" hitting hub $this->huburi subscribing to $this->uri");
+            common_log(LOG_ERR, __METHOD__ . ": error \"{$e->getMessage()}\" hitting hub $this->huburi subscribing to " . $this->getUri());
 
             $orig = clone($this);
             $this->sub_state = 'inactive';
@@ -416,10 +425,10 @@ class FeedSub extends Managed_DataObject
      */
     public function receive($post, $hmac)
     {
-        common_log(LOG_INFO, __METHOD__ . ": packet for \"$this->uri\"! $hmac $post");
+        common_log(LOG_INFO, __METHOD__ . ": packet for \"" . $this->getUri() . "\"! $hmac $post");
 
         if ($this->sub_state != 'active') {
-            common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH for inactive feed $this->uri (in state '$this->sub_state')");
+            common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH for inactive feed " . $this->getUri() . " (in state '$this->sub_state')");
             return;
         }
 
@@ -474,9 +483,9 @@ class FeedSub extends Managed_DataObject
                     if ($tempfile) {
                         file_put_contents($tempfile, $post);
                     }
-                    common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with bad SHA-1 HMAC: got $their_hmac, expected $our_hmac for feed $this->uri on $this->huburi; saved to $tempfile");
+                    common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with bad SHA-1 HMAC: got $their_hmac, expected $our_hmac for feed " . $this->getUri() . " on $this->huburi; saved to $tempfile");
                 } else {
-                    common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with bad SHA-1 HMAC: got $their_hmac, expected $our_hmac for feed $this->uri on $this->huburi");
+                    common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with bad SHA-1 HMAC: got $their_hmac, expected $our_hmac for feed " . $this->getUri() . " on $this->huburi");
                 }
             } else {
                 common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH with bogus HMAC '$hmac'");