3 * @copyright Copyright (C) 2020, Friendica
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.'));
56 $type = $args->get(1);
60 self::receivePublic();
63 self::receiveUser($args->get(2));
66 self::$logger->info('Wrong call.');
67 throw new HTTPException\BadRequestException('wrong call.');
73 * Receive a public Diaspora posting
75 * @throws HTTPException\InternalServerErrorException
76 * @throws \ImagickException
78 private static function receivePublic()
80 self::$logger->info('Diaspora: Receiving post.');
82 $msg = self::decodePost();
84 self::$logger->info('Diaspora: Dispatching.');
86 Diaspora::dispatchPublic($msg);
90 * Receive a Diaspora posting for a user
92 * @param string $guid The GUID of the importer
94 * @throws HTTPException\InternalServerErrorException
95 * @throws \ImagickException
97 private static function receiveUser(string $guid)
99 self::$logger->info('Diaspora: Receiving post.');
101 $importer = User::getByGuid($guid);
103 $msg = self::decodePost(false, $importer['prvkey'] ?? '');
105 self::$logger->info('Diaspora: Dispatching.');
107 if (Diaspora::dispatch($importer, $msg)) {
108 throw new HTTPException\OKException();
110 throw new HTTPException\InternalServerErrorException();
115 * Decodes a Diaspora message based on the posted data
117 * @param string $privKey The private key of the importer
118 * @param bool $public True, if the post is public
121 * @throws HTTPException\InternalServerErrorException
122 * @throws \ImagickException
124 private static function decodePost(bool $public = true, string $privKey = '')
126 if (empty($_POST['xml'])) {
128 $postdata = Network::postdata();
130 if (empty($postdata)) {
131 throw new HTTPException\InternalServerErrorException('Missing postdata.');
134 self::$logger->info('Diaspora: Message is in the new format.');
136 $msg = Diaspora::decodeRaw($postdata, $privKey);
139 $xml = urldecode($_POST['xml']);
141 self::$logger->info('Diaspora: Decode message in the old format.');
142 $msg = Diaspora::decode($xml, $privKey);
144 if ($public && !$msg) {
145 self::$logger->info('Diaspora: Decode message in the new format.');
146 $msg = Diaspora::decodeRaw($xml, $privKey);
150 self::$logger->info('Diaspora: Post decoded.');
151 self::$logger->debug('Diaspora: Decoded message.', ['msg' => print_r($msg, true)]);
153 if (!is_array($msg)) {
154 throw new HTTPException\InternalServerErrorException('Message is not an array.');