Files
Jump/jumpapp/classes/Site.php

49 lines
1.7 KiB
PHP
Raw Normal View History

2022-02-04 09:53:55 +00:00
<?php
namespace Jump;
2022-02-04 11:52:57 +00:00
/**
* Parse the data required to represent a site and provide method for generating
* and/or retrieving the site's icon.
*
* @author Dale Davies <dale@daledavies.co.uk>
* @license MIT
*/
2022-02-04 09:53:55 +00:00
class Site {
2022-02-12 00:22:19 +00:00
private Config $config;
2022-02-04 09:53:55 +00:00
public string $name;
public bool $nofollow;
public string $icon;
public string $url;
2022-02-12 00:22:19 +00:00
public function __construct(Config $config, array $sitearray) {
$this->config = $config;
2022-02-04 09:53:55 +00:00
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' => $this->config->get('cachedir').'/icons/',
2022-02-04 09:53:55 +00:00
'timeout' => 86400
]);
$rawimage = $favicon->get($this->url, \Favicon\FaviconDLType::RAW_IMAGE);
if (!$rawimage) {
2022-02-12 00:22:19 +00:00
$rawimage = file_get_contents($this->config->get('defaulticonpath'));
2022-02-04 09:53:55 +00:00
}
} else {
2022-02-12 00:22:19 +00:00
$rawimage = file_get_contents($this->config->get('sitesdir').'/icons/'.$icon);
2022-02-04 09:53:55 +00:00
}
$mimetype = (new \finfo(FILEINFO_MIME_TYPE))->buffer($rawimage);
return 'data:'.$mimetype.';base64,'.base64_encode($rawimage);
}
}