You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
6.9 KiB

8 years ago
  1. <?php
  2. use BarnabyWalters\Mf2;
  3. class PushTask {
  4. public static function verify_subscription($subscription_id, $mode) {
  5. $subscription = db\get_by_id('subscriptions', $subscription_id);
  6. if($subscription) {
  7. $feed = db\get_by_id('feeds', $subscription->feed_id);
  8. // Choose the expiration for the subscription
  9. $lease_seconds = 86400*7 + 3600;
  10. $exp_ts = time() + $lease_seconds;
  11. $exp_date = date('Y-m-d H:i:s', $exp_ts);
  12. if($subscription->namespaced) {
  13. $prefix = 'hub.';
  14. } else {
  15. $prefix = '';
  16. }
  17. $push_params = [
  18. $prefix.'mode' => ($mode == 'subscribe' ? 'subscribe' : 'unsubscribe'),
  19. $prefix.'topic' => $feed->feed_url,
  20. $prefix.'challenge' => $subscription->challenge
  21. ];
  22. if($mode == 'subscribe') {
  23. $push_params[$prefix.'lease_seconds'] = $lease_seconds;
  24. }
  25. echo "Verifying subscription to ".$feed->feed_url." for callback ".$subscription->callback_url."\n";
  26. $url = parse_url($subscription->callback_url);
  27. if($q=k($url, 'query')) {
  28. parse_str($q, $existing_params);
  29. $push_params = array_merge($push_params, $existing_params);
  30. }
  31. $url['query'] = http_build_query($push_params);
  32. $url = build_url($url);
  33. echo "\t".$url."\n";
  34. $response = request\get_url($url, true);
  35. $subscription->challenge_response = $response['headers']."\n\n".$response['body'];
  36. if(request\response_is($response['status'], 2) && $response['body'] == $subscription->challenge) {
  37. // The subscriber replied with a 2xx status code and confirmed the challenge string.
  38. if($mode == 'subscribe') {
  39. // The subscription is confirmed and active.
  40. $subscription->date_confirmed = db\now();
  41. $subscription->lease_seconds = $lease_seconds;
  42. $subscription->date_expires = $exp_date;
  43. $subscription->active = 1;
  44. echo "Subscriber verified the request and is now subscribed\n";
  45. } else {
  46. $subscription->date_unsubscribed = db\now();
  47. $subscription->date_expires = null;
  48. $subscription->active = 0;
  49. echo "Subscriber verified the request and is now unsubscribed\n";
  50. }
  51. } else {
  52. // The subscriber did not confirm the subscription, so reject the request
  53. echo "Subscriber did not echo the challenge\n";
  54. }
  55. db\set_updated($subscription);
  56. $subscription->save();
  57. print_r($response);
  58. } else {
  59. echo "Subscription not found\n";
  60. }
  61. }
  62. public static function publish($feed_id) {
  63. $feed = db\get_by_id('feeds', $feed_id);
  64. if($feed) {
  65. echo $feed->feed_url."\n";
  66. echo "Checking feed for updates...\n";
  67. // First check the feed to see if the content has changed since the last time we checked
  68. $response = request\get_url($feed->feed_url, true);
  69. if($response['status'] != 200) {
  70. echo "Feed did not return HTTP 200: ".$response['status'].". Skipping publish.\n";
  71. return;
  72. }
  73. $feed->last_retrieved = db\now();
  74. db\set_updated($feed);
  75. $content_hash = md5($response['body']);
  76. if($content_hash != $feed->content_hash) {
  77. $feed->content_hash = $content_hash;
  78. $headers = request\parse_headers($response['headers']);
  79. if(array_key_exists('Content-Type', $headers)) {
  80. $feed->content_type = $headers['Content-Type'];
  81. }
  82. $feed->content = $response['body'];
  83. $feed->save();
  84. $subscribers = ORM::for_table('subscriptions')->where('feed_id', $feed->id)->where('active', 1)->find_many();
  85. foreach($subscribers as $s) {
  86. echo "Queuing notification for feed_id=$feed_id ($feed->feed_url) subscription_id=$s->id ($s->callback_url)\n";
  87. DeferredTask::queue('PushTask', 'notify_subscriber', [$feed_id, $s->id, db\now()]);
  88. }
  89. if(count($subscribers) == 0) {
  90. echo "No active subscribers\n";
  91. }
  92. } else {
  93. $feed->save();
  94. echo "Feed body has the same content hash as last time, not notifying subscribers\n";
  95. }
  96. } else {
  97. echo "Feed not found\n";
  98. }
  99. }
  100. public static function notify_subscriber($feed_id, $subscription_id, $date_queued) {
  101. $feed = db\get_by_id('feeds', $feed_id);
  102. if(!$feed) {
  103. echo "Feed not found\n";
  104. return;
  105. }
  106. $subscription = db\get_by_id('subscriptions', $subscription_id);
  107. if(!$subscription) {
  108. echo "Subscription not found\n";
  109. return;
  110. }
  111. // If the job was put on the queue before the last ping was sent, ignore it.
  112. // This happens when there is a retry job in the delayed queue, and then the
  113. // publisher sends a new publish request and the subscriber responds to it immediately.
  114. if(strtotime($date_queued) < strtotime($subscription->date_last_ping_sent)) {
  115. echo "Job was queued before the last ping was sent by the publisher, skipping\n";
  116. return;
  117. }
  118. echo "Processing subscriber: " . $subscription->callback_url . "\n";
  119. // Subscription may be "active" but the expiration date may have passed.
  120. // If so, de-activate the subscription.
  121. if(strtotime($subscription->date_expires) < time()) {
  122. echo "Subscription expired!\n";
  123. $subscription->active = 0;
  124. db\set_updated($subscription);
  125. $subscription->save();
  126. return;
  127. }
  128. echo "Notifying subscriber!\n";
  129. $subscription->date_last_ping_sent = db\now();
  130. $response = request\post($subscription->callback_url, $feed->content, false, [
  131. 'Content-Type: ' . ($feed->content_type ?: 'text/plain')
  132. ]);
  133. $subscription->last_ping_status = $response['status'];
  134. $subscription->last_ping_headers = $response['headers'];
  135. $subscription->last_ping_body = $response['body'];
  136. echo "Subscriber return a " . $response['status'] . " HTTP status\n";
  137. if(request\response_is($response['status'], 2)) {
  138. $subscription->last_ping_success = 1;
  139. $subscription->last_ping_error_delay = 0;
  140. } else {
  141. $subscription->last_ping_success = 0;
  142. // If the ping failed, queue another ping for a later time with exponential backoff
  143. if($subscription->last_ping_error_delay == 0)
  144. $subscription->last_ping_error_delay = 15;
  145. // If it's timed out after 8 tries, de-activate the subscription
  146. if($subscription->last_ping_error_delay > 2000) {
  147. echo "Ping failed after " . $subscription->last_ping_error_delay . " seconds. Deactivating this subscription.\n";
  148. $subscription->active = 0;
  149. } else {
  150. echo "Ping failed, trying again in " . $subscription->last_ping_error_delay . " seconds\n";
  151. DeferredTask::queue('PushTask', 'notify_subscriber', [$feed_id, $subscription_id, db\now()], $subscription->last_ping_error_delay);
  152. $subscription->last_ping_error_delay = $subscription->last_ping_error_delay * 2;
  153. }
  154. }
  155. db\set_updated($subscription);
  156. $subscription->save();
  157. }
  158. }