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