]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/pushcallback.php
Normalize execution guards on OStatus php files; mostly helps cut down on annoying...
[quix0rs-gnu-social.git] / plugins / OStatus / actions / pushcallback.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /**
21  * @package FeedSubPlugin
22  * @maintainer Brion Vibber <brion@status.net>
23  */
24
25 if (!defined('STATUSNET')) {
26     exit(1);
27 }
28
29 class PushCallbackAction extends Action
30 {
31     function handle()
32     {
33         StatusNet::setApi(true); // Minimize error messages to aid in debugging
34         parent::handle();
35         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
36             $this->handlePost();
37         } else {
38             $this->handleGet();
39         }
40     }
41
42     /**
43      * Handler for POST content updates from the hub
44      */
45     function handlePost()
46     {
47         $feedid = $this->arg('feed');
48         common_log(LOG_INFO, "POST for feed id $feedid");
49         if (!$feedid) {
50             throw new ServerException(_m('Empty or invalid feed id.'), 400);
51         }
52
53         $feedsub = FeedSub::staticGet('id', $feedid);
54         if (!$feedsub) {
55             // TRANS: Server exception. %s is a feed ID.
56             throw new ServerException(sprintf(_m('Unknown PuSH feed id %s'),$feedid), 400);
57         }
58
59         $hmac = '';
60         if (isset($_SERVER['HTTP_X_HUB_SIGNATURE'])) {
61             $hmac = $_SERVER['HTTP_X_HUB_SIGNATURE'];
62         }
63
64         $post = file_get_contents('php://input');
65
66         // Queue this to a background process; we should return
67         // as quickly as possible from a distribution POST.
68         // If queues are disabled this'll process immediately.
69         $data = array('feedsub_id' => $feedsub->id,
70                       'post' => $post,
71                       'hmac' => $hmac);
72         $qm = QueueManager::get();
73         $qm->enqueue($data, 'pushin');
74     }
75
76     /**
77      * Handler for GET verification requests from the hub.
78      */
79     function handleGet()
80     {
81         $mode = $this->arg('hub_mode');
82         $topic = $this->arg('hub_topic');
83         $challenge = $this->arg('hub_challenge');
84         $lease_seconds = $this->arg('hub_lease_seconds');
85         $verify_token = $this->arg('hub_verify_token');
86         common_log(LOG_INFO, __METHOD__ . ": sub verification mode: $mode topic: $topic challenge: $challenge lease_seconds: $lease_seconds verify_token: $verify_token");
87
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);
91         }
92
93         $feedsub = FeedSub::staticGet('uri', $topic);
94         if (!$feedsub) {
95             // TRANS: Client exception. %s is an invalid feed name.
96             throw new ClientException(sprintf(_m('Bad hub.topic feed "%s".'),$topic), 404);
97         }
98
99         if ($feedsub->verify_token !== $verify_token) {
100             // TRANS: Client exception. %1$s the invalid token, %2$s is the topic for which the invalid token was given.
101             throw new ClientException(sprintf(_m('Bad hub.verify_token %1$s for %2$s.'),$token,$topic), 404);
102         }
103
104         if ($mode == 'subscribe') {
105             // We may get re-sub requests legitimately.
106             if ($feedsub->sub_state != 'subscribe' && $feedsub->sub_state != 'active') {
107                 // TRANS: Client exception. %s is an invalid topic.
108                 throw new ClientException(sprintf(_m('Unexpected subscribe request for %s.'),$topic), 404);
109             }
110         } else {
111             if ($feedsub->sub_state != 'unsubscribe') {
112                 // TRANS: Client exception. %s is an invalid topic.
113                 throw new ClientException(sprintf(_m('Unexpected unsubscribe request for %s.'),$topic), 404);
114             }
115         }
116
117         if ($mode == 'subscribe') {
118             if ($feedsub->sub_state == 'active') {
119                 common_log(LOG_INFO, __METHOD__ . ': sub update confirmed');
120             } else {
121                 common_log(LOG_INFO, __METHOD__ . ': sub confirmed');
122             }
123             $feedsub->confirmSubscribe($lease_seconds);
124         } else {
125             common_log(LOG_INFO, __METHOD__ . ": unsub confirmed; deleting sub record for $topic");
126             $feedsub->confirmUnsubscribe();
127         }
128         print $challenge;
129     }
130 }