]> git.mxchange.org Git - friendica.git/blob - mod/pubsubhubbub.php
Move xml_status and http_status_exit
[friendica.git] / mod / pubsubhubbub.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\Config;
5 use Friendica\Database\DBM;
6 use Friendica\Util\Network;
7
8 function post_var($name) {
9         return (x($_POST, $name)) ? notags(trim($_POST[$name])) : '';
10 }
11
12 function pubsubhubbub_init(App $a) {
13         // PuSH subscription must be considered "public" so just block it
14         // if public access isn't enabled.
15         if (Config::get('system', 'block_public')) {
16                 Network::httpStatusExit(403);
17         }
18
19         // Subscription request from subscriber
20         // https://pubsubhubbub.googlecode.com/git/pubsubhubbub-core-0.4.html#anchor4
21         // Example from GNU Social:
22         // [hub_mode] => subscribe
23         // [hub_callback] => http://status.local/main/push/callback/1
24         // [hub_verify] => sync
25         // [hub_verify_token] => af11...
26         // [hub_secret] => af11...
27         // [hub_topic] => http://friendica.local/dfrn_poll/sazius
28
29         if ($_SERVER['REQUEST_METHOD'] === 'POST') {
30                 $hub_mode = post_var('hub_mode');
31                 $hub_callback = post_var('hub_callback');
32                 $hub_verify = post_var('hub_verify');
33                 $hub_verify_token = post_var('hub_verify_token');
34                 $hub_secret = post_var('hub_secret');
35                 $hub_topic = post_var('hub_topic');
36
37                 // check for valid hub_mode
38                 if ($hub_mode === 'subscribe') {
39                         $subscribe = 1;
40                 } elseif ($hub_mode === 'unsubscribe') {
41                         $subscribe = 0;
42                 } else {
43                         logger("pubsubhubbub: invalid hub_mode=$hub_mode, ignoring.");
44                         Network::httpStatusExit(404);
45                 }
46
47                 logger("pubsubhubbub: $hub_mode request from " .
48                            $_SERVER['REMOTE_ADDR']);
49
50                 // get the nick name from the topic, a bit hacky but needed as a fallback
51                 $nick = substr(strrchr($hub_topic, "/"), 1);
52
53                 // Normally the url should now contain the nick name as last part of the url
54                 if ($a->argc > 1) {
55                         $nick = $a->argv[1];
56                 }
57
58                 if (!$nick) {
59                         logger('pubsubhubbub: bad hub_topic=$hub_topic, ignoring.');
60                         Network::httpStatusExit(404);
61                 }
62
63                 // fetch user from database given the nickname
64                 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s'" .
65                            " AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
66                            dbesc($nick));
67
68                 if (!DBM::is_result($r)) {
69                         logger('pubsubhubbub: local account not found: ' . $nick);
70                         Network::httpStatusExit(404);
71                 }
72
73                 $owner = $r[0];
74
75                 // abort if user's wall is supposed to be private
76                 if ($r[0]['hidewall']) {
77                         logger('pubsubhubbub: local user ' . $nick .
78                                    'has chosen to hide wall, ignoring.');
79                         Network::httpStatusExit(403);
80                 }
81
82                 // get corresponding row from contact table
83                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `blocked`".
84                            " AND NOT `pending` AND `self` LIMIT 1",
85                            intval($owner['uid']));
86                 if (!DBM::is_result($r)) {
87                         logger('pubsubhubbub: contact not found.');
88                         Network::httpStatusExit(404);
89                 }
90
91                 $contact = $r[0];
92
93                 // sanity check that topic URLs are the same
94                 if (!link_compare($hub_topic, $contact['poll'])) {
95                         logger('pubsubhubbub: hub topic ' . $hub_topic . ' != ' .
96                                    $contact['poll']);
97                         Network::httpStatusExit(404);
98                 }
99
100                 // do subscriber verification according to the PuSH protocol
101                 $hub_challenge = random_string(40);
102                 $params = 'hub.mode=' .
103                         ($subscribe == 1 ? 'subscribe' : 'unsubscribe') .
104                         '&hub.topic=' . urlencode($hub_topic) .
105                         '&hub.challenge=' . $hub_challenge .
106                         '&hub.lease_seconds=604800' .
107                         '&hub.verify_token=' . $hub_verify_token;
108
109                 // lease time is hard coded to one week (in seconds)
110                 // we don't actually enforce the lease time because GNU
111                 // Social/StatusNet doesn't honour it (yet)
112
113                 $body = Network::fetchURL($hub_callback . "?" . $params);
114                 $ret = $a->get_curl_code();
115
116                 // give up if the HTTP return code wasn't a success (2xx)
117                 if ($ret < 200 || $ret > 299) {
118                         logger("pubsubhubbub: subscriber verification at $hub_callback ".
119                                    "returned $ret, ignoring.");
120                         Network::httpStatusExit(404);
121                 }
122
123                 // check that the correct hub_challenge code was echoed back
124                 if (trim($body) !== $hub_challenge) {
125                         logger("pubsubhubbub: subscriber did not echo back ".
126                                    "hub.challenge, ignoring.");
127                         logger("\"$hub_challenge\" != \"".trim($body)."\"");
128                         Network::httpStatusExit(404);
129                 }
130
131                 // fetch the old subscription if it exists
132                 $r = q("SELECT * FROM `push_subscriber` WHERE `callback_url` = '%s'",
133                   dbesc($hub_callback));
134
135                 // delete old subscription if it exists
136                 q("DELETE FROM `push_subscriber` WHERE `callback_url` = '%s'",
137                   dbesc($hub_callback));
138
139                 if ($subscribe) {
140                         $last_update = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
141                         $push_flag = 0;
142
143                         // if we are just updating an old subscription, keep the
144                         // old values for push and last_update
145                         if (DBM::is_result($r)) {
146                                 $last_update = $r[0]['last_update'];
147                                 $push_flag = $r[0]['push'];
148                         }
149
150                         // subscribe means adding the row to the table
151                         q("INSERT INTO `push_subscriber` (`uid`, `callback_url`, " .
152                           "`topic`, `nickname`, `push`, `last_update`, `secret`) values " .
153                           "(%d, '%s', '%s', '%s', %d, '%s', '%s')",
154                           intval($owner['uid']),
155                           dbesc($hub_callback),
156                           dbesc($hub_topic),
157                           dbesc($nick),
158                           intval($push_flag),
159                           dbesc($last_update),
160                           dbesc($hub_secret));
161                         logger("pubsubhubbub: successfully subscribed [$hub_callback].");
162                 } else {
163                         logger("pubsubhubbub: successfully unsubscribed [$hub_callback].");
164                         // we do nothing here, since the row was already deleted
165                 }
166                 Network::httpStatusExit(202);
167         }
168
169         killme();
170 }