]> git.mxchange.org Git - friendica.git/blob - mod/pubsubhubbub.php
Rewrite Proxy module
[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 = post_var('hub_verify');
37                 $hub_verify_token = post_var('hub_verify_token');
38                 $hub_secret = post_var('hub_secret');
39                 $hub_topic = post_var('hub_topic');
40
41                 // check for valid hub_mode
42                 if ($hub_mode === 'subscribe') {
43                         $subscribe = 1;
44                 } elseif ($hub_mode === 'unsubscribe') {
45                         $subscribe = 0;
46                 } else {
47                         Logger::log("Invalid hub_mode=$hub_mode, ignoring.");
48                         System::httpExit(404);
49                 }
50
51                 Logger::log("$hub_mode request from " . $_SERVER['REMOTE_ADDR']);
52
53                 if ($a->argc > 1) {
54                         // Normally the url should now contain the nick name as last part of the url
55                         $nick = $a->argv[1];
56                 } else {
57                         // Get the nick name from the topic as a fallback
58                         $nick = $hub_topic;
59                 }
60                 // Extract nick name and strip any .atom extension
61                 $nick = basename($nick, '.atom');
62
63                 if (!$nick) {
64                         Logger::log('Bad hub_topic=$hub_topic, ignoring.');
65                         System::httpExit(404);
66                 }
67
68                 // fetch user from database given the nickname
69                 $condition = ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false];
70                 $owner = DBA::selectFirst('user', ['uid', 'hidewall', 'nickname'], $condition);
71                 if (!DBA::isResult($owner)) {
72                         Logger::log('Local account not found: ' . $nick . ' - topic: ' . $hub_topic . ' - callback: ' . $hub_callback);
73                         System::httpExit(404);
74                 }
75
76                 // abort if user's wall is supposed to be private
77                 if ($owner['hidewall']) {
78                         Logger::log('Local user ' . $nick . 'has chosen to hide wall, ignoring.');
79                         System::httpExit(403);
80                 }
81
82                 // get corresponding row from contact table
83                 $condition = ['uid' => $owner['uid'], 'blocked' => false,
84                         'pending' => false, 'self' => true];
85                 $contact = DBA::selectFirst('contact', ['poll'], $condition);
86                 if (!DBA::isResult($contact)) {
87                         Logger::log('Self contact for user ' . $owner['uid'] . ' not found.');
88                         System::httpExit(404);
89                 }
90
91                 // sanity check that topic URLs are the same
92                 $hub_topic2 = str_replace('/feed/', '/dfrn_poll/', $hub_topic);
93                 $self = System::baseUrl() . '/api/statuses/user_timeline/' . $owner['nickname'] . '.atom';
94
95                 if (!Strings::compareLink($hub_topic, $contact['poll']) && !Strings::compareLink($hub_topic2, $contact['poll']) && !Strings::compareLink($hub_topic, $self)) {
96                         Logger::log('Hub topic ' . $hub_topic . ' != ' . $contact['poll']);
97                         System::httpExit(404);
98                 }
99
100                 // do subscriber verification according to the PuSH protocol
101                 $hub_challenge = Strings::getRandomHex(40);
102
103                 $params = http_build_query([
104                         'hub.mode' => $subscribe == 1 ? 'subscribe' : 'unsubscribe',
105                         'hub.topic' => $hub_topic,
106                         'hub.challenge' => $hub_challenge,
107                         'hub.verify_token' => $hub_verify_token,
108
109                         // lease time is hard coded to one week (in seconds)
110                         // we don't actually enforce the lease time because GNU
111                         // Social/StatusNet doesn't honour it (yet)
112                         'hub.lease_seconds' => 604800,
113                 ]);
114
115                 $hub_callback = rtrim($hub_callback, ' ?&#');
116                 $separator = parse_url($hub_callback, PHP_URL_QUERY) === null ? '?' : '&';
117
118                 $fetchResult = Network::fetchUrlFull($hub_callback . $separator . $params);
119                 $body = $fetchResult->getBody();
120                 $ret = $fetchResult->getReturnCode();
121
122                 // give up if the HTTP return code wasn't a success (2xx)
123                 if ($ret < 200 || $ret > 299) {
124                         Logger::log("Subscriber verification for $hub_topic at $hub_callback returned $ret, ignoring.");
125                         System::httpExit(404);
126                 }
127
128                 // check that the correct hub_challenge code was echoed back
129                 if (trim($body) !== $hub_challenge) {
130                         Logger::log("Subscriber did not echo back hub.challenge, ignoring.");
131                         Logger::log("\"$hub_challenge\" != \"".trim($body)."\"");
132                         System::httpExit(404);
133                 }
134
135                 PushSubscriber::renew($owner['uid'], $nick, $subscribe, $hub_callback, $hub_topic, $hub_secret);
136
137                 System::httpExit(202);
138         }
139         killme();
140 }