<?php 
/*********************************************************      
 程序说明：         
    功能说明：network in a box
    调用关系：调用: header.inc   
              被调用:         
    变量说明：
    作者：Cui Ticun   2006-05-25      
      
修改注释：      
NO.1      
姓名：       
时间：    
修改说明：    
*********************************************************/
require("../../inc/header.inc"); 
$DEBUG = 0;

$helpId="2.1.5";

$nGetComm	= 0;
$nSetComm	= 3;

$nProcFail = 0;//failure
$nWaitProc = 1;//wait
$nInProc   = 2;
$nOutProc  = 3;

$nSubSysParam		= 0;
$nGlobalSubSysParam = 1;
$nGlobalSysParam	= 2;

$maxNum=120;

$ftpInfo=getMSSFtpAccount();

$pubDb				= 'OMC_PUB';
$sysInfoTable		= 'OMC_PUB.sysInfo';
$elementConfTable	= 'OMC_PUB.elementExistConf';
$sysCommTable		= 'OMC_PUB.parameterComm';

$defaultParaDirectory='./defaultPara';
$defaultSubsDirectory='./defaultSubs';

$bssParaDirectory	 ='./bssPara';

$resetCmdArr=array(
		array('remark' => 'MTP3 parameters','sysTypeNo' => '222', 'sysId' => '222_99_99', 'oid' => '3.7', 'value' => '01'),
		array('remark' => 'SCCP parameters','sysTypeNo' => '223','sysId' => '223_99_99', 'oid' => '3.2', 'value' => '00'),
		array('remark' => 'MSC parameters','sysTypeNo' => '322','sysId' => '322_99_99', 'oid' => '3.2', 'value' => '5A'),
		array('remark' => 'VLR parameters','sysTypeNo' => '325','sysId' => '325_0_0', 'oid' => '3.3', 'value' => '00'),
		// reload subs
		array('remark' => 'HLR subscribers','sysTypeNo' => '330','sysId' => '330_99_99', 'oid' => '2.1.19', 'value' => '01')
);


$saveCmdArr=array(
		array('remark' => 'MTP3 parameters','sysTypeNo' => '222', 'sysId' => '222_99_99', 'oid' => '3.7', 'value' => '02'),
		array('remark' => 'SCCP parameters','sysTypeNo' => '223','sysId' => '223_99_99', 'oid' => '3.2', 'value' => '02'),
		array('remark' => 'XAPP parameters','sysTypeNo' => '225','sysId' => '225_99_99', 'oid' => '3.2', 'value' => '01'),
		array('remark' => 'MSC parameters','sysTypeNo' => '322','sysId' => '322_99_99', 'oid' => '3.2', 'value' => 'B5'),
		array('remark' => 'VLR parameters','sysTypeNo' => '325','sysId' => '325_0_0', 'oid' => '3.3', 'value' => '01'),
		array('remark' => 'HLR parameters','sysTypeNo' => '330','sysId' => '330_0_0', 'oid' => '2.2.2', 'value' => '01'),
		array('remark' => 'AUC parameters','sysTypeNo' => '340','sysId' => '340_99_99', 'oid' => '2.2.2', 'value' => '01'),
		array('remark' => 'SMSC parameters','sysTypeNo' => '350','sysId' => '350_99_99', 'oid' => '3.3', 'value' => '01')
);

$getParamArr=array(
		array('remark' => 'MTP3 parameters','sysTypeNo' => '222', 'sysId' => '222_99_99','paramTable'=>'OBJ_222.param_99', 'oid' => '2'),
		array('remark' => 'SCCP parameters','sysTypeNo' => '223','sysId' => '223_99_99', 'paramTable'=>'OBJ_223.param_99','oid' => '2'),
		array('remark' => 'XAPP parameters','sysTypeNo' => '225','sysId' => '225_99_99', 'paramTable'=>'OBJ_225.param_99','oid' => '2'),
		array('remark' => 'MSC parameters','sysTypeNo' => '322','sysId' => '322_99_99', 'paramTable'=>'OBJ_322.param_99','oid' => '2.1.4'),
		array('remark' => 'VLR parameters','sysTypeNo' => '325','sysId' => '325_99_99', 'paramTable'=>'OBJ_325.param_99','oid' => '2.1.13')
);


