3 * @copyright Copyright (C) 2020, Friendica
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\Protocol;
25 use Friendica\Database\DBA;
27 use Friendica\Model\Contact;
28 use Friendica\Protocol\OStatus;
29 use Friendica\Util\Strings;
30 use Friendica\Util\Network;
31 use Friendica\Core\System;
33 function hub_return($valid, $body)
38 throw new \Friendica\Network\HTTPException\NotFoundException();
43 // when receiving an XML feed, always return OK
45 function hub_post_return()
47 throw new \Friendica\Network\HTTPException\OKException();
50 function pubsub_init(App $a)
52 $nick = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : '');
53 $contact_id = (($a->argc > 2) ? intval($a->argv[2]) : 0 );
55 if ($_SERVER['REQUEST_METHOD'] === 'GET') {
56 $hub_mode = Strings::escapeTags(trim($_GET['hub_mode'] ?? ''));
57 $hub_topic = Strings::escapeTags(trim($_GET['hub_topic'] ?? ''));
58 $hub_challenge = Strings::escapeTags(trim($_GET['hub_challenge'] ?? ''));
59 $hub_verify = Strings::escapeTags(trim($_GET['hub_verify_token'] ?? ''));
61 Logger::log('Subscription from ' . $_SERVER['REMOTE_ADDR'] . ' Mode: ' . $hub_mode . ' Nick: ' . $nick);
62 Logger::log('Data: ' . print_r($_GET,true), Logger::DATA);
64 $subscribe = (($hub_mode === 'subscribe') ? 1 : 0);
66 $owner = DBA::selectFirst('user', ['uid'], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
67 if (!DBA::isResult($owner)) {
68 Logger::log('Local account not found: ' . $nick);
69 hub_return(false, '');
72 $condition = ['uid' => $owner['uid'], 'id' => $contact_id, 'blocked' => false, 'pending' => false];
74 if (!empty($hub_verify)) {
75 $condition['hub-verify'] = $hub_verify;
78 $contact = DBA::selectFirst('contact', ['id', 'poll'], $condition);
79 if (!DBA::isResult($contact)) {
80 Logger::log('Contact ' . $contact_id . ' not found.');
81 hub_return(false, '');
84 if (!empty($hub_topic) && !Strings::compareLink($hub_topic, $contact['poll'])) {
85 Logger::log('Hub topic ' . $hub_topic . ' != ' . $contact['poll']);
86 hub_return(false, '');
89 // We must initiate an unsubscribe request with a verify_token.
90 // Don't allow outsiders to unsubscribe us.
92 if (($hub_mode === 'unsubscribe') && empty($hub_verify)) {
93 Logger::log('Bogus unsubscribe');
94 hub_return(false, '');
97 if (!empty($hub_mode)) {
98 DBA::update('contact', ['subhub' => $subscribe], ['id' => $contact['id']]);
99 Logger::log($hub_mode . ' success for contact ' . $contact_id . '.');
101 hub_return(true, $hub_challenge);
105 function pubsub_post(App $a)
107 $xml = Network::postdata();
109 Logger::log('Feed arrived from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . DI::args()->getCommand() . ' with user-agent: ' . $_SERVER['HTTP_USER_AGENT']);
110 Logger::log('Data: ' . $xml, Logger::DATA);
112 $nick = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : '');
113 $contact_id = (($a->argc > 2) ? intval($a->argv[2]) : 0 );
115 $importer = DBA::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
116 if (!DBA::isResult($importer)) {
120 $condition = ['id' => $contact_id, 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
121 $contact = DBA::selectFirst('contact', [], $condition);
123 if (!DBA::isResult($contact)) {
124 $author = OStatus::salmonAuthor($xml, $importer);
125 if (!empty($author['contact-id'])) {
126 $condition = ['id' => $author['contact-id'], 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
127 $contact = DBA::selectFirst('contact', [], $condition);
128 Logger::log('No record for ' . $nick .' with contact id ' . $contact_id . ' - using '.$author['contact-id'].' instead.');
130 if (!DBA::isResult($contact)) {
131 Logger::log('Contact ' . $author["author-link"] . ' (' . $contact_id . ') for user ' . $nick . " wasn't found - ignored. XML: " . $xml);
136 if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND]) && ($contact['network'] != Protocol::FEED)) {
137 Logger::log('Contact ' . $contact['id'] . ' is not expected to share with us - ignored.');
141 // We import feeds from OStatus, Friendica and ATOM/RSS.
142 /// @todo Check if Friendica posts really arrive here - otherwise we can discard some stuff
143 if (!in_array($contact['network'], [Protocol::OSTATUS, Protocol::DFRN, Protocol::FEED])) {
147 Logger::log('Import item for ' . $nick . ' from ' . $contact['nick'] . ' (' . $contact['id'] . ')');
149 consume_feed($xml, $importer, $contact, $feedhub);
151 // do it a second time for DFRN so that any children find their parents.
152 if ($contact['network'] === Protocol::DFRN) {
153 consume_feed($xml, $importer, $contact, $feedhub);