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)) {
140 $msg = 'Feed subscription failed - Not a valid feed.';
141 $this->showResult(false, $msg);
145 if (!$this->testNotificationHandler($feed)) {
146 $msg = 'Feed subscription failed - ' .
147 'notification handler doesn\'t respond correctly.';
148 $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
162 $msg = 'Thanks for the subscription. ' .
163 'When the feed(s) update(s) we\'ll notify you.';
165 $this->showResult(true, $msg);
169 * Validate that the requested feed is one we serve
172 * @param string $feed the feed in question
177 function validateFeed($feed)
179 $user = $this->userFromFeed($feed);
189 * Pull all of the urls (url1, url2, url3...urlN) that
190 * the subscriber wants to subscribe to.
192 * @return array $feeds the list of feeds
199 while (list($key, $feed) = each($this->args)) {
200 if (preg_match('/^url\d*$/', $key)) {
209 * Test that a notification handler is there and is reponding
210 * correctly. This is called before adding a subscription.
212 * @param string $feed the feed to verify
214 * @return boolean success result
217 function testNotificationHandler($feed)
219 common_debug("RSSCloudPlugin - testNotificationHandler()");
221 $notifyUrl = $this->getNotifyUrl();
223 $notifier = new RSSCloudNotifier();
225 if (isset($this->domain)) {
227 // 'domain' param set, so we have to use GET and send a challenge
229 common_log(LOG_INFO, 'Testing notification handler with challenge: ' .
231 return $notifier->challenge($notifyUrl, $feed);
234 common_log(LOG_INFO, 'Testing notification handler: ' .
237 return $notifier->postUpdate($notifyUrl, $feed);
242 * Build the URL for the notification handler based on the
243 * parameters passed in with the subscription request.
245 * @return string notification handler url
248 function getNotifyUrl()
250 if (isset($this->domain)) {
251 return 'http://' . $this->domain . ':' . $this->port . $this->path;
253 return 'http://' . $this->ip . ':' . $this->port . $this->path;
258 * Uses the nickname part of the subscribed feed URL to figure out
259 * whethere there's really a user with such a feed. Used to
260 * validate feeds before adding a subscription.
262 * @param string $feed the feed in question
264 * @return boolean success
267 function userFromFeed($feed)
269 // We only do profile feeds
271 $path = common_path('api/statuses/user_timeline/');
272 $valid = '%^' . $path . '(?<nickname>.*)\.rss$%';
274 if (preg_match($valid, $feed, $matches)) {
275 $user = User::staticGet('nickname', $matches['nickname']);
285 * Save an RSSCloud subscription
287 * @param string $feed a valid profile feed
289 * @return boolean success result
292 function saveSubscription($feed)
294 $user = $this->userFromFeed($feed);
296 $notifyUrl = $this->getNotifyUrl();
298 $sub = RSSCloudSubscription::getSubscription($user->id, $notifyUrl);
301 common_debug("Already subscribed to that!");
304 $sub = new RSSCloudSubscription();
306 $sub->subscribed = $user->id;
307 $sub->url = $notifyUrl;
308 $sub->created = common_sql_now();
310 if (!$sub->insert()) {
311 common_log_db_error($sub, 'INSERT', __FILE__);
321 * Show an XML message indicating the subscription
322 * was successful or failed.
324 * @param boolean $success whether it was good or bad
325 * @param string $msg the message to output
327 * @return boolean success result
330 function showResult($success, $msg)
333 $this->elementStart('notifyResult',
334 array('success' => ($success) ? 'true' : 'false',