jonnybarnes.uk/app/Jobs/SendWebMentions.php
Jonny Barnes fb44afd7ad Remove deprecated global helper functions (issue #99)
Squashed commit of the following:

commit 8ff29a8ab51ee5057ef786614ab95b005bf8918c
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Feb 1 18:42:05 2019 +0000

    Replace deprecated global helpers with their facade equivalents
2019-02-01 18:49:35 +00:00

142 lines
3.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace App\Jobs;
use App\Models\Note;
use GuzzleHttp\Client;
use Illuminate\Support\Str;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendWebMentions implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
protected $note;
/**
* Create the job instance, inject dependencies.
*
* @param Note $note
*/
public function __construct(Note $note)
{
$this->note = $note;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//grab the URLs
$inReplyTo = $this->note->in_reply_to ?? '';
// above so explode doesnt complain about null being passed in
$urlsInReplyTo = explode(' ', $inReplyTo);
$urlsNote = $this->getLinks($this->note->note);
$urls = array_filter(array_merge($urlsInReplyTo, $urlsNote)); //filter out none URLs
foreach ($urls as $url) {
$endpoint = $this->discoverWebmentionEndpoint($url);
if ($endpoint !== null) {
$guzzle = resolve(Client::class);
$guzzle->post($endpoint, [
'form_params' => [
'source' => $this->note->longurl,
'target' => $url,
],
]);
}
}
}
/**
* Discover if a URL has a webmention endpoint.
*
* @param string $url
* @return string|null
*/
public function discoverWebmentionEndpoint(string $url)
{
//lets not send webmentions to myself
if (parse_url($url, PHP_URL_HOST) == config('app.longurl')) {
return;
}
if (Str::startsWith($url, '/notes/tagged/')) {
return;
}
$endpoint = null;
$guzzle = resolve(Client::class);
$response = $guzzle->get($url);
//check HTTP Headers for webmention endpoint
$links = \GuzzleHttp\Psr7\parse_header($response->getHeader('Link'));
foreach ($links as $link) {
if (mb_stristr($link['rel'], 'webmention')) {
return $this->resolveUri(trim($link[0], '<>'), $url);
}
}
//failed to find a header so parse HTML
$html = (string) $response->getBody();
$mf2 = new \Mf2\Parser($html, $url);
$rels = $mf2->parseRelsAndAlternates();
if (array_key_exists('webmention', $rels[0])) {
$endpoint = $rels[0]['webmention'][0];
} elseif (array_key_exists('http://webmention.org/', $rels[0])) {
$endpoint = $rels[0]['http://webmention.org/'][0];
}
if ($endpoint) {
return $this->resolveUri($endpoint, $url);
}
}
/**
* Get the URLs from a note.
*
* @param string $html
* @return array $urls
*/
public function getLinks($html)
{
if ($html == '' || is_null($html)) {
return [];
}
$urls = [];
$dom = new \DOMDocument();
$dom->loadHTML($html);
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $anchor) {
$urls[] = ($anchor->hasAttribute('href')) ? $anchor->getAttribute('href') : false;
}
return $urls;
}
/**
* Resolve a URI if necessary.
*
* @param string $url
* @param string $base The base of the URL
* @return string
*/
public function resolveUri(string $url, string $base): string
{
$endpoint = \GuzzleHttp\Psr7\uri_for($url);
if ($endpoint->getScheme() != '') {
return (string) $endpoint;
}
return (string) \GuzzleHttp\Psr7\Uri::resolve(
\GuzzleHttp\Psr7\uri_for($base),
$endpoint
);
}
}