]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RSSCloud/lib/rsscloudnotifier.php
plugins onAutoload now only overloads if necessary (extlibs etc.)
[quix0rs-gnu-social.git] / plugins / RSSCloud / lib / 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             // NOTE: the spec says that the body must contain the string
80             // challenge.  It doesn't say that the body must contain the
81             // challenge string ONLY, although that seems to be the way
82             // the other implementors have interpreted it.
83
84             if (strpos($body, $code) !== false) {
85                 common_log(LOG_INFO, 'RSSCloud plugin - ' .
86                            "success testing notify handler:  $endpoint");
87                 return true;
88             } else {
89                 common_log(LOG_INFO, 'RSSCloud plugin - ' .
90                           'challenge/repsonse failed for notify handler ' .
91                            $endpoint);
92                 common_debug('body = ' . var_export($body, true));
93                 return false;
94             }
95         } else {
96             common_log(LOG_INFO, 'RSSCloud plugin - ' .
97                        "failure testing notify handler:  $endpoint " .
98                        ' - got HTTP ' . $status);
99             common_debug('body = ' . var_export($body, true));
100             return false;
101         }
102     }
103
104     /**
105      * HTTP POST a notification that a feed has been updated
106      * ('ping the cloud').
107      *
108      * @param String $endpoint URL of the notification handler
109      * @param String $feed     the feed being subscribed to
110      *
111      * @return boolean success
112      */
113     function postUpdate($endpoint, $feed)
114     {
115         $headers  = array();
116         $postdata = array('url' => $feed);
117
118         try {
119             $client   = new HTTPClient();
120             $response = $client->post($endpoint, $headers, $postdata);
121         } catch (HTTP_Request2_Exception $e) {
122             common_log(LOG_INFO, 'RSSCloud plugin - failure notifying ' .
123                        $endpoint . ' that feed ' . $feed .
124                        ' has changed: ' . $e->getMessage());
125             return false;
126         }
127
128         $status = $response->getStatus();
129
130         if ($status >= 200 && $status < 300) {
131             common_log(LOG_INFO, 'RSSCloud plugin - success notifying ' .
132                        $endpoint . ' that feed ' . $feed . ' has changed.');
133             return true;
134         } else {
135             common_log(LOG_INFO, 'RSSCloud plugin - failure notifying ' .
136                        $endpoint . ' that feed ' . $feed .
137                        ' has changed: got HTTP ' . $status);
138             return false;
139         }
140     }
141
142     /**
143      * Notify all subscribers to a profile feed that it has changed.
144      *
145      * @param Profile $profile the profile whose feed has been
146      *        updated
147      *
148      * @return boolean success
149      */
150     function notify($profile)
151     {
152         $feed = common_path('api/statuses/user_timeline/') .
153           $profile->id . '.rss';
154
155         $cloudSub = new RSSCloudSubscription();
156
157         $cloudSub->subscribed = $profile->id;
158
159         if ($cloudSub->find()) {
160             while ($cloudSub->fetch()) {
161                 $result = $this->postUpdate($cloudSub->url, $feed);
162                 if ($result == false) {
163                     $this->handleFailure($cloudSub);
164                 }
165             }
166         }
167
168         return true;
169     }
170
171     /**
172      * Handle problems posting cloud notifications. Increment the failure
173      * count, or delete the subscription if the maximum number of failures
174      * is exceeded.
175      *
176      * XXX: Redo with proper DB_DataObject methods once I figure out what
177      * what the problem is with pluginized DB_DataObjects. -Z
178      *
179      * @param RSSCloudSubscription $cloudSub the subscription in question
180      *
181      * @return boolean success
182      */
183     function handleFailure($cloudSub)
184     {
185         $failCnt = $cloudSub->failures + 1;
186
187         if ($failCnt == self::MAX_FAILURES) {
188
189             common_log(LOG_INFO,
190                        'Deleting RSSCloud subcription ' .
191                        '(max failure count reached), profile: ' .
192                        $cloudSub->subscribed .
193                        ' handler: ' .
194                        $cloudSub->url);
195
196             // XXX: WTF! ->delete() doesn't work. Clearly, there are some issues with
197             // the DB_DataObject, or my understanding of it.  Have to drop into SQL.
198
199             // $result = $cloudSub->delete();
200
201             $qry = 'DELETE from rsscloud_subscription' .
202               ' WHERE subscribed = ' . $cloudSub->subscribed .
203               ' AND url = \'' . $cloudSub->url . '\'';
204
205             $result = $cloudSub->query($qry);
206
207             if (!$result) {
208                 common_log_db_error($cloudSub, 'DELETE', __FILE__);
209                 common_log(LOG_ERR, 'Could not delete RSSCloud subscription.');
210             }
211         } else {
212             common_debug('Updating failure count on RSSCloud subscription. ' .
213                          $failCnt);
214
215             $failCnt = $cloudSub->failures + 1;
216
217             // XXX: ->update() not working either, gar!
218
219             $qry = 'UPDATE rsscloud_subscription' .
220               ' SET failures = ' . $failCnt .
221               ' WHERE subscribed = ' . $cloudSub->subscribed .
222               ' AND url = \'' . $cloudSub->url . '\'';
223
224             $result = $cloudSub->query($qry);
225
226             if (!$result) {
227                 common_log_db_error($cloudsub, 'UPDATE', __FILE__);
228                 common_log(LOG_ERR,
229                            'Could not update failure ' .
230                            'count on RSSCloud subscription');
231             }
232         }
233     }
234 }