]> git.mxchange.org Git - friendica.git/blob - src/Module/DFRN/Notify.php
Use the post language for the language detection / config for quality
[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\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;
34
35 /**
36  * DFRN Notify
37  */
38 class Notify extends BaseModule
39 {
40         protected function post(array $request = [])
41         {
42                 $postdata = Network::postdata();
43
44                 if (empty($postdata)) {
45                         throw new HTTPException\BadRequestException();
46                 }
47
48                 $data = json_decode($postdata);
49                 if (is_object($data) && !empty($this->parameters['nickname'])) {
50                         $user = User::getByNickname($this->parameters['nickname']);
51                         if (empty($user)) {
52                                 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
53                         }
54                         $this->dispatchPrivate($user, $postdata);
55                 } else {
56                         $this->dispatchPublic($postdata);
57                 }
58         }
59
60         private function dispatchPublic(string $postdata): bool
61         {
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]);
66                         return false;
67                 }
68
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');
74                 }
75
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');
81                 }
82
83                 $this->logger->debug('Importing post with the public envelope.', ['transmitter' => $msg['author']]);
84
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');
88
89                 return true;
90         }
91
92         private function dispatchPrivate(array $user, string $postdata)
93         {
94                 $msg = Diaspora::decodeRaw($postdata, $user['prvkey'] ?? '');
95                 if (!is_array($msg)) {
96                         $this->xmlExit(4, 'Unable to parse message');
97                 }
98
99                 // Fetch the contact
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');
104                 }
105
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');
109                 }
110
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');
116                 }
117
118                 $this->logger->debug('Importing post with the private envelope.', ['transmitter' => $msg['author'], 'receiver' => $user['nickname']]);
119
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');
123         }
124
125
126         /**
127          * Generic XML return
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.
130          *
131          * @param mixed $status
132          * @param string $message
133          * @throws \Exception
134          */
135         private function xmlExit($status, string $message = '')
136         {
137                 $result = ['status' => $status];
138
139                 if ($message != '') {
140                         $result['message'] = $message;
141                 }
142
143                 if ($status) {
144                         $this->logger->notice('xml_status returning non_zero: ' . $status . " message=" . $message);
145                 }
146
147                 $this->httpExit(XML::fromArray(['result' => $result]), Response::TYPE_XML);
148         }
149 }