]> git.mxchange.org Git - friendica.git/blob - mod/pubsubhubbub.php
Merge pull request #6977 from annando/fix-reshare
[friendica.git] / mod / pubsubhubbub.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\Config;
5 use Friendica\Core\Logger;
6 use Friendica\Core\System;
7 use Friendica\Database\DBA;
8 use Friendica\Model\PushSubscriber;
9 use Friendica\Util\Network;
10 use Friendica\Util\Strings;
11
12 function post_var($name) {
13         return !empty($_POST[$name]) ? Strings::escapeTags(trim($_POST[$name])) : '';
14 }
15
16 function pubsubhubbub_init(App $a) {
17         // PuSH subscription must be considered "public" so just block it
18         // if public access isn't enabled.
19         if (Config::get('system', 'block_public')) {
20                 System::httpExit(403);
21         }
22
23         // Subscription request from subscriber
24         // https://pubsubhubbub.googlecode.com/git/pubsubhubbub-core-0.4.html#anchor4
25         // Example from GNU Social:
26         // [hub_mode] => subscribe
27         // [hub_callback] => http://status.local/main/push/callback/1
28         // [hub_verify] => sync
29         // [hub_verify_token] => af11...
30         // [hub_secret] => af11...
31         // [hub_topic] => http://friendica.local/dfrn_poll/sazius
32
33         if ($_SERVER['REQUEST_METHOD'] === 'POST') {
34                 $hub_mode = post_var('hub_mode');
35                 $hub_callback = post_var('hub_callback');
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::log("Invalid hub_mode=$hub_mode, ignoring.");
47                         System::httpExit(404);
48                 }
49
50                 Logger::log("$hub_mode request from " . $_SERVER['REMOTE_ADDR']);
51
52                 if ($a->argc > 1) {
53                         // Normally the url should now contain the nick name as last part of the url
54                         $nick = $a->argv[1];
55                 } else {
56                         // Get the nick name from the topic as a fallback
57                         $nick = $hub_topic;
58                 }
59                 // Extract nick name and strip any .atom extension
60                 $nick = basename($nick, '.atom');
61
62                 if (!$nick) {
63                         Logger::log('Bad hub_topic=$hub_topic, ignoring.');
64                         System::httpExit(404);
65                 }
66
67                 // fetch user from database given the nickname
68                 $condition = ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false];
69                 $owner = DBA::selectFirst('user', ['uid', 'hidewall', 'nickname'], $condition);
70                 if (!DBA::isResult($owner)) {
71                         Logger::log('Local account not found: ' . $nick . ' - topic: ' . $hub_topic . ' - callback: ' . $hub_callback);
72                         System::httpExit(404);
73                 }
74
75                 // abort if user's wall is supposed to be private
76                 if ($owner['hidewall']) {
77                         Logger::log('Local user ' . $nick . 'has chosen to hide wall, ignoring.');
78                         System::httpExit(403);
79                 }
80
81                 // get corresponding row from contact table
82                 $condition = ['uid' => $owner['uid'], 'blocked' => false,
83                         'pending' => false, 'self' => true];
84                 $contact = DBA::selectFirst('contact', ['poll'], $condition);
85                 if (!DBA::isResult($contact)) {
86                         Logger::log('Self contact for user ' . $owner['uid'] . ' not found.');
87                         System::httpExit(404);
88                 }
89
90                 // sanity check that topic URLs are the same
91                 $hub_topic2 = str_replace('/feed/', '/dfrn_poll/', $hub_topic);
92                 $self = System::baseUrl() . '/api/statuses/user_timeline/' . $owner['nickname'] . '.atom';
93
94                 if (!Strings::compareLink($hub_topic, $contact['poll']) && !Strings::compareLink($hub_topic2, $contact['poll']) && !Strings::compareLink($hub_topic, $self)) {
95                         Logger::log('Hub topic ' . $hub_topic . ' != ' . $contact['poll']);
96                         System::httpExit(404);
97                 }
98
99                 // do subscriber verification according to the PuSH protocol
100                 $hub_challenge = Strings::getRandomHex(40);
101
102                 $params = http_build_query([
103                         'hub.mode' => $subscribe == 1 ? 'subscribe' : 'unsubscribe',
104                         'hub.topic' => $hub_topic,
105                         'hub.challenge' => $hub_challenge,
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                         'hub.lease_seconds' => 604800,
112                 ]);
113
114                 $hub_callback = rtrim($hub_callback, ' ?&#');
115                 $separator = parse_url($hub_callback, PHP_URL_QUERY) === null ? '?' : '&';
116
117                 $fetchResult = Network::fetchUrlFull($hub_callback . $separator . $params);
118                 $body = $fetchResult->getBody();
119                 $ret = $fetchResult->getReturnCode();
120
121                 // give up if the HTTP return code wasn't a success (2xx)
122                 if ($ret < 200 || $ret > 299) {
123                         Logger::log("Subscriber verification for $hub_topic at $hub_callback returned $ret, ignoring.");
124                         System::httpExit(404);
125                 }
126
127                 // check that the correct hub_challenge code was echoed back
128                 if (trim($body) !== $hub_challenge) {
129                         Logger::log("Subscriber did not echo back hub.challenge, ignoring.");
130                         Logger::log("\"$hub_challenge\" != \"".trim($body)."\"");
131                         System::httpExit(404);
132                 }
133
134                 PushSubscriber::renew($owner['uid'], $nick, $subscribe, $hub_callback, $hub_topic, $hub_secret);
135
136                 System::httpExit(202);
137         }
138         exit();
139 }