]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FeedSub/actions/feedsubcallback.php
Initial functional version of feed subscription plugin, currently supporting only...
[quix0rs-gnu-social.git] / plugins / FeedSub / actions / feedsubcallback.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 FeedSubCallbackAction extends Action
29 {
30     function handle()
31     {
32         parent::handle();
33         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
34             $this->handlePost();
35         } else {
36             $this->handleGet();
37         }
38     }
39     
40     /**
41      * Handler for POST content updates from the hub
42      */
43     function handlePost()
44     {
45         $feedid = $this->arg('feed');
46         common_log(LOG_INFO, "POST for feed id $feedid");
47         if (!$feedid) {
48             throw new ServerException('Empty or invalid feed id', 400);
49         }
50
51         $feedinfo = Feedinfo::staticGet('id', $feedid);
52         if (!$feedinfo) {
53             throw new ServerException('Unknown feed id ' . $feedid, 400);
54         }
55         
56         $post = file_get_contents('php://input');
57         $feedinfo->postUpdates($post);
58     }
59     
60     /**
61      * Handler for GET verification requests from the hub
62      */
63     function handleGet()
64     {
65         $mode = $this->arg('hub_mode');
66         $topic = $this->arg('hub_topic');
67         $challenge = $this->arg('hub_challenge');
68         $lease_seconds = $this->arg('hub_lease_seconds');
69         $verify_token = $this->arg('hub_verify_token');
70         
71         if ($mode != 'subscribe' && $mode != 'unsubscribe') {
72             common_log(LOG_WARNING, __METHOD__ . ": bogus hub callback with mode \"$mode\"");
73             throw new ServerException("Bogus hub callback: bad mode", 404);
74         }
75         
76         $feedinfo = Feedinfo::staticGet('feeduri', $topic);
77         if (!$feedinfo) {
78             common_log(LOG_WARNING, __METHOD__ . ": bogus hub callback for unknown feed $topic");
79             throw new ServerException("Bogus hub callback: unknown feed", 404);
80         }
81
82         # Can't currently set the token in our sub api
83         #if ($feedinfo->verify_token !== $verify_token) {
84         #    common_log(LOG_WARNING, __METHOD__ . ": bogus hub callback with bad token \"$verify_token\" for feed $topic");
85         #    throw new ServerError("Bogus hub callback: bad token", 404);
86         #}
87         
88         // OK!
89         common_log(LOG_INFO, __METHOD__ . ': sub confirmed');
90         $feedinfo->sub_start = common_sql_date(time());
91         if ($lease_seconds > 0) {
92             $feedinfo->sub_end = common_sql_date(time() + $lease_seconds);
93         } else {
94             $feedinfo->sub_end = null;
95         }
96         $feedinfo->update();
97         
98         print $challenge;
99     }
100 }