From ca88bbdb915f84693dd3d22ef34d4616c2ebb453 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Mon, 19 Sep 2016 15:50:15 +0100 Subject: [PATCH] =?UTF-8?q?Filter=20html=20on=20output=20and=20rely=20on?= =?UTF-8?q?=20HTMLFilter=E2=80=99s=20own=20cache?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/NotesController.php | 19 ++++++- app/Observers/WebMentionObserver.php | 65 ------------------------ app/Providers/AppServiceProvider.php | 3 -- 3 files changed, 18 insertions(+), 69 deletions(-) delete mode 100644 app/Observers/WebMentionObserver.php diff --git a/app/Http/Controllers/NotesController.php b/app/Http/Controllers/NotesController.php index 590ff654..f1bddc32 100644 --- a/app/Http/Controllers/NotesController.php +++ b/app/Http/Controllers/NotesController.php @@ -6,6 +6,8 @@ use Cache; use Twitter; use App\Tag; use App\Note; +use HTMLPurifier; +use HTMLPurifier_Config; use Jonnybarnes\IndieWeb\Numbers; use Illuminate\Filesystem\Filesystem; use Jonnybarnes\WebmentionsParser\Authorship; @@ -103,7 +105,7 @@ class NotesController extends Controller case 'in-reply-to': $content['source'] = $webmention->source; $content['date'] = $carbon->parse($content['date'])->toDayDateTimeString(); - $content['reply'] = $microformats['items'][0]['properties']['content'][0]['html_purified']; + $content['reply'] = $this->filterHTML($microformats['items'][0]['properties']['content'][0]['html']); $replies[] = $content; break; @@ -260,4 +262,19 @@ class NotesController extends Controller return $oEmbed; } + + /** + * Filter the HTML in a reply webmention. + * + * @param string The reply HTML + * @return string The filtered HTML + */ + private function filterHTML($html) + { + $config = HTMLPurifier_Config::createDefault(); + $config->set('Cache.SerializerPath', storage_path() . '/HTMLPurifier'); + $purifier = new HTMLPurifier($config); + + return $purifier->purify($html); + } } diff --git a/app/Observers/WebMentionObserver.php b/app/Observers/WebMentionObserver.php deleted file mode 100644 index b15c3b3e..00000000 --- a/app/Observers/WebMentionObserver.php +++ /dev/null @@ -1,65 +0,0 @@ -addFilteredHTML($webmention); - } - - /** - * Listen for the updated event. - * - * @param WebMention $webmention - * @return void - */ - public function updated(WebMention $webmention) - { - $this->addFilteredHTML($webmention); - } - - /** - * Filter the HTML in a reply webmention. - * - * @param WebMention The WebMention model - * @return void - */ - private function addFilteredHTML(WebMention $webmention) - { - $mf2 = json_decode($webmention->mf2); - if (isset($mf2['items'][0]['properties']['content'][0]['html'])) { - $mf2['items'][0]['properties']['content'][0]['html_purified'] = $this->useHTMLPurifier( - $mf2['items'][0]['properties']['content'][0]['html'] - ); - } - $webmention->mf2 = json_encode($mf2); - $webmetion->save(); - } - - /** - * Set up and use HTMLPurifer on some HTML. - * - * @param string The HTML to be processed - * @return string The processed HTML - */ - private function useHTMLPurifier($html) - { - $config = HTMLPurifier_Config::createDefault(); - $config->set('Cache.SerializerPath', storage_path() . '/HTMLPurifier'); - $purifier = new HTMLPurifier($config); - - return $purifier->purify($html); - } -} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index a83013ab..177b94cc 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -47,9 +47,6 @@ class AppServiceProvider extends ServiceProvider $note->tags()->attach($tagsToAdd); } }); - - //observer the webmention model - WebMention::observe(WebMentionObserver::class); } /**