<?php 


/**
 * file: ethernet.min.php
 *
 * Copyright Miller Electric Mfg. Co. as an unpublished work.
 * All rights reserved.
 *
 * The information contained herein is confidential property of
 * Miller Electric Mfg. Co.  The use of, copying, transfer or
 * disclosure of such information is prohibited except by express
 * written agreement with Miller Electric.
 */


session_start();


require_once('../lib/user_authentication.php');
require_once('../lib/utility.php');


if(!UserAuthentication::isUserLoggedIn()) {
	navigateToLoginPage();
}


// Get Ethernet MAC  - perform a grep on ifconfig shell command and extract the Ethernet MAC
// $ifconfig_grep_string = "ifconfig | grep eth0";
// $ifgrep_out = exec($ifconfig_grep_string);
// $search_string = "HWaddr ";
// $EthMAC = substr($ifgrep_out, strpos($ifgrep_out, $search_string) + strlen($search_string));


// Get network settings
require('network_settings.php');
//echo "networkSettings:<br>";
//print_r($networkSettings);


$settingsFilePath = "/Insight/devicePage2SBC.conf";

$defaultNetworkSettings = array(
	"SSID" => "",
    "Authentication" => "WPA2",
    "Encryption" => "AES",
    "Passkey" => "",
    "WiFiCredentialsChanged" => false,
    "EthDHCP" => "dynamic",
    "EthIP" => [],
    "EthSubnet" => [],
    "EthGateway" => [],
    "EthDNS" => "dynamic",
    "EthDNSAddr" => [],
    "WiFiDHCP" => "dynamic",
    "WiFiIP" => [],
    "IPSettingsChanged" => false,
    "WiFiSubnet" => [],
    "WiFiGateway" => [],
    "WiFiDNS" => "dynamic",
    "WiFiDNSAddr" => []
);

$networkSettings = array();


$EthDHCP = $defaultNetworkSettings['EthDHCP'];
$EthIP = $defaultNetworkSettings['EthIP'];
$EthSubnet = $defaultNetworkSettings['EthSubnet'];
$EthGateway = $defaultNetworkSettings['EthGateway'];
$EthDNS = $defaultNetworkSettings['EthDNS'];
$EthDNSAddr = $defaultNetworkSettings['EthDNSAddr'];
$EthMAC = '';

function getNetworkSettings() {
	global $settingsFilePath;
	global $defaultNetworkSettings;
	global $networkSettings;
	global $EthDHCP;
	global $EthIP;
	global $EthMAC;
	global $EthSubnet;
	global $EthGateway;
	global $EthDNS;
	global $EthDNSAddr;

	getJsonArrayFromFile($settingsFilePath, true, $networkSettings);

	if($networkSettings != NULL)
	{
		$networkSettings = array_merge($defaultNetworkSettings, $networkSettings);
	}

	$interface = NetworkInterfaceInformation::fromInterfaceName("eth1");

	$networkSettings["EthIP"] = $interface->ipAddress;
	$networkSettings["EthDHCP"] = $interface->dhcp;
	$networkSettings["EthSubnet"] = $interface->subnetMask;
	$networkSettings["EthGateway"] = $interface->gateway;
	$EthMAC = $interface->macAddress;
	$networkSettings["EthDNSAddr"] = $interface->dnsServer;
	$networkSettings["EthDNS"] = $interface->isDnsSet || ($interface->dhcp === "static") ? "static" : "dynamic";

	$EthDHCP = $networkSettings['EthDHCP'] ?? null;
	$EthIP = $networkSettings['EthIP'] ?? null;
	$EthSubnet = $networkSettings['EthSubnet'] ?? null;
	$EthGateway = $networkSettings['EthGateway'] ?? null;
	$EthDNS = $networkSettings['EthDNS'] ?? null;
	$EthDNSAddr = $networkSettings['EthDNSAddr'] ?? null;
}

// Process the form data on submission.
if($_SERVER['REQUEST_METHOD'] == 'POST') {
	UpdateSBCSettings();
	$_SERVER['IpRetries'] = 0;
	echo 'post';
}



?>


<!DOCTYPE html>
<script type="text/javascript" >


function InitialSettings(dhcp, dns)
{
	DHCP_Toggle(dhcp);
	DNS_Toggle(dns);
}


