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 WebSub (previously PubSubHubbub/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 WebSub (PubSubHubbub/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 // WebSub 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 WebSub feed.
109 * WebSub standard: https://www.w3.org/TR/websub/
110 * old: https://en.wikipedia.org/wiki/PubSubHubbub
112 * If huburi is empty, then doublecheck that we are not using
113 * a fallback hub. If there is a fallback hub, it is only if the
114 * sub_state is "nohub" that we assume it's not a WebSub feed.
116 public function isWebSub()
118 if (empty($this->huburi)
119 && (!common_config('feedsub', 'fallback_hub')
120 || $this->sub_state === 'nohub')) {
121 // Here we have no huburi set. Also, either there is no
122 // fallback hub configured or sub_state is "nohub".
129 * Fetch the StatusNet-side profile for this feed
132 public function localProfile()
134 if ($this->profile_id) {
135 return Profile::getKV('id', $this->profile_id);
141 * Fetch the StatusNet-side profile for this feed
144 public function localGroup()
146 if ($this->group_id) {
147 return User_group::getKV('id', $this->group_id);
153 * @param string $feeduri
155 * @throws FeedSubException if feed is invalid or lacks WebSub setup
157 public static function ensureFeed($feeduri)
159 $feedsub = self::getKV('uri', $feeduri);
160 if ($feedsub instanceof FeedSub) {
161 if (!empty($feedsub->huburi)) {
162 // If there is already a huburi we don't
163 // rediscover it on ensureFeed, call
164 // ensureHub to do that (compare ->modified
165 // to see if it might be time to do it).
168 if ($feedsub->sub_state !== 'inactive') {
169 throw new ServerException('Can only ensure WebSub hub for inactive (unsubscribed) feeds.');
171 // If huburi is empty we continue with ensureHub
173 // If we don't have that local feed URI
174 // stored then we create a new DB object.
175 $feedsub = new FeedSub();
176 $feedsub->uri = $feeduri;
177 $feedsub->sub_state = 'inactive';
181 // discover the hub uri
182 $feedsub->ensureHub();
184 } catch (FeedSubNoHubException $e) {
185 // Only throw this exception if we can't handle huburi-less feeds
186 // (i.e. we have a fallback hub or we can do feed polling (nohub)
187 if (!common_config('feedsub', 'fallback_hub') && !common_config('feedsub', 'nohub')) {
192 if (empty($feedsub->id)) {
193 // if $feedsub doesn't have an id we'll insert it into the db here
194 $feedsub->created = common_sql_now();
195 $feedsub->modified = common_sql_now();
196 $result = $feedsub->insert();
197 if ($result === false) {
198 throw new FeedDBException($feedsub);
206 * ensureHub will only do $this->update if !empty($this->id)
207 * because otherwise the object has not been created yet.
209 * @param bool $rediscovered Whether the hub info is rediscovered (to avoid endless loop nesting)
211 * @return null if actively avoiding the database
212 * int number of rows updated in the database (0 means untouched)
214 * @throws ServerException if something went wrong when updating the database
215 * FeedSubNoHubException if no hub URL was discovered
217 public function ensureHub($rediscovered=false)
219 common_debug('Now inside ensureHub again, $rediscovered=='._ve($rediscovered));
220 if ($this->sub_state !== 'inactive') {
221 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)));
224 $discover = new FeedDiscovery();
225 $discover->discoverFromFeedURL($this->uri);
227 $huburi = $discover->getHubLink();
228 if (empty($huburi)) {
229 // Will be caught and treated with if statements in regards to
230 // fallback hub and feed polling (nohub) configuration.
231 throw new FeedSubNoHubException();
234 // if we've already got a DB object stored, we want to UPDATE, not INSERT
235 $orig = !empty($this->id) ? clone($this) : null;
237 $old_huburi = $this->huburi; // most likely null if we're INSERTing
238 $this->huburi = $huburi;
240 if (!empty($this->id)) {
241 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)));
242 $result = $this->update($orig);
243 if ($result === false) {
244 // TODO: Get a DB exception class going...
245 common_debug('Database update failed for FeedSub id=='._ve($this->id).' with new huburi: '._ve($this->huburi));
246 throw new ServerException('Database update failed for FeedSub.');
248 if (!$rediscovered) {
254 return null; // we haven't done anything with the database
258 * Send a subscription request to the hub for this feed.
259 * The hub will later send us a confirmation POST to /main/push/callback.
262 * @throws ServerException if feed state is not valid
264 public function subscribe($rediscovered=false)
266 if ($this->sub_state && $this->sub_state != 'inactive') {
267 common_log(LOG_WARNING, sprintf('Attempting to (re)start WebSub subscription to %s in unexpected state %s', $this->getUri(), $this->sub_state));
270 if (!Event::handle('FeedSubscribe', array($this))) {
271 // A plugin handled it
275 if (empty($this->huburi)) {
276 if (common_config('feedsub', 'fallback_hub')) {
277 // No native hub on this feed?
278 // Use our fallback hub, which handles polling on our behalf.
279 } else if (common_config('feedsub', 'nohub')) {
280 // For this to actually work, we'll need some polling mechanism.
281 // The FeedPoller plugin should take care of it.
284 // TRANS: Server exception.
285 throw new ServerException(_m('Attempting to start WebSub subscription for feed with no hub.'));
289 $this->doSubscribe('subscribe', $rediscovered);
293 * Send a WebSub unsubscription request to the hub for this feed.
294 * The hub will later send us a confirmation POST to /main/push/callback.
295 * Warning: this will cancel the subscription even if someone else in
296 * the system is using it. Most callers will want garbageCollect() instead,
297 * which confirms there's no uses left.
299 * @throws ServerException if feed state is not valid
301 public function unsubscribe() {
302 if ($this->sub_state != 'active') {
303 common_log(LOG_WARNING, sprintf('Attempting to (re)end WebSub subscription to %s in unexpected state %s', $this->getUri(), $this->sub_state));
306 if (!Event::handle('FeedUnsubscribe', array($this))) {
307 // A plugin handled it
311 if (empty($this->huburi) && !common_config('feedsub', 'fallback_hub')) {
313 * If the huburi is empty and we don't have a fallback hub,
314 * there is nowhere we can send an unsubscribe to.
316 * A plugin should handle the FeedSub above and set the proper state
317 * if there is no hub. (instead of 'nohub' it should be 'inactive' if
318 * the instance has enabled feed polling for feeds that don't publish
319 * WebSub/PuSH hubs. FeedPoller is a plugin which enables polling.
321 * Secondly, if we don't have the setting "nohub" enabled (i.e.)
322 * we're ready to poll ourselves, there is something odd with the
323 * database, such as a polling plugin that has been disabled.
326 if (!common_config('feedsub', 'nohub')) {
327 // TRANS: Server exception.
328 throw new ServerException(_m('Attempting to end WebSub subscription for feed with no hub.'));
334 $this->doSubscribe('unsubscribe');
338 * Check if there are any active local uses of this feed, and if not then
339 * make sure it's inactive, unsubscribing if necessary.
341 * @return boolean true if the subscription is now inactive, false if still active.
342 * @throws NoProfileException in FeedSubSubscriberCount for missing Profile entries
343 * @throws Exception if something goes wrong in unsubscribe() method
345 public function garbageCollect()
347 if ($this->sub_state == '' || $this->sub_state == 'inactive') {
348 // No active WebSub subscription, we can just leave it be.
352 // WebSub subscription is either active or in an indeterminate state.
353 // Check if we're out of subscribers, and if so send an unsubscribe.
355 Event::handle('FeedSubSubscriberCount', array($this, &$count));
358 common_log(LOG_INFO, __METHOD__ . ': ok, ' . $count . ' user(s) left for ' . $this->getUri());
362 common_log(LOG_INFO, __METHOD__ . ': unsubscribing, no users left for ' . $this->getUri());
363 // Unsubscribe throws various Exceptions on failure
364 $this->unsubscribe();
369 static public function renewalCheck()
372 // the "" empty string check is because we historically haven't saved unsubscribed feeds as NULL
373 $fs->whereAdd('sub_end IS NOT NULL AND sub_end!="" AND sub_end < NOW() + INTERVAL 1 day');
374 if (!$fs->find()) { // find can be both false and 0, depending on why nothing was found
375 throw new NoResultException($fs);
380 public function renew($rediscovered=false)
382 common_debug('FeedSub is being renewed for uri=='._ve($this->uri).' on huburi=='._ve($this->huburi));
383 $this->subscribe($rediscovered);
387 * Setting to subscribe means it is _waiting_ to become active. This
388 * cannot be done in a transaction because there is a chance that the
389 * remote script we're calling (as in the case of PuSHpress) performs
390 * the lookup _while_ we're POSTing data, which means the transaction
391 * never completes (PushcallbackAction gets an 'inactive' state).
393 * @return boolean true when everything is ok (throws Exception on fail)
394 * @throws Exception on failure, can be HTTPClient's or our own.
396 protected function doSubscribe($mode, $rediscovered=false)
398 $msg = null; // carries descriptive error message to enduser (no remote data strings!)
400 $orig = clone($this);
401 if ($mode == 'subscribe') {
402 $this->secret = common_random_hexstr(32);
404 $this->sub_state = $mode;
405 $this->update($orig);
409 $callback = common_local_url('pushcallback', array('feed' => $this->id));
410 $headers = array('Content-Type: application/x-www-form-urlencoded');
411 $post = array('hub.mode' => $mode,
412 'hub.callback' => $callback,
413 'hub.verify' => 'async', // TODO: deprecated, remove when noone uses PuSH <0.4 (only 'async' method used there)
414 'hub.verify_token' => 'Deprecated-since-PuSH-0.4', // TODO: rm!
416 'hub.lease_seconds' => 2592000, // 3600*24*30, request approximately month long lease (may be changed by hub)
417 'hub.secret' => $this->secret,
418 'hub.topic' => $this->getUri());
419 $client = new HTTPClient();
421 $hub = $this->huburi;
423 if (common_config('feedsub', 'fallback_hub')) {
424 $hub = common_config('feedsub', 'fallback_hub');
425 if (common_config('feedsub', 'hub_user')) {
426 $u = common_config('feedsub', 'hub_user');
427 $p = common_config('feedsub', 'hub_pass');
428 $client->setAuth($u, $p);
431 throw new FeedSubException('Server could not find a usable WebSub hub.');
434 $response = $client->post($hub, $headers, $post);
435 $status = $response->getStatus();
436 // WebSub specificed response status code
437 if ($status == 202 || $status == 204) {
438 common_log(LOG_INFO, __METHOD__ . ': sub req ok, awaiting verification callback');
440 } else if ($status >= 200 && $status < 300) {
441 common_log(LOG_ERR, __METHOD__ . ": sub req returned unexpected HTTP $status: " . $response->getBody());
442 $msg = sprintf(_m("Unexpected HTTP status: %d"), $status);
443 } else if ($status == 422 && !$rediscovered) {
444 // Error code regarding something wrong in the data (it seems
445 // that we're talking to a WebSub hub at least, so let's check
446 // our own data to be sure we're not mistaken somehow, which
447 // means rediscovering hub data (the boolean parameter means
448 // we avoid running this part over and over and over and over):
450 common_debug('Running ensureHub again due to 422 status, $rediscovered=='._ve($rediscovered));
451 $discoveryResult = $this->ensureHub(true);
452 common_debug('ensureHub is now done and its result was: '._ve($discoveryResult));
454 common_log(LOG_ERR, __METHOD__ . ": sub req failed with HTTP $status: " . $response->getBody());
456 } catch (Exception $e) {
457 common_log(LOG_ERR, __METHOD__ . ": error \"{$e->getMessage()}\" hitting hub {$this->huburi} subscribing to {$this->getUri()}");
459 // Reset the subscription state.
460 $orig = clone($this);
461 $this->sub_state = 'inactive';
462 $this->update($orig);
464 // Throw the Exception again.
467 throw new ServerException("{$mode} request failed" . (!is_null($msg) ? " ($msg)" : '.'));
471 * Save WebSub subscription confirmation.
472 * Sets approximate lease start and end times and finalizes state.
474 * @param int $lease_seconds provided hub.lease_seconds parameter, if given
476 public function confirmSubscribe($lease_seconds)
478 $original = clone($this);
480 $this->sub_state = 'active';
481 $this->sub_start = common_sql_date(time());
482 if ($lease_seconds > 0) {
483 $this->sub_end = common_sql_date(time() + $lease_seconds);
485 $this->sub_end = null; // Backwards compatibility to StatusNet (PuSH <0.4 supported permanent subs)
487 $this->modified = common_sql_now();
489 common_debug(__METHOD__ . ': Updating sub state and metadata for '.$this->getUri());
490 return $this->update($original);
494 * Save WebSub unsubscription confirmation.
495 * Wipes active WebSub sub info and resets state.
497 public function confirmUnsubscribe()
499 $original = clone($this);
501 // @fixme these should all be null, but DB_DataObject doesn't save null values...?????
503 $this->sub_state = '';
504 $this->sub_start = '';
506 $this->modified = common_sql_now();
508 return $this->update($original);
512 * Accept updates from a WebSub feed. If validated, this object and the
513 * feed (as a DOMDocument) will be passed to the StartFeedSubHandleFeed
514 * and EndFeedSubHandleFeed events for processing.
516 * Not guaranteed to be running in an immediate POST context; may be run
517 * from a queue handler.
519 * Side effects: the feedsub record's lastupdate field will be updated
520 * to the current time (not published time) if we got a legit update.
522 * @param string $post source of Atom or RSS feed
523 * @param string $hmac X-Hub-Signature header, if present
525 public function receive($post, $hmac)
527 common_log(LOG_INFO, sprintf(__METHOD__.': packet for %s with HMAC %s', _ve($this->getUri()), _ve($hmac)));
529 if (!in_array($this->sub_state, array('active', 'nohub'))) {
530 common_log(LOG_ERR, sprintf(__METHOD__.': ignoring WebSub for inactive feed %s (in state %s)', _ve($this->getUri()), _ve($this->sub_state)));
535 common_log(LOG_ERR, __METHOD__ . ": ignoring empty post");
540 if (!$this->validatePushSig($post, $hmac)) {
541 // Per spec we silently drop input with a bad sig,
542 // while reporting receipt to the server.
546 $this->receiveFeed($post);
548 } catch (FeedSubBadPushSignatureException $e) {
549 // We got a signature, so something could be wrong. Let's check to see if
550 // maybe upstream has switched to another hub. Let's fetch feed and then
551 // compare rel="hub" with $this->huburi, which is done in $this->ensureHub()
553 $this->ensureHub(true);
558 * All our feed URIs should be URLs.
560 public function importFeed()
562 $feed_url = $this->getUri();
566 common_log(LOG_INFO, sprintf('Importing feed backlog from %s', $feed_url));
567 $feed_xml = HTTPClient::quickGet($feed_url, 'application/atom+xml');
568 } catch (Exception $e) {
569 throw new FeedSubException("Could not fetch feed from URL '%s': %s (%d).\n", $feed_url, $e->getMessage(), $e->getCode());
572 return $this->receiveFeed($feed_xml);
575 protected function receiveFeed($feed_xml)
577 // We're passed the XML for the Atom feed as $feed_xml,
578 // so read it into a DOMDocument and process.
579 $feed = new DOMDocument();
580 if (!$feed->loadXML($feed_xml)) {
581 // @fixme might help to include the err message
582 common_log(LOG_ERR, __METHOD__ . ": ignoring invalid XML");
586 $orig = clone($this);
587 $this->last_update = common_sql_now();
588 $this->update($orig);
590 Event::handle('StartFeedSubReceive', array($this, $feed));
591 Event::handle('EndFeedSubReceive', array($this, $feed));
595 * Validate the given Atom chunk and HMAC signature against our
596 * shared secret that was set up at subscription time.
598 * If we don't have a shared secret, there should be no signature.
599 * If we do, our calculated HMAC should match theirs.
601 * @param string $post raw XML source as POSTed to us
602 * @param string $hmac X-Hub-Signature HTTP header value, or empty
603 * @return boolean true for a match
605 protected function validatePushSig($post, $hmac)
608 // {3,16} because shortest hash algorithm name is 3 characters (md2,md4,md5) and longest
609 // is currently 11 characters, but we'll leave some margin in the end...
610 if (preg_match('/^([0-9a-zA-Z\-\,]{3,16})=([0-9a-fA-F]+)$/', $hmac, $matches)) {
611 $hash_algo = strtolower($matches[1]);
612 $their_hmac = strtolower($matches[2]);
613 common_debug(sprintf(__METHOD__ . ': WebSub push from feed %s uses HMAC algorithm %s with value: %s', _ve($this->getUri()), _ve($hash_algo), _ve($their_hmac)));
615 if (!in_array($hash_algo, hash_algos())) {
616 // 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)
617 common_log(LOG_ERR, sprintf(__METHOD__.': HMAC algorithm %s unsupported, not found in PHP hash_algos()', _ve($hash_algo)));
619 } elseif (!is_null(common_config('security', 'hash_algos')) && !in_array($hash_algo, common_config('security', 'hash_algos'))) {
620 // We _won't_ handle this because there is a list of accepted hash algorithms and this one is not in it.
621 common_log(LOG_ERR, sprintf(__METHOD__.': Whitelist for HMAC algorithms exist, but %s is not included.', _ve($hash_algo)));
625 $our_hmac = hash_hmac($hash_algo, $post, $this->secret);
626 if ($their_hmac !== $our_hmac) {
627 common_log(LOG_ERR, sprintf(__METHOD__.': ignoring WebSub 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)));
628 throw new FeedSubBadPushSignatureException('Incoming WebSub push signature did not match expected HMAC hash.');
633 common_log(LOG_ERR, sprintf(__METHOD__.': ignoring WebSub push with bogus HMAC==', _ve($hmac)));
639 common_log(LOG_ERR, sprintf(__METHOD__.': ignoring WebSub push with unexpected HMAC==%s', _ve($hmac)));
645 public function delete($useWhere=false)
648 $oprofile = Ostatus_profile::getKV('feeduri', $this->getUri());
649 if ($oprofile instanceof Ostatus_profile) {
650 // Check if there's a profile. If not, handle the NoProfileException below
651 $profile = $oprofile->localProfile();
653 } catch (NoProfileException $e) {
654 // If the Ostatus_profile has no local Profile bound to it, let's clean it out at the same time
656 } catch (NoUriException $e) {
657 // FeedSub->getUri() can throw a NoUriException, let's just go ahead and delete it
659 return parent::delete($useWhere);