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

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

//==========================================================
//变量定义
//==========================================================
$pubDb				= 'OMC_PUB';
$sysInfoTable		= 'OMC_PUB.sysInfo';

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

$impFileArr[0][localFile]	='impFile_1';
$impFileArr[0][remoteFile]	='/usr/local/apache/htdocs/wxc2_omc/fault/networkStatus/images/map.jpg';

if($DEBUG)echo "<br>ftpLoginName=$ftpLoginName ";
if($DEBUG)echo "<br>ftpLoginPwd=$ftpLoginPwd";

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

function putImpFile($ftpCon)
{
	global $impFileArr,$impFile_1,$language;
	for($j=0;$j<sizeof($impFileArr);$j++){
		$localFile	=${$impFileArr[$j][localFile]};
		$remoteFile	=$impFileArr[$j][remoteFile];
		/* close ftp
		$delRes	= ftp_delete($ftpCon,$remoteFile);
		if($language == 'chn'){
			if(FALSE == $delRes)	echo "没有删除 map.jpg 的权限<br>";
		}else{
			if(FALSE == $delRes)	echo "you have no priviledge to delete map.jpg<br>";
		}
		*/
		/* close ftp
		if(!ftp_put($ftpCon,$remoteFile,$localFile,FTP_ASCII)){
		*/
		if(!sftp_put($ftpCon,$remoteFile,$localFile,SFTP_ASCII)){
			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";

//==========================================================
//数据处理
//==========================================================
//判断是否有文件上载
if( $_SERVER['REQUEST_METHOD'] == "POST" && $impFile_1 != "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);
	$omcNum=mysqli_num_rows($getSysIdResult);
	if($omcNum <= 0){
		if($language == 'chn'){
			echo "[错误]：系统列表中没有配置 OMC 的IP!";
		}else{
			echo "Error:Can not find any OMC's IP in system list,Please ADD it!";
		}
		delImpFile();
		exit();
	}
	$impOkFlag=0;
	for($i=0;$i<$omcNum;$i++){
		$getSysIdRow=mysqli_fetch_array($getSysIdResult);
		$sysName='OMC_'.$getSysIdRow[sysNo].'_'.$getSysIdRow[subSysNo];
		$dstSeverIp = $getSysIdRow[ip];
		if($DEBUG) echo "<br>sysId = $sysId";
		if($DEBUG) echo "<br>dstSeverIp = $dstSeverIp";

		if($language == 'chn'){
			echo "<br>上传地图到 $sysName ( $dstSeverIp ) ......";
		}else{
			echo "<br> Try to upload omc map to $sysName($dstSeverIp) ......";
		}
		flush();

		/* close ftp
		$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{
			//echo "<br><font color=\"#0000FF\">Connect $sysName OK</font>";
			flush();
		}
		*/
		
		$connection=sftp_connect($dstSeverIp);
		if(!$connection){
			if($language == 'chn'){
				echo "<br><font color=\"#FF0000\">连接 $sysName 失败!</font>";
			}else{
				echo "<br><font color=\"#FF0000\">Connect $sysName Failed!</font>";
			}
			continue;
		}else{
			//echo "<br><font color=\"#0000FF\">Connect $sysName OK</font>";
			flush();
		}
		
		/* close ftp
		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{
			//echo "<br><font color=\"#0000FF\">Login $sysName OK</font>";
			flush();
		}
		*/
		
		$key=sftp_login($connection,$ftpLoginName,$ftpLoginPwd);
		if(!$key){
			if($language == 'chn'){
				echo "<br><font color=\"#FF0000\">登陆 $sysName 失败!</font>";
			}else{
				echo "<br><font color=\"#FF0000\">Login $sysName Failed!</font>";
			}
			continue;
		}else{
			//echo "<br><font color=\"#0000FF\">Login $sysName OK</font>";
			flush();
		}
		
		/* close ftp
		if(putImpFile($ftpCon)){
			$impOkFlag = 1;
		}
		@ftp_quit($ftpCon);
		*/
		
		if(putImpFile($key)){
			$impOkFlag = 1;
		}
		
		flush();
	}
	delImpFile();

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

	flush();
	if($language == 'chn'){
		echo "<br><br><br><a href=\"$PHP_SELF\"> &nbsp;<< 返回 </a>";
	}else{
		echo "<br><br><br><a href=\"$PHP_SELF\"> &nbsp;<< Return </a>";
	}
	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 system map 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%\">";
if($language == 'chn'){
	echo "高级管理 > 系统地图 >地图上载 ";
}else{
	echo "Advanced > System Map > Central Office Map ";
}
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 
	if($language == 'chn'){
		echo "地图上载"; 
	}else{
		echo "Central office map file import"; 
	}
	?></b></font>
</td>
</tr>
<tr>
<td colspan=2>
<font color="#0000FF" size=2>
<?php
	if($language == 'chn'){
		echo "注意：图片大小 ＝ 750 * 500，文件大小 < 200K ";
	}else{
		echo "Note: image size = 750*500, max file size = 200K";
	}
?>
</font>
</td>
</tr>
<tr>
<td>
<?php
	if($language == 'chn'){
		echo "选择地图文件：(*.jpg)";
	}else{
		echo "Select system map file:(*.jpg)";
	}
?></td>
<td>
<input type="file" name="impFile_1" class="button">
<input type="hidden" name="MAX_FILE_SIZE" value="204800"><!-- MAX_FILE_SIZE = 1M -->
</td>
</tr>
<tr>
<td colspan=2>
<?php
echo "<input type=\"submit\" $submitStat value=\"$strSubmit\" class=\"button\">";
?>
</td>
</tr>
</table>
</form>
<?php
if($language == 'chn'){
	echo "<b>说明：</b><br><br>";
	echo "上传文件成功后，请关掉所有的浏览器窗口，再重新打开浏览器，地图更改才能生效。";
}else{
	echo "<b>Remarks:</b><br><br>";
	echo "Change OMC system map. after uploaded this file, close all explorer windows and restart, newly updated map should be displayed.";
}
?>
</body>
</html>
