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;
24 use Friendica\BaseModule;
25 use Friendica\Core\Logger;
26 use Friendica\Core\System;
28 use Friendica\Model\Contact;
29 use Friendica\Model\Conversation;
30 use Friendica\Model\User;
31 use Friendica\Protocol\DFRN;
32 use Friendica\Protocol\Diaspora;
33 use Friendica\Util\Network;
34 use Friendica\Network\HTTPException;
39 class Notify extends BaseModule
41 protected function post(array $request = [])
43 $postdata = Network::postdata();
45 if (empty($postdata)) {
46 throw new HTTPException\BadRequestException();
49 $data = json_decode($postdata);
50 if (is_object($data) && !empty($this->parameters['nickname'])) {
51 $user = User::getByNickname($this->parameters['nickname']);
53 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
55 self::dispatchPrivate($user, $postdata);
56 } elseif (!self::dispatchPublic($postdata)) {
57 require_once 'mod/salmon.php';
58 salmon_post(DI::app(), $postdata);
62 private static function dispatchPublic(array $postdata)
64 $msg = Diaspora::decodeRaw($postdata, '', true);
65 if (!is_array($msg)) {
66 // We have to fail silently to be able to hand it over to the salmon parser
67 Logger::warning('Diaspora::decodeRaw() has failed for some reason.');
71 // Fetch the corresponding public contact
72 $contact_id = Contact::getIdForURL($msg['author']);
73 if (empty($contact_id)) {
74 Logger::notice('Contact not found', ['address' => $msg['author']]);
75 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
78 // Fetch the importer (Mixture of sender and receiver)
79 $importer = DFRN::getImporter($contact_id);
80 if (empty($importer)) {
81 Logger::notice('Importer contact not found', ['address' => $msg['author']]);
82 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
85 Logger::info('Importing post with the public envelope.', ['transmitter' => $msg['author']]);
87 // Now we should be able to import it
88 $ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::RELAY);
89 System::xmlExit($ret, 'Done');
92 private static function dispatchPrivate(array $user, string $postdata)
94 $msg = Diaspora::decodeRaw($postdata, $user['prvkey'] ?? '');
95 if (!is_array($msg)) {
96 System::xmlExit(4, 'Unable to parse message');
100 $contact = Contact::getByURLForUser($msg['author'], $user['uid'], null, ['id', 'blocked', 'pending']);
101 if (empty($contact['id'])) {
102 Logger::notice('Contact not found', ['address' => $msg['author']]);
103 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
106 if ($contact['pending'] || $contact['blocked']) {
107 Logger::notice('Contact is blocked or pending', ['address' => $msg['author'], 'contact' => $contact]);
108 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
111 // Fetch the importer (Mixture of sender and receiver)
112 $importer = DFRN::getImporter($contact['id'], $user['uid']);
113 if (empty($importer)) {
114 Logger::notice('Importer contact not found for user', ['uid' => $user['uid'], 'cid' => $contact['id'], 'address' => $msg['author']]);
115 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
118 Logger::info('Importing post with the private envelope.', ['transmitter' => $msg['author'], 'receiver' => $user['nickname']]);
120 // Now we should be able to import it
121 $ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::PUSH);
122 System::xmlExit($ret, 'Done');