2018-05-18 10:09:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Zotlabs\Lib;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Fetch and return a webfinger for a resource
|
|
|
|
*
|
|
|
|
* @param string $resource - The resource
|
|
|
|
* @return boolean|string false or associative array from result JSON
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Webfinger {
|
|
|
|
|
|
|
|
static private $server = EMPTY_STR;
|
|
|
|
static private $resource = EMPTY_STR;
|
|
|
|
|
|
|
|
static function exec($resource) {
|
|
|
|
|
2019-04-15 06:41:26 +00:00
|
|
|
if (! $resource) {
|
2018-05-18 10:09:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-07-30 03:19:02 +00:00
|
|
|
|
2018-05-18 10:09:38 +00:00
|
|
|
self::parse_resource($resource);
|
|
|
|
|
2019-04-15 06:41:26 +00:00
|
|
|
if (! ( self::$server && self::$resource)) {
|
2018-05-18 10:09:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-15 06:41:26 +00:00
|
|
|
if (! check_siteallowed(self::$server)) {
|
2020-06-14 11:58:27 +00:00
|
|
|
logger('denied: ' . self::$server);
|
2018-05-30 04:08:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-16 00:09:46 +00:00
|
|
|
logger('fetching resource: ' . self::$resource . ' from ' . self::$server, LOGGER_DEBUG, LOG_INFO);
|
2018-05-30 04:08:52 +00:00
|
|
|
|
|
|
|
$url = 'https://' . self::$server . '/.well-known/webfinger?f=&resource=' . self::$resource ;
|
2018-05-18 10:09:38 +00:00
|
|
|
|
|
|
|
$counter = 0;
|
2018-05-30 04:08:52 +00:00
|
|
|
$s = z_fetch_url($url, false, $counter, [ 'headers' => [ 'Accept: application/jrd+json, */*' ] ]);
|
2018-05-18 10:09:38 +00:00
|
|
|
|
2019-04-15 06:41:26 +00:00
|
|
|
if ($s['success']) {
|
2018-05-18 10:09:38 +00:00
|
|
|
$j = json_decode($s['body'], true);
|
|
|
|
return($j);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function parse_resource($resource) {
|
|
|
|
|
|
|
|
self::$resource = urlencode($resource);
|
|
|
|
|
2019-04-15 06:41:26 +00:00
|
|
|
if (strpos($resource,'http') === 0) {
|
2018-05-18 10:09:38 +00:00
|
|
|
$m = parse_url($resource);
|
2019-04-15 06:41:26 +00:00
|
|
|
if ($m) {
|
|
|
|
if ($m['scheme'] !== 'https') {
|
2018-05-18 10:09:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
self::$server = $m['host'] . (($m['port']) ? ':' . $m['port'] : '');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 06:41:26 +00:00
|
|
|
elseif (strpos($resource,'tag:') === 0) {
|
2018-05-23 06:20:29 +00:00
|
|
|
$arr = explode(':',$resource); // split the tag
|
|
|
|
$h = explode(',',$arr[1]); // split the host,date
|
|
|
|
self::$server = $h[0];
|
|
|
|
}
|
2018-05-18 10:09:38 +00:00
|
|
|
else {
|
|
|
|
$x = explode('@',$resource);
|
2019-04-15 06:41:26 +00:00
|
|
|
if (! strlen($x[0])) {
|
2018-12-04 05:45:35 +00:00
|
|
|
// e.g. @dan@pixelfed.org
|
|
|
|
array_shift($x);
|
|
|
|
}
|
2018-05-18 10:09:38 +00:00
|
|
|
$username = $x[0];
|
2019-04-15 06:41:26 +00:00
|
|
|
if (count($x) > 1) {
|
2018-05-18 10:09:38 +00:00
|
|
|
self::$server = $x[1];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-15 06:41:26 +00:00
|
|
|
if (strpos($resource,'acct:') !== 0) {
|
2018-05-18 10:09:38 +00:00
|
|
|
self::$resource = urlencode('acct:' . $resource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 04:08:52 +00:00
|
|
|
/**
|
|
|
|
* @brief fetch a webfinger resource and return a zot6 discovery url if present
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-05-29 02:42:40 +00:00
|
|
|
static function zot_url($resource) {
|
|
|
|
|
|
|
|
$arr = self::exec($resource);
|
2018-05-24 05:50:33 +00:00
|
|
|
|
2019-04-15 06:41:26 +00:00
|
|
|
if (is_array($arr) && array_key_exists('links',$arr)) {
|
|
|
|
foreach ($arr['links'] as $link) {
|
|
|
|
if (array_key_exists('rel',$link) && $link['rel'] === PROTOCOL_ZOT6) {
|
|
|
|
if (array_key_exists('href',$link) && $link['href'] !== EMPTY_STR) {
|
2018-05-24 05:50:33 +00:00
|
|
|
return $link['href'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-18 10:09:38 +00:00
|
|
|
}
|