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