3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Diaspora;
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;
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.
40 class Receive extends BaseModule
42 /** @var IManageConfigValues */
45 public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IManageConfigValues $config, array $server, array $parameters = [])
47 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
49 $this->config = $config;
52 protected function post(array $request = [])
54 $enabled = $this->config->get('system', 'diaspora_enabled', false);
56 $this->logger->info('Diaspora disabled.');
57 throw new HTTPException\ForbiddenException($this->t('Access denied.'));
60 if ($this->parameters['type'] === 'public') {
61 $this->receivePublic();
62 } else if ($this->parameters['type'] === 'users') {
68 * Receive a public Diaspora posting
71 * @throws HTTPException\InternalServerErrorException
72 * @throws \ImagickException
74 private function receivePublic()
76 $this->logger->info('Diaspora: Receiving post.');
78 $msg = $this->decodePost();
80 $this->logger->info('Diaspora: Dispatching.');
82 Diaspora::dispatchPublic($msg, Diaspora::PUSHED);
86 * Receive a Diaspora posting for a user
89 * @throws HTTPException\InternalServerErrorException
90 * @throws \ImagickException
92 private function receiveUser()
94 $this->logger->info('Diaspora: Receiving post.');
96 $importer = User::getByGuid($this->parameters['guid']);
97 if (empty($importer)) {
98 // We haven't found the user.
99 // To avoid the remote system trying again we send the message that we accepted the content.
100 throw new HTTPException\AcceptedException();
103 if ($importer['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) {
104 // Communities aren't working with the Diaspora protocol
105 // We throw an "accepted" here, so that the sender doesn't repeat the delivery
106 throw new HTTPException\AcceptedException();
109 $msg = $this->decodePost(false, $importer['prvkey']);
111 $this->logger->info('Diaspora: Dispatching.');
113 if (Diaspora::dispatch($importer, $msg)) {
114 throw new HTTPException\OKException();
116 // We couldn't process the content.
117 // To avoid the remote system trying again we send the message that we accepted the content.
118 throw new HTTPException\AcceptedException();
123 * Decodes a Diaspora message based on the posted data
125 * @param string $privKey The private key of the importer
126 * @param bool $public True, if the post is public
129 * @throws HTTPException\InternalServerErrorException
130 * @throws \ImagickException
132 private function decodePost(bool $public = true, string $privKey = ''): array
134 if (empty($_POST['xml'])) {
136 $postdata = Network::postdata();
138 if (empty($postdata)) {
139 throw new HTTPException\InternalServerErrorException('Missing postdata.');
142 $this->logger->info('Diaspora: Message is in the new format.');
144 $msg = Diaspora::decodeRaw($postdata, $privKey);
147 $xml = urldecode($_POST['xml']);
149 $this->logger->info('Diaspora: Decode message in the old format.');
150 $msg = Diaspora::decode($xml, $privKey);
152 if ($public && !$msg) {
153 $this->logger->info('Diaspora: Decode message in the new format.');
154 $msg = Diaspora::decodeRaw($xml, $privKey);
158 $this->logger->info('Diaspora: Post decoded.');
159 $this->logger->debug('Diaspora: Decoded message.', ['msg' => $msg]);
161 if (!is_array($msg)) {
162 throw new HTTPException\InternalServerErrorException('Message is not an array.');