pixelfed/app/Services/NodeinfoService.php

83 lines
2 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Services;
use Illuminate\Http\Client\ConnectionException;
2024-09-05 06:40:57 +00:00
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;
class NodeinfoService
{
public static function get($domain)
{
2024-09-05 06:40:57 +00:00
$version = config('pixelfed.version');
$appUrl = config('app.url');
$headers = [
'Accept' => 'application/json',
'User-Agent' => "(Pixelfed/{$version}; +{$appUrl})",
];
2024-09-05 06:40:57 +00:00
$url = 'https://'.$domain;
$wk = $url.'/.well-known/nodeinfo';
try {
$res = Http::withOptions([
'allow_redirects' => false,
])
2024-09-05 06:40:57 +00:00
->withHeaders($headers)
->timeout(5)
->get($wk);
} catch (RequestException $e) {
return false;
} catch (ConnectionException $e) {
return false;
} catch (\Exception $e) {
return false;
}
2024-09-05 06:40:57 +00:00
if (! $res) {
return false;
}
$json = $res->json();
2024-09-05 06:40:57 +00:00
if (! isset($json['links'])) {
return false;
}
2024-09-05 06:40:57 +00:00
if (is_array($json['links'])) {
if (isset($json['links']['href'])) {
$href = $json['links']['href'];
} else {
$href = $json['links'][0]['href'];
}
} else {
return false;
}
$domain = parse_url($url, PHP_URL_HOST);
$hrefDomain = parse_url($href, PHP_URL_HOST);
2024-09-05 06:40:57 +00:00
if ($domain !== $hrefDomain) {
2024-09-05 06:37:54 +00:00
return false;
}
try {
$res = Http::withOptions([
'allow_redirects' => false,
])
2024-09-05 06:40:57 +00:00
->withHeaders($headers)
->timeout(5)
->get($href);
} catch (RequestException $e) {
return false;
} catch (ConnectionException $e) {
return false;
} catch (\Exception $e) {
return false;
}
2024-09-05 06:40:57 +00:00
return $res->json();
}
}