<?php
//
// Created on: 22 Feb 2001
//
// This is only used by TS_OMC only case
// For integration with TS_OMC, use dropbss.php instead
// 
// Usage: dropbss.php?language="gb|eng"
//                    &local_sys_id=xx
//                    &remote_sys_id=BSS003
//                    &start_ip=172.18.250.100
//                    [&ReturnURL=abc.php]
//
// http://172.32.20.26/~build/bssomc/dropbss.php?language=gb&local_sys_id=2&remote_sys_id=BSS002&start_ip=172.32.20.123 
//
?>
<?php
include("nocache.inc");
//include("header.inc");
include("login_parm.inc");
$DEBUG = 0;

$bssIdArr=explode('_',$sysId); 
$bssid = $bssIdArr[1] -0; // convert to integer

if($DEBUG)
{
    echo "Incomplete information<BR>";
    echo "mode=$mode<BR>";
    echo "sysId=$sysId<BR>";
    echo "ip=$ip<BR>";
	echo "bssid = $bssid<BR>";
}

$baseUrl="../configuration/systemList/systemConf.php?bssId=$bssid&&mode=delete&sysId=$sysId&ip=$ip";
//check in url
if(strlen($sysId) < 5 || $bssIdArr[1] == '' || $mode !='delete'){
	$errorInfo = "This url's value error!";
	if($DEBUG) echo "<br>errorInfo=$errorInfo";
	$errorInfo=rawurlencode($errorInfo);
	$returnUrl=$baseUrl.'&errorInfo='.$errorInfo;
	if($DEBUG) echo "<br>returnUrl=$returnUrl";
	if(!$DEBUG) echo " <meta http-equiv=\"refresh\" content=\"0; url=$returnUrl\"> ";
	exit();
}


// connect database
$db = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname,$db);

$sqlstring = "SELECT bssid,ip,connected FROM OMCR_BSSTABLE WHERE ip='$ip' OR bssid ='$bssid'";
if($DEBUG) echo "$sqlstring<BR>";
$resultSet = @mysql_query($sqlstring,$db);
$resultRowNum = @mysqli_num_rows($resultSet);
if($resultRowNum > 0){
	$resultRows=mysqli_fetch_array($resultSet);
	if($resultRows[connected] != '255'){ //exsit and connected
		$updateSql = "UPDATE OMCR_BSSTABLE 
					SET ip='', name='', remark='', connected='0', alarmed='0', uploaded='0'  
					WHERE bssid ='$bssid'
				";
		if($DEBUG) echo "<BR>updateSql=$updateSql";
		mysql_query($updateSql,$db);
		echo mysqli_error($pubConn);
	}else{
		$ip = $resultRows[ip];
		$bssid = $resultRows[bssid];
	}
}else{
	$returnUrl=$baseUrl;
	if($DEBUG) echo "<br>returnUrl=$returnUrl";
	if(!$DEBUG) echo " <meta http-equiv=\"refresh\" content=\"0; url=$returnUrl\"> ";
	exit();
}



//insert to command table
$sqlstring = "SELECT row_no FROM OMCR_COMMAND WHERE status=0 " .
             "ORDER BY seqNum ASC LIMIT 10";
//echo "$sqlstring<BR>";
$res = mysql_query($sqlstring ,$db) or
          die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
while (list($row_no[]) = mysqli_fetch_row($res));
$no_of_rows = mysqli_num_rows($res);
mysql_free_result($res);

if ( $no_of_rows <= 0 || ($no_of_rows > 10) )
{
    //
    // No available row in OMCR_COMMAND
    // Send error to MySQL()
    //
	$errorInfo = "Command table is full: Wait and send the command again";
	if($DEBUG) echo "<br>errorInfo=$errorInfo";
	$errorInfo=rawurlencode($errorInfo);
	$returnUrl=$baseUrl.'&errorInfo='.$errorInfo;
	if($DEBUG) echo "<br>returnUrl=$returnUrl";
	if(!$DEBUG) echo " <meta http-equiv=\"refresh\" content=\"0; url=$returnUrl\"> ";
	exit();
}

//
// Insert the delete bss commands in OMCR_COMMAND out of 10 available rows
//
$i=0;
do
{
    if ($row_no[$i] != '')
    {
        $sqlstring = "REPLACE INTO OMCR_COMMAND VALUES(" .
            "{$row_no[$i]},-1,'dropbss $bssid $ip',null,255,-1,null,null,null)";
		//echo "dropbss=$sqlstring<br>";
        $res       = mysql_query($sqlstring ,$db) or
                     die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
    }
    $i++;
} while ($i<10 && mysql_affected_rows() != 2);
$cmd_row_no = $row_no[$i-1];
if ($i>=10)
{
	$errorInfo = "Fail to insert command into Command table";
	if($DEBUG) echo "<br>errorInfo=$errorInfo";
	$errorInfo=rawurlencode($errorInfo);
	$returnUrl=$baseUrl.'&errorInfo='.$errorInfo;
	if($DEBUG) echo "<br>returnUrl=$returnUrl";
	if(!$DEBUG) echo " <meta http-equiv=\"refresh\" content=\"0; url=$returnUrl\"> ";
	exit();
}

//
//
// Wait for response: timeout after 500ms x 10 = 5 secs
//
//
$i=0;
while ($i<20)
{
    $result=$response=null;
    usleep(500000);  // 0.5 sec
    $sqlstring = "SELECT resultcode,result,responsecode,response from OMCR_COMMAND " .
                 "WHERE status=0 AND row_no=$cmd_row_no ";
    echo "$sqlstring<BR>";
    $res       = mysql_query($sqlstring ,$db) or
                 die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
    while ( $mydata = mysqli_fetch_row($res) )
    {
        list($resultcode,$result,$responsecode,$response) = $mydata;
    }
    $no_of_rows = mysqli_num_rows($res);
    mysql_free_result($res);

    if ($no_of_rows = 1){
        if ($resultcode != 0){
			$errorInfo = "dropbss command failed";
			if($DEBUG) echo "<br>errorInfo=$errorInfo";
			$errorInfo=rawurlencode($errorInfo);
			$returnUrl=$baseUrl.'&errorInfo='.$errorInfo;
			if($DEBUG) echo "<br>returnUrl=$returnUrl";
			if(!$DEBUG) echo " <meta http-equiv=\"refresh\" content=\"0; url=$returnUrl\"> ";
			exit();
        }else{
			// Success !!!
			$returnUrl=$baseUrl;
			echo " <meta http-equiv=\"refresh\" content=\"0; url=$returnUrl\"> ";
			exit();
        }
    }
}
?>

