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?
39 class PushHubAction extends Action
41 function arg($arg, $def=null)
43 // PHP converts '.'s in incoming var names to '_'s.
44 // It also merges multiple values, which'll break hub.verify and hub.topic for publishing
45 // @fixme handle multiple args
46 $arg = str_replace('hub.', 'hub_', $arg);
47 return parent::arg($arg, $def);
50 function prepare($args)
52 StatusNet::setApi(true); // reduce exception reports to aid in debugging
53 return parent::prepare($args);
58 $mode = $this->trimmed('hub.mode');
62 $this->subunsub($mode);
65 // @todo i18n FIXME: added i18n and use sprintf when using parameters.
66 throw new ClientException("Publishing outside feeds not supported.", 400);
68 // @todo i18n FIXME: added i18n and use sprintf when using parameters.
69 throw new ClientException("Unrecognized mode '$mode'.", 400);
74 * Process a request for a new or modified PuSH feed subscription.
75 * If asynchronous verification is requested, updates won't be saved immediately.
78 * 202 Accepted - request saved and awaiting verification
79 * 204 No Content - already subscribed
80 * 400 Bad Request - rejecting this (not specifically spec'd)
82 function subunsub($mode)
84 $callback = $this->argUrl('hub.callback');
86 $topic = $this->argUrl('hub.topic');
87 if (!$this->recognizedFeed($topic)) {
88 // @todo i18n FIXME: added i18n and use sprintf when using parameters.
89 throw new ClientException("Unsupported hub.topic $topic; this hub only serves local user and group Atom feeds.");
92 $verify = $this->arg('hub.verify'); // @fixme may be multiple
93 if ($verify != 'sync' && $verify != 'async') {
94 // @todo i18n FIXME: added i18n and use sprintf when using parameters.
95 throw new ClientException("Invalid hub.verify $verify; must be sync or async.");
98 $lease = $this->arg('hub.lease_seconds', null);
99 if ($mode == 'subscribe' && $lease != '' && !preg_match('/^\d+$/', $lease)) {
100 // @todo i18n FIXME: added i18n and use sprintf when using parameters.
101 throw new ClientException("Invalid hub.lease $lease; must be empty or positive integer.");
104 $token = $this->arg('hub.verify_token', null);
106 $secret = $this->arg('hub.secret', null);
107 if ($secret != '' && strlen($secret) >= 200) {
108 // @todo i18n FIXME: added i18n and use sprintf when using parameters.
109 throw new ClientException("Invalid hub.secret $secret; must be under 200 bytes.");
112 $sub = HubSub::staticGet($topic, $callback);
114 // Creating a new one!
116 $sub->topic = $topic;
117 $sub->callback = $callback;
119 if ($mode == 'subscribe') {
121 $sub->secret = $secret;
124 $sub->setLease(intval($lease));
128 if (!common_config('queue', 'enabled')) {
129 // Won't be able to background it.
132 if ($verify == 'async') {
133 $sub->scheduleVerify($mode, $token);
134 header('HTTP/1.1 202 Accepted');
136 $sub->verify($mode, $token);
137 header('HTTP/1.1 204 No Content');
142 * Check whether the given URL represents one of our canonical
143 * user or group Atom feeds.
145 * @param string $feed URL
146 * @return boolean true if it matches
148 function recognizedFeed($feed)
151 if (preg_match('!/(\d+)\.atom$!', $feed, $matches)) {
153 $params = array('id' => $id, 'format' => 'atom');
154 $userFeed = common_local_url('ApiTimelineUser', $params);
155 $groupFeed = common_local_url('ApiTimelineGroup', $params);
157 if ($feed == $userFeed) {
158 $user = User::staticGet('id', $id);
160 // @todo i18n FIXME: added i18n and use sprintf when using parameters.
161 throw new ClientException("Invalid hub.topic $feed; user doesn't exist.");
166 if ($feed == $groupFeed) {
167 $user = User_group::staticGet('id', $id);
169 // @todo i18n FIXME: added i18n and use sprintf when using parameters.
170 throw new ClientException("Invalid hub.topic $feed; group doesn't exist.");
175 common_log(LOG_DEBUG, "Not a user or group feed? $feed $userFeed $groupFeed");
177 common_log(LOG_DEBUG, "LOST $feed");
182 * Grab and validate a URL from POST parameters.
183 * @throws ClientException for malformed or non-http/https URLs
185 protected function argUrl($arg)
187 $url = $this->arg($arg);
188 $params = array('domain_check' => false, // otherwise breaks my local tests :P
189 'allowed_schemes' => array('http', 'https'));
190 if (Validate::uri($url, $params)) {
193 // @todo i18n FIXME: added i18n and use sprintf when using parameters.
194 throw new ClientException("Invalid URL passed for $arg: '$url'");
199 * Get HubSub subscription record for a given feed & subscriber.
201 * @param string $feed
202 * @param string $callback
203 * @return mixed HubSub or false
205 protected function getSub($feed, $callback)
207 return HubSub::staticGet($feed, $callback);