refactor(plg): upc server state data simplify

This commit is contained in:
Zack Spear
2023-07-20 14:12:01 -07:00
committed by Zack Spear
parent e2d0a1b6ac
commit 5503b253c2
3 changed files with 99 additions and 109 deletions

View File

@@ -0,0 +1,11 @@
<?php
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
$var = (array)parse_ini_file('state/var.ini');
require_once "$docroot/webGui/include/Helpers.php";
require_once "$docroot/plugins/dynamix.my.servers/include/state.php";
header('Content-type: application/json');
echo json_encode($serverState);

View File

@@ -1,113 +1,8 @@
<?php
/**
* Build vars for user profile prop
*/
// add 'ipaddr' function for 6.9 backwards compatibility
if (!function_exists('ipaddr')) {
function ipaddr($ethX = 'eth0', $prot = 4)
{
$ethXData = $$ethX;
if (isset($ethXData['PROTOCOL:0'])) {
$protocol = $ethXData['PROTOCOL:0'];
$ipaddr = $ethXData['IPADDR:0'];
$ipaddr6 = $ethXData['IPADDR6:0'];
switch ($protocol) {
case 'ipv4':
return $ipaddr;
case 'ipv6':
return $ipaddr6;
case 'ipv4+ipv6':
return ($prot === 4) ? $ipaddr : $ipaddr6;
}
}
return '';
}
}
$configErrorEnum = [
"error" => 'UNKNOWN_ERROR',
"invalid" => 'INVALID',
"nokeyserver" => 'NO_KEY_SERVER',
"withdrawn" => 'WITHDRAWN',
];
// read flashbackup ini file
$flashbackup_ini = '/var/local/emhttp/flashbackup.ini';
$flashbackup_status = (file_exists($flashbackup_ini)) ? @parse_ini_file($flashbackup_ini) : [];
$nginx = parse_ini_file('/var/local/emhttp/nginx.ini');
// base OS only, plugin not installed • show ad for plugin
$connectPluginInstalled = '';
if (file_exists('/var/lib/pkgtools/packages/dynamix.unraid.net')) $connectPluginInstalled = 'dynamix.unraid.net.plg';
if (file_exists('/var/lib/pkgtools/packages/dynamix.unraid.net.staging')) $connectPluginInstalled = 'dynamix.unraid.net.staging.plg';
// plugin install failed if the unraid-api file doesn't fully install • append failure detected so we can show warning about failed install via UPC
if ($connectPluginInstalled && !file_exists('/usr/local/sbin/unraid-api')) $connectPluginInstalled .= '_installFailed';
$pluginVersion = file_exists('/var/log/plugins/dynamix.unraid.net.plg')
? trim(@exec('/usr/local/sbin/plugin version /var/log/plugins/dynamix.unraid.net.plg 2>/dev/null'))
: (file_exists('/var/log/plugins/dynamix.unraid.net.staging.plg')
? trim(@exec('/usr/local/sbin/plugin version /var/log/plugins/dynamix.unraid.net.staging.plg 2>/dev/null'))
: 'base-' . $var['version']);
$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) : [];
$serverData = [
"apiKey" => $myservers['upc']['apikey'] ?? '',
"apiVersion" => $myservers['api']['version'] ?? '',
"avatar" => (!empty($myservers['remote']['avatar']) && $connectPluginInstalled) ? $myservers['remote']['avatar'] : '',
"config" => [
'valid' => ($var['configValid'] === 'yes'),
'error' => isset($configErrorEnum[$var['configValid']]) ? $configErrorEnum[$var['configValid']] : 'UNKNOWN_ERROR',
],
"connectPluginInstalled" => $connectPluginInstalled,
"csrf" => $var['csrf_token'],
"description" => $var['COMMENT'] ?? '',
"deviceCount" => $var['deviceCount'],
"email" => $myservers['remote']['email'] ?? '',
"expireTime" => 1000 * (($var['regTy'] === 'Trial' || strstr($var['regTy'], 'expired')) ? $var['regTm2'] : 0),
"extraOrigins" => explode(',', $myservers['api']['extraOrigins'] ?? ''),
"flashProduct" => $var['flashProduct'],
"flashVendor" => $var['flashVendor'],
"flashBackupActivated" => empty($flashbackup_status['activated']) ? '' : 'true',
"guid" => $var['flashGUID'],
"hasRemoteApikey" => !empty($myservers['remote']['apikey']),
"internalPort" => $_SERVER['SERVER_PORT'],
"keyfile" => empty($var['regFILE']) ? '' : str_replace(['+', '/', '='], ['-', '_', ''], trim(base64_encode(@file_get_contents($var['regFILE'])))),
"lanIp" => ipaddr(),
"locale" => ($_SESSION['locale']) ? $_SESSION['locale'] : 'en_US',
"model" => $var['SYS_MODEL'],
"name" => $var['NAME'],
"osVersion" => $var['version'],
"pluginVersion" => $pluginVersion,
"protocol" => $_SERVER['REQUEST_SCHEME'],
"regGen" => (int)$var['regGen'],
"regGuid" => $var['regGUID'],
"registered" => (!empty($myservers['remote']['username']) && $connectPluginInstalled),
"registeredTime" => $myservers['remote']['regWizTime'] ?? '',
"site" => $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'],
"state" => strtoupper(empty($var['regCheck']) ? $var['regTy'] : $var['regCheck']),
"theme" => [
"banner" => !empty($display['banner']),
"bannerGradient" => $display['showBannerGradient'] === 'yes' ?? false,
"bgColor" => ($backgnd) ? '#' . $backgnd : '',
"descriptionShow" => (!empty($display['headerdescription']) && $display['headerdescription'] !== 'no'),
"metaColor" => ($display['headermetacolor'] ?? '') ? '#' . $display['headermetacolor'] : '',
"name" => $display['theme'],
"textColor" => ($header) ? '#' . $header : '',
],
"ts" => time(),
"uptime" => 1000 * (time() - round(strtok(exec("cat /proc/uptime"), ' '))),
"username" => (!empty($myservers['remote']['username']) && $connectPluginInstalled) ? $myservers['remote']['username'] : '',
"wanFQDN" => $nginx['NGINX_WANFQDN'] ?? '',
];
echo "<connect-user-profile server='" . json_encode($serverData) . "'></connect-user-profile>";
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
require_once("$docroot/plugins/dynamix.my.servers/include/state.php");
// var_dump($serverState);
echo "<connect-user-profile server='" . json_encode($serverState) . "'></connect-user-profile>";
?>
<script>
/**

View File

@@ -0,0 +1,84 @@
<?php
// read flashbackup ini file
$flashbackup_ini = '/var/local/emhttp/flashbackup.ini';
$flashbackup_status = (file_exists($flashbackup_ini)) ? @parse_ini_file($flashbackup_ini) : [];
$nginx = parse_ini_file('/var/local/emhttp/nginx.ini');
// base OS only, plugin not installed • show ad for plugin
$connectPluginInstalled = '';
if (file_exists('/var/lib/pkgtools/packages/dynamix.unraid.net')) $connectPluginInstalled = 'dynamix.unraid.net.plg';
if (file_exists('/var/lib/pkgtools/packages/dynamix.unraid.net.staging')) $connectPluginInstalled = 'dynamix.unraid.net.staging.plg';
// plugin install failed if the unraid-api file doesn't fully install • append failure detected so we can show warning about failed install via UPC
if ($connectPluginInstalled && !file_exists('/usr/local/sbin/unraid-api')) $connectPluginInstalled .= '_installFailed';
$pluginVersion = file_exists('/var/log/plugins/dynamix.unraid.net.plg')
? trim(@exec('/usr/local/sbin/plugin version /var/log/plugins/dynamix.unraid.net.plg 2>/dev/null'))
: (file_exists('/var/log/plugins/dynamix.unraid.net.staging.plg')
? trim(@exec('/usr/local/sbin/plugin version /var/log/plugins/dynamix.unraid.net.staging.plg 2>/dev/null'))
: 'base-' . $var['version']);
$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) : [];
$configErrorEnum = [
"error" => 'UNKNOWN_ERROR',
"invalid" => 'INVALID',
"nokeyserver" => 'NO_KEY_SERVER',
"withdrawn" => 'WITHDRAWN',
];
$serverState = [
"__DEV__" => [
'HTTP_REFERER' => $_SERVER['HTTP_REFERER'],
'HTTP_X_REQUESTED_WITH' => $_SERVER['HTTP_X_REQUESTED_WITH'],
'REQUEST_URI' => $_SERVER['REQUEST_URI'],
],
"apiKey" => $myservers['upc']['apikey'] ?? '',
"apiVersion" => $myservers['api']['version'] ?? '',
"avatar" => (!empty($myservers['remote']['avatar']) && $connectPluginInstalled) ? $myservers['remote']['avatar'] : '',
"config" => [
'valid' => ($var['configValid'] === 'yes'),
'error' => isset($configErrorEnum[$var['configValid']]) ? $configErrorEnum[$var['configValid']] : 'UNKNOWN_ERROR',
],
"connectPluginInstalled" => $connectPluginInstalled,
"csrf" => $var['csrf_token'],
"description" => $var['COMMENT'] ?? '',
"deviceCount" => $var['deviceCount'],
"email" => $myservers['remote']['email'] ?? '',
"expireTime" => 1000 * (($var['regTy'] === 'Trial' || strstr($var['regTy'], 'expired')) ? $var['regTm2'] : 0),
"extraOrigins" => explode(',', $myservers['api']['extraOrigins'] ?? ''),
"flashProduct" => $var['flashProduct'],
"flashVendor" => $var['flashVendor'],
"flashBackupActivated" => empty($flashbackup_status['activated']) ? '' : 'true',
"guid" => $var['flashGUID'],
"hasRemoteApikey" => !empty($myservers['remote']['apikey']),
"internalPort" => $_SERVER['SERVER_PORT'],
"keyfile" => empty($var['regFILE']) ? '' : str_replace(['+', '/', '='], ['-', '_', ''], trim(base64_encode(@file_get_contents($var['regFILE'])))),
"lanIp" => ipaddr(),
"locale" => ($_SESSION['locale']) ? $_SESSION['locale'] : 'en_US',
"model" => $var['SYS_MODEL'],
"name" => $var['NAME'],
"osVersion" => $var['version'],
"pluginVersion" => $pluginVersion,
"protocol" => $_SERVER['REQUEST_SCHEME'],
"regGen" => (int)$var['regGen'],
"regGuid" => $var['regGUID'],
"registered" => (!empty($myservers['remote']['username']) && $connectPluginInstalled),
"registeredTime" => $myservers['remote']['regWizTime'] ?? '',
"site" => $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'],
"state" => strtoupper(empty($var['regCheck']) ? $var['regTy'] : $var['regCheck']),
"theme" => [
"banner" => !empty($display['banner']),
"bannerGradient" => $display['showBannerGradient'] === 'yes' ?? false,
"bgColor" => ($backgnd) ? '#' . $backgnd : '',
"descriptionShow" => (!empty($display['headerdescription']) && $display['headerdescription'] !== 'no'),
"metaColor" => ($display['headermetacolor'] ?? '') ? '#' . $display['headermetacolor'] : '',
"name" => $display['theme'],
"textColor" => ($header) ? '#' . $header : '',
],
"ts" => time(),
"uptime" => 1000 * (time() - round(strtok(exec("cat /proc/uptime"), ' '))),
"username" => (!empty($myservers['remote']['username']) && $connectPluginInstalled) ? $myservers['remote']['username'] : '',
"wanFQDN" => $nginx['NGINX_WANFQDN'] ?? '',
];