2018-09-28 11:41:24 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nextcloud - Social Support
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later. See the COPYING file.
|
|
|
|
*
|
|
|
|
* @author Maxence Lange <maxence@artificial-owl.com>
|
|
|
|
* @copyright 2018, Maxence Lange <maxence@artificial-owl.com>
|
|
|
|
* @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 <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Social\Service;
|
|
|
|
|
|
|
|
|
2018-10-12 06:44:35 +00:00
|
|
|
use daita\MySmallPhpTools\Model\Request;
|
2018-11-12 22:57:32 +00:00
|
|
|
use daita\MySmallPhpTools\Traits\TArrayTools;
|
2018-09-28 11:41:24 +00:00
|
|
|
use Exception;
|
2018-12-17 09:12:27 +00:00
|
|
|
use OCA\Social\AP;
|
2018-11-21 19:22:46 +00:00
|
|
|
use OCA\Social\Db\FollowsRequest;
|
2018-11-20 12:47:14 +00:00
|
|
|
use OCA\Social\Db\NotesRequest;
|
2018-09-28 11:41:24 +00:00
|
|
|
use OCA\Social\Exceptions\ActorDoesNotExistException;
|
2018-11-27 16:59:19 +00:00
|
|
|
use OCA\Social\Exceptions\EmptyQueueException;
|
2018-11-15 11:26:25 +00:00
|
|
|
use OCA\Social\Exceptions\InvalidResourceException;
|
2018-11-27 16:59:19 +00:00
|
|
|
use OCA\Social\Exceptions\NoHighPriorityRequestException;
|
|
|
|
use OCA\Social\Exceptions\QueueStatusException;
|
2018-11-28 15:26:28 +00:00
|
|
|
use OCA\Social\Exceptions\Request410Exception;
|
2018-10-01 12:14:23 +00:00
|
|
|
use OCA\Social\Exceptions\RequestException;
|
2018-11-15 11:26:25 +00:00
|
|
|
use OCA\Social\Exceptions\SocialAppConfigException;
|
2018-12-17 09:12:27 +00:00
|
|
|
use OCA\Social\Exceptions\UnknownItemException;
|
2018-11-12 22:57:32 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\ACore;
|
2018-11-19 10:34:54 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Create;
|
2018-11-21 19:22:46 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Activity\Delete;
|
2018-12-13 09:40:27 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Actor\Person;
|
|
|
|
use OCA\Social\Model\ActivityPub\Object\Tombstone;
|
2018-10-01 12:14:23 +00:00
|
|
|
use OCA\Social\Model\InstancePath;
|
2018-11-27 16:59:19 +00:00
|
|
|
use OCA\Social\Model\RequestQueue;
|
2018-09-28 11:41:24 +00:00
|
|
|
|
2018-11-12 22:57:32 +00:00
|
|
|
class ActivityService {
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
|
2018-11-12 22:57:32 +00:00
|
|
|
use TArrayTools;
|
|
|
|
|
|
|
|
|
|
|
|
const REQUEST_INBOX = 1;
|
|
|
|
|
2018-12-04 10:58:00 +00:00
|
|
|
const TIMEOUT_LIVE = 2;
|
|
|
|
const TIMEOUT_ASYNC = 5;
|
|
|
|
const TIMEOUT_SERVICE = 10;
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
const CONTEXT_ACTIVITYSTREAMS = 'https://www.w3.org/ns/activitystreams';
|
|
|
|
const CONTEXT_SECURITY = 'https://w3id.org/security/v1';
|
|
|
|
|
|
|
|
const TO_PUBLIC = 'https://www.w3.org/ns/activitystreams#Public';
|
|
|
|
|
|
|
|
const DATE_FORMAT = 'D, d M Y H:i:s T';
|
|
|
|
const DATE_DELAY = 30;
|
|
|
|
|
2018-11-27 16:59:19 +00:00
|
|
|
|
2018-11-20 12:47:14 +00:00
|
|
|
/** @var NotesRequest */
|
|
|
|
private $notesRequest;
|
|
|
|
|
2018-11-21 19:22:46 +00:00
|
|
|
/** @var FollowsRequest */
|
|
|
|
private $followsRequest;
|
|
|
|
|
2018-11-27 16:59:19 +00:00
|
|
|
/** @var QueueService */
|
|
|
|
private $queueService;
|
|
|
|
|
2018-12-17 09:12:27 +00:00
|
|
|
/** @var AccountService */
|
|
|
|
private $accountService;
|
2018-10-01 12:14:23 +00:00
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
/** @var ConfigService */
|
|
|
|
private $configService;
|
|
|
|
|
|
|
|
/** @var CurlService */
|
|
|
|
private $curlService;
|
|
|
|
|
|
|
|
/** @var MiscService */
|
|
|
|
private $miscService;
|
|
|
|
|
|
|
|
|
2018-11-28 20:21:28 +00:00
|
|
|
/** @var array */
|
|
|
|
private $failInstances;
|
|
|
|
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
/**
|
2018-11-12 22:57:32 +00:00
|
|
|
* ActivityService constructor.
|
2018-09-28 11:41:24 +00:00
|
|
|
*
|
2018-11-20 12:47:14 +00:00
|
|
|
* @param NotesRequest $notesRequest
|
2018-11-21 19:22:46 +00:00
|
|
|
* @param FollowsRequest $followsRequest
|
2018-12-17 09:12:27 +00:00
|
|
|
* @param QueueService $queueService
|
|
|
|
* @param AccountService $accountService
|
2018-10-01 12:14:23 +00:00
|
|
|
* @param CurlService $curlService
|
2018-09-28 11:41:24 +00:00
|
|
|
* @param ConfigService $configService
|
|
|
|
* @param MiscService $miscService
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2018-12-17 09:12:27 +00:00
|
|
|
NotesRequest $notesRequest, FollowsRequest $followsRequest, QueueService $queueService,
|
|
|
|
AccountService $accountService,
|
|
|
|
CurlService $curlService, ConfigService $configService, MiscService $miscService
|
2018-09-28 11:41:24 +00:00
|
|
|
) {
|
2018-11-20 12:47:14 +00:00
|
|
|
$this->notesRequest = $notesRequest;
|
2018-11-21 19:22:46 +00:00
|
|
|
$this->followsRequest = $followsRequest;
|
2018-11-27 16:59:19 +00:00
|
|
|
$this->queueService = $queueService;
|
2018-12-17 09:12:27 +00:00
|
|
|
$this->accountService = $accountService;
|
2018-11-27 16:59:19 +00:00
|
|
|
$this->curlService = $curlService;
|
2018-09-28 11:41:24 +00:00
|
|
|
$this->configService = $configService;
|
|
|
|
$this->miscService = $miscService;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-11-19 10:34:54 +00:00
|
|
|
* @param Person $actor
|
2018-11-12 22:57:32 +00:00
|
|
|
* @param ACore $item
|
|
|
|
* @param ACore $activity
|
2018-09-28 11:41:24 +00:00
|
|
|
*
|
2018-11-27 16:59:19 +00:00
|
|
|
* @return string
|
|
|
|
* @throws Exception
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
2018-12-03 21:03:22 +00:00
|
|
|
public function createActivity(Person $actor, ACore $item, ACore &$activity = null): string {
|
2018-11-12 22:57:32 +00:00
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
$activity = new Create();
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
// $this->activityStreamsService->initCore($activity);
|
|
|
|
|
2018-11-12 22:57:32 +00:00
|
|
|
$activity->setObject($item);
|
2018-09-28 11:41:24 +00:00
|
|
|
$activity->setId($item->getId() . '/activity');
|
2018-11-21 21:58:56 +00:00
|
|
|
$activity->setInstancePaths($item->getInstancePaths());
|
2018-10-01 12:14:23 +00:00
|
|
|
|
|
|
|
// if ($item->getToArray() !== []) {
|
|
|
|
// $activity->setToArray($item->getToArray());
|
|
|
|
// } else {
|
|
|
|
// $activity->setTo($item->getTo());
|
|
|
|
// }
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
$activity->setActor($actor);
|
|
|
|
|
2018-11-27 16:59:19 +00:00
|
|
|
return $this->request($activity);
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-21 19:22:46 +00:00
|
|
|
/**
|
|
|
|
* @param ACore $item
|
|
|
|
*
|
2018-11-27 16:59:19 +00:00
|
|
|
* @return string
|
|
|
|
* @throws Exception
|
2018-11-21 19:22:46 +00:00
|
|
|
*/
|
2018-11-27 16:59:19 +00:00
|
|
|
public function deleteActivity(ACore $item): string {
|
2018-11-21 19:22:46 +00:00
|
|
|
$delete = new Delete();
|
|
|
|
$delete->setId($item->getId() . '#delete');
|
|
|
|
$delete->setActorId($item->getActorId());
|
|
|
|
|
|
|
|
$tombstone = new Tombstone($delete);
|
|
|
|
$tombstone->setId($item->getId());
|
|
|
|
|
|
|
|
$delete->setObject($tombstone);
|
|
|
|
$delete->addInstancePaths($item->getInstancePaths());
|
|
|
|
|
2018-11-27 16:59:19 +00:00
|
|
|
return $this->request($delete);
|
2018-11-21 19:22:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-20 12:47:14 +00:00
|
|
|
/**
|
|
|
|
* @param string $id
|
|
|
|
*
|
|
|
|
* @return ACore
|
|
|
|
* @throws InvalidResourceException
|
|
|
|
*/
|
|
|
|
public function getItem(string $id): ACore {
|
|
|
|
if ($id === '') {
|
|
|
|
throw new InvalidResourceException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$requests = [
|
2018-12-17 10:54:16 +00:00
|
|
|
'Note'
|
2018-11-20 12:47:14 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($requests as $request) {
|
|
|
|
try {
|
2018-12-17 09:12:27 +00:00
|
|
|
$interface = AP::$activityPub->getInterfaceFromType($request);
|
2018-11-20 12:47:14 +00:00
|
|
|
|
2018-12-17 09:12:27 +00:00
|
|
|
return $interface->getItemById($id);
|
2018-11-20 12:47:14 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new InvalidResourceException();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
/**
|
|
|
|
* @param ACore $activity
|
|
|
|
*
|
2018-11-27 16:59:19 +00:00
|
|
|
* @return string
|
|
|
|
* @throws Exception
|
2018-11-19 10:34:54 +00:00
|
|
|
*/
|
2018-11-27 16:59:19 +00:00
|
|
|
public function request(ACore $activity): string {
|
2018-12-03 21:03:22 +00:00
|
|
|
$this->saveActivity($activity);
|
2018-11-27 16:59:19 +00:00
|
|
|
|
|
|
|
$author = $this->getAuthorFromItem($activity);
|
|
|
|
$instancePaths = $this->generateInstancePaths($activity);
|
|
|
|
$token = $this->queueService->generateRequestQueue($instancePaths, $activity, $author);
|
2018-11-28 20:21:28 +00:00
|
|
|
$this->manageInit();
|
2018-11-27 16:59:19 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$directRequest = $this->queueService->getPriorityRequest($token);
|
2018-12-04 10:58:00 +00:00
|
|
|
$directRequest->setTimeout(self::TIMEOUT_LIVE);
|
2018-11-27 16:59:19 +00:00
|
|
|
$this->manageRequest($directRequest);
|
2018-11-28 20:21:28 +00:00
|
|
|
} catch (RequestException $e) {
|
2018-11-27 16:59:19 +00:00
|
|
|
} catch (NoHighPriorityRequestException $e) {
|
|
|
|
} catch (EmptyQueueException $e) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->curlService->asyncWithToken($token);
|
|
|
|
|
|
|
|
return $token;
|
2018-11-19 10:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-28 20:21:28 +00:00
|
|
|
public function manageInit() {
|
|
|
|
$this->failInstances = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
/**
|
2018-11-27 16:59:19 +00:00
|
|
|
* @param RequestQueue $queue
|
2018-11-15 11:26:25 +00:00
|
|
|
*
|
2018-10-01 12:14:23 +00:00
|
|
|
* @throws RequestException
|
2018-11-15 11:26:25 +00:00
|
|
|
* @throws SocialAppConfigException
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
2018-11-27 16:59:19 +00:00
|
|
|
public function manageRequest(RequestQueue $queue) {
|
2018-11-28 20:21:28 +00:00
|
|
|
$host = $queue->getInstance()
|
|
|
|
->getAddress();
|
|
|
|
if (in_array($host, $this->failInstances)) {
|
|
|
|
throw new RequestException();
|
|
|
|
}
|
2018-11-27 16:59:19 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->queueService->initRequest($queue);
|
|
|
|
} catch (QueueStatusException $e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 18:18:37 +00:00
|
|
|
try {
|
2018-12-04 10:58:00 +00:00
|
|
|
$result = $this->generateRequestFromQueue($queue);
|
2018-11-28 18:18:37 +00:00
|
|
|
} catch (ActorDoesNotExistException $e) {
|
|
|
|
$this->queueService->deleteRequest($queue);
|
2018-11-30 11:21:11 +00:00
|
|
|
|
|
|
|
return;
|
2018-11-28 18:18:37 +00:00
|
|
|
} catch (Request410Exception $e) {
|
|
|
|
$this->queueService->deleteRequest($queue);
|
2018-11-30 11:21:11 +00:00
|
|
|
|
|
|
|
return;
|
2018-11-28 18:18:37 +00:00
|
|
|
}
|
2018-09-28 11:41:24 +00:00
|
|
|
|
2018-11-27 16:59:19 +00:00
|
|
|
try {
|
2018-12-04 23:49:52 +00:00
|
|
|
$accepted = [200, 202];
|
|
|
|
if (in_array($this->getint('_code', $result, 500), $accepted)) {
|
2018-11-27 16:59:19 +00:00
|
|
|
$this->queueService->endRequest($queue, true);
|
|
|
|
} else {
|
|
|
|
$this->queueService->endRequest($queue, false);
|
2018-11-28 20:21:28 +00:00
|
|
|
$this->failInstances[] = $host;
|
2018-11-27 16:59:19 +00:00
|
|
|
}
|
|
|
|
} catch (QueueStatusException $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-17 09:12:27 +00:00
|
|
|
/** // ====> instanceService
|
|
|
|
*
|
2018-11-27 16:59:19 +00:00
|
|
|
* @param ACore $activity
|
|
|
|
*
|
|
|
|
* @return InstancePath[]
|
|
|
|
*/
|
|
|
|
private function generateInstancePaths(ACore $activity): array {
|
|
|
|
$instancePaths = [];
|
2018-11-21 19:22:46 +00:00
|
|
|
foreach ($activity->getInstancePaths() as $instancePath) {
|
|
|
|
if ($instancePath->getType() === InstancePath::TYPE_FOLLOWERS) {
|
2018-11-27 16:59:19 +00:00
|
|
|
$instancePaths = array_merge(
|
|
|
|
$instancePaths, $this->generateInstancePathsFollowers($instancePath)
|
|
|
|
);
|
2018-11-21 19:22:46 +00:00
|
|
|
} else {
|
2018-11-27 16:59:19 +00:00
|
|
|
$instancePaths[] = $instancePath;
|
2018-10-01 12:14:23 +00:00
|
|
|
}
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 16:59:19 +00:00
|
|
|
return $instancePaths;
|
2018-11-21 19:22:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param InstancePath $instancePath
|
|
|
|
*
|
2018-11-27 16:59:19 +00:00
|
|
|
* @return InstancePath[]
|
2018-11-21 19:22:46 +00:00
|
|
|
*/
|
2018-11-27 16:59:19 +00:00
|
|
|
private function generateInstancePathsFollowers(InstancePath $instancePath): array {
|
|
|
|
$follows = $this->followsRequest->getByFollowId($instancePath->getUri());
|
2018-11-21 19:22:46 +00:00
|
|
|
|
|
|
|
$sharedInboxes = [];
|
2018-11-27 16:59:19 +00:00
|
|
|
$instancePaths = [];
|
2018-11-21 19:22:46 +00:00
|
|
|
foreach ($follows as $follow) {
|
|
|
|
if (!$follow->gotActor()) {
|
|
|
|
// TODO - check if cache can be empty at this point ?
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sharedInbox = $follow->getActor()
|
|
|
|
->getSharedInbox();
|
|
|
|
if (in_array($sharedInbox, $sharedInboxes)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sharedInboxes[] = $sharedInbox;
|
2018-11-27 16:59:19 +00:00
|
|
|
$instancePaths[] = new InstancePath(
|
|
|
|
$sharedInbox, InstancePath::TYPE_GLOBAL, $instancePath->getPriority()
|
2018-11-21 19:22:46 +00:00
|
|
|
);
|
2018-11-27 16:59:19 +00:00
|
|
|
// $result[] = $this->generateRequest(
|
|
|
|
// new InstancePath($sharedInbox, InstancePath::TYPE_GLOBAL), $activity
|
|
|
|
// );
|
2018-11-21 19:22:46 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 16:59:19 +00:00
|
|
|
return $instancePaths;
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-12-04 10:58:00 +00:00
|
|
|
* @param RequestQueue $queue
|
2018-09-28 11:41:24 +00:00
|
|
|
*
|
2018-10-01 12:14:23 +00:00
|
|
|
* @return Request[]
|
2018-11-21 19:22:46 +00:00
|
|
|
* @throws ActorDoesNotExistException
|
2018-11-28 18:18:37 +00:00
|
|
|
* @throws Request410Exception
|
2018-10-01 12:14:23 +00:00
|
|
|
* @throws RequestException
|
2018-11-15 11:26:25 +00:00
|
|
|
* @throws SocialAppConfigException
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
2018-12-04 10:58:00 +00:00
|
|
|
public function generateRequestFromQueue(RequestQueue $queue): array {
|
|
|
|
//InstancePath $path, string $activity, string $author
|
|
|
|
// $queue->getInstance(), $queue->getActivity(), $queue->getAuthor()
|
|
|
|
// );
|
|
|
|
$path = $queue->getInstance();
|
|
|
|
|
2018-11-27 16:59:19 +00:00
|
|
|
// $document = json_encode($activity);
|
2018-11-12 22:57:32 +00:00
|
|
|
$date = gmdate(self::DATE_FORMAT);
|
2018-12-04 10:58:00 +00:00
|
|
|
$localActor = $this->getActorFromAuthor($queue->getAuthor());
|
2018-09-28 11:41:24 +00:00
|
|
|
|
2018-12-17 09:12:27 +00:00
|
|
|
// TODO: move this to SignatureService ?
|
2018-09-28 11:41:24 +00:00
|
|
|
$localActorLink =
|
2018-11-23 20:36:35 +00:00
|
|
|
$this->configService->getUrlSocial() . '@' . $localActor->getPreferredUsername();
|
2018-11-21 19:22:46 +00:00
|
|
|
$signature = "(request-target): post " . $path->getPath() . "\nhost: " . $path->getAddress()
|
2018-09-28 11:41:24 +00:00
|
|
|
. "\ndate: " . $date;
|
|
|
|
openssl_sign($signature, $signed, $localActor->getPrivateKey(), OPENSSL_ALGO_SHA256);
|
|
|
|
|
|
|
|
$signed = base64_encode($signed);
|
|
|
|
$header =
|
|
|
|
'keyId="' . $localActorLink . '",headers="(request-target) host date",signature="'
|
|
|
|
. $signed . '"';
|
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
$requestType = Request::TYPE_GET;
|
2018-11-21 19:22:46 +00:00
|
|
|
if ($path->getType() === InstancePath::TYPE_INBOX
|
|
|
|
|| $path->getType() === InstancePath::TYPE_GLOBAL
|
|
|
|
|| $path->getType() === InstancePath::TYPE_FOLLOWERS) {
|
2018-11-12 22:57:32 +00:00
|
|
|
$requestType = Request::TYPE_POST;
|
|
|
|
}
|
|
|
|
|
|
|
|
$request = new Request($path->getPath(), $requestType);
|
2018-12-04 10:58:00 +00:00
|
|
|
$request->setTimeout($queue->getTimeout());
|
2018-11-21 19:22:46 +00:00
|
|
|
$request->addHeader('Host: ' . $path->getAddress());
|
2018-09-28 11:41:24 +00:00
|
|
|
$request->addHeader('Date: ' . $date);
|
|
|
|
$request->addHeader('Signature: ' . $header);
|
|
|
|
|
2018-12-04 10:58:00 +00:00
|
|
|
$request->setDataJson($queue->getActivity());
|
2018-11-21 19:22:46 +00:00
|
|
|
$request->setAddress($path->getAddress());
|
2018-09-28 11:41:24 +00:00
|
|
|
|
2018-10-01 12:14:23 +00:00
|
|
|
return $this->curlService->request($request);
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
/**
|
|
|
|
* @param ACore $activity
|
|
|
|
*
|
2018-11-27 16:59:19 +00:00
|
|
|
* @return string
|
2018-11-19 10:34:54 +00:00
|
|
|
*/
|
2018-11-27 16:59:19 +00:00
|
|
|
private function getAuthorFromItem(Acore $activity): string {
|
2018-11-19 10:34:54 +00:00
|
|
|
if ($activity->gotActor()) {
|
2018-11-27 16:59:19 +00:00
|
|
|
return $activity->getActor()
|
|
|
|
->getId();
|
2018-11-19 10:34:54 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 16:59:19 +00:00
|
|
|
return $activity->getActorId();
|
|
|
|
}
|
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
|
2018-11-27 16:59:19 +00:00
|
|
|
/**
|
|
|
|
* @param string $author
|
|
|
|
*
|
|
|
|
* @return Person
|
|
|
|
* @throws SocialAppConfigException
|
2018-12-17 09:12:27 +00:00
|
|
|
* @throws ActorDoesNotExistException
|
2018-11-27 16:59:19 +00:00
|
|
|
*/
|
|
|
|
private function getActorFromAuthor(string $author): Person {
|
2018-12-17 09:12:27 +00:00
|
|
|
return $this->accountService->getFromId($author);
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-17 09:12:27 +00:00
|
|
|
// /**
|
|
|
|
// * @param IRequest $request
|
|
|
|
// *
|
|
|
|
// * @return string
|
|
|
|
// * @throws InvalidResourceException
|
|
|
|
// * @throws MalformedArrayException
|
|
|
|
// * @throws Request410Exception
|
|
|
|
// * @throws RequestException
|
|
|
|
// * @throws SignatureException
|
|
|
|
// * @throws SocialAppConfigException
|
|
|
|
// * @throws UrlCloudException
|
|
|
|
// * @throws InvalidOriginException
|
|
|
|
// */
|
|
|
|
// private function checkSignature(IRequest $request): string {
|
|
|
|
// $signatureHeader = $request->getHeader('Signature');
|
|
|
|
//
|
|
|
|
// $sign = $this->parseSignatureHeader($signatureHeader);
|
|
|
|
// $this->mustContains(['keyId', 'headers', 'signature'], $sign);
|
|
|
|
//
|
|
|
|
// $keyId = $sign['keyId'];
|
|
|
|
// $origin = $this->getKeyOrigin($keyId);
|
|
|
|
//
|
|
|
|
// $headers = $sign['headers'];
|
|
|
|
// $signed = base64_decode($sign['signature']);
|
|
|
|
// $estimated = $this->generateEstimatedSignature($headers, $request);
|
|
|
|
//
|
|
|
|
// $publicKey = $this->retrieveKey($keyId);
|
|
|
|
//
|
|
|
|
// if ($publicKey === '' || openssl_verify($estimated, $signed, $publicKey, 'sha256') !== 1) {
|
|
|
|
// throw new SignatureException('signature cannot be checked');
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return $origin;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * @param $id
|
|
|
|
// *
|
|
|
|
// * @return string
|
|
|
|
// * @throws InvalidOriginException
|
|
|
|
// */
|
|
|
|
// private function getKeyOrigin($id) {
|
|
|
|
// $host = parse_url($id, PHP_URL_HOST);
|
|
|
|
// if (is_string($host) && ($host !== '')) {
|
|
|
|
// return $host;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// throw new InvalidOriginException();
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// /**
|
|
|
|
// * @param string $headers
|
|
|
|
// * @param IRequest $request
|
|
|
|
// *
|
|
|
|
// * @return string
|
|
|
|
// */
|
|
|
|
// private function generateEstimatedSignature(string $headers, IRequest $request): string {
|
|
|
|
// $keys = explode(' ', $headers);
|
|
|
|
//
|
|
|
|
// $target = '';
|
|
|
|
// try {
|
|
|
|
// $target = strtolower($request->getMethod()) . " " . $request->getRequestUri();
|
|
|
|
// } catch (Exception $e) {
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// $estimated = "(request-target): " . $target;
|
|
|
|
//
|
|
|
|
// foreach ($keys as $key) {
|
|
|
|
// if ($key === '(request-target)') {
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// $estimated .= "\n" . $key . ': ' . $request->getHeader($key);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return $estimated;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// /**
|
|
|
|
// * @param $signatureHeader
|
|
|
|
// *
|
|
|
|
// * @return array
|
|
|
|
// */
|
|
|
|
// private function parseSignatureHeader($signatureHeader) {
|
|
|
|
// $sign = [];
|
|
|
|
//
|
|
|
|
// $entries = explode(',', $signatureHeader);
|
|
|
|
// foreach ($entries as $entry) {
|
|
|
|
// list($k, $v) = explode('=', $entry, 2);
|
|
|
|
// preg_match('/"([^"]+)"/', $v, $varr);
|
|
|
|
// $v = trim($varr[0], '"');
|
|
|
|
//
|
|
|
|
// $sign[$k] = $v;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return $sign;
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * @param $keyId
|
|
|
|
// *
|
|
|
|
// * @return string
|
|
|
|
// * @throws InvalidResourceException
|
|
|
|
// * @throws RequestException
|
|
|
|
// * @throws SocialAppConfigException
|
|
|
|
// * @throws UrlCloudException
|
|
|
|
// * @throws Request410Exception
|
|
|
|
// */
|
|
|
|
// private function retrieveKey($keyId): string {
|
|
|
|
// $actor = $this->cacheActorService->getFromId($keyId);
|
|
|
|
//
|
|
|
|
// return $actor->getPublicKey();
|
|
|
|
// }
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-12-17 09:12:27 +00:00
|
|
|
* @param ACore $activity
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
2018-12-17 09:12:27 +00:00
|
|
|
private function saveActivity(ACore $activity) {
|
|
|
|
// TODO: save activity in DB ?
|
2018-09-28 11:41:24 +00:00
|
|
|
|
2018-12-17 09:12:27 +00:00
|
|
|
if ($activity->gotObject()) {
|
|
|
|
$this->saveObject($activity->getObject());
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-11-12 22:57:32 +00:00
|
|
|
* @param ACore $activity
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
2018-12-17 09:12:27 +00:00
|
|
|
private function saveObject(ACore $activity) {
|
|
|
|
try {
|
|
|
|
if ($activity->gotObject()) {
|
|
|
|
$this->saveObject($activity->getObject());
|
|
|
|
}
|
2018-09-28 11:41:24 +00:00
|
|
|
|
2018-12-17 09:12:27 +00:00
|
|
|
$service = AP::$activityPub->getInterfaceForItem($activity);
|
|
|
|
$service->save($activity);
|
|
|
|
} catch (UnknownItemException $e) {
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 22:57:32 +00:00
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
2018-11-12 22:57:32 +00:00
|
|
|
|