diff --git a/app/Http/Controllers/WebMentionsController.php b/app/Http/Controllers/WebMentionsController.php index fc254058..ad0a59fe 100644 --- a/app/Http/Controllers/WebMentionsController.php +++ b/app/Http/Controllers/WebMentionsController.php @@ -28,7 +28,7 @@ class WebMentionsController extends Controller } //next check the $target is valid - $path = parse_url($request->input('target'))['path']; + $path = parse_url($request->input('target'), PHP_URL_PATH); $pathParts = explode('/', $path); switch ($pathParts[1]) { @@ -36,9 +36,8 @@ class WebMentionsController extends Controller //we have a note $noteId = $pathParts[2]; $numbers = new Numbers(); - $realId = $numbers->b60tonum($noteId); try { - $note = Note::findOrFail($realId); + $note = Note::findOrFail($numbers->b60tonum($noteId)); $this->dispatch(new ProcessWebMention($note, $request->input('source'))); } catch (ModelNotFoundException $e) { return new Response('This note doesn’t exist.', 400); diff --git a/app/Jobs/ProcessWebMention.php b/app/Jobs/ProcessWebMention.php index b2593427..d9d73eb4 100644 --- a/app/Jobs/ProcessWebMention.php +++ b/app/Jobs/ProcessWebMention.php @@ -2,8 +2,8 @@ namespace App\Jobs; +use Mf2; use App\Note; -use Mf2\parse; use HTMLPurifier; use App\WebMention; use GuzzleHttp\Client; @@ -11,6 +11,7 @@ use HTMLPurifier_Config; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Jonnybarnes\WebmentionsParser\Parser; +use GuzzleHttp\Exception\RequestException; use Illuminate\Contracts\Queue\ShouldQueue; class ProcessWebMention extends Job implements ShouldQueue @@ -44,7 +45,11 @@ class ProcessWebMention extends Job implements ShouldQueue $sourceURL = parse_url($this->source); $baseURL = $sourceURL['scheme'] . '://' . $sourceURL['host']; $remoteContent = $this->getRemoteContent($this->source); - $microformats = $this->parseHTML($remoteContent, $baseURL); + if ($remoteContent === null) { + return false; + } + $mf2Parser = new Mf2\Parser($remoteContent, $baseURL, true); + $microformats = $mf2Parser->parse(); $count = WebMention::where('source', '=', $this->source)->count(); if ($count > 0) { //we already have a webmention from this source @@ -140,14 +145,18 @@ class ProcessWebMention extends Job implements ShouldQueue /** * Retreive the remote content from a URL, and caches the result. * - * @param string The URL to retreive content from - * @return string The HTML from the URL + * @param string The URL to retreive content from + * @return string|null The HTML from the URL (or null if error) */ private function getRemoteContent($url) { $client = new Client(); - $response = $client->get($url); + try { + $response = $client->request('GET', $url); + } catch(RequestException $e) { + return; + } $html = (string) $response->getBody(); $path = storage_path() . '/HTML/' . $this->createFilenameFromURL($url); $this->fileForceContents($path, $html); @@ -189,20 +198,6 @@ class ProcessWebMention extends Job implements ShouldQueue file_put_contents("$dir/$name", $contents); } - /** - * A wrapper function for php-mf2’s parse method. - * - * @param string The HTML to parse - * @param string The base URL to resolve relative URLs in the HTML against - * @return array The porcessed microformats - */ - private function parseHTML($html, $baseurl) - { - $microformats = \Mf2\parse((string) $html, $baseurl); - - return $microformats; - } - /** * Save a profile image to the local cache. *