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 (url1, url2, url3 ... urlN).';
128 $this->showResult(false, $msg);
132 // We have to validate everything before saving anything.
133 // We only return one success or failure no matter how
134 // many feeds the subscriber is trying to subscribe to
136 foreach ($this->feeds as $feed) {
138 if (!$this->validateFeed($feed)) {
139 $msg = 'Feed subscription failed - Not a valid feed.';
140 $this->showResult(false, $msg);
144 if (!$this->testNotificationHandler($feed)) {
145 $msg = 'Feed subscription failed - ' .
146 'notification handler doesn\'t respond correctly.';
147 $this->showResult(false, $msg);
153 foreach ($this->feeds as $feed) {
154 $this->saveSubscription($feed);
157 // XXX: What to do about deleting stale subscriptions?
158 // 25 hours seems harsh. WordPress doesn't ever remove
161 $msg = 'Thanks for the subscription. ' .
162 'When the feed(s) update(s) we\'ll notify you.';
164 $this->showResult(true, $msg);
168 * Validate that the requested feed is one we serve
171 * @param string $feed the feed in question
176 function validateFeed($feed)
178 $user = $this->userFromFeed($feed);
188 * Pull all of the urls (url1, url2, url3...urlN) that
189 * the subscriber wants to subscribe to.
191 * @return array $feeds the list of feeds
198 while (list($key, $feed) = each ($this->args)) {
199 if (preg_match('/^url\d*$/', $key)) {
208 * Test that a notification handler is there and is reponding
209 * correctly. This is called before adding a subscription.
211 * @param string $feed the feed to verify
213 * @return boolean success result
216 function testNotificationHandler($feed)
218 common_debug("RSSCloudPlugin - testNotificationHandler()");
220 $notifyUrl = $this->getNotifyUrl();
222 $notifier = new RSSCloudNotifier();
224 if (isset($this->domain)) {
226 // 'domain' param set, so we have to use GET and send a challenge
228 common_log(LOG_INFO, 'Testing notification handler with challenge: ' .
230 return $notifier->challenge($notifyUrl, $feed);
233 common_log(LOG_INFO, 'Testing notification handler: ' .
236 return $notifier->postUpdate($notifyUrl, $feed);
241 * Build the URL for the notification handler based on the
242 * parameters passed in with the subscription request.
244 * @return string notification handler url
247 function getNotifyUrl()
249 if (isset($this->domain)) {
250 return 'http://' . $this->domain . ':' . $this->port . $this->path;
252 return 'http://' . $this->ip . ':' . $this->port . $this->path;
257 * Uses the nickname part of the subscribed feed URL to figure out
258 * whethere there's really a user with such a feed. Used to
259 * validate feeds before adding a subscription.
261 * @param string $feed the feed in question
263 * @return boolean success
266 function userFromFeed($feed)
268 // We only do profile feeds
270 $path = common_path('api/statuses/user_timeline/');
271 $valid = '%^' . $path . '(?<nickname>.*)\.rss$%';
273 if (preg_match($valid, $feed, $matches)) {
274 $user = User::staticGet('nickname', $matches['nickname']);
284 * Save an RSSCloud subscription
286 * @param $feed a valid profile feed
288 * @return boolean success result
291 function saveSubscription($feed)
293 $user = $this->userFromFeed($feed);
295 $notifyUrl = $this->getNotifyUrl();
297 $sub = RSSCloudSubscription::getSubscription($user->id, $notifyUrl);
300 common_debug("Already subscribed to that!");
303 $sub = new RSSCloudSubscription();
305 $sub->subscribed = $user->id;
306 $sub->url = $notifyUrl;
307 $sub->created = common_sql_now();
309 if (!$sub->insert()) {
310 common_log_db_error($sub, 'INSERT', __FILE__);
320 * Show an XML message indicating the subscription
321 * was successful or failed.
323 * @param boolean $success whether it was good or bad
324 * @param string $msg the message to output
326 * @return boolean success result
329 function showResult($success, $msg)
332 $this->elementStart('notifyResult', array('success' => ($success) ? 'true' : 'false',