<?php
$error5="error5 Przykro nam, serwis jest dostepny wylacznie dla autoryzowanych uzytkownikow $REMOTE_ADDR";


if(file_exists(".config.php")) include(".config.php");


require("$komputery_arr");

if(in_array("$REMOTE_ADDR",$KOMPUTERY))  $czy_error_5=1 ;

$czy_error_5=1;

if($czy_error_5) {

foreach ($_GET as $k => $v) {
	$_GET[$k]=addslashes($_GET[$k]);
	$v=addslashes($v);
	// $v=ereg_replace(';','',$v);
	eval(" \$$k = \"$v\" ;");
}
foreach ($_POST as $k => $v) {
	$_POST[$k]=addslashes($_POST[$k]);
	$v=addslashes($v);
	// $v=ereg_replace(';','',$v);
	eval(" \$$k = \"$v\" ;");
}
      


$ADM_USER=$SQL_USER ;
$ADM_USER_PASS=$SQL_USER_PASS ;
$SQL_TABLE_USERS="USERS2";
$SQL_TABLE_ADMIN_USERS="ADMIN_USERS";

$conn=mysql_pconnect($INTERFACE_ADDR,$ADM_USER,$ADM_USER_PASS);
if (!$conn) die("ERROR podczas tworzenia polaczenia do bazy $SQL_DATABASE !\n");
mysql_select_db($SQL_DATABASE) or die("ERROR podczas wyboru bazy!");



#$query="select t.T_TELBOX_BUILDING_IN as BUILDING,u.S_IP as IP from USERS2 u join TELBOXES t on u.T_TELBOX_ADDRESS=t.T_TELBOX_NAME where S_IP like '".$_GET[ip].".%' and u.A_STATUS='NORMAL'";
#$result=mysql_query($query);
#while ($row=mysql_fetch_object($result)) {
#	echo $row->BUILDING."|".$row->IP."\n";
#}

#echo $_GET[ips];
$IPS=explode(",",$_POST[ips]);
$SUBNETS=explode(",",$_POST[subnets]);

$query="select t.T_TELBOX_BUILDING_IN as BUILDING,u.S_IP as S_IP from USERS2 u join TELBOXES t on u.T_TELBOX_ADDRESS=t.T_TELBOX_NAME where u.A_STATUS in ('NORMAL') and t.T_TELBOX_BUILDING_IN like '_%' and (";
foreach ($SUBNETS as $i => $SUBNET) {
	$query.="S_IP like '".$SUBNET.".%'";
	if ($i<count($SUBNETS)-1) $query.=" or ";
}
$query.=")";
$result=mysql_query($query);
while ($row=mysql_fetch_object($result)) {
//	$BUILDINGS[$row->BUILDING][ACT]+=in_array($row->S_IP,$IPS);
	$BUILDINGS[$row->BUILDING][ALL]+=1;
	if (in_array($row->S_IP,$IPS)) $BUILDINGS[$row->BUILDING][IPS][]=$row->S_IP;
}
#print_r($BUILDINGS);

//die();

foreach ($BUILDINGS as $BUILDING => $IPS) {
	if ($IPS[ALL]>4 && !count($IPS[IPS])) $ERRORS[]=$BUILDING;
	if (count($IPS[IPS])>1) {
		$rand1=rand(0,count($IPS[IPS])-1);
		do $rand2=rand(0,count($IPS[IPS])-1); while ($rand1==$rand2);
		$IPS_TO_TEST[]=$BUILDING.":".$IPS[IPS][$rand1].":".$IPS[IPS][$rand2];
	}
}
#print_r($IPS);
echo "buildings_not_seen=(".implode(" ",$ERRORS).")\n";
echo "ips_to_test=(".implode(" ",$IPS_TO_TEST).")";

} else {
echo $error5 ;
}
?>
