<?php

class inet {
	
	public function __construct(){
		global $err;
		
		$act=preg_replace('/[^a-z_]/','',$_GET['act']);
		if($act){
			call_user_func(array(&$this,$act));
		} 
	}
	
	
	public function update_ip(){
		global $DB;
		
		$id=intval($_GET[id]);
		$obj["ip"]=$_POST['ip']; 
		if (!$obj["ip"]) error("Вы не выбрали IP-адрес");
		$connected = $DB->selectCell("SELECT active FROM internet WHERE id=$id");
		if($connected==1) error("Услуга в данный момент используется");
		$DB->query("UPDATE internet set ?a WHERE id=?d",$obj,$id);
 		success();
	}	
	
	public function get_ip(){
		global $DB;
		$s_id = intval($_GET[id]);
		$vlan = $DB->selectCell("SELECT vlan_id FROM internet WHERE id=$s_id");
		
		$start_ip = $DB->selectCell("SELECT start_ip FROM vlans WHERE id=$vlan");
		$s_ip=explode(".",$start_ip);
		$sip=$s_ip[3];
		$sgui=$s_ip[2];
		$end_ip = $DB->selectCell("SELECT end_ip FROM vlans WHERE id=$vlan");
		$e_ip=explode(".",$end_ip);
		$eip=$e_ip[3];
		$egui=$e_ip[2];
		$ips= array();
		if($sgui < $egui) {
			while($sgui <= $egui)
			{
				if($sgui < $egui)
				while($sip <= 255)
				{
					$ips[]=$s_ip[0].".".$s_ip[1].".".$sgui.".".$sip;
					$sip++;
				}
				if($sgui == $egui)
				while($sip <= $eip)
				{
					$ips[]=$s_ip[0].".".$s_ip[1].".".$sgui.".".$sip;
					$sip++;
				}
				$sgui++;
				$sip=0;
			}
		} else 
		while($sip <= $eip)
			{
				$ips[]=$s_ip[0].".".$s_ip[1].".".$sgui.".".$sip;
				$sip++;
			}
		$have = $DB->selectCol("SELECT ip FROM internet");
		$ip = array_diff($ips,$have);
		$count=0;
		foreach($ip as $ips_k=>$ips_v)
		{
			$data[$count][value] = $ips_v;
			$data[$count][text] = $ips_v;
			$count++;
		}
		
		$json=array("success"=>true,"data"=>$data);
		echo json_encode($json);
	}
	
}
?>