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