3 * @copyright Copyright (C) 2010-2024, 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\Model\Contact;
26 use Friendica\Model\Conversation;
27 use Friendica\Model\User;
28 use Friendica\Module\Response;
29 use Friendica\Network\HTTPException;
30 use Friendica\Protocol\DFRN;
31 use Friendica\Protocol\Diaspora;
32 use Friendica\Util\Network;
33 use Friendica\Util\XML;
38 class Notify extends BaseModule
40 protected function post(array $request = [])
42 $postdata = Network::postdata();
44 if (empty($postdata)) {
45 throw new HTTPException\BadRequestException();
48 $data = json_decode($postdata);
49 if (is_object($data) && !empty($this->parameters['nickname'])) {
50 $user = User::getByNickname($this->parameters['nickname']);
52 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
54 $this->dispatchPrivate($user, $postdata);
56 $this->dispatchPublic($postdata);
60 private function dispatchPublic(string $postdata): bool
62 $msg = Diaspora::decodeRaw($postdata, '', true);
63 if (!is_array($msg)) {
64 // We have to fail silently to be able to hand it over to the salmon parser
65 $this->logger->warning('Diaspora::decodeRaw() has failed for some reason.', ['post-data' => $postdata]);
69 // Fetch the corresponding public contact
70 $contact_id = Contact::getIdForURL($msg['author']);
71 if (empty($contact_id)) {
72 $this->logger->notice('Contact not found', ['address' => $msg['author']]);
73 $this->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 $this->logger->notice('Importer contact not found', ['address' => $msg['author']]);
80 $this->xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
83 $this->logger->debug('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 $this->xmlExit($ret, 'Done');
92 private function dispatchPrivate(array $user, string $postdata)
94 $msg = Diaspora::decodeRaw($postdata, $user['prvkey'] ?? '');
95 if (!is_array($msg)) {
96 $this->xmlExit(4, 'Unable to parse message');
100 $contact = Contact::getByURLForUser($msg['author'], $user['uid'], null, ['id', 'blocked', 'pending']);
101 if (empty($contact['id'])) {
102 $this->logger->notice('Contact not found', ['address' => $msg['author']]);
103 $this->xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
106 if ($contact['pending'] || $contact['blocked']) {
107 $this->logger->notice('Contact is blocked or pending', ['address' => $msg['author'], 'contact' => $contact]);
108 $this->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 $this->logger->notice('Importer contact not found for user', ['uid' => $user['uid'], 'cid' => $contact['id'], 'address' => $msg['author']]);
115 $this->xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
118 $this->logger->debug('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 $this->xmlExit($ret, 'Done');
128 * Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
129 * of $st and an optional text <message> of $message and terminates the current process.
131 * @param mixed $status
132 * @param string $message
135 private function xmlExit($status, string $message = '')
137 $result = ['status' => $status];
139 if ($message != '') {
140 $result['message'] = $message;
144 $this->logger->notice('xml_status returning non_zero: ' . $status . " message=" . $message);
147 $this->httpExit(XML::fromArray(['result' => $result]), Response::TYPE_XML);