<?php

defined('VSIGN') or die("Sorry, you are not allowed to directly access this page.<br /> Please press the back button in your browser.");

class networkDAO
{	
	private $db;
	private $user;
	
	function __construct(database $db, user $user) 
	{
		$this->db 	=& $db;
		$this->user =& $user;
	}

	function displayPage() 
	{
	
		include("template/pages/network_view.php");
		
		return false;
	}

	function display_status() 
	{
		$db =& $this->db;
		$user =& $this->user;

		$command = shell_exec('ipconfig /all');
		$lines = explode("\n", $command);
		
		$ip = "";
		$gateway = "";
		$subnet = "";
		$dns1 = "";

		foreach ($lines as $line) {
			$cols = preg_split('/:/', $line);
			if (preg_match('/IPv4 Address/i', $cols[0]) && !$ip) {
				$ips = preg_split('#\(#i', $cols[1]);
				$ip = trim($ips[0]);
				if(!filter_var("$ip", FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
					$ip = "";
			}
			elseif (preg_match('/Subnet Mask/i', $cols[0]) && !$subnet) {
				$subnet = trim($cols[1]);
				if(!filter_var("$subnet", FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
					$subnet = "";
			}
			elseif (preg_match('/Default Gateway/i', $cols[0]) && !$gateway) {
				$gateway = trim($cols[1]);
				if(!filter_var("$gateway", FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
					$gateway = "";
			}
			elseif (preg_match('/DNS Servers/i', $cols[0]) && !$dns1) {
				$dns1 = trim($cols[1]);
				if(!filter_var("$dns1", FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
					$dns1 = "";
			}
		}

		$form = '<form method="post" action="index.php?p=network&q=modify">';
		$tabs .= "<li><a href='#network_tab'><span>Network Configuration</span></a></li>";

		$pages .= <<<PAGES
		<div id="network_tab">
		<h3>IP Address</h3>
		<p>If you change the IP, the browser may not respond due to unable to automatically redirect to the new IP. In that case, please redirect your browser manually.</p>
		<input id="newip" class="panjang" type="text" name="ip" value="$ip">
		<h3>Subnet Mask</h3>
		<input class="panjang" type="text" name="subnet" value="$subnet">
		<h3>Gateway</h3>
		<input class="panjang" type="text" name="gateway" value="$gateway">
		<h3>DNS</h3>
		<input class="panjang" type="text" name="dns1" value="$dns1">
		</div>
PAGES;
		
		$msgSave = __('Modify');

		$form .= <<<FORM
		<div id="tabs">
			<ul class="tabs-nav">
			</ul>
			$pages
		</div>
		<input type="hidden" name="location" value="index.php?p=network&q=modify">
		<input id="newurl" type="hidden" name="refer" value="index.php?p=network">
		<input type="submit" value="$msgSave" onclick="document.getElementById('newurl').value='http://'+document.getElementById('newip').value + '/player/index.php?p=network&';" />
FORM;
		$form .= "</form>";

		echo $form;
		// var_dump($lines);
		// exit();

		return false;
	}

	function modify()
	{
		$db =& $this->db;

		$refer 		= Kit::GetParam('refer', _POST, _STRING);
		$ip 		= Kit::GetParam('ip',_POST,_STRING);
		$subnet 	= Kit::GetParam('subnet',_POST,_STRING);
		$gateway 	= Kit::GetParam('gateway',_POST,_STRING);
		$dns1 		= Kit::GetParam('dns1',_POST,_STRING);

		$cmddns = shell_exec('netsh int ip set dns name="Local Area Connection" static ' . $dns1);
		$cmdip = shell_exec('netsh int ip set address "Local Area Connection" static ' . $ip . " " . $subnet . " " . $gateway . " 1");

		return $refer;
	}
}

?>
