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;
11 function post_var($name) {
12 return (x($_POST, $name)) ? notags(trim($_POST[$name])) : '';
15 function pubsubhubbub_init(App $a) {
16 // PuSH subscription must be considered "public" so just block it
17 // if public access isn't enabled.
18 if (Config::get('system', 'block_public')) {
19 System::httpExit(403);
22 // Subscription request from subscriber
23 // https://pubsubhubbub.googlecode.com/git/pubsubhubbub-core-0.4.html#anchor4
24 // Example from GNU Social:
25 // [hub_mode] => subscribe
26 // [hub_callback] => http://status.local/main/push/callback/1
27 // [hub_verify] => sync
28 // [hub_verify_token] => af11...
29 // [hub_secret] => af11...
30 // [hub_topic] => http://friendica.local/dfrn_poll/sazius
32 if ($_SERVER['REQUEST_METHOD'] === 'POST') {
33 $hub_mode = post_var('hub_mode');
34 $hub_callback = post_var('hub_callback');
35 $hub_verify = post_var('hub_verify');
36 $hub_verify_token = post_var('hub_verify_token');
37 $hub_secret = post_var('hub_secret');
38 $hub_topic = post_var('hub_topic');
40 // check for valid hub_mode
41 if ($hub_mode === 'subscribe') {
43 } elseif ($hub_mode === 'unsubscribe') {
46 Logger::log("Invalid hub_mode=$hub_mode, ignoring.");
47 System::httpExit(404);
50 Logger::log("$hub_mode request from " . $_SERVER['REMOTE_ADDR']);
52 // get the nick name from the topic, a bit hacky but needed as a fallback
53 $nick = substr(strrchr($hub_topic, "/"), 1);
55 // Normally the url should now contain the nick name as last part of the url
61 Logger::log('Bad hub_topic=$hub_topic, ignoring.');
62 System::httpExit(404);
65 // fetch user from database given the nickname
66 $condition = ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false];
67 $owner = DBA::selectFirst('user', ['uid', 'hidewall'], $condition);
68 if (!DBA::isResult($owner)) {
69 Logger::log('Local account not found: ' . $nick . ' - topic: ' . $hub_topic . ' - callback: ' . $hub_callback);
70 System::httpExit(404);
73 // abort if user's wall is supposed to be private
74 if ($owner['hidewall']) {
75 Logger::log('Local user ' . $nick . 'has chosen to hide wall, ignoring.');
76 System::httpExit(403);
79 // get corresponding row from contact table
80 $condition = ['uid' => $owner['uid'], 'blocked' => false,
81 'pending' => false, 'self' => true];
82 $contact = DBA::selectFirst('contact', ['poll'], $condition);
83 if (!DBA::isResult($contact)) {
84 Logger::log('Self contact for user ' . $owner['uid'] . ' not found.');
85 System::httpExit(404);
88 // sanity check that topic URLs are the same
89 $hub_topic2 = str_replace('/feed/', '/dfrn_poll/', $hub_topic);
90 if (!link_compare($hub_topic, $contact['poll']) && !link_compare($hub_topic2, $contact['poll'])) {
91 Logger::log('Hub topic ' . $hub_topic . ' != ' . $contact['poll']);
92 System::httpExit(404);
95 // do subscriber verification according to the PuSH protocol
96 $hub_challenge = random_string(40);
97 $params = 'hub.mode=' .
98 ($subscribe == 1 ? 'subscribe' : 'unsubscribe') .
99 '&hub.topic=' . urlencode($hub_topic) .
100 '&hub.challenge=' . $hub_challenge .
101 '&hub.lease_seconds=604800' .
102 '&hub.verify_token=' . $hub_verify_token;
104 // lease time is hard coded to one week (in seconds)
105 // we don't actually enforce the lease time because GNU
106 // Social/StatusNet doesn't honour it (yet)
108 $fetchResult = Network::fetchUrlFull($hub_callback . "?" . $params);
109 $body = $fetchResult->getBody();
110 $ret = $fetchResult->getReturnCode();
112 // give up if the HTTP return code wasn't a success (2xx)
113 if ($ret < 200 || $ret > 299) {
114 Logger::log("Subscriber verification for $hub_topic at $hub_callback returned $ret, ignoring.");
115 System::httpExit(404);
118 // check that the correct hub_challenge code was echoed back
119 if (trim($body) !== $hub_challenge) {
120 Logger::log("Subscriber did not echo back hub.challenge, ignoring.");
121 Logger::log("\"$hub_challenge\" != \"".trim($body)."\"");
122 System::httpExit(404);
125 PushSubscriber::renew($owner['uid'], $nick, $subscribe, $hub_callback, $hub_topic, $hub_secret);
127 System::httpExit(202);