<?php
//
// Created on: 22 Feb 2001
// Usage     : modify.php?bssid=0
//             [&url=bssfunc.php]
//             [&language="eng|gb"]
//             [&modified_data=Adminstate,CellIdentity]
//             [&nmicommand=set Adminstate=2 Cellidentity=1293]
//             [&objectid=BssFunc]
//             [&timeout=15]
//             [&x=0]
//             [&y=100]
//
// Function  : 1. Get 10 available rows
//             2. Send "oid $objectid"
//             3. Send the nmi command to OMCR_Command
//
// Remarks   : If url is defined, it will redirect to another php file.
// MySQL table: OMCR_Command
//
// $url --> modify.php --> $url
// 
//
?>
<script language="JavaScript">
function jump2url(urlstr)
{
	//alert(urlstr);
	window.location.href=urlstr;
}
</script>
<?php

$dbname="BssOmcDb";
$hostname="localhost";
$username="root";
$password="rootaa";
$progress_value=0;
?>
<?php
include("nocache.inc");       
include("modify.inc");
if (!isset($url))
    $url = "objects.php";
if (!isset($timeout))
    $timeout = 15;
if (!isset($language))
    $language = "eng";
$parameter="${language}namearray";

//
// if nothing has been modified, reload the original page
//
if ( ($modified_data == '') && ($nmicommand == ''))
{
	//***************** zhengjianhui start *********************
	echo "\n<script language=\"JavaScript\">\n";
	$jump_url="{$url}?".
						"language={$language}&".
						"bssid={$bssid}&".
						"objectid={$objectid}&".
						"tablename={$tablename}&".
						"progress_value={$progress_value}&".
						"status='Nothing is requested'";
						
	if(isset($x))
		$jump_url.="&x=$x";
	if(isset($y))
		$jump_url.="&y=$y";
	echo "jump2url('{$jump_url}');\n";
	echo "</script>\n";
	//***************** zhengjianhui end *********************
	/*
    header("Location: " .
        "$url?language=$language&bssid=$bssid&objectid=$objectid" .
        "&tablename=$tablename" .
        "&status='Nothing is requested'" .
        (isset($x)?"&x=$x":"") .
        (isset($y)?"&y=$y":"")
    );
    */
    return;
}

//
// Find the available row in OMCR_COMMAND to insert nmi commands
// Get 10 available and insert 2 nmi commands
// 1. oid $objectid
// 2. set $para1=$val1 [$para2=$val2] ...
//
include("meta_table_type.inc");
include("login_parm.inc");
$db = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname,$db);

if ($nmicommand != '')
{
	$nmicmd = $nmicommand;
}

if ($modified_data != '')
{
    
	//echo $modified_data . "<BR>";
    $parm_name = explode(",",$modified_data);
    if(strncmp($nmicmd,"create",6)&& strncmp($nmicmd,"delete",6))
    	$nmicmd="set";
    
    foreach ($parm_name as $attribute)
    {
		
      switch(${"t$attribute"})
      {
			
            case NUMBERS:
              $tmpSql="SELECT Attribute_Id FROM OMCR_Meta_Table WHERE Attribute_Name='$attribute'";
              $tmpResult = mysql_query($tmpSql,$db) or
                die("Invalid query: $tmpSql\n" . mysqli_error($pubConn));
              $tmpRow = mysqli_fetch_row($tmpResult);
              $tmpStr=$tmpRow[0];
              
              if (isset(${$attribute}))
              {
                //$nmicmd.=" $attribute=${$attribute}";
                if(strncmp($nmicmd,"create",6)&& strncmp($nmicmd,"delete",6))
                  $nmicmd.=" $tmpStr=${$attribute}";
                else
                  $nmicmd.=" ${$attribute}";
              }
              else // for checkbox:  ${$attribute}=true or false
              {
                  //$nmicmd.=" $attribute=0									
                  $nmicmd.=" $tmpStr=0";
              }
              break;
            case STRINGS:
              $tmpSql="SELECT Attribute_Id FROM OMCR_Meta_Table WHERE Attribute_Name='$attribute'";
              $tmpResult = mysql_query($tmpSql,$db) or
                die("Invalid query: $tmpSql\n" . mysqli_error($pubConn));
              $tmpRow = mysqli_fetch_row($tmpResult);
              $tmpStr=$tmpRow[0];
              if(strlen($tmpStr)>0){
                //$nmicmd.=" $tmpStr=\"" . rawurlencode(${$attribute}) . "\"";
                if(strncmp($nmicmd,"create",6)&& strncmp($nmicmd,"delete",6))
                  $nmicmd.=" $tmpStr=\"" . rawurlencode(${$attribute}) . "\"";
                else
                  $nmicmd.=" \"" . rawurlencode(${$attribute}) . "\"";
              }else{
              	if(strncmp($nmicmd,"create",6)&& strncmp($nmicmd,"delete",6))
                  $nmicmd.=" $attribute=\"" . rawurlencode(${$attribute}) . "\"";
                else
                  $nmicmd.=" \"" . rawurlencode(${$attribute}) . "\"";
              }
              break;
            case INTEGER_ARRAY:
              $tmpSql="SELECT Attribute_Id FROM OMCR_Meta_Table WHERE Attribute_Name='$attribute'";
              $tmpResult = mysql_query($tmpSql,$db) or
					      die("Invalid query: $tmpSql\n" . mysqli_error($pubConn));
              $tmpRow = mysqli_fetch_row($tmpResult);
              $tmpStr=$tmpRow[0];
        	    $stringarray = explode("_",$tmpStr);
        	    if(strncmp($nmicmd,"create",6)&& strncmp($nmicmd,"delete",6))
                $nmicmd.= " $stringarray[0]=[$stringarray[1]=${$attribute}]";
              else
                 $nmicmd.= " [$stringarray[1]=${$attribute}]";
              break;
            default:
              break;				
    	}
        //echo $nmicmd . "<BR>";
    }
}

