3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 use Friendica\Core\Logger;
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
27 use Friendica\Model\PushSubscriber;
28 use Friendica\Util\Strings;
30 function pubsubhubbub_init(App $a) {
31 // PuSH subscription must be considered "public" so just block it
32 // if public access isn't enabled.
33 if (DI::config()->get('system', 'block_public')) {
34 throw new \Friendica\Network\HTTPException\ForbiddenException();
37 // Subscription request from subscriber
38 // https://pubsubhubbub.googlecode.com/git/pubsubhubbub-core-0.4.html#anchor4
39 // Example from GNU Social:
40 // [hub_mode] => subscribe
41 // [hub_callback] => http://status.local/main/push/callback/1
42 // [hub_verify] => sync
43 // [hub_verify_token] => af11...
44 // [hub_secret] => af11...
45 // [hub_topic] => http://friendica.local/dfrn_poll/sazius
47 if (DI::args()->getMethod() === App\Router::POST) {
48 $hub_mode = $_POST['hub_mode'] ?? '';
49 $hub_callback = $_POST['hub_callback'] ?? '';
50 $hub_verify_token = $_POST['hub_verify_token'] ?? '';
51 $hub_secret = $_POST['hub_secret'] ?? '';
52 $hub_topic = $_POST['hub_topic'] ?? '';
54 // check for valid hub_mode
55 if ($hub_mode === 'subscribe') {
57 } elseif ($hub_mode === 'unsubscribe') {
60 Logger::notice("Invalid hub_mode=$hub_mode, ignoring.");
61 throw new \Friendica\Network\HTTPException\NotFoundException();
64 Logger::info("$hub_mode request from " . $_SERVER['REMOTE_ADDR']);
66 if (DI::args()->getArgc() > 1) {
67 // Normally the url should now contain the nick name as last part of the url
68 $nick = DI::args()->getArgv()[1];
70 // Get the nick name from the topic as a fallback
73 // Extract nick name and strip any .atom extension
74 $nick = basename($nick, '.atom');
77 Logger::notice('Bad hub_topic=$hub_topic, ignoring.');
78 throw new \Friendica\Network\HTTPException\NotFoundException();
81 // fetch user from database given the nickname
82 $condition = ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false];
83 $owner = DBA::selectFirst('user', ['uid', 'nickname'], $condition);
84 if (!DBA::isResult($owner)) {
85 Logger::notice('Local account not found: ' . $nick . ' - topic: ' . $hub_topic . ' - callback: ' . $hub_callback);
86 throw new \Friendica\Network\HTTPException\NotFoundException();
89 // get corresponding row from contact table
90 $condition = ['uid' => $owner['uid'], 'blocked' => false,
91 'pending' => false, 'self' => true];
92 $contact = DBA::selectFirst('contact', ['poll'], $condition);
93 if (!DBA::isResult($contact)) {
94 Logger::notice('Self contact for user ' . $owner['uid'] . ' not found.');
95 throw new \Friendica\Network\HTTPException\NotFoundException();
98 // sanity check that topic URLs are the same
99 $hub_topic2 = str_replace('/feed/', '/dfrn_poll/', $hub_topic);
100 $self = DI::baseUrl() . '/api/statuses/user_timeline/' . $owner['nickname'] . '.atom';
102 if (!Strings::compareLink($hub_topic, $contact['poll']) && !Strings::compareLink($hub_topic2, $contact['poll']) && !Strings::compareLink($hub_topic, $self)) {
103 Logger::notice('Hub topic ' . $hub_topic . ' != ' . $contact['poll']);
104 throw new \Friendica\Network\HTTPException\NotFoundException();
107 // do subscriber verification according to the PuSH protocol
108 $hub_challenge = Strings::getRandomHex(40);
110 $params = http_build_query([
111 'hub.mode' => $subscribe == 1 ? 'subscribe' : 'unsubscribe',
112 'hub.topic' => $hub_topic,
113 'hub.challenge' => $hub_challenge,
114 'hub.verify_token' => $hub_verify_token,
116 // lease time is hard coded to one week (in seconds)
117 // we don't actually enforce the lease time because GNU
118 // Social/StatusNet doesn't honour it (yet)
119 'hub.lease_seconds' => 604800,
122 $hub_callback = rtrim($hub_callback, ' ?&#');
123 $separator = parse_url($hub_callback, PHP_URL_QUERY) === null ? '?' : '&';
125 $fetchResult = DI::httpClient()->fetchFull($hub_callback . $separator . $params);
126 $body = $fetchResult->getBody();
127 $ret = $fetchResult->getReturnCode();
129 // give up if the HTTP return code wasn't a success (2xx)
130 if ($ret < 200 || $ret > 299) {
131 Logger::notice("Subscriber verification for $hub_topic at $hub_callback returned $ret, ignoring.");
132 throw new \Friendica\Network\HTTPException\NotFoundException();
135 // check that the correct hub_challenge code was echoed back
136 if (trim($body) !== $hub_challenge) {
137 Logger::notice("Subscriber did not echo back hub.challenge, ignoring.");
138 Logger::notice("\"$hub_challenge\" != \"".trim($body)."\"");
139 throw new \Friendica\Network\HTTPException\NotFoundException();
142 PushSubscriber::renew($owner['uid'], $nick, $subscribe, $hub_callback, $hub_topic, $hub_secret);
144 throw new \Friendica\Network\HTTPException\AcceptedException();