3 * StatusNet, the distributed open-source microblogging tool
5 * Class to ping an rssCloud endpoint when a feed has been updated
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.
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.
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/>.
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/
30 if (!defined('STATUSNET')) {
35 * Class for notifying cloud-enabled RSS aggregators that StatusNet
36 * feeds have been updated.
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/
44 class RSSCloudNotifier
46 const MAX_FAILURES = 3;
49 * Send an HTTP GET to the notification handler with a
50 * challenge string to see if it repsonds correctly.
52 * @param string $endpoint URL of the notification handler
53 * @param string $feed the feed being subscribed to
55 * @return boolean success
57 function challenge($endpoint, $feed)
59 $code = common_confirmation_code(128);
60 $params = array('url' => $feed, 'challenge' => $code);
61 $url = $endpoint . '?' . http_build_query($params);
64 $client = new HTTPClient();
65 $response = $client->get($url);
66 } catch (HTTP_Request2_Exception $e) {
68 'RSSCloud plugin - failure testing notify handler ' .
69 $endpoint . ' - ' . $e->getMessage());
73 // Check response is betweet 200 and 299 and body contains challenge data
75 $status = $response->getStatus();
76 $body = $response->getBody();
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.
84 if (strpos($body, $code) !== false) {
85 common_log(LOG_INFO, 'RSSCloud plugin - ' .
86 "success testing notify handler: $endpoint");
89 common_log(LOG_INFO, 'RSSCloud plugin - ' .
90 'challenge/repsonse failed for notify handler ' .
92 common_debug('body = ' . var_export($body, true));
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));
105 * HTTP POST a notification that a feed has been updated
106 * ('ping the cloud').
108 * @param String $endpoint URL of the notification handler
109 * @param String $feed the feed being subscribed to
111 * @return boolean success
113 function postUpdate($endpoint, $feed)
116 $postdata = array('url' => $feed);
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());
128 $status = $response->getStatus();
130 if ($status >= 200 && $status < 300) {
131 common_log(LOG_INFO, 'RSSCloud plugin - success notifying ' .
132 $endpoint . ' that feed ' . $feed . ' has changed.');
135 common_log(LOG_INFO, 'RSSCloud plugin - failure notifying ' .
136 $endpoint . ' that feed ' . $feed .
137 ' has changed: got HTTP ' . $status);
143 * Notify all subscribers to a profile feed that it has changed.
145 * @param Profile $profile the profile whose feed has been
148 * @return boolean success
150 function notify($profile)
152 $feed = common_path('api/statuses/user_timeline/') .
153 $profile->id . '.rss';
155 $cloudSub = new RSSCloudSubscription();
157 $cloudSub->subscribed = $profile->id;
159 if ($cloudSub->find()) {
160 while ($cloudSub->fetch()) {
161 $result = $this->postUpdate($cloudSub->url, $feed);
162 if ($result == false) {
163 $this->handleFailure($cloudSub);
172 * Handle problems posting cloud notifications. Increment the failure
173 * count, or delete the subscription if the maximum number of failures
176 * XXX: Redo with proper DB_DataObject methods once I figure out what
177 * what the problem is with pluginized DB_DataObjects. -Z
179 * @param RSSCloudSubscription $cloudSub the subscription in question
181 * @return boolean success
183 function handleFailure($cloudSub)
185 $failCnt = $cloudSub->failures + 1;
187 if ($failCnt == self::MAX_FAILURES) {
190 'Deleting RSSCloud subcription ' .
191 '(max failure count reached), profile: ' .
192 $cloudSub->subscribed .
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.
199 // $result = $cloudSub->delete();
201 $qry = 'DELETE from rsscloud_subscription' .
202 ' WHERE subscribed = ' . $cloudSub->subscribed .
203 ' AND url = \'' . $cloudSub->url . '\'';
205 $result = $cloudSub->query($qry);
208 common_log_db_error($cloudSub, 'DELETE', __FILE__);
209 common_log(LOG_ERR, 'Could not delete RSSCloud subscription.');
212 common_debug('Updating failure count on RSSCloud subscription. ' .
215 $failCnt = $cloudSub->failures + 1;
217 // XXX: ->update() not working either, gar!
219 $qry = 'UPDATE rsscloud_subscription' .
220 ' SET failures = ' . $failCnt .
221 ' WHERE subscribed = ' . $cloudSub->subscribed .
222 ' AND url = \'' . $cloudSub->url . '\'';
224 $result = $cloudSub->query($qry);
227 common_log_db_error($cloudsub, 'UPDATE', __FILE__);
229 'Could not update failure ' .
230 'count on RSSCloud subscription');