]> git.mxchange.org Git - friendica.git/blob - src/Module/Diaspora/Receive.php
Merge remote-tracking branch 'upstream/develop' into api4
[friendica.git] / src / Module / Diaspora / Receive.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\Diaspora;
23
24 use Friendica\BaseModule;
25 use Friendica\DI;
26 use Friendica\Model\User;
27 use Friendica\Network\HTTPException;
28 use Friendica\Protocol\Diaspora;
29 use Friendica\Util\Network;
30 use Psr\Log\LoggerInterface;
31
32 /**
33  * This module is part of the Diaspora protocol.
34  * It is used for receiving single posts either for public or for a specific user.
35  */
36 class Receive extends BaseModule
37 {
38         /** @var LoggerInterface */
39         private static $logger;
40
41         public function init()
42         {
43                 self::$logger = DI::logger();
44         }
45
46         public function post()
47         {
48                 $enabled = DI::config()->get('system', 'diaspora_enabled', false);
49                 if (!$enabled) {
50                         self::$logger->info('Diaspora disabled.');
51                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
52                 }
53
54                 if ($this->parameters['type'] === 'public') {
55                         self::receivePublic();
56                 } else if ($this->parameters['type'] === 'users') {
57                         self::receiveUser($this->parameters['guid']);
58                 }
59         }
60
61         /**
62          * Receive a public Diaspora posting
63          *
64          * @throws HTTPException\InternalServerErrorException
65          * @throws \ImagickException
66          */
67         private static function receivePublic()
68         {
69                 self::$logger->info('Diaspora: Receiving post.');
70
71                 $msg = self::decodePost();
72
73                 self::$logger->info('Diaspora: Dispatching.');
74
75                 Diaspora::dispatchPublic($msg);
76         }
77
78         /**
79          * Receive a Diaspora posting for a user
80          *
81          * @param string $guid The GUID of the importer
82          *
83          * @throws HTTPException\InternalServerErrorException
84          * @throws \ImagickException
85          */
86         private static function receiveUser(string $guid)
87         {
88                 self::$logger->info('Diaspora: Receiving post.');
89
90                 $importer = User::getByGuid($guid);
91
92                 $msg = self::decodePost(false, $importer['prvkey'] ?? '');
93
94                 self::$logger->info('Diaspora: Dispatching.');
95
96                 if (Diaspora::dispatch($importer, $msg)) {
97                         throw new HTTPException\OKException();
98                 } else {
99                         // We couldn't process the content.
100                         // To avoid the remote system trying again we send the message that we accepted the content.
101                         throw new HTTPException\AcceptedException();
102                 }
103         }
104
105         /**
106          * Decodes a Diaspora message based on the posted data
107          *
108          * @param string $privKey The private key of the importer
109          * @param bool   $public  True, if the post is public
110          *
111          * @return array
112          * @throws HTTPException\InternalServerErrorException
113          * @throws \ImagickException
114          */
115         private static function decodePost(bool $public = true, string $privKey = '')
116         {
117                 if (empty($_POST['xml'])) {
118
119                         $postdata = Network::postdata();
120
121                         if (empty($postdata)) {
122                                 throw new HTTPException\InternalServerErrorException('Missing postdata.');
123                         }
124
125                         self::$logger->info('Diaspora: Message is in the new format.');
126
127                         $msg = Diaspora::decodeRaw($postdata, $privKey);
128                 } else {
129
130                         $xml = urldecode($_POST['xml']);
131
132                         self::$logger->info('Diaspora: Decode message in the old format.');
133                         $msg = Diaspora::decode($xml, $privKey);
134
135                         if ($public && !$msg) {
136                                 self::$logger->info('Diaspora: Decode message in the new format.');
137                                 $msg = Diaspora::decodeRaw($xml, $privKey);
138                         }
139                 }
140
141                 self::$logger->info('Diaspora: Post decoded.');
142                 self::$logger->debug('Diaspora: Decoded message.', ['msg' => $msg]);
143
144                 if (!is_array($msg)) {
145                         throw new HTTPException\InternalServerErrorException('Message is not an array.');
146                 }
147
148                 return $msg;
149         }
150 }