<?php
include '../../../auth.php';
?>
<?php
//ArOZ Online network hardware detection code written for debian and windows (require exe!)
function remove_utf8_bom($text)
{
    $bom = pack('H*','EFBBBF');
    $text = preg_replace("/^$bom/", '', $text);
    return $text;
}

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    exec("getNICinfo.exe");
	sleep(1);
	$nics = [];
	$content = remove_utf8_bom(file_get_contents("NICinfo.txt"));
	$data = explode(PHP_EOL,$content);
	foreach ($data as $nicinfo){
		array_push($nics,explode(",",$nicinfo)[0]);
	}
	header('Content-Type: application/json');
	echo json_encode($nics);
	exit();
} else {
	$result = [];
    exec('sudo ifconfig',$arrayoutput);
	foreach ($arrayoutput as $line){
			$line = trim($line);
			$inlinedoutput .= $line."\n";
		}
	

$InterfaceRawInformation = explode("\n\n",$inlinedoutput);
array_pop($InterfaceRawInformation);
//$prased_data = preg_split("/wlan.|lo|eth./", $unprased_data,-1,PREG_SPLIT_NO_EMPTY);
foreach ($InterfaceRawInformation as $data){
	$data = preg_replace('/\n/', '', $data);
	preg_match('/(wlan[0-9]+|eth[0-9]+|lo)/', $data, $tmp);
	if(isset($tmp[1])){
		//$exported_information["InterfaceName"] = $tmp[1];
		$exported_information["InterfaceID"] = preg_replace('/\w+([0-9]+)/', '$1', $tmp[1]);
		if(strpos($tmp[1], 'eth') !== false){
			$exported_information["InterfaceIcon"] = "Ethernet";
		}else if(strpos($tmp[1], 'wlan') !== false){
			$exported_information["InterfaceIcon"] = "WiFi";
		}else if(strpos($tmp[1], 'lo') !== false){
			continue;
		}else{
			$exported_information["InterfaceIcon"] = "Unknown";
		}
	}else{
		$exported_information["InterfaceName"] = "Unknown";
		$exported_information["InterfaceIcon"] = "Unknown";
	}
	
	preg_match('/HWaddr ([0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z])/', $data, $tmp);
	if(isset($tmp[1])){
		$exported_information["HardwareAddress"] = $tmp[1];
	}else{
		preg_match('/ether ([0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z])/', $data, $tmp);
		if(isset($tmp[1])){
			$exported_information["HardwareAddress"] = $tmp[1];
		}else{
			$exported_information["HardwareAddress"] = "Unknown";
		}
	}
	
	preg_match('/inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/', $data, $tmp);
	if(isset($tmp[1])){
		$exported_information["IPv4Address"] = $tmp[1];
	}else{
		preg_match('/inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/', $data, $tmp);
		if(isset($tmp[1])){
			$exported_information["IPv4Address"] = $tmp[1];
		}else{
			$exported_information["IPv4Address"] = "Unknown";
		}
	}
	
	preg_match('/Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/', $data, $tmp);
	if(isset($tmp[1])){
		$exported_information["IPv4SubNetMask"] = $tmp[1];
	}else{
		preg_match('/mask ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/', $data, $tmp);
		if(isset($tmp[1])){
			$exported_information["IPv4SubNetMask"] = $tmp[1];
		}else{
			$exported_information["IPv4SubNetMask"] = "Unknown";
		}
	}
	
	preg_match('/inet6 addr: ([a-zA-Z0-9:]+\/[0-9]+)/', $data, $tmp);
	if(isset($tmp[1])){
		$exported_information["IPv6Address"] = $tmp[1];
	}else{
		preg_match('/inet6 ([a-zA-Z0-9:]+)/', $data, $tmp);
		if(isset($tmp[1])){
			$exported_information["IPv6Address"] = $tmp[1];
		}else{
			$exported_information["IPv6Address"] = "Unknown";
		}
	}

	array_push($result,$exported_information);
}


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

}
?>