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/
44 class RSSCloudRequestNotifyAction extends Action
49 * @param array $args Web and URL arguments
51 * @return boolean false if user doesn't exist
53 function prepare($args)
55 parent::prepare($args);
57 $this->ip = $_SERVER['REMOTE_ADDR'];
58 $this->port = $this->arg('port');
59 $this->path = $this->arg('path');
61 if ($this->path[0] != '/') {
62 $this->path = '/' . $this->path;
65 $this->protocol = $this->arg('protocol');
66 $this->procedure = $this->arg('notifyProcedure');
67 $this->domain = $this->arg('domain');
69 $this->feeds = $this->getFeeds();
77 * Checks for all the required parameters for a subscription,
78 * validates that the feed being subscribed to is real, and then
79 * saves the subsctiption.
81 * @param array $args $_REQUEST data (unused)
85 function handle($args)
87 parent::handle($args);
89 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
90 $this->showResult(false, _m('Request must be POST.'));
96 if (empty($this->port)) {
100 if (empty($this->path)) {
104 if (empty($this->protocol)) {
105 $missing[] = 'protocol';
106 } else if (strtolower($this->protocol) != 'http-post') {
107 $msg = _m('Only http-post notifications are supported at this time.');
108 $this->showResult(false, $msg);
112 if (!isset($this->procedure)) {
113 $missing[] = 'notifyProcedure';
116 if (!empty($missing)) {
117 // TRANS: %s is a comma separated list of parameters.
118 $msg = sprintf(_m('The following parameters were missing from the request body: %s.'),implode(', ', $missing));
119 $this->showResult(false, $msg);
123 if (empty($this->feeds)) {
124 $msg = _m('You must provide at least one valid profile feed url ' .
125 '(url1, url2, url3 ... urlN).');
126 $this->showResult(false, $msg);
130 // We have to validate everything before saving anything.
131 // We only return one success or failure no matter how
132 // many feeds the subscriber is trying to subscribe to
133 foreach ($this->feeds as $feed) {
135 if (!$this->validateFeed($feed)) {
137 $nh = $this->getNotifyUrl();
138 common_log(LOG_WARNING,
139 "RSSCloud plugin - $nh tried to subscribe to invalid feed: $feed");
141 $msg = _m('Feed subscription failed: Not a valid feed.');
142 $this->showResult(false, $msg);
146 if (!$this->testNotificationHandler($feed)) {
147 $msg = _m('Feed subscription failed - ' .
148 'notification handler doesn\'t respond correctly.');
149 $this->showResult(false, $msg);
154 foreach ($this->feeds as $feed) {
155 $this->saveSubscription($feed);
158 // XXX: What to do about deleting stale subscriptions?
159 // 25 hours seems harsh. WordPress doesn't ever remove
161 $msg = _m('Thanks for the subscription. ' .
162 'When the feed(s) update(s), you will be notified.');
164 $this->showResult(true, $msg);
168 * Validate that the requested feed is one we serve
171 * @param string $feed the feed in question
175 function validateFeed($feed)
177 $user = $this->userFromFeed($feed);
187 * Pull all of the urls (url1, url2, url3...urlN) that
188 * the subscriber wants to subscribe to.
190 * @return array $feeds the list of feeds
196 while (list($key, $feed) = each($this->args)) {
197 if (preg_match('/^url\d*$/', $key)) {
206 * Test that a notification handler is there and is reponding
207 * correctly. This is called before adding a subscription.
209 * @param string $feed the feed to verify
211 * @return boolean success result
213 function testNotificationHandler($feed)
215 $notifyUrl = $this->getNotifyUrl();
217 $notifier = new RSSCloudNotifier();
219 if (isset($this->domain)) {
220 // 'domain' param set, so we have to use GET and send a challenge
222 'RSSCloud plugin - Testing notification handler with challenge: ' .
224 return $notifier->challenge($notifyUrl, $feed);
227 common_log(LOG_INFO, 'RSSCloud plugin - Testing notification handler: ' .
230 return $notifier->postUpdate($notifyUrl, $feed);
235 * Build the URL for the notification handler based on the
236 * parameters passed in with the subscription request.
238 * @return string notification handler url
240 function getNotifyUrl()
242 if (isset($this->domain)) {
243 return 'http://' . $this->domain . ':' . $this->port . $this->path;
245 return 'http://' . $this->ip . ':' . $this->port . $this->path;
250 * Uses the nickname part of the subscribed feed URL to figure out
251 * whethere there's really a user with such a feed. Used to
252 * validate feeds before adding a subscription.
254 * @param string $feed the feed in question
256 * @return boolean success
258 function userFromFeed($feed)
260 // We only do canonical RSS2 profile feeds (specified by ID), e.g.:
261 // http://www.example.com/api/statuses/user_timeline/2.rss
262 $path = common_path('api/statuses/user_timeline/');
263 $valid = '%^' . $path . '(?<id>.*)\.rss$%';
265 if (preg_match($valid, $feed, $matches)) {
266 $user = User::staticGet('id', $matches['id']);
276 * Save an RSSCloud subscription
278 * @param string $feed a valid profile feed
280 * @return boolean success result
282 function saveSubscription($feed)
284 $user = $this->userFromFeed($feed);
286 $notifyUrl = $this->getNotifyUrl();
288 $sub = RSSCloudSubscription::getSubscription($user->id, $notifyUrl);
291 common_log(LOG_INFO, "RSSCloud plugin - $notifyUrl refreshed subscription" .
292 " to user $user->nickname (id: $user->id).");
295 $sub = new RSSCloudSubscription();
297 $sub->subscribed = $user->id;
298 $sub->url = $notifyUrl;
299 $sub->created = common_sql_now();
301 if (!$sub->insert()) {
302 common_log_db_error($sub, 'INSERT', __FILE__);
306 common_log(LOG_INFO, "RSSCloud plugin - $notifyUrl subscribed" .
307 " to user $user->nickname (id: $user->id)");
314 * Show an XML message indicating the subscription
315 * was successful or failed.
317 * @param boolean $success whether it was good or bad
318 * @param string $msg the message to output
320 * @return boolean success result
322 function showResult($success, $msg)
325 $this->elementStart('notifyResult',
326 array('success' => ($success) ? 'true' : 'false',