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>
26 if (!defined('STATUSNET')) {
31 * Things to consider...
32 * should we purge incomplete subscriptions that never get a verification pingback?
33 * when can we send subscription renewal checks?
34 * - at next send time probably ok
35 * when can we handle trimming of subscriptions?
36 * - at next send time probably ok
37 * 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 protected function prepare(array $args=array())
52 StatusNet::setApi(true); // reduce exception reports to aid in debugging
53 return parent::prepare($args);
56 protected function handle()
58 $mode = $this->trimmed('hub.mode');
62 $this->subunsub($mode);
65 // TRANS: Client exception.
66 throw new ClientException(_m('Publishing outside feeds not supported.'), 400);
68 // TRANS: Client exception. %s is a mode.
69 throw new ClientException(sprintf(_m('Unrecognized mode "%s".'),$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 // TRANS: Client exception. %s is a topic.
89 throw new ClientException(sprintf(_m('Unsupported hub.topic %s this hub only serves local user and group Atom feeds.'),$topic));
92 $lease = $this->arg('hub.lease_seconds', null);
93 if ($mode == 'subscribe' && $lease != '' && !preg_match('/^\d+$/', $lease)) {
94 // TRANS: Client exception. %s is the invalid lease value.
95 throw new ClientException(sprintf(_m('Invalid hub.lease "%s". It must be empty or positive integer.'),$lease));
98 $secret = $this->arg('hub.secret', null);
99 if ($secret != '' && strlen($secret) >= 200) {
100 // TRANS: Client exception. %s is the invalid hub secret.
101 throw new ClientException(sprintf(_m('Invalid hub.secret "%s". It must be under 200 bytes.'),$secret));
104 $sub = HubSub::getByHashkey($topic, $callback);
105 if (!$sub instanceof HubSub) {
106 // Creating a new one!
108 $sub->topic = $topic;
109 $sub->callback = $callback;
111 if ($mode == 'subscribe') {
113 $sub->secret = $secret;
116 $sub->setLease(intval($lease));
120 $verify = $this->arg('hub.verify'); // TODO: deprecated
121 $token = $this->arg('hub.verify_token', null); // TODO: deprecated
122 if ($verify == 'sync') { // pre-0.4 PuSH
123 $sub->verify($mode, $token);
124 header('HTTP/1.1 204 No Content');
125 } else { // If $verify is not "sync", we might be using PuSH 0.4
126 $sub->scheduleVerify($mode, $token); // If we were certain it's PuSH 0.4, token could be removed
127 header('HTTP/1.1 202 Accepted');
132 * Check whether the given URL represents one of our canonical
133 * user or group Atom feeds.
135 * @param string $feed URL
136 * @return boolean true if it matches
138 function recognizedFeed($feed)
141 if (preg_match('!/(\d+)\.atom$!', $feed, $matches)) {
143 $params = array('id' => $id, 'format' => 'atom');
144 $userFeed = common_local_url('ApiTimelineUser', $params);
145 $groupFeed = common_local_url('ApiTimelineGroup', $params);
147 if ($feed == $userFeed) {
148 $user = User::getKV('id', $id);
150 // TRANS: Client exception. %s is a feed URL.
151 throw new ClientException(sprintt(_m('Invalid hub.topic "%s". User does not exist.'),$feed));
156 if ($feed == $groupFeed) {
157 $user = User_group::getKV('id', $id);
159 // TRANS: Client exception. %s is a feed URL.
160 throw new ClientException(sprintf(_m('Invalid hub.topic "%s". Group does not exist.'),$feed));
165 } else if (preg_match('!/(\d+)/lists/(\d+)/statuses\.atom$!', $feed, $matches)) {
168 $params = array('user' => $user, 'id' => $id, 'format' => 'atom');
169 $listFeed = common_local_url('ApiTimelineList', $params);
171 if ($feed == $listFeed) {
172 $list = Profile_list::getKV('id', $id);
173 $user = User::getKV('id', $user);
174 if (!$list || !$user || $list->tagger != $user->id) {
175 // TRANS: Client exception. %s is a feed URL.
176 throw new ClientException(sprintf(_m('Invalid hub.topic %s; list does not exist.'),$feed));
181 common_log(LOG_DEBUG, "Not a user, group or people tag feed? $feed $userFeed $groupFeed $listFeed");
183 common_log(LOG_DEBUG, "LOST $feed");
188 * Grab and validate a URL from POST parameters.
189 * @throws ClientException for malformed or non-http/https URLs
191 protected function argUrl($arg)
193 $url = $this->arg($arg);
194 $params = array('domain_check' => false, // otherwise breaks my local tests :P
195 'allowed_schemes' => array('http', 'https'));
196 $validate = new Validate;
197 if ($validate->uri($url, $params)) {
200 // TRANS: Client exception.
201 // TRANS: %1$s is this argument to the method this exception occurs in, %2$s is a URL.
202 throw new ClientException(sprintf(_m('Invalid URL passed for %1$s: "%2$s"'),$arg,$url));
207 * Get HubSub subscription record for a given feed & subscriber.
209 * @param string $feed
210 * @param string $callback
211 * @return mixed HubSub or false
213 protected function getSub($feed, $callback)
215 return HubSub::getByHashkey($feed, $callback);