Filter html on output and rely on HTMLFilter’s own cache
This commit is contained in:
parent
49e6e9a84d
commit
ca88bbdb91
3 changed files with 18 additions and 69 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,65 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use HTMLPurifier;
|
||||
use App\WebMention;
|
||||
use HTMLPurifier_Config;
|
||||
|
||||
class WebMentionObserver
|
||||
{
|
||||
/**
|
||||
* Listen for the created event.
|
||||
*
|
||||
* @param WebMention $webmention
|
||||
* @return void
|
||||
*/
|
||||
public function created(WebMention $webmention)
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
|
@ -47,9 +47,6 @@ class AppServiceProvider extends ServiceProvider
|
|||
$note->tags()->attach($tagsToAdd);
|
||||
}
|
||||
});
|
||||
|
||||
//observer the webmention model
|
||||
WebMention::observe(WebMentionObserver::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue