jonnybarnes.uk/app/Jobs/SendWebMentions.php
Jonny Barnes cb465f027a Squashed commit of the following:
commit c835e6e220950dd2f95976eae2a50f71a1c532d1
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Mon Nov 7 11:49:26 2016 +0000

    Change visibilty of a method

commit e67d6bfd54cb677e738f934544e0c45c3de59891
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sun Nov 6 19:42:07 2016 +0000

    Edit method to send webmentions using the new job method

commit fe9839572148b644c5e0e0f32f639650ffbb968c
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sun Nov 6 19:10:32 2016 +0000

    Updated changelog

commit 1b09744404459e7a6a164788f09ebcc6468b7055
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sun Nov 6 19:08:45 2016 +0000

    Allow notes to be deleted
2016-11-07 11:50:02 +00:00

137 lines
3.7 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
namespace App\Jobs;
use App\Note;
use GuzzleHttp\Client;
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
* @return void
*/
public function __construct(Note $note)
{
$this->note = $note;
}
/**
* Execute the job.
*
* @param \GuzzleHttp\Client $guzzle
* @return void
*/
public function handle(Client $guzzle)
{
//grab the URLs
$urlsInReplyTo = explode(' ', $this->note->in_reply_to);
$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, $guzzle);
if ($endpoint) {
$guzzle->post($endpoint, [
'form_params' => [
'source' => $this->note->longurl,
'target' => $url,
],
]);
}
}
}
/**
* Discover if a URL has a webmention endpoint.
*
* @param string The URL
* @param \GuzzleHttp\Client $guzzle
* @return string The webmention endpoint URL
*/
public function discoverWebmentionEndpoint($url, $guzzle)
{
//lets not send webmentions to myself
if (parse_url($url, PHP_URL_HOST) == env('LONG_URL', 'localhost')) {
return false;
}
if (starts_with($url, '/notes/tagged/')) {
return false;
}
$endpoint = null;
$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($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);
}
return false;
}
/**
* Get the URLs from a note.
*
* @param string $html
* @return array $urls
*/
public function getLinks($html)
{
$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
* @return string
*/
public function resolveUri(string $url, string $base): string
{
$endpoint = \GuzzleHttp\Psr7\uri_for($url);
if ($endpoint->getScheme() !== null) {
return (string) $endpoint;
}
return (string) \GuzzleHttp\Psr7\Uri::resolve(
\GuzzleHttp\Psr7\uri_for($base),
$endpoint
);
}
}