jonnybarnes.uk/app/Models/Like.php
Jonny Barnes ae77ef3423 Switch to html-sanitizer (issue #92)
Squashed commit of the following:

commit 504fb82beea5eff26591e117496d41c88f3737e4
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Jan 25 16:59:05 2019 +0000

    Fix coding style issue

commit 0ae14f0d90f131d65894abdc36f787032c7c97db
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Jan 25 16:57:26 2019 +0000

    html-sanitizer output differs slightly from HTMLPurifier

commit c5912312e0c8a41dbd7f7e52489e516d9784bc26
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Jan 25 16:56:54 2019 +0000

    Use html-sanitizer instead of HTMLPruifier, consolidate logic into a trait

commit 563b5b5ae8e2ef9c5aeb87214acab8fa9b0683ce
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Jan 25 16:56:10 2019 +0000

    Add html-sanitizer instead of HTMLPurifier
2019-01-25 17:21:32 +00:00

59 lines
1.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace App\Models;
use Mf2;
use App\Traits\FilterHtml;
use Illuminate\Database\Eloquent\Model;
class Like extends Model
{
use FilterHtml;
protected $fillable = ['url'];
/**
* Normalize the URL of a Like.
*
* @param string $value The provided URL
*/
public function setUrlAttribute(string $value)
{
$this->attributes['url'] = normalize_url($value);
}
/**
* Normalize the URL of the author of the like.
*
* @param string $value The authors url
*/
public function setAuthorUrlAttribute(?string $value)
{
$this->attributes['author_url'] = normalize_url($value);
}
/**
* If the content contains HTML, filter it.
*
* @param string $value The content of the like
* @return string|null
*/
public function getContentAttribute(?string $value): ?string
{
if ($value === null) {
return null;
}
$mf2 = Mf2\parse($value, $this->url);
if (array_get($mf2, 'items.0.properties.content.0.html')) {
return $this->filterHtml(
$mf2['items'][0]['properties']['content'][0]['html']
);
}
return $value;
}
}