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.

60 lines
1.7 KiB

  1. <?php
  2. use BarnabyWalters\Mf2;
  3. class PushTask {
  4. public static function verify_subscription($subscription_id) {
  5. $subscription = db\get_by_id('subscriptions', $subscription_id);
  6. if($subscription) {
  7. $feed = db\get_by_id('feeds', $subscription->feed_id);
  8. $url = parse_url($subscription->callback_url);
  9. // Choose the expiration for the subscription
  10. $lease_seconds = 86400*3;
  11. $exp_ts = time() + $lease_seconds;
  12. $exp_date = date('Y-m-d H:i:s', $exp_ts);
  13. $push_params = [
  14. 'hub.mode' => 'subscribe',
  15. 'hub.topic' => $feed->feed_url,
  16. 'hub.challenge' => $subscription->challenge,
  17. 'hub.lease_seconds' => $lease_seconds
  18. ];
  19. if($q=k($url, 'query')) {
  20. parse_str($q, $existing_params);
  21. $push_params = array_merge($push_params, $existing_params);
  22. }
  23. $url['query'] = http_build_query($push_params);
  24. $url = build_url($url);
  25. $response = request\get_url($url, true);
  26. $subscription->challenge_response = $response['headers']."\n\n".$response['body'];
  27. if(request\response_is($response['status'], 2) && $response['body'] == $subscription->challenge) {
  28. // The subscriber replied with a 2xx status code and confirmed the challenge string.
  29. // The subscription is confirmed and active.
  30. $subscription->date_confirmed = db\now();
  31. $subscription->lease_seconds = $lease_seconds;
  32. $subscription->date_expires = $exp_date;
  33. db\set_updated($subscription);
  34. $subscription->active = 1;
  35. } else {
  36. // The subscriber did not confirm the subscription, so reject it
  37. }
  38. $subscription->save();
  39. print_r($response);
  40. }
  41. }
  42. }