2019-10-20 11:00:08 +00:00
|
|
|
<?php
|
2020-02-09 14:45:36 +00:00
|
|
|
/**
|
2021-03-29 06:40:20 +00:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 14:45:36 +00: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 11:00:08 +00:00
|
|
|
|
|
|
|
namespace Friendica\Module\Diaspora;
|
|
|
|
|
2021-11-20 14:38:03 +00:00
|
|
|
use Friendica\App;
|
2019-10-20 11:00:08 +00:00
|
|
|
use Friendica\BaseModule;
|
2021-11-19 19:18:48 +00:00
|
|
|
use Friendica\Core\Config\Capability\IManageConfigValues;
|
|
|
|
use Friendica\Core\L10n;
|
2019-10-20 11:00:08 +00:00
|
|
|
use Friendica\Model\User;
|
|
|
|
use Friendica\Network\HTTPException;
|
|
|
|
use Friendica\Protocol\Diaspora;
|
|
|
|
use Friendica\Util\Network;
|
2021-11-20 14:38:03 +00:00
|
|
|
use Friendica\Util\Profiler;
|
2019-10-20 11:00:08 +00:00
|
|
|
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
|
|
|
|
{
|
2021-11-19 19:18:48 +00:00
|
|
|
/** @var IManageConfigValues */
|
|
|
|
protected $config;
|
2019-10-20 11:00:08 +00:00
|
|
|
|
2021-11-20 14:38:03 +00:00
|
|
|
public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, IManageConfigValues $config, array $server, array $parameters = [])
|
2019-10-20 11:00:08 +00:00
|
|
|
{
|
2021-11-20 14:38:03 +00:00
|
|
|
parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $server, $parameters);
|
|
|
|
|
2021-11-19 19:18:48 +00:00
|
|
|
$this->config = $config;
|
2019-10-20 11:00:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 14:38:03 +00:00
|
|
|
protected function post(array $request = [], array $post = [])
|
2019-10-20 11:00:08 +00:00
|
|
|
{
|
2021-11-19 19:18:48 +00:00
|
|
|
$enabled = $this->config->get('system', 'diaspora_enabled', false);
|
2019-10-20 11:00:08 +00:00
|
|
|
if (!$enabled) {
|
2021-11-19 19:18:48 +00:00
|
|
|
$this->logger->info('Diaspora disabled.');
|
|
|
|
throw new HTTPException\ForbiddenException($this->t('Access denied.'));
|
2019-10-20 11:00:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 22:19:25 +00:00
|
|
|
if ($this->parameters['type'] === 'public') {
|
2021-11-19 19:18:48 +00:00
|
|
|
$this->receivePublic();
|
2021-11-14 22:19:25 +00:00
|
|
|
} else if ($this->parameters['type'] === 'users') {
|
2021-11-19 19:18:48 +00:00
|
|
|
$this->receiveUser();
|
2019-10-20 11:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receive a public Diaspora posting
|
|
|
|
*
|
|
|
|
* @throws HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
|
|
|
*/
|
2021-11-19 19:18:48 +00:00
|
|
|
private function receivePublic()
|
2019-10-20 11:00:08 +00:00
|
|
|
{
|
2021-11-19 19:18:48 +00:00
|
|
|
$this->logger->info('Diaspora: Receiving post.');
|
2019-10-20 11:00:08 +00:00
|
|
|
|
2021-11-19 19:18:48 +00:00
|
|
|
$msg = $this->decodePost();
|
2019-10-20 11:00:08 +00:00
|
|
|
|
2021-11-19 19:18:48 +00:00
|
|
|
$this->logger->info('Diaspora: Dispatching.');
|
2019-10-20 11:00:08 +00:00
|
|
|
|
|
|
|
Diaspora::dispatchPublic($msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receive a Diaspora posting for a user
|
|
|
|
*
|
|
|
|
* @throws HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
|
|
|
*/
|
2021-11-19 19:18:48 +00:00
|
|
|
private function receiveUser()
|
2019-10-20 11:00:08 +00:00
|
|
|
{
|
2021-11-19 19:18:48 +00:00
|
|
|
$this->logger->info('Diaspora: Receiving post.');
|
2019-10-20 11:00:08 +00:00
|
|
|
|
2021-11-19 19:18:48 +00:00
|
|
|
$importer = User::getByGuid($this->parameters['guid']);
|
2019-10-20 11:00:08 +00:00
|
|
|
|
2021-11-19 19:18:48 +00:00
|
|
|
$msg = $this->decodePost(false, $importer['prvkey'] ?? '');
|
2019-10-20 11:00:08 +00:00
|
|
|
|
2021-11-19 19:18:48 +00:00
|
|
|
$this->logger->info('Diaspora: Dispatching.');
|
2019-10-20 11:00:08 +00:00
|
|
|
|
|
|
|
if (Diaspora::dispatch($importer, $msg)) {
|
|
|
|
throw new HTTPException\OKException();
|
|
|
|
} else {
|
2021-10-29 23:21:07 +00: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 11:00:08 +00: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
|
|
|
|
*/
|
2021-11-19 19:18:48 +00:00
|
|
|
private function decodePost(bool $public = true, string $privKey = '')
|
2019-10-20 11:00:08 +00:00
|
|
|
{
|
|
|
|
if (empty($_POST['xml'])) {
|
|
|
|
|
|
|
|
$postdata = Network::postdata();
|
|
|
|
|
|
|
|
if (empty($postdata)) {
|
|
|
|
throw new HTTPException\InternalServerErrorException('Missing postdata.');
|
|
|
|
}
|
|
|
|
|
2021-11-19 19:18:48 +00:00
|
|
|
$this->logger->info('Diaspora: Message is in the new format.');
|
2019-10-20 11:00:08 +00:00
|
|
|
|
|
|
|
$msg = Diaspora::decodeRaw($postdata, $privKey);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$xml = urldecode($_POST['xml']);
|
|
|
|
|
2021-11-19 19:18:48 +00:00
|
|
|
$this->logger->info('Diaspora: Decode message in the old format.');
|
2019-10-20 11:00:08 +00:00
|
|
|
$msg = Diaspora::decode($xml, $privKey);
|
|
|
|
|
|
|
|
if ($public && !$msg) {
|
2021-11-19 19:18:48 +00:00
|
|
|
$this->logger->info('Diaspora: Decode message in the new format.');
|
2019-10-20 11:00:08 +00:00
|
|
|
$msg = Diaspora::decodeRaw($xml, $privKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 19:18:48 +00:00
|
|
|
$this->logger->info('Diaspora: Post decoded.');
|
|
|
|
$this->logger->debug('Diaspora: Decoded message.', ['msg' => $msg]);
|
2019-10-20 11:00:08 +00:00
|
|
|
|
|
|
|
if (!is_array($msg)) {
|
|
|
|
throw new HTTPException\InternalServerErrorException('Message is not an array.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
}
|