<?php 
/*********************************************************      
 程序说明：         
    功能说明：wav导入
              
    调用关系：调用: header.inc   
              被调用:        
 
    变量说明：

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

//==========================================================
//变量定义
//==========================================================
if(isset($backurl)){
	$backurl = rawurldecode($backurl);
}


$pubDb				= 'OMC_PUB';

//$omc_sysTypeNo	=0;	//OMC
$ftpInfo=getMSSFtpAccount();
$ftpLoginName		= $ftpInfo[UserName];
$ftpLoginPwd		= $ftpInfo[Password];
$maxImpFileSize		= 1024*2000; //2000K

$remotepath = '/usr/local/prompt/';
$localpath  = '/usr/local/apache/htdocs/wxc2_omc/element/toElement/aasAnnounce/prompt/';
$strName = 'AAS';
if(isset($vmsvoice))
{
	$remotepath = '/usr/local/vms/prompt/';
	$localpath  = '/usr/local/apache/htdocs/wxc2_omc/element/toElement/vmsAnnounce/prompt/';
	$strName = 'VMS';
}
$impFileArr[0][localFile]	='impFile_1';
$impFileArr[0][remoteFile] = $remotepath."$voicefile";


$defaultomcfile = $localpath."$voicefile";
$defaultaasfile = $remotepath."$voicefile";

if($DEBUG)echo "<br>ftpLoginName=$ftpLoginName ";
if($DEBUG)echo "<br>ftpLoginPwd=$ftpLoginPwd";
if($DEBUG)echo "<br>impFileArr[0][localFile]={$impFileArr[0][localFile]}";

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

function putImpFile($ftpCon)
{
	global $defaultaasfile,$defaultomcfile,$impFileArr,$impFile_1,$language,$default;
	for($j=0;$j<sizeof($impFileArr);$j++){
		if($default == 1){
			$localFile	=$defaultomcfile;
			$remoteFile	=$defaultaasfile;
		}else{
			$localFile	=${$impFileArr[$j][localFile]};
			$remoteFile	=$impFileArr[$j][remoteFile];
		}
/*		
		$delRes	= ftp_delete($ftpCon,$remoteFile);
		if($language == 'chn'){
			if(FALSE == $delRes)	echo "没有删除 文件 的权限<br>";
		}else{
			if(FALSE == $delRes)	echo "you have no priviledge to delete files<br>";
		}
*/		

		//echo "r = $remoteFile, L =$localFile<br>";
		if(!@ftp_put($ftpCon,$remoteFile,$localFile,FTP_BINARY)){
			if($language == 'chn'){
				echo "<br><font color=\"#FF0000\">上传失败.</font>";
			}else{
				echo "<br><font color=\"#FF0000\">Failed.</font>";
			}
			return(-1);
		}else{
			if($language == 'chn'){
				echo "<br><font color=\"#0000FF\">上传成功.</font>";
			}else{
				echo "<br><font color=\"#0000FF\">Successful.</font>";
			}
		}
	}
	return(1);
}

function chkImpFile()
{
	$DEBUG=0;
	global $impFileArr,$impFile_1;
	global $maxImpFileSize,$language;
	//检查导入的文件大小
	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";
			if($language == 'chn'){
				echo "<br><font color=\"#FF0000\">文件过大!</font>";
			}else{
				echo "<br><font color=\"#FF0000\">File size is too large!</font>";
			}
			return(-1);
		}
	}
	return(1);
}

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

//==========================================================
//数据处理
//==========================================================


