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 !empty($_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']);
54 // Normally the url should now contain the nick name as last part of the url
57 // Get the nick name from the topic as a fallback
60 // Extract nick name and strip any .atom extension
61 $nick = basename($nick, '.atom');
64 Logger::log('Bad hub_topic=$hub_topic, ignoring.');
65 System::httpExit(404);
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);
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);
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);
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';
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);
100 // do subscriber verification according to the PuSH protocol
101 $hub_challenge = Strings::getRandomHex(40);
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,
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,
115 $hub_callback = rtrim($hub_callback, ' ?&#');
116 $separator = parse_url($hub_callback, PHP_URL_QUERY) === null ? '?' : '&';
118 $fetchResult = Network::fetchUrlFull($hub_callback . $separator . $params);
119 $body = $fetchResult->getBody();
120 $ret = $fetchResult->getReturnCode();
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);
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);
135 PushSubscriber::renew($owner['uid'], $nick, $subscribe, $hub_callback, $hub_topic, $hub_secret);
137 System::httpExit(202);