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