2010-02-09 04:06:03 +09:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* StatusNet - the 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Integrated PuSH hub; lets us only ping them what need it.
|
|
|
|
* @package Hub
|
|
|
|
* @maintainer Brion Vibber <brion@status.net>
|
|
|
|
*/
|
|
|
|
|
2010-10-09 02:42:59 +09:00
|
|
|
if (!defined('STATUSNET')) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2010-02-09 04:06:03 +09:00
|
|
|
/**
|
|
|
|
|
|
|
|
|
|
|
|
Things to consider...
|
|
|
|
* should we purge incomplete subscriptions that never get a verification pingback?
|
|
|
|
* when can we send subscription renewal checks?
|
|
|
|
- at next send time probably ok
|
|
|
|
* when can we handle trimming of subscriptions?
|
|
|
|
- at next send time probably ok
|
|
|
|
* should we keep a fail count?
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2010-02-09 07:06:36 +09:00
|
|
|
class PushHubAction extends Action
|
2010-02-09 04:06:03 +09:00
|
|
|
{
|
|
|
|
function arg($arg, $def=null)
|
|
|
|
{
|
|
|
|
// PHP converts '.'s in incoming var names to '_'s.
|
|
|
|
// It also merges multiple values, which'll break hub.verify and hub.topic for publishing
|
|
|
|
// @fixme handle multiple args
|
2010-02-17 07:03:24 +09:00
|
|
|
$arg = str_replace('hub.', 'hub_', $arg);
|
2010-02-09 04:06:03 +09:00
|
|
|
return parent::arg($arg, $def);
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepare($args)
|
|
|
|
{
|
|
|
|
StatusNet::setApi(true); // reduce exception reports to aid in debugging
|
|
|
|
return parent::prepare($args);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handle()
|
|
|
|
{
|
|
|
|
$mode = $this->trimmed('hub.mode');
|
|
|
|
switch ($mode) {
|
|
|
|
case "subscribe":
|
|
|
|
case "unsubscribe":
|
2010-02-22 07:46:26 +09:00
|
|
|
$this->subunsub($mode);
|
2010-02-09 04:06:03 +09:00
|
|
|
break;
|
|
|
|
case "publish":
|
2010-09-19 22:17:36 +09:00
|
|
|
// TRANS: Client exception.
|
|
|
|
throw new ClientException(_m('Publishing outside feeds not supported.'), 400);
|
2010-02-09 04:06:03 +09:00
|
|
|
default:
|
2010-09-19 22:17:36 +09:00
|
|
|
// TRANS: Client exception. %s is a mode.
|
|
|
|
throw new ClientException(sprintf(_m('Unrecognized mode "%s".'),$mode), 400);
|
2010-02-09 04:06:03 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-22 07:46:26 +09:00
|
|
|
* Process a request for a new or modified PuSH feed subscription.
|
|
|
|
* If asynchronous verification is requested, updates won't be saved immediately.
|
2010-02-09 04:06:03 +09:00
|
|
|
*
|
|
|
|
* HTTP return codes:
|
|
|
|
* 202 Accepted - request saved and awaiting verification
|
|
|
|
* 204 No Content - already subscribed
|
2010-02-22 07:46:26 +09:00
|
|
|
* 400 Bad Request - rejecting this (not specifically spec'd)
|
2010-02-09 04:06:03 +09:00
|
|
|
*/
|
2010-02-22 07:46:26 +09:00
|
|
|
function subunsub($mode)
|
2010-02-09 04:06:03 +09:00
|
|
|
{
|
|
|
|
$callback = $this->argUrl('hub.callback');
|
|
|
|
|
2010-02-22 07:46:26 +09:00
|
|
|
$topic = $this->argUrl('hub.topic');
|
|
|
|
if (!$this->recognizedFeed($topic)) {
|
2010-09-19 22:17:36 +09:00
|
|
|
// TRANS: Client exception. %s is a topic.
|
|
|
|
throw new ClientException(sprintf(_m('Unsupported hub.topic %s this hub only serves local user and group Atom feeds.'),$topic));
|
2010-02-09 04:06:03 +09:00
|
|
|
}
|
|
|
|
|
2010-02-22 07:46:26 +09:00
|
|
|
$verify = $this->arg('hub.verify'); // @fixme may be multiple
|
|
|
|
if ($verify != 'sync' && $verify != 'async') {
|
2010-09-19 22:17:36 +09:00
|
|
|
// TRANS: Client exception.
|
|
|
|
throw new ClientException(sprintf(_m('Invalid hub.verify "%s". It must be sync or async.'),$verify));
|
2010-02-17 07:03:24 +09:00
|
|
|
}
|
2010-02-09 04:06:03 +09:00
|
|
|
|
2010-02-22 07:46:26 +09:00
|
|
|
$lease = $this->arg('hub.lease_seconds', null);
|
|
|
|
if ($mode == 'subscribe' && $lease != '' && !preg_match('/^\d+$/', $lease)) {
|
2010-09-19 22:17:36 +09:00
|
|
|
// TRANS: Client exception.
|
|
|
|
throw new ClientException(sprintf(_m('Invalid hub.lease "%s". It must be empty or positive integer.'),$lease));
|
2010-02-22 07:46:26 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
$token = $this->arg('hub.verify_token', null);
|
2010-02-09 04:06:03 +09:00
|
|
|
|
2010-02-22 07:46:26 +09:00
|
|
|
$secret = $this->arg('hub.secret', null);
|
|
|
|
if ($secret != '' && strlen($secret) >= 200) {
|
2010-09-19 22:17:36 +09:00
|
|
|
// TRANS: Client exception.
|
|
|
|
throw new ClientException(sprintf(_m('Invalid hub.secret "%s". It must be under 200 bytes.'),$secret));
|
2010-02-09 04:06:03 +09:00
|
|
|
}
|
|
|
|
|
2010-03-02 11:46:34 +09:00
|
|
|
$sub = HubSub::staticGet($topic, $callback);
|
2010-02-22 07:46:26 +09:00
|
|
|
if (!$sub) {
|
|
|
|
// Creating a new one!
|
|
|
|
$sub = new HubSub();
|
|
|
|
$sub->topic = $topic;
|
|
|
|
$sub->callback = $callback;
|
|
|
|
}
|
|
|
|
if ($mode == 'subscribe') {
|
|
|
|
if ($secret) {
|
|
|
|
$sub->secret = $secret;
|
|
|
|
}
|
|
|
|
if ($lease) {
|
|
|
|
$sub->setLease(intval($lease));
|
|
|
|
}
|
|
|
|
}
|
2010-02-09 04:06:03 +09:00
|
|
|
|
2010-02-22 07:46:26 +09:00
|
|
|
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 {
|
|
|
|
$sub->verify($mode, $token);
|
|
|
|
header('HTTP/1.1 204 No Content');
|
|
|
|
}
|
2010-02-09 04:06:03 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-22 07:46:26 +09:00
|
|
|
* Check whether the given URL represents one of our canonical
|
|
|
|
* user or group Atom feeds.
|
2010-02-19 03:20:48 +09:00
|
|
|
*
|
2010-02-22 07:46:26 +09:00
|
|
|
* @param string $feed URL
|
|
|
|
* @return boolean true if it matches
|
2010-02-09 04:06:03 +09:00
|
|
|
*/
|
2010-02-22 07:46:26 +09:00
|
|
|
function recognizedFeed($feed)
|
2010-02-09 04:06:03 +09:00
|
|
|
{
|
2010-02-22 07:46:26 +09:00
|
|
|
$matches = array();
|
|
|
|
if (preg_match('!/(\d+)\.atom$!', $feed, $matches)) {
|
|
|
|
$id = $matches[1];
|
|
|
|
$params = array('id' => $id, 'format' => 'atom');
|
|
|
|
$userFeed = common_local_url('ApiTimelineUser', $params);
|
|
|
|
$groupFeed = common_local_url('ApiTimelineGroup', $params);
|
|
|
|
|
|
|
|
if ($feed == $userFeed) {
|
|
|
|
$user = User::staticGet('id', $id);
|
|
|
|
if (!$user) {
|
2010-09-19 22:17:36 +09:00
|
|
|
// TRANS: Client exception.
|
|
|
|
throw new ClientException(sprintt(_m('Invalid hub.topic "%s". User doesn\'t exist.'),$feed));
|
2010-02-22 07:46:26 +09:00
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2010-02-09 04:06:03 +09:00
|
|
|
}
|
2010-02-22 07:46:26 +09:00
|
|
|
if ($feed == $groupFeed) {
|
|
|
|
$user = User_group::staticGet('id', $id);
|
|
|
|
if (!$user) {
|
2010-09-19 22:17:36 +09:00
|
|
|
// TRANS: Client exception.
|
|
|
|
throw new ClientException(sprintf(_m('Invalid hub.topic "%s". Group doesn\'t exist.'),$feed));
|
2010-02-22 07:46:26 +09:00
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2011-03-07 04:15:34 +09:00
|
|
|
} else if (preg_match('!/(\d+)/lists/(\d+)/statuses\.atom$!', $feed, $matches)) {
|
|
|
|
$user = $matches[1];
|
|
|
|
$id = $matches[2];
|
|
|
|
$params = array('user' => $user, 'id' => $id, 'format' => 'atom');
|
|
|
|
$listFeed = common_local_url('ApiTimelineList', $params);
|
|
|
|
|
|
|
|
if ($feed == $listFeed) {
|
|
|
|
$list = Profile_list::staticGet('id', $id);
|
|
|
|
$user = User::staticGet('id', $user);
|
|
|
|
if (!$list || !$user || $list->tagger != $user->id) {
|
|
|
|
throw new ClientException("Invalid hub.topic $feed; people tag doesn't exist.");
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
common_log(LOG_DEBUG, "Not a user, group or people tag feed? $feed $userFeed $groupFeed $listFeed");
|
2010-02-09 04:06:03 +09:00
|
|
|
}
|
2010-02-22 07:46:26 +09:00
|
|
|
common_log(LOG_DEBUG, "LOST $feed");
|
|
|
|
return false;
|
2010-02-09 04:06:03 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grab and validate a URL from POST parameters.
|
2010-02-22 07:46:26 +09:00
|
|
|
* @throws ClientException for malformed or non-http/https URLs
|
2010-02-09 04:06:03 +09:00
|
|
|
*/
|
|
|
|
protected function argUrl($arg)
|
|
|
|
{
|
|
|
|
$url = $this->arg($arg);
|
|
|
|
$params = array('domain_check' => false, // otherwise breaks my local tests :P
|
|
|
|
'allowed_schemes' => array('http', 'https'));
|
|
|
|
if (Validate::uri($url, $params)) {
|
|
|
|
return $url;
|
|
|
|
} else {
|
2010-09-19 22:17:36 +09:00
|
|
|
// TRANS: Client exception.
|
|
|
|
// TRANS: %1$s is this argument to the method this exception occurs in, %2$s is a URL.
|
|
|
|
throw new ClientException(sprintf(_m('Invalid URL passed for %1$s: "%2$s"'),$arg,$url));
|
2010-02-09 04:06:03 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get HubSub subscription record for a given feed & subscriber.
|
|
|
|
*
|
|
|
|
* @param string $feed
|
|
|
|
* @param string $callback
|
|
|
|
* @return mixed HubSub or false
|
|
|
|
*/
|
|
|
|
protected function getSub($feed, $callback)
|
|
|
|
{
|
|
|
|
return HubSub::staticGet($feed, $callback);
|
|
|
|
}
|
|
|
|
}
|