gnu-social/plugins/ActivityPub/Util/Explorer.php

307 lines
11 KiB
PHP
Raw Normal View History

2021-10-27 04:14:01 +01:00
<?php
2021-12-26 09:48:16 +00:00
declare(strict_types = 1);
2021-10-27 04:14:01 +01:00
// {{{ License
2021-10-27 04:14:01 +01:00
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social 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.
//
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
/**
* ActivityPub implementation for GNU social
*
* @package GNUsocial
* @category ActivityPub
2021-12-26 09:48:16 +00:00
*
* @author Diogo Peralta Cordeiro <@diogo.site>
* @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
2021-10-27 04:14:01 +01:00
namespace Plugin\ActivityPub\Util;
use App\Core\DB;
2021-10-27 04:14:01 +01:00
use App\Core\HTTPClient;
use App\Core\Log;
use App\Entity\Actor;
use App\Entity\LocalUser;
use App\Util\Common;
2021-10-27 04:14:01 +01:00
use App\Util\Exception\NoSuchActorException;
use App\Util\Nickname;
2021-10-27 04:14:01 +01:00
use Exception;
use InvalidArgumentException;
2021-12-26 09:48:16 +00:00
use const JSON_UNESCAPED_SLASHES;
2021-10-27 04:14:01 +01:00
use Plugin\ActivityPub\ActivityPub;
use Plugin\ActivityPub\Entity\ActivitypubActor;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
/**
* ActivityPub's own Explorer
*
* Allows to discovery new remote actors
*
* @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
2021-10-27 04:14:01 +01:00
*/
class Explorer
{
private array $discovered_actors = [];
2021-10-27 04:14:01 +01:00
/**
* Shortcut function to get a single profile from its URL.
*
* @param bool $try_online whether to try online grabbing, defaults to true
2021-10-27 04:14:01 +01:00
*
* @throws ClientExceptionInterface
* @throws NoSuchActorException
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public static function getOneFromUri(string $uri, bool $try_online = true): Actor
2021-10-27 04:14:01 +01:00
{
$actors = (new self())->lookup($uri, $try_online);
switch (\count($actors)) {
case 1:
return $actors[0];
case 0:
throw new NoSuchActorException('Invalid Actor.');
default:
throw new InvalidArgumentException('More than one actor found for this URI.');
2021-10-27 04:14:01 +01:00
}
}
/**
* Get every profile from the given URL
* This function cleans the $this->discovered_actor_profiles array
* so that there is no erroneous data
*
* @param string $uri User's url
* @param bool $try_online whether to try online grabbing, defaults to true
2021-10-27 04:14:01 +01:00
*
* @throws ClientExceptionInterface
* @throws NoSuchActorException
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*
* @return array of Actor objects
2021-10-27 04:14:01 +01:00
*/
public function lookup(string $uri, bool $try_online = true): array
2021-10-27 04:14:01 +01:00
{
if (\in_array($uri, ActivityPub::PUBLIC_TO)) {
2021-10-27 04:14:01 +01:00
return [];
}
Log::debug('ActivityPub Explorer: Started now looking for ' . $uri);
$this->discovered_actors = [];
2021-10-27 04:14:01 +01:00
return $this->_lookup($uri, $try_online);
2021-10-27 04:14:01 +01:00
}
/**
* Get every profile from the given URL
* This is a recursive function that will accumulate the results on
* $discovered_actor_profiles array
*
* @param string $uri User's url
* @param bool $try_online whether to try online grabbing, defaults to true
2021-10-27 04:14:01 +01:00
*
* @throws ClientExceptionInterface
* @throws NoSuchActorException
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*
* @return array of Actor objects
2021-10-27 04:14:01 +01:00
*/
private function _lookup(string $uri, bool $try_online = true): array
2021-10-27 04:14:01 +01:00
{
$grab_known = $this->grabKnownActor($uri);
2021-10-27 04:14:01 +01:00
// First check if we already have it locally and, if so, return it.
// If the known fetch fails and remote grab is required: store locally and return.
if (!$grab_known && (!$try_online || !$this->grabRemoteActor($uri))) {
2021-10-27 04:14:01 +01:00
throw new NoSuchActorException('Actor not found.');
}
return $this->discovered_actors;
2021-10-27 04:14:01 +01:00
}
/**
* Get a known user profile from its URL and joins it on
* $this->discovered_actor_profiles
*
* @param string $uri Actor's uri
*
* @throws Exception
* @throws NoSuchActorException
*
* @return bool success state
*/
private function grabKnownActor(string $uri): bool
2021-10-27 04:14:01 +01:00
{
Log::debug('ActivityPub Explorer: Searching locally for ' . $uri . ' offline.');
// Try local
if (Common::isValidHttpUrl($uri)) {
// This means $uri is a valid url
$resource_parts = parse_url($uri);
// TODO: Use URLMatcher
if ($resource_parts['host'] === Common::config('site', 'server')) {
$str = $resource_parts['path'];
// actor_view_nickname
$renick = '/\/@(' . Nickname::DISPLAY_FMT . ')\/?/m';
// actor_view_id
$reuri = '/\/actor\/(\d+)\/?/m';
if (preg_match_all($renick, $str, $matches, \PREG_SET_ORDER, 0) === 1) {
$this->discovered_actors[] = DB::findOneBy(
LocalUser::class,
['nickname' => $matches[0][1]],
)->getActor();
return true;
} elseif (preg_match_all($reuri, $str, $matches, \PREG_SET_ORDER, 0) === 1) {
$this->discovered_actors[] = Actor::getById((int) $matches[0][1]);
return true;
}
}
}
2021-10-27 04:14:01 +01:00
// Try standard ActivityPub route
// Is this a known filthy little mudblood?
$aprofile = DB::findOneBy(ActivitypubActor::class, ['uri' => $uri], return_null: true);
if (!\is_null($aprofile)) {
Log::debug('ActivityPub Explorer: Found a known ActivityPub Actor for ' . $uri);
$this->discovered_actors[] = $aprofile->getActor();
2021-10-27 04:14:01 +01:00
return true;
} else {
Log::debug('ActivityPub Explorer: Unable to find a known ActivityPub Actor for ' . $uri);
2021-10-27 04:14:01 +01:00
}
return false;
}
/**
* Get a remote user(s) profile(s) from its URL and joins it on
* $this->discovered_actor_profiles
*
* @param string $uri User's url
2021-10-27 04:14:01 +01:00
*
* @throws ClientExceptionInterface
* @throws NoSuchActorException
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*
* @return bool success state
*/
private function grabRemoteActor(string $uri): bool
2021-10-27 04:14:01 +01:00
{
Log::debug('ActivityPub Explorer: Trying to grab a remote actor for ' . $uri);
$response = HTTPClient::get($uri, ['headers' => ACTIVITYPUB::HTTP_CLIENT_HEADERS]);
2021-12-26 09:48:16 +00:00
$res = json_decode($response->getContent(), true);
2021-10-27 04:14:01 +01:00
if ($response->getStatusCode() == 410) { // If it was deleted
return true; // Nothing to add.
} elseif (!HTTPClient::statusCodeIsOkay($response)) { // If it is unavailable
return false; // Try to add at another time.
}
2021-12-26 09:48:16 +00:00
if (\is_null($res)) {
Log::debug('ActivityPub Explorer: Invalid response returned from given Actor URL: ' . $res);
2021-10-27 04:14:01 +01:00
return true; // Nothing to add.
}
if ($res['type'] === 'OrderedCollection') { // It's a potential collection of actors!!!
Log::debug('ActivityPub Explorer: Found a collection of actors for ' . $uri);
$this->travelCollection($res['first']);
2021-10-27 04:14:01 +01:00
return true;
} else {
try {
2022-02-26 14:45:38 +00:00
$this->discovered_actors[] = DB::wrapInTransaction(fn () => Model\Actor::fromJson(json_encode($res)))->getActor();
return true;
} catch (Exception $e) {
Log::debug(
'ActivityPub Explorer: Invalid potential remote actor while grabbing remotely: ' . $uri
. '. He returned the following: ' . json_encode($res, JSON_UNESCAPED_SLASHES)
2021-12-26 09:48:16 +00:00
. ' and the following exception: ' . $e->getMessage(),
);
return false;
}
2021-10-27 04:14:01 +01:00
}
return false;
}
/**
* Allows the Explorer to transverse a collection of persons.
*
* @throws ClientExceptionInterface
2021-10-27 04:14:01 +01:00
* @throws NoSuchActorException
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
2021-10-27 04:14:01 +01:00
*/
private function travelCollection(string $uri): bool
2021-10-27 04:14:01 +01:00
{
$response = HTTPClient::get($uri, ['headers' => ACTIVITYPUB::HTTP_CLIENT_HEADERS]);
2021-12-26 09:48:16 +00:00
$res = json_decode($response->getContent(), true);
2021-10-27 04:14:01 +01:00
if (!isset($res['orderedItems'])) {
return false;
}
// Accumulate findings
foreach ($res['orderedItems'] as $actor_uri) {
$this->_lookup($actor_uri);
2021-10-27 04:14:01 +01:00
}
2021-10-27 04:14:01 +01:00
// Go through entire collection
2021-12-26 09:48:16 +00:00
if (!\is_null($res['next'])) {
$this->travelCollection($res['next']);
2021-10-27 04:14:01 +01:00
}
return true;
}
/**
* Get a remote user array from its URL (this function is only used for
* profile updating and shall not be used for anything else)
*
* @param string $uri User's url
2021-10-27 04:14:01 +01:00
*
* @throws ClientExceptionInterface
2021-12-26 09:48:16 +00:00
* @throws Exception
2021-10-27 04:14:01 +01:00
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*
2021-12-26 09:48:16 +00:00
* @return null|string If it is able to fetch, false if it's gone
2021-10-27 04:14:01 +01:00
* // Exceptions when network issues or unsupported Activity format
*/
public static function getRemoteActorActivity(string $uri): string|null
2021-10-27 04:14:01 +01:00
{
$response = HTTPClient::get($uri, ['headers' => ACTIVITYPUB::HTTP_CLIENT_HEADERS]);
2021-10-27 04:14:01 +01:00
// If it was deleted
if ($response->getStatusCode() == 410) {
return null;
2021-10-27 04:14:01 +01:00
} elseif (!HTTPClient::statusCodeIsOkay($response)) { // If it is unavailable
throw new Exception('Non Ok Status Code for given Actor URL.');
}
return $response->getContent();
2021-10-27 04:14:01 +01:00
}
}