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');
63 $this->subunsub($mode);
66 throw new ClientException("Publishing outside feeds not supported.", 400);
68 throw new ClientException("Unrecognized mode '$mode'.", 400);
73 * Process a request for a new or modified PuSH feed subscription.
74 * If asynchronous verification is requested, updates won't be saved immediately.
77 * 202 Accepted - request saved and awaiting verification
78 * 204 No Content - already subscribed
79 * 400 Bad Request - rejecting this (not specifically spec'd)
81 function subunsub($mode)
83 $callback = $this->argUrl('hub.callback');
85 $topic = $this->argUrl('hub.topic');
86 if (!$this->recognizedFeed($topic)) {
87 throw new ClientException("Unsupported hub.topic $topic; this hub only serves local user and group Atom feeds.");
90 $verify = $this->arg('hub.verify'); // @fixme may be multiple
91 if ($verify != 'sync' && $verify != 'async') {
92 throw new ClientException("Invalid hub.verify $verify; must be sync or async.");
95 $lease = $this->arg('hub.lease_seconds', null);
96 if ($mode == 'subscribe' && $lease != '' && !preg_match('/^\d+$/', $lease)) {
97 throw new ClientException("Invalid hub.lease $lease; must be empty or positive integer.");
100 $token = $this->arg('hub.verify_token', null);
102 $secret = $this->arg('hub.secret', null);
103 if ($secret != '' && strlen($secret) >= 200) {
104 throw new ClientException("Invalid hub.secret $secret; must be under 200 bytes.");
107 $sub = HubSub::staticGet($topic, $callback);
109 // Creating a new one!
111 $sub->topic = $topic;
112 $sub->callback = $callback;
114 if ($mode == 'subscribe') {
116 $sub->secret = $secret;
119 $sub->setLease(intval($lease));
123 if (!common_config('queue', 'enabled')) {
124 // Won't be able to background it.
127 if ($verify == 'async') {
128 $sub->scheduleVerify($mode, $token);
129 header('HTTP/1.1 202 Accepted');
131 $sub->verify($mode, $token);
132 header('HTTP/1.1 204 No Content');
137 * Check whether the given URL represents one of our canonical
138 * user or group Atom feeds.
140 * @param string $feed URL
141 * @return boolean true if it matches
143 function recognizedFeed($feed)
146 if (preg_match('!/(\d+)\.atom$!', $feed, $matches)) {
148 $params = array('id' => $id, 'format' => 'atom');
149 $userFeed = common_local_url('ApiTimelineUser', $params);
150 $groupFeed = common_local_url('ApiTimelineGroup', $params);
152 if ($feed == $userFeed) {
153 $user = User::staticGet('id', $id);
155 throw new ClientException("Invalid hub.topic $feed; user doesn't exist.");
160 if ($feed == $groupFeed) {
161 $user = User_group::staticGet('id', $id);
163 throw new ClientException("Invalid hub.topic $feed; group doesn't exist.");
168 common_log(LOG_DEBUG, "Not a user or group feed? $feed $userFeed $groupFeed");
170 common_log(LOG_DEBUG, "LOST $feed");
175 * Grab and validate a URL from POST parameters.
176 * @throws ClientException for malformed or non-http/https URLs
178 protected function argUrl($arg)
180 $url = $this->arg($arg);
181 $params = array('domain_check' => false, // otherwise breaks my local tests :P
182 'allowed_schemes' => array('http', 'https'));
183 if (Validate::uri($url, $params)) {
186 throw new ClientException("Invalid URL passed for $arg: '$url'");
191 * Get HubSub subscription record for a given feed & subscriber.
193 * @param string $feed
194 * @param string $callback
195 * @return mixed HubSub or false
197 protected function getSub($feed, $callback)
199 return HubSub::staticGet($feed, $callback);