4 use Friendica\Core\Logger;
5 use Friendica\Database\DBA;
7 use Friendica\Model\PushSubscriber;
8 use Friendica\Util\Network;
9 use Friendica\Util\Strings;
11 function post_var($name) {
12 return !empty($_POST[$name]) ? Strings::escapeTags(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 (DI::config()->get('system', 'block_public')) {
19 throw new \Friendica\Network\HTTPException\ForbiddenException();
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_token = post_var('hub_verify_token');
36 $hub_secret = post_var('hub_secret');
37 $hub_topic = post_var('hub_topic');
39 // check for valid hub_mode
40 if ($hub_mode === 'subscribe') {
42 } elseif ($hub_mode === 'unsubscribe') {
45 Logger::log("Invalid hub_mode=$hub_mode, ignoring.");
46 throw new \Friendica\Network\HTTPException\NotFoundException();
49 Logger::log("$hub_mode request from " . $_SERVER['REMOTE_ADDR']);
52 // Normally the url should now contain the nick name as last part of the url
55 // Get the nick name from the topic as a fallback
58 // Extract nick name and strip any .atom extension
59 $nick = basename($nick, '.atom');
62 Logger::log('Bad hub_topic=$hub_topic, ignoring.');
63 throw new \Friendica\Network\HTTPException\NotFoundException();
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 throw new \Friendica\Network\HTTPException\NotFoundException();
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 throw new \Friendica\Network\HTTPException\ForbiddenException();
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 throw new \Friendica\Network\HTTPException\NotFoundException();
89 // sanity check that topic URLs are the same
90 $hub_topic2 = str_replace('/feed/', '/dfrn_poll/', $hub_topic);
91 $self = DI::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 throw new \Friendica\Network\HTTPException\NotFoundException();
98 // do subscriber verification according to the PuSH protocol
99 $hub_challenge = Strings::getRandomHex(40);
101 $params = http_build_query([
102 'hub.mode' => $subscribe == 1 ? 'subscribe' : 'unsubscribe',
103 'hub.topic' => $hub_topic,
104 'hub.challenge' => $hub_challenge,
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)
110 'hub.lease_seconds' => 604800,
113 $hub_callback = rtrim($hub_callback, ' ?&#');
114 $separator = parse_url($hub_callback, PHP_URL_QUERY) === null ? '?' : '&';
116 $fetchResult = Network::fetchUrlFull($hub_callback . $separator . $params);
117 $body = $fetchResult->getBody();
118 $ret = $fetchResult->getReturnCode();
120 // give up if the HTTP return code wasn't a success (2xx)
121 if ($ret < 200 || $ret > 299) {
122 Logger::log("Subscriber verification for $hub_topic at $hub_callback returned $ret, ignoring.");
123 throw new \Friendica\Network\HTTPException\NotFoundException();
126 // check that the correct hub_challenge code was echoed back
127 if (trim($body) !== $hub_challenge) {
128 Logger::log("Subscriber did not echo back hub.challenge, ignoring.");
129 Logger::log("\"$hub_challenge\" != \"".trim($body)."\"");
130 throw new \Friendica\Network\HTTPException\NotFoundException();
133 PushSubscriber::renew($owner['uid'], $nick, $subscribe, $hub_callback, $hub_topic, $hub_secret);
135 throw new \Friendica\Network\HTTPException\AcceptedException();