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
59 lines
1.2 KiB
PHP
59 lines
1.2 KiB
PHP
<?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 author’s 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;
|
||
}
|
||
}
|