<?php
/***************************************************************
File Name: mscSync.php
File Relation: getFile, putFile, startMsc, stopMsc
File Include:  wxc2_omc/inc/header.inc
Function: To synchronize the mss-0 conf file to the mss-1 file or reversely 
Global Variable: 
Global function: 
Author: JianHui Zheng
Date: 2008-06-17

----- Modify  -----
Description: 
***************************************************************/
	require("../../inc/header.inc");
?>

<script language="JavaScript">
	function sync(direction)
	{
		if(true==confirm("Are you sure to do this operation?"))
		{
			window.location.href="./mscSync.php?operation=1&direction="+direction;
		}
	}
	
</script>	

<body leftmargin="15" rightmargin="10" onload="javascript:adjust();" onresize="javascript:adjust();" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;">

<?php
adjust_head();
adjust_title_head();
echo "<TABLE id=\"table_up\" border=\"0\" width=\"100%\">";
echo "<TR>";
echo "<TD width=\"50%\" align=\"left\">";
echo "Maintenance > Control Panel > MSS Sync ";
echo "</TD>";
echo "<TD width=\"50%\" align=\"right\" title=\"Help\">";
$helpId="2.58";
showHelp($helpId);
echo "</TD>";
echo "</TR>";
echo "</TABLE>";

adjust_title_tail();
adjust_content_head();
echo "<br>";

	
	//show the page
	echo "<p><table  border=\"1\" width=\"100%\" cellpadding=\"2\" cellspacing=\"0\"  bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
	echo "<tr bgcolor=\"#E6E6E6\"><td align=center colspan=2><b>MSS Configuration Synchronization</b></td></tr>";
	echo "<tr>";
	echo "<td align=center>";
	if($operation==1)
	{
		echo "<input type=\"button\" disabled onClick=\"javascript:sync(0);\" value=\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MSS-0 to MSS-1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\" style=\"font-family: Verdana;\">";
		echo "<input type=\"button\" disabled onClick=\"javascript:sync(1);\" value=\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MSS-1 to MSS-0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\" style=\"font-family: Verdana;\">";
	}
	else
	{
		echo "<input type=\"button\" onClick=\"javascript:sync(0);\" value=\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MSS-0 to MSS-1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\" style=\"font-family: Verdana;\">";
		echo "<input type=\"button\" onClick=\"javascript:sync(1);\" value=\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MSS-1 to MSS-0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\" style=\"font-family: Verdana;\">";
	}
	echo "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td><font color=\"blue\" size=3><b>Warning: This operation will overwrite the MSS configuration data on the target server. </b></td>";
	echo "</tr>";
	echo "</table>";
	
	$ftpInfo=getMSSFtpAccount();	
	
	if($operation==1)
	{
		//start
		if($direction==0)
		{
			echo "<p><b>Start to synchronize from MSS-0 to MSS-1</b><br>";
			insertLog("Synchronize conf files from MSS-0 to MSS-1");
		}
		else
		{
			echo "<p><b>Start to synchronize from MSS-1 to MSS-0</b><br>";
			insertLog("Synchronize conf files from MSS-1 to MSS-0");
		}
		echo "The operation will last many minutes, please wait...<br>";
		
		flush();
		//check the heartbeat of mss-0
		echo "Check the connection to MSS-0...";
		flush();
		$database="OMC_PUB";
		$selSql = "SELECT ip FROM sysInfo where sysTypeNo=320 and sysNo=0 and baseStat=1 ";
		$selRes = @mysqli_query($pubConn,$selSql);
		$count=0;
		$count=mysqli_num_rows($selRes);
		echo mysqli_error($pubConn);
		$mss_0_ip="";
		if($count==1)
		{
			echo "<font color=blue>successful</font><br>";
			$selRow = @mysqli_fetch_array($selRes);
			$mss_0_ip=$selRow[ip];
			//echo "mss_0_ip=$mss_0_ip<br>";
		}
		else
		{
			echo "<font color=red>failed<br>";
			echo "The MSS-0 is unavailable<br></font>";
			back();
		}
		flush();
		//check the heartbeat of mss-1
		echo "Check the connection to MSS-1...";
		flush();
		$database="OMC_PUB";
		$selSql = "SELECT ip FROM sysInfo where sysTypeNo=320 and sysNo=1 and baseStat=1 ";
		$selRes = @mysqli_query($pubConn,$selSql);
		$count=0;
		$count=mysqli_num_rows($selRes);
		echo mysqli_error($pubConn);
		$mss_1_ip="";
		if($count==1)
		{
			echo "<font color=blue>successful</font><br>";
			$selRow = @mysqli_fetch_array($selRes);
			$mss_1_ip=$selRow[ip];
			//echo "mss_1_ip=$mss_1_ip<br>";
		}
		else
		{
			echo "<font color=red>failed<br>";
			echo "The MSS-1 is unavailable<br></font>";
			back();
		}
		flush();
		//get the conf file from mss
		/*
		$mss_conf_file=array("msc_art.conf",
							  				 "msc_conf",
							  				 "msc_cua.conf",
							  				 "msc_dps.conf",
							  				 "msc_la.conf",
							  				 "msc_mg.conf",
							  				 "msc_rzs.conf",
							  				 "msc_system.conf",
												 //"none.conf",
							  				 "msc_tg.conf");
		*/

		$get_mss_ip="";
		$put_mss_ip="";
		if($direction==0)
		{
			$get_mss_ip=$mss_0_ip;
			$put_mss_ip=$mss_1_ip;
			$get_msc_name="MSS-0";
			$put_msc_name="MSS-1";
		}
		else
		{
			$get_mss_ip=$mss_1_ip;
			$put_mss_ip=$mss_0_ip;
			$get_msc_name="MSS-1";
			$put_msc_name="MSS-0";
		}
		
		//get the conf file list by ftp
		$conn_id=ftp_connect($get_mss_ip);
		if($conn_id == FALSE)
		{
			echo "<b>FTP:connect to $get_mss_ip <font color=red>failed</font>.</b><br>";
			back();
		}
		if(FALSE == ftp_login($conn_id,$ftpinfo[UserName],$ftpinfo[Password]))
		{
			echo "<b>FTP:login to $get_mss_ip <font color=red>failed</font>.</b><br>";
			back();
		}
		
		$contents=ftp_rawlist($conn_id, "/usr/local/wxc2/bin/conf/");		
		foreach ($contents as $entry) 
		{
	    		$part=explode(" ",$entry);
	    		if(!strstr($part[0],"d"))
	    		{
	    			$mss_conf_file[]=$part[sizeof($part)-1];
	    		}
		}
		ftp_close($conn_id);
		
		//get conf file
		for($i=0;$i<sizeof($mss_conf_file);$i++)
		{
			$command="./getFile ".$get_mss_ip." ".$mss_conf_file[$i];
			system("rm -rf result.txt");
			system($command);
			$resFile=fopen("result.txt","r");
			$line=fgets($resFile);
			fclose($resFile);
			$line=trim($line);
			if(strlen($line)==0)
			{
				echo "Get conf file <font color=blue>".$mss_conf_file[$i]."</font> from $get_msc_name...<font color=blue>successful</font><br>";
			}
			else
			{
				echo "Get conf file <font color=blue>".$mss_conf_file[$i]."</font> from $get_msc_name...<font color=red>failed</font><br>";
				echo "<b>The operation is <font color=red>failed</font>.</b><br>";
				back();
			}
			flush();
			system("rm -rf result.txt");
		}
		
		//change the privilege and stop msc
		echo "<br>Prepare to update the conf file into ".$put_msc_name."<br>";
		echo "Stop the ".$put_msc_name."...";
		flush();
		system("./stopMsc ".$put_mss_ip);
		echo "<font color=blue>successful</font><br>";
		flush();
		
		//put file
		for($i=0;$i<sizeof($mss_conf_file);$i++)
		{
			system("rm -rf result.txt");
			$command="./putFile ".$put_mss_ip." ".$mss_conf_file[$i];
			system($command);
			
			$resFile=fopen("result.txt","r");
			$line=fgets($resFile);
			fclose($resFile);
			if(strlen($line)==0)
			{
				echo "Put conf file <font color=blue>".$mss_conf_file[$i]."</font> to $put_msc_name...<font color=blue>successful</font><br>";
			}
			else
			{
				echo "Put conf file <font color=blue>".$mss_conf_file[$i]."</font> to $put_msc_name...<font color=red>failed</font><br>";
			}
			flush();
			system("rm -rf {$mss_conf_file[$i]}");
		}
		
		//change the privilege and stop msc
		echo "Start the ".$put_msc_name."...";
		flush();
		system("./startMsc ".$put_mss_ip);
		echo "<font color=blue>successful</font><br>";
		flush();

		
		echo "<b>The operation is <font color=blue>successful</font>.</b><br>";
		back();
	}
	
	
	
	
	//function define
	function back()
	{
		global $mss_conf_file;
		for($i=0;$i<sizeof($mss_conf_file);$i++)
		{
			system("rm -rf {$mss_conf_file[$i]}");
		}
		system("rm -rf result.txt");
		echo "<p align=\"left\"><a href=\"JavaScript:history.go(-1)\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">Back</a>";
		exit;
	}
adjust_content_tail();
?>