//判断是否有文件上载  dafault--恢复默认语音
if(($_SERVER['REQUEST_METHOD'] == "POST" && $impFile_1 != "none")|| $default == 1){
	if($default !=1 && chkImpFile() < 0){
		delImpFile();
		exit();
	}
	$sysIdArr	= explode('_',$sysId);
	$sysTypeNo	= $sysIdArr[0];
	$sysNo		= $sysIdArr[1];
	$subSysNo	= $sysIdArr[2];	
		
	$getSysIdSql = "SELECT sysNo,subSysNo,ip FROM sysInfo
				WHERE sysTypeNo='$sysTypeNo' AND sysNo='$sysNo' AND subSysNo='$subSysNo'
			";
	if($DEBUG) echo "<br>getSysIdSql=$getSysIdSql";
	$getSysIdResult= mysqli_query($pubConn,$getSysIdSql);
	echo mysqli_error($pubConn);
	$omcNum=mysqli_num_rows($getSysIdResult);
	if($omcNum <= 0){
		if($language == 'chn'){
			echo "[错误]：系统列表中没有配置 $strName 的IP!";
		}else{
			echo "Error:Can not find any $strName's IP in system list,Please ADD it!";
		}		
		if($default !=1){
			delImpFile();
		}
		if($default == 1){
			$backpage =1;
		}else{
			$backpage =2;
		}		
		if(isset($backurl)){
			echo " <meta http-equiv=\"refresh\" content=\"2; url=$backurl\"> ";
		}else{
			echo " <meta http-equiv=\"refresh\" content=\"2; url=javascript:history.go(-$backpage)\"> ";
		}		
		exit();
	}
	$impOkFlag=0;
	for($i=0;$i<$omcNum;$i++){
		$getSysIdRow=mysqli_fetch_array($getSysIdResult);
		$sysName=$strName.'_'.$getSysIdRow[sysNo].'_'.$getSysIdRow[subSysNo];
		$dstSeverIp = $getSysIdRow[ip];

		if($language == 'chn'){
			echo "<br>上传语音文件到 $sysName ( $dstSeverIp ) ......";
		}else{
			echo "<br> Try to upload $strName wav to $sysName($dstSeverIp) ......";
		}
		flush();

		$ftpCon=@ftp_connect($dstSeverIp);
		if($ftpCon < 1){
			if($language == 'chn'){
				echo "<br><font color=\"#FF0000\">连接 $sysName 失败!</font>";
			}else{
				echo "<br><font color=\"#FF0000\">Connect $sysName Failed!</font>";
			}
			continue;
		}else{
			flush();
		}
		if(!@ftp_login($ftpCon,$ftpLoginName,$ftpLoginPwd)){
			if($language == 'chn'){
				echo "<br><font color=\"#FF0000\">登陆 $sysName 失败!</font>";
			}else{
				echo "<br><font color=\"#FF0000\">Login $sysName Failed!</font>";
			}
			@ftp_quit($ftpCon);
			continue;
		}else{
			flush();
		}
		if(putImpFile($ftpCon)){
			$impOkFlag = 1;
		}
		@ftp_quit($ftpCon);
		
		flush();
	}
	if($default !=1){
		delImpFile();
	}

	if($impOkFlag == 0){
		if($default == 1){
			$backpage =1;
		}else{
			$backpage =2;
		}		
		if(isset($backurl)){
			echo " <meta http-equiv=\"refresh\" content=\"2; url=$backurl\"> ";
		}else{
			echo " <meta http-equiv=\"refresh\" content=\"2; url=javascript:history.go(-$backpage)\"> ";
		}				
		exit();
	}

	flush();
	if($default == 1){
		$backpage =1;
	}else{
		$backpage =2;
	}
	if(isset($backurl)){
		echo " <meta http-equiv=\"refresh\" content=\"2; url=$backurl\"> ";
	}else{
		echo " <meta http-equiv=\"refresh\" content=\"2; url=javascript:history.go(-$backpage)\"> ";

	}
	exit();
}elseif(isset($impFile)){
	echo "<br><font color=\"#FF0000\" size=\"3\">";
	if($language == 'chn'){
		echo "<b>上传失败，文件不存在！</b></font>";
	}else{
		echo "<b>Upload file failed, File doesn't exist!</b></font>";
	}
}

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

<script LANGUAGE="JavaScript">      
function checkAllInput(theform)
{           
	if(theform.impFile_1.value.length == 0 ){
		alert('Plase select file!');
		theform.impFile_1.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 "Advanced > Upload voice file. ";
echo "Configuration > <a href=\"../overview/overview.php\">Network</a> > <a href=\"$backurl\">$strName</a> > <a href=\"#\">Upload voice file</a>";
echo "</td>";
echo "<td width=\"50%\" align=\"right\" title=\"Back\">";
//showHelp($helpId);
if(isset($backurl)){
	echo "<input type=\"hidden\" name=\"backurl\" value=\"$backurl\">";
	echo "<p align=\"right\"><a href=\"$backurl\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">$strBack</a>";
}else{
	echo "<p align=\"right\"><a href=\"javascript:history.go(-1)\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">$strBack</a>";
}

echo "</td>";
echo "</tr>";
echo "</table>";
echo "<input type=\"hidden\" name=\"voicefile\" value=\"$voicefile\">";
echo "<input type=\"hidden\" name=\"default\" value=\"$default\">";
echo "<input type=\"hidden\" name=\"sysId\" value=\"$sysId\">";
echo "<input type=\"hidden\" name=\"vmsvoice\" value=\"$vmsvoice\">";
?>
<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 
	if($language == 'chn'){
		echo "语音上载"; 
	}else{
		echo "Voice file import"; 
	}
	?></b></font>
</td>
</tr>
<tr>
<td colspan=2>
<font color="#0000FF" size=2>
<?php
	if($language == 'chn'){
		echo "WAV ";
	}else{
		echo "WAV";
	}
?>
</font>
</td>
</tr>
<tr>
<td>
<?php
	if($language == 'chn'){
		echo "选择语音文件：(*.wav)";
	}else{
		echo "Select Voice file:(*.wav)";
	}
?></td>
<td>
<input type="file" name="impFile_1" class="button">
<input type="hidden" name="MAX_FILE_SIZE" value="2048000"><!-- MAX_FILE_SIZE = 1M -->

</td>
</tr>
<tr>
<td colspan=2>
<?php
echo "<input type=\"submit\" $submitStat value=\"$strSubmit\" class=\"button\">";
?>
</td>
</tr>
</table>
</form>
</body>
</html>
