2016-05-19 15:01:28 +01:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
2016-06-21 15:53:05 +01:00
|
|
|
|
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
|
|
|
|
|
2016-06-21 15:53:05 +01:00
|
|
|
|
protected $note;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
|
|
|
|
|
/**
|
2016-06-21 15:53:05 +01:00
|
|
|
|
* Create the job instance, inject dependencies.
|
2016-05-19 15:01:28 +01:00
|
|
|
|
*
|
2016-06-21 15:53:05 +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
|
|
|
|
{
|
2016-06-21 15:53:05 +01:00
|
|
|
|
$this->note = $note;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the job.
|
|
|
|
|
*
|
2016-06-23 17:32:50 +01:00
|
|
|
|
* @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
|
|
|
|
{
|
2016-06-21 15:53:05 +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);
|
2016-06-21 15:53:05 +01:00
|
|
|
|
if ($endpoint) {
|
2016-09-07 16:28:31 +01:00
|
|
|
|
$guzzle->post($endpoint, [
|
2016-06-21 15:53:05 +01:00
|
|
|
|
'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-06-21 15:53:05 +01:00
|
|
|
|
}
|
2016-05-19 15:01:28 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Discover if a URL has a webmention endpoint.
|
|
|
|
|
*
|
|
|
|
|
* @param string The URL
|
2016-06-23 17:32:50 +01:00
|
|
|
|
* @param \GuzzleHttp\Client $guzzle
|
2016-05-19 15:01:28 +01:00
|
|
|
|
* @return string The webmention endpoint URL
|
|
|
|
|
*/
|
2016-06-23 17:32:50 +01:00
|
|
|
|
private function discoverWebmentionEndpoint($url, $guzzle)
|
2016-05-19 15:01:28 +01:00
|
|
|
|
{
|
2016-06-21 16:11:08 +01:00
|
|
|
|
//let’s 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/')) {
|
2016-06-28 22:25:07 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-06-21 16:11:08 +01:00
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
|
$endpoint = null;
|
|
|
|
|
|
2016-06-23 17:32:50 +01:00
|
|
|
|
$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) {
|
2016-09-05 23:39:54 +01:00
|
|
|
|
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) {
|
2016-09-05 23:39:54 +01:00
|
|
|
|
return $this->resolveUri($endpoint, $url);
|
2016-05-19 15:01:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-06-21 15:53:05 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the URLs from a note.
|
2016-06-23 17:32:50 +01:00
|
|
|
|
*
|
|
|
|
|
* @param string $html
|
|
|
|
|
* @return array $urls
|
2016-06-21 15:53:05 +01:00
|
|
|
|
*/
|
2016-09-05 23:39:54 +01:00
|
|
|
|
public function getLinks($html)
|
2016-06-21 15:53:05 +01:00
|
|
|
|
{
|
|
|
|
|
$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-09-05 23:39:54 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
2016-09-05 23:39:54 +01:00
|
|
|
|
return (string) \GuzzleHttp\Psr7\Uri::resolve(
|
|
|
|
|
\GuzzleHttp\Psr7\uri_for($base),
|
|
|
|
|
$endpoint
|
|
|
|
|
);
|
|
|
|
|
}
|
2016-05-19 15:01:28 +01:00
|
|
|
|
}
|