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/>.
22 namespace Friendica\Module\OStatus;
25 use Friendica\Core\L10n;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\System;
28 use Friendica\Database\Database;
29 use Friendica\Model\Contact;
30 use Friendica\Model\GServer;
31 use Friendica\Model\Post;
32 use Friendica\Module\Response;
33 use Friendica\Network\HTTPException;
34 use Friendica\Protocol\OStatus;
35 use Friendica\Util\Network;
36 use Friendica\Util\Profiler;
37 use Friendica\Util\Strings;
38 use Psr\Log\LoggerInterface;
40 class PubSub extends \Friendica\BaseModule
44 /** @var App\Request */
47 public function __construct(App\Request $request, Database $database, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
49 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
51 $this->database = $database;
52 $this->request = $request;
55 protected function post(array $request = [])
57 $xml = Network::postdata();
59 $this->logger->info('Feed arrived.', ['from' => $this->request->getRemoteAddress(), 'for' => $this->args->getCommand(), 'user-agent' => $this->server['HTTP_USER_AGENT']]);
60 $this->logger->debug('Data stream.', ['xml' => $xml]);
62 $nickname = $this->parameters['nickname'] ?? '';
63 $contact_id = $this->parameters['cid'] ?? 0;
65 $importer = $this->database->selectFirst('user', [], ['nickname' => $nickname, 'account_expired' => false, 'account_removed' => false]);
67 throw new HTTPException\OKException();
70 $condition = ['id' => $contact_id, 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
71 $contact = $this->database->selectFirst('contact', [], $condition);
73 $author = OStatus::salmonAuthor($xml, $importer);
74 if (!empty($author['contact-id'])) {
75 $condition = ['id' => $author['contact-id'], 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
76 $contact = $this->database->selectFirst('contact', [], $condition);
77 $this->logger->notice('No record found for nickname, using author entry instead.', ['nickname' => $nickname, 'contact-id' => $contact_id, 'author-contact-id' => $author['contact-id']]);
81 $this->logger->notice("Contact wasn't found - ignored.", ['author-link' => $author['author-link'], 'contact-id' => $contact_id, 'nickname' => $nickname, 'xml' => $xml]);
82 throw new HTTPException\OKException();
86 if (!empty($contact['gsid'])) {
87 GServer::setProtocol($contact['gsid'], Post\DeliveryData::OSTATUS);
90 if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND]) && ($contact['network'] != Protocol::FEED)) {
91 $this->logger->notice('Contact is not expected to share with us - ignored.', ['contact-id' => $contact['id']]);
92 throw new HTTPException\OKException();
95 // We only import feeds from OStatus here
96 if (!in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::OSTATUS])) {
97 $this->logger->warning('Unexpected network', ['contact' => $contact, 'network' => $contact['network']]);
98 throw new HTTPException\OKException();
101 $this->logger->info('Import item from Contact.', ['nickname' => $nickname, 'contact-nickname' => $contact['nick'], 'contact-id' => $contact['id']]);
103 OStatus::import($xml, $importer, $contact, $feedhub);
105 throw new HTTPException\OKException();
108 protected function rawContent(array $request = [])
110 $nickname = $this->parameters['nickname'] ?? '';
111 $contact_id = $this->parameters['cid'] ?? 0;
113 $hub_mode = trim($request['hub_mode'] ?? '');
114 $hub_topic = trim($request['hub_topic'] ?? '');
115 $hub_challenge = trim($request['hub_challenge'] ?? '');
116 $hub_verify = trim($request['hub_verify_token'] ?? '');
118 $this->logger->notice('Subscription start.', ['from' => $this->request->getRemoteAddress(), 'mode' => $hub_mode, 'nickname' => $nickname]);
119 $this->logger->debug('Data: ', ['get' => $request]);
121 $owner = $this->database->selectFirst('user', ['uid'], ['nickname' => $nickname, 'account_expired' => false, 'account_removed' => false]);
123 $this->logger->notice('Local account not found.', ['nickname' => $nickname]);
124 throw new HTTPException\NotFoundException();
127 $condition = ['uid' => $owner['uid'], 'id' => $contact_id, 'blocked' => false, 'pending' => false];
129 if (!empty($hub_verify)) {
130 $condition['hub-verify'] = $hub_verify;
133 $contact = $this->database->selectFirst('contact', ['id', 'poll'], $condition);
135 $this->logger->notice('Contact not found.', ['contact' => $contact_id]);
136 throw new HTTPException\NotFoundException();
139 if (!empty($hub_topic) && !Strings::compareLink($hub_topic, $contact['poll'])) {
140 $this->logger->notice("Hub topic isn't valid for Contact.", ['hub_topic' => $hub_topic, 'contact_poll' => $contact['poll']]);
141 throw new HTTPException\NotFoundException();
144 // We must initiate an unsubscribe request with a verify_token.
145 // Don't allow outsiders to unsubscribe us.
147 if (($hub_mode === 'unsubscribe') && empty($hub_verify)) {
148 $this->logger->notice('Bogus unsubscribe');
149 throw new HTTPException\NotFoundException();
152 if (!empty($hub_mode)) {
153 Contact::update(['subhub' => $hub_mode === 'subscribe'], ['id' => $contact['id']]);
154 $this->logger->notice('Success for contact.', ['mode' => $hub_mode, 'contact' => $contact_id]);
157 System::httpExit($hub_challenge);