$NetOPCArr=array(
   //array('oid', 'name'),
   array('2.3.1', '14bit international network'),
   array('2.3.3', '14bit national network'), 
   array('2.3.5', '24bit international network'), 
   array('2.3.7', '24bit national network')
);
?>

<script LANGUAGE="JavaScript">  
function confCheck(actionType)
{
	switch(actionType){
		case 1:
			if(confirm('Do you want to upload MSS test configuration?')!=true)
				window.location.href='<?php echo $PHP_SELF;?>';
			else
				window.location.href='<?php echo $PHP_SELF;?>'+"?uploadDefault="+1;
			break;
		default:
			break;
	}
}
</script>

<body>
<form method="POST" action="<?php echo "$PHP_SELF";?>" name="paraConf" enctype="multipart/form-data">
<?php
echo "<table border=\"0\" width=\"100%\">";
echo "<tr>";
echo "<td width=\"50%\" align=\"left\">";
echo "$strConfiguration > $strNetwork >NIB";
echo "</td>";
echo "<td width=\"50%\" align=\"right\" title=\"Help\">";
showHelp($helpId);
echo "</td>";
echo "</tr>";
echo "</table>";
?>

<?php
if($uploadDefault){		
	//upload the parameters
	echo "Uploading 8KE1 card parameters!<br>";

	$updateSql="REPLACE param_0 SELECT * FROM nibPara";
	if($DEBUG)
		echo "<br>updateSql=$updateSql";
	mysqli_query($pubConn,$updateSql);
	
	$commStr=sysCommSend('111_0_99',$nSetComm,'2');
	if($DEBUG)echo "<br>commStr=$commStr";
		
	//check the command status
	if($commStr==""){
		echo "<br><font color=\"#FF0000\">Send the command error, Please 8KE1 card's state!</font><br>";
		require("./display.php");
		exit();
	}
		
	$cmmIdStrArr=explode("-",$commStr);
	
	for($i=0;$i<sizeof($cmmIdStrArr)-1;$i++){
		if($i==0)
			$tmpWhere="(commId='$cmmIdStrArr[$i]'";
		else
			$tmpWhere=$tmpWhere." OR commId='$cmmIdStrArr[$i]'";
	}
	$tmpWhere=$tmpWhere.")";	
	
				
	$i = 0;
	while(1){
		if($i > $maxNum){
			$tmpRemark='Timeout';			
			$delComm = " DELETE FROM $sysCommTable WHERE $tmpWhere ";
			mysqli_query($pubConn,$delComm);						
			
			echo "<br><font color=\"#FF0000\">Error: $tmpRemark.</font>";
			require("./display.php");
			exit();
		}
		echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
		flush();	
		
		$selComm = "SELECT commState,remark,objId,sysTypeNo FROM $sysCommTable WHERE $tmpWhere AND commState='$nProcFail'";
		if($DEBUG) echo "<br>selComm=$selComm";
		
		$selRes  = mysqli_query($pubConn,$selComm);
		
		if(mysqli_num_rows($selRes)>0){
			$selRow  = mysqli_fetch_array($selRes);
			$tmpRemark=$selRow[remark];
			$tmpOid=$selRow[objId];
			$tmpSysTypeNo=$selRow[sysTypeNo];
			$delComm = " DELETE FROM $sysCommTable WHERE $tmpWhere ";	
			mysqli_query($pubConn,$delComm);
			
			echo "<br><font color=\"#FF0000\">Error: $tmpRemark.</font>";
			require("./display.php");
			exit();
		}
		
		$selComm = "SELECT commState,remark FROM $sysCommTable WHERE $tmpWhere AND (commState='$nWaitProc' OR commState='$nInProc')";
	
		if($DEBUG)echo "<br>selComm=$selComm";
		
		$selRes  = mysqli_query($pubConn,$selComm);
		if($DEBUG) $tmpNum=mysqli_num_rows($selRes);
		if($DEBUG) echo "<br>tmpNum=$tmpNum";
		
		if(mysqli_num_rows($selRes)>0){
			sleep(1);
			$i++;
			continue;
		}
		$delComm = " DELETE FROM $sysCommTable WHERE $tmpWhere ";	
		mysqli_query($pubConn,$delComm);
		echo "<font size=\"2\" color=\"#0000FF\">Finished</font>";
		break;		
	}
	
	$getSysIdSql = "SELECT * FROM $sysInfoTable 
					WHERE sysTypeNo='320' 
					ORDER BY baseStat DESC,updateTime DESC";

	if($DEBUG) echo "<br>getSysIdSql=$getSysIdSql";
	$getSysIdResult=mysqli_query($pubConn,$getSysIdSql);
	echo mysqli_error($pubConn);

	while($getSysIdRows=mysqli_fetch_array($getSysIdResult)){
		$dstSeverIp=$getSysIdRows[ip];	
		echo "<br>Stopping MSS,please wait about 3 minutes!<br>";
		flush();
		$tmpScript="./beforeFtp".' '.$dstSeverIp;
		system($tmpScript);
		
		//clear heartbeat
		$delSql="DELETE FROM OMC_PUB.sysStat";
		mysqli_query($pubConn,$delSql);

		for($i=0;$i<30;$i++){
			echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
			flush();
			sleep(4);
		}
		echo "<font size=\"2\" color=\"#0000FF\">Finished</font>";
		flush();
		echo "<br>Uploading MSS parameters and subscribers!<br>";
		flush();

		$ftpCon=@ftp_connect($dstSeverIp);
		
		if($ftpCon < 0){
			echo "<br><font color=\"#FF0000\">Connect $dstSeverIp Failed!</font>";
		}else{
			if(!@ftp_login($ftpCon,$ftpInfo[UserName],$ftpInfo[Password])){
				echo "<br><font color=\"#FF0000\">Login $dstSeverIp Failed!</font>";
			}else{
				$finishFlag=1;
				$directory=$defaultParaDirectory;
				$mydir=@dir($directory);
				while($file=$mydir->read()){ 
					if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!="..")){
						continue;
					}elseif($file =="." || $file ==".."){
						continue;
					}else{
						$remoteFileName='/usr/local/wxc2/bin/conf/'.$file;
						$localFile=$directory.'/'.$file;
						if($DEBUG)
							echo "<br>dstSeverIp=$dstSeverIp,remoteFileName=$remoteFileName,localFile=$localFile";
						if(!@ftp_put($ftpCon,$remoteFileName,$localFile,FTP_ASCII)){
							echo "<br><font color=\"#FF0000\">Put $file Failed,please check the network configuration and network speed</font>";
							$finishFlag=0;
							break;
						}			
					}
					echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
					flush();	
				}//end of while
				$mydir->close();

				//upload the subscribers
				$directory=$defaultSubsDirectory;
				$mydir=@dir($directory);
				while($file=$mydir->read()){ 
					if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!="..")){
						continue;
					}elseif($file =="." || $file ==".."){
						continue;
					}else{
						$remoteFileName='/usr/local/backup/'.$file;
						$localFile=$directory.'/'.$file;
						if(!@ftp_put($ftpCon,$remoteFileName,$localFile,FTP_ASCII)){
							echo "<br><font color=\"#FF0000\">Put $file Failed,please check the network configuration and network speed</font>";
							$finishFlag=0;
							break;
						}			
					}
					echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
					flush();	
				}//end of while
				$mydir->close();
				ftp_quit($ftpCon);
				if($finishFlag==1)
					echo "<font size=\"2\" color=\"#0000FF\">Finished</font>";
			}
		}

		echo "<br>Starting MSS,please wait about 3 minutes!<br>";
		flush();
		$tmpScript="./mssStart".' '.$dstSeverIp;
		system($tmpScript);
		
		for($i=0;$i<45;$i++){
			$tmpSql = "SELECT * FROM OMC_PUB.sysStat WHERE sysTypeNo='200' AND sysNo='9'";
			if($DEBUG) echo "<br>tmpSql=$tmpSql";
			$tmpResult=mysqli_query($pubConn,$tmpSql);
			echo mysqli_error($pubConn);
			if(mysqli_num_rows($tmpResult)>0)
				break;
			echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
			flush();
			sleep(4);
		}
		if($i==30)
			echo "<font size=\"2\" color=\"#FF0000\">Starting MSS failed,please check it</font>";
		else
			echo "<font size=\"2\" color=\"#0000FF\">Finished</font>";
		flush();
	}
	
	echo "<br><font size=\"2\" color=\"#0000FF\">Process finished</font><br>";
}

