jonnybarnes.uk/app/Jobs/SendWebMentions.php

138 lines
3.7 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;
2016-09-20 13:13:05 +01:00
use Illuminate\Bus\Queueable;
2016-05-19 15:01:28 +01:00
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
2016-09-20 13:13:05 +01:00
class SendWebMentions implements ShouldQueue
2016-05-19 15:01:28 +01:00
{
2016-09-20 13:13:05 +01:00
use InteractsWithQueue, Queueable, SerializesModels;
2016-05-19 15:01:28 +01:00
protected $note;
2016-05-19 15:01:28 +01:00
/**
* Create the job instance, inject dependencies.
2016-05-19 15:01:28 +01:00
*
* @param Note $note
2016-05-19 15:01:28 +01:00
* @return void
*/
2016-09-07 16:28:31 +01:00
public function __construct(Note $note)
2016-05-19 15:01:28 +01:00
{
$this->note = $note;
2016-05-19 15:01:28 +01:00
}
/**
* Execute the job.
*
* @param \GuzzleHttp\Client $guzzle
2016-05-19 15:01:28 +01:00
* @return void
*/
2016-09-07 16:28:31 +01:00
public function handle(Client $guzzle)
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) {
2016-09-07 16:28:31 +01:00
$endpoint = $this->discoverWebmentionEndpoint($url, $guzzle);
if ($endpoint) {
2016-09-07 16:28:31 +01:00
$guzzle->post($endpoint, [
'form_params' => [
'source' => $this->note->longurl,
2016-06-23 15:58:45 +01:00
'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 $guzzle
2016-05-19 15:01:28 +01:00
* @return string The webmention endpoint URL
*/
private function discoverWebmentionEndpoint($url, $guzzle)
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-06-28 22:30:31 +01:00
if (starts_with($url, '/notes/tagged/')) {
return false;
}
2016-06-21 16:11:08 +01:00
2016-05-19 15:01:28 +01:00
$endpoint = null;
$response = $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 (mb_stristr($link['rel'], 'webmention')) {
return $this->resolveUri($link[0], $url);
2016-05-19 15:01:28 +01:00
}
}
//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);
2016-05-19 15:01:28 +01:00
}
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;
}
2016-09-06 16:44:15 +01:00
return (string) \GuzzleHttp\Psr7\Uri::resolve(
\GuzzleHttp\Psr7\uri_for($base),
$endpoint
);
}
2016-05-19 15:01:28 +01:00
}