mirror of
https://git.sekbaer.de/Friendica/friendica.git
synced 2025-06-16 20:05:14 +02:00
Move mod/receive to src/Module/Diaspora/receive
- Added routes - Make Diaspora::decode(Raw) more explicit - Add new User::getByGuid() method
This commit is contained in:
parent
49c05036ae
commit
7716374593
6 changed files with 182 additions and 103 deletions
|
@ -18,7 +18,6 @@ use Friendica\Core\Protocol;
|
|||
use Friendica\Core\System;
|
||||
use Friendica\Core\Worker;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Model\Photo;
|
||||
use Friendica\Model\TwoFactor\AppSpecificPassword;
|
||||
use Friendica\Object\Image;
|
||||
use Friendica\Util\Crypto;
|
||||
|
@ -105,6 +104,27 @@ class User
|
|||
return DBA::selectFirst('user', $fields, ['uid' => $uid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a user record based on it's GUID
|
||||
*
|
||||
* @param string $guid The guid of the user
|
||||
* @param array $fields The fields to retrieve
|
||||
* @param bool $active True, if only active records are searched
|
||||
*
|
||||
* @return array|boolean User record if it exists, false otherwise
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function getByGuid(string $guid, array $fields = [], bool $active = true)
|
||||
{
|
||||
if ($active) {
|
||||
$cond = ['guid' => $guid, 'account_expired' => false, 'account_removed' => false];
|
||||
} else {
|
||||
$cond = ['guid' => $guid];
|
||||
}
|
||||
|
||||
return DBA::selectFirst('user', $fields, $cond);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $nickname
|
||||
* @param array $fields
|
||||
|
|
146
src/Module/Diaspora/Receive.php
Normal file
146
src/Module/Diaspora/Receive.php
Normal file
|
@ -0,0 +1,146 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Module\Diaspora;
|
||||
|
||||
use Friendica\App;
|
||||
use Friendica\BaseModule;
|
||||
use Friendica\Core\Config\Configuration;
|
||||
use Friendica\Model\User;
|
||||
use Friendica\Network\HTTPException;
|
||||
use Friendica\Protocol\Diaspora;
|
||||
use Friendica\Util\Network;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* This module is part of the Diaspora protocol.
|
||||
* It is used for receiving single posts either for public or for a specific user.
|
||||
*/
|
||||
class Receive extends BaseModule
|
||||
{
|
||||
/** @var LoggerInterface */
|
||||
private static $logger;
|
||||
|
||||
public static function init()
|
||||
{
|
||||
/** @var LoggerInterface $logger */
|
||||
self::$logger = self::getClass(LoggerInterface::class);
|
||||
}
|
||||
|
||||
public static function post()
|
||||
{
|
||||
/** @var Configuration $config */
|
||||
$config = self::getClass(Configuration::class);
|
||||
|
||||
$enabled = $config->get('system', 'diaspora_enabled', false);
|
||||
if (!$enabled) {
|
||||
self::$logger->info('Diaspora disabled.');
|
||||
throw new HTTPException\InternalServerErrorException('Diaspora disabled.');
|
||||
}
|
||||
|
||||
/** @var App\Arguments $args */
|
||||
$args = self::getClass(App\Arguments::class);
|
||||
|
||||
$type = $args->get(1);
|
||||
|
||||
switch ($type) {
|
||||
case 'public':
|
||||
self::receivePublic();
|
||||
break;
|
||||
case 'users':
|
||||
self::receiveUser($args->get(2));
|
||||
break;
|
||||
default:
|
||||
self::$logger->info('Wrong call.');
|
||||
throw new HTTPException\InternalServerErrorException('wrong call.');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a public Diaspora posting
|
||||
*
|
||||
* @throws HTTPException\InternalServerErrorException
|
||||
* @throws \ImagickException
|
||||
*/
|
||||
private static function receivePublic()
|
||||
{
|
||||
self::$logger->info('Diaspora: Receiving post.');
|
||||
|
||||
$msg = self::decodePost();
|
||||
|
||||
self::$logger->info('Diaspora: Dispatching.');
|
||||
|
||||
Diaspora::dispatchPublic($msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a Diaspora posting for a user
|
||||
*
|
||||
* @param string $guid The GUID of the importer
|
||||
*
|
||||
* @throws HTTPException\InternalServerErrorException
|
||||
* @throws \ImagickException
|
||||
*/
|
||||
private static function receiveUser(string $guid)
|
||||
{
|
||||
self::$logger->info('Diaspora: Receiving post.');
|
||||
|
||||
$importer = User::getByGuid($guid);
|
||||
|
||||
$msg = self::decodePost(false, $importer['privKey'] ?? '');
|
||||
|
||||
self::$logger->info('Diaspora: Dispatching.');
|
||||
|
||||
if (Diaspora::dispatch($importer, $msg)) {
|
||||
throw new HTTPException\OKException();
|
||||
} else {
|
||||
throw new HTTPException\InternalServerErrorException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a Diaspora message based on the posted data
|
||||
*
|
||||
* @param string $privKey The private key of the importer
|
||||
* @param bool $public True, if the post is public
|
||||
*
|
||||
* @return array
|
||||
* @throws HTTPException\InternalServerErrorException
|
||||
* @throws \ImagickException
|
||||
*/
|
||||
private static function decodePost(bool $public = true, string $privKey = '')
|
||||
{
|
||||
if (empty($_POST['xml'])) {
|
||||
|
||||
$postdata = Network::postdata();
|
||||
|
||||
if (empty($postdata)) {
|
||||
throw new HTTPException\InternalServerErrorException('Missing postdata.');
|
||||
}
|
||||
|
||||
self::$logger->info('Diaspora: Message is in the new format.');
|
||||
|
||||
$msg = Diaspora::decodeRaw($postdata, $privKey);
|
||||
} else {
|
||||
|
||||
$xml = urldecode($_POST['xml']);
|
||||
|
||||
self::$logger->info('Diaspora: Decode message in the old format.');
|
||||
$msg = Diaspora::decode($xml, $privKey);
|
||||
|
||||
if ($public && !$msg) {
|
||||
self::$logger->info('Diaspora: Decode message in the new format.');
|
||||
$msg = Diaspora::decodeRaw($xml, $privKey);
|
||||
}
|
||||
}
|
||||
|
||||
self::$logger->info('Diaspora: Post encoded.');
|
||||
self::$logger->debug('Diaspora: Decoded message.', ['msg' => print_r($msg, true)]);
|
||||
|
||||
if (!is_array($msg)) {
|
||||
throw new HTTPException\InternalServerErrorException('Message is not an array.');
|
||||
}
|
||||
|
||||
return $msg;
|
||||
}
|
||||
}
|
|
@ -413,8 +413,8 @@ class Diaspora
|
|||
/**
|
||||
* @brief: Decodes incoming Diaspora message in the new format
|
||||
*
|
||||
* @param array $importer Array of the importer user
|
||||
* @param string $raw raw post message
|
||||
* @param string $privKey The private key of the importer
|
||||
* @param boolean $no_exit Don't do an http exit on error
|
||||
*
|
||||
* @return array
|
||||
|
@ -424,7 +424,7 @@ class Diaspora
|
|||
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
||||
* @throws \ImagickException
|
||||
*/
|
||||
public static function decodeRaw(array $importer, $raw, $no_exit = false)
|
||||
public static function decodeRaw(string $raw, string $privKey = '', bool $no_exit = false)
|
||||
{
|
||||
$data = json_decode($raw);
|
||||
|
||||
|
@ -434,7 +434,7 @@ class Diaspora
|
|||
$ciphertext = base64_decode($data->encrypted_magic_envelope);
|
||||
|
||||
$outer_key_bundle = '';
|
||||
@openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $importer['prvkey']);
|
||||
@openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $privKey);
|
||||
$j_outer_key_bundle = json_decode($outer_key_bundle);
|
||||
|
||||
if (!is_object($j_outer_key_bundle)) {
|
||||
|
@ -519,8 +519,8 @@ class Diaspora
|
|||
/**
|
||||
* @brief: Decodes incoming Diaspora message in the deprecated format
|
||||
*
|
||||
* @param array $importer Array of the importer user
|
||||
* @param string $xml urldecoded Diaspora salmon
|
||||
* @param string $privKey The private key of the importer
|
||||
*
|
||||
* @return array
|
||||
* 'message' -> decoded Diaspora XML message
|
||||
|
@ -529,7 +529,7 @@ class Diaspora
|
|||
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
||||
* @throws \ImagickException
|
||||
*/
|
||||
public static function decode(array $importer, $xml)
|
||||
public static function decode(string $xml, string $privKey = '')
|
||||
{
|
||||
$public = false;
|
||||
$basedom = XML::parseString($xml);
|
||||
|
@ -548,7 +548,7 @@ class Diaspora
|
|||
$author_link = str_replace('acct:', '', $children->header->author_id);
|
||||
} else {
|
||||
// This happens with posts from a relais
|
||||
if (!$importer) {
|
||||
if (empty($privKey)) {
|
||||
Logger::log("This is no private post in the old format", Logger::DEBUG);
|
||||
return false;
|
||||
}
|
||||
|
@ -559,7 +559,7 @@ class Diaspora
|
|||
$ciphertext = base64_decode($encrypted_header->ciphertext);
|
||||
|
||||
$outer_key_bundle = '';
|
||||
openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $importer['prvkey']);
|
||||
openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $privKey);
|
||||
|
||||
$j_outer_key_bundle = json_decode($outer_key_bundle);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue