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) {
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.
85 if (strpos($body, $code) !== false) {
86 common_log(LOG_INFO, 'RSSCloud plugin - ' .
87 "success testing notify handler: $endpoint");
90 common_log(LOG_INFO, 'RSSCloud plugin - ' .
91 'challenge/repsonse failed for notify handler ' .
93 common_debug('body = ' . var_export($body, true));
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));
106 * HTTP POST a notification that a feed has been updated
107 * ('ping the cloud').
109 * @param String $endpoint URL of the notification handler
110 * @param String $feed the feed being subscribed to
112 * @return boolean success
114 function postUpdate($endpoint, $feed)
118 $postdata = array('url' => $feed);
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());
130 $status = $response->getStatus();
132 if ($status >= 200 && $status < 300) {
133 common_log(LOG_INFO, 'RSSCloud plugin - success notifying ' .
134 $endpoint . ' that feed ' . $feed . ' has changed.');
137 common_log(LOG_INFO, 'RSSCloud plugin - failure notifying ' .
138 $endpoint . ' that feed ' . $feed .
139 ' has changed: got HTTP ' . $status);
145 * Notify all subscribers to a profile feed that it has changed.
147 * @param Profile $profile the profile whose feed has been
150 * @return boolean success
152 function notify($profile)
154 $feed = common_path('api/statuses/user_timeline/') .
155 $profile->id . '.rss';
157 $cloudSub = new RSSCloudSubscription();
159 $cloudSub->subscribed = $profile->id;
161 if ($cloudSub->find()) {
162 while ($cloudSub->fetch()) {
163 $result = $this->postUpdate($cloudSub->url, $feed);
164 if ($result == false) {
165 $this->handleFailure($cloudSub);
174 * Handle problems posting cloud notifications. Increment the failure
175 * count, or delete the subscription if the maximum number of failures
178 * XXX: Redo with proper DB_DataObject methods once I figure out what
179 * what the problem is with pluginized DB_DataObjects. -Z
181 * @param RSSCloudSubscription $cloudSub the subscription in question
183 * @return boolean success
185 function handleFailure($cloudSub)
187 $failCnt = $cloudSub->failures + 1;
189 if ($failCnt == self::MAX_FAILURES) {
192 'Deleting RSSCloud subcription ' .
193 '(max failure count reached), profile: ' .
194 $cloudSub->subscribed .
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.
201 // $result = $cloudSub->delete();
203 $qry = 'DELETE from rsscloud_subscription' .
204 ' WHERE subscribed = ' . $cloudSub->subscribed .
205 ' AND url = \'' . $cloudSub->url . '\'';
207 $result = $cloudSub->query($qry);
210 common_log_db_error($cloudSub, 'DELETE', __FILE__);
211 common_log(LOG_ERR, 'Could not delete RSSCloud subscription.');
216 common_debug('Updating failure count on RSSCloud subscription. ' .
219 $failCnt = $cloudSub->failures + 1;
221 // XXX: ->update() not working either, gar!
223 $qry = 'UPDATE rsscloud_subscription' .
224 ' SET failures = ' . $failCnt .
225 ' WHERE subscribed = ' . $cloudSub->subscribed .
226 ' AND url = \'' . $cloudSub->url . '\'';
228 $result = $cloudSub->query($qry);
231 common_log_db_error($cloudsub, 'UPDATE', __FILE__);
233 'Could not update failure ' .
234 'count on RSSCloud subscription');