X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FOStatus%2Fclasses%2FFeedSub.php;h=d0ac83b56d754d96946c3c6bdd18c86e553c76ed;hb=ad7cd155fbc643c9b873415d542745bb79c4e239;hp=a7a77c6644c2d6d8076d8eaa87cc9f4280dbe03e;hpb=220b51d8be61e9bd316567f3ad03fffdbc4b7526;p=quix0rs-gnu-social.git diff --git a/plugins/OStatus/classes/FeedSub.php b/plugins/OStatus/classes/FeedSub.php index a7a77c6644..d0ac83b56d 100644 --- a/plugins/OStatus/classes/FeedSub.php +++ b/plugins/OStatus/classes/FeedSub.php @@ -62,7 +62,7 @@ class FeedSub extends Managed_DataObject public $__table = 'feedsub'; public $id; - public $uri; + public $uri; // varchar(191) not 255 because utf8mb4 takes more space // PuSH subscription data public $huburi; @@ -80,13 +80,13 @@ class FeedSub extends Managed_DataObject return array( 'fields' => array( 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'FeedSub local unique id'), - 'uri' => array('type' => 'varchar', 'not null' => true, 'length' => 255, 'description' => 'FeedSub uri'), + 'uri' => array('type' => 'varchar', 'not null' => true, 'length' => 191, '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","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'), + 'last_update' => array('type' => 'datetime', 'description' => 'when this record was last updated'), 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'), 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'), ), @@ -103,7 +103,7 @@ class FeedSub extends Managed_DataObject public function getUri() { if (empty($this->uri)) { - throw new ServerException('No URI for FeedSub entry'); + throw new NoUriException($this); } return $this->uri; } @@ -168,7 +168,7 @@ class FeedSub extends Managed_DataObject $discover->discoverFromFeedURL($feeduri); $huburi = $discover->getHubLink(); - if (!$huburi && !common_config('feedsub', 'fallback_hub')) { + if (!$huburi && !common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) { throw new FeedSubNoHubException(); } @@ -192,18 +192,18 @@ class FeedSub extends Managed_DataObject * Send a subscription request to the hub for this feed. * The hub will later send us a confirmation POST to /main/push/callback. * - * @return bool true on success, false on failure + * @return void * @throws ServerException if feed state is not valid */ 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))) { // A plugin handled it - return true; + return; } if (empty($this->huburi)) { @@ -211,16 +211,16 @@ class FeedSub extends Managed_DataObject // No native hub on this feed? // Use our fallback hub, which handles polling on our behalf. } else if (common_config('feedsub', 'nohub')) { - // Fake it! We're just testing remote feeds w/o hubs. - // We'll never actually get updates in this mode. - return true; + // For this to actually work, we'll need some polling mechanism. + // The FeedPoller plugin should take care of it. + return; } else { // TRANS: Server exception. throw new ServerException(_m('Attempting to start PuSH subscription for feed with no hub.')); } } - return $this->doSubscribe('subscribe'); + $this->doSubscribe('subscribe'); } /** @@ -230,17 +230,16 @@ class FeedSub extends Managed_DataObject * the system is using it. Most callers will want garbageCollect() instead, * which confirms there's no uses left. * - * @return bool true on success, false on failure * @throws ServerException if feed state is not valid */ 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))) { // A plugin handled it - return true; + return; } if (empty($this->huburi)) { @@ -248,16 +247,16 @@ class FeedSub extends Managed_DataObject // No native hub on this feed? // Use our fallback hub, which handles polling on our behalf. } else if (common_config('feedsub', 'nohub')) { - // Fake it! We're just testing remote feeds w/o hubs. - // We'll never actually get updates in this mode. - return true; + // We need a feedpolling plugin (like FeedPoller) active so it will + // set the 'nohub' state to 'inactive' for us. + return; } else { // TRANS: Server exception. throw new ServerException(_m('Attempting to end PuSH subscription for feed with no hub.')); } } - return $this->doSubscribe('unsubscribe'); + $this->doSubscribe('unsubscribe'); } /** @@ -265,26 +264,31 @@ class FeedSub extends Managed_DataObject * make sure it's inactive, unsubscribing if necessary. * * @return boolean true if the subscription is now inactive, false if still active. + * @throws NoProfileException in FeedSubSubscriberCount for missing Profile entries + * @throws Exception if something goes wrong in unsubscribe() method */ public function garbageCollect() { if ($this->sub_state == '' || $this->sub_state == 'inactive') { // No active PuSH subscription, we can just leave it be. return true; - } else { - // PuSH subscription is either active or in an indeterminate state. - // Check if we're out of subscribers, and if so send an unsubscribe. - $count = 0; - Event::handle('FeedSubSubscriberCount', array($this, &$count)); - - if ($count) { - common_log(LOG_INFO, __METHOD__ . ': ok, ' . $count . ' user(s) left for ' . $this->uri); - return false; - } else { - common_log(LOG_INFO, __METHOD__ . ': unsubscribing, no users left for ' . $this->uri); - return $this->unsubscribe(); - } } + + // PuSH subscription is either active or in an indeterminate state. + // Check if we're out of subscribers, and if so send an unsubscribe. + $count = 0; + Event::handle('FeedSubSubscriberCount', array($this, &$count)); + + if ($count > 0) { + common_log(LOG_INFO, __METHOD__ . ': ok, ' . $count . ' user(s) left for ' . $this->getUri()); + return false; + } + + common_log(LOG_INFO, __METHOD__ . ': unsubscribing, no users left for ' . $this->getUri()); + // Unsubscribe throws various Exceptions on failure + $this->unsubscribe(); + + return true; } static public function renewalCheck() @@ -310,7 +314,8 @@ class FeedSub extends Managed_DataObject * the lookup _while_ we're POSTing data, which means the transaction * never completes (PushcallbackAction gets an 'inactive' state). * - * @return boolean true on successful sub/unsub, false on failure + * @return boolean true when everything is ok (throws Exception on fail) + * @throws Exception on failure, can be HTTPClient's or our own. */ protected function doSubscribe($mode) { @@ -331,7 +336,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; @@ -344,29 +349,32 @@ class FeedSub extends Managed_DataObject $client->setAuth($u, $p); } } else { - throw new FeedSubException('WTF?'); + throw new FeedSubException('Server could not find a usable PuSH hub.'); } } $response = $client->post($hub, $headers, $post); $status = $response->getStatus(); + // PuSH specificed response status code if ($status == 202) { common_log(LOG_INFO, __METHOD__ . ': sub req ok, awaiting verification callback'); - return true; + return; } else if ($status >= 200 && $status < 300) { common_log(LOG_ERR, __METHOD__ . ": sub req returned unexpected HTTP $status: " . $response->getBody()); } else { common_log(LOG_ERR, __METHOD__ . ": sub req failed with HTTP $status: " . $response->getBody()); } } 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()}"); + // Reset the subscription state. $orig = clone($this); $this->sub_state = 'inactive'; $this->update($orig); - unset($orig); + + // Throw the Exception again. + throw $e; } - return false; + throw new ServerException("{$mode} request failed."); } /** @@ -425,10 +433,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')"); + if (!in_array($this->sub_state, array('active', 'nohub'))) { + common_log(LOG_ERR, __METHOD__ . ": ignoring PuSH for inactive feed " . $this->getUri() . " (in state '$this->sub_state')"); return; } @@ -483,9 +491,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'"); @@ -499,4 +507,21 @@ class FeedSub extends Managed_DataObject } return false; } + + public function delete($useWhere=false) + { + try { + $oprofile = Ostatus_profile::getKV('feeduri', $this->getUri()); + if ($oprofile instanceof Ostatus_profile) { + // Check if there's a profile. If not, handle the NoProfileException below + $profile = $oprofile->localProfile(); + } + } catch (NoProfileException $e) { + // If the Ostatus_profile has no local Profile bound to it, let's clean it out at the same time + $oprofile->delete(); + } catch (NoUriException $e) { + // FeedSub->getUri() can throw a NoUriException, let's just go ahead and delete it + } + return parent::delete($useWhere); + } }