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/>.
22 namespace Friendica\Module\DFRN;
24 use Friendica\BaseModule;
25 use Friendica\Core\Logger;
26 use Friendica\Core\System;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Conversation;
29 use Friendica\Model\User;
30 use Friendica\Protocol\DFRN;
31 use Friendica\Protocol\Diaspora;
32 use Friendica\Util\Network;
33 use Friendica\Network\HTTPException;
38 class Notify extends BaseModule
40 public static function post(array $parameters = [])
42 $postdata = Network::postdata();
44 if (empty($postdata)) {
45 throw new HTTPException\BadRequestException();
48 $data = json_decode($postdata);
49 if (is_object($data) && !empty($parameters['nickname'])) {
50 $user = User::getByNickname($parameters['nickname']);
52 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
54 self::dispatchPrivate($user, $postdata);
55 } elseif (!self::dispatchPublic($postdata)) {
56 require_once 'mod/salmon.php';
57 salmon_post($a, $postdata);
61 private static function dispatchPublic($postdata)
63 $msg = Diaspora::decodeRaw($postdata, '', true);
65 // We have to fail silently to be able to hand it over to the salmon parser
69 // Fetch the corresponding public contact
70 $contact_id = Contact::getIdForURL($msg['author']);
71 if (empty($contact_id)) {
72 Logger::notice('Contact not found', ['address' => $msg['author']]);
73 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
76 // Fetch the importer (Mixture of sender and receiver)
77 $importer = DFRN::getImporter($contact_id);
78 if (empty($importer)) {
79 Logger::notice('Importer contact not found', ['address' => $msg['author']]);
80 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
83 Logger::info('Importing post with the public envelope.', ['transmitter' => $msg['author']]);
85 // Now we should be able to import it
86 $ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::RELAY);
87 System::xmlExit($ret, 'Done');
90 private static function dispatchPrivate($user, $postdata)
92 $msg = Diaspora::decodeRaw($postdata, $user['prvkey'] ?? '');
94 System::xmlExit(4, 'Unable to parse message');
98 $contact = Contact::getByURLForUser($msg['author'], $user['uid'], null, ['id', 'blocked', 'pending']);
99 if (empty($contact['id'])) {
100 Logger::notice('Contact not found', ['address' => $msg['author']]);
101 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
104 if ($contact['pending'] || $contact['blocked']) {
105 Logger::notice('Contact is blocked or pending', ['address' => $msg['author'], 'contact' => $contact]);
106 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
109 // Fetch the importer (Mixture of sender and receiver)
110 $importer = DFRN::getImporter($contact['id'], $user['uid']);
111 if (empty($importer)) {
112 Logger::notice('Importer contact not found for user', ['uid' => $user['uid'], 'cid' => $contact['id'], 'address' => $msg['author']]);
113 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
116 Logger::info('Importing post with the private envelope.', ['transmitter' => $msg['author'], 'receiver' => $user['nickname']]);
118 // Now we should be able to import it
119 $ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::PUSH);
120 System::xmlExit($ret, 'Done');