$sqlstring = "LOCK TABLES OMCR_COMMAND WRITE";
$res = mysql_query($sqlstring ,$db) or
          die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
$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()
    //

		//***************** zhengjianhui start *********************
		echo "\n<script language=\"JavaScript\">\n";
		$jump_url="{$url}?".
							"language={$language}&".
							"bssid={$bssid}&".
							"objectid={$objectid}&".
							"tablename={$tablename}&".
							"progress_value={$progress_value}&".
							"status='{${$parameter}[cmdtablefull]}'";
							
		if(isset($x))
			$jump_url.="&x=$x";
		if(isset($y))
			$jump_url.="&y=$y";
		echo "jump2url('{$jump_url}');\n";
		echo "</script>\n";
		//***************** zhengjianhui end *********************
    /*
    header("Location: " .
        "$url?language=$language&bssid=$bssid&objectid=$objectid" .
        "&tablename=$tablename" .
        "&status='{${$parameter}[cmdtablefull]}'" .
        (isset($x)?"&x=$x":"") .
        (isset($y)?"&y=$y":"")
    );
    */
    return;
}

//
// Insert 2 commands in OMCR_COMMAND out of 10 possible available rows
//
$i=0;
do
{
    if ($row_no[$i] != '')
    {
    	if(!strncmp($nmicmd,"delete",6)&& strncmp($nmicmd,"delete BtsMgr",13))
        	$sqlstring = "REPLACE INTO OMCR_COMMAND VALUES(" . $row_no[$i] .
        	             ",$bssid,'oid $parent_objectid',null,255,-1,null,null,null)";
       	else
       		$sqlstring = "REPLACE INTO OMCR_COMMAND VALUES(" . $row_no[$i] .
        	             ",$bssid,'oid $objectid',null,255,-1,null,null,null)";
        $res       = mysql_query($sqlstring ,$db) or
                     die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
    }
    $i++;
} while ($i<10 && mysql_affected_rows() != 2);
$cmd1_row_no = $row_no[$i-1];

do
{
    if ($row_no[$i] != '')
    {
    	if(!strncmp($nmicmd,"delete",6)&& strncmp($nmicmd,"delete BtsMgr",13)){
    		$objectid_arr=explode('.',$objectid);
    		$nmicmd=$nmicmd.' '.$objectid_arr[sizeof($objectid_arr)-1];
    	}
        $sqlstring = "REPLACE INTO OMCR_COMMAND VALUES(" .
                     $row_no[$i] . ",$bssid,'$nmicmd',null,255,-1,null,null,null)";
        $res       = mysql_query($sqlstring ,$db) or
                     die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
    }
    $i++;
} while (mysql_affected_rows() != 2 && $i<10);
$cmd2_row_no = $row_no[$i-1];

$sqlstring = "UNLOCK TABLES";
$res = mysql_query($sqlstring ,$db) or
          die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));

//
//
// Wait for response: timeout value = $timeout
//
//
//output dump data to br
//for($ppp=0;$ppp<256;$ppp++)
//	print(" ");

