2021-04-02 17:20:02 +00:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
/**
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2021 Ad Aures
|
2021-04-02 17:20:02 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
2021-08-23 11:05:16 +00:00
|
|
|
namespace Modules\Fediverse;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
use Exception;
|
2023-08-29 12:58:20 +00:00
|
|
|
use Modules\Fediverse\Entities\Actor;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
class WebFinger
|
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-05-19 16:35:13 +00:00
|
|
|
private const RESOURCE_PATTERN = '/^acct:(?P<username>([\w_]+))@(?P<domain>([\w\-\.]+[\w]+)(:[\d]+)?)$/x';
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected string $username;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected string $domain;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected string $host;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected string $port;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
/**
|
2021-05-14 17:59:35 +00:00
|
|
|
* @var string[]
|
2021-04-02 17:20:02 +00:00
|
|
|
*/
|
2021-05-18 17:16:36 +00:00
|
|
|
protected array $aliases = [];
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
/**
|
2021-05-14 17:59:35 +00:00
|
|
|
* @var array<array<string, string>>
|
2021-04-02 17:20:02 +00:00
|
|
|
*/
|
2021-05-18 17:16:36 +00:00
|
|
|
protected array $links = [];
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
public function __construct(
|
|
|
|
protected string $subject
|
|
|
|
) {
|
2021-04-02 17:20:02 +00:00
|
|
|
// Split resource into its parts (username, domain)
|
2021-05-14 17:59:35 +00:00
|
|
|
$parts = $this->splitResource($subject);
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! $parts) {
|
2021-04-02 17:20:02 +00:00
|
|
|
throw new Exception('Wrong WebFinger resource pattern.');
|
|
|
|
}
|
2021-05-12 14:00:25 +00:00
|
|
|
|
|
|
|
$username = $parts['username'];
|
|
|
|
$domain = $parts['domain'];
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
$this->username = $username;
|
|
|
|
$this->domain = $domain;
|
|
|
|
|
|
|
|
$currentUrl = current_url(true);
|
|
|
|
$currentDomain =
|
|
|
|
$currentUrl->getHost() .
|
|
|
|
($currentUrl->getPort() ? ':' . $currentUrl->getPort() : '');
|
|
|
|
if ($currentDomain !== $domain) {
|
|
|
|
// TODO: return error code
|
|
|
|
throw new Exception('Domain does not correspond to Instance.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2023-08-29 12:58:20 +00:00
|
|
|
! ($actor = model('ActorModel', false)->getActorByUsername($username, $domain)) instanceof Actor
|
2021-04-02 17:20:02 +00:00
|
|
|
) {
|
|
|
|
throw new Exception('Could not find actor');
|
|
|
|
}
|
|
|
|
|
2024-04-28 16:39:01 +00:00
|
|
|
$this->aliases = [$actor->uri];
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->links = [
|
|
|
|
[
|
2023-06-12 14:47:38 +00:00
|
|
|
'rel' => 'self',
|
2021-04-02 17:20:02 +00:00
|
|
|
'type' => 'application/activity+json',
|
|
|
|
'href' => $actor->uri,
|
|
|
|
],
|
|
|
|
[
|
2023-06-12 14:47:38 +00:00
|
|
|
'rel' => 'http://webfinger.net/rel/profile-page',
|
2021-04-02 17:20:02 +00:00
|
|
|
'type' => 'text/html',
|
2021-05-19 16:35:13 +00:00
|
|
|
'href' => $actor->uri,
|
|
|
|
# TODO: should there be 2 values? @actorUsername
|
2021-04-02 17:20:02 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* Get WebFinger response as an array
|
|
|
|
*
|
2022-07-03 16:42:20 +00:00
|
|
|
* @return array{subject: string, aliases: string[], links: array<mixed, array<string, string>>}
|
2021-05-06 14:00:48 +00:00
|
|
|
*/
|
|
|
|
public function toArray(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'subject' => $this->subject,
|
|
|
|
'aliases' => $this->aliases,
|
2023-06-12 14:47:38 +00:00
|
|
|
'links' => $this->links,
|
2021-05-06 14:00:48 +00:00
|
|
|
];
|
|
|
|
}
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
/**
|
|
|
|
* Split resource into its parts (username, domain)
|
|
|
|
*
|
2025-01-08 11:11:19 +00:00
|
|
|
* @return array{0:string,username:non-empty-string,1:non-empty-string,2:non-empty-string,domain:non-falsy-string,3:non-falsy-string,4:non-falsy-string,5?:non-falsy-string}
|
2021-04-02 17:20:02 +00:00
|
|
|
*/
|
2022-11-10 16:47:55 +00:00
|
|
|
private function splitResource(string $resource): bool|array
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! preg_match(self::RESOURCE_PATTERN, $resource, $matches)) {
|
2021-04-02 17:20:02 +00:00
|
|
|
// Resource pattern failed
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $matches;
|
|
|
|
}
|
|
|
|
}
|