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;
12 function post_var($name) {
13 return (x($_POST, $name)) ? Strings::escapeTags(trim($_POST[$name])) : '';
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);
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
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');
41 // check for valid hub_mode
42 if ($hub_mode === 'subscribe') {
44 } elseif ($hub_mode === 'unsubscribe') {
47 Logger::log("Invalid hub_mode=$hub_mode, ignoring.");
48 System::httpExit(404);
51 Logger::log("$hub_mode request from " . $_SERVER['REMOTE_ADDR']);
53 // get the nick name from the topic, a bit hacky but needed as a fallback
54 $nick = substr(strrchr($hub_topic, "/"), 1);
56 // Normally the url should now contain the nick name as last part of the url
62 Logger::log('Bad hub_topic=$hub_topic, ignoring.');
63 System::httpExit(404);
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', 'nickname'], $condition);
69 if (!DBA::isResult($owner)) {
70 Logger::log('Local account not found: ' . $nick . ' - topic: ' . $hub_topic . ' - callback: ' . $hub_callback);
71 System::httpExit(404);
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);
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);
89 // sanity check that topic URLs are the same
90 $hub_topic2 = str_replace('/feed/', '/dfrn_poll/', $hub_topic);
91 $self = System::baseUrl() . '/api/statuses/user_timeline/' . $owner['nickname'] . '.atom';
93 if (!Strings::compareLink($hub_topic, $contact['poll']) && !Strings::compareLink($hub_topic2, $contact['poll']) && !Strings::compareLink($hub_topic, $self)) {
94 Logger::log('Hub topic ' . $hub_topic . ' != ' . $contact['poll']);
95 System::httpExit(404);
98 // do subscriber verification according to the PuSH protocol
99 $hub_challenge = Strings::getRandomHex(40);
100 $params = 'hub.mode=' .
101 ($subscribe == 1 ? 'subscribe' : 'unsubscribe') .
102 '&hub.topic=' . urlencode($hub_topic) .
103 '&hub.challenge=' . $hub_challenge .
104 '&hub.lease_seconds=604800' .
105 '&hub.verify_token=' . $hub_verify_token;
107 // lease time is hard coded to one week (in seconds)
108 // we don't actually enforce the lease time because GNU
109 // Social/StatusNet doesn't honour it (yet)
111 $fetchResult = Network::fetchUrlFull($hub_callback . "?" . $params);
112 $body = $fetchResult->getBody();
113 $ret = $fetchResult->getReturnCode();
115 // give up if the HTTP return code wasn't a success (2xx)
116 if ($ret < 200 || $ret > 299) {
117 Logger::log("Subscriber verification for $hub_topic at $hub_callback returned $ret, ignoring.");
118 System::httpExit(404);
121 // check that the correct hub_challenge code was echoed back
122 if (trim($body) !== $hub_challenge) {
123 Logger::log("Subscriber did not echo back hub.challenge, ignoring.");
124 Logger::log("\"$hub_challenge\" != \"".trim($body)."\"");
125 System::httpExit(404);
128 PushSubscriber::renew($owner['uid'], $nick, $subscribe, $hub_callback, $hub_topic, $hub_secret);
130 System::httpExit(202);