$timewait=0;
$interval=800000;   // in micro-seconds
echo str_pad(" ", 256); 
while ($timewait < $timeout)
{
		//for progress
		$progress_value++;
		
	echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">"; 
	ob_flush();
	flush();
	usleep(1000000);

		$timewait ++;
		
		$resultcode=$responsecode=$result=$response=null;
    //sleep(1);
    

    $sqlstring = "SELECT t1.resultcode,t1.responsecode," .
                 "t2.${language}StatInfo,t3.${language}StatInfo " .
                 "FROM OMCR_COMMAND AS t1,OMCR_STATCODE AS t2,OMCR_VARSTATCODE AS t3 " .
                 "WHERE t2.statcode=t1.resultcode AND t3.statcode=t1.responsecode " .
                 "AND t1.status=0 " .
                 "AND (t1.row_no=$cmd1_row_no OR t1.row_no=$cmd2_row_no) " .
                 "ORDER BY seqNum ASC";


    $res       = mysql_query($sqlstring ,$db) or
                 die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
    while ( $mydata = mysqli_fetch_row($res) )
    {
        list($resultcode[],$responsecode[],$result[],$response[]) = $mydata;
        //list($a[],$b[],$c[],$d[],$e[],$result[],$response[]) = $mydata;
    }
    $no_of_rows = mysqli_num_rows($res);
    //mysql_free_result($res);
    if ($no_of_rows == 2)
    {
        if ($resultcode[0] != 0)  // 0='ok'
        {
            // Oid failed !!!
						//***************** zhengjianhui start *********************
						echo "\n<script language=\"JavaScript\">\n";
						$jump_url="{$url}?".
											"language={$language}&".
											"bssid={$bssid}&".
											"objectid={$objectid}&".
											"progress_value={$progress_value}&".
											"tablename={$tablename}&".
											"alarm={$result[0]}";
											
						if(isset($x))
							$jump_url.="&x=$x";
						if(isset($y))
							$jump_url.="&y=$y";
						echo "jump2url('{$jump_url}');\n";
						echo "</script>\n";
						//***************** zhengjianhui end *********************
						/*
            header("Location: " .
                "$url?language=$language&bssid=$bssid" .
                "&objectid=$objectid&tablename=$tablename" .
                "&alarm=$result[0]" .
                (($responsecode[0]!=0)? " --- $response[0]":"") .
                (isset($x)?"&x=$x":"") .
                (isset($y)?"&y=$y":"")
            );
            */
        }
        elseif ($resultcode[1] != 0)
        {
            // Nmi command failed !!!
						//***************** zhengjianhui start *********************
						echo "\n<script language=\"JavaScript\">\n";
						$jump_url="{$url}?".
											"language={$language}&".
											"bssid={$bssid}&".
											"objectid={$objectid}&".
											"progress_value={$progress_value}&".
											"tablename={$tablename}&".
											"alarm={$result[1]}";
						
						if($responsecode[1]!=0)
							 $jump_url.="--- {$response[1]}";
											
						if(isset($x))
							$jump_url.="&x=$x";
						if(isset($y))
							$jump_url.="&y=$y";
						echo "jump2url('{$jump_url}');\n";
						echo "</script>\n";
						//***************** zhengjianhui end *********************
						/*
            header("Location: " .
                "$url?language=$language&bssid=$bssid" .
                "&objectid=$objectid&tablename=$tablename" .
                "&alarm=$result[1]" .
                (($responsecode[1]!=0)? " --- $response[1]":"") .
                (isset($x)?"&x=$x":"") .
                (isset($y)?"&y=$y":"")
            );
            */
        }
        else
        {
            // Success !!!
            if(strncmp($nmicmd,"create",6)==0){
            	$tablename=str_replace("New_", "", $tablename);
            	$parent_objectid=$objectid;
    			$objectid_arr=explode('.',$objectid);
    			$objectid=str_replace("OMCR_", "", $tablename).'.';
    			for($i=1;$i<sizeof($objectid_arr);$i++)
    				$objectid=$objectid.$objectid_arr[$i].'.';
    			$objectid=$objectid.${$attribute};
    		}else if(strncmp($nmicmd,"delete",6)==0){
    			$objectid=$parent_objectid;
    			$objectid_arr=explode('.',$parent_objectid);
    			$tablename='OMCR_'.$objectid_arr[0];
    		}
    		
    					if(!strncmp(strtolower($nmicommand), "create",6)
								|| !strncmp(strtolower($nmicommand), "delete",6)
								|| !strncmp(strtolower($nmicommand),"upload",6)){
							for($i=0;$i<5;$i++){
								sleep(1);
								echo "<img width=10 height=10 src=\"../../images/arrow.gif\">";
								flush(); 
							}		
						}//***************** zhengjianhui start *********************
						echo "\n<script language=\"JavaScript\">\n";
						$jump_url="{$url}?".
											"language={$language}&".
											"bssid={$bssid}&".
											"parent_objectid={$parent_objectid}&".
											"progress_value={$progress_value}&".
											"objectid={$objectid}&".
											"tablename={$tablename}&";
											
						/*if(strncmp(strtolower($nmicommand), "create",6)
								&& strncmp(strtolower($nmicommand), "delete",6)
								&& (strncmp(strtolower($objectid),"abismgr",7) || strncmp(strtolower($nmicommand),"set adminstate=2",16))
							)
							$jump_url.="status={${$parameter}[saveiscompleted]}";
						else
							$jump_url.="status={${$parameter}[actioniscompleted]}";*/
						
						if(!strncmp(strtolower($nmicommand), "create",6)
								|| !strncmp(strtolower($nmicommand), "delete",6)
								|| !strncmp(strtolower($nmicommand),"upload",6)){
							$jump_url.="status={${$parameter}[actioniscompleted]}";
						}else
							$jump_url.="status={${$parameter}[saveiscompleted]}";
							
							
						if($modified_data == '')
							$jump_url.="";
						else
							$jump_url.="&saved={$modified_data}";
						
						
						if(isset($x))
							$jump_url.="&x=$x";
						if(isset($y))
							$jump_url.="&y=$y";
						echo "jump2url('{$jump_url}');\n";
						echo "</script>\n";
						//***************** zhengjianhui end *********************
    				/*
            header("Location: " .
                "$url?language=$language&bssid=$bssid" .
                "&parent_objectid=$parent_objectid" .
                "&objectid=$objectid" .
                "&tablename=$tablename" .
                ( (strncmp(strtolower($nmicommand), "create",6)
                  && strncmp(strtolower($nmicommand), "delete",6)
                  && (strncmp(strtolower($objectid),"abismgr",7) || strncmp(strtolower($nmicommand),"set adminstate=2",16))
                  )?
                 "&status={${$parameter}[saveiscompleted]}":
                 "&status={${$parameter}[actioniscompleted]}"
                ) .
//                (($nmicommand != '') ?  "&status={${$parameter}[actioniscompleted]}":
//                 "&status={${$parameter}[saveiscompleted]}") .
                (($modified_data == '') ? "":"&saved=$modified_data") .
                (isset($x)?"&x=$x":"") .
                (isset($y)?"&y=$y":"")
            );
            */
        }
        return;
    }
    
    
}

