From 8988ad9f9dd5030c0cdaa826c986b98096205a75 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 7 May 2022 16:04:12 -0400 Subject: [PATCH] Add subscribe URL retrieval to server poll --- src/classes/Pollers/Server.php | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/classes/Pollers/Server.php b/src/classes/Pollers/Server.php index e6c160c..96ef29b 100644 --- a/src/classes/Pollers/Server.php +++ b/src/classes/Pollers/Server.php @@ -147,6 +147,10 @@ class Server $addons = $probe_result['data']['plugins']; } + if ($probe_result['data']['admin']['profile']) { + $subscribe = $this->getSubscribeUrl($probe_result['data']['url'], $probe_result['data']['admin']['profile']); + } + $this->atlas->perform( 'UPDATE `server` SET `available` = 1, @@ -161,6 +165,7 @@ class Server `admin_name` = :admin_name, `admin_profile` = :admin_profile, `noscrape_url` = :noscrape_url, + `subscribe_url` = :subscribe_url, `ssl_state` = :ssl_state WHERE `id` = :server_id', [ @@ -175,6 +180,7 @@ class Server 'admin_name' => $probe_result['data']['admin']['name'], 'admin_profile' => $probe_result['data']['admin']['profile'], 'noscrape_url' => $probe_result['data']['no_scrape_url'] ?? null, + 'subscribe_url' => $subscribe ?? null, 'ssl_state' => $probe_result['ssl_state'] ] ); @@ -452,4 +458,38 @@ class Server } } } + + public function getSubscribeUrl($base_url, $profile) + { + $xrdRequest = new WebRequest($base_url . '/xrd'); + $xrdRequest->addRequestHeader('Accept', 'application/jrd+json'); + $xrdJsonData = $xrdRequest->get(['uri' => $profile]); + + $this->logger->debug('WebRequest: ' . $xrdRequest->getLastFetchedUrl() . ' Status: ' . $xrdRequest->getLastStatus()); + + if ($xrdRequest->getLastStatus() != 200) { + $this->logger->info('Unsuccessful XRD request: ' . $xrdRequest->getLastFetchedUrl()); + return null; + } + + try { + $xrdData = json_decode($xrdJsonData); + } catch (\Throwable $e) { + $this->logger->notice('Invalid JSON string for XRD URL: ' . $xrdRequest->getLastFetchedUrl()); + return null; + } + + if (!isset($xrdData->links)) { + $this->logger->notice('Invalid JSON structure for XRD URL: ' . $xrdRequest->getLastFetchedUrl()); + return null; + } + + foreach ($xrdData->links as $link) { + if ($link->rel == 'http://ostatus.org/schema/1.0/subscribe') { + return $link->template ?? null; + } + } + + return null; + } }