<?php 


/**
 * file: wifi.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.
 */

error_reporting(E_ALL);
ini_set("display_errors", 1);

session_start();


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


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


// 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();

$SSID = $defaultNetworkSettings['SSID'];
$Passkey = $defaultNetworkSettings['Passkey'];
$Authentication = $defaultNetworkSettings['Authentication'];
$Encryption = $defaultNetworkSettings['Encryption'];

$WiFiDHCP = $defaultNetworkSettings['WiFiDHCP'];
$WiFiIP = $defaultNetworkSettings['WiFiIP'];
$WiFiSubnet = $defaultNetworkSettings['WiFiSubnet'];
$WiFiGateway = $defaultNetworkSettings['WiFiGateway'];
$WiFiDNS = $defaultNetworkSettings['WiFiDNS'];
$WiFiDNSAddr = $defaultNetworkSettings['WiFiDNSAddr'];
$WiFi_MAC = '';


function getNetworkSettings() {
	global $settingsFilePath;
	global $defaultNetworkSettings;
	global $networkSettings;
	global $SSID;
	global $Passkey;
	global $Authentication;
	global $Encryption;
	global $WiFiDHCP;
	global $WiFi_MAC;
	global $WiFiIP;
	global $WiFiSubnet;
	global $WiFiGateway;
	global $WiFiDNS;
	global $WiFiDNSAddr;
	
	getJsonArrayFromFile($settingsFilePath, true, $networkSettings);
	
	if($networkSettings != NULL)
	{
		$networkSettings = array_merge($defaultNetworkSettings, $networkSettings);
	}

	$interface = NetworkInterfaceInformation::fromInterfaceName("wlan0");
	
	if($networkSettings != NULL)	
	{
		$networkSettings = array_merge($defaultNetworkSettings, $networkSettings);
	}
	$networkSettings["WiFiIP"] = $interface->ipAddress;
	$networkSettings["WiFiDHCP"] = $interface->dhcp;
	$networkSettings["WiFiSubnet"] = $interface->subnetMask;
	$networkSettings["WiFiGateway"] = $interface->gateway;
	$WiFi_MAC = $interface->macAddress;
	$networkSettings["WiFiDNSAddr"] = $interface->dnsServer;
	$networkSettings["WiFiDNS"] = $interface->isDnsSet || ($interface->dhcp === "static") ? "static" : "dynamic";
	
	$SSID = $networkSettings['SSID'] ?? null;
	$Passkey = $networkSettings['Passkey'] ?? null;
	$Authentication = $networkSettings['Authentication'] ?? null;
	$Encryption = $networkSettings['Encryption'] ?? null;
	
	$WiFiDHCP = $networkSettings['WiFiDHCP'] ?? null;
	$WiFiIP = $networkSettings['WiFiIP'] ?? null;
	$WiFiSubnet = $networkSettings['WiFiSubnet'] ?? null;
	$WiFiGateway = $networkSettings['WiFiGateway'] ?? null;
	$WiFiDNS = $networkSettings['WiFiDNS'] ?? null;
	$WiFiDNSAddr = $networkSettings['WiFiDNSAddr'] ?? null;
}


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


?>


<!DOCTYPE html>
<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 (!isset($_SESSION['IpRetries'])) {
		$_SESSION['IpRetries'] = 0;
	}

	// If the loaded ip address is invalid, refresh.
	if (in_array(-1, $WiFiIP)) {
		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.';
		}
	}
  ?>
<script type="text/javascript" >


function InitialSettings(dhcp, dns)
{
	DHCP_Toggle(dhcp);
	DNS_Toggle(dns);
	//document.getElementById("Passkey").value = "";
	UpdateFields();
}


function DHCP_Toggle(selection)
{
	console.log("dhcp toggle");
	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("WiFiDHCP").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("WiFiDHCP").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("WiFiDNS").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("WiFiDNS").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;
	}
}


function UpdateFields()
{
	// Show Encryption block only if authentication is WPA or WPA2
	if(document.getElementById("Authentication").value == "WPA" || 
		document.getElementById("Authentication").value == "WPA2")
	{
		document.getElementById("EncryptionBlock").style.visibility = "visible";
	}
	else 
	{
		document.getElementById("EncryptionBlock").style.visibility = "collapse";
	}
}