//
// Server or BSC has no response
// OMCR_COMMAND.status did not change to zero before timeout
//
$sqlstring = "SELECT * FROM OMCR_COMMAND " .
             "WHERE status=254 AND (row_no=$cmd1_row_no OR row_no=$cmd2_row_no) ";
//echo "$sqlstring<BR>";
$res = mysql_query($sqlstring ,$db) or
       die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
if (mysqli_fetch_row($res))
{
		//***************** zhengjianhui start *********************
		echo "\n<script language=\"JavaScript\">\n";
		$jump_url="{$url}?".
							"language={$language}&".
							"bssid={$bssid}&".
							"objectid={$objectid}&".
							"progress_value={$progress_value}&".
							"tablename={$tablename}&".
							"alarm={${$parameter}[nobscresponse]}";
							
		if(isset($x))
			$jump_url.="&x=$x";
		if(isset($y))
			$jump_url.="&y=$y";
		echo "jump2url('{$jump_url}');\n";
		echo "</script>\n";
		//***************** zhengjianhui end *********************
		/*
    header("Location: " .
        "$url?language=$language&bssid=$bssid&objectid=$objectid" .
        "&tablename=$tablename" .
        "&alarm={${$parameter}[nobscresponse]}" .
        (isset($x)?"&x=$x":"") .
        (isset($y)?"&y=$y":"")
    );
    */
}
else
{
//***************** zhengjianhui start *********************
		echo "\n<script language=\"JavaScript\">\n";
		$jump_url="{$url}?".
							"language={$language}&".
							"bssid={$bssid}&".
							"objectid={$objectid}&".
							"progress_value={$progress_value}&".
							"tablename={$tablename}&".
							"alarm={${$parameter}[nobsscommresponse]}";
							
		if(isset($x))
			$jump_url.="&x=$x";
		if(isset($y))
			$jump_url.="&y=$y";
		echo "jump2url('{$jump_url}');\n";
		echo "</script>\n";
		//***************** zhengjianhui end *********************
		/*
    header("Location: " .
        "$url?language=$language&bssid=$bssid&objectid=$objectid" .
        "&tablename=$tablename" .
        "&alarm={${$parameter}[nobsscommresponse]}" .
        (isset($x)?"&x=$x":"") .
        (isset($y)?"&y=$y":"")
    );
    */
}

//
// Change status to 0
//
$sqlstring = "UPDATE OMCR_COMMAND SET status=0 WHERE row_no=$cmd1_row_no OR row_no=$cmd2_row_no";
//echo "$sqlstring<BR>";
$res = mysql_query($sqlstring ,$db) or
       die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
?>
