refactor: conditional display of MS in UPC

This commit is contained in:
Zack Spear
2021-03-31 08:54:06 -07:00
parent 5fe1fd9ff3
commit b9001a43d0

View File

@@ -1273,140 +1273,6 @@ function upcEnv(str) {
}, 2000);
return document.cookie = `${ckName}=${str}; expires=${ckDate.toGMTString()}`;
};
function handleMessage(e) {
//if (e.origin != "http://child.com") { return; }
if (e.data.length == 0) { return; }
const SAFE_JSON_PARSE = (str) => {
try {
return [null, JSON.parse(str)];
} catch (err) {
return [err];
}
};
const [err, data] = SAFE_JSON_PARSE(e.data);
if (err) return false; // swallow json parse error
const HANDLE_LICENSES = (data, e) => {
if (data.license) {
$.get('/webGui/include/InstallKey.php', {url: data.license}, function() {
console.log('New license key installed: ' + data.license);
const payload = {
event: 'LICENSE_PINGBACK',
message: 'New license key installed',
license: data.license,
success: true,
};
e.source.postMessage(JSON.stringify(payload), e.origin);
}).fail(function() {
console.error('Failed to license new key: ' + data.license);
const payload = {
event: 'LICENSE_PINGBACK',
message: 'Failed to license new key',
license: data.license,
success: false,
};
e.source.postMessage(JSON.stringify(payload), e.origin);
});
} else {
console.error('KEY_PURCHASE event fired! but missing license data:', data);
}
};
switch (data.event) {
case "CLOSE_SHADOWBOX":
hideRegWizard();
break;
case "SHUTDOWN":
window.location.href = '/webGui/include/Boot.php';
break;
case "REG_WIZARD":
if (data.apikey) {
var postargs = {
'#file': 'Unraid.net/myservers.cfg',
'#section': 'remote',
apikey: data.apikey,
regWizTime: `${Date.now()}_${data.guid}`, // set when signing in the first time and never unset for the sake of displaying Sign In/Up in the UPC without needing to validate guid every time
};
if (data.email) {
postargs['email'] = data.email;
}
if (data.username) {
postargs['username'] = data.username;
}
if (data.avatar) {
postargs['avatar'] = data.avatar;
}
$.post('/update.php', postargs, function() {
console.log('Unraid.net/myservers.cfg: Updated apikey under [remote] section');
// send a ping back to the regwiz
const payload = { event: 'ACCOUNT_PINGBACK', success: true, type: 'signIn', webGuiPathname: window.location.pathname };
e.source.postMessage(JSON.stringify(payload), e.origin);
}).fail(function() {
console.error('Failed to update apikey under [remote] section');
// send a ping back to the regwiz
const payload = { event: 'ACCOUNT_PINGBACK', success: false, type: 'signIn', webGuiPathname: window.location.pathname };
e.source.postMessage(JSON.stringify(payload), e.origin);
});
}
// duplicate conditional so we don't get the error from HANDLE_LICENSES()
if (data.license) HANDLE_LICENSES(data, e);
break;
case "KEY_PURCHASE":
HANDLE_LICENSES(data, e);
break;
case "GET_STATE":
$.get('/plugins/dynamix.unraid.net/include/state.php', function(newstate) {
e.source.postMessage(newstate, e.origin);
}).fail(function(err) {
console.error('Failed to get new state: ' + err);
});
break;
case "SUCCESS_NEW_STATE":
$.get('/plugins/dynamix.unraid.net/include/state.php', function(newstate) {
// parse response so we can update the event type string to send back
const [stateError, stateData] = SAFE_JSON_PARSE(newstate);
const newPayload = { ...stateData, event: 'DELIVER_NEW_STATE', success: true };
e.source.postMessage(JSON.stringify(newPayload), e.origin);
}).fail(function(err) {
e.source.postMessage(JSON.stringify({ event: 'DELIVER_NEW_STATE', success: false }), e.origin);
});
break;
case "MYSERVERS_UNREGISTER":
$.post('/update.php', {
'#file': 'Unraid.net/myservers.cfg',
'#section': 'remote',
apikey: '',
avatar: '',
email: '',
username: '',
}, function() {
console.log('Unraid.net/myservers.cfg: Unregistered myservers, cleared apikey under [remote] section');
// send a ping back to the regwiz
const payload = { event: 'ACCOUNT_PINGBACK', success: true, type: 'signOut', webGuiPathname: window.location.pathname };
e.source.postMessage(JSON.stringify(payload), e.origin);
}).fail(function() {
console.error('Failed to unregister');
// send a ping back to the regwiz
const payload = { event: 'ACCOUNT_PINGBACK', success: false, type: 'signOut', webGuiPathname: window.location.pathname };
e.source.postMessage(JSON.stringify(payload), e.origin);
});
break;
case "RELOAD":
return window.location.reload();
case "REDIRECT_MAIN":
return window.location.href = '/Main';
case "STOP_SENDING_SESSION":
break;
case "PREFLIGHT_REQUEST":
break;
default:
console.error('Unhandled event \'' + data.event + '\' fired. data:', data);
break;
}
}
window.addEventListener('message', handleMessage, false);
</script>
<?
// Determine what source we should use for web components
@@ -2009,8 +1875,10 @@ unraid-launchpad {
apikey="<?=($remote['apikey']) ? $remote['apikey'] : ''?>"
banner="<?=($display['banner']) ? $display['banner'] : ''?>"
bgcolor="<?=($backgnd) ? '#'.$backgnd : ''?>"
csrf="<?=$var['csrf_token']?>"
displaydesc="<?=($display['headerdescription']!='no') ? 'true' : ''?>"
expiretime="<?=1000*($var['regTy']=='Trial'||strstr($var['regTy'],'expired')?$var['regTm2']:0)?>"
include-my-servers="<?=(file_exists('/usr/local/sbin/unraid-api')) ? 'yes' : '' ?>"
locale="<?=($_SESSION['locale']) ? $_SESSION['locale'] : 'en_US'?>"
locale-messages="<?=rawurlencode(json_encode($upc_translations, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE))?>"
reg-wiz-time="<?=($remote['regWizTime']) ? $remote['regWizTime'] : ''?>"