From a9021c89f487f295fe9a76f4b6db314f6d439e74 Mon Sep 17 00:00:00 2001 From: billz Date: Sat, 25 Jan 2025 02:49:48 -0800 Subject: [PATCH] Refactor getUserPlugins(), define pluginsManifest --- src/RaspAP/Plugins/PluginInstaller.php | 150 +++++++++---------------- 1 file changed, 51 insertions(+), 99 deletions(-) diff --git a/src/RaspAP/Plugins/PluginInstaller.php b/src/RaspAP/Plugins/PluginInstaller.php index b5ba43af..976b2c3f 100644 --- a/src/RaspAP/Plugins/PluginInstaller.php +++ b/src/RaspAP/Plugins/PluginInstaller.php @@ -21,6 +21,7 @@ class PluginInstaller private $destSudoers; private $refModules; private $rootPath; + private $pluginsManifest; public function __construct() { @@ -30,6 +31,7 @@ class PluginInstaller $this->destSudoers = '/etc/sudoers.d/'; $this->refModules = '/refs/heads/master/.gitmodules'; $this->rootPath = $_SERVER['DOCUMENT_ROOT']; + $this->pluginsManifest = '/plugins/manifest.json'; } // Returns a single instance of PluginInstaller @@ -42,112 +44,51 @@ class PluginInstaller } /** - * Returns user plugin details from associated manifest.json files + * Returns availble user plugin details from a manifest.json file * * @return array $plugins */ public function getUserPlugins() { - $installedPlugins = $this->getPlugins(); - try { - $submodules = $this->getSubmodules(RASPI_PLUGINS_URL); + $manifestPath = $this->rootPath . $this->pluginsManifest; + if (!file_exists($manifestPath)) { + throw new \Exception("Manifest file not found at " . $manifestPath); + } + + // decode manifest file contents + $manifestContents = file_get_contents($manifestPath); + $manifestData = json_decode($manifestContents, true); + + if (json_last_error() !== JSON_ERROR_NONE) { + throw new \Exception("Error parsing manifest.json: " . json_last_error_msg()); + } + // fetch installed plugins + $installedPlugins = $this->getPlugins(); + $plugins = []; - foreach ($submodules as $submodule) { - $manifestUrl = $submodule['url'] .$this->manifestRaw; - $manifest = $this->getPluginManifest($manifestUrl); + foreach ($manifestData as $pluginManifest) { + $installed = false; - if ($manifest) { - $installed = false; - - foreach ($installedPlugins as $plugin) { - if (str_contains($plugin, $manifest['namespace'])) { - $installed = true; - break; - } + // Check if the plugin is installed + foreach ($installedPlugins as $plugin) { + if (str_contains($plugin, $pluginManifest['namespace'])) { + $installed = true; + break; } - $plugins[] = [ - 'manifest' => $manifest, - 'installed' => $installed - ]; } + $plugins[] = [ + 'manifest' => $pluginManifest, + 'installed' => $installed + ]; } return $plugins; } catch (\Exception $e) { - echo "An error occured: " .$e->getMessage(); + echo "An error occurred: " . $e->getMessage(); + return []; } } - /** - * Retrieves a plugin's associated manifest JSON - * - * @param string $url - * @return array $json - */ - public function getPluginManifest(string $url): ?array - { - $options = [ - 'http' => [ - 'method' => 'GET', - 'follow_location' => 1, - ], - ]; - - $context = stream_context_create($options); - $content = file_get_contents($url, false, $context); - - if ($content === false) { - return null; - } - $json = json_decode($content, true); - - if (json_last_error() !== JSON_ERROR_NONE) { - return null; - } - return $json; - } - - /** - * Returns git submodules for the specified repository - * - * @param string $repoURL - * @return array $submodules - */ - public function getSubmodules(string $repoUrl): array - { - $gitmodulesUrl = $repoUrl .$this->refModules; - $gitmodulesContent = file_get_contents($gitmodulesUrl); - - if ($gitmodulesContent === false) { - throw new \Exception('Unable to fetch .gitmodules file from the repository'); - } - - $submodules = []; - $lines = explode("\n", $gitmodulesContent); - $currentSubmodule = []; - - foreach ($lines as $line) { - $line = trim($line); - - if (strpos($line, '[submodule "') === 0) { - if (!empty($currentSubmodule)) { - $submodules[] = $currentSubmodule; - } - $currentSubmodule = []; - } elseif (strpos($line, 'path = ') === 0) { - $currentSubmodule['path'] = substr($line, strlen('path = ')); - } elseif (strpos($line, 'url = ') === 0) { - $currentSubmodule['url'] = substr($line, strlen('url = ')); - } - } - - if (!empty($currentSubmodule)) { - $submodules[] = $currentSubmodule; - } - - return $submodules; - } - /** * Returns an array of installed plugins in pluginPath * @@ -419,13 +360,15 @@ class PluginInstaller $html .= 'Version'; $html .= 'Description'; $html .= ''; - $html .= ''; + $html .= ''; foreach ($plugins as $plugin) { - $manifest = htmlspecialchars(json_encode($plugin['manifest']), ENT_QUOTES, 'UTF-8'); + $manifestData = $plugin['manifest'][0] ?? []; // Access the first manifest entry or default to an empty array + + $manifest = htmlspecialchars(json_encode($manifestData), ENT_QUOTES, 'UTF-8'); $installed = $plugin['installed']; - if ($installed === true ) { + if ($installed === true) { $button = ''; @@ -434,14 +377,23 @@ class PluginInstaller name="install-plugin" data-bs-toggle="modal" data-bs-target="#install-user-plugin" data-plugin-manifest="' .$manifest. '"> ' . _("Details") .''; } - $name = '' - . htmlspecialchars($plugin['manifest']['name']). ''; + . $nameText. ''; + + $version = htmlspecialchars($manifestData['version'] ?? 'N/A'); + $description = htmlspecialchars($manifestData['description'] ?? 'No description available'); + $html .= '' .$name. ''; - $html .= '' .htmlspecialchars($plugin['manifest']['version']). ''; - $html .= '' .htmlspecialchars($plugin['manifest']['description']). ''; - $html .= '' .$button. ''; + $html .= '' .$version. ''; + $html .= '' .$description. ''; + $html .= '' .$button. ''; } $html .= ''; return $html;