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

    作者：     
      
修改注释：      
NO.1      
姓名：   
时间：2003-03-10    
修改说明：优化流程，规范变量命名    
*********************************************************/
require("../../inc/header.inc");
global $pubConn;
function getImpParamValue($sysId,$objIdStr,$instanceNo,$valueName)
{

	global $paramConf_table,$OMC_server;

	$DEBUG=0;

	$tmpSysIdArr = explode('_',$sysId);
	$sysTypeNo	= $tmpSysIdArr[0];
	$sysNo		= $tmpSysIdArr[1];
	$subSysNo	= $tmpSysIdArr[2];
	$objDbConn = mysqli_connect($OMC_server[0]['host'],$OMC_server[0]['user'],$OMC_server[0]['password'], 'OBJ_'.$sysTypeNo);

	$paramDb = 'OBJ_'.$sysTypeNo;
	//获取参数定义表的表名，根据oid取出参数定义
	$tmpObjArr = explode('.',$objIdStr);
	$objWhereStr='';
	for($i=0;$i<sizeof($tmpObjArr);$i++){
		$j=$i+1;
		$objWhereStr .= ' level_'.$j."='{$tmpObjArr[$i]}' AND ";
	}
	$objWhereStr = substr($objWhereStr,0,-5); //最后多了一个' AND ',将其去掉
	
	//根据参数类型，推出参数表的表名
	if($sysTypeNo==390)
	{
		$paramTable = 'param_'.$sysNo;
	}
	else
	{
		$paramTable = 'param_99';
		$subSysNo = '99';
	}

	//subSysNo = '$subSysNo' AND instanceNo = '$instanceNo' AND 

	//修改对应的参数值
	if($DEBUG) echo"<br>valueName=$valueName <br>";
	$getParamValueSql="SELECT $valueName FROM $paramTable 
			WHERE $objWhereStr and instanceNo <> ''
			";
	if($DEBUG) echo"<br> aaa=$getParamValueSql <br>";
	$getResult=mysqli_query($objDbConn,$getParamValueSql);
	echo mysqli_error($objDbConn);
	if(mysqli_num_rows($getResult) > 0){
		$paramRows = mysqli_fetch_array($getResult);
		//print_r($paramRows);
		$paramValue = $paramRows[0];
		return($paramValue);
	}else{
		return(-1);
	}
}//end of function
$DEBUG =0;
//command state
$nProcFail	=0;
$nWaitProc	=1;
$nInProc	=2;
$nOutProc	=3;
$sysId = $_REQUEST['sysId'];
$dataTypeNo = $_REQUEST['dataTypeNo'];


//impCommState
//01=通知导入用户；02=正在导入用户；03=导入用户成功；04=导入用户失败
/*
$impNote	='01';
$impBgn		='02';
$impOut		='03';
$impFail	='04';
*/
?>
<body leftmargin="15" rightmargin="10" onload="javascript:adjust_frame('down');" onresize="javascript:adjust_frame('down');" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;">

<?php
adjust_head_frame("down");
adjust_title_head_frame("down");
adjust_title_tail_frame("down");

adjust_content_head_frame("down");	
echo "<br>";

if($DEBUG)echo "paramValue=$paramValue";
$showFlag = $_REQUEST['showFlag'];
$dstSeverIp= $_REQUEST['dstSeverIp'];
$impRecordNum = $_REQUEST['impRecordNum'];
$commStr = $_REQUEST['commStr'];
$impFile = $_REQUEST['impFile'];
if ($DEBUG) echo "showFlag=$showFlag<br>";
if ($DEBUG)  echo "remote ip = $dstSeverIp<br>";
if ($DEBUG)  echo "impRecordNum=$impRecordNum<br>";
if ($DEBUG)  echo "commStr=$commStr<br>";
if ($DEBUG)  echo "impFile=$impFile<br>";
if(!$showFlag){
	exit();
}


if($DEBUG)echo "<br>sysId=$sysId";
$runSeconds = $_REQUEST['runSeconds'];
$oldDoneNum = $_REQUEST['oldDoneNum'];

