]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/pushcallback.php
Merge branch 'testing' into 0.9.x
[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') && !defined('LACONICA')) { exit(1); }
26
27
28 class PushCallbackAction extends Action
29 {
30     function handle()
31     {
32         StatusNet::setApi(true); // Minimize error messages to aid in debugging
33         parent::handle();
34         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
35             $this->handlePost();
36         } else {
37             $this->handleGet();
38         }
39     }
40
41     /**
42      * Handler for POST content updates from the hub
43      */
44     function handlePost()
45     {
46         $feedid = $this->arg('feed');
47         common_log(LOG_INFO, "POST for feed id $feedid");
48         if (!$feedid) {
49             throw new ServerException('Empty or invalid feed id.', 400);
50         }
51
52         $feedsub = FeedSub::staticGet('id', $feedid);
53         if (!$feedsub) {
54             // @todo i18n FIXME: added i18n and use sprintf when using parameters.
55             throw new ServerException('Unknown PuSH feed id ' . $feedid, 400);
56         }
57
58         $hmac = '';
59         if (isset($_SERVER['HTTP_X_HUB_SIGNATURE'])) {
60             $hmac = $_SERVER['HTTP_X_HUB_SIGNATURE'];
61         }
62
63         $post = file_get_contents('php://input');
64
65         // Queue this to a background process; we should return
66         // as quickly as possible from a distribution POST.
67         // If queues are disabled this'll process immediately.
68         $data = array('feedsub_id' => $feedsub->id,
69                       'post' => $post,
70                       'hmac' => $hmac);
71         $qm = QueueManager::get();
72         $qm->enqueue($data, 'pushin');
73     }
74
75     /**
76      * Handler for GET verification requests from the hub.
77      */
78     function handleGet()
79     {
80         $mode = $this->arg('hub_mode');
81         $topic = $this->arg('hub_topic');
82         $challenge = $this->arg('hub_challenge');
83         $lease_seconds = $this->arg('hub_lease_seconds');
84         $verify_token = $this->arg('hub_verify_token');
85
86         if ($mode != 'subscribe' && $mode != 'unsubscribe') {
87             throw new ClientException("Bad hub.mode $mode", 404);
88         }
89
90         $feedsub = FeedSub::staticGet('uri', $topic);
91         if (!$feedsub) {
92             // @todo i18n FIXME: added i18n and use sprintf when using parameters.
93             throw new ClientException("Bad hub.topic feed $topic.", 404);
94         }
95
96         if ($feedsub->verify_token !== $verify_token) {
97             // @todo i18n FIXME: added i18n and use sprintf when using parameters.
98             throw new ClientException("Bad hub.verify_token $token for $topic.", 404);
99         }
100
101         if ($mode == 'subscribe') {
102             // We may get re-sub requests legitimately.
103             if ($feedsub->sub_state != 'subscribe' && $feedsub->sub_state != 'active') {
104                 // @todo i18n FIXME: added i18n and use sprintf when using parameters.
105                 throw new ClientException("Unexpected subscribe request for $topic.", 404);
106             }
107         } else {
108             if ($feedsub->sub_state != 'unsubscribe') {
109                 // @todo i18n FIXME: added i18n and use sprintf when using parameters.
110                 throw new ClientException("Unexpected unsubscribe request for $topic.", 404);
111             }
112         }
113
114         if ($mode == 'subscribe') {
115             if ($feedsub->sub_state == 'active') {
116                 common_log(LOG_INFO, __METHOD__ . ': sub update confirmed');
117             } else {
118                 common_log(LOG_INFO, __METHOD__ . ': sub confirmed');
119             }
120             $feedsub->confirmSubscribe($lease_seconds);
121         } else {
122             common_log(LOG_INFO, __METHOD__ . ": unsub confirmed; deleting sub record for $topic");
123             $feedsub->confirmUnsubscribe();
124         }
125         print $challenge;
126     }
127 }