3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @package FeedSubPlugin
22 * @maintainer Brion Vibber <brion@status.net>
25 if (!defined('STATUSNET')) {
29 class PushCallbackAction extends Action
31 protected function handle()
33 GNUsocial::setApi(true); // Minimize error messages to aid in debugging
35 if ($this->isPost()) {
36 return $this->handlePost();
39 return $this->handleGet();
43 * Handler for POST content updates from the hub
47 $feedid = $this->arg('feed');
48 common_log(LOG_INFO, "POST for feed id $feedid");
50 // TRANS: Server exception thrown when referring to a non-existing or empty feed.
51 throw new ServerException(_m('Empty or invalid feed id.'), 400);
54 $feedsub = FeedSub::getKV('id', $feedid);
55 if (!$feedsub instanceof FeedSub) {
56 // TRANS: Server exception. %s is a feed ID.
57 throw new ServerException(sprintf(_m('Unknown PuSH feed id %s'),$feedid), 400);
61 if (isset($_SERVER['HTTP_X_HUB_SIGNATURE'])) {
62 $hmac = $_SERVER['HTTP_X_HUB_SIGNATURE'];
65 $post = file_get_contents('php://input');
67 // Queue this to a background process; we should return
68 // as quickly as possible from a distribution POST.
69 // If queues are disabled this'll process immediately.
70 $data = array('feedsub_id' => $feedsub->id,
73 $qm = QueueManager::get();
74 $qm->enqueue($data, 'pushin');
78 * Handler for GET verification requests from the hub.
82 $mode = $this->arg('hub_mode');
83 $topic = $this->arg('hub_topic');
84 $challenge = $this->arg('hub_challenge');
85 $lease_seconds = $this->arg('hub_lease_seconds'); // Must be >0 for PuSH 0.4!
86 common_log(LOG_INFO, __METHOD__ . ": sub verification mode: $mode topic: $topic challenge: $challenge lease_seconds: $lease_seconds");
88 if ($mode != 'subscribe' && $mode != 'unsubscribe') {
89 // TRANS: Client exception. %s is an invalid value for hub.mode.
90 throw new ClientException(sprintf(_m('Bad hub.mode "$s".',$mode)), 404);
93 $feedsub = FeedSub::getKV('uri', $topic);
94 if (!$feedsub instanceof FeedSub) {
95 // TRANS: Client exception. %s is an invalid feed name.
96 throw new ClientException(sprintf(_m('Bad hub.topic feed "%s".'),$topic), 404);
99 if ($mode == 'subscribe') {
100 // We may get re-sub requests legitimately.
101 if ($feedsub->sub_state != 'subscribe' && $feedsub->sub_state != 'active') {
102 // TRANS: Client exception. %s is an invalid topic.
103 throw new ClientException(sprintf(_m('Unexpected subscribe request for %s.'),$topic), 404);
106 if ($feedsub->sub_state != 'unsubscribe') {
107 // TRANS: Client exception. %s is an invalid topic.
108 throw new ClientException(sprintf(_m('Unexpected unsubscribe request for %s.'),$topic), 404);
112 if ($mode == 'subscribe') {
113 if ($feedsub->sub_state == 'active') {
114 common_log(LOG_INFO, __METHOD__ . ': sub update confirmed');
116 common_log(LOG_INFO, __METHOD__ . ': sub confirmed');
118 $feedsub->confirmSubscribe($lease_seconds);
120 common_log(LOG_INFO, __METHOD__ . ": unsub confirmed; deleting sub record for $topic");
121 $feedsub->confirmUnsubscribe();