<?php 
/*********************************************************      
 程序说明：         
    功能说明：导入system.ini,控制MSS License
              
    调用关系：调用: 
              被调用:        
 
    变量说明：

    作者：    
      
修改注释：      
NO.1      
姓名：      
时间：    
修改说明：   
*********************************************************/
require("../sub_inc/commfunc.php");
$DEBUG = 0;

$nGetComm	= 0;
$nSetComm	= 3;

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

$omc_db				= 'OMC_PUB';
$sysInfoTable		= 'OMC_PUB.sysInfo';

$impObjId			= '2.1.2';
$impFileName	= 'system.ini';
$ftpDir				= '/usr/local/subsManage/';
$ftpLoginName	= 'subsManager';
$baseLogFileName	= 'fib.txt';
$ftpLoginPwd	= 'iammanager';
$paramType			= $nGlobalSysParam;
$maxImpFileSize		= 4*1024;

if($DEBUG)echo "<br>impObjId= $impObjId";
if($DEBUG)echo "<br>impFileName=$impFileName ";
if($DEBUG)echo "<br>ftpDir=$ftpDir ";
if($DEBUG)echo "<br>ftpLoginName=$ftpLoginName ";
if($DEBUG)echo "<br>ftpLoginPwd=$ftpLoginPwd";
if($DEBUG)echo "<br>paramType=$paramType ";
if($DEBUG)echo "<br>maxImpFileSize=$maxImpFileSize ";

//判断是否有文件上载
if(isset($impFile) && $impFile != "none"){
	$getSysIdSql = "SELECT sysNo,subSysNo,ip 
				FROM $sysInfoTable
				WHERE sysTypeNo='200' AND sysNo='9' AND baseStat > '0'
				ORDER BY sysNo,subSysNo
			";
	if($DEBUG) echo "<br>getSysIdSql=$getSysIdSql";
	$getSysIdResult= mysqli_query($pubConn,$getSysIdSql);
	echo mysqli_error($pubConn);
	$runningServerNum	= mysqli_num_rows($getSysIdResult);
	if($runningServerNum <= 0){
		echo "<br><b>Error: No system run normally</font>";
	}
	if($DEBUG)	echo "running server number = $runningServerNum<br>";
	
	//检查导入的文件大小，记录条数
	$impFileSize = @filesize($impFile);
	if($DEBUG) {
		echo "<br>impFile=$impFile";
		echo "<br>impFileSize=$impFileSize";
	}
	if($impFileSize > $maxImpFileSize){
		echo "<br><b>Import File Size is $impFileSize bytes";
		echo "<br>File size is too large!(Must be <= $maxImpFileSize)</font>";
		@unlink ($impFile); 
		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();
	}

	if($impFileSize <=0){
		echo "<br><b>No import File";
		@unlink ($impFile); 
		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();
	}

	$submitStat = 'DISABLED';
	for ($k=0; $k<$runningServerNum; $k++) {
		$getSysIdRow=mysqli_fetch_array($getSysIdResult);
		$dstSeverIp = $getSysIdRow[ip];
		$dstSeverIpArr[$k] = $getSysIdRow[ip];
		if($DEBUG) echo "<br>dstSeverIp = $dstSeverIp";
		
		/* close ftp
		$ftpCon=@ftp_connect($dstSeverIp);
		if($ftpCon < 0){
			echo "<br><b>Connect $dstSeverIp Failed!</font>";
			@unlink ($impFile);
			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();
		}
		*/
		
		$connection=sftp_connect($dstSeverIp);
		if(!$connection)
		{
			echo "<br><b>Connect $dstSeverIp Failed!</font>";
			@unlink ($impFile);
			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();
		}
		
		/* close ftp
		if(!@ftp_login($ftpCon,$ftpLoginName,$ftpLoginPwd)){
			echo "<br><b>Login $dstSeverIp Failed!</font>";
			@unlink ($impFile);
			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();		
		}
		*/
		
		$key=sftp_login($connection,$ftpLoginName,$ftpLoginPwd);
		if(!$key)
		{
			echo "<br><b>Login $dstSeverIp Failed!</font>";
			@unlink ($impFile);
			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();		
		}

		$remoteFileName=$ftpDir.$impFileName;
		if($DEBUG)echo "$ftpCon,$remoteFileName,$impFile";
		/* close ftp
		if(!@ftp_put($ftpCon,$remoteFileName,$impFile,FTP_BINARY)){
			echo "<br><b>Put file to $dstSeverIp Failed</font>";
			exit();
			@unlink ($impFile);
			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();	
		}
		ftp_quit($ftpCon);
		*/
		
		if(!sftp_put($key,$remoteFileName,$impFile,SFTP_ASCII))
		{
			echo "<br><b>Put file to $dstSeverIp Failed</font>";
			exit();
			@unlink ($impFile);
			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();	
		}
	}
	@unlink ($impFile); 
	
	$sysId='388_99_99';
	if($DEBUG)	echo "<br>(2) sysId=$sysId";
	
	//send set command
	
	$chgResult	= setSysParamValue('388_99_99',$impObjId,$instanceNo,'00');
	$commIdArr	= sysCommSend('388_99_99',$nSetComm,$impObjId);			
	if($DEBUG){
		echo "setSysParamValue($sysId,$impObjId,$instanceNo,$newValue)<br>";
		echo "sysCommSend($sysId,$nSetComm,$impObjId)";
	}
	
	
	CheckState($commIdArr);
	echo "<br><font color=\"#0000FF\" size=2>Finished</font>";
	
	for($k=0;$k<sizeof($dstSeverIpArr);$k++){
		
		if($DEBUG)echo "<br>dstSeverIp=$dstSeverIp";
		$dstSeverIp=$dstSeverIpArr[$k];
		/* close ftp
		$ftpCon=@ftp_connect($dstSeverIp);
		if($ftpCon < 0){
			echo "<br><b>Connect $dstSeverIp Failed!</font>";
			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();	
		}
		*/
		
		$connection=sftp_connect($dstSeverIp);
		if(!$connection){
			echo "<br><b>Connect $dstSeverIp Failed!</font>";
			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();	
		}
		
		/* close ftp
		if(!@ftp_login($ftpCon,$ftpLoginName,$ftpLoginPwd)){
			echo "<br><b>Login $dstSeverIp Failed!</font>";
			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();		
		}
		*/
		
		$key=sftp_login($connection,$ftpLoginName,$ftpLoginPwd);
		if(!$key)
		{
			echo "<br><b>Login $dstSeverIp Failed!</font>";
			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();		
		}
		
		$remoteFileName	=$ftpDir.$baseLogFileName;
		$timestamp	= time();
		$nameArray = explode(".",$baseLogFileName);
		$logFileName = $nameArray[0].$timestamp."_".$k.".log";
		$localFileName	="/usr/local/apache/htdocs/ftpFile/$logFileName";
  		/*	
		//delete *.log
		$handle=opendir('/usr/local/apache/htdocs/ftpFile/');
		while($file=readdir($handle)){
			$retval="";
			$pt=strrpos($file,".");
			if($pt) $retval=substr($file,$pt+1,strlen($file)-$pt);
			$delFileName = "/usr/local/apache/htdocs/ftpFile/".$file;
			if($retval=='log') unlink($delFileName);
		}
		*/
		//echo "$ftpCon,$localFileName,$remoteFileName";	
		/* close ftp
		if(!ftp_get($ftpCon,$localFileName,$remoteFileName,FTP_ASCII)){
			echo "<br><b>Get log file from $dstSeverIp Failed</font>";
			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();	
		}
		ftp_quit($ftpCon);
  	*/
  	
  	//if(!sftp_get($key,$localFileName,$remoteFileName,SFTP_ASCII))
  	if(!sftp_get($connection,$localFileName,$remoteFileName,SFTP_ASCII))
  	{
  		echo "<br><b>Get log file from $dstSeverIp Failed</font>";
			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();	
  	}

		$logFileUrl="../../../ftpFile/$logFileName";
		echo "<br><br><a href=\"$logFileUrl\">Click here to open the log&nbsp;&nbsp;($dstSeverIp).</a>";
	}
	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();		
}elseif(isset($impFile)){
	echo "<br><b><b>Upload file failed, File isn't exist or file is empty!</b></font>";
}
?>
<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%\">";
echo "$strConfiguration > MSS License Control";
echo "</td>";
echo "<td width=\"50%\" align=\"right\">";
$helpId="2.61";
showHelp($helpId);
echo "</td>";
echo "</tr>";
echo "</table>";	
adjust_title_tail();
adjust_content_head();
echo "<br>";
?>


