3 * @copyright Copyright (C) 2010-2021, 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;
24 use Friendica\BaseModule;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Friendica\Core\L10n;
27 use Friendica\Model\User;
28 use Friendica\Network\HTTPException;
29 use Friendica\Protocol\Diaspora;
30 use Friendica\Util\Network;
31 use Psr\Log\LoggerInterface;
34 * This module is part of the Diaspora protocol.
35 * It is used for receiving single posts either for public or for a specific user.
37 class Receive extends BaseModule
39 /** @var LoggerInterface */
41 /** @var IManageConfigValues */
44 public function __construct(LoggerInterface $logger, IManageConfigValues $config, L10n $l10n, array $parameters = [])
46 parent::__construct($l10n, $parameters);
48 $this->logger = $logger;
49 $this->config = $config;
52 public function post()
54 $enabled = $this->config->get('system', 'diaspora_enabled', false);
56 $this->logger->info('Diaspora disabled.');
57 throw new HTTPException\ForbiddenException($this->l10n->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
70 * @throws HTTPException\InternalServerErrorException
71 * @throws \ImagickException
73 private function receivePublic()
75 $this->logger->info('Diaspora: Receiving post.');
77 $msg = $this->decodePost();
79 $this->logger->info('Diaspora: Dispatching.');
81 Diaspora::dispatchPublic($msg);
85 * Receive a Diaspora posting for a user
87 * @throws HTTPException\InternalServerErrorException
88 * @throws \ImagickException
90 private function receiveUser()
92 $this->logger->info('Diaspora: Receiving post.');
94 $importer = User::getByGuid($this->parameters['guid']);
96 $msg = $this->decodePost(false, $importer['prvkey'] ?? '');
98 $this->logger->info('Diaspora: Dispatching.');
100 if (Diaspora::dispatch($importer, $msg)) {
101 throw new HTTPException\OKException();
103 // We couldn't process the content.
104 // To avoid the remote system trying again we send the message that we accepted the content.
105 throw new HTTPException\AcceptedException();
110 * Decodes a Diaspora message based on the posted data
112 * @param string $privKey The private key of the importer
113 * @param bool $public True, if the post is public
116 * @throws HTTPException\InternalServerErrorException
117 * @throws \ImagickException
119 private function decodePost(bool $public = true, string $privKey = '')
121 if (empty($_POST['xml'])) {
123 $postdata = Network::postdata();
125 if (empty($postdata)) {
126 throw new HTTPException\InternalServerErrorException('Missing postdata.');
129 $this->logger->info('Diaspora: Message is in the new format.');
131 $msg = Diaspora::decodeRaw($postdata, $privKey);
134 $xml = urldecode($_POST['xml']);
136 $this->logger->info('Diaspora: Decode message in the old format.');
137 $msg = Diaspora::decode($xml, $privKey);
139 if ($public && !$msg) {
140 $this->logger->info('Diaspora: Decode message in the new format.');
141 $msg = Diaspora::decodeRaw($xml, $privKey);
145 $this->logger->info('Diaspora: Post decoded.');
146 $this->logger->debug('Diaspora: Decoded message.', ['msg' => $msg]);
148 if (!is_array($msg)) {
149 throw new HTTPException\InternalServerErrorException('Message is not an array.');