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\DFRN;
25 use Friendica\BaseModule;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Logger;
28 use Friendica\Core\System;
29 use Friendica\Database\Database;
31 use Friendica\Model\Contact;
32 use Friendica\Model\Conversation;
33 use Friendica\Model\User;
34 use Friendica\Module\OStatus\Salmon;
35 use Friendica\Module\Response;
36 use Friendica\Protocol\DFRN;
37 use Friendica\Protocol\Diaspora;
38 use Friendica\Util\Network;
39 use Friendica\Network\HTTPException;
40 use Friendica\Util\Profiler;
41 use Psr\Log\LoggerInterface;
46 class Notify extends BaseModule
51 public function __construct(Database $database, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
53 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
55 $this->database = $database;
58 protected function post(array $request = [])
60 $postdata = Network::postdata();
62 if (empty($postdata)) {
63 throw new HTTPException\BadRequestException();
66 $data = json_decode($postdata);
67 if (is_object($data) && !empty($this->parameters['nickname'])) {
68 $user = User::getByNickname($this->parameters['nickname']);
70 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
72 $this->dispatchPrivate($user, $postdata);
73 } elseif (!$this->dispatchPublic($postdata)) {
84 ))->rawContent($request);
88 private function dispatchPublic(string $postdata): bool
90 $msg = Diaspora::decodeRaw($postdata, '', true);
91 if (!is_array($msg)) {
92 // We have to fail silently to be able to hand it over to the salmon parser
93 $this->logger->warning('Diaspora::decodeRaw() has failed for some reason.');
97 // Fetch the corresponding public contact
98 $contact_id = Contact::getIdForURL($msg['author']);
99 if (empty($contact_id)) {
100 $this->logger->notice('Contact not found', ['address' => $msg['author']]);
101 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
104 // Fetch the importer (Mixture of sender and receiver)
105 $importer = DFRN::getImporter($contact_id);
106 if (empty($importer)) {
107 $this->logger->notice('Importer contact not found', ['address' => $msg['author']]);
108 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
111 $this->logger->debug('Importing post with the public envelope.', ['transmitter' => $msg['author']]);
113 // Now we should be able to import it
114 $ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::RELAY);
115 System::xmlExit($ret, 'Done');
120 private function dispatchPrivate(array $user, string $postdata)
122 $msg = Diaspora::decodeRaw($postdata, $user['prvkey'] ?? '');
123 if (!is_array($msg)) {
124 System::xmlExit(4, 'Unable to parse message');
128 $contact = Contact::getByURLForUser($msg['author'], $user['uid'], null, ['id', 'blocked', 'pending']);
129 if (empty($contact['id'])) {
130 $this->logger->notice('Contact not found', ['address' => $msg['author']]);
131 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
134 if ($contact['pending'] || $contact['blocked']) {
135 $this->logger->notice('Contact is blocked or pending', ['address' => $msg['author'], 'contact' => $contact]);
136 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
139 // Fetch the importer (Mixture of sender and receiver)
140 $importer = DFRN::getImporter($contact['id'], $user['uid']);
141 if (empty($importer)) {
142 $this->logger->notice('Importer contact not found for user', ['uid' => $user['uid'], 'cid' => $contact['id'], 'address' => $msg['author']]);
143 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
146 $this->logger->debug('Importing post with the private envelope.', ['transmitter' => $msg['author'], 'receiver' => $user['nickname']]);
148 // Now we should be able to import it
149 $ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::PUSH);
150 System::xmlExit($ret, 'Done');