<form method="POST" action="<?php echo "$PHP_SELF";?>" name="subsData" enctype="multipart/form-data">
<table  border="1" width="100%" cellpadding="2" cellspacing="0"  bordercolor="#666666" bordercolordark="#FFFFFF" > 

<?php
	$title = "Import system.ini ";
	echo "<tr bgcolor=\"#E6E6E6\">";
	echo "<td width=100% colspan=2 align=center>";
	echo "<font size=3><b>$title</b></font>";
	echo "</td>";
	echo "</tr>";
?>

<tr>
<td colspan=2>
<font color="#0000FF" size=2>Note:Import File's size <= <?php echo $maxImpFileSize?> bytes</font>
</td>
</tr>
<tr>
<td>Select File:</td>
<td>
<input type="file" name="impFile">
<input type="hidden" name="MAX_FILE_SIZE" value="20971520"><!-- MAX_FILE_SIZE = 20M -->
</td>
</tr>

<tr>
<td colspan=2>
<?php
if($DEBUG) echo "<br>submitStat=$submitStat";

if($submitStat == 'DISABLED'){
	echo "<input type=\"submit\" DISABLED value=\"$strSubmit\">";
}else{
	echo "<input type=\"submit\"  value=\"$strSubmit\">";
}

echo "<input Name=\"dataTypeNo\" type=\"hidden\" value=\"$dataTypeNo\">";
echo "<input Name=\"sysId\" type=\"hidden\" value=\"$sysId\">";
?>
</td>
</tr>
</table>
</form>
<?php
adjust_content_tail();
?>
</body>
</html>
