2022-02-04 09:53:55 +00:00
|
|
|
<?php
|
2022-07-19 16:38:55 +01:00
|
|
|
/**
|
|
|
|
|
* ██ ██ ██ ███ ███ ██████
|
|
|
|
|
* ██ ██ ██ ████ ████ ██ ██
|
|
|
|
|
* ██ ██ ██ ██ ████ ██ ██████
|
|
|
|
|
* ██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
|
* █████ ██████ ██ ██ ██
|
|
|
|
|
*
|
|
|
|
|
* @author Dale Davies <dale@daledavies.co.uk>
|
|
|
|
|
* @copyright Copyright (c) 2022, Dale Davies
|
|
|
|
|
* @license MIT
|
|
|
|
|
*/
|
2022-02-04 09:53:55 +00:00
|
|
|
|
|
|
|
|
namespace Jump;
|
|
|
|
|
|
2022-03-15 21:38:39 +00:00
|
|
|
use stdClass;
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2022-02-04 09:53:55 +00:00
|
|
|
class Site {
|
|
|
|
|
|
2023-04-17 12:55:18 +01:00
|
|
|
public string $id;
|
2022-02-04 09:53:55 +00:00
|
|
|
public string $name;
|
|
|
|
|
public bool $nofollow;
|
2022-03-15 21:38:39 +00:00
|
|
|
public ?string $iconname;
|
2022-02-04 09:53:55 +00:00
|
|
|
public string $url;
|
2022-03-15 21:38:39 +00:00
|
|
|
public array $tags = ['home'];
|
2022-02-04 09:53:55 +00:00
|
|
|
|
2022-03-15 21:38:39 +00:00
|
|
|
/**
|
|
|
|
|
* Parse the data required to represent a site and provide method for generating
|
|
|
|
|
* and/or retrieving the site's icon.
|
|
|
|
|
*
|
|
|
|
|
* @param Config $config A Jump Config() object.
|
|
|
|
|
* @param array $sitearray Array of options for this site from sites.json.
|
|
|
|
|
* @param array $defaults Array of default values for this site to use, defined in sites.json.
|
|
|
|
|
*/
|
2023-04-17 12:55:18 +01:00
|
|
|
public function __construct(private Config $config, private Cache $cache, array $sitearray, private array $defaults) {
|
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"!');
|
|
|
|
|
}
|
2023-03-22 21:50:54 +00:00
|
|
|
$this->id = 'site-'.md5($sitearray['url']);
|
2022-02-04 09:53:55 +00:00
|
|
|
$this->name = $sitearray['name'];
|
|
|
|
|
$this->url = $sitearray['url'];
|
2022-02-21 10:58:20 +00:00
|
|
|
$this->nofollow = isset($sitearray['nofollow']) ? $sitearray['nofollow'] : (isset($this->defaults['nofollow']) ? $this->defaults['nofollow'] : false);
|
2022-03-22 22:40:50 +00:00
|
|
|
$this->newtab = isset($sitearray['newtab']) ? $sitearray['newtab'] : (isset($this->defaults['newtab']) ? $this->defaults['newtab'] : false);
|
2022-03-15 21:38:39 +00:00
|
|
|
$this->iconname = $sitearray['icon'] ?? null;
|
|
|
|
|
$this->tags = $sitearray['tags'] ?? $this->tags;
|
2022-06-30 10:43:41 +01:00
|
|
|
$this->description = isset($sitearray['description']) ? $sitearray['description'] : $sitearray['name'];
|
2023-03-03 14:41:06 +00:00
|
|
|
$this->status = $sitearray['status'] ?? null;
|
2022-02-04 09:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-21 10:58:20 +00:00
|
|
|
/**
|
2022-03-15 21:38:39 +00:00
|
|
|
* Return an object containing mimetype and raw image data, or a site's
|
|
|
|
|
* favicon if an icon is not provided in sites.json.
|
2022-02-21 10:58:20 +00:00
|
|
|
*
|
2022-03-15 21:38:39 +00:00
|
|
|
* @return object Containing mimetype and raw image data.
|
2022-02-21 10:58:20 +00:00
|
|
|
*/
|
2022-03-15 21:38:39 +00:00
|
|
|
public function get_favicon_image_data(): object {
|
2023-04-17 12:55:18 +01:00
|
|
|
return $this->cache->load(cachename: 'sites/favicons', key: $this->id, callback: function() {
|
|
|
|
|
// Use the applications own default icon unless one is supplied via the sites.json file.
|
|
|
|
|
$defaulticon = $this->config->get('defaulticonpath');
|
|
|
|
|
if (isset($this->defaults['icon'])) {
|
|
|
|
|
$defaulticon = $this->config->get('sitesdir').'/icons/'.$this->defaults['icon'];
|
|
|
|
|
}
|
|
|
|
|
// Did we have a supplied icon or are we going to try retrieving the favicon?
|
|
|
|
|
if ($this->iconname === null) {
|
|
|
|
|
// Go get the favicon, if there isnt one then use the default icon.
|
|
|
|
|
$favicon = new \Favicon\Favicon();
|
|
|
|
|
$rawimage = $favicon->get($this->url, \Favicon\FaviconDLType::RAW_IMAGE);
|
2023-04-15 14:50:06 +01:00
|
|
|
} else {
|
2023-04-17 12:55:18 +01:00
|
|
|
// If the icon name has a file extension the n try to retrieve it locally, otherwise
|
|
|
|
|
// see if we can get it from Dashboard Icons.
|
|
|
|
|
if (pathinfo($this->iconname, PATHINFO_EXTENSION)) {
|
|
|
|
|
$file = $this->config->get('sitesdir').'/icons/'.$this->iconname;
|
2023-04-18 14:31:39 +01:00
|
|
|
$errormessage = 'Icon file not found... '.$file;
|
2023-04-17 12:55:18 +01:00
|
|
|
} else {
|
|
|
|
|
$file = 'https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons@master/svg/'.$this->iconname.'.svg';
|
2023-04-18 14:31:39 +01:00
|
|
|
$errormessage = 'Dashboard icon does not exist... '.$this->iconname;
|
|
|
|
|
}
|
|
|
|
|
$rawimage = @file_get_contents($file);
|
|
|
|
|
if (!$rawimage) {
|
|
|
|
|
error_log($errormessage);
|
2023-04-17 12:55:18 +01:00
|
|
|
}
|
2023-04-15 14:50:06 +01:00
|
|
|
}
|
2023-04-17 12:55:18 +01:00
|
|
|
// If we didnt manage to get any icon data from any of the above methods then return
|
|
|
|
|
// the default icon.
|
|
|
|
|
if (!$rawimage) {
|
|
|
|
|
$rawimage = file_get_contents($defaulticon);
|
|
|
|
|
}
|
|
|
|
|
$imagedata = new stdClass();
|
|
|
|
|
$imagedata->mimetype = (new \finfo(FILEINFO_MIME_TYPE))->buffer($rawimage);
|
|
|
|
|
$imagedata->data = $rawimage;
|
|
|
|
|
return $imagedata;
|
|
|
|
|
});
|
2022-03-15 21:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a data uri or a site's favicon if an icon is not provided.
|
|
|
|
|
*
|
|
|
|
|
* @return string Base 64 encoded datauri for the icon image.
|
|
|
|
|
*/
|
|
|
|
|
public function get_favicon_datauri(): string {
|
|
|
|
|
$imagedata = $this->get_favicon_image_data();
|
|
|
|
|
return 'data:'.$imagedata->mimetype.';base64,'.base64_encode($imagedata->data);
|
2022-02-04 09:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
2022-07-25 12:55:53 +01:00
|
|
|
/**
|
|
|
|
|
* Get the online status of this site.
|
|
|
|
|
*
|
|
|
|
|
* @return string The site status.
|
|
|
|
|
*/
|
|
|
|
|
public function get_status(): string {
|
|
|
|
|
$cache = new Cache($this->config);
|
|
|
|
|
return (new Status($cache, $this))->get_status();
|
|
|
|
|
}
|
2022-02-21 10:58:20 +00:00
|
|
|
}
|