3 namespace Friendica\Module\Diaspora;
6 use Friendica\BaseModule;
7 use Friendica\Core\Config\Configuration;
8 use Friendica\Model\User;
9 use Friendica\Network\HTTPException;
10 use Friendica\Protocol\Diaspora;
11 use Friendica\Util\Network;
12 use Psr\Log\LoggerInterface;
15 * This module is part of the Diaspora protocol.
16 * It is used for receiving single posts either for public or for a specific user.
18 class Receive extends BaseModule
20 /** @var LoggerInterface */
21 private static $logger;
23 public static function init()
25 /** @var LoggerInterface $logger */
26 self::$logger = self::getClass(LoggerInterface::class);
29 public static function post()
31 /** @var Configuration $config */
32 $config = self::getClass(Configuration::class);
34 $enabled = $config->get('system', 'diaspora_enabled', false);
36 self::$logger->info('Diaspora disabled.');
37 throw new HTTPException\InternalServerErrorException('Diaspora disabled.');
40 /** @var App\Arguments $args */
41 $args = self::getClass(App\Arguments::class);
43 $type = $args->get(1);
47 self::receivePublic();
50 self::receiveUser($args->get(2));
53 self::$logger->info('Wrong call.');
54 throw new HTTPException\InternalServerErrorException('wrong call.');
60 * Receive a public Diaspora posting
62 * @throws HTTPException\InternalServerErrorException
63 * @throws \ImagickException
65 private static function receivePublic()
67 self::$logger->info('Diaspora: Receiving post.');
69 $msg = self::decodePost();
71 self::$logger->info('Diaspora: Dispatching.');
73 Diaspora::dispatchPublic($msg);
77 * Receive a Diaspora posting for a user
79 * @param string $guid The GUID of the importer
81 * @throws HTTPException\InternalServerErrorException
82 * @throws \ImagickException
84 private static function receiveUser(string $guid)
86 self::$logger->info('Diaspora: Receiving post.');
88 $importer = User::getByGuid($guid);
90 $msg = self::decodePost(false, $importer['prvkey'] ?? '');
92 self::$logger->info('Diaspora: Dispatching.');
94 if (Diaspora::dispatch($importer, $msg)) {
95 throw new HTTPException\OKException();
97 throw new HTTPException\InternalServerErrorException();
102 * Decodes a Diaspora message based on the posted data
104 * @param string $privKey The private key of the importer
105 * @param bool $public True, if the post is public
108 * @throws HTTPException\InternalServerErrorException
109 * @throws \ImagickException
111 private static function decodePost(bool $public = true, string $privKey = '')
113 if (empty($_POST['xml'])) {
115 $postdata = Network::postdata();
117 if (empty($postdata)) {
118 throw new HTTPException\InternalServerErrorException('Missing postdata.');
121 self::$logger->info('Diaspora: Message is in the new format.');
123 $msg = Diaspora::decodeRaw($postdata, $privKey);
126 $xml = urldecode($_POST['xml']);
128 self::$logger->info('Diaspora: Decode message in the old format.');
129 $msg = Diaspora::decode($xml, $privKey);
131 if ($public && !$msg) {
132 self::$logger->info('Diaspora: Decode message in the new format.');
133 $msg = Diaspora::decodeRaw($xml, $privKey);
137 self::$logger->info('Diaspora: Post encoded.');
138 self::$logger->debug('Diaspora: Decoded message.', ['msg' => print_r($msg, true)]);
140 if (!is_array($msg)) {
141 throw new HTTPException\InternalServerErrorException('Message is not an array.');