<?php require './XX_logintest.php'; ?>
<?php

if (isset($_SERVER['HTTP_ORIGIN'])) {
  header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
  header('Access-Control-Allow-Credentials: true');
  header('Access-Control-Max-Age: 86400');    // cache for 1 day
}

// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

  if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
      header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

  if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
      header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

  exit(0);
}


  $seperator = "//\n";
  $q=$_GET["q"];
    
    
  //require './XX_versions.php';
  require './XX_webstatus.php';
  require './XX_readsettings.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";} 
  
//****************************************************
  // delete web config = W0
  // new_password = W1
  // ethernet = W2
  // wifi = W3
  // vpn nickname,email = W4
  // wallet addr = W5
  // port = W6

  $cfgstr = "";
  if($q == "W0"){    delete_cfg();    $command = $q;    write_todo($command);  }

  if($q == "W1"){
    if($ShieldSetting_01 == ""){$ShieldSetting_01 = "admin";}
    $cfgstr .= "01_user_password=" .  $ShieldSetting_01 . "\n";
    if (isset($_GET['02'])) {$cfgstr .= "02_user_newpassword=" .        $_GET['02'] . "\n";}
    $cfgstr = str_replace('*', '', $cfgstr);   
    write_cfg($cfgstr);
    $command = $q;
    write_todo($command);
  }

  if($q == "W2"){
    // check_vpnConfig();
    $settingfile = "./default.cfg";
//    if (!(file_exists($settingfile))) { 
      if($ShieldSetting_01 == ""){$ShieldSetting_01 = "admin";}
      $cfgstr .= "01_user_password=" .  $ShieldSetting_01 . "\n";
//    }
    if (isset($_GET['09'])) {$cfgstr .= "09_eth_enabled=" .        $_GET['09'] . "\n";}
    $cfgstr = str_replace('*', '', $cfgstr);   
    write_cfg($cfgstr);

    $command = $q;
    write_todo($command);
  }

  if($q == "W3"){
    // check_vpnConfig();
    $SSID = "";
    if (isset($_GET['05'])) { $SSID = $_GET['05']; }
    if($SSID == ""){
      $cfgstr .= "03_wifi_state=off\n";
      $cfgstr .= "04_wifi_mode=ap\n";
    } else {
      $cfgstr .= "03_wifi_state=on\n";
      $cfgstr .= "04_wifi_mode=join\n";
    }      
    $cfgstr .= "05_wifi_ssid_join=" . $SSID . "\n";
    if (isset($_GET['06'])) {$cfgstr .= "06_wifi_password_join=" . $_GET['06'] . "\n";}
    $cfgstr = str_replace('*', '', $cfgstr);   
    append_cfg($cfgstr);
    $command = $q;
    write_todo($command);
  }

  if($q == "W4"){
    // check_vpnConfig();
    if($ShieldSetting_01 == ""){$ShieldSetting_01 = "admin";}
    $cfgstr .= "01_user_password=" .  $ShieldSetting_01 . "\n";
    if (isset($_GET['13'])) {$cfgstr .= "13_vpn_nickname=" . 	$_GET['13'] . "\n";}
    if (isset($_GET['14'])) {$cfgstr .= "14_vpn_email=" .     	$_GET['14'] . "\n";}
    $cfgstr = str_replace('*', '', $cfgstr);   
    append_cfg($cfgstr);
    echo $cfgstr;
    $command = $q;
    write_todo($command);
  }

  if($q == "W5"){
    // check_vpnConfig();
    $nick = $_GET['13'];
    $addy = $_GET['15'];
    echo $addy;
    
    if (isset($_GET['15'])) {$cfgstr .= "15_vpn_wallet_id=" . 	$_GET['15'] . "\n";};
    check_vpnConfig($nick,$addy);
    $cfgstr = str_replace('*', '', $cfgstr);   
    append_cfg($cfgstr);
    $command = $q;
    write_todo($command);
  }

  if($q == "W6"){
    // check_vpnConfig();
    if($ShieldSetting_01 == ""){$ShieldSetting_01 = "admin";}
    if (isset($_GET['16'])) {$cfgstr .= "16_vpn_port=" . 	$_GET['16'] . "\n";}
    $cfgstr .= "10_vpn_enabled=on\n";
    if (isset($_GET['11'])) {$cfgstr .= "11_vpn_string=" .         $_GET['11'] . "\n";}
    if (isset($_GET['12'])) {$cfgstr .= "12_vpn_pingstring=" .     $_GET['12'] . "\n";}
    $cfgstr = str_replace('*', '', $cfgstr);   
    append_cfg($cfgstr);
    $command = $q;
    write_todo($command);
  }

  if($q == "W7"){    $command = $q;    write_todo($command);  }

  //****************************************************

  if($q == 1){    $command = "R";    write_todo($command);  }
  if($q == 2){    $command = "r";    write_todo($command);  }
  if($q == 3){    $command = "H";    if ($SHIELD_apmode == "HOTSPOT:"){$command = "h";}    write_todo($command);  }
  if($q == 4){    $command = "F";    if ($SHIELD_fan == "On"){$command = "f";}             write_todo($command);  }

  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'];
      }
    }    

    //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['94'])) {$cfgstr .= "94_TIMEZONE=" .           $_GET['94'] . "\n";}
    //if (isset($_GET['95'])) {$cfgstr .= "95_SHELL=" .              $_GET['95'] . "\n";}

    
    //94_TIMEZONE=Europe/London
    //95_SHELL=apt-get -y update
    //95_SHELL=timedatectl set-timezone Europe/London
    //95_SHELL=ls

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

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

  $reply = $seperator;
  $reply .= "SHIELD_allstates=$SHIELD_allstates$seperator";
  $reply .= "SHIELD_state=$SHIELD_state$seperator";

  $reply .= "SHIELD_now_time=$SHIELD_now_time$seperator";
  $reply .= "SHIELD_build_time=$SHIELD_build_time$seperator";
  $reply .= "SHIELD_build_date=$SHIELD_build_date$seperator";
  $reply .= "SHIELD_build_version=$SHIELD_build_version$seperator";
  $reply .= "SHIELD_wlan0_lbl=$SHIELD_apmode$seperator";

  $reply .= "SHIELD_wlan0=$SHIELD_wlan0$seperator";
  $reply .= "SHIELD_eth0=$SHIELD_eth0$seperator";
  $reply .= "SHIELD_vpn=$SHIELD_vpn$seperator";
  $reply .= "SHIELD_enc=$SHIELD_enc$seperator";
  $reply .= "SHIELD_network=$SHIELD_network$seperator";

  $reply .= "SHIELD_soctemp=$SHIELD_soctemp$seperator";
  $reply .= "SHIELD_gputemp=$SHIELD_gputemp$seperator";
  $reply .= "SHIELD_fan=$SHIELD_fan$seperator";
  $reply .= "SHIELD_testindex=$SHIELD_testindex$seperator";
  $reply .= "SHIELD_configstate=$SHIELD_configstate$seperator";
  $reply .= "SHIELD_pid=$SHIELD_pid$seperator";
  $reply .= "SHIELD_start=$SHIELD_start$seperator";
  $reply .= "SHIELD_ver_os=$SHIELD_ver_os$seperator";
  $reply .= "SHIELD_ver_vpn=$SHIELD_ver_vpn$seperator";

  if($q == 7){    $reply .= $SheildSettings;    $user_setting_status = " ";  }
  if($q == 8){    $command = "S";    write_todo($command);  }
  
  $reply .= "user_setting_status=$user_setting_status$seperator";

  if($q == 6){$reply .= "Hello 6"; }
  
  echo $reply;
  exit;

  function write_todo($dothis){
    $myfile = fopen("./00_dothis.txt", "w") or die("Unable to open file!");
    fwrite($myfile, $dothis);
    fclose($myfile);  
  }

  function delete_cfg(){
    $settingfile = "./default.cfg";
    if (file_exists($settingfile)) { 
      unlink($settingfile);
    }
  }

  function write_cfg($dothiscfg){
    // $check = "woah";
    $settingfile = "./default.cfg";
    $myfile = fopen($settingfile, "w") or die("Unable to open file!");
    fwrite($myfile, $dothiscfg);
    // echo $check;
    fclose($myfile);  
  }

  function append_cfg($dothiscfg){
    $settingfile = "./default.cfg";
    if (file_exists($settingfile)) { 
      $myfile = fopen($settingfile, "a") or die("Unable to open file!");
    } else {
      $myfile = fopen($settingfile, "w") or die("Unable to open file!");
    }
    fwrite($myfile, $dothiscfg);
    fclose($myfile);  
  }

  function check_vpnConfig($par1, $par2){
    
    // if (!isset($apppath2)) {
    $apppath = "/home/relayup/";
    // }

    $vpnConfigFile = $apppath . 'dev/vpn_config.cfg';

    $defaultContent = "BandwidthRate 100 Mbits\nBandwidthBurst 120 Mbits\n";

    if ($par1) {
      $defaultContent .= "Nickname " . $par1 . "\n";
    }

    if ($par2) {
      $defaultContent .= "ContactInfo " . " @anon: " . $par2 . "\n";
    }
    
    $defaultContent .= "ShutdownWaitLength 10\nMetricsPort 127.0.0.1:8080\nMetricsPortPolicy accept 127.0.0.1\nControlPort 9051\n";

    $result = file_put_contents($vpnConfigFile, $defaultContent) or die("Unable to create file: $vpnConfigFile");

  }
  
?>