2019-10-20 07:00:08 -04:00
|
|
|
<?php
|
2020-02-09 09:45:36 -05:00
|
|
|
/**
|
2021-03-29 02:40:20 -04:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 09:45:36 -05:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-10-20 07:00:08 -04:00
|
|
|
|
|
|
|
namespace Friendica\Module\Diaspora;
|
|
|
|
|
|
|
|
use Friendica\BaseModule;
|
2019-12-15 17:28:01 -05:00
|
|
|
use Friendica\DI;
|
2019-10-20 07:00:08 -04:00
|
|
|
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;
|
|
|
|
|
2019-11-05 16:48:54 -05:00
|
|
|
public static function init(array $parameters = [])
|
2019-10-20 07:00:08 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
self::$logger = DI::logger();
|
2019-10-20 07:00:08 -04:00
|
|
|
}
|
|
|
|
|
2019-11-05 16:48:54 -05:00
|
|
|
public static function post(array $parameters = [])
|
2019-10-20 07:00:08 -04:00
|
|
|
{
|
2019-12-15 17:28:01 -05:00
|
|
|
$enabled = DI::config()->get('system', 'diaspora_enabled', false);
|
2019-10-20 07:00:08 -04:00
|
|
|
if (!$enabled) {
|
|
|
|
self::$logger->info('Diaspora disabled.');
|
2019-12-15 17:28:01 -05:00
|
|
|
throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
|
2019-10-20 07:00:08 -04:00
|
|
|
}
|
|
|
|
|
2021-10-26 11:42:49 -04:00
|
|
|
if ($parameters['type'] === 'public') {
|
|
|
|
self::receivePublic();
|
|
|
|
} else if ($parameters['type'] === 'users') {
|
|
|
|
self::receiveUser($parameters['guid']);
|
2019-10-20 07:00:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2019-10-21 05:34:47 -04:00
|
|
|
$msg = self::decodePost(false, $importer['prvkey'] ?? '');
|
2019-10-20 07:00:08 -04:00
|
|
|
|
|
|
|
self::$logger->info('Diaspora: Dispatching.');
|
|
|
|
|
|
|
|
if (Diaspora::dispatch($importer, $msg)) {
|
|
|
|
throw new HTTPException\OKException();
|
|
|
|
} else {
|
2021-10-29 19:21:07 -04:00
|
|
|
// We couldn't process the content.
|
|
|
|
// To avoid the remote system trying again we send the message that we accepted the content.
|
|
|
|
throw new HTTPException\AcceptedException();
|
2019-10-20 07:00:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 11:29:53 -04:00
|
|
|
self::$logger->info('Diaspora: Post decoded.');
|
2020-06-29 16:22:00 -04:00
|
|
|
self::$logger->debug('Diaspora: Decoded message.', ['msg' => $msg]);
|
2019-10-20 07:00:08 -04:00
|
|
|
|
|
|
|
if (!is_array($msg)) {
|
|
|
|
throw new HTTPException\InternalServerErrorException('Message is not an array.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
}
|