$pubDb				= 'OMC_PUB';
$sysSubsConfTable	= 'OMC_PUB.sysSubsConf';
$systemConfTable	= 'OMC_PUB.sysConf';
$sysInfoTable		= 'OMC_PUB.sysInfo';
$perTime			= 0.05;
if(isset($sysId)){
	$sysIdArr	=explode('_',$sysId);
	$sysTypeNo	=$sysIdArr[0];
	$sysNo		=$sysIdArr[1];
	$subSysNo	=$sysIdArr[2];
	/*if($systypeNo==360){
		$getSysIdSql = "SELECT sysNo,subSysNo,ip 
					FROM $sysInfoTable
					WHERE sysTypeNo='$sysTypeNo' AND baseStat > '0'
					ORDER BY sysNo,subSysNo
				";
		if($DEBUG) echo "<br>getSysIdSql=$getSysIdSql";
		$getSysIdResult= mysqli_query($pubConn,$getSysIdSql);
		echo mysqli_error($pubConn);
		$getSysIdRow=mysqli_fetch_array($getSysIdResult);
		$sysNo=$getSysIdRow[sysNo];
		$subSysNo=$getSysIdRow[subSysNo];
	}*/
}else{
	exit(0);
}

$dataTypeNo = isset($dataTypeNo)?$dataTypeNo:0;
$getConfSql="SELECT impObjId,ftpDir,ftpLoginName,ftpLoginPwd,logFileName
				FROM $sysSubsConfTable 
				WHERE sysTypeNo='$sysTypeNo' AND dataTypeNo='$dataTypeNo'
				";
if($DEBUG) echo "<br>getConfSql=$getConfSql";
$getConfResult=mysqli_query($pubConn,$getConfSql);
echo mysqli_error($pubConn);
$getConfRows=mysqli_fetch_array($getConfResult);

$impObjId			= $getConfRows[impObjId];
$logFileName		= $getConfRows[logFileName];
$ftpDir				= $getConfRows[ftpDir];
$ftpLoginName		= $getConfRows[ftpLoginName];
$ftpLoginPwd		= $getConfRows[ftpLoginPwd];

if($DEBUG)echo "<br>impObjId		=$impObjId";
if($DEBUG)echo "<br>logFileName		=$logFileName";
if($DEBUG)echo "<br>ftpDir			=$ftpDir";
if($DEBUG)echo "<br>ftpLoginName	=$ftpLoginName";
if($DEBUG)echo "<br>ftpLoginPwd		=$ftpLoginPwd";
if($DEBUG)echo "<br>sysId			=$sysId";

//if($sysTypeNo==360)
//	$paramValue = getSysParamValue($sysId,$impObjId,0,'readValue');
//else
	$paramValue = getImpParamValue($sysId,$impObjId,0,'readValue');
if($DEBUG)echo "<br>paramValue=$paramValue";

$impCommState = substr($paramValue,0,2);
if($DEBUG)echo "<br>impCommState=$impCommState";

if($sysTypeNo==340){
	$allNum=$impRecordNum;
	$doneNum = substr($paramValue,2,6);
	$doneNum = HexDec($doneNum);
}else{
	$allNum = substr($paramValue,2,6);
	$allNum = HexDec($allNum);
	$doneNum = substr($paramValue,8,6);
	$doneNum = HexDec($doneNum);
}
if($DEBUG)echo "<br>allNum=$allNum";
if($DEBUG)echo "<br>doneNum=$doneNum";

//show process
omcProgressBar($allNum,$doneNum);

