2019-05-01 18:24:09 +02:00
|
|
|
<?php
|
2024-08-24 15:27:00 +02:00
|
|
|
|
|
|
|
// Copyright (C) 2010-2024, the Friendica project
|
|
|
|
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
2019-05-01 18:24:09 +02:00
|
|
|
|
|
|
|
namespace Friendica\Module;
|
|
|
|
|
|
|
|
use Friendica\BaseModule;
|
2019-12-15 22:34:11 +01:00
|
|
|
use Friendica\DI;
|
2019-06-19 17:05:29 +00:00
|
|
|
use Friendica\Model\Photo;
|
2019-10-24 09:06:22 +02:00
|
|
|
use Friendica\Model\User;
|
2024-07-08 19:23:20 +02:00
|
|
|
use Friendica\Network\HTTPException\BadRequestException;
|
2022-08-13 22:31:38 +02:00
|
|
|
use Friendica\Network\HTTPException\NotFoundException;
|
2019-10-25 00:34:46 +02:00
|
|
|
use Friendica\Protocol\ActivityNamespace;
|
2024-09-08 16:19:01 +00:00
|
|
|
use Friendica\Util\Network;
|
2022-09-11 07:00:16 +00:00
|
|
|
use Friendica\Util\XML;
|
2019-05-01 18:24:09 +02:00
|
|
|
|
2019-05-01 19:17:52 +02:00
|
|
|
/**
|
|
|
|
* Prints responses to /.well-known/webfinger or /xrd requests
|
|
|
|
*/
|
2019-05-01 18:24:09 +02:00
|
|
|
class Xrd extends BaseModule
|
|
|
|
{
|
2021-11-20 15:38:03 +01:00
|
|
|
protected function rawContent(array $request = [])
|
2019-05-01 18:24:09 +02:00
|
|
|
{
|
2023-07-08 21:01:48 -04:00
|
|
|
header('Vary: Accept', false);
|
|
|
|
|
2019-05-01 19:17:52 +02:00
|
|
|
// @TODO: Replace with parameter from router
|
2021-07-25 15:23:37 +00:00
|
|
|
if (DI::args()->getArgv()[0] == 'xrd') {
|
2019-05-01 19:17:52 +02:00
|
|
|
if (empty($_GET['uri'])) {
|
2024-07-11 18:18:54 +02:00
|
|
|
throw new BadRequestException();
|
2019-05-01 19:17:52 +02:00
|
|
|
}
|
|
|
|
|
2021-11-05 19:59:18 +00:00
|
|
|
$uri = urldecode(trim($_GET['uri']));
|
2024-09-06 18:05:36 +00:00
|
|
|
$mode = self::getAcceptedContentType($_SERVER['HTTP_ACCEPT'] ?? '', Response::TYPE_JSON);
|
2019-05-01 19:17:52 +02:00
|
|
|
} else {
|
|
|
|
if (empty($_GET['resource'])) {
|
2024-07-11 18:18:54 +02:00
|
|
|
throw new BadRequestException();
|
2019-05-01 19:17:52 +02:00
|
|
|
}
|
|
|
|
|
2021-11-05 19:59:18 +00:00
|
|
|
$uri = urldecode(trim($_GET['resource']));
|
2024-09-06 18:05:36 +00:00
|
|
|
$mode = self::getAcceptedContentType($_SERVER['HTTP_ACCEPT'] ?? '', Response::TYPE_XML);
|
2019-05-01 19:17:52 +02:00
|
|
|
}
|
|
|
|
|
2024-09-08 16:19:01 +00:00
|
|
|
if (Network::isValidHttpUrl($uri)) {
|
2019-05-01 19:17:52 +02:00
|
|
|
$name = ltrim(basename($uri), '~');
|
2023-05-06 17:57:12 +02:00
|
|
|
$host = parse_url($uri, PHP_URL_HOST);
|
2024-07-08 20:04:34 +02:00
|
|
|
} else if (preg_match('/^[[:alpha:]][[:alnum:]+-.]+:/', $uri)) {
|
2019-05-01 19:17:52 +02:00
|
|
|
$local = str_replace('acct:', '', $uri);
|
|
|
|
if (substr($local, 0, 2) == '//') {
|
|
|
|
$local = substr($local, 2);
|
|
|
|
}
|
|
|
|
|
2023-05-06 17:57:12 +02:00
|
|
|
list($name, $host) = explode('@', $local);
|
2024-07-08 19:23:20 +02:00
|
|
|
} else {
|
|
|
|
throw new BadRequestException();
|
2023-05-06 17:57:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($host) && $host !== DI::baseUrl()->getHost()) {
|
2024-09-06 18:05:36 +00:00
|
|
|
DI::logger()->notice('Invalid host name for xrd query', ['host' => $host, 'uri' => $uri]);
|
2023-05-06 17:57:12 +02:00
|
|
|
throw new NotFoundException('Invalid host name for xrd query: ' . $host);
|
2019-05-01 19:17:52 +02:00
|
|
|
}
|
|
|
|
|
2023-06-25 20:54:04 +02:00
|
|
|
header('Vary: Accept', false);
|
|
|
|
|
2020-08-22 18:52:37 +00:00
|
|
|
if ($name == User::getActorName()) {
|
2020-08-22 14:48:09 +00:00
|
|
|
$owner = User::getSystemAccount();
|
|
|
|
if (empty($owner)) {
|
2022-08-13 22:31:38 +02:00
|
|
|
throw new NotFoundException('System account was not found. Please setup your Friendica installation properly.');
|
2020-08-22 14:48:09 +00:00
|
|
|
}
|
2022-08-13 22:31:38 +02:00
|
|
|
$this->printSystemJSON($owner);
|
2020-08-22 14:48:09 +00:00
|
|
|
} else {
|
2022-09-11 07:00:16 +00:00
|
|
|
$owner = User::getOwnerDataByNick($name);
|
2020-08-22 14:48:09 +00:00
|
|
|
if (empty($owner)) {
|
2022-09-16 05:00:06 +00:00
|
|
|
DI::logger()->notice('No owner data for user id', ['uri' => $uri, 'name' => $name]);
|
2022-09-11 07:00:16 +00:00
|
|
|
throw new NotFoundException('Owner was not found for user->uid=' . $name);
|
2020-08-22 14:48:09 +00:00
|
|
|
}
|
2019-05-01 19:17:52 +02:00
|
|
|
|
2020-08-22 14:48:09 +00:00
|
|
|
$alias = str_replace('/profile/', '/~', $owner['url']);
|
2019-05-01 19:17:52 +02:00
|
|
|
|
2020-08-22 14:48:09 +00:00
|
|
|
$avatar = Photo::selectFirst(['type'], ['uid' => $owner['uid'], 'profile' => true]);
|
2020-07-08 09:14:34 -04:00
|
|
|
}
|
|
|
|
|
2020-08-22 14:48:09 +00:00
|
|
|
if (empty($avatar)) {
|
2019-06-19 17:05:29 +00:00
|
|
|
$avatar = ['type' => 'image/jpeg'];
|
2019-05-01 19:17:52 +02:00
|
|
|
}
|
|
|
|
|
2022-04-18 05:42:05 +00:00
|
|
|
if ($mode == Response::TYPE_XML) {
|
2022-09-11 07:00:16 +00:00
|
|
|
$this->printXML($alias, $owner, $avatar);
|
2019-05-01 19:17:52 +02:00
|
|
|
} else {
|
2022-08-13 22:31:38 +02:00
|
|
|
$this->printJSON($alias, $owner, $avatar);
|
2019-05-01 18:24:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 18:05:36 +00:00
|
|
|
/**
|
|
|
|
* Detect the accepted content type.
|
|
|
|
* @todo Handle priorities (see "application/xrd+xml,text/xml;q=0.9")
|
|
|
|
*
|
|
|
|
* @param string $accept
|
|
|
|
* @param string $default
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getAcceptedContentType(string $accept, string $default): string
|
|
|
|
{
|
|
|
|
$parts = [];
|
|
|
|
foreach (explode(',', $accept) as $part) {
|
|
|
|
$parts[] = current(explode(';', $part));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($parts)) {
|
|
|
|
return $default;
|
|
|
|
} elseif (in_array('application/jrd+json', $parts) && !in_array('application/xrd+xml', $parts)) {
|
|
|
|
return Response::TYPE_JSON;
|
|
|
|
} elseif (!in_array('application/jrd+json', $parts) && in_array('application/xrd+xml', $parts)) {
|
|
|
|
return Response::TYPE_XML;
|
|
|
|
} elseif (in_array('application/json', $parts) && !in_array('text/xml', $parts)) {
|
|
|
|
return Response::TYPE_JSON;
|
|
|
|
} elseif (!in_array('application/json', $parts) && in_array('text/xml', $parts)) {
|
|
|
|
return Response::TYPE_XML;
|
|
|
|
} else {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-13 22:31:38 +02:00
|
|
|
private function printSystemJSON(array $owner)
|
2020-08-22 14:48:09 +00:00
|
|
|
{
|
2023-04-15 14:17:30 +00:00
|
|
|
$baseURL = (string)$this->baseUrl;
|
2020-08-22 14:48:09 +00:00
|
|
|
$json = [
|
|
|
|
'subject' => 'acct:' . $owner['addr'],
|
|
|
|
'aliases' => [$owner['url']],
|
|
|
|
'links' => [
|
2024-08-24 04:27:00 +00:00
|
|
|
[
|
|
|
|
'rel' => ActivityNamespace::FEED,
|
|
|
|
'type' => 'application/atom+xml',
|
|
|
|
'href' => $owner['poll'] ?? $baseURL,
|
|
|
|
],
|
2020-08-22 14:48:09 +00:00
|
|
|
[
|
2024-05-20 19:36:40 +00:00
|
|
|
'rel' => ActivityNamespace::WEBFINGERPROFILE,
|
2020-08-22 14:48:09 +00:00
|
|
|
'type' => 'text/html',
|
|
|
|
'href' => $owner['url'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'rel' => 'self',
|
|
|
|
'type' => 'application/activity+json',
|
|
|
|
'href' => $owner['url'],
|
|
|
|
],
|
2021-08-05 08:30:44 +00:00
|
|
|
[
|
2024-05-20 19:36:40 +00:00
|
|
|
'rel' => ActivityNamespace::HCARD,
|
2021-08-05 08:30:44 +00:00
|
|
|
'type' => 'text/html',
|
2022-08-13 06:50:50 +02:00
|
|
|
'href' => $baseURL . '/hcard/' . $owner['nickname'],
|
2021-08-05 08:30:44 +00:00
|
|
|
],
|
|
|
|
[
|
2024-05-20 19:36:40 +00:00
|
|
|
'rel' => ActivityNamespace::DIASPORA_SEED,
|
2021-08-05 08:30:44 +00:00
|
|
|
'type' => 'text/html',
|
2022-08-13 06:50:50 +02:00
|
|
|
'href' => $baseURL,
|
2021-08-05 08:30:44 +00:00
|
|
|
],
|
2024-08-24 04:27:00 +00:00
|
|
|
[
|
|
|
|
'rel' => 'salmon',
|
|
|
|
'href' => $baseURL . '/receive/users/' . $owner['guid'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'rel' => ActivityNamespace::OSTATUSSUB,
|
|
|
|
'template' => $baseURL . '/contact/follow?url={uri}',
|
|
|
|
],
|
2020-08-22 14:48:09 +00:00
|
|
|
]
|
|
|
|
];
|
|
|
|
header('Access-Control-Allow-Origin: *');
|
2023-09-21 12:16:17 -04:00
|
|
|
$this->jsonExit($json, 'application/jrd+json; charset=utf-8');
|
2020-08-22 14:48:09 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 22:31:38 +02:00
|
|
|
private function printJSON(string $alias, array $owner, array $avatar)
|
2019-05-01 18:24:09 +02:00
|
|
|
{
|
2023-04-15 14:17:30 +00:00
|
|
|
$baseURL = (string)$this->baseUrl;
|
2019-05-01 18:24:09 +02:00
|
|
|
|
2019-05-01 19:17:52 +02:00
|
|
|
$json = [
|
2019-06-19 17:05:29 +00:00
|
|
|
'subject' => 'acct:' . $owner['addr'],
|
2019-05-01 19:17:52 +02:00
|
|
|
'aliases' => [
|
|
|
|
$alias,
|
2019-06-19 17:05:29 +00:00
|
|
|
$owner['url'],
|
2019-05-01 19:17:52 +02:00
|
|
|
],
|
|
|
|
'links' => [
|
|
|
|
[
|
2024-05-20 19:36:40 +00:00
|
|
|
'rel' => ActivityNamespace::DFRN,
|
2019-06-19 17:05:29 +00:00
|
|
|
'href' => $owner['url'],
|
2019-05-01 19:17:52 +02:00
|
|
|
],
|
|
|
|
[
|
2019-10-25 00:32:35 +02:00
|
|
|
'rel' => ActivityNamespace::FEED,
|
2019-05-01 19:17:52 +02:00
|
|
|
'type' => 'application/atom+xml',
|
2019-06-19 18:32:38 +00:00
|
|
|
'href' => $owner['poll'],
|
2019-05-01 19:17:52 +02:00
|
|
|
],
|
|
|
|
[
|
2024-05-20 19:36:40 +00:00
|
|
|
'rel' => ActivityNamespace::WEBFINGERPROFILE,
|
2019-05-01 19:17:52 +02:00
|
|
|
'type' => 'text/html',
|
2019-06-19 17:05:29 +00:00
|
|
|
'href' => $owner['url'],
|
2019-05-01 19:17:52 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'rel' => 'self',
|
|
|
|
'type' => 'application/activity+json',
|
2019-06-19 17:05:29 +00:00
|
|
|
'href' => $owner['url'],
|
2019-05-01 19:17:52 +02:00
|
|
|
],
|
|
|
|
[
|
2024-05-20 19:36:40 +00:00
|
|
|
'rel' => ActivityNamespace::HCARD,
|
2019-05-01 19:17:52 +02:00
|
|
|
'type' => 'text/html',
|
2019-06-19 17:05:29 +00:00
|
|
|
'href' => $baseURL . '/hcard/' . $owner['nickname'],
|
2019-05-01 19:17:52 +02:00
|
|
|
],
|
|
|
|
[
|
2024-05-20 19:36:40 +00:00
|
|
|
'rel' => ActivityNamespace::WEBFINGERAVATAR,
|
2019-06-19 17:05:29 +00:00
|
|
|
'type' => $avatar['type'],
|
2021-10-02 17:28:29 -04:00
|
|
|
'href' => User::getAvatarUrl($owner),
|
2019-05-01 19:17:52 +02:00
|
|
|
],
|
|
|
|
[
|
2024-05-20 19:36:40 +00:00
|
|
|
'rel' => ActivityNamespace::DIASPORA_SEED,
|
2019-05-01 19:17:52 +02:00
|
|
|
'type' => 'text/html',
|
|
|
|
'href' => $baseURL,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'rel' => 'salmon',
|
2024-08-24 04:27:00 +00:00
|
|
|
'href' => $baseURL . '/receive/users/' . $owner['guid'],
|
2019-05-01 19:17:52 +02:00
|
|
|
],
|
|
|
|
[
|
2024-05-20 19:36:40 +00:00
|
|
|
'rel' => ActivityNamespace::OSTATUSSUB,
|
2022-10-31 17:58:04 +01:00
|
|
|
'template' => $baseURL . '/contact/follow?url={uri}',
|
2019-05-01 19:17:52 +02:00
|
|
|
],
|
|
|
|
[
|
2024-05-20 19:36:40 +00:00
|
|
|
'rel' => ActivityNamespace::OPENWEBAUTH,
|
2019-05-01 19:17:52 +02:00
|
|
|
'type' => 'application/x-zot+json',
|
|
|
|
'href' => $baseURL . '/owa',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2021-08-09 06:56:41 +00:00
|
|
|
header('Access-Control-Allow-Origin: *');
|
2023-09-21 12:16:17 -04:00
|
|
|
$this->jsonExit($json, 'application/jrd+json; charset=utf-8');
|
2019-05-01 19:17:52 +02:00
|
|
|
}
|
|
|
|
|
2022-09-11 07:00:16 +00:00
|
|
|
private function printXML(string $alias, array $owner, array $avatar)
|
2019-05-01 19:17:52 +02:00
|
|
|
{
|
2023-04-15 14:17:30 +00:00
|
|
|
$baseURL = (string)$this->baseUrl;
|
2019-05-01 19:17:52 +02:00
|
|
|
|
2022-12-31 11:59:19 -05:00
|
|
|
$xmlString = XML::fromArray([
|
2022-09-11 07:00:16 +00:00
|
|
|
'XRD' => [
|
|
|
|
'@attributes' => [
|
|
|
|
'xmlns' => 'http://docs.oasis-open.org/ns/xri/xrd-1.0',
|
|
|
|
],
|
|
|
|
'Subject' => 'acct:' . $owner['addr'],
|
|
|
|
'1:Alias' => $owner['url'],
|
|
|
|
'2:Alias' => $alias,
|
|
|
|
'1:link' => [
|
|
|
|
'@attributes' => [
|
2024-08-24 04:27:00 +00:00
|
|
|
'rel' => ActivityNamespace::DFRN,
|
2022-09-11 07:00:16 +00:00
|
|
|
'href' => $owner['url']
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'2:link' => [
|
|
|
|
'@attributes' => [
|
2024-08-24 04:27:00 +00:00
|
|
|
'rel' => ActivityNamespace::FEED,
|
2022-09-11 07:00:16 +00:00
|
|
|
'type' => 'application/atom+xml',
|
|
|
|
'href' => $owner['poll']
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'3:link' => [
|
|
|
|
'@attributes' => [
|
2024-05-20 19:36:40 +00:00
|
|
|
'rel' => ActivityNamespace::WEBFINGERPROFILE,
|
2022-09-11 07:00:16 +00:00
|
|
|
'type' => 'text/html',
|
|
|
|
'href' => $owner['url']
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'4:link' => [
|
2024-08-17 23:05:45 +02:00
|
|
|
'@attributes' => [
|
|
|
|
'rel' => 'self',
|
|
|
|
'type' => 'application/activity+json',
|
|
|
|
'href' => $owner['url']
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'5:link' => [
|
2022-09-11 07:00:16 +00:00
|
|
|
'@attributes' => [
|
2024-05-20 19:36:40 +00:00
|
|
|
'rel' => ActivityNamespace::HCARD,
|
2022-09-11 07:00:16 +00:00
|
|
|
'type' => 'text/html',
|
|
|
|
'href' => $baseURL . '/hcard/' . $owner['nickname']
|
|
|
|
]
|
|
|
|
],
|
2024-08-17 23:05:45 +02:00
|
|
|
'6:link' => [
|
2022-09-11 07:00:16 +00:00
|
|
|
'@attributes' => [
|
2024-05-20 19:36:40 +00:00
|
|
|
'rel' => ActivityNamespace::WEBFINGERAVATAR,
|
2022-09-11 07:00:16 +00:00
|
|
|
'type' => $avatar['type'],
|
|
|
|
'href' => User::getAvatarUrl($owner)
|
|
|
|
]
|
|
|
|
],
|
2024-08-17 23:05:45 +02:00
|
|
|
'7:link' => [
|
2022-09-11 07:00:16 +00:00
|
|
|
'@attributes' => [
|
2024-05-20 19:36:40 +00:00
|
|
|
'rel' => ActivityNamespace::DIASPORA_SEED,
|
2022-09-11 07:00:16 +00:00
|
|
|
'type' => 'text/html',
|
|
|
|
'href' => $baseURL
|
|
|
|
]
|
|
|
|
],
|
2024-08-17 23:05:45 +02:00
|
|
|
'8:link' => [
|
2022-09-11 07:00:16 +00:00
|
|
|
'@attributes' => [
|
|
|
|
'rel' => 'salmon',
|
2024-08-24 04:27:00 +00:00
|
|
|
'href' => $baseURL . '/receive/users/' . $owner['guid']
|
2022-09-11 07:00:16 +00:00
|
|
|
]
|
|
|
|
],
|
2024-08-17 23:05:45 +02:00
|
|
|
'9:link' => [
|
2022-09-11 07:00:16 +00:00
|
|
|
'@attributes' => [
|
2024-05-20 19:36:40 +00:00
|
|
|
'rel' => ActivityNamespace::OSTATUSSUB,
|
2022-10-31 17:58:04 +01:00
|
|
|
'template' => $baseURL . '/contact/follow?url={uri}'
|
2022-09-11 07:00:16 +00:00
|
|
|
]
|
|
|
|
],
|
2024-08-24 04:27:00 +00:00
|
|
|
'10:link' => [
|
2022-09-11 07:00:16 +00:00
|
|
|
'@attributes' => [
|
2024-05-20 19:36:40 +00:00
|
|
|
'rel' => ActivityNamespace::OPENWEBAUTH,
|
2022-09-11 07:00:16 +00:00
|
|
|
'type' => 'application/x-zot+json',
|
|
|
|
'href' => $baseURL . '/owa'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
],
|
2022-12-31 11:59:19 -05:00
|
|
|
]);
|
2019-05-01 19:17:52 +02:00
|
|
|
|
2022-04-10 08:31:55 +00:00
|
|
|
header('Access-Control-Allow-Origin: *');
|
2023-09-21 12:35:55 -04:00
|
|
|
$this->httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml');
|
2019-05-01 18:24:09 +02:00
|
|
|
}
|
|
|
|
}
|