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;
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;
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.
36 class Receive extends BaseModule
38 /** @var LoggerInterface */
39 private static $logger;
41 public static function init(array $parameters = [])
43 self::$logger = DI::logger();
46 public static function post(array $parameters = [])
48 $enabled = DI::config()->get('system', 'diaspora_enabled', false);
50 self::$logger->info('Diaspora disabled.');
51 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
54 if ($parameters['type'] === 'public') {
55 self::receivePublic();
56 } else if ($parameters['type'] === 'users') {
57 self::receiveUser($parameters['guid']);
62 * Receive a public Diaspora posting
64 * @throws HTTPException\InternalServerErrorException
65 * @throws \ImagickException
67 private static function receivePublic()
69 self::$logger->info('Diaspora: Receiving post.');
71 $msg = self::decodePost();
73 self::$logger->info('Diaspora: Dispatching.');
75 Diaspora::dispatchPublic($msg);
79 * Receive a Diaspora posting for a user
81 * @param string $guid The GUID of the importer
83 * @throws HTTPException\InternalServerErrorException
84 * @throws \ImagickException
86 private static function receiveUser(string $guid)
88 self::$logger->info('Diaspora: Receiving post.');
90 $importer = User::getByGuid($guid);
92 $msg = self::decodePost(false, $importer['prvkey'] ?? '');
94 self::$logger->info('Diaspora: Dispatching.');
96 if (Diaspora::dispatch($importer, $msg)) {
97 throw new HTTPException\OKException();
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();
106 * Decodes a Diaspora message based on the posted data
108 * @param string $privKey The private key of the importer
109 * @param bool $public True, if the post is public
112 * @throws HTTPException\InternalServerErrorException
113 * @throws \ImagickException
115 private static function decodePost(bool $public = true, string $privKey = '')
117 if (empty($_POST['xml'])) {
119 $postdata = Network::postdata();
121 if (empty($postdata)) {
122 throw new HTTPException\InternalServerErrorException('Missing postdata.');
125 self::$logger->info('Diaspora: Message is in the new format.');
127 $msg = Diaspora::decodeRaw($postdata, $privKey);
130 $xml = urldecode($_POST['xml']);
132 self::$logger->info('Diaspora: Decode message in the old format.');
133 $msg = Diaspora::decode($xml, $privKey);
135 if ($public && !$msg) {
136 self::$logger->info('Diaspora: Decode message in the new format.');
137 $msg = Diaspora::decodeRaw($xml, $privKey);
141 self::$logger->info('Diaspora: Post decoded.');
142 self::$logger->debug('Diaspora: Decoded message.', ['msg' => $msg]);
144 if (!is_array($msg)) {
145 throw new HTTPException\InternalServerErrorException('Message is not an array.');