forked from grumpydevelop/federator
73 lines
1.9 KiB
PHP
73 lines
1.9 KiB
PHP
<?php
|
|
/**
|
|
* SPDX-FileCopyrightText: 2024 Sascha Nitsch (grumpydeveloper) https://contentnation.net/@grumpydevelop
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
*
|
|
* @author Sascha Nitsch (grumpydeveloper)
|
|
**/
|
|
|
|
namespace Federator\Api\WellKnown;
|
|
|
|
class WebFinger
|
|
{
|
|
/**
|
|
* parent instance
|
|
*
|
|
* @var \Federator\Api\WellKnown $wellKnown
|
|
*/
|
|
private $wellKnown;
|
|
|
|
/**
|
|
* main instance
|
|
*
|
|
* @var \Federator\Main $main
|
|
*/
|
|
private $main;
|
|
|
|
/**
|
|
* constructor
|
|
*
|
|
* @param \Federator\Api\WellKnown $wellKnown parent instance
|
|
* @param \Federator\Main $main main instance
|
|
* @return void
|
|
*/
|
|
public function __construct($wellKnown, $main)
|
|
{
|
|
$this->wellKnown = $wellKnown;
|
|
$this->main = $main;
|
|
}
|
|
|
|
/**
|
|
* handle webfinger request
|
|
*
|
|
* @return bool true on success
|
|
*/
|
|
public function exec()
|
|
{
|
|
$_resource = $this->main->extractFromURI('resource');
|
|
$matches = [];
|
|
$config = $this->main->getConfig();
|
|
$domain = $config['generic']['externaldomain'];
|
|
if (preg_match("/^acct:([^@]+)@(.*)$/", $_resource, $matches) != 1 || $matches[2] !== $domain) {
|
|
throw new \Federator\Exceptions\InvalidArgument();
|
|
}
|
|
$domain = $matches[2];
|
|
$user = \Federator\DIO\User::getUserByName(
|
|
$this->main->getDatabase(),
|
|
$matches[1],
|
|
$this->main->getConnector(),
|
|
$this->main->getCache()
|
|
);
|
|
if ($user->id == 0) {
|
|
echo "not found";
|
|
throw new \Federator\Exceptions\FileNotFound();
|
|
}
|
|
$data = [
|
|
'username' => $user->id,
|
|
'domain' => $domain,
|
|
];
|
|
$response = $this->main->renderTemplate('webfinger_acct.json', $data);
|
|
$this->wellKnown->setResponse($response);
|
|
return true;
|
|
}
|
|
}
|