3 * Action to let RSSCloud aggregators request update notification when
4 * user profile feeds change.
10 * @author Zach Copley <zach@status.net>
11 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
12 * @link http://status.net/
14 * StatusNet - the distributed open-source microblogging tool
15 * Copyright (C) 2009, StatusNet, Inc.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU Affero General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Affero General Public License for more details.
27 * You should have received a copy of the GNU Affero General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 if (!defined('STATUSNET')) {
36 * Action class to handle RSSCloud notification (subscription) requests
40 * @author Zach Copley <zach@status.net>
41 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
42 * @link http://status.net/
45 class RSSCloudRequestNotifyAction extends Action
50 * @param array $args Web and URL arguments
52 * @return boolean false if user doesn't exist
55 function prepare($args)
57 parent::prepare($args);
59 $this->ip = $_SERVER['REMOTE_ADDR'];
60 $this->port = $this->arg('port');
61 $this->path = $this->arg('path');
63 if ($this->path[0] != '/') {
64 $this->path = '/' . $this->path;
67 $this->protocol = $this->arg('protocol');
68 $this->procedure = $this->arg('notifyProcedure');
69 $this->domain = $this->arg('domain');
71 $this->feeds = $this->getFeeds();
79 * Checks for all the required parameters for a subscription,
80 * validates that the feed being subscribed to is real, and then
81 * saves the subsctiption.
83 * @param array $args $_REQUEST data (unused)
88 function handle($args)
90 parent::handle($args);
92 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
93 $this->showResult(false, 'Request must be POST.');
99 if (empty($this->port)) {
103 if (empty($this->path)) {
107 if (empty($this->protocol)) {
108 $missing[] = 'protocol';
109 } else if (strtolower($this->protocol) != 'http-post') {
110 $msg = 'Only http-post notifications are supported at this time.';
111 $this->showResult(false, $msg);
115 if (!isset($this->procedure)) {
116 $missing[] = 'notifyProcedure';
119 if (!empty($missing)) {
120 $msg = 'The following parameters were missing from the request body: ' .
121 implode(', ', $missing) . '.';
122 $this->showResult(false, $msg);
126 if (empty($this->feeds)) {
127 $msg = 'You must provide at least one valid profile feed url ' .
128 '(url1, url2, url3 ... urlN).';
129 $this->showResult(false, $msg);
133 // We have to validate everything before saving anything.
134 // We only return one success or failure no matter how
135 // many feeds the subscriber is trying to subscribe to
137 foreach ($this->feeds as $feed) {
139 if (!$this->validateFeed($feed)) {
141 $nh = $this->getNotifyUrl();
142 common_log(LOG_WARNING,
143 "RSSCloud plugin - $nh tried to subscribe to invalid feed: $feed");
145 $msg = 'Feed subscription failed - Not a valid feed.';
146 $this->showResult(false, $msg);
150 if (!$this->testNotificationHandler($feed)) {
151 $msg = 'Feed subscription failed - ' .
152 'notification handler doesn\'t respond correctly.';
153 $this->showResult(false, $msg);
159 foreach ($this->feeds as $feed) {
160 $this->saveSubscription($feed);
163 // XXX: What to do about deleting stale subscriptions?
164 // 25 hours seems harsh. WordPress doesn't ever remove
167 $msg = 'Thanks for the subscription. ' .
168 'When the feed(s) update(s) we\'ll notify you.';
170 $this->showResult(true, $msg);
174 * Validate that the requested feed is one we serve
177 * @param string $feed the feed in question
182 function validateFeed($feed)
184 $user = $this->userFromFeed($feed);
194 * Pull all of the urls (url1, url2, url3...urlN) that
195 * the subscriber wants to subscribe to.
197 * @return array $feeds the list of feeds
204 while (list($key, $feed) = each($this->args)) {
205 if (preg_match('/^url\d*$/', $key)) {
214 * Test that a notification handler is there and is reponding
215 * correctly. This is called before adding a subscription.
217 * @param string $feed the feed to verify
219 * @return boolean success result
222 function testNotificationHandler($feed)
224 $notifyUrl = $this->getNotifyUrl();
226 $notifier = new RSSCloudNotifier();
228 if (isset($this->domain)) {
230 // 'domain' param set, so we have to use GET and send a challenge
233 'RSSCloud plugin - Testing notification handler with challenge: ' .
235 return $notifier->challenge($notifyUrl, $feed);
238 common_log(LOG_INFO, 'RSSCloud plugin - Testing notification handler: ' .
241 return $notifier->postUpdate($notifyUrl, $feed);
246 * Build the URL for the notification handler based on the
247 * parameters passed in with the subscription request.
249 * @return string notification handler url
252 function getNotifyUrl()
254 if (isset($this->domain)) {
255 return 'http://' . $this->domain . ':' . $this->port . $this->path;
257 return 'http://' . $this->ip . ':' . $this->port . $this->path;
262 * Uses the nickname part of the subscribed feed URL to figure out
263 * whethere there's really a user with such a feed. Used to
264 * validate feeds before adding a subscription.
266 * @param string $feed the feed in question
268 * @return boolean success
271 function userFromFeed($feed)
273 // We only do canonical RSS2 profile feeds (specified by ID), e.g.:
274 // http://www.example.com/api/statuses/user_timeline/2.rss
276 $path = common_path('api/statuses/user_timeline/');
277 $valid = '%^' . $path . '(?<id>.*)\.rss$%';
279 if (preg_match($valid, $feed, $matches)) {
280 $user = User::staticGet('id', $matches['id']);
290 * Save an RSSCloud subscription
292 * @param string $feed a valid profile feed
294 * @return boolean success result
297 function saveSubscription($feed)
299 $user = $this->userFromFeed($feed);
301 $notifyUrl = $this->getNotifyUrl();
303 $sub = RSSCloudSubscription::getSubscription($user->id, $notifyUrl);
306 common_log(LOG_INFO, "RSSCloud plugin - $notifyUrl refreshed subscription" .
307 " to user $user->nickname (id: $user->id).");
310 $sub = new RSSCloudSubscription();
312 $sub->subscribed = $user->id;
313 $sub->url = $notifyUrl;
314 $sub->created = common_sql_now();
316 if (!$sub->insert()) {
317 common_log_db_error($sub, 'INSERT', __FILE__);
321 common_log(LOG_INFO, "RSSCloud plugin - $notifyUrl subscribed" .
322 " to user $user->nickname (id: $user->id)");
329 * Show an XML message indicating the subscription
330 * was successful or failed.
332 * @param boolean $success whether it was good or bad
333 * @param string $msg the message to output
335 * @return boolean success result
338 function showResult($success, $msg)
341 $this->elementStart('notifyResult',
342 array('success' => ($success) ? 'true' : 'false',