mirror of
https://github.com/daledavies/jump.git
synced 2025-11-18 10:50:41 +01:00
40 lines
1.4 KiB
PHP
40 lines
1.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Jump;
|
||
|
|
|
||
|
|
class Site {
|
||
|
|
|
||
|
|
public string $name;
|
||
|
|
public bool $nofollow;
|
||
|
|
public string $icon;
|
||
|
|
public string $url;
|
||
|
|
|
||
|
|
public function __construct(array $sitearray) {
|
||
|
|
if (!isset($sitearray['name'], $sitearray['url'])) {
|
||
|
|
throw new \Exception('The array passed to Site() must contain the keys "name" and "url"!');
|
||
|
|
}
|
||
|
|
$this->name = $sitearray['name'];
|
||
|
|
$this->url = $sitearray['url'];
|
||
|
|
$this->nofollow = isset($sitearray['nofollow']) ? $sitearray['nofollow'] : false;
|
||
|
|
$this->icon = isset($sitearray['icon']) ? $this->get_favicon_datauri($sitearray['icon']) : $this->get_favicon_datauri();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function get_favicon_datauri(?string $icon = null): string {
|
||
|
|
if ($icon === null) {
|
||
|
|
$favicon = new \Favicon\Favicon();
|
||
|
|
$favicon->cache([
|
||
|
|
'dir' => '/var/www/cache/icons/',
|
||
|
|
'timeout' => 86400
|
||
|
|
]);
|
||
|
|
$rawimage = $favicon->get($this->url, \Favicon\FaviconDLType::RAW_IMAGE);
|
||
|
|
if (!$rawimage) {
|
||
|
|
$rawimage = file_get_contents(__DIR__ . '/../assets/images/default-icon.png');
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$rawimage = file_get_contents(__DIR__ . '/../sites/icons/'.$icon);
|
||
|
|
}
|
||
|
|
$mimetype = (new \finfo(FILEINFO_MIME_TYPE))->buffer($rawimage);
|
||
|
|
return 'data:'.$mimetype.';base64,'.base64_encode($rawimage);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|