3 namespace Friendica\Module\Diaspora;
5 use Friendica\BaseModule;
7 use Friendica\Model\User;
8 use Friendica\Network\HTTPException;
9 use Friendica\Protocol\Diaspora;
10 use Friendica\Util\Network;
11 use Psr\Log\LoggerInterface;
14 * This module is part of the Diaspora protocol.
15 * It is used for receiving single posts either for public or for a specific user.
17 class Receive extends BaseModule
19 /** @var LoggerInterface */
20 private static $logger;
22 public static function init(array $parameters = [])
24 self::$logger = DI::logger();
27 public static function post(array $parameters = [])
29 $enabled = DI::config()->get('system', 'diaspora_enabled', false);
31 self::$logger->info('Diaspora disabled.');
32 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
37 $type = $args->get(1);
41 self::receivePublic();
44 self::receiveUser($args->get(2));
47 self::$logger->info('Wrong call.');
48 throw new HTTPException\BadRequestException('wrong call.');
54 * Receive a public Diaspora posting
56 * @throws HTTPException\InternalServerErrorException
57 * @throws \ImagickException
59 private static function receivePublic()
61 self::$logger->info('Diaspora: Receiving post.');
63 $msg = self::decodePost();
65 self::$logger->info('Diaspora: Dispatching.');
67 Diaspora::dispatchPublic($msg);
71 * Receive a Diaspora posting for a user
73 * @param string $guid The GUID of the importer
75 * @throws HTTPException\InternalServerErrorException
76 * @throws \ImagickException
78 private static function receiveUser(string $guid)
80 self::$logger->info('Diaspora: Receiving post.');
82 $importer = User::getByGuid($guid);
84 $msg = self::decodePost(false, $importer['prvkey'] ?? '');
86 self::$logger->info('Diaspora: Dispatching.');
88 if (Diaspora::dispatch($importer, $msg)) {
89 throw new HTTPException\OKException();
91 throw new HTTPException\InternalServerErrorException();
96 * Decodes a Diaspora message based on the posted data
98 * @param string $privKey The private key of the importer
99 * @param bool $public True, if the post is public
102 * @throws HTTPException\InternalServerErrorException
103 * @throws \ImagickException
105 private static function decodePost(bool $public = true, string $privKey = '')
107 if (empty($_POST['xml'])) {
109 $postdata = Network::postdata();
111 if (empty($postdata)) {
112 throw new HTTPException\InternalServerErrorException('Missing postdata.');
115 self::$logger->info('Diaspora: Message is in the new format.');
117 $msg = Diaspora::decodeRaw($postdata, $privKey);
120 $xml = urldecode($_POST['xml']);
122 self::$logger->info('Diaspora: Decode message in the old format.');
123 $msg = Diaspora::decode($xml, $privKey);
125 if ($public && !$msg) {
126 self::$logger->info('Diaspora: Decode message in the new format.');
127 $msg = Diaspora::decodeRaw($xml, $privKey);
131 self::$logger->info('Diaspora: Post decoded.');
132 self::$logger->debug('Diaspora: Decoded message.', ['msg' => print_r($msg, true)]);
134 if (!is_array($msg)) {
135 throw new HTTPException\InternalServerErrorException('Message is not an array.');