3 namespace Friendica\Module\Diaspora;
6 use Friendica\BaseModule;
7 use Friendica\Core\Config\Configuration;
8 use Friendica\Core\L10n\L10n;
9 use Friendica\Model\User;
10 use Friendica\Network\HTTPException;
11 use Friendica\Protocol\Diaspora;
12 use Friendica\Util\Network;
13 use Psr\Log\LoggerInterface;
16 * This module is part of the Diaspora protocol.
17 * It is used for receiving single posts either for public or for a specific user.
19 class Receive extends BaseModule
21 /** @var LoggerInterface */
22 private static $logger;
24 public static function init()
26 /** @var LoggerInterface $logger */
27 self::$logger = self::getClass(LoggerInterface::class);
30 public static function post()
32 /** @var Configuration $config */
33 $config = self::getClass(Configuration::class);
35 $enabled = $config->get('system', 'diaspora_enabled', false);
37 self::$logger->info('Diaspora disabled.');
38 $l10n = self::getClass(L10n::class);
39 throw new HTTPException\ForbiddenException($l10n->t('Access denied.'));
42 /** @var App\Arguments $args */
43 $args = self::getClass(App\Arguments::class);
45 $type = $args->get(1);
49 self::receivePublic();
52 self::receiveUser($args->get(2));
55 self::$logger->info('Wrong call.');
56 throw new HTTPException\BadRequestException('wrong call.');
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 throw new HTTPException\InternalServerErrorException();
104 * Decodes a Diaspora message based on the posted data
106 * @param string $privKey The private key of the importer
107 * @param bool $public True, if the post is public
110 * @throws HTTPException\InternalServerErrorException
111 * @throws \ImagickException
113 private static function decodePost(bool $public = true, string $privKey = '')
115 if (empty($_POST['xml'])) {
117 $postdata = Network::postdata();
119 if (empty($postdata)) {
120 throw new HTTPException\InternalServerErrorException('Missing postdata.');
123 self::$logger->info('Diaspora: Message is in the new format.');
125 $msg = Diaspora::decodeRaw($postdata, $privKey);
128 $xml = urldecode($_POST['xml']);
130 self::$logger->info('Diaspora: Decode message in the old format.');
131 $msg = Diaspora::decode($xml, $privKey);
133 if ($public && !$msg) {
134 self::$logger->info('Diaspora: Decode message in the new format.');
135 $msg = Diaspora::decodeRaw($xml, $privKey);
139 self::$logger->info('Diaspora: Post decoded.');
140 self::$logger->debug('Diaspora: Decoded message.', ['msg' => print_r($msg, true)]);
142 if (!is_array($msg)) {
143 throw new HTTPException\InternalServerErrorException('Message is not an array.');