]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RSSCloud/RSSCloudRequestNotify.php
Notifier works, and bad subscriptions are deleted properly now.
[quix0rs-gnu-social.git] / plugins / RSSCloud / RSSCloudRequestNotify.php
1 <?php
2 /**
3  * Action to let RSSCloud aggregators request update notification when
4  * user profile feeds change.
5  *
6  * PHP version 5
7  *
8  * @category Plugin
9  * @package  StatusNet
10  * @author   Zach Copley <zach@status.net>
11  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
12  * @link     http://status.net/
13  *
14  * StatusNet - the distributed open-source microblogging tool
15  * Copyright (C) 2009, StatusNet, Inc.
16  *
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.
21  *
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.
26  *
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/>.
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 class RSSCloudRequestNotifyAction extends Action
36 {
37     /**
38      * Initialization.
39      *
40      * @param array $args Web and URL arguments
41      *
42      * @return boolean false if user doesn't exist
43      */
44     function prepare($args)
45     {
46         parent::prepare($args);
47
48         $this->ip        = $_SERVER['REMOTE_ADDR'];
49         $this->port      = $this->arg('port');
50         $this->path      = $this->arg('path');
51
52         if ($this->path[0] != '/') {
53             $this->path = '/' . $this->path;
54         }
55
56         $this->protocol  = $this->arg('protocol');
57         $this->procedure = $this->arg('notifyProcedure');
58         $this->domain    = $this->arg('domain');
59
60         $this->feeds     = $this->getFeeds();
61
62         return true;
63     }
64
65     function handle($args)
66     {
67         parent::handle($args);
68
69         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
70             $this->showResult(false, 'Request must be POST.');
71             return;
72         }
73
74         $missing = array();
75
76         if (empty($this->port)) {
77             $missing[] = 'port';
78         }
79
80         if (empty($this->path)) {
81             $missing[] = 'path';
82         }
83
84         if (empty($this->protocol)) {
85             $missing[] = 'protocol';
86         }
87
88         if (!isset($this->procedure)) {
89             $missing[] = 'notifyProcedure';
90         }
91
92         if (!empty($missing)) {
93             $msg = 'The following parameters were missing from the request body: ' .
94                 implode(', ', $missing) . '.';
95             $this->showResult(false, $msg);
96             return;
97         }
98
99         if (empty($this->feeds)) {
100             $this->showResult(false,
101                               'You must provide at least one valid profile feed url (url1, url2, url3 ... urlN).');
102             return;
103         }
104
105         // We have to validate everything before saving anything.
106         // We only return one success or failure no matter how
107         // many feeds the subscriber is trying to subscribe to
108
109         foreach ($this->feeds as $feed) {
110
111             if (!$this->validateFeed($feed)) {
112                 $msg = 'Feed subscription failed - Not a valid feed.';
113                 $this->showResult(false, $msg);
114                 return;
115             }
116
117             if (!$this->testNotificationHandler($feed)) {
118                 $msg = 'Feed subscription failed - ' .
119                 'notification handler doesn\'t respond correctly.';
120                 $this->showResult(false, $msg);
121                 return;
122             }
123
124         }
125
126         foreach ($this->feeds as $feed) {
127             $this->saveSubscription($feed);
128         }
129
130         // XXX: What to do about deleting stale subscriptions?
131         // 25 hours seems harsh. WordPress doesn't ever remove
132         // subscriptions.
133
134         $msg = 'Thanks for the subscription. ' .
135           'When the feed(s) update(s) we\'ll notify you.';
136
137         $this->showResult(true, $msg);
138     }
139
140     function validateFeed($feed)
141     {
142         $user = $this->userFromFeed($feed);
143
144         if (empty($user)) {
145             return false;
146         }
147
148         return true;
149     }
150
151     function getFeeds()
152     {
153         $feeds = array();
154
155         while (list($key, $feed) = each ($this->args)) {
156             if (preg_match('/^url\d*$/', $key)) {
157                 $feeds[] = $feed;
158             }
159         }
160
161         return $feeds;
162     }
163
164     function testNotificationHandler($feed)
165     {
166         common_debug("RSSCloudPlugin - testNotificationHandler()");
167
168         $notifyUrl = $this->getNotifyUrl();
169
170         $notifier = new RSSCloudNotifier();
171
172         if (isset($this->domain)) {
173
174             // 'domain' param set, so we have to use GET and send a challenge
175
176             common_log(LOG_INFO, 'Testing notification handler with challenge: ' .
177                        $notifyUrl);
178             return $notifier->challenge($notifyUrl, $feed);
179
180         } else {
181             common_log(LOG_INFO, 'Testing notification handler: ' .
182                        $notifyUrl);
183
184             return $notifier->postUpdate($notifyUrl, $feed);
185         }
186     }
187
188     function getNotifyUrl()
189     {
190         if (isset($this->domain)) {
191             return 'http://' . $this->domain . ':' . $this->port . $this->path;
192         } else {
193             return 'http://' . $this->ip . ':' . $this->port . $this->path;
194         }
195      }
196
197     function userFromFeed($feed)
198     {
199         // We only do profile feeds
200
201         // XXX: Add cloud element to RSS 1.0 feeds?
202
203         $path = common_path('api/statuses/user_timeline/');
204         $valid = '%^' . $path . '(?<nickname>.*)\.rss$%';
205
206         if (preg_match($valid, $feed, $matches)) {
207             $user = User::staticGet('nickname', $matches['nickname']);
208             if (!empty($user)) {
209                 return $user;
210             }
211         }
212
213         return false;
214     }
215
216     function saveSubscription($feed)
217     {
218         $user = $this->userFromFeed($feed);
219
220         $notifyUrl = $this->getNotifyUrl();
221
222         $sub = RSSCloudSubscription::getSubscription($user->id, $notifyUrl);
223
224         if ($sub) {
225             common_debug("Already subscribed to that!");
226         } else {
227
228             $sub = new RSSCloudSubscription();
229
230             $sub->subscribed = $user->id;
231             $sub->url        = $notifyUrl;
232             $sub->created    = common_sql_now();
233
234             if (!$sub->insert()) {
235                 common_log_db_error($sub, 'INSERT', __FILE__);
236                 return false;
237             }
238
239         }
240
241         return true;
242     }
243
244     function showResult($success, $msg)
245     {
246         $this->startXML();
247         $this->elementStart('notifyResult', array('success' => ($success) ? 'true' : 'false',
248                                                   'msg'     => $msg));
249         $this->endXML();
250     }
251
252 }
253