]> git.mxchange.org Git - friendica.git/blob - src/Module/DFRN/Notify.php
Merge branch '2023.03-rc' into stable
[friendica.git] / src / Module / DFRN / Notify.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\DFRN;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Logger;
28 use Friendica\Core\System;
29 use Friendica\Database\Database;
30 use Friendica\DI;
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;
42
43 /**
44  * DFRN Notify
45  */
46 class Notify extends BaseModule
47 {
48         /** @var Database */
49         private $database;
50
51         public function __construct(Database $database, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
52         {
53                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
54
55                 $this->database = $database;
56         }
57
58         protected function post(array $request = [])
59         {
60                 $postdata = Network::postdata();
61
62                 if (empty($postdata)) {
63                         throw new HTTPException\BadRequestException();
64                 }
65
66                 $data = json_decode($postdata);
67                 if (is_object($data) && !empty($this->parameters['nickname'])) {
68                         $user = User::getByNickname($this->parameters['nickname']);
69                         if (empty($user)) {
70                                 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
71                         }
72                         $this->dispatchPrivate($user, $postdata);
73                 } else {
74                         $this->dispatchPublic($postdata);
75                 }
76         }
77
78         private function dispatchPublic(string $postdata): bool
79         {
80                 $msg = Diaspora::decodeRaw($postdata, '', true);
81                 if (!is_array($msg)) {
82                         // We have to fail silently to be able to hand it over to the salmon parser
83                         $this->logger->warning('Diaspora::decodeRaw() has failed for some reason.', ['post-data' => $postdata]);
84                         return false;
85                 }
86
87                 // Fetch the corresponding public contact
88                 $contact_id = Contact::getIdForURL($msg['author']);
89                 if (empty($contact_id)) {
90                         $this->logger->notice('Contact not found', ['address' => $msg['author']]);
91                         System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
92                 }
93
94                 // Fetch the importer (Mixture of sender and receiver)
95                 $importer = DFRN::getImporter($contact_id);
96                 if (empty($importer)) {
97                         $this->logger->notice('Importer contact not found', ['address' => $msg['author']]);
98                         System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
99                 }
100
101                 $this->logger->debug('Importing post with the public envelope.', ['transmitter' => $msg['author']]);
102
103                 // Now we should be able to import it
104                 $ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::RELAY);
105                 System::xmlExit($ret, 'Done');
106
107                 return true;
108         }
109
110         private function dispatchPrivate(array $user, string $postdata)
111         {
112                 $msg = Diaspora::decodeRaw($postdata, $user['prvkey'] ?? '');
113                 if (!is_array($msg)) {
114                         System::xmlExit(4, 'Unable to parse message');
115                 }
116
117                 // Fetch the contact
118                 $contact = Contact::getByURLForUser($msg['author'], $user['uid'], null, ['id', 'blocked', 'pending']);
119                 if (empty($contact['id'])) {
120                         $this->logger->notice('Contact not found', ['address' => $msg['author']]);
121                         System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
122                 }
123
124                 if ($contact['pending'] || $contact['blocked']) {
125                         $this->logger->notice('Contact is blocked or pending', ['address' => $msg['author'], 'contact' => $contact]);
126                         System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
127                 }
128
129                 // Fetch the importer (Mixture of sender and receiver)
130                 $importer = DFRN::getImporter($contact['id'], $user['uid']);
131                 if (empty($importer)) {
132                         $this->logger->notice('Importer contact not found for user', ['uid' => $user['uid'], 'cid' => $contact['id'], 'address' => $msg['author']]);
133                         System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
134                 }
135
136                 $this->logger->debug('Importing post with the private envelope.', ['transmitter' => $msg['author'], 'receiver' => $user['nickname']]);
137
138                 // Now we should be able to import it
139                 $ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::PUSH);
140                 System::xmlExit($ret, 'Done');
141         }
142 }