hostas2/public/api/webfinger-lookup.php

50 lines
1.1 KiB
PHP
Executable File

<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/config.php');
$resource = $_GET['resource'];
$canonical_username = substr($resource, offset: 5); // Drop the 'acct:'
$username_parts = explode(separator: '@', string: $canonical_username, limit: 2);
$local_username = $username_parts[0];
$given_domain = $username_parts[1];
// We're only returning results at our domain
if ($given_domain !== HOSTAS_DOMAIN) {
http_response_code(404);
die();
}
$conn = new SQLite3(HOSTAS_DATABASE_PATH);
$people_query = $conn->prepare("
select * from actor
join object on actor.objectId = object.id
where actor.preferredUsername = :preferredUsername
");
$people_query->bindValue(':preferredUsername', $local_username);
$people_query_results = $people_query->execute();
$person = $people_query_results->fetchArray();
if (!$person) {
http_response_code(404);
die();
}
header('Content-Type: application/jrd+json; charset=utf8');
echo json_encode(array(
'subject' => "acct:$local_username@$given_domain",
'links' => array(
array(
'rel'=> 'self',
'href'=> $person['url'],
'type'=> 'application/activity+json'
)
)
));
$conn->close();