function DHCP_Toggle(selection)
{
	var static_button = document.getElementById("static_b");
	var dynamic_button = document.getElementById("dynamic_b");
	var DHCP_enabled = dynamic_button.classList.contains("btn-warning");
	
	if(selection == "static")
	{
		dynamic_button.classList.remove("btn-warning");
		dynamic_button.classList.add("btn-default");
		static_button.classList.remove("btn-default");
		static_button.classList.add("btn-warning");
		document.getElementById("EthDHCP").value = "static";
		document.getElementById("dhcp_auto_b").style.display = "none";
		document.getElementById("specific_server_b").style.display = "block";
		DHCP_FieldsEnable(true);
		DNS_Toggle(selection);
	}
	else
	{
		static_button.classList.remove("btn-warning");
		static_button.classList.add("btn-default");
		dynamic_button.classList.remove("btn-default");
		dynamic_button.classList.add("btn-warning");
		document.getElementById("EthDHCP").value = "dynamic";
		document.getElementById("dhcp_auto_b").style.display = "block";
		document.getElementById("specific_server_b").style.display = "block";
		DHCP_FieldsEnable(false);
	}
}


function DHCP_FieldsEnable(enable)
{
	var index;
	var id_string = "spinEdit";
	for(index = 17; index <= 28; index++)
	{
		var id = id_string.concat(index);
		document.getElementById(id).disabled = !enable;
	}
}


function DNS_Toggle(selection)
{
	var static_button = document.getElementById("specific_server_b");
	var dynamic_button = document.getElementById("dhcp_auto_b");
	var DHCP_enabled = dynamic_button.classList.contains("btn-warning");
	
	if(selection == "static")
	{
		dynamic_button.classList.remove("btn-warning");
		dynamic_button.classList.add("btn-default");
		static_button.classList.remove("btn-default");
		static_button.classList.add("btn-warning");
		document.getElementById("EthDNS").value = "static";
		DNS_FieldsEnable(true);
	}
	else
	{
		static_button.classList.remove("btn-warning");
		static_button.classList.add("btn-default");
		dynamic_button.classList.remove("btn-default");
		dynamic_button.classList.add("btn-warning");
		document.getElementById("EthDNS").value = "dynamic";
		DNS_FieldsEnable(false);
	}
}


function DNS_FieldsEnable(enable)
{
	var index;
	var id_string = "spinEdit";
	for(index = 37; index <= 40; index++)
	{
		var id = id_string.concat(index);
		document.getElementById(id).disabled = !enable;
	}
}


</script>


