Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.5 KiB

2 years ago
  1. <?php
  2. namespace Friendica\Module\Diaspora;
  3. use Friendica\App;
  4. use Friendica\BaseModule;
  5. use Friendica\Core\Config\Configuration;
  6. use Friendica\Model\User;
  7. use Friendica\Network\HTTPException;
  8. use Friendica\Protocol\Diaspora;
  9. use Friendica\Util\Network;
  10. use Psr\Log\LoggerInterface;
  11. /**
  12. * This module is part of the Diaspora protocol.
  13. * It is used for receiving single posts either for public or for a specific user.
  14. */
  15. class Receive extends BaseModule
  16. {
  17. /** @var LoggerInterface */
  18. private static $logger;
  19. public static function init()
  20. {
  21. /** @var LoggerInterface $logger */
  22. self::$logger = self::getClass(LoggerInterface::class);
  23. }
  24. public static function post()
  25. {
  26. /** @var Configuration $config */
  27. $config = self::getClass(Configuration::class);
  28. $enabled = $config->get('system', 'diaspora_enabled', false);
  29. if (!$enabled) {
  30. self::$logger->info('Diaspora disabled.');
  31. throw new HTTPException\InternalServerErrorException('Diaspora disabled.');
  32. }
  33. /** @var App\Arguments $args */
  34. $args = self::getClass(App\Arguments::class);
  35. $type = $args->get(1);
  36. switch ($type) {
  37. case 'public':
  38. self::receivePublic();
  39. break;
  40. case 'users':
  41. self::receiveUser($args->get(2));
  42. break;
  43. default:
  44. self::$logger->info('Wrong call.');
  45. throw new HTTPException\InternalServerErrorException('wrong call.');
  46. break;
  47. }
  48. }
  49. /**
  50. * Receive a public Diaspora posting
  51. *
  52. * @throws HTTPException\InternalServerErrorException
  53. * @throws \ImagickException
  54. */
  55. private static function receivePublic()
  56. {
  57. self::$logger->info('Diaspora: Receiving post.');
  58. $msg = self::decodePost();
  59. self::$logger->info('Diaspora: Dispatching.');
  60. Diaspora::dispatchPublic($msg);
  61. }
  62. /**
  63. * Receive a Diaspora posting for a user
  64. *
  65. * @param string $guid The GUID of the importer
  66. *
  67. * @throws HTTPException\InternalServerErrorException
  68. * @throws \ImagickException
  69. */
  70. private static function receiveUser(string $guid)
  71. {
  72. self::$logger->info('Diaspora: Receiving post.');
  73. $importer = User::getByGuid($guid);
  74. $msg = self::decodePost(false, $importer['prvkey'] ?? '');
  75. self::$logger->info('Diaspora: Dispatching.');
  76. if (Diaspora::dispatch($importer, $msg)) {
  77. throw new HTTPException\OKException();
  78. } else {
  79. throw new HTTPException\InternalServerErrorException();
  80. }
  81. }
  82. /**
  83. * Decodes a Diaspora message based on the posted data
  84. *
  85. * @param string $privKey The private key of the importer
  86. * @param bool $public True, if the post is public
  87. *
  88. * @return array
  89. * @throws HTTPException\InternalServerErrorException
  90. * @throws \ImagickException
  91. */
  92. private static function decodePost(bool $public = true, string $privKey = '')
  93. {
  94. if (empty($_POST['xml'])) {
  95. $postdata = Network::postdata();
  96. if (empty($postdata)) {
  97. throw new HTTPException\InternalServerErrorException('Missing postdata.');
  98. }
  99. self::$logger->info('Diaspora: Message is in the new format.');
  100. $msg = Diaspora::decodeRaw($postdata, $privKey);
  101. } else {
  102. $xml = urldecode($_POST['xml']);
  103. self::$logger->info('Diaspora: Decode message in the old format.');
  104. $msg = Diaspora::decode($xml, $privKey);
  105. if ($public && !$msg) {
  106. self::$logger->info('Diaspora: Decode message in the new format.');
  107. $msg = Diaspora::decodeRaw($xml, $privKey);
  108. }
  109. }
  110. self::$logger->info('Diaspora: Post encoded.');
  111. self::$logger->debug('Diaspora: Decoded message.', ['msg' => print_r($msg, true)]);
  112. if (!is_array($msg)) {
  113. throw new HTTPException\InternalServerErrorException('Message is not an array.');
  114. }
  115. return $msg;
  116. }
  117. }