$maxSeconds = 20;
$runSeconds = $runSeconds + 2;
if($DEBUG)echo "<br>maxSeconds=$maxSeconds";
if($DEBUG)echo "<br>runSeconds=$runSeconds";
if($DEBUG)echo "<br>if($runSeconds > $maxSeconds || $doneNum >= $allNum || $impCommState == '03' || $impCommState == '04') ";
if($doneNum > $oldDoneNum){
	$runSeconds = 0;
}
if($runSeconds > $maxSeconds || $doneNum >= $allNum || $impCommState == '03' || $impCommState == '04') 
{
	if($impCommState == '04'){
		echo "<br><FONT color=\"#FF0000\" size=\"3\"><b>upload $impFile Failed,please check log!!</b></FONT>";
	}elseif($runSeconds > $maxSeconds){
		echo "<br><FONT color=\"#FF0000\" size=\"3\"><b>Response Time Out!!</b></FONT>";
	}else{
		echo "<br><FONT color=\"#0000FF\" size=\"3\"><b>upload $impFile Finished!</b></FONT>";
		echo "<br><br>After Import subscriber data finish,Plase upload it and get the lastest data.";
	}

	if($DEBUG)echo "<br>dstSeverIp=$dstSeverIp";
	/* close ftp
	$ftpCon=ftp_connect($dstSeverIp);
	if($ftpCon < 0){
		echo "<br><font color=\"#FF0000\">Connect Failed!</font>";
		exit();	
	}
	*/
	
	//$connection=sftp_connect($dstSeverIp);
	//if($connection < 0){
	//	echo "<br><font color=\"#FF0000\">Connect Failed!</font>";
	//	exit();	
	//}
	
	/* close ftp
	if(!ftp_login($ftpCon,$ftpLoginName,$ftpLoginPwd)){
		echo "<br><font color=\"#FF0000\">Login Failed!</font>";
		exit();		
	}
	*/
	
	//$key=sftp_login($connection,$ftpLoginName,$ftpLoginPwd);
	//if(!$key)
	//{
	//	echo "<br><font color=\"#FF0000\">Login Failed!</font>";
	//	exit();	
	//}
	
	$remoteFileName	=$ftpDir.$logFileName;
	$timestamp	= time();
	$nameArray = explode(".",$logFileName);
	$logFileName = $nameArray[0].$timestamp.".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);
	}
	
	/* close ftp
	if(!ftp_get($ftpCon,$localFileName,$remoteFileName,FTP_ASCII)){
		echo "<br><font color=\"#FF0000\">Get log file Failed</font>";
		exit();	
	}
	*/
	
	//if(!sftp_get($key,$localFileName,$remoteFileName,SFTP_ASCII))
	//{
	//	echo "<br><font color=\"#FF0000\">Get log file Failed</font>";
	//	exit();	
	//}

	/* close ftp
	ftp_quit($ftpCon);
	*/
	
	
	//$logFileUrl="../../../ftpFile/$logFileName";
	$logFileUrl="./get_log_file.php?dstSeverIp={$dstSeverIp}&ftpLoginName={$ftpLoginName}&ftpLoginPwd={$ftpLoginPwd}&localFileName={$localFileName}&remoteFileName={$remoteFileName}&logFileName={$logFileName}";
	echo "<br><br><a href=\"$logFileUrl\">Click here to download log.</a>";
	echo "<script language=\"javascript\">";
	echo "window.parent.frames.header.location.href='./subscriberImportUp.php?sysId=$sysId&dataTypeNo=$dataTypeNo&submitStat=ENABLE';";
	echo "</script>";
	echo "<font size=\"2\">";
	exit();
}
$commIdArr	= explode('-',$commStr);
$commId		= $commIdArr[0];
if($DEBUG)echo "<br>commId=$commId";
//echo "sysNo=$sysNo";
/*if($sysTypeNo==360)
	$commSql	= " SELECT commState FROM parameterComm WHERE commId = '$commId' AND sysNo= '$sysNo'";
else*/
	$commSql	= " SELECT commState FROM parameterComm WHERE commId = '$commId' ";
if($DEBUG) echo "<br>commSql=$commSql";
$commRes	= mysqli_query($pubConn,$commSql);
$commRow	= @mysqli_fetch_array($commRes);
$commStat	= $commRow[commState];
if($DEBUG)echo "<br>commStat=$commStat";
//echo "<br>sysId=$sysId,impObjId=$impObjId";


if($commStat == $nOutProc || !isset($commStat)){
	//send get command
	$commStr = sysCommSend($sysId,0,$impObjId);
}
echo "<meta http-equiv=\"refresh\" content=\"2;url=$PHP_SELF?showFlag=$showFlag&sysId=$sysId&dataTypeNo=$dataTypeNo&impRecordNum=$impRecordNum&runSeconds=$runSeconds&commStr=$commStr&dstSeverIp=$dstSeverIp&oldDoneNum=$doneNum\">";
echo "<meta http-equiv=\"pragma\" content=\"no-cache\">";
adjust_content_tail("down");
?>