</script> 
  	<body onload="$('.selectpicker').selectpicker();  InitialSettings('<?php echo $WiFiDHCP; ?>', '<?php echo $WiFiDNS; ?>');">
    	<!-- Modal -->
		<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="third_important" 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 WiFi settings may result in the unit 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="wifi_header" class="panel-title"><span class="glyphicon glyphicon-cog"></span> WiFi Setup</h3>
						</div>
						<div class="panel-body">
							<div class="row">
								<div class="col-md-8 col-md-offset-2">
									<div class="panel panel-info">
										<div class="panel-heading">
											<h3 id="wifi_info_header" class="panel-title"><span class="glyphicon glyphicon-info-sign"></span> WiFi Information</h3>
										</div>
										<div class="panel-body">
											<h5 id="mac_b">MAC:  <?PHP echo $WiFi_MAC; ?></h5>
										</div>
									</div>
								</div>
							</div>
							<div class="row">
								<form action="/setup/wifi.min.php" enctype="multipart/form-data" method="post" id="cont_wifi_security" name="cont_wifi_security">
									<div class="col-md-8 col-md-offset-2">
										<div class="panel panel-primary">
											<div class="panel-heading">
												<h3 id="security_header" class="panel-title"><span class="glyphicon glyphicon-cog"></span> Security Setup</h3>
											</div>
											<div class="panel-body">
												<div class="row">
													<label for="asset" class="col-md-4 control-label">SSID</label>
													<div class="col-md-8">
														<input id="SSID"  style="text-align:left" maxlength="32" class="form-control" name="SSID" value="<?php echo $SSID; ?>"/>
													</div>
												</div>
												<div class="row">
													&nbsp;
												</div>
												<div class="row">
													<label id="authentication" for="authenticationTypes" class="col-md-4 control-label">Authentication</label>
													<div class="col-md-8">
														<select class="selectpicker" id="Authentication" name="Authentication" data-style="btn-primary" onchange="UpdateFields()">
															<option id="none" value="None" <?php if($Authentication == "None") echo "selected=\"selected\""; ?>>None</option>
															<option id="wpa" value="WPA" <?php if($Authentication == "WPA") echo "selected=\"selected\""; ?>>WPA</option>
															<option id="wpa2" value="WPA2" <?php if($Authentication == "WPA2") echo "selected=\"selected\""; ?>>WPA2</option>
														</select>
													</div>
												</div>
												<div class="row">
													&nbsp;
												</div>
												<div class="row" id="EncryptionBlock">
													<label id="encryption" for="encryptionTypes" class="col-md-4 control-label">Encryption</label>
													<div class="col-md-8">
														<select class="selectpicker" id="Encryption" name="Encryption" data-style="btn-primary">
															<option value="AES" <?php if($Encryption == "AES") echo "selected=\"selected\""; ?>>AES</option>
															<option value="TKIP" <?php if($Encryption == "TKIP") echo "selected=\"selected\""; ?>>TKIP</option>
														</select>
													</div>
												</div>
												<div class="row">
													&nbsp;
												</div>
												<div class="row">
													<label id="password" for="wifi_password" class="col-md-4 control-label">Password</label>
													<div class="col-md-8">
														<input id="wifi_password" style="text-align:left" type="password" maxlength="64" class="form-control" name="Passkey" value="<?php echo $Passkey; ?>"/>
														<!--input class="hidden_values" type="hidden" name="WiFiDHCP" id="WiFiDHCP" value="" /-->
														<input class="hidden_values" type="hidden" name="WiFiCredentialsChanged" id="WiFiCredentialsChanged" value="true" />
													</div>
												</div>
												<div class="row">
													&nbsp;
												</div>
												<div class="row">
													<div class="col-md-6 col-md-offset-4">
														<ul class="pager">
															<li class="edit">
																<a id="security_change" href="#" onclick="document.getElementById('cont_wifi_security').submit();">
																	Save Security Settings 
																	<span class="glyphicon glyphicon-save"></span>
																	<span class="sr-only">(current)</span>
																</a>
															</li>
														</ul>
													</div>
												</div>
											</div>
										</div>
									</div>
								</form>
							</div>
							<div class="row">
								<div class="col-md-8 col-md-offset-2">
									<form action="/setup/wifi.min.php" enctype="multipart/form-data" method="post" id="cont_wifi_ip" name="cont_wifi_ip">
										<div class="panel panel-info">
											<div class="panel-heading">
												<h3 id="wifi_address" class="panel-title"><span class="glyphicon glyphicon-info-sign"></span> WiFi 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="WiFiDHCP" id="WiFiDHCP" value="" />
															</div>
														</div>
													</div>
												</div>
												<div class="row">
													<div class="col-md-12">
														&nbsp;
													</div>
												</div>
												<div id="wifi_addresses" class="panel panel-primary">
													<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" 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="WiFiIP[0]" value="<?php echo $WiFiIP[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="WiFiIP[1]" value="<?php echo $WiFiIP[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="WiFiIP[2]" value="<?php echo $WiFiIP[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="WiFiIP[3]" value="<?php echo $WiFiIP[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" 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="WiFiSubnet[0]" value="<?php echo $WiFiSubnet[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="WiFiSubnet[1]" value="<?php echo $WiFiSubnet[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="WiFiSubnet[2]" value="<?php echo $WiFiSubnet[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="WiFiSubnet[3]" value="<?php echo $WiFiSubnet[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" 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="WiFiGateway[0]" value="<?php echo $WiFiGateway[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="WiFiGateway[1]" value="<?php echo $WiFiGateway[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="WiFiGateway[2]" value="<?php echo $WiFiGateway[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="WiFiGateway[3]" value="<?php echo $WiFiGateway[3] ?>" step="1" min="0" max="255"/>
																</div>
															</div>
														</div>
														<div class="row">
															<div class="col-md-12">
																&nbsp;
															</div>
														</div>
													</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" 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="WiFiDNS" id="WiFiDNS" 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="WiFiDNSAddr[0]" value="<?php echo $WiFiDNSAddr[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="WiFiDNSAddr[1]" value="<?php echo $WiFiDNSAddr[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="WiFiDNSAddr[2]" value="<?php echo $WiFiDNSAddr[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="WiFiDNSAddr[3]" value="<?php echo $WiFiDNSAddr[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 id="ip_change" href="#" onclick="document.getElementById('cont_wifi_ip').submit();">
																Save Address Changes 
																<span class="glyphicon glyphicon-save"></span>
																<span class="sr-only">(current)</span>
															</a>
														</li>
													</ul>
												</div>
											</div>
										</div> 
									</form>
								</div>
							</div>
						</div>
					</div>
					<?php require('footer.php'); ?>
				</div>  
			</div>       
			<?php require('js_footer.php'); ?>
		</div>
  	</body>
</html>
