jonnybarnes.uk/app/Jobs/SendWebMentions.php

117 lines
3.2 KiB
PHP
Raw Normal View History

2016-05-19 15:01:28 +01:00
<?php
namespace App\Jobs;
use App\Note;
2016-05-19 15:01:28 +01:00
use GuzzleHttp\Client;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendWebMentions extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $note;
protected $guzzle;
2016-05-19 15:01:28 +01:00
/**
* Create the job instance, inject dependencies.
2016-05-19 15:01:28 +01:00
*
* @param User $user
* @param Note $note
2016-05-19 15:01:28 +01:00
* @return void
*/
2016-06-23 14:27:00 +01:00
public function __construct(Note $note, Client $guzzle = null)
2016-05-19 15:01:28 +01:00
{
$this->note = $note;
$this->guzzle = $guzzle ?? new Client();
2016-05-19 15:01:28 +01:00
}
/**
* Execute the job.
*
* @return void
*/
public function handle(Note $note)
2016-05-19 15:01:28 +01:00
{
//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);
if ($endpoint) {
$this->guzzle->post($endpoint, [
'form_params' => [
'source' => $this->note->longurl,
'target' => $url
]
2016-06-23 14:27:00 +01:00
]);
}
2016-05-19 15:01:28 +01:00
}
}
/**
* Discover if a URL has a webmention endpoint.
*
* @param string The URL
* @param \GuzzleHttp\Client $client
* @return string The webmention endpoint URL
*/
private function discoverWebmentionEndpoint($url)
2016-05-19 15:01:28 +01:00
{
2016-06-21 16:11:08 +01:00
//lets not send webmentions to myself
if (parse_url($url, PHP_URL_HOST) == env('LONG_URL', 'localhost')) {
return false;
}
2016-05-19 15:01:28 +01:00
$endpoint = null;
$response = $this->guzzle->get($url);
2016-05-19 15:01:28 +01:00
//check HTTP Headers for webmention endpoint
$links = \GuzzleHttp\Psr7\parse_header($response->getHeader('Link'));
foreach ($links as $link) {
if ($link['rel'] == 'webmention') {
return trim($link[0], '<>');
}
}
//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) {
if (filter_var($endpoint, FILTER_VALIDATE_URL)) {
return $endpoint;
}
//it must be a relative url, so resolve with php-mf2
return $mf2->resolveUrl($endpoint);
}
return false;
}
/**
* Get the URLs from a note.
*/
private 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;
}
2016-05-19 15:01:28 +01:00
}