]> git.mxchange.org Git - friendica.git/blob - src/Module/DFRN/Notify.php
Merge pull request #10544 from annando/router-lock
[friendica.git] / src / Module / DFRN / Notify.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\BaseModule;
25 use Friendica\Core\Logger;
26 use Friendica\Core\System;
27 use Friendica\DI;
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;
35
36 /**
37  * DFRN Notify
38  */
39 class Notify extends BaseModule
40 {
41         public static function post(array $parameters = [])
42         {
43                 $postdata = Network::postdata();
44
45                 if (empty($postdata)) {
46                         throw new HTTPException\BadRequestException();
47                 }
48
49                 $data = json_decode($postdata);
50                 if (is_object($data) && !empty($parameters['nickname'])) {
51                         $user = User::getByNickname($parameters['nickname']);
52                         if (empty($user)) {
53                                 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
54                         }
55                         self::dispatchPrivate($user, $postdata);
56                 } elseif (!self::dispatchPublic($postdata)) {
57                         require_once 'mod/salmon.php';
58                         salmon_post(DI::app(), $postdata);
59                 }
60         }
61
62         private static function dispatchPublic($postdata)
63         {
64                 $msg = Diaspora::decodeRaw($postdata, '', true);
65                 if (!$msg) {
66                         // We have to fail silently to be able to hand it over to the salmon parser
67                         return false;
68                 }
69
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');
75                 }
76
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');
82                 }
83
84                 Logger::info('Importing post with the public envelope.', ['transmitter' => $msg['author']]);
85
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');
89         }
90
91         private static function dispatchPrivate($user, $postdata)
92         {
93                 $msg = Diaspora::decodeRaw($postdata, $user['prvkey'] ?? '');
94                 if (!$msg) {
95                         System::xmlExit(4, 'Unable to parse message');
96                 }
97
98                 // Fetch the contact
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');
103                 }
104
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');
108                 }
109
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');
115                 }
116
117                 Logger::info('Importing post with the private envelope.', ['transmitter' => $msg['author'], 'receiver' => $user['nickname']]);
118
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');
122         }
123 }