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;
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($postdata)
64 $msg = Diaspora::decodeRaw($postdata, '', true);
66 // We have to fail silently to be able to hand it over to the salmon parser
70 // Fetch the corresponding public contact
71 $contact_id = Contact::getIdForURL($msg['author']);
72 if (empty($contact_id)) {
73 Logger::notice('Contact not found', ['address' => $msg['author']]);
74 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
77 // Fetch the importer (Mixture of sender and receiver)
78 $importer = DFRN::getImporter($contact_id);
79 if (empty($importer)) {
80 Logger::notice('Importer contact not found', ['address' => $msg['author']]);
81 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
84 Logger::info('Importing post with the public envelope.', ['transmitter' => $msg['author']]);
86 // Now we should be able to import it
87 $ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::RELAY);
88 System::xmlExit($ret, 'Done');
91 private static function dispatchPrivate($user, $postdata)
93 $msg = Diaspora::decodeRaw($postdata, $user['prvkey'] ?? '');
95 System::xmlExit(4, 'Unable to parse message');
99 $contact = Contact::getByURLForUser($msg['author'], $user['uid'], null, ['id', 'blocked', 'pending']);
100 if (empty($contact['id'])) {
101 Logger::notice('Contact not found', ['address' => $msg['author']]);
102 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
105 if ($contact['pending'] || $contact['blocked']) {
106 Logger::notice('Contact is blocked or pending', ['address' => $msg['author'], 'contact' => $contact]);
107 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
110 // Fetch the importer (Mixture of sender and receiver)
111 $importer = DFRN::getImporter($contact['id'], $user['uid']);
112 if (empty($importer)) {
113 Logger::notice('Importer contact not found for user', ['uid' => $user['uid'], 'cid' => $contact['id'], 'address' => $msg['author']]);
114 System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
117 Logger::info('Importing post with the private envelope.', ['transmitter' => $msg['author'], 'receiver' => $user['nickname']]);
119 // Now we should be able to import it
120 $ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::PUSH);
121 System::xmlExit($ret, 'Done');