$mode = $this->arg('hub_mode');
$topic = $this->arg('hub_topic');
$challenge = $this->arg('hub_challenge');
- $lease_seconds = $this->arg('hub_lease_seconds');
- $verify_token = $this->arg('hub_verify_token');
- common_log(LOG_INFO, __METHOD__ . ": sub verification mode: $mode topic: $topic challenge: $challenge lease_seconds: $lease_seconds verify_token: $verify_token");
+ $lease_seconds = $this->arg('hub_lease_seconds'); // Must be >0 for PuSH 0.4!
+ common_log(LOG_INFO, __METHOD__ . ": sub verification mode: $mode topic: $topic challenge: $challenge lease_seconds: $lease_seconds");
if ($mode != 'subscribe' && $mode != 'unsubscribe') {
// TRANS: Client exception. %s is an invalid value for hub.mode.
}
$feedsub = FeedSub::getKV('uri', $topic);
- if (!$feedsub) {
+ if (!$feedsub instanceof FeedSub) {
// TRANS: Client exception. %s is an invalid feed name.
throw new ClientException(sprintf(_m('Bad hub.topic feed "%s".'),$topic), 404);
}
- if ($feedsub->verify_token !== $verify_token) {
- // TRANS: Client exception. %1$s the invalid token, %2$s is the topic for which the invalid token was given.
- throw new ClientException(sprintf(_m('Bad hub.verify_token %1$s for %2$s.'),$token,$topic), 404);
- }
-
if ($mode == 'subscribe') {
// We may get re-sub requests legitimately.
if ($feedsub->sub_state != 'subscribe' && $feedsub->sub_state != 'active') {
throw new ClientException(sprintf(_m('Unsupported hub.topic %s this hub only serves local user and group Atom feeds.'),$topic));
}
- $verify = $this->arg('hub.verify'); // @fixme may be multiple
- if ($verify != 'sync' && $verify != 'async') {
- // TRANS: Client exception. %s is sync or async.
- throw new ClientException(sprintf(_m('Invalid hub.verify "%s". It must be sync or async.'),$verify));
- }
-
$lease = $this->arg('hub.lease_seconds', null);
if ($mode == 'subscribe' && $lease != '' && !preg_match('/^\d+$/', $lease)) {
// TRANS: Client exception. %s is the invalid lease value.
throw new ClientException(sprintf(_m('Invalid hub.lease "%s". It must be empty or positive integer.'),$lease));
}
- $token = $this->arg('hub.verify_token', null);
-
$secret = $this->arg('hub.secret', null);
if ($secret != '' && strlen($secret) >= 200) {
// TRANS: Client exception. %s is the invalid hub secret.
}
$sub = HubSub::getByHashkey($topic, $callback);
- if (!$sub) {
+ if (!$sub instanceof HubSub) {
// Creating a new one!
$sub = new HubSub();
$sub->topic = $topic;
}
}
- if (!common_config('queue', 'enabled')) {
- // Won't be able to background it.
- $verify = 'sync';
- }
- if ($verify == 'async') {
- $sub->scheduleVerify($mode, $token);
- header('HTTP/1.1 202 Accepted');
- } else {
+ $verify = $this->arg('hub.verify'); // TODO: deprecated
+ $token = $this->arg('hub.verify_token', null); // TODO: deprecated
+ if ($verify == 'sync') { // pre-0.4 PuSH
$sub->verify($mode, $token);
header('HTTP/1.1 204 No Content');
+ } else { // If $verify is not "sync", we might be using PuSH 0.4
+ $sub->scheduleVerify($mode, $token); // If we were certain it's PuSH 0.4, token could be removed
+ header('HTTP/1.1 202 Accepted');
}
}
PuSH subscription flow:
$profile->subscribe()
- generate random verification token
- save to verify_token
sends a sub request to the hub...
main/push/callback
// PuSH subscription data
public $huburi;
public $secret;
- public $verify_token;
public $sub_state; // subscribe, active, unsubscribe, inactive
public $sub_start;
public $sub_end;
'id' => array('type' => 'serial', 'not null' => true, 'description' => 'FeedSub local unique id'),
'uri' => array('type' => 'varchar', 'not null' => true, 'length' => 255, 'description' => 'FeedSub uri'),
'huburi' => array('type' => 'text', 'description' => 'FeedSub hub-uri'),
- 'verify_token' => array('type' => 'text', 'description' => 'FeedSub verify-token'),
'secret' => array('type' => 'text', 'description' => 'FeedSub stored secret'),
'sub_state' => array('type' => 'enum("subscribe","active","unsubscribe","inactive")', 'not null' => true, 'description' => 'subscription state'),
'sub_start' => array('type' => 'datetime', 'description' => 'subscription start'),
* @return bool true on success, false on failure
* @throws ServerException if feed state is not valid
*/
- public function subscribe($mode='subscribe')
+ 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, "Attempting to (re)start PuSH subscription to {$this->uri} in unexpected state {$this->sub_state}");
}
if (empty($this->huburi)) {
if (common_config('feedsub', 'fallback_hub')) {
*/
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, "Attempting to (re)end PuSH subscription to {$this->uri} in unexpected state {$this->sub_state}");
}
if (empty($this->huburi)) {
if (common_config('feedsub', 'fallback_hub')) {
}
}
+ static public function renewalCheck()
+ {
+ $fs = new FeedSub();
+ // the "" empty string check is because we historically haven't saved unsubscribed feeds as NULL
+ $fs->whereAdd('sub_end IS NOT NULL AND sub_end!="" AND sub_end < NOW() - INTERVAL 1 day');
+ if ($fs->find() === false) {
+ throw new NoResultException($fs);
+ }
+ return $fs;
+ }
+
+ public function renew()
+ {
+ $this->subscribe();
+ }
+
+ /**
+ * @return boolean true on successful sub/unsub, false on failure
+ */
protected function doSubscribe($mode)
{
+ $this->query('BEGIN');
$orig = clone($this);
- $this->verify_token = common_random_hexstr(16);
if ($mode == 'subscribe') {
$this->secret = common_random_hexstr(32);
}
$headers = array('Content-Type: application/x-www-form-urlencoded');
$post = array('hub.mode' => $mode,
'hub.callback' => $callback,
- 'hub.verify' => 'sync',
- 'hub.verify_token' => $this->verify_token,
+ 'hub.verify' => 'async', // TODO: deprecated, remove when noone uses PuSH <0.4
'hub.secret' => $this->secret,
'hub.topic' => $this->uri);
$client = new HTTPClient();
$response = $client->post($hub, $headers, $post);
$status = $response->getStatus();
if ($status == 202) {
+ $this->query('COMMIT');
common_log(LOG_INFO, __METHOD__ . ': sub req ok, awaiting verification callback');
return true;
- } else if ($status == 204) {
- common_log(LOG_INFO, __METHOD__ . ': sub req ok and verified');
- return true;
} else if ($status >= 200 && $status < 300) {
common_log(LOG_ERR, __METHOD__ . ": sub req returned unexpected HTTP $status: " . $response->getBody());
- return false;
} else {
common_log(LOG_ERR, __METHOD__ . ": sub req failed with HTTP $status: " . $response->getBody());
- return false;
}
+ $this->query('ROLLBACK');
} catch (Exception $e) {
+ $this->query('ROLLBACK');
// wtf!
common_log(LOG_ERR, __METHOD__ . ": error \"{$e->getMessage()}\" hitting hub $this->huburi subscribing to $this->uri");
$orig = clone($this);
- $this->verify_token = '';
$this->sub_state = 'inactive';
$this->update($orig);
unset($orig);
-
- return false;
}
+ return false;
}
/**
*
* @param int $lease_seconds provided hub.lease_seconds parameter, if given
*/
- public function confirmSubscribe($lease_seconds=0)
+ public function confirmSubscribe($lease_seconds)
{
$original = clone($this);
if ($lease_seconds > 0) {
$this->sub_end = common_sql_date(time() + $lease_seconds);
} else {
- $this->sub_end = null;
+ $this->sub_end = null; // Backwards compatibility to StatusNet (PuSH <0.4 supported permanent subs)
}
$this->modified = common_sql_now();
$original = clone($this);
// @fixme these should all be null, but DB_DataObject doesn't save null values...?????
- $this->verify_token = '';
$this->secret = '';
$this->sub_state = '';
$this->sub_start = '';
--- /dev/null
+#!/usr/bin/env php
+<?php
+/*
+ * StatusNet - a distributed open-source microblogging tool
+ * Copyright (C) 2010, StatusNet, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
+
+$helptext = <<<END_OF_HELP
+resub-feed.php
+Resubscribe to any soon expiring feeds. Default time to expiration is 1 day.
+
+END_OF_HELP;
+
+require_once INSTALLDIR.'/scripts/commandline.inc';
+
+try {
+ $sub = FeedSub::renewalCheck();
+} catch (NoResultException $e) {
+ echo 'There were no expiring feeds.';
+ exit;
+}
+
+while ($sub->fetch()) {
+ echo "Renewing feed subscription\n\tExp.: {$sub->sub_end}\n\tFeed: {$sub->uri}\n\tHub: {$sub->huburi}\n";
+ $sub->renew();
+}
+
+echo "Done!";