3 * @copyright Copyright (C) 2010-2021, 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\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\Model\GServer;
32 use Friendica\Model\Post;
34 function hub_return($valid, $body)
39 throw new \Friendica\Network\HTTPException\NotFoundException();
44 // when receiving an XML feed, always return OK
46 function hub_post_return()
48 throw new \Friendica\Network\HTTPException\OKException();
51 function pubsub_init(App $a)
53 $nick = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : '');
54 $contact_id = (($a->argc > 2) ? intval($a->argv[2]) : 0 );
56 if ($_SERVER['REQUEST_METHOD'] === 'GET') {
57 $hub_mode = Strings::escapeTags(trim($_GET['hub_mode'] ?? ''));
58 $hub_topic = Strings::escapeTags(trim($_GET['hub_topic'] ?? ''));
59 $hub_challenge = Strings::escapeTags(trim($_GET['hub_challenge'] ?? ''));
60 $hub_verify = Strings::escapeTags(trim($_GET['hub_verify_token'] ?? ''));
62 Logger::log('Subscription from ' . $_SERVER['REMOTE_ADDR'] . ' Mode: ' . $hub_mode . ' Nick: ' . $nick);
63 Logger::log('Data: ' . print_r($_GET,true), Logger::DATA);
65 $subscribe = (($hub_mode === 'subscribe') ? 1 : 0);
67 $owner = DBA::selectFirst('user', ['uid'], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
68 if (!DBA::isResult($owner)) {
69 Logger::log('Local account not found: ' . $nick);
70 hub_return(false, '');
73 $condition = ['uid' => $owner['uid'], 'id' => $contact_id, 'blocked' => false, 'pending' => false];
75 if (!empty($hub_verify)) {
76 $condition['hub-verify'] = $hub_verify;
79 $contact = DBA::selectFirst('contact', ['id', 'poll'], $condition);
80 if (!DBA::isResult($contact)) {
81 Logger::log('Contact ' . $contact_id . ' not found.');
82 hub_return(false, '');
85 if (!empty($hub_topic) && !Strings::compareLink($hub_topic, $contact['poll'])) {
86 Logger::log('Hub topic ' . $hub_topic . ' != ' . $contact['poll']);
87 hub_return(false, '');
90 // We must initiate an unsubscribe request with a verify_token.
91 // Don't allow outsiders to unsubscribe us.
93 if (($hub_mode === 'unsubscribe') && empty($hub_verify)) {
94 Logger::log('Bogus unsubscribe');
95 hub_return(false, '');
98 if (!empty($hub_mode)) {
99 DBA::update('contact', ['subhub' => $subscribe], ['id' => $contact['id']]);
100 Logger::log($hub_mode . ' success for contact ' . $contact_id . '.');
102 hub_return(true, $hub_challenge);
106 function pubsub_post(App $a)
108 $xml = Network::postdata();
110 Logger::log('Feed arrived from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . DI::args()->getCommand() . ' with user-agent: ' . $_SERVER['HTTP_USER_AGENT']);
111 Logger::log('Data: ' . $xml, Logger::DATA);
113 $nick = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : '');
114 $contact_id = (($a->argc > 2) ? intval($a->argv[2]) : 0 );
116 $importer = DBA::selectFirst('user', [], ['nickname' => $nick, 'account_expired' => false, 'account_removed' => false]);
117 if (!DBA::isResult($importer)) {
121 $condition = ['id' => $contact_id, 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
122 $contact = DBA::selectFirst('contact', [], $condition);
124 if (!DBA::isResult($contact)) {
125 $author = OStatus::salmonAuthor($xml, $importer);
126 if (!empty($author['contact-id'])) {
127 $condition = ['id' => $author['contact-id'], 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
128 $contact = DBA::selectFirst('contact', [], $condition);
129 Logger::log('No record for ' . $nick .' with contact id ' . $contact_id . ' - using '.$author['contact-id'].' instead.');
131 if (!DBA::isResult($contact)) {
132 Logger::log('Contact ' . $author["author-link"] . ' (' . $contact_id . ') for user ' . $nick . " wasn't found - ignored. XML: " . $xml);
137 if (!empty($contact['gsid'])) {
138 GServer::setProtocol($contact['gsid'], Post\DeliveryData::OSTATUS);
141 if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND]) && ($contact['network'] != Protocol::FEED)) {
142 Logger::log('Contact ' . $contact['id'] . ' is not expected to share with us - ignored.');
146 // We only import feeds from OStatus here
147 if ($contact['network'] != Protocol::OSTATUS) {
148 Logger::warning('Unexpected network', ['contact' => $contact]);
152 Logger::log('Import item for ' . $nick . ' from ' . $contact['nick'] . ' (' . $contact['id'] . ')');
154 OStatus::import($xml, $importer, $contact, $feedhub);