]> git.mxchange.org Git - friendica.git/blob - mod/pubsubhubbub.php
notags calls
[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 (x($_POST, $name)) ? Strings::removeTags(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                 // get the nick name from the topic, a bit hacky but needed as a fallback
54                 $nick = substr(strrchr($hub_topic, "/"), 1);
55
56                 // Normally the url should now contain the nick name as last part of the url
57                 if ($a->argc > 1) {
58                         $nick = $a->argv[1];
59                 }
60
61                 if (!$nick) {
62                         Logger::log('Bad hub_topic=$hub_topic, ignoring.');
63                         System::httpExit(404);
64                 }
65
66                 // fetch user from database given the nickname
67                 $condition = ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false];
68                 $owner = DBA::selectFirst('user', ['uid', 'hidewall'], $condition);
69                 if (!DBA::isResult($owner)) {
70                         Logger::log('Local account not found: ' . $nick . ' - topic: ' . $hub_topic . ' - callback: ' . $hub_callback);
71                         System::httpExit(404);
72                 }
73
74                 // abort if user's wall is supposed to be private
75                 if ($owner['hidewall']) {
76                         Logger::log('Local user ' . $nick . 'has chosen to hide wall, ignoring.');
77                         System::httpExit(403);
78                 }
79
80                 // get corresponding row from contact table
81                 $condition = ['uid' => $owner['uid'], 'blocked' => false,
82                         'pending' => false, 'self' => true];
83                 $contact = DBA::selectFirst('contact', ['poll'], $condition);
84                 if (!DBA::isResult($contact)) {
85                         Logger::log('Self contact for user ' . $owner['uid'] . ' not found.');
86                         System::httpExit(404);
87                 }
88
89                 // sanity check that topic URLs are the same
90                 $hub_topic2 = str_replace('/feed/', '/dfrn_poll/', $hub_topic);
91                 if (!link_compare($hub_topic, $contact['poll']) && !link_compare($hub_topic2, $contact['poll'])) {
92                         Logger::log('Hub topic ' . $hub_topic . ' != ' . $contact['poll']);
93                         System::httpExit(404);
94                 }
95
96                 // do subscriber verification according to the PuSH protocol
97                 $hub_challenge = Strings::getRandomHex(40);
98                 $params = 'hub.mode=' .
99                         ($subscribe == 1 ? 'subscribe' : 'unsubscribe') .
100                         '&hub.topic=' . urlencode($hub_topic) .
101                         '&hub.challenge=' . $hub_challenge .
102                         '&hub.lease_seconds=604800' .
103                         '&hub.verify_token=' . $hub_verify_token;
104
105                 // lease time is hard coded to one week (in seconds)
106                 // we don't actually enforce the lease time because GNU
107                 // Social/StatusNet doesn't honour it (yet)
108
109                 $fetchResult = Network::fetchUrlFull($hub_callback . "?" . $params);
110                 $body = $fetchResult->getBody();
111                 $ret = $fetchResult->getReturnCode();
112
113                 // give up if the HTTP return code wasn't a success (2xx)
114                 if ($ret < 200 || $ret > 299) {
115                         Logger::log("Subscriber verification for $hub_topic at $hub_callback returned $ret, ignoring.");
116                         System::httpExit(404);
117                 }
118
119                 // check that the correct hub_challenge code was echoed back
120                 if (trim($body) !== $hub_challenge) {
121                         Logger::log("Subscriber did not echo back hub.challenge, ignoring.");
122                         Logger::log("\"$hub_challenge\" != \"".trim($body)."\"");
123                         System::httpExit(404);
124                 }
125
126                 PushSubscriber::renew($owner['uid'], $nick, $subscribe, $hub_callback, $hub_topic, $hub_secret);
127
128                 System::httpExit(202);
129         }
130         killme();
131 }