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