
<?php
  $seperator = "//\n";
  $q=$_GET["q"];
  
  //require './ZZ_versions.php';
  require './ZZ_webstatus.php';
  require './ZZ_readsettings.php';
  require './ZZ_readconfig.php';

  $user_setting_status = "";

  $boxtime = strtotime($SHIELD_now_time);
  $servertime = strtotime(date("H:i:s"));
  $timeerror = round(abs($servertime - $boxtime) / 60,2);
  $timeerror = number_format($timeerror, 2, '.', ',');


  if($SHIELD_state == "Running"){
    if($timeerror >= 1){
      $SHIELD_state = "Time Error";      
      $user_setting_status = $SHIELD_state . " " . $timeerror;
    }
  }
  if($SHIELD_state == "NOT RUNNING"){$user_setting_status = $SHIELD_state;}
  if($SHIELD_state == "UPDATING"){$user_setting_status = "Settings Applied";} 
  if($SHIELD_state == "Running"){$user_setting_status = " ";} 

  //if($SHIELD_wlan0 == "Scanning"){$user_setting_status = "Scanning for $ShieldSetting_05";} 

  if($q == 5){
    $cfgstr = "";
    if($ShieldSetting_01 == ""){$ShieldSetting_01 = "admin";}
    $oldpass = $ShieldSetting_01;
    $newpass = "";
    
    if (isset($_GET['02'])) {
      $newpass = $_GET['02'];
      if($newpass != ""){$oldpass = "";}
      if (isset($_GET['01'])) {
        $oldpass = $_GET['01'];
      }
    }    

    $cfgstr .= "01_user_password=" .  $oldpass . "\n";
    if($newpass != ""){
      $cfgstr .= "02_user_newpassword=" .  $newpass . "\n";
    }
    
    if (isset($_GET['03'])) {$cfgstr .= "03_wifi_state=" .         $_GET['03'] . "\n";}
    if (isset($_GET['04'])) {$cfgstr .= "04_wifi_mode=" .          $_GET['04'] . "\n";}
    if (isset($_GET['05'])) {$cfgstr .= "05_wifi_ssid_join=" .     $_GET['05'] . "\n";}
    if (isset($_GET['06'])) {$cfgstr .= "06_wifi_password_join=" . $_GET['06'] . "\n";}
    if (isset($_GET['07'])) {$cfgstr .= "07_wifi_ssid_AP=" .       $_GET['07'] . "\n";}
    if (isset($_GET['08'])) {$cfgstr .= "08_wifi_password_AP=" .   $_GET['08'] . "\n";}
    if (isset($_GET['09'])) {$cfgstr .= "09_eth_enabled=" .        $_GET['09'] . "\n";}
    if (isset($_GET['10'])) {$cfgstr .= "10_vpn_enabled=" .        $_GET['10'] . "\n";}
    if (isset($_GET['11'])) {$cfgstr .= "11_vpn_string=" .         $_GET['11'] . "\n";}
    if (isset($_GET['12'])) {$cfgstr .= "12_vpn_pingstring=" .     $_GET['12'] . "\n";}
    if (isset($_GET['13'])) {$cfgstr .= "13_vpn_nickname=" .       $_GET['13'] . "\n";}
    if (isset($_GET['14'])) {$cfgstr .= "14_vpn_email=" .          $_GET['14'] . "\n";}
    if (isset($_GET['15'])) {$cfgstr .= "15_vpn_wallet_id=" .      $_GET['15'] . "\n";}
    if (isset($_GET['16'])) {$cfgstr .= "16_vpn_port=" .           $_GET['16'] . "\n";}
    if (isset($_GET['17'])) {$cfgstr .= "17_vpn_purge=" .          $_GET['17'] . "\n";}
    if (isset($_GET['18'])) {$cfgstr .= "18_proxy_enabled_eth0=" . $_GET['18'] . "\n";}
    if (isset($_GET['19'])) {$cfgstr .= "19_proxy_enabled_wlan0=". $_GET['19'] . "\n";}
    if (isset($_GET['20'])) {$cfgstr .= "20_proxy_enabled_ap=" .   $_GET['20'] . "\n";}
    if (isset($_GET['21'])) {$cfgstr .= "21_vpn_json_post=".       $_GET['21'] . "\n";}
    if (isset($_GET['22'])) {$cfgstr .= "22_nft_index=".           $_GET['22'] . "\n";}
    if (isset($_GET['94'])) {$cfgstr .= "94_TIMEZONE=" .           $_GET['94'] . "\n";}

    $cfgstr = str_replace('*', '', $cfgstr);
    
    write_cfg($cfgstr);
    $command = "C";
    write_todo($command);

    $user_setting_status = "Bad Password";
    if ($ShieldSetting_01 == $oldpass){
      $user_setting_status = "Settings written";
    }
  }

  $reply = array();
  $reply["SHIELD_allstates"] = $SHIELD_allstates;
  $reply["SHIELD_state"] = $SHIELD_state;
  $reply["SHIELD_now_time"] = $SHIELD_now_time;
  $reply["SHIELD_build_time"] = $SHIELD_build_time;
  $reply["SHIELD_build_date"] = $SHIELD_build_date;
  $reply["SHIELD_build_version"] = $SHIELD_build_version;
  $reply["SHIELD_wlan0_lbl"] = $SHIELD_apmode;
  $reply["SHIELD_wlan0"] = $SHIELD_wlan0;
  $reply["SHIELD_eth0"] = $SHIELD_eth0;
  $reply["SHIELD_vpn"] = $SHIELD_vpn;
  $reply["SHIELD_enc"] = $SHIELD_enc;
  $reply["SHIELD_network"] = $SHIELD_network;
  $reply["SHIELD_soctemp"] = $SHIELD_soctemp;
  $reply["SHIELD_gputemp"] = $SHIELD_gputemp;
  $reply["SHIELD_fan"] = $SHIELD_fan;
  $reply["SHIELD_testindex"] = $SHIELD_testindex;
  $reply["SHIELD_configstate"] = $SHIELD_configstate;
  $reply["SHIELD_pid"] = $SHIELD_pid;
  $reply["SHIELD_start"] = $SHIELD_start;
  $reply["SHIELD_ver_os"] = $SHIELD_ver_os;
  $reply["SHIELD_ver_vpn"] = $SHIELD_ver_vpn;
  $reply["SHIELD_vpnorport"] = $SHIELD_vpnorport;
  $reply["SHIELD_serial"] = $SHIELD_serial;
  $reply["SHIELD_atecid"] = $SHIELD_atecid;
  $reply['SHIELD_ipwlan0'] = $SHIELD_ipwlan0;
  $reply['SHIELD_ipeth0'] = $SHIELD_ipeth0;
  $reply['SHIELD_ippublic'] = $SHIELD_ippublic;
  $reply['SHIELD_fngrprnt'] = $SHIELD_fngrprnt;
  $reply['SHIELD_devicePUBKEY'] = $SHIELD_devicePUBKEY;
  $reply['SHIELD_ver_BL'] = $SHIELD_ver_BL;
  $reply['SHIELD_ver_FW'] = $SHIELD_ver_FW;
  $reply['SHIELD_web'] = $SHIELD_web;
  $reply['Fingerprints'] = $MyFamily;

  if($q == 7){
        // Initialize an array to hold the ShieldSetting key-value pairs
        $shieldSettings = array();
        // Assign specific variables to the $shieldSettings array
        $shieldSettings["AccountingEnabled"] = $accountingEnabled;
        $shieldSettings["AccountingMax"] = $accountingMax;
        $shieldSettings["AccountingStart"] = $accountingStart;
        $shieldSettings["RelayBandwidthRate"] = $relayBandwidthRate;
        $shieldSettings["ShieldSetting_00"] = $ShieldSetting_00;
        $shieldSettings["ShieldSetting_02"] = $ShieldSetting_02;
        $shieldSettings["ShieldSetting_03"] = $ShieldSetting_03;
        $shieldSettings["ShieldSetting_04"] = $ShieldSetting_04;
        $shieldSettings["ShieldSetting_05"] = $ShieldSetting_05;
        $shieldSettings["ShieldSetting_06"] = $ShieldSetting_06;
        $shieldSettings["ShieldSetting_07"] = $ShieldSetting_07;
        $shieldSettings["ShieldSetting_08"] = $ShieldSetting_08;
        $shieldSettings["ShieldSetting_09"] = $ShieldSetting_09;
        $shieldSettings["ShieldSetting_10"] = $ShieldSetting_10;
        $shieldSettings["ShieldSetting_11"] = $ShieldSetting_11;
        $shieldSettings["ShieldSetting_12"] = $ShieldSetting_12;
        $shieldSettings["ShieldSetting_13"] = $ShieldSetting_13;
        $shieldSettings["ShieldSetting_14"] = $ShieldSetting_14;
        $shieldSettings["ShieldSetting_15"] = $ShieldSetting_15;
        $shieldSettings["ShieldSetting_16"] = $ShieldSetting_16;
        $shieldSettings["ShieldSetting_17"] = $ShieldSetting_17;
        $shieldSettings["ShieldSetting_18"] = $ShieldSetting_18;
        $shieldSettings["ShieldSetting_19"] = $ShieldSetting_19;
        $shieldSettings["ShieldSetting_20"] = $ShieldSetting_20;
        $shieldSettings["ShieldSetting_21"] = $ShieldSetting_21;
        $shieldSettings["ShieldSetting_22"] = $ShieldSetting_22;
        $shieldSettings["ShieldSetting_23"] = $ShieldSetting_23;
        $shieldSettings["ShieldSetting_24"] = $ShieldSetting_24;
        $shieldSettings["ShieldSetting_25"] = $ShieldSetting_25;
        $shieldSettings["ShieldSetting_26"] = $ShieldSetting_26;
        $shieldSettings["ShieldSetting_27"] = $ShieldSetting_27;
        $shieldSettings["ShieldSetting_28"] = $ShieldSetting_28;
        $shieldSettings["ShieldSetting_29"] = $ShieldSetting_29;
        // Add the $shieldSettings array to the $reply array under the "Settings" key
        $reply["Settings"] = $shieldSettings;
        $user_setting_status = " ";
  }
  
  $reply["user_setting_status"] = $user_setting_status;
  $reply["VER_home_css"] = $VER_home_css;
  $reply["VER_home"] = $VER_home;
  $reply["VER_css"] = $VER_index_css;
  $reply["VER_index"] = $VER_index;
  $reply["VER_info_css"] = $VER_info_css;
  $reply["VER_info"] = $VER_info;
  $reply["VER_logs_css"] = $VER_logs_css;
  $reply["VER_logs"] = $VER_logs;
  $reply["VER_settings_css"] = $VER_settings_css;
  $reply["VER_settings"] = $VER_settings;
  $reply["VER_style_css"] = $VER_style_css;
  $reply["VER_utils_js"] = $VER_utils_js;
  $reply["VER_login"] = $VER_login;
  $reply["VER_logintest"] = $VER_logintest;
  $reply["VER_readsettings"] = $VER_readsettings;
  $reply["VER_js"] = $VER_ajax_js;
  $reply["VER_ajax"] = $VER_ajax_php;
  $reply["VER_vers"] = $VER_vers;
  $reply["VER_webstatus"] = $VER_webstatus;
  $reply["VER_app"] = $VER_app;
  $reply["VER_os"] = $VER_os;
  $reply["VER_vpn"] = $VER_vpn;
  $reply["unsaved_settings"] = $Unsaved_Settings;

  header('Content-Type: application/json');
  echo json_encode($reply);
  exit;

?>