]> git.mxchange.org Git - friendica.git/blob - src/Module/Diaspora/Receive.php
01c04dfb6cfc5b6aa4c85442c0c2319a91d668f3
[friendica.git] / src / Module / Diaspora / Receive.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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 static function init(array $parameters = [])
42         {
43                 self::$logger = DI::logger();
44         }
45
46         public static function post(array $parameters = [])
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                 $args = DI::args();
55
56                 $type = $args->get(1);
57
58                 switch ($type) {
59                         case 'public':
60                                 self::receivePublic();
61                                 break;
62                         case 'users':
63                                 self::receiveUser($args->get(2));
64                                 break;
65                         default:
66                                 self::$logger->info('Wrong call.');
67                                 throw new HTTPException\BadRequestException('wrong call.');
68                                 break;
69                 }
70         }
71
72         /**
73          * Receive a public Diaspora posting
74          *
75          * @throws HTTPException\InternalServerErrorException
76          * @throws \ImagickException
77          */
78         private static function receivePublic()
79         {
80                 self::$logger->info('Diaspora: Receiving post.');
81
82                 $msg = self::decodePost();
83
84                 self::$logger->info('Diaspora: Dispatching.');
85
86                 Diaspora::dispatchPublic($msg);
87         }
88
89         /**
90          * Receive a Diaspora posting for a user
91          *
92          * @param string $guid The GUID of the importer
93          *
94          * @throws HTTPException\InternalServerErrorException
95          * @throws \ImagickException
96          */
97         private static function receiveUser(string $guid)
98         {
99                 self::$logger->info('Diaspora: Receiving post.');
100
101                 $importer = User::getByGuid($guid);
102
103                 $msg = self::decodePost(false, $importer['prvkey'] ?? '');
104
105                 self::$logger->info('Diaspora: Dispatching.');
106
107                 if (Diaspora::dispatch($importer, $msg)) {
108                         throw new HTTPException\OKException();
109                 } else {
110                         throw new HTTPException\InternalServerErrorException();
111                 }
112         }
113
114         /**
115          * Decodes a Diaspora message based on the posted data
116          *
117          * @param string $privKey The private key of the importer
118          * @param bool   $public  True, if the post is public
119          *
120          * @return array
121          * @throws HTTPException\InternalServerErrorException
122          * @throws \ImagickException
123          */
124         private static function decodePost(bool $public = true, string $privKey = '')
125         {
126                 if (empty($_POST['xml'])) {
127
128                         $postdata = Network::postdata();
129
130                         if (empty($postdata)) {
131                                 throw new HTTPException\InternalServerErrorException('Missing postdata.');
132                         }
133
134                         self::$logger->info('Diaspora: Message is in the new format.');
135
136                         $msg = Diaspora::decodeRaw($postdata, $privKey);
137                 } else {
138
139                         $xml = urldecode($_POST['xml']);
140
141                         self::$logger->info('Diaspora: Decode message in the old format.');
142                         $msg = Diaspora::decode($xml, $privKey);
143
144                         if ($public && !$msg) {
145                                 self::$logger->info('Diaspora: Decode message in the new format.');
146                                 $msg = Diaspora::decodeRaw($xml, $privKey);
147                         }
148                 }
149
150                 self::$logger->info('Diaspora: Post decoded.');
151                 self::$logger->debug('Diaspora: Decoded message.', ['msg' => print_r($msg, true)]);
152
153                 if (!is_array($msg)) {
154                         throw new HTTPException\InternalServerErrorException('Message is not an array.');
155                 }
156
157                 return $msg;
158         }
159 }