streams/Zotlabs/Module/Webfinger.php

201 lines
5 KiB
PHP
Raw Normal View History

2016-04-19 03:38:38 +00:00
<?php
namespace Zotlabs\Module;
class Webfinger extends \Zotlabs\Web\Controller {
2018-05-18 10:09:38 +00:00
function init() {
// This is a public resource with relaxed CORS policy. Close the current login session.
session_write_close();
2018-05-23 06:20:29 +00:00
header('Access-Control-Allow-Origin: *');
2018-05-18 10:09:38 +00:00
$result = [];
2018-05-23 06:20:29 +00:00
if(! is_https_request()) {
header($_SERVER['SERVER_PROTOCOL'] . ' ' . 500 . ' ' . 'Webfinger requires HTTPS');
2018-05-18 10:09:38 +00:00
killme();
}
$resource = $_REQUEST['resource'];
2018-05-23 06:20:29 +00:00
if(! $resource) {
http_status_exit(404,'Not found');
2018-05-18 10:09:38 +00:00
}
2018-05-23 06:20:29 +00:00
logger('webfinger: ' . $resource,LOGGER_DEBUG);
2018-05-18 10:09:38 +00:00
2018-05-23 06:20:29 +00:00
// Response for a site resource
2018-05-18 10:09:38 +00:00
2018-05-23 06:20:29 +00:00
if(strcasecmp(rtrim($resource,'/'),z_root()) === 0) {
2018-05-18 10:09:38 +00:00
$result['subject'] = $resource;
$result['properties'] = [
'https://w3id.org/security/v1#publicKeyPem' => get_config('system','pubkey')
];
$result['links'] = [
[
'rel' => 'http://purl.org/openwebauth/v1',
'type' => 'application/x-zot+json',
'href' => z_root() . '/owa',
],
[
'rel' => 'http://purl.org/zot/protocol/6.0',
'type' => 'application/x-zot+json',
'href' => z_root(),
],
];
2018-05-23 06:20:29 +00:00
}
else {
// some other resource
if(strpos($resource,'tag:' === 0)) {
$arr = explode(':',$resource);
if(count($arr) > 3 && $arr[2] === 'zotid') {
$guid = $arr[3];
$r = q("select * from channel left join xchan on channel_hash = xchan_hash
where channel_hash = '%s' limit 1",
dbesc($guid)
);
if($r) {
$channel_target = $r[0];
}
}
}
if(strpos($resource,'acct:') === 0) {
$channel_nickname = punify(str_replace('acct:','',$resource));
if(strpos($channel_nickname,'@') !== false) {
$host = punify(substr($channel_nickname,strpos($channel_nickname,'@')+1));
// If the webfinger address points off site, redirect to the correct site
if(strcasecmp($host,\App::get_hostname())) {
goaway('https://' . $host . '/.well-known/webfinger?f=&resource=' . $resource);
}
$channel_nickname = substr($channel_nickname,0,strpos($channel_nickname,'@'));
}
}
if(strpos($resource,'http') === 0) {
$channel_nickname = str_replace('~','',basename($resource));
}
if($channel_nickname) {
$r = q("select * from channel left join xchan on channel_hash = xchan_hash
where channel_address = '%s' limit 1",
dbesc($channel_nickname)
);
if($r) {
$channel_target = $r[0];
}
}
2018-05-18 10:09:38 +00:00
}
2018-05-23 06:20:29 +00:00
if($channel_target) {
2016-04-19 03:38:38 +00:00
2018-05-18 10:09:38 +00:00
$h = q("select hubloc_addr from hubloc where hubloc_hash = '%s' and hubloc_deleted = 0",
dbesc($channel_target['channel_hash'])
);
2016-04-19 03:38:38 +00:00
2018-05-18 10:09:38 +00:00
$result['subject'] = $resource;
2016-04-19 03:38:38 +00:00
2018-05-18 10:09:38 +00:00
$aliases = array(
z_root() . '/channel/' . $channel_target['channel_address'],
2018-05-18 10:09:38 +00:00
z_root() . '/~' . $channel_target['channel_address'],
z_root() . '/@' . $channel_target['channel_address']
2018-05-18 10:09:38 +00:00
);
if($h) {
foreach($h as $hh) {
$aliases[] = 'acct:' . $hh['hubloc_addr'];
}
2016-04-19 03:38:38 +00:00
}
2018-05-18 10:09:38 +00:00
$result['aliases'] = [];
$result['properties'] = [
'http://webfinger.net/ns/name' => $channel_target['channel_name'],
'http://xmlns.com/foaf/0.1/name' => $channel_target['channel_name'],
'https://w3id.org/security/v1#publicKeyPem' => $channel_target['xchan_pubkey'],
2018-08-23 06:04:37 +00:00
'http://purl.org/zot/federation' => 'zot6,activitypub'
2018-05-18 10:09:38 +00:00
];
foreach($aliases as $alias) {
if($alias != $resource) {
2018-05-18 10:09:38 +00:00
$result['aliases'][] = $alias;
}
}
2018-05-18 10:09:38 +00:00
$result['links'] = [
[
'rel' => 'http://webfinger.net/rel/avatar',
'type' => $channel_target['xchan_photo_mimetype'],
'href' => $channel_target['xchan_photo_l']
],
[
'rel' => 'http://webfinger.net/rel/blog',
'href' => z_root() . '/channel/' . $channel_target['channel_address'],
],
2018-08-14 03:36:40 +00:00
[
'rel' => 'http://openid.net/specs/connect/1.0/issuer',
'href' => z_root()
],
2018-05-18 10:09:38 +00:00
[
'rel' => 'http://purl.org/zot/protocol/6.0',
'type' => 'application/x-zot+json',
'href' => z_root() . '/channel/' . $channel_target['channel_address'],
],
[
'rel' => 'http://purl.org/openwebauth/v1',
'type' => 'application/x-zot+json',
2018-08-06 06:15:15 +00:00
'href' => z_root() . '/owa'
],
[
'rel' => 'http://ostatus.org/schema/1.0/subscribe',
'template' => z_root() . '/follow?url={uri}'
2018-05-18 10:09:38 +00:00
],
2018-10-04 02:10:52 +00:00
];
}
if(! defined('NOMADIC')) {
$result['links'][] =
2018-08-23 06:04:37 +00:00
[
'rel' => 'self',
'type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
'href' => z_root() . '/channel/' . $channel_target['channel_address']
2018-10-04 02:10:52 +00:00
];
$result['links'][] =
2018-08-23 06:04:37 +00:00
[
'rel' => 'self',
'type' => 'application/activity+json',
'href' => z_root() . '/channel/' . $channel_target['channel_address']
2018-10-04 02:10:52 +00:00
];
2016-04-19 03:38:38 +00:00
}
2018-05-18 10:09:38 +00:00
if(! $result) {
2018-05-23 06:20:29 +00:00
header($_SERVER['SERVER_PROTOCOL'] . ' ' . 400 . ' ' . 'Bad Request');
2018-05-18 10:09:38 +00:00
killme();
}
$arr = [ 'channel' => $channel_target, 'request' => $_REQUEST, 'result' => $result ];
call_hooks('webfinger',$arr);
json_return_and_die($arr['result'],'application/jrd+json');
2016-04-19 03:38:38 +00:00
}
}