diff --git a/jumpapp/classes/Site.php b/jumpapp/classes/Site.php index 0de8bf9..692b1d1 100644 --- a/jumpapp/classes/Site.php +++ b/jumpapp/classes/Site.php @@ -17,19 +17,34 @@ class Site { public string $icon; public string $url; - public function __construct(Config $config, array $sitearray) { + public function __construct(Config $config, array $sitearray, array $default) { $this->config = $config; + $this->defaults = $default; 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->nofollow = isset($sitearray['nofollow']) ? $sitearray['nofollow'] : (isset($this->defaults['nofollow']) ? $this->defaults['nofollow'] : false); $this->icon = isset($sitearray['icon']) ? $this->get_favicon_datauri($sitearray['icon']) : $this->get_favicon_datauri(); } + /** + * Return a data uri for a given icon, or a site's favicon if an icon + * is not provided. + * + * @param string|null $icon File name of a given icon to retrieve. + * @return string Base 64 encoded datauri for the icon image. + */ public function get_favicon_datauri(?string $icon = null): string { + // 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 ($icon === null) { + // Go get the favicon, if there isnt one then use the default icon. $favicon = new \Favicon\Favicon(); $favicon->cache([ 'dir' => $this->config->get('cachedir').'/icons/', @@ -37,7 +52,7 @@ class Site { ]); $rawimage = $favicon->get($this->url, \Favicon\FaviconDLType::RAW_IMAGE); if (!$rawimage) { - $rawimage = file_get_contents($this->config->get('defaulticonpath')); + $rawimage = file_get_contents($defaulticon); } } else { $rawimage = file_get_contents($this->config->get('sitesdir').'/icons/'.$icon); @@ -46,4 +61,4 @@ class Site { return 'data:'.$mimetype.';base64,'.base64_encode($rawimage); } -} \ No newline at end of file +} diff --git a/jumpapp/classes/Sites.php b/jumpapp/classes/Sites.php index f6357f8..c0ac904 100644 --- a/jumpapp/classes/Sites.php +++ b/jumpapp/classes/Sites.php @@ -15,6 +15,7 @@ class Sites { private Cache $cache; private Config $config; + private array $default; private string $sitesfilelocation; private array $loadedsites; @@ -26,6 +27,10 @@ class Sites { $this->loadedsites = []; $this->sitesfilelocation = $this->config->get('sitesfile'); $this->cache = $cache; + $this->default = [ + 'icon' => null, + 'nofollow' => false + ]; $this->load_sites_from_json(); } @@ -48,13 +53,23 @@ class Sites { if ($rawjson === '') { throw new Exception('The sites.json file is empty'); } - $decodedjson = json_decode($rawjson, true); - if (!is_array($decodedjson)) { - throw new Exception('Provided sites json does not contain a top-level array'); + // Do some checks to see if the JSON decodes into something + // like what we expect to see... + $decodedjson = json_decode($rawjson); + if (is_array($decodedjson)) { + $sites = $decodedjson; } - foreach ($decodedjson as $array) { - $sites[] = new Site($this->config, $array); + if (isset($decodedjson->sites) && is_array($decodedjson->sites)) { + $sites = $decodedjson->sites; + $this->default = (array) $decodedjson->default; } + // Walk over the sites array and instantiate an actual Site() object + // for each element. + array_walk($sites, function(&$item, $key, $default) { + $item = new Site($this->config, (array) $item, $default); + }, $this->default); + // Return the array of Site() objects, note we are in a callback + // so the return is not from the outer function. return $sites; }); } diff --git a/jumpapp/sites/icons/bitwarden.png b/jumpapp/sites/icons/bitwarden.png index f10836c..83773df 100644 Binary files a/jumpapp/sites/icons/bitwarden.png and b/jumpapp/sites/icons/bitwarden.png differ diff --git a/jumpapp/sites/icons/gitea.png b/jumpapp/sites/icons/gitea.png index dcd4edb..f8a2c9c 100644 Binary files a/jumpapp/sites/icons/gitea.png and b/jumpapp/sites/icons/gitea.png differ diff --git a/jumpapp/sites/icons/my-default-icon.png b/jumpapp/sites/icons/my-default-icon.png new file mode 100644 index 0000000..90cfe40 Binary files /dev/null and b/jumpapp/sites/icons/my-default-icon.png differ diff --git a/jumpapp/sites/icons/nextcloud.png b/jumpapp/sites/icons/nextcloud.png index 80144b6..cfac16b 100644 Binary files a/jumpapp/sites/icons/nextcloud.png and b/jumpapp/sites/icons/nextcloud.png differ diff --git a/jumpapp/sites/icons/paperless.jpg b/jumpapp/sites/icons/paperless.jpg index 2a4c54a..bb4413a 100644 Binary files a/jumpapp/sites/icons/paperless.jpg and b/jumpapp/sites/icons/paperless.jpg differ diff --git a/jumpapp/sites/sites.json b/jumpapp/sites/sites.json index 2881686..2a66a88 100644 --- a/jumpapp/sites/sites.json +++ b/jumpapp/sites/sites.json @@ -1,30 +1,33 @@ -[ - { - "name": "Bitwarden", - "url" : "https://bitwarden.example.com", +{ + "default": { "nofollow": true, - "icon": "bitwarden.png" + "icon": "my-default-icon.png" }, - { - "name": "Gitea", - "url" : "https://git.example.com", - "nofollow": true, - "icon": "gitea.png" - }, - { - "name": "Nextcloud", - "url" : "https://cloud.example.com", - "nofollow": true, - "icon": "nextcloud.png" - }, - { - "name": "Paperless", - "url" : "https://paperless.example.com", - "nofollow": true, - "icon": "paperless.jpg" - }, - { - "name": "Google", - "url" : "https://www.google.com" - } -] \ No newline at end of file + "sites": [ + { + "name": "Bitwarden", + "url" : "https://bitwarden.example.com", + "icon": "bitwarden.png" + }, + { + "name": "Gitea", + "url" : "https://git.example.com", + "icon": "gitea.png" + }, + { + "name": "Nextcloud", + "url" : "https://cloud.example.com", + "icon": "nextcloud.png" + }, + { + "name": "Paperless", + "url" : "https://paperless.example.com", + "icon": "paperless.jpg" + }, + { + "name": "Google", + "url" : "https://www.google.com", + "nofollow": false + } + ] +}