123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- class PushCallbackAction extends Action
- {
- protected function handle()
- {
- GNUsocial::setApi(true);
- parent::handle();
- if ($this->isPost()) {
- return $this->handlePost();
- }
-
- return $this->handleGet();
- }
-
- function handlePost()
- {
- $feedid = $this->arg('feed');
- common_log(LOG_INFO, "POST for feed id $feedid");
- if (!$feedid) {
-
- throw new ServerException(_m('Empty or invalid feed id.'), 400);
- }
- $feedsub = FeedSub::getKV('id', $feedid);
- if (!$feedsub instanceof FeedSub) {
-
- throw new ServerException(sprintf(_m('Unknown PuSH feed id %s'),$feedid), 400);
- }
- $hmac = '';
- if (isset($_SERVER['HTTP_X_HUB_SIGNATURE'])) {
- $hmac = $_SERVER['HTTP_X_HUB_SIGNATURE'];
- }
- $post = file_get_contents('php://input');
-
-
-
- $data = array('feedsub_id' => $feedsub->id,
- 'post' => $post,
- 'hmac' => $hmac);
- $qm = QueueManager::get();
- $qm->enqueue($data, 'pushin');
- }
-
- function handleGet()
- {
- $mode = $this->arg('hub_mode');
- $topic = $this->arg('hub_topic');
- $challenge = $this->arg('hub_challenge');
- $lease_seconds = $this->arg('hub_lease_seconds');
- common_log(LOG_INFO, __METHOD__ . ": sub verification mode: $mode topic: $topic challenge: $challenge lease_seconds: $lease_seconds");
- if ($mode != 'subscribe' && $mode != 'unsubscribe') {
-
- throw new ClientException(sprintf(_m('Bad hub.mode "$s".',$mode)), 404);
- }
- $feedsub = FeedSub::getKV('uri', $topic);
- if (!$feedsub instanceof FeedSub) {
-
- throw new ClientException(sprintf(_m('Bad hub.topic feed "%s".'),$topic), 404);
- }
- if ($mode == 'subscribe') {
-
- if ($feedsub->sub_state != 'subscribe' && $feedsub->sub_state != 'active') {
-
- throw new ClientException(sprintf(_m('Unexpected subscribe request for %s.'),$topic), 404);
- }
- } else {
- if ($feedsub->sub_state != 'unsubscribe') {
-
- throw new ClientException(sprintf(_m('Unexpected unsubscribe request for %s.'),$topic), 404);
- }
- }
- if ($mode == 'subscribe') {
- if ($feedsub->sub_state == 'active') {
- common_log(LOG_INFO, __METHOD__ . ': sub update confirmed');
- } else {
- common_log(LOG_INFO, __METHOD__ . ': sub confirmed');
- }
- $feedsub->confirmSubscribe($lease_seconds);
- } else {
- common_log(LOG_INFO, __METHOD__ . ": unsub confirmed; deleting sub record for $topic");
- $feedsub->confirmUnsubscribe();
- }
- print $challenge;
- }
- }
|