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.

157 lines
5.1 KiB

  1. <?php
  2. function push_error(&$app, $msg) {
  3. $app->response()->status(400);
  4. echo $msg . "\n";
  5. die();
  6. }
  7. function push_param($params, $name) {
  8. // Look 'mode' first, fall back to 'hub_mode'
  9. if(k($params, $name))
  10. return k($params, $name);
  11. return k($params, 'hub_'.$name);
  12. }
  13. ///////////////////////////////////////////////////////////////
  14. // These are just test routes
  15. $app->get('/callback-success', function() use($app) {
  16. $params = $app->request()->params();
  17. $app->response()->status(200);
  18. echo $params['hub_challenge'];
  19. });
  20. $app->post('/callback-success', function() use($app) {
  21. $params = $app->request()->params();
  22. $app->response()->status(200);
  23. });
  24. $app->get('/callback-fail', function() use($app) {
  25. $params = $app->request()->params();
  26. $app->response()->status(404);
  27. });
  28. ///////////////////////////////////////////////////////////////
  29. function verify_push_topic_url($topic, &$app) {
  30. // If we've already seen the topic, assume it's valid and don't check it again
  31. if(!db\feed_from_url($topic)) {
  32. $topic_head = request\get_head($topic);
  33. if($topic_head && !request\response_is($topic_head['status'], 2)) {
  34. push_error($app, "The topic URL returned a " . $topic_head['status'] . " status code");
  35. } elseif(!$topic_head) {
  36. push_error($app, 'We tried to verify the topic URL exists but it didn\'t respond to a HEAD request.');
  37. }
  38. }
  39. }
  40. $app->post('/', function() use($app) {
  41. $params = $app->request()->params();
  42. switch($mode=push_param($params, 'mode')) {
  43. case 'subscribe':
  44. case 'unsubscribe':
  45. // Sanity check the request params
  46. $topic = push_param($params, 'topic');
  47. $callback = push_param($params, 'callback');
  48. if(!$topic) {
  49. push_error($app, 'No topic URL was specified. Send the topic URL in a parameter named "topic"');
  50. }
  51. if(!$callback) {
  52. push_error($app, 'No callback URL was specified. Send the callback URL in a parameter named "callback"');
  53. }
  54. if(!is_valid_push_url($topic)) {
  55. push_error($app, 'Topic URL was invalid ('.$topic.')');
  56. }
  57. if(!is_valid_push_url($callback)) {
  58. push_error($app, 'Callback URL was invalid');
  59. }
  60. if($mode == 'subscribe') {
  61. verify_push_topic_url($topic, $app);
  62. // Find or create the feed given the topic URL
  63. $feed = db\find_or_create('feeds', ['feed_url'=>$topic], [
  64. 'hash' => db\random_hash(),
  65. ], true);
  66. // Find or create the subscription for this callback URL and feed
  67. $subscription = db\find_or_create('subscriptions', ['feed_id'=>$feed->id, 'callback_url'=>$callback], [
  68. 'hash' => db\random_hash()
  69. ], true);
  70. // Always set a new requested date and challenge
  71. $subscription->date_requested = db\now();
  72. $subscription->challenge = db\random_hash();
  73. db\set_updated($subscription);
  74. $subscription->save();
  75. // Queue the worker to validate the subscription
  76. DeferredTask::queue('PushTask', 'verify_subscription', [$subscription->id, 'subscribe']);
  77. } else {
  78. $feed = db\feed_from_url($topic);
  79. if(!$feed) {
  80. push_error($app, 'The topic was not found, so there is no subscription active');
  81. }
  82. $subscription = db\find('subscriptions', ['feed_id'=>$feed->id, 'callback_url'=>$callback]);
  83. if(!$subscription) {
  84. push_error($app, 'There was no subscription found for this callback URL and topic');
  85. }
  86. // Queue the worker to validate the subscription
  87. DeferredTask::queue('PushTask', 'verify_subscription', [$subscription->id, 'unsubscribe']);
  88. }
  89. $app->response()->status(202);
  90. echo "The subscription request is being validated. Check the status here:\n";
  91. echo Config::$base_url . '/subscription/' . $subscription->hash . "\n";
  92. break;
  93. case 'publish':
  94. // Sanity check the request params
  95. $url = push_param($params, 'url');
  96. // Allow publishers to use either "url" or "topic" to indicate the URL that changed
  97. if(!$url) {
  98. $url = push_param($params, 'topic');
  99. }
  100. if(!$url) {
  101. push_error($app, 'No URL was specified. When publishing, send the topic URL in a parameter named "url"');
  102. }
  103. if(!is_valid_push_url($url)) {
  104. push_error($app, 'URL was invalid');
  105. }
  106. verify_push_topic_url($url, $app);
  107. // Find or create the feed given the topic URL
  108. $feed = db\find_or_create('feeds', ['feed_url'=>$url], [
  109. 'hash' => db\random_hash(),
  110. ], true);
  111. $num_subscribers = ORM::for_table('subscriptions')->where('feed_id', $feed->id)->where('active', 1)->count();
  112. $feed->push_last_ping_received = db\now();
  113. db\set_updated($feed);
  114. $feed->save();
  115. // Queue the worker to ping all the subscribers about the new content
  116. DeferredTask::queue('PushTask', 'publish', $feed->id);
  117. $app->response()->status(202);
  118. echo "There are currently $num_subscribers active subscriptions for this feed.\n";
  119. echo "The hub is checking the feed for new content and notifying the subscribers.\nCheck the status here:\n";
  120. echo Config::$base_url . '/feed/' . $feed->hash . "\n";
  121. break;
  122. }
  123. });