]> git.mxchange.org Git - friendica.git/blob - mod/pubsubhubbub.php
Fixes:
[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\DBA;
7 use Friendica\Database\DBM;
8 use Friendica\Model\PushSubscriber;
9 use Friendica\Util\Network;
10
11 function post_var($name) {
12         return (x($_POST, $name)) ? notags(trim($_POST[$name])) : '';
13 }
14
15 function pubsubhubbub_init(App $a) {
16         // PuSH subscription must be considered "public" so just block it
17         // if public access isn't enabled.
18         if (Config::get('system', 'block_public')) {
19                 System::httpExit(403);
20         }
21
22         // Subscription request from subscriber
23         // https://pubsubhubbub.googlecode.com/git/pubsubhubbub-core-0.4.html#anchor4
24         // Example from GNU Social:
25         // [hub_mode] => subscribe
26         // [hub_callback] => http://status.local/main/push/callback/1
27         // [hub_verify] => sync
28         // [hub_verify_token] => af11...
29         // [hub_secret] => af11...
30         // [hub_topic] => http://friendica.local/dfrn_poll/sazius
31
32         if ($_SERVER['REQUEST_METHOD'] === 'POST') {
33                 $hub_mode = post_var('hub_mode');
34                 $hub_callback = post_var('hub_callback');
35                 $hub_verify = post_var('hub_verify');
36                 $hub_verify_token = post_var('hub_verify_token');
37                 $hub_secret = post_var('hub_secret');
38                 $hub_topic = post_var('hub_topic');
39
40                 // check for valid hub_mode
41                 if ($hub_mode === 'subscribe') {
42                         $subscribe = 1;
43                 } elseif ($hub_mode === 'unsubscribe') {
44                         $subscribe = 0;
45                 } else {
46                         logger("Invalid hub_mode=$hub_mode, ignoring.");
47                         System::httpExit(404);
48                 }
49
50                 logger("$hub_mode request from " . $_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('Bad hub_topic=$hub_topic, ignoring.');
62                         System::httpExit(404);
63                 }
64
65                 // fetch user from database given the nickname
66                 $condition = ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false];
67                 $owner = DBA::selectFirst('user', ['uid', 'hidewall'], $condition);
68                 if (!DBM::is_result($owner)) {
69                         logger('Local account not found: ' . $nick . ' - topic: ' . $hub_topic . ' - callback: ' . $hub_callback);
70                         System::httpExit(404);
71                 }
72
73                 // abort if user's wall is supposed to be private
74                 if ($owner['hidewall']) {
75                         logger('Local user ' . $nick . 'has chosen to hide wall, ignoring.');
76                         System::httpExit(403);
77                 }
78
79                 // get corresponding row from contact table
80                 $condition = ['uid' => $owner['uid'], 'blocked' => false,
81                         'pending' => false, 'self' => true];
82                 $contact = DBA::selectFirst('contact', ['poll'], $condition);
83                 if (!DBM::is_result($contact)) {
84                         logger('Self contact for user ' . $owner['uid'] . ' not found.');
85                         System::httpExit(404);
86                 }
87
88                 // sanity check that topic URLs are the same
89                 $hub_topic2 = str_replace('/feed/', '/dfrn_poll/', $hub_topic);
90                 if (!link_compare($hub_topic, $contact['poll']) && !link_compare($hub_topic2, $contact['poll'])) {
91                         logger('Hub topic ' . $hub_topic . ' != ' . $contact['poll']);
92                         System::httpExit(404);
93                 }
94
95                 // do subscriber verification according to the PuSH protocol
96                 $hub_challenge = random_string(40);
97                 $params = 'hub.mode=' .
98                         ($subscribe == 1 ? 'subscribe' : 'unsubscribe') .
99                         '&hub.topic=' . urlencode($hub_topic) .
100                         '&hub.challenge=' . $hub_challenge .
101                         '&hub.lease_seconds=604800' .
102                         '&hub.verify_token=' . $hub_verify_token;
103
104                 // lease time is hard coded to one week (in seconds)
105                 // we don't actually enforce the lease time because GNU
106                 // Social/StatusNet doesn't honour it (yet)
107
108                 $body = Network::fetchUrl($hub_callback . "?" . $params);
109                 $ret = $a->get_curl_code();
110
111                 // give up if the HTTP return code wasn't a success (2xx)
112                 if ($ret < 200 || $ret > 299) {
113                         logger("Subscriber verification for $hub_topic at $hub_callback returned $ret, ignoring.");
114                         System::httpExit(404);
115                 }
116
117                 // check that the correct hub_challenge code was echoed back
118                 if (trim($body) !== $hub_challenge) {
119                         logger("Subscriber did not echo back hub.challenge, ignoring.");
120                         logger("\"$hub_challenge\" != \"".trim($body)."\"");
121                         System::httpExit(404);
122                 }
123
124                 PushSubscriber::renew($owner['uid'], $nick, $subscribe, $hub_callback, $hub_topic, $hub_secret);
125
126                 System::httpExit(202);
127         }
128         killme();
129 }