Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for local hosted plugin logo files (png only) #18811

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 75 additions & 1 deletion src/Glpi/Marketplace/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public static function installed(
$clean_plugin = [
'key' => $key,
'name' => $plugin['name'],
'logo_url' => $apidata['logo_url'] ?? "",
'logo_url' => self::getLogoUrl($apidata, $key),
'description' => $apidata['descriptions'][0]['short_description'] ?? "",
'authors' => $apidata['authors'] ?? [['id' => 'all', 'name' => $plugin['author'] ?? ""]],
'license' => $apidata['license'] ?? $plugin['license'] ?? "",
Expand All @@ -268,6 +268,80 @@ public static function installed(
self::displayList($plugins, "installed", $only_lis);
}

/**
* Get the logo of a plugin
* Useful for custom plugins which will be used internally only and are not published in the internet
*
* @param array $apidata collected data of the plugin
* @param string $key plugins directory name
*
* @return string url to the plugins logo or empty string
*/
protected static function getLogoUrl(array $apidata, string $key): string {
// Check if a logo URL is provided from an online resource
if (isset($apidata['logo_url'])) {
return $apidata['logo_url'];
}

// Relative path to the plugin directory (used for HTML rendering)
$pluginPath = '../plugins/' . $key . '/';

// Attempt to find a local logo named after the plugin directory
if (self::checkImage($key, $key . '.png')) {
return $pluginPath . $key . '.png';
}

// Attempt to find a default file "logo.png" in the plugin directory
if (self::checkImage($key, 'logo.png')) {
return $pluginPath . 'logo.png';
}

// Attempt to find a default file "icon.png" in the plugin directory
if (self::checkImage($key, 'icon.png')) {
return $pluginPath . 'icon.png';
}

// No logo found, return an empty string
return '';
}

/**
* Checks whether an image file exists, is readable, and is a valid image.
*
* @param string $key The plugin key used to determine the plugin directory.
* @param string $filename The name of the image file to check.
* @return bool Returns true if the image exists, is readable, and is valid; otherwise, false.
*/
protected static function checkImage(string $key, string $filename): bool {
// Get the absolute path to the plugin directory
$pluginDir = Plugin::getPhpDir($key) . '/';

// Construct the full path to the image file
$logo = $pluginDir . $filename;

// Check if the file exists, is readable, and is a valid image
if (file_exists($logo) && is_readable($logo) && self::isValidImage($logo)) {
return true;
}

// Image not usable
return false;
}

/**
* Checks if the given file is a valid image.
*
* @param string $filePath The path to the file to check.
* @return bool True if the file is a valid image, false otherwise.
*/
protected static function isValidImage(string $filePath): bool {
// Attempt to retrieve image information
$imageInfo = @getimagesize($filePath);

// Return true if the file is a valid image, otherwise false
return ($imageInfo !== false);
}

/**
* Display discover tab (all availble plugins)
*
Expand Down