2023-03-08 15:31:49 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Lewisdale\Webmentions;
|
|
|
|
|
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
2023-03-08 16:07:43 +00:00
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
2023-03-08 15:31:49 +00:00
|
|
|
|
|
|
|
class EndpointParser {
|
|
|
|
public static function parse(ResponseInterface $response) : string | null
|
|
|
|
{
|
|
|
|
$headers = $response->getHeaders();
|
|
|
|
$endpoint = null;
|
|
|
|
|
|
|
|
if (isset($headers["link"])) {
|
|
|
|
$link = $headers["link"][0];
|
|
|
|
if (preg_match('/rel=("?)webmention("?)/', $link)) {
|
|
|
|
$matches = [];
|
|
|
|
preg_match('/\<(..*?)\>/', $link, $matches);
|
|
|
|
|
|
|
|
if (count($matches) > 1) {
|
|
|
|
$endpoint = $matches[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 16:07:43 +00:00
|
|
|
if (!$endpoint) {
|
|
|
|
$doc = new Crawler($response->getContent());
|
|
|
|
$webmention = $doc->filter('rel="webmention"')->first();
|
|
|
|
}
|
|
|
|
|
2023-03-08 15:31:49 +00:00
|
|
|
if ($endpoint && !str_contains($endpoint, "https://")) {
|
|
|
|
$res = parse_url($response->getInfo('url'));
|
|
|
|
$endpoint = $res["scheme"] . "://" . $res["host"] . $endpoint;
|
|
|
|
}
|
|
|
|
return $endpoint;
|
|
|
|
}
|
|
|
|
}
|