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