4 * Action to let RSSCloud aggregators request update notification when
5 * user profile feeds change.
11 * @author Zach Copley <zach@status.net>
12 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
13 * @link http://status.net/
15 * StatusNet - the distributed open-source microblogging tool
16 * Copyright (C) 2009, StatusNet, Inc.
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU Affero General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Affero General Public License for more details.
28 * You should have received a copy of the GNU Affero General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 if (!defined('STATUSNET')) {
36 class RSSCloudRequestNotifyAction extends Action
41 * @param array $args Web and URL arguments
43 * @return boolean false if user doesn't exist
45 function prepare($args)
47 parent::prepare($args);
49 $this->ip = $_SERVER['REMOTE_ADDR'];
50 $this->port = $this->arg('port');
51 $this->path = $this->arg('path');
52 $this->protocol = $this->arg('protocol');
53 $this->procedure = $this->arg('notifyProcedure');
54 $this->feeds = $this->getFeeds();
56 $this->subscriber_url = 'http://' . $this->ip . ':' . $this->port . $this->path;
61 function handle($args)
63 parent::handle($args);
65 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
66 $this->showResult(false, 'Request must be POST.');
72 if (empty($this->port)) {
76 $path = $this->arg('path');
78 if (empty($this->path)) {
82 $protocol = $this->arg('protocol');
84 if (empty($this->protocol)) {
85 $missing[] = 'protocol';
88 if (!isset($this->procedure)) {
89 $missing[] = 'notifyProcedure';
92 if (!empty($missing)) {
93 $msg = 'The following parameters were missing from the request body: ' .
94 implode(', ', $missing) . '.';
95 $this->showResult(false, $msg);
99 if (empty($this->feeds)) {
100 $this->showResult(false,
101 'You must provide at least one valid profile feed url (url1, url2, url3 ... urlN).');
105 $endpoint = $ip . ':' . $port . $path;
107 foreach ($this->feeds as $feed) {
108 $this->saveSubscription($feed);
111 // XXX: What to do about deleting stale subscriptions? 25 hours seems harsh.
112 // WordPress doesn't ever remove subscriptions.
114 $msg = 'Thanks for the registration. It worked. When the feed(s) update(s) we\'ll notify you. ' .
115 ' Don\'t forget to re-register after 24 hours, your subscription will expire in 25.';
117 $this->showResult(true, $msg);
124 foreach ($this->args as $key => $feed ) {
125 if (preg_match('|url\d+|', $key)) {
127 if ($this->testFeed($feed)) {
130 $msg = 'RSSCloud Plugin - ' . $this->ip . ' tried to subscribe ' .
131 'to a non-existent feed: ' . $feed;
132 common_log(LOG_WARN, $msg);
140 function testNotificationHandler($feed)
142 $notifier = new RSSCloudNotifier();
143 return $notifier->postUpdate($endpoint, $feed);
146 // returns valid user or false
147 function testFeed($feed)
149 $user = $this->userFromFeed($feed);
153 common_debug("Valid feed: $feed");
155 // OK, so this is a valid profile feed url, now let's see if the
156 // other system reponds to our notifications before we
159 if ($this->testNotificationHandler($feed)) {
167 // this actually does the validating and figuring out the
168 // user, which it returns
169 function userFromFeed($feed)
171 // We only do profile feeds
173 $path = common_path('api/statuses/user_timeline/');
174 $valid = '%^' . $path . '(?<nickname>.*)\.rss$%';
176 if (preg_match($valid, $feed, $matches)) {
177 $user = User::staticGet('nickname', $matches['nickname']);
186 function saveSubscription($feed)
188 // check to see if we already have a profile for this subscriber
190 $other = Remote_profile::staticGet('uri', $this->subscriber_url);
192 if ($other === false) {
193 $other->saveProfile();
196 $user = userFromFeed($feed);
198 $result = subs_subscribe_to($user, $other);
200 if ($result != true) {
201 $msg = "RSSPlugin - got '$result' trying to subscribe " .
202 "$this->subscriber_url to $user->nickname" . "'s profile feed.";
203 common_log(LOG_WARN, $msg);
205 $msg = 'RSSCloud plugin - subscribe: ' . $this->subscriber_url .
206 ' subscribed to ' . $feed;
208 common_log(LOG_INFO, $msg);
212 function saveProfile()
214 common_debug("Saving remote profile for $this->subscriber_url");
216 // XXX: We need to add a field to Remote_profile to indicate the kind
217 // of remote profile? i.e: OMB, RSSCloud, PuSH, Twitter
219 $remote = new Remote_profile();
220 $remote->uri = $this->subscriber_url;
221 $remote->postnoticeurl = $this->subscriber_url;
222 $remote->created = DB_DataObject_Cast::dateTime();
224 if (!$remote->insert()) {
225 throw new Exception(_('RSSCloud plugin - Error inserting remote profile!'));
229 function showResult($success, $msg)
232 $this->elementStart('notifyResult', array('success' => ($success) ? 'true' : 'false',