3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009-2010, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('GNUSOCIAL')) { exit(1); }
23 * @package OStatusPlugin
24 * @maintainer Brion Vibber <brion@status.net>
28 PuSH subscription flow:
31 sends a sub request to the hub...
34 hub sends confirmation back to us via GET
35 We verify the request, then echo back the challenge.
36 On our end, we save the time we subscribed and the lease expiration
39 hub sends us updates via POST
44 * FeedSub handles low-level PubHubSubbub (PuSH) subscriptions.
45 * Higher-level behavior building OStatus stuff on top is handled
46 * under Ostatus_profile.
48 class FeedSub extends Managed_DataObject
50 public $__table = 'feedsub';
53 public $uri; // varchar(191) not 255 because utf8mb4 takes more space
55 // PuSH subscription data
58 public $sub_state; // subscribe, active, unsubscribe, inactive, nohub
66 public static function schemaDef()
70 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'FeedSub local unique id'),
71 'uri' => array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'FeedSub uri'),
72 'huburi' => array('type' => 'text', 'description' => 'FeedSub hub-uri'),
73 'secret' => array('type' => 'text', 'description' => 'FeedSub stored secret'),
74 'sub_state' => array('type' => 'enum("subscribe","active","unsubscribe","inactive","nohub")', 'not null' => true, 'description' => 'subscription state'),
75 'sub_start' => array('type' => 'datetime', 'description' => 'subscription start'),
76 'sub_end' => array('type' => 'datetime', 'description' => 'subscription end'),
77 'last_update' => array('type' => 'datetime', 'description' => 'when this record was last updated'),
78 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
79 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
81 'primary key' => array('id'),
82 'unique keys' => array(
83 'feedsub_uri_key' => array('uri'),
89 * Get the feed uri (http/https)
91 public function getUri()
93 if (empty($this->uri)) {
94 throw new NoUriException($this);
99 function getLeaseRemaining()
101 if (empty($this->sub_end)) {
104 return strtotime($this->sub_end) - time();
108 * Do we have a hub? Then we are a PuSH feed.
109 * https://en.wikipedia.org/wiki/PubSubHubbub
111 * If huburi is empty, then doublecheck that we are not using
112 * a fallback hub. If there is a fallback hub, it is only if the
113 * sub_state is "nohub" that we assume it's not a PuSH feed.
115 public function isPuSH()
117 if (empty($this->huburi)
118 && (!common_config('feedsub', 'fallback_hub')
119 || $this->sub_state === 'nohub')) {
120 // Here we have no huburi set. Also, either there is no
121 // fallback hub configured or sub_state is "nohub".
128 * Fetch the StatusNet-side profile for this feed
131 public function localProfile()
133 if ($this->profile_id) {
134 return Profile::getKV('id', $this->profile_id);
140 * Fetch the StatusNet-side profile for this feed
143 public function localGroup()
145 if ($this->group_id) {
146 return User_group::getKV('id', $this->group_id);
152 * @param string $feeduri
154 * @throws FeedSubException if feed is invalid or lacks PuSH setup
156 public static function ensureFeed($feeduri)
158 $feedsub = self::getKV('uri', $feeduri);
159 if ($feedsub instanceof FeedSub) {
160 if (!empty($feedsub->huburi)) {
161 // If there is already a huburi we don't
162 // rediscover it on ensureFeed, call
163 // ensureHub to do that (compare ->modified
164 // to see if it might be time to do it).
167 if ($feedsub->sub_state !== 'inactive') {
168 throw new ServerException('Can only ensure WebSub hub for inactive (unsubscribed) feeds.');
170 // If huburi is empty we continue with ensureHub
172 // If we don't have that local feed URI
173 // stored then we create a new DB object.
174 $feedsub = new FeedSub();
175 $feedsub->uri = $feeduri;
176 $feedsub->sub_state = 'inactive';
180 // discover the hub uri
181 $feedsub->ensureHub();
183 } catch (FeedSubNoHubException $e) {
184 // Only throw this exception if we can't handle huburi-less feeds
185 // (i.e. we have a fallback hub or we can do feed polling (nohub)
186 if (!common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) {
191 if (empty($feedsub->id)) {
192 // if $feedsub doesn't have an id we'll insert it into the db here
193 $feedsub->created = common_sql_now();
194 $feedsub->modified = common_sql_now();
195 $result = $feedsub->insert();
196 if ($result === false) {
197 throw new FeedDBException($feedsub);
205 * ensureHub will only do $this->update if !empty($this->id)
206 * because otherwise the object has not been created yet.
208 * @param bool $autorenew Whether to autorenew the feed after ensuring the hub URL
210 * @return null if actively avoiding the database
211 * int number of rows updated in the database (0 means untouched)
213 * @throws ServerException if something went wrong when updating the database
214 * FeedSubNoHubException if no hub URL was discovered
216 public function ensureHub($autorenew=false)
218 if ($this->sub_state !== 'inactive') {
219 common_log(LOG_INFO, sprintf(__METHOD__ . ': Running hub discovery a possibly active feed in %s state for URI %s', _ve($this->sub_state), _ve($this->uri)));
222 $discover = new FeedDiscovery();
223 $discover->discoverFromFeedURL($this->uri);
225 $huburi = $discover->getHubLink();
226 if (empty($huburi)) {
227 // Will be caught and treated with if statements in regards to
228 // fallback hub and feed polling (nohub) configuration.
229 throw new FeedSubNoHubException();
232 // if we've already got a DB object stored, we want to UPDATE, not INSERT
233 $orig = !empty($this->id) ? clone($this) : null;
235 $old_huburi = $this->huburi; // most likely null if we're INSERTing
236 $this->huburi = $huburi;
238 if (!empty($this->id)) {
239 common_debug(sprintf(__METHOD__ . ': Feed uri==%s huburi before=%s after=%s (identical==%s)', _ve($this->uri), _ve($old_huburi), _ve($this->huburi), _ve($old_huburi===$this->huburi)));
240 $result = $this->update($orig);
241 if ($result === false) {
242 // TODO: Get a DB exception class going...
243 common_debug('Database update failed for FeedSub id=='._ve($this->id).' with new huburi: '._ve($this->huburi));
244 throw new ServerException('Database update failed for FeedSub.');
252 return null; // we haven't done anything with the database
256 * Send a subscription request to the hub for this feed.
257 * The hub will later send us a confirmation POST to /main/push/callback.
260 * @throws ServerException if feed state is not valid
262 public function subscribe()
264 if ($this->sub_state && $this->sub_state != 'inactive') {
265 common_log(LOG_WARNING, sprintf('Attempting to (re)start PuSH subscription to %s in unexpected state %s', $this->getUri(), $this->sub_state));
268 if (!Event::handle('FeedSubscribe', array($this))) {
269 // A plugin handled it
273 if (empty($this->huburi)) {
274 if (common_config('feedsub', 'fallback_hub')) {
275 // No native hub on this feed?
276 // Use our fallback hub, which handles polling on our behalf.
277 } else if (common_config('feedsub', 'nohub')) {
278 // For this to actually work, we'll need some polling mechanism.
279 // The FeedPoller plugin should take care of it.
282 // TRANS: Server exception.
283 throw new ServerException(_m('Attempting to start PuSH subscription for feed with no hub.'));
287 $this->doSubscribe('subscribe');
291 * Send a PuSH unsubscription request to the hub for this feed.
292 * The hub will later send us a confirmation POST to /main/push/callback.
293 * Warning: this will cancel the subscription even if someone else in
294 * the system is using it. Most callers will want garbageCollect() instead,
295 * which confirms there's no uses left.
297 * @throws ServerException if feed state is not valid
299 public function unsubscribe() {
300 if ($this->sub_state != 'active') {
301 common_log(LOG_WARNING, sprintf('Attempting to (re)end PuSH subscription to %s in unexpected state %s', $this->getUri(), $this->sub_state));
304 if (!Event::handle('FeedUnsubscribe', array($this))) {
305 // A plugin handled it
309 if (empty($this->huburi) && !common_config('feedsub', 'fallback_hub')) {
311 * If the huburi is empty and we don't have a fallback hub,
312 * there is nowhere we can send an unsubscribe to.
314 * A plugin should handle the FeedSub above and set the proper state
315 * if there is no hub. (instead of 'nohub' it should be 'inactive' if
316 * the instance has enabled feed polling for feeds that don't publish
317 * PuSH/WebSub hubs. FeedPoller is a plugin which enables polling.
319 * Secondly, if we don't have the setting "nohub" enabled (i.e.)
320 * we're ready to poll ourselves, there is something odd with the
321 * database, such as a polling plugin that has been disabled.
324 if (!common_config('feedsub', 'nohub')) {
325 // TRANS: Server exception.
326 throw new ServerException(_m('Attempting to end PuSH subscription for feed with no hub.'));
332 $this->doSubscribe('unsubscribe');
336 * Check if there are any active local uses of this feed, and if not then
337 * make sure it's inactive, unsubscribing if necessary.
339 * @return boolean true if the subscription is now inactive, false if still active.
340 * @throws NoProfileException in FeedSubSubscriberCount for missing Profile entries
341 * @throws Exception if something goes wrong in unsubscribe() method
343 public function garbageCollect()
345 if ($this->sub_state == '' || $this->sub_state == 'inactive') {
346 // No active PuSH subscription, we can just leave it be.
350 // PuSH subscription is either active or in an indeterminate state.
351 // Check if we're out of subscribers, and if so send an unsubscribe.
353 Event::handle('FeedSubSubscriberCount', array($this, &$count));
356 common_log(LOG_INFO, __METHOD__ . ': ok, ' . $count . ' user(s) left for ' . $this->getUri());
360 common_log(LOG_INFO, __METHOD__ . ': unsubscribing, no users left for ' . $this->getUri());
361 // Unsubscribe throws various Exceptions on failure
362 $this->unsubscribe();
367 static public function renewalCheck()
370 // the "" empty string check is because we historically haven't saved unsubscribed feeds as NULL
371 $fs->whereAdd('sub_end IS NOT NULL AND sub_end!="" AND sub_end < NOW() + INTERVAL 1 day');
372 if (!$fs->find()) { // find can be both false and 0, depending on why nothing was found
373 throw new NoResultException($fs);
378 public function renew()
380 common_debug('FeedSub is being renewed for uri=='._ve($this->uri).' on huburi=='._ve($this->huburi));
385 * Setting to subscribe means it is _waiting_ to become active. This
386 * cannot be done in a transaction because there is a chance that the
387 * remote script we're calling (as in the case of PuSHpress) performs
388 * the lookup _while_ we're POSTing data, which means the transaction
389 * never completes (PushcallbackAction gets an 'inactive' state).
391 * @return boolean true when everything is ok (throws Exception on fail)
392 * @throws Exception on failure, can be HTTPClient's or our own.
394 protected function doSubscribe($mode)
396 $msg = null; // carries descriptive error message to enduser (no remote data strings!)
398 $orig = clone($this);
399 if ($mode == 'subscribe') {
400 $this->secret = common_random_hexstr(32);
402 $this->sub_state = $mode;
403 $this->update($orig);
407 $callback = common_local_url('pushcallback', array('feed' => $this->id));
408 $headers = array('Content-Type: application/x-www-form-urlencoded');
409 $post = array('hub.mode' => $mode,
410 'hub.callback' => $callback,
411 'hub.verify' => 'async', // TODO: deprecated, remove when noone uses PuSH <0.4 (only 'async' method used there)
412 'hub.verify_token' => 'Deprecated-since-PuSH-0.4', // TODO: rm!
414 'hub.lease_seconds' => 2592000, // 3600*24*30, request approximately month long lease (may be changed by hub)
415 'hub.secret' => $this->secret,
416 'hub.topic' => $this->getUri());
417 $client = new HTTPClient();
419 $hub = $this->huburi;
421 if (common_config('feedsub', 'fallback_hub')) {
422 $hub = common_config('feedsub', 'fallback_hub');
423 if (common_config('feedsub', 'hub_user')) {
424 $u = common_config('feedsub', 'hub_user');
425 $p = common_config('feedsub', 'hub_pass');
426 $client->setAuth($u, $p);
429 throw new FeedSubException('Server could not find a usable PuSH hub.');
432 $response = $client->post($hub, $headers, $post);
433 $status = $response->getStatus();
434 // PuSH specificed response status code
435 if ($status == 202 || $status == 204) {
436 common_log(LOG_INFO, __METHOD__ . ': sub req ok, awaiting verification callback');
438 } else if ($status >= 200 && $status < 300) {
439 common_log(LOG_ERR, __METHOD__ . ": sub req returned unexpected HTTP $status: " . $response->getBody());
440 $msg = sprintf(_m("Unexpected HTTP status: %d"), $status);
441 } else if ($status == 422) {
442 // Error code regarding something wrong in the data (it seems
443 // that we're talking to a PuSH hub at least, so let's check
444 // our own data to be sure we're not mistaken somehow.
446 $this->ensureHub(true);
448 common_log(LOG_ERR, __METHOD__ . ": sub req failed with HTTP $status: " . $response->getBody());
450 } catch (Exception $e) {
451 common_log(LOG_ERR, __METHOD__ . ": error \"{$e->getMessage()}\" hitting hub {$this->huburi} subscribing to {$this->getUri()}");
453 // Reset the subscription state.
454 $orig = clone($this);
455 $this->sub_state = 'inactive';
456 $this->update($orig);
458 // Throw the Exception again.
461 throw new ServerException("{$mode} request failed" . (!is_null($msg) ? " ($msg)" : '.'));
465 * Save PuSH subscription confirmation.
466 * Sets approximate lease start and end times and finalizes state.
468 * @param int $lease_seconds provided hub.lease_seconds parameter, if given
470 public function confirmSubscribe($lease_seconds)
472 $original = clone($this);
474 $this->sub_state = 'active';
475 $this->sub_start = common_sql_date(time());
476 if ($lease_seconds > 0) {
477 $this->sub_end = common_sql_date(time() + $lease_seconds);
479 $this->sub_end = null; // Backwards compatibility to StatusNet (PuSH <0.4 supported permanent subs)
481 $this->modified = common_sql_now();
483 common_debug(__METHOD__ . ': Updating sub state and metadata for '.$this->getUri());
484 return $this->update($original);
488 * Save PuSH unsubscription confirmation.
489 * Wipes active PuSH sub info and resets state.
491 public function confirmUnsubscribe()
493 $original = clone($this);
495 // @fixme these should all be null, but DB_DataObject doesn't save null values...?????
497 $this->sub_state = '';
498 $this->sub_start = '';
500 $this->modified = common_sql_now();
502 return $this->update($original);
506 * Accept updates from a PuSH feed. If validated, this object and the
507 * feed (as a DOMDocument) will be passed to the StartFeedSubHandleFeed
508 * and EndFeedSubHandleFeed events for processing.
510 * Not guaranteed to be running in an immediate POST context; may be run
511 * from a queue handler.
513 * Side effects: the feedsub record's lastupdate field will be updated
514 * to the current time (not published time) if we got a legit update.
516 * @param string $post source of Atom or RSS feed
517 * @param string $hmac X-Hub-Signature header, if present
519 public function receive($post, $hmac)
521 common_log(LOG_INFO, sprintf(__METHOD__.': packet for %s with HMAC %s', _ve($this->getUri()), _ve($hmac)));
523 if (!in_array($this->sub_state, array('active', 'nohub'))) {
524 common_log(LOG_ERR, sprintf(__METHOD__.': ignoring PuSH for inactive feed %s (in state %s)', _ve($this->getUri()), _ve($this->sub_state)));
529 common_log(LOG_ERR, __METHOD__ . ": ignoring empty post");
534 if (!$this->validatePushSig($post, $hmac)) {
535 // Per spec we silently drop input with a bad sig,
536 // while reporting receipt to the server.
540 $this->receiveFeed($post);
542 } catch (FeedSubBadPushSignatureException $e) {
543 // We got a signature, so something could be wrong. Let's check to see if
544 // maybe upstream has switched to another hub. Let's fetch feed and then
545 // compare rel="hub" with $this->huburi, which is done in $this->ensureHub()
547 $this->ensureHub(true);
552 * All our feed URIs should be URLs.
554 public function importFeed()
556 $feed_url = $this->getUri();
560 common_log(LOG_INFO, sprintf('Importing feed backlog from %s', $feed_url));
561 $feed_xml = HTTPClient::quickGet($feed_url, 'application/atom+xml');
562 } catch (Exception $e) {
563 throw new FeedSubException("Could not fetch feed from URL '%s': %s (%d).\n", $feed_url, $e->getMessage(), $e->getCode());
566 return $this->receiveFeed($feed_xml);
569 protected function receiveFeed($feed_xml)
571 // We're passed the XML for the Atom feed as $feed_xml,
572 // so read it into a DOMDocument and process.
573 $feed = new DOMDocument();
574 if (!$feed->loadXML($feed_xml)) {
575 // @fixme might help to include the err message
576 common_log(LOG_ERR, __METHOD__ . ": ignoring invalid XML");
580 $orig = clone($this);
581 $this->last_update = common_sql_now();
582 $this->update($orig);
584 Event::handle('StartFeedSubReceive', array($this, $feed));
585 Event::handle('EndFeedSubReceive', array($this, $feed));
589 * Validate the given Atom chunk and HMAC signature against our
590 * shared secret that was set up at subscription time.
592 * If we don't have a shared secret, there should be no signature.
593 * If we do, our calculated HMAC should match theirs.
595 * @param string $post raw XML source as POSTed to us
596 * @param string $hmac X-Hub-Signature HTTP header value, or empty
597 * @return boolean true for a match
599 protected function validatePushSig($post, $hmac)
602 // {3,16} because shortest hash algorithm name is 3 characters (md2,md4,md5) and longest
603 // is currently 11 characters, but we'll leave some margin in the end...
604 if (preg_match('/^([0-9a-zA-Z\-\,]{3,16})=([0-9a-fA-F]+)$/', $hmac, $matches)) {
605 $hash_algo = strtolower($matches[1]);
606 $their_hmac = strtolower($matches[2]);
607 common_debug(sprintf(__METHOD__ . ': PuSH from feed %s uses HMAC algorithm %s with value: %s', _ve($this->getUri()), _ve($hash_algo), _ve($their_hmac)));
609 if (!in_array($hash_algo, hash_algos())) {
610 // We can't handle this at all, PHP doesn't recognize the algorithm name ('md5', 'sha1', 'sha256' etc: https://secure.php.net/manual/en/function.hash-algos.php)
611 common_log(LOG_ERR, sprintf(__METHOD__.': HMAC algorithm %s unsupported, not found in PHP hash_algos()', _ve($hash_algo)));
613 } elseif (!is_null(common_config('security', 'hash_algos')) && !in_array($hash_algo, common_config('security', 'hash_algos'))) {
614 // We _won't_ handle this because there is a list of accepted hash algorithms and this one is not in it.
615 common_log(LOG_ERR, sprintf(__METHOD__.': Whitelist for HMAC algorithms exist, but %s is not included.', _ve($hash_algo)));
619 $our_hmac = hash_hmac($hash_algo, $post, $this->secret);
620 if ($their_hmac !== $our_hmac) {
621 common_log(LOG_ERR, sprintf(__METHOD__.': ignoring PuSH with bad HMAC hash: got %s, expected %s for feed %s from hub %s', _ve($their_hmac), _ve($our_hmac), _ve($this->getUri()), _ve($this->huburi)));
622 throw new FeedSubBadPushSignatureException('Incoming PuSH signature did not match expected HMAC hash.');
627 common_log(LOG_ERR, sprintf(__METHOD__.': ignoring PuSH with bogus HMAC==', _ve($hmac)));
633 common_log(LOG_ERR, sprintf(__METHOD__.': ignoring PuSH with unexpected HMAC==%s', _ve($hmac)));
639 public function delete($useWhere=false)
642 $oprofile = Ostatus_profile::getKV('feeduri', $this->getUri());
643 if ($oprofile instanceof Ostatus_profile) {
644 // Check if there's a profile. If not, handle the NoProfileException below
645 $profile = $oprofile->localProfile();
647 } catch (NoProfileException $e) {
648 // If the Ostatus_profile has no local Profile bound to it, let's clean it out at the same time
650 } catch (NoUriException $e) {
651 // FeedSub->getUri() can throw a NoUriException, let's just go ahead and delete it
653 return parent::delete($useWhere);