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.

176 lines
6.0 KiB

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