<?php 
/*********************************************************      
 程序说明：         
    功能说明：PPS语音数据的导入
              
    调用关系：调用: header.inc   
              被调用:        
 
    变量说明：

    作者：
      
修改注释：      
NO.1      
姓名：    
时间：
修改说明：
*********************************************************/
require("../../inc/header.inc"); 
$helpId = "6.2.2"; 
$DEBUG=0;

//==========================================================
//变量定义
//==========================================================
$nGetComm	= 0;
$nSetComm	= 3;
$nSubSysParam		= 0;
$nPubSubSysParam	= 1;
$nPubSysParam		= 2;

$pubDb				= 'OMC_PUB';
$sysSubsConfTable	= 'OMC_PUB.sysSubsConf';
$systemConfTable	= 'OMC_PUB.sysConf';
$sysInfoTable		= 'OMC_PUB.sysInfo';

$sysTypeNo	=360;
$sysTypeName = 'PPS';



$impObjId			= "2.1.31";
$ftpInfo=getMSSFtpAccount();
$ftpLoginName		= $ftpInfo[UserName];
$ftpLoginPwd		= $ftpInfo[Password];
$paramType			= $nPubSysParam;
$maxImpFileSize		= 1024*1024*5; //5M
$maxImpFileNum		= 2;
$maxImpRecordNum	= 100000;

$impFileArr[0][localFile]	='impFile_1';
$impFileArr[0][remoteFile]	='/tmp/digital_syntax.csv';
$impFileArr[1][localFile]	='impFile_2';
$impFileArr[1][remoteFile]	='/tmp/ivr_syntax.csv';

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 ";

function delImpFile()
{
	global $impFileArr,$impFile_1,$impFile_2;
	for($j=0;$j<sizeof($impFileArr);$j++){
		$localFile	=${$impFileArr[$j][localFile]};
		@unlink ($localFile); 
	}
}

function putImpFile($ftpCon)
{
	global $impFileArr,$impFile_1,$impFile_2;
	for($j=0;$j<sizeof($impFileArr);$j++){
		$localFile	=${$impFileArr[$j][localFile]};
		$remoteFile	=$impFileArr[$j][remoteFile];
		if(!ftp_put($ftpCon,$remoteFile,$localFile,FTP_ASCII)){
			echo "<br><font color=\"#FF0000\">Put file $remoteFile Failed</font>";
			return(-1);
		}else{
			echo "<br><font color=\"#0000FF\">Put file $remoteFile OK</font>";
		}
	}
	return(1);
}

function chkImpFile()
{
	$DEBUG=0;
	global $impFileArr,$impFile_1,$impFile_2;
	global $maxImpFileSize,$maxImpRecordNum;
	//检查导入的文件大小，记录条数
	for($j=0;$j<sizeof($impFileArr);$j++){
		$impFileSize = filesize(${$impFileArr[$j][localFile]});
		if($DEBUG) echo "<br>impFileSize=$impFileSize";
		if($impFileSize > $maxImpFileSize){
			echo "<br>impRecordNum=$impRecordNum";
			echo "<br><font color=\"#FF0000\">File size is too large!</font>";
			return(-1);
		}
		$impRecordNum = count(file(${$impFileArr[$j][localFile]}));
		if($DEBUG) echo "<br>impRecordNum=$impRecordNum";
		if($impRecordNum > $maxImpRecordNum){
			echo "<br><font color=\"#FF0000\">Records are too much</font>";
			return(-1);
		}
	}
	return(1);
}

//==========================================================
//输入变量
//==========================================================
if($DEBUG) echo "<br>impFile_1=$impFile_1";
if($DEBUG) echo "<br>impFile_2=$impFile_2";

