refactor: myservers1/2 update to match plg

This commit is contained in:
Zack Spear
2023-04-25 07:55:17 -07:00
parent ffc3955a4b
commit 85ae590fb4
2 changed files with 650 additions and 613 deletions
@@ -28,21 +28,40 @@ unraid-promo {
<?
$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) : [];
$ALLOWED_UPC_ENV_VALS = [
'production',
'staging',
'stagingLogs',
'development',
'local',
'preview',
];
$UPC_ENV_CK = in_array($_COOKIE['UPC_ENV']??'', $ALLOWED_UPC_ENV_VALS)
? $_COOKIE['UPC_ENV']
$ALLOWED_UPC_ENV_PREVIEW_CNAME = '.d1eohvtyc6gnee.amplifyapp.com/';
// defaults
$computedCookieValue = $_COOKIE['UPC_ENV'] ?? '';
$previewUrl = '';
$isPreview = strpos($computedCookieValue, 'preview::');
// extract preview src url
if ($isPreview !== false) {
list($computedCookieValue, $previewUrl) = explode('::', $computedCookieValue);
// prevent unauthoraized URLs for previews
$isPreviewAllowed = strpos($previewUrl, $ALLOWED_UPC_ENV_PREVIEW_CNAME);
if (!$isPreviewAllowed) {
$computedCookieValue = '';
$previewUrl = '';
}
}
// finalize cookie value
$UPC_ENV_CK = in_array($computedCookieValue, $ALLOWED_UPC_ENV_VALS)
? $computedCookieValue
: null;
// Determine what source we should use for web components
if (!file_exists('/usr/local/sbin/unraid-api')) { // When NOT using the plugin we should load the UPC from the file system unless $UPC_ENV_CK exists.
$UPC_ENV = $UPC_ENV_CK ?? 'local';
} else { // When PLG exists load from local when not signed in but when signed in load UPC from production.
$UPC_ENV = $UPC_ENV_CK ?? ((empty($myservers['remote']['username']) || empty($var['regFILE'])) ? 'local' : 'production');
} else {
$UPC_ENV = $UPC_ENV_CK ?? 'production';
}
$upcLocalSrc = autov('/plugins/dynamix.my.servers/webComps/unraid.min.js', true);
switch ($UPC_ENV) {
@@ -58,6 +77,9 @@ switch ($UPC_ENV) {
case 'development':
$upcSrc = 'https://launchpad.unraid.test:6969/webComps/unraid.js?t=' . time();
break;
case 'preview':
$upcSrc = $previewUrl . 'webComps/unraid.min.js';
break;
default: // load from webGUI filesystem.
$upcSrc = $upcLocalSrc;
break;
@@ -68,18 +90,20 @@ echo '<script id="unraid-wc" defer src="' . $upcSrc . '"></script>';
<script type="text/javascript">
const upcEnvCookie = "<?=$UPC_ENV_CK??''?>";
if (upcEnvCookie) console.debug('[UPC_ENV] ✨', upcEnvCookie);
// If the UPC isn't defined after 2secs inject UPC via
// If the UPC isn't defined after 3secs inject UPC via
setTimeout(() => {
if (!window.customElements.get('unraid-user-profile')) {
console.log('[UPC] Fallback to filesystem src 😖');
const el = document.createElement('script');
el.type = 'text/javascript';
el.src = '<?=$upcLocalSrc?>';
document.head.appendChild(el);
return upcEnv('local', false, true); // set session cookie to prevent delayed loads of UPC
}
return false;
}, 2000);
// UPC exists do nothing
if (window.customElements.get('unraid-user-profile')) return;
console.log('[UPC] Fallback to filesystem src 😖');
const el = document.createElement('script');
el.type = 'text/javascript';
el.src = '<?=$upcLocalSrc?>';
document.head.appendChild(el);
return upcEnv('local', false, true); // set session cookie to prevent delayed loads of UPC
}, 3000);
function upcEnv(str, reload = true, session = false) { // overwrite upc src
const ckName = 'UPC_ENV';
const ckDate = new Date();
File diff suppressed because it is too large Load Diff