jonnybarnes.uk/app/Services/WebMention/Parser.php
Jonny Barnes df9b0dc490
Move webmention parsing and authorship into the app
The jonnybarnes/webmentions-parser package is now archived, and its
guzzlehttp/guzzle ^6|^7 constraint was the only thing holding Guzzle
back from 8. Port the parts we use into App\Services\WebMention and
drop the package.

Parser keeps getMentionType and the checkInReplyTo/LikeOf/RepostOf
checks unchanged. Authorship follows the same authorship algorithm,
with some fixes:

- fetch the author page with Laravel's Http client, so tests can fake
  it, and treat connection failures as AuthorshipParserException too
- keep state in locals so one instance can be reused safely
- make the url == uid == author-page check actually compare the
  h-card's uid, rather than an undefined variable

Add direct tests for both, and mark the library as archived on the
projects page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 16:23:49 +00:00

86 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\WebMention;
use App\Exceptions\InvalidMentionException;
/**
* Inspect the parsed microformats of a webmention source.
*
* Originally from the jonnybarnes/webmentions-parser package.
*/
class Parser
{
/**
* Return the type of mention.
*
* @throws InvalidMentionException when the mention type can't be determined
*/
public function getMentionType(array $microformats): string
{
foreach (['in-reply-to', 'like-of', 'repost-of'] as $type) {
if ($this->arrayKeyExistsRecursive($type, $microformats)) {
return $type;
}
}
throw new InvalidMentionException;
}
/**
* Check a reply is to the intended target.
*/
public function checkInReplyTo(array $microformats, string $target): bool
{
foreach ($microformats['items'] ?? [] as $item) {
$inReplyTo = $item['properties']['in-reply-to'] ?? null;
if ($inReplyTo === null) {
continue;
}
// The reply context may be an h-cite rather than a plain URL
if (is_array($inReplyTo[0]) && ($inReplyTo[0]['properties']['url'][0] ?? null) === $target) {
return true;
}
if (in_array($target, $inReplyTo, true)) {
return true;
}
}
return false;
}
/**
* Check the microformats contain a like of the target.
*/
public function checkLikeOf(array $microformats, string $target): bool
{
return in_array($target, $microformats['items'][0]['properties']['like-of'] ?? [], true);
}
/**
* Check the microformats contain a repost of the target.
*/
public function checkRepostOf(array $microformats, string $target): bool
{
return in_array($target, $microformats['items'][0]['properties']['repost-of'] ?? [], true);
}
private function arrayKeyExistsRecursive(string $needle, array $haystack): bool
{
if (array_key_exists($needle, $haystack)) {
return true;
}
foreach ($haystack as $value) {
if (is_array($value) && $this->arrayKeyExistsRecursive($needle, $value)) {
return true;
}
}
return false;
}
}