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
134 lines
3.5 KiB
PHP
134 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use Cache;
|
|
use Twitter;
|
|
use App\Traits\FilterHtml;
|
|
use Illuminate\Filesystem\Filesystem;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Jonnybarnes\WebmentionsParser\Authorship;
|
|
|
|
class WebMention extends Model
|
|
{
|
|
use FilterHtml;
|
|
|
|
/**
|
|
* The database table used by the model.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $table = 'webmentions';
|
|
|
|
/**
|
|
* We shall set a blacklist of non-modifiable model attributes.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $guarded = ['id'];
|
|
|
|
/**
|
|
* Define the relationship.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
|
|
*/
|
|
public function commentable()
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
/**
|
|
* Get the author of the webmention.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getAuthorAttribute(): array
|
|
{
|
|
$authorship = new Authorship();
|
|
$hCard = $authorship->findAuthor(json_decode($this->mf2, true));
|
|
if (array_key_exists('properties', $hCard) &&
|
|
array_key_exists('photo', $hCard['properties'])
|
|
) {
|
|
$hCard['properties']['photo'][0] = $this->createPhotoLink($hCard['properties']['photo'][0]);
|
|
}
|
|
|
|
return $hCard;
|
|
}
|
|
|
|
/**
|
|
* Get the published value for the webmention.
|
|
*
|
|
* @return string|null
|
|
*/
|
|
public function getPublishedAttribute(): ?string
|
|
{
|
|
$mf2 = $this->mf2 ?? '';
|
|
$microformats = json_decode($mf2, true);
|
|
if (isset($microformats['items'][0]['properties']['published'][0])) {
|
|
try {
|
|
$published = carbon()->parse(
|
|
$microformats['items'][0]['properties']['published'][0]
|
|
)->toDayDateTimeString();
|
|
} catch (\Exception $exception) {
|
|
$published = $this->updated_at->toDayDateTimeString();
|
|
}
|
|
} else {
|
|
$published = $this->updated_at->toDayDateTimeString();
|
|
}
|
|
|
|
return $published;
|
|
}
|
|
|
|
/**
|
|
* Get the filtered HTML of a reply.
|
|
*
|
|
* @return string|null
|
|
*/
|
|
public function getReplyAttribute(): ?string
|
|
{
|
|
if ($this->mf2 === null) {
|
|
return null;
|
|
}
|
|
$microformats = json_decode($this->mf2, true);
|
|
if (isset($microformats['items'][0]['properties']['content'][0]['html'])) {
|
|
return $this->filterHtml($microformats['items'][0]['properties']['content'][0]['html']);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Create the photo link.
|
|
*
|
|
* @param string
|
|
* @return string
|
|
*/
|
|
public function createPhotoLink(string $url): string
|
|
{
|
|
$url = normalize_url($url);
|
|
$host = parse_url($url, PHP_URL_HOST);
|
|
if ($host == 'pbs.twimg.com') {
|
|
//make sure we use HTTPS, we know twitter supports it
|
|
return str_replace('http://', 'https://', $url);
|
|
}
|
|
if ($host == 'twitter.com') {
|
|
if (Cache::has($url)) {
|
|
return Cache::get($url);
|
|
}
|
|
$username = ltrim(parse_url($url, PHP_URL_PATH), '/');
|
|
$info = Twitter::getUsers(['screen_name' => $username]);
|
|
$profile_image = $info->profile_image_url_https;
|
|
Cache::put($url, $profile_image, 10080); //1 week
|
|
|
|
return $profile_image;
|
|
}
|
|
$filesystem = new Filesystem();
|
|
if ($filesystem->exists(public_path() . '/assets/profile-images/' . $host . '/image')) {
|
|
return '/assets/profile-images/' . $host . '/image';
|
|
}
|
|
|
|
return $url;
|
|
}
|
|
}
|