3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 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/>.
21 * Integrated PuSH hub; lets us only ping them what need it.
23 * @maintainer Brion Vibber <brion@status.net>
30 * should we purge incomplete subscriptions that never get a verification pingback?
31 * when can we send subscription renewal checks?
32 - at next send time probably ok
33 * when can we handle trimming of subscriptions?
34 - at next send time probably ok
35 * should we keep a fail count?
40 class PushHubAction extends Action
42 function arg($arg, $def=null)
44 // PHP converts '.'s in incoming var names to '_'s.
45 // It also merges multiple values, which'll break hub.verify and hub.topic for publishing
46 // @fixme handle multiple args
47 $arg = str_replace('hub.', 'hub_', $arg);
48 return parent::arg($arg, $def);
51 function prepare($args)
53 StatusNet::setApi(true); // reduce exception reports to aid in debugging
54 return parent::prepare($args);
59 $mode = $this->trimmed('hub.mode');
68 throw new ServerException("Publishing outside feeds not supported.", 400);
70 throw new ServerException("Unrecognized mode '$mode'.", 400);
75 * Process a PuSH feed subscription request.
78 * 202 Accepted - request saved and awaiting verification
79 * 204 No Content - already subscribed
80 * 403 Forbidden - rejecting this (not specifically spec'd)
84 $feed = $this->argUrl('hub.topic');
85 $callback = $this->argUrl('hub.callback');
87 common_log(LOG_DEBUG, __METHOD__ . ": checking sub'd to $feed $callback");
88 if ($this->getSub($feed, $callback)) {
89 // Already subscribed; return 204 per spec.
90 header('HTTP/1.1 204 No Content');
91 common_log(LOG_DEBUG, __METHOD__ . ': already subscribed');
95 common_log(LOG_DEBUG, __METHOD__ . ': setting up');
98 $sub->callback = $callback;
99 $sub->verify_token = $this->arg('hub.verify_token', null);
100 $sub->secret = $this->arg('hub.secret', null);
101 if (strlen($sub->secret) > 200) {
102 throw new ClientException("hub.secret must be no longer than 200 chars", 400);
104 $sub->setLease(intval($this->arg('hub.lease_seconds')));
106 // @fixme check for feeds we don't manage
107 // @fixme check the verification mode, might want a return immediately?
109 common_log(LOG_DEBUG, __METHOD__ . ': inserting');
110 $ok = $sub->insert();
113 throw new ServerException("Failed to save subscription record", 500);
116 // @fixme check errors ;)
118 $data = array('sub' => $sub, 'mode' => 'subscribe');
119 $qm = QueueManager::get();
120 $qm->enqueue($data, 'hubverify');
122 header('HTTP/1.1 202 Accepted');
123 common_log(LOG_DEBUG, __METHOD__ . ': done');
127 * Process a PuSH feed unsubscription request.
130 * 202 Accepted - request saved and awaiting verification
131 * 204 No Content - already subscribed
132 * 400 Bad Request - invalid params or rejected feed
134 function unsubscribe()
136 $feed = $this->argUrl('hub.topic');
137 $callback = $this->argUrl('hub.callback');
138 $sub = $this->getSub($feed, $callback);
141 if ($sub->verify('unsubscribe')) {
143 common_log(LOG_INFO, "PuSH unsubscribed $feed for $callback");
145 throw new ServerException("Failed PuSH unsubscription: verification failed! $feed for $callback");
148 throw new ServerException("Failed PuSH unsubscription: not subscribed! $feed for $callback");
153 * Grab and validate a URL from POST parameters.
154 * @throws ServerException for malformed or non-http/https URLs
156 protected function argUrl($arg)
158 $url = $this->arg($arg);
159 $params = array('domain_check' => false, // otherwise breaks my local tests :P
160 'allowed_schemes' => array('http', 'https'));
161 if (Validate::uri($url, $params)) {
164 throw new ServerException("Invalid URL passed for $arg: '$url'", 400);
169 * Get HubSub subscription record for a given feed & subscriber.
171 * @param string $feed
172 * @param string $callback
173 * @return mixed HubSub or false
175 protected function getSub($feed, $callback)
177 return HubSub::staticGet($feed, $callback);