refactor(myservers1): improved manifest usage w/ remote comparison

This commit is contained in:
Zack Spear
2023-06-21 10:56:01 -05:00
committed by Zack Spear
parent 6b80016123
commit 1660ac7e02

View File

@@ -1,4 +1,3 @@
<!-- myservers1 -->
<style>
#header {
z-index: 102 !important;
@@ -18,19 +17,72 @@ connect-user-profile {
height: 100%;
}
</style>
<?
/**
* @todo create web component env switcher liker upcEnv(). If we utilize manifest.json then we'll be switching its path.
*/
$myservers_flash_cfg_path='/boot/config/plugins/dynamix.my.servers/myservers.cfg';
$myservers = file_exists($myservers_flash_cfg_path) ? @parse_ini_file($myservers_flash_cfg_path,true) : [];
// extract web component JS file from manifest
$jsonManifest = file_get_contents('/usr/local/emhttp/plugins/dynamix.my.servers/webComponents/manifest.json');
$jsonManifestData = json_decode($jsonManifest, true);
$webComponentJsFile = $jsonManifestData["connect-components.client.mjs"]["file"];
// add the web component source to the DOM
$localSourceBasePath = '/plugins/dynamix.my.servers/webComponents/';
$localSourceJs = $localSourceBasePath . $webComponentJsFile;
echo '<script defer src="' . $localSourceJs . '"></script>';
<?php
// Set the path for the local manifest file
$localManifestFile = '/usr/local/emhttp/plugins/dynamix.my.servers/connect-components/manifest.json';
// Define the remote resource URL
$remoteResourceUrl = 'https://components.myunraid.com/';
// Check if session cookie exists
if (!isset($_COOKIE['manifest_last_checked']) || time() - $_COOKIE['manifest_last_checked'] >= 300) {
// Get the remote manifest JSON
$remoteManifestJson = file_get_contents($remoteResourceUrl . 'manifest.json');
// Compare the remote and local manifest versions
$remoteManifest = json_decode($remoteManifestJson, true);
$localManifest = json_decode(file_get_contents($localManifestFile), true);
if ($remoteManifest && $localManifest && $remoteManifest !== $localManifest) {
// Update the local manifest
file_put_contents($localManifestFile, $remoteManifestJson);
// Download the file contents for the search value
$searchText = 'connect-components.client.mjs';
$fileValue = null;
foreach ($remoteManifest as $key => $value) {
if (strpos($key, $searchText) !== false && isset($value["file"])) {
$fileValue = file_get_contents($remoteResourceUrl . $value["file"]);
break;
}
}
if ($fileValue !== null) {
// Extract the directory path from the URL
$directoryPath = pathinfo($value["file"], PATHINFO_DIRNAME);
// Set the local file path
$localFilePath = '/usr/local/emhttp/plugins/dynamix.my.servers' . $directoryPath;
// Create the directory if it doesn't exist
if (!is_dir($localFilePath)) {
mkdir($localFilePath, 0777, true);
}
// Save the file contents to a local file
file_put_contents($localFilePath . '/' . basename($value["file"]), $fileValue);
}
}
// Set the session cookie with the current timestamp
setcookie('manifest_last_checked', time(), time() + 300); // Expire in 5 minutes
}
// Load the local manifest
$localManifest = json_decode(file_get_contents($localManifestFile), true);
$searchText = 'connect-components.client.mjs';
$fileValue = null;
foreach ($localManifest as $key => $value) {
if (strpos($key, $searchText) !== false && isset($value["file"])) {
$fileValue = $value["file"];
break;
}
}
if ($fileValue !== null) {
$prefixedPath = '/plugins/dynamix.my.servers/connect-components/';
echo '<script src="' . $prefixedPath . $fileValue . '"></script>';
} else {
echo '<script>console.error("%cNo matching key containing \'' . $searchText . '\' found.", "font-weight: bold; color: white; background-color: red");</script>';
}
?>
<!-- /myservers1 -->