Menu="ManagementAccess:200"
Title="Callback Tests"
Icon="icon-u-globe"
Tag="globe"
---
<?php
/**
 * @todo create web component env switcher liker upcEnv(). If we utilize manifest.json then we'll be switching its path.
 */
$docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/webGui/include/Wrappers.php";
$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) : [];
// print_r($mystatus);
// 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"];
// web component
$localSourceBasePath = '/plugins/dynamix.my.servers/webComponents/';
$localSourceJs = $localSourceBasePath . $webComponentJsFile;
// add the web component source to the DOM
echo '<script id="unraid-webcomponents" defer src="' . $localSourceJs . '"></script>';

/**
 * Build vars for user profile prop
 */
// add 'ipaddr' function for 6.9 backwards compatibility
if (!function_exists('ipaddr')) {
  function ipaddr($ethX='eth0', $prot=4) {
    global $$ethX;
    switch ($$ethX['PROTOCOL:0']) {
    case 'ipv4':
      return $$ethX['IPADDR:0'];
    case 'ipv6':
      return $$ethX['IPADDR6:0'];
    case 'ipv4+ipv6':
      switch ($prot) {
      case 4: return $$ethX['IPADDR:0'];
      case 6: return $$ethX['IPADDR6:0'];
      default:return [$$ethX['IPADDR:0'],$$ethX['IPADDR6:0']];}
    default:
      return $$ethX['IPADDR:0'];
    }
  }
}
$configErrorEnum = [ // used to map $var['configValid'] value to mimic unraid-api's `configError` ENUM
  "error" => 'UNKNOWN_ERROR',
  "ineligible" => 'INELIGIBLE',
  "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');

$connectPluginInstalled = '';
if (!file_exists('/var/lib/pkgtools/packages/dynamix.unraid.net') && !file_exists('/var/lib/pkgtools/packages/dynamix.unraid.net.staging')) {
  $connectPluginInstalled = ''; // base OS only, plugin not installed • show ad for plugin
} else {
  // plugin is installed but if the unraid-api file doesn't fully install it's a failed install
  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 • append failure detected so we can show warning about failed install via UPC
  if (!file_exists('/usr/bin/unraid-api')) $connectPluginInstalled = $connectPluginInstalled . '_installFailed';
}

$serverData = [
  "apiVersion" => $myservers['api']['version'] ?? '',
  "avatar" => (!empty($myservers['remote']['avatar']) && $connectPluginInstalled) ? $myservers['remote']['avatar'] : '',
  "banner" => $display['banner'] ?? '',
  "bannerGradient" => $display['showBannerGradient'] ?? 'yes',
  "bgColor" => ($backgnd) ? '#'.$backgnd : '',
  "config" => [
    'valid' => $var['configValid'] === 'yes',
    'error' => $var['configValid'] !== 'yes'
      ? (array_key_exists($var['configValid'], $configErrorEnum) ? $configErrorEnum[$var['configValid']] : 'UNKNOWN_ERROR')
      : null,
  ],
  "csrf" => $var['csrf_token'],
  "description" => $var['COMMENT'],
  "descriptionShow" => ($display['headerdescription']??''!='no') ? 'true' : '',
  "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']),
  "lanIp" => ipaddr(),
  "internalPort" => $_SERVER['SERVER_PORT'],
  "keyfile" => empty($var['regFILE'])? "" : str_replace(['+','/','='], ['-','_',''], trim(base64_encode(@file_get_contents($var['regFILE'])))),
  "locale" => ($_SESSION['locale']) ? $_SESSION['locale'] : 'en_US',
  "metaColor" => ($display['headermetacolor']??'') ? '#'.$display['headermetacolor'] : '',
  "model" => $var['SYS_MODEL'],
  "name" => $var['NAME'],
  "osVersion" => $var['version'],
  "plgVersion" => $plgversion = 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'] ),
  "connectPluginInstalled" => $connectPluginInstalled,
  "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']),
  "textColor" => ($header) ? '#'.$header : '',
  "theme" => $display['theme'],
  "ts" => time(),
  "uptime" => 1000*(time() - round(strtok(exec("cat /proc/uptime"),' '))),
  "username" => (!empty($myservers['remote']['username']) && $connectPluginInstalled) ? $myservers['remote']['username'] : '',
  "wanFQDN" => $nginx['NGINX_WANFQDN'] ?? '',
];

$themeBg = '#111';
if ($display['theme'] === 'black' || $display['theme'] === 'azure') {
  $themeBg = '#fff';
}
?>
<style>
  .ComponentWrapper {
    padding: 16px;
  }
</style>
<div class="ComponentWrapper" style="background-color: <?=$themeBg?>;">
  <?="<unraid-user-profile server='" . json_encode($serverData) . "'></connect-user-profile>"?>
</div>
<div class="ComponentWrapper">
  <unraid-auth></connect-auth>
</div>
<div class="ComponentWrapper">
</div>
<div class="ComponentWrapper">
  <unraid-key-actions></connect-key-actions>
</div>
<div class="ComponentWrapper">
  <unraid-wan-ip-check php-wan-ip="<?=http_get_contents('https://wanip4.unraid.net/')?>"></connect-wan-ip-check>
</div>

<script>
/**
 * So we're not needing to modify DefaultLayout with an additional include,
 * we'll add the Modals web component to the bottom of the body
 */
const modalsWebComponent = 'connect-modals';
if (!document.getElementsByTagName(modalsWebComponent).length) {
  const $body = document.getElementsByTagName('body')[0];
  const $modals = document.createElement(modalsWebComponent);
  $body.appendChild($modals);
}
</script>