<html lang="en">
  <?php 
	// Uncomment to display errors and warnings in browser
	// ini_set('display_errors', '1');
	// ini_set('display_startup_errors', '1');
	// error_reporting(E_ALL);

	require('head.php'); 

	// Need to get the new settings after the head.php script executes.
	getNetworkSettings(); 

	// If the loaded ip address is invalid, refresh.
	if (!isset($_SESSION['IpRetries'])) {
		$_SESSION['IpRetries'] = 0;
	}

	if (in_array(-1, $EthIP)) {
		if ($_SESSION['IpRetries'] < 4) {
			$_SESSION['IpRetries'] += 1;
			$page = $_SESSION['PHP_SELF'];
			echo '<meta http-equiv="Refresh" content="0;' . $page . '">';
		} else {
			echo 'Unable to use current settings.';
		}
	}
  ?>
  <body onload="InitialSettings('<?php echo $EthDHCP; ?>', '<?php echo $EthDNS; ?>')">
    <!-- Modal -->
        <div class="modal fade bs-example-modal-sm" id="invalidAddressModal" tabindex="-1" role="dialog" aria-labelledby="invalidAddressLabel" aria-hidden="true">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header bg-info">
                <button type="button" id="close" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="invalidAddressLabel"><span class="glyphicon glyphicon-info-sign"></span> Information</h4>
              </div>
              <div class="modal-body">
                <h4 id="second_important" class="bg-warning"><span class="glyphicon glyphicon-warning-sign"></span> Important!</h4>
                <p id="empty_fields">One or more of the entered fields is invalid.  Please re-enter all required fields with valid information.</p>  
              </div>
              <div class="modal-footer">
                <ul class="pager">
                    <li class="cancel"><a id="second_close" href="#" data-dismiss="modal">Close <span class="glyphicon glyphicon-ban-circle"></span><span class="sr-only">(current)</span></a></li>
                </ul>
              </div>
            </div>
          </div>
        </div>
      <div class="container">
        <div class="panel panel-primary">
          <div class="panel-body">
            <?php require('navbar_header.php'); ?>
             <div class="panel">
                <div class="panel-heading">
                    <h3 id="another_important_header" class="panel-title"><span class="glyphicon glyphicon-warning-sign"></span> IMPORTANT!</h3>
                </div>
                <div class="panel-body bg-warning">
                    <p id="modification" class="bg-warning">Modifying these values without an understanding of Ethernet settings may result in the web interface being unreachable.</p>
                    <p id="local_it" class="bg-warning">Please contact your local IT expert if you are unsure of how to proceed.</p>
                </div>
              </div>
              <div class="panel panel-primary">
                    <div class="panel-heading">
                        <h3 id="ethernet_setup_header" class="panel-title"><span class="glyphicon glyphicon-cog"></span> Ethernet Setup</h3>
                    </div>
                    <div class="panel-body">
                        
                            <div class="row">
                                <div class="col-md-8 col-md-offset-2">
                                                <form action="/setup/ethernet.min.php" enctype="multipart/form-data" method="post" id="cont_ethernet" name="cont_ethernet">
								
                                    <div class="panel panel-info">
                                        <div class="panel-heading">
                                            <h3 id="ip_adress_header" class="panel-title"><span class="glyphicon glyphicon-info-sign"></span> Ethernet IP Address</h3>
                                        </div>
                                        <div class="panel-body">
                                            <div class="row">
                                                <div class="col-md-12">
                                                    <div class="btn-group">
                                                        <div class="btn-group btn-toggle" data-toggle="buttons">
                                                            <label label-default="" class="btn btn-default" id="static_b"  onclick = "DHCP_Toggle('static');">
                                                                Static
                                                            </label>
                                                            <label label-default="" class="btn btn-warning" id="dynamic_b" onclick = "DHCP_Toggle('dynamic');">
                                                                Dynamic
                                                            </label>
                                                            <input class="hidden_values" type="hidden" name="EthDHCP" id="EthDHCP" value="" />
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-md-12">
                                                    &nbsp;
                                                </div>
                                            </div>
                                            <div class="panel panel-primary">
											<h1 id="status"></h1>
                                                <div class="panel-heading">
                                                    <h3 id="network_header" class="panel-title"><span class="glyphicon glyphicon-info-sign"></span> Network Information</h3>
                                                </div>
                                                <div class="panel-body">
													<div class="row">
													<div class="col-md-12">
														<label id="ip_address_label" for="ipaddress" class="col-md-4 control-label numonly">IP Address</label>
													</div>
													</div>
													<div class="row">
														<div class="col-md-12">
															<div class="col-sm-3">
																<label class="control-label" for="spinEdit17" id="Label16"></label>
																<input type="number" id="spinEdit17" class="SpinEdit form-control" name="EthIP[0]" value="<?php echo $EthIP[0] ?>" step="1" min="0" max="255"/>
																<input class="hidden_values" type="hidden" name="IPSettingsChanged" id="IPSettingsChanged" value="true" />
															</div>
															<div class="col-sm-3">
																<label class="control-label" for="spinEdit18" id="Label17"></label>
																<input type="number" id="spinEdit18" class="SpinEdit form-control" name="EthIP[1]" value="<?php echo $EthIP[1] ?>" step="1" min="0" max="255"/>
															</div>
															<div class="col-sm-3">
																<label class="control-label" for="spinEdit19" id="Label18"></label>
																<input type="number" id="spinEdit19" class="SpinEdit form-control" name="EthIP[2]" value="<?php echo $EthIP[2] ?>" step="1" min="0" max="255"/>
															</div>
															<div class="col-sm-3">
																<label class="control-label" for="spinEdit20" id="Label19"></label>
																<input type="number" id="spinEdit20" class="SpinEdit form-control" name="EthIP[3]" value="<?php echo $EthIP[3] ?>" step="1" min="0" max="255"/>
															</div>
														</div>
													</div>
													<div class="row">
													<div class="col-md-12">
														&nbsp;
													</div>
													</div>
													<div class="row">
													<div class="col-md-12">
														<label id="subnet_label" for="subnetmask" class="col-md-4 control-label numonly">Subnet Mask</label>
													</div>
													</div>
													<div class="row">
														<div class="col-md-12">
															<div class="col-sm-3">
																<label class="control-label" for="spinEdit21" id="Label20"></label>
																<input type="number" id="spinEdit21" class="SpinEdit form-control" name="EthSubnet[0]" value="<?php echo $EthSubnet[0] ?>" step="1" min="0" max="255"/>
															</div>
															<div class="col-sm-3">
																<label class="control-label" for="spinEdit22" id="Label21"></label>
																<input type="number" id="spinEdit22" class="SpinEdit form-control" name="EthSubnet[1]" value="<?php echo $EthSubnet[1] ?>" step="1" min="0" max="255"/>
															</div>
															<div class="col-sm-3">
																<label class="control-label" for="spinEdit23" id="Label22"></label>
																<input type="number" id="spinEdit23" class="SpinEdit form-control" name="EthSubnet[2]" value="<?php echo $EthSubnet[2] ?>" step="1" min="0" max="255"/>
															</div>
															<div class="col-sm-3">
																<label class="control-label" for="spinEdit24" id="Label23"></label>
																<input type="number" id="spinEdit24" class="SpinEdit form-control" name="EthSubnet[3]" value="<?php echo $EthSubnet[3] ?>" step="1" min="0" max="255"/>
															</div>
														</div>
													</div>
													<div class="row">
													<div class="col-md-12">
														&nbsp;
													</div>
													</div>
													<div class="row">
													<div class="col-md-12">
														<label id="gateway_label" for="networkgateway" class="col-md-12 control-label numonly">Network Gateway</label>
													</div>
													</div>
													<div class="row">
														<div class="col-md-12">
															<div class="col-sm-3">
																<label class="control-label" for="spinEdit25" id="Label24"></label>
																<input type="number" id="spinEdit25" class="SpinEdit form-control" name="EthGateway[0]" value="<?php echo $EthGateway[0] ?>" step="1" min="0" max="255"/>
															</div>
															<div class="col-sm-3">
																<label class="control-label" for="spinEdit26" id="Label25"></label>
																<input type="number" id="spinEdit26" class="SpinEdit form-control" name="EthGateway[1]" value="<?php echo $EthGateway[1] ?>" step="1" min="0" max="255"/>
															</div>
															<div class="col-sm-3">
																<label class="control-label" for="spinEdit27" id="Label26"></label>
																<input type="number" id="spinEdit27" class="SpinEdit form-control" name="EthGateway[2]" value="<?php echo $EthGateway[2] ?>" step="1" min="0" max="255"/>
															</div>
															<div class="col-sm-3">
																<label class="control-label" for="spinEdit28" id="Label27"></label>
																<input type="number" id="spinEdit28" class="SpinEdit form-control" name="EthGateway[3]" value="<?php echo $EthGateway[3] ?>" step="1" min="0" max="255"/>
															</div>
														</div>
													</div>
													<div class="row">
													<div class="col-md-12">
														&nbsp;
													</div>
													</div>
                                            <div class="row">
                                                <div class="col-md-12">
                                                    &nbsp;
                                                </div>
                                            </div>
                                            <div class="panel panel-primary">
                                                <div class="panel-heading">
                                                    <h3 id="dns_header" class="panel-title">DNS Address</h3>
                                                </div>
                                                <div class="panel-body">
                                                    <div class="row">
                                                        <div class="col-md-12">
                                                            <div class="btn-group">
                                                                <div class="btn-group btn-toggle" data-toggle="buttons">
                                                                    <label label-default="" class="btn btn-default" id="dhcp_auto_b" onclick="DNS_Toggle('dynamic')">
                                                                        DHCP Automatic
                                                                    </label>
                                                                    <label label-default="" class="btn btn-warning" id="specific_server_b" onclick="DNS_Toggle('static')">
                                                                        DNS Server
                                                                    </label>
                                                                    <input class="hidden_values" type="hidden" name="EthDNS" id="EthDNS" value="" />
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="row">
                                                        <div class="col-md-12">
                                                            &nbsp;
                                                        </div>
                                                    </div>
                                                    <div id="dns1">
													<div class="row">
														<div class="col-md-12">
															<div class="col-sm-3">
																<label class="control-label" for="spinEdit37" id="Label36"></label>
																<input type="number" id="spinEdit37" class="SpinEdit form-control" name="EthDNSAddr[0]" value="<?php echo $EthDNSAddr[0] ?>" step="1" min="0" max="255"/>
															</div>
															<div class="col-sm-3">
																<label class="control-label" for="spinEdit38" id="Label37"></label>
																<input type="number" id="spinEdit38" class="SpinEdit form-control" name="EthDNSAddr[1]" value="<?php echo $EthDNSAddr[1] ?>" step="1" min="0" max="255"/>
															</div>
															<div class="col-sm-3">
																<label class="control-label" for="spinEdit39" id="Label38"></label>
																<input type="number" id="spinEdit39" class="SpinEdit form-control" name="EthDNSAddr[2]" value="<?php echo $EthDNSAddr[2] ?>" step="1" min="0" max="255"/>
															</div>
															<div class="col-sm-3">
																<label class="control-label" for="spinEdit40" id="Label39"></label>
																<input type="number" id="spinEdit40" class="SpinEdit form-control" name="EthDNSAddr[3]" value="<?php echo $EthDNSAddr[3] ?>" step="1" min="0" max="255"/>
															</div>
														</div>
													</div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-6 col-md-offset-4">
                                                <ul class="pager">
                                                    <li class="edit">
                                                        <a href="#" id="save_all" onclick="document.getElementById('cont_ethernet').submit();">
                                                            Save All 
                                                            <span class="glyphicon glyphicon-save"></span>
                                                            <span class="sr-only">(current)</span>
                                                        </a>
                                                    </li>
                                                </ul>
                                            </div>
                                        </div>
                                  </div> 
                              </div>
                        </div>
                       </form>
                  </div>
                </div>
            </div>
            <?php require('footer.php'); ?>
          </div>  
      </div>   
    <?php require('js_footer.php'); ?>
  </body>
</html>