]> git.mxchange.org Git - friendica.git/blob - src/Module/DFRN/Notify.php
Move settings/addons to src/Module
[friendica.git] / src / Module / DFRN / Notify.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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                 } elseif (!$this->dispatchPublic($postdata)) {
74                         (new Salmon(
75                                 $this->database,
76                                 $this->l10n,
77                                 $this->baseUrl,
78                                 $this->args,
79                                 $this->logger,
80                                 $this->profiler,
81                                 $this->response,
82                                 $this->server,
83                                 $this->parameters
84                         ))->rawContent($request);
85                 }
86         }
87
88         private function dispatchPublic(string $postdata): bool
89         {
90                 $msg = Diaspora::decodeRaw($postdata, '', true);
91                 if (!is_array($msg)) {
92                         // We have to fail silently to be able to hand it over to the salmon parser
93                         $this->logger->warning('Diaspora::decodeRaw() has failed for some reason.');
94                         return false;
95                 }
96
97                 // Fetch the corresponding public contact
98                 $contact_id = Contact::getIdForURL($msg['author']);
99                 if (empty($contact_id)) {
100                         $this->logger->notice('Contact not found', ['address' => $msg['author']]);
101                         System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
102                 }
103
104                 // Fetch the importer (Mixture of sender and receiver)
105                 $importer = DFRN::getImporter($contact_id);
106                 if (empty($importer)) {
107                         $this->logger->notice('Importer contact not found', ['address' => $msg['author']]);
108                         System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
109                 }
110
111                 $this->logger->debug('Importing post with the public envelope.', ['transmitter' => $msg['author']]);
112
113                 // Now we should be able to import it
114                 $ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::RELAY);
115                 System::xmlExit($ret, 'Done');
116
117                 return true;
118         }
119
120         private function dispatchPrivate(array $user, string $postdata)
121         {
122                 $msg = Diaspora::decodeRaw($postdata, $user['prvkey'] ?? '');
123                 if (!is_array($msg)) {
124                         System::xmlExit(4, 'Unable to parse message');
125                 }
126
127                 // Fetch the contact
128                 $contact = Contact::getByURLForUser($msg['author'], $user['uid'], null, ['id', 'blocked', 'pending']);
129                 if (empty($contact['id'])) {
130                         $this->logger->notice('Contact not found', ['address' => $msg['author']]);
131                         System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
132                 }
133
134                 if ($contact['pending'] || $contact['blocked']) {
135                         $this->logger->notice('Contact is blocked or pending', ['address' => $msg['author'], 'contact' => $contact]);
136                         System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
137                 }
138
139                 // Fetch the importer (Mixture of sender and receiver)
140                 $importer = DFRN::getImporter($contact['id'], $user['uid']);
141                 if (empty($importer)) {
142                         $this->logger->notice('Importer contact not found for user', ['uid' => $user['uid'], 'cid' => $contact['id'], 'address' => $msg['author']]);
143                         System::xmlExit(3, 'Contact ' . $msg['author'] . ' not found');
144                 }
145
146                 $this->logger->debug('Importing post with the private envelope.', ['transmitter' => $msg['author'], 'receiver' => $user['nickname']]);
147
148                 // Now we should be able to import it
149                 $ret = DFRN::import($msg['message'], $importer, Conversation::PARCEL_DIASPORA_DFRN, Conversation::PUSH);
150                 System::xmlExit($ret, 'Done');
151         }
152 }