if($uploadBss){
	echo "<br>Uploading BSS parameters!<br>";
	flush();
	if($DEBUG)
		echo "<br>bssIpAddr=$bssIpAddr";
	
	if(!isset($bssIpAddr)||strlen($bssIpAddr)<=0){
		echo "<br><font color=\"#FF0000\">BSS's ip is error</font>";
		require("./display.php");
		exit();
	}
	
	$ftpCon=@ftp_connect($bssIpAddr);
	if($ftpCon < 0){
		echo "<br><font color=\"#FF0000\">BSS's ip is error or connect $bssIpAddr Failed!</font>";
		require("./display.php");
		exit();
	}else{
		if(!@ftp_login($ftpCon,'','')){
			echo "<br><font color=\"#FF0000\">BSS's ip is error or login $bssIpAddr Failed!</font>";
			require("./display.php");
			exit();
		}else{
			if(!isset($bssConfFileDirectory)||strlen($bssConfFileDirectory)<=0){
				echo "<br><font color=\"#FF0000\">BSS's directory is error</font>";
				require("./display.php");
				exit();
			}else{
				$directory=$bssParaDirectory;
				$mydir=@dir($directory);
				while($file=$mydir->read()){ 
					if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!="..")){
						continue;
					}elseif($file =="." || $file ==".."){
						continue;
					}else{
						$remoteFileName=$bssConfFileDirectory.'/'.$file;
						$localFile=$directory.'/'.$file;
						if($DEBUG)
								echo "<br>bssIpAddr=$bssIpAddr,remoteFileName=$remoteFileName,localFile=$localFile";
						if(!@ftp_put($ftpCon,$remoteFileName,$localFile,FTP_BINARY)){
							echo "<br><font color=\"#FF0000\">BSS's directory is error or put $file Failed</font>";
							require("./display.php");
							exit();
						}			
					}
					echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
					flush();		
				}//end of while
				echo "<font size=\"2\" color=\"#0000FF\">Finished</font>";
				flush();		

				$mydir->close();
				ftp_quit($ftpCon);
			}
		}
	}

	echo "<br>Rebooting BSS,please wait about 3 minutes!<br>";
	flush();

	$tmpScript="./bssReboot".' '.$bssIpAddr;
	
	if($DEBUG)
		echo "<br>tmpScript=$tmpScript";
		
	system($tmpScript);
	
	//clear heartbeat
	$delSql="DELETE FROM OMC_PUB.sysStat";
	mysqli_query($pubConn,$delSql);
	
	for($i=0;$i<4;$i++){
		echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
		flush();
		sleep(4);
	}
	
	for($i=0;$i<60;$i++){
		$getStatSql = "SELECT detailStat FROM OMC_PUB.sysStat 
						WHERE sysTypeNo='111' AND sysNo='0'";

		if($DEBUG) 
			echo "<br>getStatSql=$getStatSql";
		$getStatResult=mysqli_query($pubConn,$getStatSql);
		echo mysqli_error($pubConn);

		if($getStatRows=mysqli_fetch_array($getStatResult)){
			$ALink_stat=getBitValue($getStatRows[detailStat],"25.1-25.1");
			//echo "<br>ALink_stat=$ALink_stat";
			if($ALink_stat==1){
				break;
			}else{
				echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
				flush();
				sleep(4);
			}
		}else{
			echo "<font size=\"2\" color=\"#FF0000\">Cannot receive 8KE1 card heartbeat,please check 8KE1 card</font>";
			break;
		}
	}
	if($i==60)
		echo "<font size=\"2\" color=\"#FF0000\">The A link status is error,please check it</font>";
	else
		echo "<font size=\"2\" color=\"#0000FF\">Finished</font>";
	echo "<br><font size=\"2\" color=\"#0000FF\">Process finished</font>";
}

if($configureMss){
	require("./configureForBss.php");
}

if($configurePstn){
	require("./configureForPstn.php");
}

require("./display.php");
?>
</form>
</body>
</html>
