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.

70 lines
1.9 KiB

  1. <?php
  2. function push_error(&$app, $msg) {
  3. $app->response()->status(400);
  4. echo $msg . "\n";
  5. die();
  6. }
  7. $app->post('/', function() use($app) {
  8. $params = $app->request()->params();
  9. switch(k($params, 'hub_mode')) {
  10. case 'subscribe':
  11. // Sanity check the request params
  12. $topic = k($params, 'hub_topic');
  13. $callback = k($params, 'hub_callback');
  14. if(!is_valid_push_url($topic)) {
  15. push_error($app, 'Topic URL was invalid');
  16. }
  17. if(!is_valid_push_url($callback)) {
  18. push_error($app, 'Callback URL was invalid');
  19. }
  20. // Make a HEAD request to the topic URL to check if it exists
  21. $topic_head = request\get_head($topic);
  22. if($topic_head) {
  23. if(request\response_is($topic_head['status'], 2)) {
  24. $app->response()->status(202);
  25. // Find or create the feed given the topic URL
  26. $feed = db\find_or_create('feeds', ['feed_url'=>$topic], [
  27. 'hash' => db\random_hash(),
  28. ], true);
  29. print_r($feed);
  30. // Find or create the subscription for this callback URL and feed
  31. $subscription = db\find_or_create('subscriptions', ['feed_id'=>$feed->id, 'callback_url'=>$callback], [
  32. 'hash' => db\random_hash(),
  33. ]);
  34. $subscription->date_requested = db\now();
  35. $subscription->challenge = db\random_hash();
  36. $subscription->save();
  37. echo "The subscription request is being validated. Check the status here:\n";
  38. echo Config::$base_url . '/subscription/' . $subscription->hash . "\n";
  39. } else {
  40. $app->response()->status(400);
  41. echo "The topic URL returned a " . $topic_head['status'] . " status code\n";
  42. }
  43. } else {
  44. push_error($app, 'There was a problem trying to verify the topic URL');
  45. }
  46. break;
  47. case 'unsubscribe':
  48. break;
  49. }
  50. });