//==========================================================
//数据处理
//==========================================================
//判断是否有文件上载
if( $_SERVER['REQUEST_METHOD'] == "POST" && $impFile_1 != "none" && $impFile_2 != "none"){
	if(chkImpFile() < 0){
		delImpFile();
		exit();
	}
	
	//查找是否有运行的系统被导入参数
	$getSysIdSql = "SELECT sysNo,subSysNo,ip 
				FROM $sysInfoTable
				WHERE sysTypeNo='$sysTypeNo'
				ORDER BY sysNo,subSysNo
			";
	if($DEBUG) echo "<br>getSysIdSql=$getSysIdSql";
	$getSysIdResult= mysqli_query($pubConn,$getSysIdSql);
	echo mysqli_error($pubConn);
	$ppsNum=mysqli_num_rows($getSysIdResult);
	if($ppsNum <= 0){
		echo "Error:Can not find any PPS's IP in system list,Please ADD it!";
		delImpFile();
		exit();
	}
	$impOkFlag=0;
	for($i=0;$i<$ppsNum;$i++){
		$getSysIdRow=mysqli_fetch_array($getSysIdResult);
		$sysName='PPS_'.$getSysIdRow[sysNo].'_'.$getSysIdRow[subSysNo];
		$dstSeverIp = $getSysIdRow[ip];
		if($DEBUG) echo "<br>sysId = $sysId";
		if($DEBUG) echo "<br>dstSeverIp = $dstSeverIp";

		echo "<br> Try to ftp voice parameter files to $sysName($dstSeverIp) ......";
		flush();

		$ftpCon=@ftp_connect($dstSeverIp);
		if($ftpCon < 1){
			echo "<br><font color=\"#FF0000\">Connect $sysName Failed!</font>";
			continue;
		}else{
			echo "<br><font color=\"#0000FF\">Connect $sysName OK</font>";
			flush();
		}
		if(!@ftp_login($ftpCon,$ftpLoginName,$ftpLoginPwd)){
			echo "<br><font color=\"#FF0000\">Login $sysName Failed!</font>";
			@ftp_quit($ftpCon);
			continue;
		}else{
			echo "<br><font color=\"#0000FF\">Login $sysName OK</font>";
			flush();
		}
		if(putImpFile($ftpCon)){
			$impOkFlag = 1;
		}
		@ftp_quit($ftpCon);
		
		flush();
	}
	delImpFile();

	if($impOkFlag == 0){
		exit();
	}

	echo "<br> Try to send command to $sysTypeName ......";
	flush();

	switch($paramType){
		case $nPubSubSysParam:
			$sysId=$sysTypeNo.'_'.$getSysIdRow[sysNo].'_99';
			break;
		case $nPubSysParam:
			$sysId=$sysTypeNo.'_99_99';
			break;
		default:
			break;
	}
	//修改设备参数
	$instanceNo	= 0;
	$commState	= '01';
	$newValue	= '01';
	$chgResult	= setSysParamValue($sysId,$impObjId,$instanceNo,$newValue);

	//send set command
	if($DEBUG) echo "<br>sysCommSend($sysId,$nSetComm,$impObjId)";
	$commStr	= sysCommSend($sysId,$nSetComm,$impObjId);
	sleep(2);
	
	echo "OK!";
	flush();
	echo "<br><br><br><a href=\"$PHP_SELF\"> &nbsp;<< Return </a>";
	exit();
}elseif(isset($impFile)){
	echo "<br><font color=\"#FF0000\" size=\"3\">";
	echo "<b>Upload file failed, File isn't exist or file is empty!</b></font>";
}

//==========================================================
//数据显示
//==========================================================
?>

<script LANGUAGE="JavaScript">      
function checkAllInput(theform)
{           
	if(theform.impFile_1.value.length == 0 || theform.impFile_2.value.length == 0){
		alert('Plase select parameter file!');
		if(theform.impFile_1.value.length == 0){
			theform.impFile_1.focus();
		}else{
			theform.impFile_2.focus();
		}
		return(false);
	}
    return (true);
}
</script>
<body topmargin="20" leftmargin="6">
<form method="POST" action="<?php echo "$PHP_SELF";?>" name="subsData" enctype="multipart/form-data"  onsubmit="return checkAllInput(this)">
<?php
echo "<table border=\"0\" width=\"100%\">";
echo "<tr>";
echo "<td width=\"50%\">";
echo "Advance > Network > PPS voice file ";
echo "</td>";
echo "<td width=\"50%\" align=\"right\" title=\"Help\">";
showHelp($helpId);
echo "</td>";
echo "</tr>";
echo "</table>";
?>
<br>
<table  border="1" width="100%" cellpadding="2" cellspacing="0"  bordercolor="#666666" bordercolordark="#FFFFFF" > 
<tr bgcolor="#E6E6E6" >    
<td width="100%" colspan="2" align="center">
	<font size=2><b><?php echo "$strPPSParamImport"; ?></b></font>
</td>
</tr>
<tr>
<td colspan=2>
<font color="#0000FF" size=2>Note: Import file's size <= 5M, records <= 5000</font>
</td>
</tr>
<tr>
<td>Select file 1:(digital_syntax.csv)</td>
<td>
<input type="file" name="impFile_1" class="button">
<input type="hidden" name="MAX_FILE_SIZE" value="20971520"><!-- MAX_FILE_SIZE = 20M -->
</td>
</tr>
<tr>
<td>Select file 2:(ivr_syntax.csv)</td>
<td>
<input type="file" name="impFile_2" class="button">
</td>
</tr>
<tr>
<td colspan=2>
<?php
echo "<input type=\"submit\" $submitStat value=\"$strSubmit\" class=\"button\">";
?>
</td>
</tr>
</table>
</form>
<b>Remarks:</b><br><br>
The digital_syntax.csv is used to set the pronunciation of numbers for up to eight languages. The ivr_syntax.csv is used to set dial-in numbers for recharge and balance inquiry.
</body>
</html>
