3 * @copyright Copyright (C) 2010-2023, 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]);
61 $this->logger->debug('Got request data.', ['request' => $request]);
63 $nickname = $this->parameters['nickname'] ?? '';
64 $contact_id = $this->parameters['cid'] ?? 0;
66 $importer = $this->database->selectFirst('user', [], ['nickname' => $nickname, 'account_expired' => false, 'account_removed' => false]);
68 throw new HTTPException\OKException();
71 $condition = ['id' => $contact_id, 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
72 $contact = $this->database->selectFirst('contact', [], $condition);
74 $author = OStatus::salmonAuthor($xml, $importer);
75 if (!empty($author['contact-id'])) {
76 $condition = ['id' => $author['contact-id'], 'uid' => $importer['uid'], 'subhub' => true, 'blocked' => false];
77 $contact = $this->database->selectFirst('contact', [], $condition);
78 $this->logger->notice('No record found for nickname, using author entry instead.', ['nickname' => $nickname, 'contact-id' => $contact_id, 'author-contact-id' => $author['contact-id']]);
82 $this->logger->notice("Contact wasn't found - ignored.", ['author-link' => $author['author-link'], 'contact-id' => $contact_id, 'nickname' => $nickname, 'xml' => $xml]);
83 throw new HTTPException\OKException();
87 if (!empty($contact['gsid'])) {
88 GServer::setProtocol($contact['gsid'], Post\DeliveryData::OSTATUS);
91 if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND]) && ($contact['network'] != Protocol::FEED)) {
92 $this->logger->notice('Contact is not expected to share with us - ignored.', ['contact-id' => $contact['id']]);
93 throw new HTTPException\OKException();
96 // We only import feeds from OStatus here
97 if (!in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::OSTATUS])) {
98 $this->logger->warning('Unexpected network', ['contact' => $contact, 'network' => $contact['network']]);
99 throw new HTTPException\OKException();
102 $this->logger->info('Import item from Contact.', ['nickname' => $nickname, 'contact-nickname' => $contact['nick'], 'contact-id' => $contact['id']]);
104 OStatus::import($xml, $importer, $contact, $feedhub);
106 throw new HTTPException\OKException();
109 protected function rawContent(array $request = [])
111 $nickname = $this->parameters['nickname'] ?? '';
112 $contact_id = $this->parameters['cid'] ?? 0;
114 $hub_mode = trim($request['hub_mode'] ?? '');
115 $hub_topic = trim($request['hub_topic'] ?? '');
116 $hub_challenge = trim($request['hub_challenge'] ?? '');
117 $hub_verify = trim($request['hub_verify_token'] ?? '');
119 $this->logger->notice('Subscription start.', ['from' => $this->request->getRemoteAddress(), 'mode' => $hub_mode, 'nickname' => $nickname]);
120 $this->logger->debug('Data: ', ['get' => $request]);
122 $owner = $this->database->selectFirst('user', ['uid'], ['nickname' => $nickname, 'account_expired' => false, 'account_removed' => false]);
124 $this->logger->notice('Local account not found.', ['nickname' => $nickname]);
125 throw new HTTPException\NotFoundException();
128 $condition = ['uid' => $owner['uid'], 'id' => $contact_id, 'blocked' => false, 'pending' => false];
130 if (!empty($hub_verify)) {
131 $condition['hub-verify'] = $hub_verify;
134 $contact = $this->database->selectFirst('contact', ['id', 'poll'], $condition);
136 $this->logger->notice('Contact not found.', ['contact' => $contact_id]);
137 throw new HTTPException\NotFoundException();
140 if (!empty($hub_topic) && !Strings::compareLink($hub_topic, $contact['poll'])) {
141 $this->logger->notice("Hub topic isn't valid for Contact.", ['hub_topic' => $hub_topic, 'contact_poll' => $contact['poll']]);
142 throw new HTTPException\NotFoundException();
145 // We must initiate an unsubscribe request with a verify_token.
146 // Don't allow outsiders to unsubscribe us.
148 if (($hub_mode === 'unsubscribe') && empty($hub_verify)) {
149 $this->logger->notice('Bogus unsubscribe');
150 throw new HTTPException\NotFoundException();
153 if (!empty($hub_mode)) {
154 Contact::update(['subhub' => $hub_mode === 'subscribe'], ['id' => $contact['id']]);
155 $this->logger->notice('Success for contact.', ['mode' => $hub_mode, 'contact' => $contact_id]);
158 System::httpExit($hub_challenge);