3 namespace Friendica\Module\Diaspora;
6 use Friendica\BaseModule;
7 use Friendica\Core\Config\Configuration;
8 use Friendica\Core\L10n\L10n;
10 use Friendica\Model\User;
11 use Friendica\Network\HTTPException;
12 use Friendica\Protocol\Diaspora;
13 use Friendica\Util\Network;
14 use Psr\Log\LoggerInterface;
17 * This module is part of the Diaspora protocol.
18 * It is used for receiving single posts either for public or for a specific user.
20 class Receive extends BaseModule
22 /** @var LoggerInterface */
23 private static $logger;
25 public static function init(array $parameters = [])
27 self::$logger = DI::logger();
30 public static function post(array $parameters = [])
32 $enabled = DI::config()->get('system', 'diaspora_enabled', false);
34 self::$logger->info('Diaspora disabled.');
35 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
40 $type = $args->get(1);
44 self::receivePublic();
47 self::receiveUser($args->get(2));
50 self::$logger->info('Wrong call.');
51 throw new HTTPException\BadRequestException('wrong call.');
57 * Receive a public Diaspora posting
59 * @throws HTTPException\InternalServerErrorException
60 * @throws \ImagickException
62 private static function receivePublic()
64 self::$logger->info('Diaspora: Receiving post.');
66 $msg = self::decodePost();
68 self::$logger->info('Diaspora: Dispatching.');
70 Diaspora::dispatchPublic($msg);
74 * Receive a Diaspora posting for a user
76 * @param string $guid The GUID of the importer
78 * @throws HTTPException\InternalServerErrorException
79 * @throws \ImagickException
81 private static function receiveUser(string $guid)
83 self::$logger->info('Diaspora: Receiving post.');
85 $importer = User::getByGuid($guid);
87 $msg = self::decodePost(false, $importer['prvkey'] ?? '');
89 self::$logger->info('Diaspora: Dispatching.');
91 if (Diaspora::dispatch($importer, $msg)) {
92 throw new HTTPException\OKException();
94 throw new HTTPException\InternalServerErrorException();
99 * Decodes a Diaspora message based on the posted data
101 * @param string $privKey The private key of the importer
102 * @param bool $public True, if the post is public
105 * @throws HTTPException\InternalServerErrorException
106 * @throws \ImagickException
108 private static function decodePost(bool $public = true, string $privKey = '')
110 if (empty($_POST['xml'])) {
112 $postdata = Network::postdata();
114 if (empty($postdata)) {
115 throw new HTTPException\InternalServerErrorException('Missing postdata.');
118 self::$logger->info('Diaspora: Message is in the new format.');
120 $msg = Diaspora::decodeRaw($postdata, $privKey);
123 $xml = urldecode($_POST['xml']);
125 self::$logger->info('Diaspora: Decode message in the old format.');
126 $msg = Diaspora::decode($xml, $privKey);
128 if ($public && !$msg) {
129 self::$logger->info('Diaspora: Decode message in the new format.');
130 $msg = Diaspora::decodeRaw($xml, $privKey);
134 self::$logger->info('Diaspora: Post decoded.');
135 self::$logger->debug('Diaspora: Decoded message.', ['msg' => print_r($msg, true)]);
137 if (!is_array($msg)) {
138 throw new HTTPException\InternalServerErrorException('Message is not an array.');