2010-02-08 19:06:03 +00: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-08 18:42:59 +01:00
|
|
|
if (!defined('STATUSNET')) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2010-02-08 19:06:03 +00:00
|
|
|
/**
|
2011-04-10 23:39:27 +01: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-08 22:06:36 +00:00
|
|
|
class PushHubAction extends Action
|
2010-02-08 19:06:03 +00: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-16 22:03:24 +00:00
|
|
|
$arg = str_replace('hub.', 'hub_', $arg);
|
2010-02-08 19:06:03 +00:00
|
|
|
return parent::arg($arg, $def);
|
|
|
|
}
|
|
|
|
|
2013-10-29 09:26:46 +00:00
|
|
|
protected function prepare(array $args=array())
|
2010-02-08 19:06:03 +00:00
|
|
|
{
|
2015-02-27 11:44:15 +00:00
|
|
|
GNUsocial::setApi(true); // reduce exception reports to aid in debugging
|
2010-02-08 19:06:03 +00:00
|
|
|
return parent::prepare($args);
|
|
|
|
}
|
|
|
|
|
2013-10-20 16:21:56 +01:00
|
|
|
protected function handle()
|
2010-02-08 19:06:03 +00:00
|
|
|
{
|
|
|
|
$mode = $this->trimmed('hub.mode');
|
|
|
|
switch ($mode) {
|
|
|
|
case "subscribe":
|
|
|
|
case "unsubscribe":
|
2010-02-21 22:46:26 +00:00
|
|
|
$this->subunsub($mode);
|
2010-02-08 19:06:03 +00:00
|
|
|
break;
|
|
|
|
case "publish":
|
2010-09-19 14:17:36 +01:00
|
|
|
// TRANS: Client exception.
|
|
|
|
throw new ClientException(_m('Publishing outside feeds not supported.'), 400);
|
2010-02-08 19:06:03 +00:00
|
|
|
default:
|
2010-09-19 14:17:36 +01:00
|
|
|
// TRANS: Client exception. %s is a mode.
|
|
|
|
throw new ClientException(sprintf(_m('Unrecognized mode "%s".'),$mode), 400);
|
2010-02-08 19:06:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-21 22:46:26 +00: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-08 19:06:03 +00:00
|
|
|
*
|
|
|
|
* HTTP return codes:
|
|
|
|
* 202 Accepted - request saved and awaiting verification
|
|
|
|
* 204 No Content - already subscribed
|
2010-02-21 22:46:26 +00:00
|
|
|
* 400 Bad Request - rejecting this (not specifically spec'd)
|
2010-02-08 19:06:03 +00:00
|
|
|
*/
|
2010-02-21 22:46:26 +00:00
|
|
|
function subunsub($mode)
|
2010-02-08 19:06:03 +00:00
|
|
|
{
|
|
|
|
$callback = $this->argUrl('hub.callback');
|
|
|
|
|
2016-01-11 18:54:05 +00:00
|
|
|
common_debug('New PuSH hub request ('._ve($mode).') for callback '._ve($callback));
|
2010-02-21 22:46:26 +00:00
|
|
|
$topic = $this->argUrl('hub.topic');
|
|
|
|
if (!$this->recognizedFeed($topic)) {
|
2016-01-11 18:54:05 +00:00
|
|
|
common_debug('PuSH hub request had unrecognized feed topic=='._ve($topic));
|
2010-09-19 14:17:36 +01: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-08 19:06:03 +00:00
|
|
|
}
|
|
|
|
|
2010-02-21 22:46:26 +00:00
|
|
|
$lease = $this->arg('hub.lease_seconds', null);
|
|
|
|
if ($mode == 'subscribe' && $lease != '' && !preg_match('/^\d+$/', $lease)) {
|
2016-01-11 18:54:05 +00:00
|
|
|
common_debug('PuSH hub request had invalid lease_seconds=='._ve($lease));
|
2011-04-10 23:39:27 +01:00
|
|
|
// TRANS: Client exception. %s is the invalid lease value.
|
2010-09-19 14:17:36 +01:00
|
|
|
throw new ClientException(sprintf(_m('Invalid hub.lease "%s". It must be empty or positive integer.'),$lease));
|
2010-02-21 22:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$secret = $this->arg('hub.secret', null);
|
|
|
|
if ($secret != '' && strlen($secret) >= 200) {
|
2016-01-11 18:54:05 +00:00
|
|
|
common_debug('PuSH hub request had invalid secret=='._ve($secret));
|
2011-04-10 23:39:27 +01:00
|
|
|
// TRANS: Client exception. %s is the invalid hub secret.
|
2010-09-19 14:17:36 +01:00
|
|
|
throw new ClientException(sprintf(_m('Invalid hub.secret "%s". It must be under 200 bytes.'),$secret));
|
2010-02-08 19:06:03 +00:00
|
|
|
}
|
|
|
|
|
2013-08-21 10:25:08 +01:00
|
|
|
$sub = HubSub::getByHashkey($topic, $callback);
|
2013-11-02 19:02:28 +00:00
|
|
|
if (!$sub instanceof HubSub) {
|
2010-02-21 22:46:26 +00:00
|
|
|
// Creating a new one!
|
2016-01-11 18:54:05 +00:00
|
|
|
common_debug('PuSH creating new HubSub entry for topic=='._ve($topic).' to remote callback '._ve($callback));
|
2010-02-21 22:46:26 +00:00
|
|
|
$sub = new HubSub();
|
|
|
|
$sub->topic = $topic;
|
|
|
|
$sub->callback = $callback;
|
|
|
|
}
|
|
|
|
if ($mode == 'subscribe') {
|
|
|
|
if ($secret) {
|
|
|
|
$sub->secret = $secret;
|
|
|
|
}
|
|
|
|
if ($lease) {
|
|
|
|
$sub->setLease(intval($lease));
|
|
|
|
}
|
|
|
|
}
|
2016-01-11 18:54:05 +00:00
|
|
|
common_debug('PuSH hub request is now:'._ve($sub));
|
2010-02-08 19:06:03 +00:00
|
|
|
|
2013-11-02 19:02:28 +00:00
|
|
|
$verify = $this->arg('hub.verify'); // TODO: deprecated
|
|
|
|
$token = $this->arg('hub.verify_token', null); // TODO: deprecated
|
|
|
|
if ($verify == 'sync') { // pre-0.4 PuSH
|
2010-02-21 22:46:26 +00:00
|
|
|
$sub->verify($mode, $token);
|
|
|
|
header('HTTP/1.1 204 No Content');
|
2013-11-02 19:02:28 +00:00
|
|
|
} 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');
|
2010-02-21 22:46:26 +00:00
|
|
|
}
|
2010-02-08 19:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-21 22:46:26 +00:00
|
|
|
* Check whether the given URL represents one of our canonical
|
|
|
|
* user or group Atom feeds.
|
2010-02-18 18:20:48 +00:00
|
|
|
*
|
2010-02-21 22:46:26 +00:00
|
|
|
* @param string $feed URL
|
2015-02-08 10:47:15 +00:00
|
|
|
* @return boolean true if it matches, false if not a recognized local feed
|
|
|
|
* @throws exception if local entity does not exist
|
2010-02-08 19:06:03 +00:00
|
|
|
*/
|
2015-02-08 10:47:15 +00:00
|
|
|
protected function recognizedFeed($feed)
|
2010-02-08 19:06:03 +00:00
|
|
|
{
|
2010-02-21 22:46:26 +00:00
|
|
|
$matches = array();
|
2015-02-08 10:47:15 +00:00
|
|
|
// Simple mapping to local ID for user or group
|
2010-02-21 22:46:26 +00:00
|
|
|
if (preg_match('!/(\d+)\.atom$!', $feed, $matches)) {
|
|
|
|
$id = $matches[1];
|
|
|
|
$params = array('id' => $id, 'format' => 'atom');
|
|
|
|
|
2015-02-08 10:47:15 +00:00
|
|
|
// Double-check against locally generated URLs
|
|
|
|
switch ($feed) {
|
|
|
|
case common_local_url('ApiTimelineUser', $params):
|
2013-08-18 12:04:58 +01:00
|
|
|
$user = User::getKV('id', $id);
|
2015-02-08 10:47:15 +00:00
|
|
|
if (!$user instanceof User) {
|
2011-04-10 23:39:27 +01:00
|
|
|
// TRANS: Client exception. %s is a feed URL.
|
2015-02-08 10:47:15 +00:00
|
|
|
throw new ClientException(sprintf(_m('Invalid hub.topic "%s". User does not exist.'),$feed));
|
2010-02-21 22:46:26 +00:00
|
|
|
}
|
2015-02-08 10:47:15 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case common_local_url('ApiTimelineGroup', $params):
|
|
|
|
$group = Local_group::getKV('group_id', $id);
|
|
|
|
if (!$group instanceof Local_group) {
|
2011-04-10 23:39:27 +01:00
|
|
|
// TRANS: Client exception. %s is a feed URL.
|
2015-02-08 10:47:15 +00:00
|
|
|
throw new ClientException(sprintf(_m('Invalid hub.topic "%s". Local_group does not exist.'),$feed));
|
2010-02-21 22:46:26 +00:00
|
|
|
}
|
2015-02-08 10:47:15 +00:00
|
|
|
return true;
|
2010-02-21 22:46:26 +00:00
|
|
|
}
|
2015-02-08 10:47:15 +00:00
|
|
|
common_debug("Feed was not recognized by any local User or Group Atom feed URLs: {$feed}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Profile lists are unique per user, so we need both IDs
|
|
|
|
if (preg_match('!/(\d+)/lists/(\d+)/statuses\.atom$!', $feed, $matches)) {
|
2011-03-06 19:15:34 +00:00
|
|
|
$user = $matches[1];
|
|
|
|
$id = $matches[2];
|
|
|
|
$params = array('user' => $user, 'id' => $id, 'format' => 'atom');
|
|
|
|
|
2015-02-08 10:47:15 +00:00
|
|
|
// Double-check against locally generated URLs
|
|
|
|
switch ($feed) {
|
|
|
|
case common_local_url('ApiTimelineList', $params):
|
2013-08-18 12:04:58 +01:00
|
|
|
$list = Profile_list::getKV('id', $id);
|
|
|
|
$user = User::getKV('id', $user);
|
2015-02-08 10:47:15 +00:00
|
|
|
if (!$list instanceof Profile_list || !$user instanceof User || $list->tagger != $user->id) {
|
2011-04-10 23:39:27 +01:00
|
|
|
// TRANS: Client exception. %s is a feed URL.
|
2011-04-17 19:08:03 +01:00
|
|
|
throw new ClientException(sprintf(_m('Invalid hub.topic %s; list does not exist.'),$feed));
|
2011-03-06 19:15:34 +00:00
|
|
|
}
|
2015-02-08 10:47:15 +00:00
|
|
|
return true;
|
2011-03-06 19:15:34 +00:00
|
|
|
}
|
2015-02-08 10:47:15 +00:00
|
|
|
common_debug("Feed was not recognized by any local Profile_list Atom feed URL: {$feed}");
|
|
|
|
return false;
|
2010-02-08 19:06:03 +00:00
|
|
|
}
|
2015-02-08 10:47:15 +00:00
|
|
|
|
|
|
|
common_debug("Unknown feed URL structure, can't match against local user, group or profile_list: {$feed}");
|
2010-02-21 22:46:26 +00:00
|
|
|
return false;
|
2010-02-08 19:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grab and validate a URL from POST parameters.
|
2017-04-26 21:41:59 +01:00
|
|
|
* @throws ClientException for malformed or non-http/https or blacklisted URLs
|
2010-02-08 19:06:03 +00: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'));
|
2015-11-08 09:33:41 +00:00
|
|
|
$validate = new Validate();
|
2017-04-26 21:41:59 +01:00
|
|
|
if (!$validate->uri($url, $params)) {
|
2010-09-19 14:17:36 +01: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-08 19:06:03 +00:00
|
|
|
}
|
2017-04-26 21:41:59 +01:00
|
|
|
|
|
|
|
Event::handle('UrlBlacklistTest', array($url));
|
|
|
|
return $url;
|
2010-02-08 19:06:03 +00: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)
|
|
|
|
{
|
2013-08-21 10:25:08 +01:00
|
|
|
return HubSub::getByHashkey($feed, $callback);
|
2010-02-08 19:06:03 +00:00
|
|
|
}
|
|
|
|
}
|