]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RSSCloud/RSSCloudNotifier.php
Merge branch 'sitemap' of gitorious.org:~evan/statusnet/evans-mainline into sitemap
[quix0rs-gnu-social.git] / plugins / RSSCloud / RSSCloudNotifier.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Class to ping an rssCloud endpoint when a feed has been updated
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18    *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Class for notifying cloud-enabled RSS aggregators that StatusNet
36  * feeds have been updated.
37  *
38  * @category Plugin
39  * @package  StatusNet
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/
43  **/
44 class RSSCloudNotifier
45 {
46     const MAX_FAILURES = 3;
47
48     /**
49      * Send an HTTP GET to the notification handler with a
50      * challenge string to see if it repsonds correctly.
51      *
52      * @param string $endpoint URL of the notification handler
53      * @param string $feed     the feed being subscribed to
54      *
55      * @return boolean success
56      */
57     function challenge($endpoint, $feed)
58     {
59         $code   = common_confirmation_code(128);
60         $params = array('url' => $feed, 'challenge' => $code);
61         $url    = $endpoint . '?' . http_build_query($params);
62
63         try {
64             $client   = new HTTPClient();
65             $response = $client->get($url);
66         } catch (HTTP_Request2_Exception $e) {
67             common_log(LOG_INFO,
68                        'RSSCloud plugin - failure testing notify handler ' .
69                        $endpoint . ' - '  . $e->getMessage());
70             return false;
71         }
72
73         // Check response is betweet 200 and 299 and body contains challenge data
74
75         $status = $response->getStatus();
76         $body   = $response->getBody();
77
78         if ($status >= 200 && $status < 300) {
79
80             // NOTE: the spec says that the body must contain the string
81             // challenge.  It doesn't say that the body must contain the
82             // challenge string ONLY, although that seems to be the way
83             // the other implementors have interpreted it.
84
85             if (strpos($body, $code) !== false) {
86                 common_log(LOG_INFO, 'RSSCloud plugin - ' .
87                            "success testing notify handler:  $endpoint");
88                 return true;
89             } else {
90                 common_log(LOG_INFO, 'RSSCloud plugin - ' .
91                           'challenge/repsonse failed for notify handler ' .
92                            $endpoint);
93                 common_debug('body = ' . var_export($body, true));
94                 return false;
95             }
96         } else {
97             common_log(LOG_INFO, 'RSSCloud plugin - ' .
98                        "failure testing notify handler:  $endpoint " .
99                        ' - got HTTP ' . $status);
100             common_debug('body = ' . var_export($body, true));
101             return false;
102         }
103     }
104
105     /**
106      * HTTP POST a notification that a feed has been updated
107      * ('ping the cloud').
108      *
109      * @param String $endpoint URL of the notification handler
110      * @param String $feed     the feed being subscribed to
111      *
112      * @return boolean success
113      */
114     function postUpdate($endpoint, $feed)
115     {
116
117         $headers  = array();
118         $postdata = array('url' => $feed);
119
120         try {
121             $client   = new HTTPClient();
122             $response = $client->post($endpoint, $headers, $postdata);
123         } catch (HTTP_Request2_Exception $e) {
124             common_log(LOG_INFO, 'RSSCloud plugin - failure notifying ' .
125                        $endpoint . ' that feed ' . $feed .
126                        ' has changed: ' . $e->getMessage());
127             return false;
128         }
129
130         $status = $response->getStatus();
131
132         if ($status >= 200 && $status < 300) {
133             common_log(LOG_INFO, 'RSSCloud plugin - success notifying ' .
134                        $endpoint . ' that feed ' . $feed . ' has changed.');
135             return true;
136         } else {
137             common_log(LOG_INFO, 'RSSCloud plugin - failure notifying ' .
138                        $endpoint . ' that feed ' . $feed .
139                        ' has changed: got HTTP ' . $status);
140             return false;
141         }
142     }
143
144     /**
145      * Notify all subscribers to a profile feed that it has changed.
146      *
147      * @param Profile $profile the profile whose feed has been
148      *        updated
149      *
150      * @return boolean success
151      */
152     function notify($profile)
153     {
154         $feed = common_path('api/statuses/user_timeline/') .
155           $profile->id . '.rss';
156
157         $cloudSub = new RSSCloudSubscription();
158
159         $cloudSub->subscribed = $profile->id;
160
161         if ($cloudSub->find()) {
162             while ($cloudSub->fetch()) {
163                 $result = $this->postUpdate($cloudSub->url, $feed);
164                 if ($result == false) {
165                     $this->handleFailure($cloudSub);
166                 }
167             }
168         }
169
170         return true;
171     }
172
173     /**
174      * Handle problems posting cloud notifications. Increment the failure
175      * count, or delete the subscription if the maximum number of failures
176      * is exceeded.
177      *
178      * XXX: Redo with proper DB_DataObject methods once I figure out what
179      * what the problem is with pluginized DB_DataObjects. -Z
180      *
181      * @param RSSCloudSubscription $cloudSub the subscription in question
182      *
183      * @return boolean success
184      */
185     function handleFailure($cloudSub)
186     {
187         $failCnt = $cloudSub->failures + 1;
188
189         if ($failCnt == self::MAX_FAILURES) {
190
191             common_log(LOG_INFO,
192                        'Deleting RSSCloud subcription ' .
193                        '(max failure count reached), profile: ' .
194                        $cloudSub->subscribed .
195                        ' handler: ' .
196                        $cloudSub->url);
197
198             // XXX: WTF! ->delete() doesn't work. Clearly, there are some issues with
199             // the DB_DataObject, or my understanding of it.  Have to drop into SQL.
200
201             // $result = $cloudSub->delete();
202
203             $qry = 'DELETE from rsscloud_subscription' .
204               ' WHERE subscribed = ' . $cloudSub->subscribed .
205               ' AND url = \'' . $cloudSub->url . '\'';
206
207             $result = $cloudSub->query($qry);
208
209             if (!$result) {
210                 common_log_db_error($cloudSub, 'DELETE', __FILE__);
211                 common_log(LOG_ERR, 'Could not delete RSSCloud subscription.');
212             }
213
214         } else {
215
216             common_debug('Updating failure count on RSSCloud subscription. ' .
217                          $failCnt);
218
219             $failCnt = $cloudSub->failures + 1;
220
221             // XXX: ->update() not working either, gar!
222
223             $qry = 'UPDATE rsscloud_subscription' .
224               ' SET failures = ' . $failCnt .
225               ' WHERE subscribed = ' . $cloudSub->subscribed .
226               ' AND url = \'' . $cloudSub->url . '\'';
227
228             $result = $cloudSub->query($qry);
229
230             if (!$result) {
231                 common_log_db_error($cloudsub, 'UPDATE', __FILE__);
232                 common_log(LOG_ERR,
233                            'Could not update failure ' .
234                            'count on RSSCloud subscription');
235             }
236         }
237     }
238
239 }
240