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.

161 lines
5.3 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, 'url');
  47. // Support subscribing using either "url" or "topic" parameters
  48. if(!$topic) {
  49. $topic = push_param($params, 'topic');
  50. }
  51. $callback = push_param($params, 'callback');
  52. if(!$topic) {
  53. push_error($app, 'No topic URL was specified. Send the topic URL in a parameter named "topic"');
  54. }
  55. if(!$callback) {
  56. push_error($app, 'No callback URL was specified. Send the callback URL in a parameter named "callback"');
  57. }
  58. if(!is_valid_push_url($topic)) {
  59. push_error($app, 'Topic URL was invalid ('.$topic.')');
  60. }
  61. if(!is_valid_push_url($callback)) {
  62. push_error($app, 'Callback URL was invalid');
  63. }
  64. if($mode == 'subscribe') {
  65. verify_push_topic_url($topic, $app);
  66. // Find or create the feed given the topic URL
  67. $feed = db\find_or_create('feeds', ['feed_url'=>$topic], [
  68. 'hash' => db\random_hash(),
  69. ], true);
  70. // Find or create the subscription for this callback URL and feed
  71. $subscription = db\find_or_create('subscriptions', ['feed_id'=>$feed->id, 'callback_url'=>$callback], [
  72. 'hash' => db\random_hash()
  73. ], true);
  74. // Always set a new requested date and challenge
  75. $subscription->date_requested = db\now();
  76. $subscription->challenge = db\random_hash();
  77. db\set_updated($subscription);
  78. $subscription->save();
  79. // Queue the worker to validate the subscription
  80. DeferredTask::queue('PushTask', 'verify_subscription', [$subscription->id, 'subscribe']);
  81. } else {
  82. $feed = db\feed_from_url($topic);
  83. if(!$feed) {
  84. push_error($app, 'The topic was not found, so there is no subscription active');
  85. }
  86. $subscription = db\find('subscriptions', ['feed_id'=>$feed->id, 'callback_url'=>$callback]);
  87. if(!$subscription) {
  88. push_error($app, 'There was no subscription found for this callback URL and topic');
  89. }
  90. // Queue the worker to validate the subscription
  91. DeferredTask::queue('PushTask', 'verify_subscription', [$subscription->id, 'unsubscribe']);
  92. }
  93. $app->response()->status(202);
  94. echo "The subscription request is being validated. Check the status here:\n";
  95. echo Config::$base_url . '/subscription/' . $subscription->hash . "\n";
  96. break;
  97. case 'publish':
  98. // Sanity check the request params
  99. $url = push_param($params, 'url');
  100. // Allow publishers to use either "url" or "topic" to indicate the URL that changed
  101. if(!$url) {
  102. $url = push_param($params, 'topic');
  103. }
  104. if(!$url) {
  105. push_error($app, 'No URL was specified. When publishing, send the topic URL in a parameter named "url"');
  106. }
  107. if(!is_valid_push_url($url)) {
  108. push_error($app, 'URL was invalid');
  109. }
  110. verify_push_topic_url($url, $app);
  111. // Find or create the feed given the topic URL
  112. $feed = db\find_or_create('feeds', ['feed_url'=>$url], [
  113. 'hash' => db\random_hash(),
  114. ], true);
  115. $num_subscribers = ORM::for_table('subscriptions')->where('feed_id', $feed->id)->where('active', 1)->count();
  116. $feed->push_last_ping_received = db\now();
  117. db\set_updated($feed);
  118. $feed->save();
  119. // Queue the worker to ping all the subscribers about the new content
  120. DeferredTask::queue('PushTask', 'publish', $feed->id);
  121. $app->response()->status(202);
  122. echo "There are currently $num_subscribers active subscriptions for this feed.\n";
  123. echo "The hub is checking the feed for new content and notifying the subscribers.\nCheck the status here:\n";
  124. echo Config::$base_url . '/feed/' . $feed->hash . "\n";
  125. break;
  126. }
  127. });