rsscloudnotifier.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * Class for notifying cloud-enabled RSS aggregators that StatusNet
  34. * feeds have been updated.
  35. *
  36. * @category Plugin
  37. * @package StatusNet
  38. * @author Zach Copley <zach@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  40. * @link http://status.net/
  41. */
  42. class RSSCloudNotifier
  43. {
  44. const MAX_FAILURES = 3;
  45. /**
  46. * Send an HTTP GET to the notification handler with a
  47. * challenge string to see if it repsonds correctly.
  48. *
  49. * @param string $endpoint URL of the notification handler
  50. * @param string $feed the feed being subscribed to
  51. *
  52. * @return boolean success
  53. */
  54. function challenge($endpoint, $feed)
  55. {
  56. $code = common_confirmation_code(128);
  57. $params = array('url' => $feed, 'challenge' => $code);
  58. $url = $endpoint . '?' . http_build_query($params);
  59. try {
  60. $client = new HTTPClient();
  61. $response = $client->get($url);
  62. } catch (Exception $e) {
  63. common_log(LOG_INFO,
  64. 'RSSCloud plugin - failure testing notify handler ' .
  65. $endpoint . ' - ' . $e->getMessage());
  66. return false;
  67. }
  68. // Check response is betweet 200 and 299 and body contains challenge data
  69. $status = $response->getStatus();
  70. $body = $response->getBody();
  71. if ($status >= 200 && $status < 300) {
  72. // NOTE: the spec says that the body must contain the string
  73. // challenge. It doesn't say that the body must contain the
  74. // challenge string ONLY, although that seems to be the way
  75. // the other implementors have interpreted it.
  76. if (strpos($body, $code) !== false) {
  77. common_log(LOG_INFO, 'RSSCloud plugin - ' .
  78. "success testing notify handler: $endpoint");
  79. return true;
  80. } else {
  81. common_log(LOG_INFO, 'RSSCloud plugin - ' .
  82. 'challenge/repsonse failed for notify handler ' .
  83. $endpoint);
  84. common_debug('body = ' . var_export($body, true));
  85. return false;
  86. }
  87. } else {
  88. common_log(LOG_INFO, 'RSSCloud plugin - ' .
  89. "failure testing notify handler: $endpoint " .
  90. ' - got HTTP ' . $status);
  91. common_debug('body = ' . var_export($body, true));
  92. return false;
  93. }
  94. }
  95. /**
  96. * HTTP POST a notification that a feed has been updated
  97. * ('ping the cloud').
  98. *
  99. * @param String $endpoint URL of the notification handler
  100. * @param String $feed the feed being subscribed to
  101. *
  102. * @return boolean success
  103. */
  104. function postUpdate($endpoint, $feed)
  105. {
  106. $headers = array();
  107. $postdata = array('url' => $feed);
  108. try {
  109. $client = new HTTPClient();
  110. $response = $client->post($endpoint, $headers, $postdata);
  111. } catch (Exception $e) {
  112. common_log(LOG_INFO, 'RSSCloud plugin - failure notifying ' .
  113. $endpoint . ' that feed ' . $feed .
  114. ' has changed: ' . $e->getMessage());
  115. return false;
  116. }
  117. $status = $response->getStatus();
  118. if ($status >= 200 && $status < 300) {
  119. common_log(LOG_INFO, 'RSSCloud plugin - success notifying ' .
  120. $endpoint . ' that feed ' . $feed . ' has changed.');
  121. return true;
  122. } else {
  123. common_log(LOG_INFO, 'RSSCloud plugin - failure notifying ' .
  124. $endpoint . ' that feed ' . $feed .
  125. ' has changed: got HTTP ' . $status);
  126. return false;
  127. }
  128. }
  129. /**
  130. * Notify all subscribers to a profile feed that it has changed.
  131. *
  132. * @param Profile $profile the profile whose feed has been
  133. * updated
  134. *
  135. * @return boolean success
  136. */
  137. function notify($profile)
  138. {
  139. $feed = common_path('api/statuses/user_timeline/') .
  140. $profile->id . '.rss';
  141. $cloudSub = new RSSCloudSubscription();
  142. $cloudSub->subscribed = $profile->id;
  143. if ($cloudSub->find()) {
  144. while ($cloudSub->fetch()) {
  145. $result = $this->postUpdate($cloudSub->url, $feed);
  146. if ($result == false) {
  147. $this->handleFailure($cloudSub);
  148. }
  149. }
  150. }
  151. return true;
  152. }
  153. /**
  154. * Handle problems posting cloud notifications. Increment the failure
  155. * count, or delete the subscription if the maximum number of failures
  156. * is exceeded.
  157. *
  158. * XXX: Redo with proper DB_DataObject methods once I figure out what
  159. * what the problem is with pluginized DB_DataObjects. -Z
  160. *
  161. * @param RSSCloudSubscription $cloudSub the subscription in question
  162. *
  163. * @return boolean success
  164. */
  165. function handleFailure($cloudSub)
  166. {
  167. $failCnt = $cloudSub->failures + 1;
  168. if ($failCnt == self::MAX_FAILURES) {
  169. common_log(LOG_INFO,
  170. 'Deleting RSSCloud subcription ' .
  171. '(max failure count reached), profile: ' .
  172. $cloudSub->subscribed .
  173. ' handler: ' .
  174. $cloudSub->url);
  175. // XXX: WTF! ->delete() doesn't work. Clearly, there are some issues with
  176. // the DB_DataObject, or my understanding of it. Have to drop into SQL.
  177. // $result = $cloudSub->delete();
  178. $qry = 'DELETE from rsscloud_subscription' .
  179. ' WHERE subscribed = ' . $cloudSub->subscribed .
  180. ' AND url = \'' . $cloudSub->url . '\'';
  181. $result = $cloudSub->query($qry);
  182. if (!$result) {
  183. common_log_db_error($cloudSub, 'DELETE', __FILE__);
  184. common_log(LOG_ERR, 'Could not delete RSSCloud subscription.');
  185. }
  186. } else {
  187. common_debug('Updating failure count on RSSCloud subscription. ' .
  188. $failCnt);
  189. $failCnt = $cloudSub->failures + 1;
  190. // XXX: ->update() not working either, gar!
  191. $qry = 'UPDATE rsscloud_subscription' .
  192. ' SET failures = ' . $failCnt .
  193. ' WHERE subscribed = ' . $cloudSub->subscribed .
  194. ' AND url = \'' . $cloudSub->url . '\'';
  195. $result = $cloudSub->query($qry);
  196. if (!$result) {
  197. common_log_db_error($cloudsub, 'UPDATE', __FILE__);
  198. common_log(LOG_ERR,
  199. 'Could not update failure ' .
  200. 'count on RSSCloud subscription');
  201. }
  202. }
  203. }
  204. }