<?php
/*********************************************************      
 程序说明：         
    功能说明：SMSC参数配置

    调用关系：调用: header.inc     
              被调用:   
 
    变量说明：

     返回值：无

 作者：    
      
修改注释：      
NO.1      
姓名：            
时间：      
修改说明：      
*********************************************************/  
require("../../inc/header.inc");
$helpId = "2.1.5"; 
$DEBUG = 0;
$sysInfoTable = 'OMC_PUB.sysInfo';
?>
<script LANGUAGE="JavaScript">           
function openSet(oType,iNum){
	var x,y;
	x = document.body.scrollLeft;
	y = document.body.scrollTop;

	URL="./smscEdit.php?oType="+oType+"&iNum="+iNum+"&scroll_x="+x+"&scroll_y="+y;
	var msgWindow = open(URL, 'newWin', 'toolbar=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,status=yes,width=400,height=400');
	msgWindow.focus();
}


function delFirm(i)
{
	var delValue,x,y;
	x = document.body.scrollLeft;
	y = document.body.scrollTop;

	if(confirm('Do you want to delete the selected?')!=true){
		window.location.href='<?php echo $PHP_SELF;?>'+"?scroll_x="+x+"&scroll_y="+y;
	}else{
		window.location.href='<?php echo $PHP_SELF;?>?delete=1&delValue='+i+"&scroll_x="+x+"&scroll_y="+y;
	}
}

</script>
<?php
$sysTypeNo = "350";

$db = "OBJ_".$sysTypeNo;

$tb_0 = "param_0";
$tb_1 = "param_1";
$tb_2 = "param_99";

$maxNum = 20;

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

$nGetComm = 0;
$nSetComm = 3;

$nInteger = 2;
$nOctStr = 4;

$nProcFail = 0;
$nWaitProc = 1;
$nInProc   = 2;	
$nOutProc  = 3;



$wSc = " AND level_1 = 2 AND (level_2 = 2 OR level_2 = 3)";


/************get default value**************/
$defSql = " SELECT level_1,level_2,level_3,initValue FROM paramConf WHERE keyId > 0 ".$wSc." ORDER BY level_2,level_3 ";
if($DEBUG)echo "<br>defSql=$defSql";
$defRes = @mysqli_query($pubConn,$defSql);
$defRow = @mysqli_fetch_array($defRes);

do
{
	$level_1 = $defRow[level_1];
	$level_2 = $defRow[level_2];
	$level_3 = $defRow[level_3];
	$defaultValue[$level_2][$level_3] = $defRow[initValue];
	if($DEBUG) echo "<br>defaultValue[$level_2][$level_3]={$defaultValue[$level_2][$level_3]}";
	
}while($defRow = @mysqli_fetch_array($defRes));

$cellFlagArr=array(
		array('num' => '0',  'remark' => 'N', 'color' => '#FF0000'),
		array('num' => '1',  'remark' => 'Y', 'color' => '#0000FF')
);

//****************************************************************
//函数
//	ChComm($sysId)
//函数说明:检查命令处理结果
//	
//变量说明: 
//	无
//	
//                                                             
//返回值：$paramType
//$nSubSysParam			= 0;
//$nGlobalSubSysParam	= 1;
//$nGlobalSysParam		= 2;	
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------

function ChComm($sysId,$chkType=1)
{     
	global $omc_db;
	global $remark,$nProcFail,$nWaitProc,$nInProc,$nOutProc;

	$commTable = "parameterComm";
	$DEBUG = 0;

	$sysIdArr = explode('_',$sysId);
	$sysTypeNo	= $sysIdArr[0];
	$sysNo		= $sysIdArr[1];
	$subSysNo	= $sysIdArr[2];
	
	if($chkType==0)
		$selComm = " SELECT commState,remark FROM $commTable WHERE sysTypeNo = '$sysTypeNo' ";
	else
		$selComm = " SELECT commState,remark FROM $commTable WHERE sysTypeNo = '$sysTypeNo' AND sysNo = '$sysNo' ";
	
	if($DEBUG)echo "<br>selComm=$selComm";
	$selRes  = @mysqli_query($pubConn,$selComm);
	$selRow  = @mysqli_fetch_array($selRes);
	echo mysqli_error($pubConn);
	do
	{
		$temCommState = $selRow[commState];
		if($selRow[remark]){
			$remark = $selRow[remark];
			//echo "<br>remark=$remark";
		}
		//echo "<br>temCommState=$temCommState";
		if($temCommState == $nWaitProc)
			return($nWaitProc);
		elseif($temCommState == $nInProc)
			return($nInProc);
		elseif($temCommState == $nOutProc)
			$commState = $nOutProc;
		else
			$commState = 0;
	}while($selRow  = @mysqli_fetch_array($selRes));

	return($commState);
}



//****************************************************************
//函数
//	从Buffer_2拷贝到$buffer_1
//函数说明:
//	
//变量说明: 
//	无
//	
//                                                             
//返回值：$paramType
//
//
//	
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------

function copyBuffer($buffer_1,$buffer_2,$wDef,$type='1')
{     
	global $sysTypeNo,$sysTypeNo_1,$db,$db_1,$tb_0,$tb_1,$tb_2,$wSc;
	$DEBUG = 0;
	$addWer = "	AND (LENGTH($buffer_2) > 0) AND $buffer_2 != 'Null' AND $buffer_2 NOT LIKE 'Error%' ";

	//Location management
	if($type)
		$uTgSql = " UPDATE $tb_2 set $buffer_1=$buffer_2 WHERE subSysNo = '99' ".$wSc.$addWer; 
	else
		$uTgSql = " UPDATE $tb_2 set $buffer_1=$buffer_2 WHERE subSysNo = '99' ".$wSc.$wDef.$addWer; 

	if($DEBUG) echo " <br>uTgSql=$uTgSql ";
	$uTgRes = @mysqli_query($pubConn,$uTgSql);

	echo mysqli_error($pubConn);

	if($uTgRes)
		return 1;
	else
		return 0;
}

//****************************************************************
//函数
//	get 或 set
//函数说明:
//	
//变量说明: 
//	无
//	
//                                                             
//返回值：$commType
//
//
//	
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function eSetGet($commType)
{     
	global $sysTypeNo,$sysTypeNo_1,$db,$db_1,$tb_0,$tb_1,$tb_2,$wSc,$nProcFail,$nWaitProc,$nInProc,$nOutProc;
	global $maxNum,$remark,$nGetComm,$nSetComm;
	$DEBUG = 0;

	if($commType == $nGetComm)
		$showType = "Get ";
	else
		$showType = "Set ";


	//2.2
	$sysId = "350_99_99";
	$objIdStr = "2.2";
	$scSend = sysCommSend($sysId,$commType,$objIdStr);

	$i = 0;
	while(1)
	{
		//echo "<br>i=$i";
		if($i == 0)
			echo "<br>$showType UDP parameters &nbsp;&nbsp;";
		if($i > $maxNum){
			echo "<br><font size=\"2\" color=\"#FF0000\">Timeout</font>";
			flush();
			break;
		}
		echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
		flush();	

		$sysId = "350";
		$chkType = "0";
		$chRes=ChComm($sysId,$chkType);
		//echo "<br>chRes=$chRes";
		if($chRes == $nOutProc || $chRes == $nProcFail){
			$pNext1 = 1;
			if($chRes == $nProcFail)
				echo "<br><font size=\"2\" color=\"#FF0000\">$remark</font><br>";
			elseif($remark == 'response timeout')
				echo "<br><font size=\"2\" color=\"#FF0000\">$remark</font><br>";
			else
				echo "<br><font size=\"2\" color=\"#0000FF\">$remark</font><br>";

			flush();
			break;
		}
	
		sleep(1);
		$i++;
	}

	if($pNext1)
	{
		$sysId = "350_99_99";
		$objIdStr = "2.3";
		$scSend = sysCommSend($sysId,$commType,$objIdStr);

		$i = 0;
		while(1)
		{
			//echo "<br>i=$i";
			if($i == 0)
				echo "<br>$showType TCP parameters &nbsp;&nbsp;";
			if($i > $maxNum){
				echo "<br><font size=\"2\" color=\"#FF0000\">Timeout</font>";
				$rValue = -1;
				flush();
				break;
			}
			echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
			flush();	

			$sysId = "350";
			$chkType = "0";
			$chRes=ChComm($sysId,$chkType);
			//echo "<br>chRes=$chRes";
			if($chRes == $nOutProc || $chRes == $nProcFail){
				$pNext1 = 1;
				if($chRes == $nProcFail)
					echo "<br><font size=\"2\" color=\"#FF0000\">$remark</font><br>";
				elseif($remark == 'response timeout')
					echo "<br><font size=\"2\" color=\"#FF0000\">$remark</font><br>";
				else
					echo "<br><font size=\"2\" color=\"#0000FF\">$remark</font><br>";

				flush();
				break;
			}
		
			sleep(1);
			$i++;
		}
	}
}//end of function



if($saveSys)
{

	/****************copy initValue to editValue******************/
	$buffer_1 = "editValue";
	$buffer_2 = "initValue";
	$edCin=copyBuffer($buffer_1,$buffer_2,$wDef);
	
	eSetGet($nSetComm);

	sleep(1);
	echo "<script language=\"javascript\">";
	//echo "window.location.href='$PHP_SELF';";
	echo "</script>";


}//end of set sys
elseif($readSys)
{	
	eSetGet($nGetComm);//get value

	/****************copy readValue to initValue******************/
	$buffer_1 = "initValue";
	$buffer_2 = "readValue";
	$inCre=copyBuffer($buffer_1,$buffer_2,$wDef);
	if($DEBUG) echo "<br>inCre=$inCr";

	sleep(1);
	echo "<script language=\"javascript\">";
	//echo "window.location.href='$PHP_SELF';";
	echo "</script>";
}//end of $readSys
elseif($saveDisk)
{
	/****************copy initValue to editValue******************/
	$buffer_1 = "editValue";
	$buffer_2 = "initValue";
	$inCed=copyBuffer($buffer_1,$buffer_2,$wDef);
	if($DEBUG) echo "<br>$inCed=$$inCed";

	if($inCed)
		echo "<br><font color=\"#0000FF\">Save success.</font>";
	else
		echo "<br><font color=\"#FF0000\">Save Failure.</font>";

	flush();
	sleep(1);
	echo "<script language=\"javascript\">";
	echo "window.location.href='$PHP_SELF';";
	echo "</script>";
}//end of $saveDisk
elseif($readDisk)
{
	/****************copy editValue to initValue******************/
	$buffer_1 = "initValue";
	$buffer_2 = "editValue";
	$edCin=copyBuffer($buffer_1,$buffer_2,$wDef);
	if($DEBUG) echo "<br>edCin=$edCin";

	if($edCin)
		echo "<br><font color=\"#0000FF\">Read success.</font>";
	else
		echo "<br><font color=\"#FF0000\">Read failure.</font>";
	
	flush();
	sleep(1);
	echo "<script language=\"javascript\">";
	echo "window.location.href='$PHP_SELF';";
	echo "</script>";

}//end of $comSys
elseif($delete)
{
	$delSql = " UPDATE $tb_2 set initValue = '$defaultValue' WHERE subSysNo = '99' ".$wSc. " AND instanceNo = $delValue ";
	//echo "<br>delSql=$delSql";
	$delRes = @mysqli_query($pubConn,$delSql);
	echo mysqli_error($pubConn);
}
elseif($saveComm)
{
	for($i=0;$i<1;$i++)
	{
		$temTb = $tb_2;
		$updSql = " UPDATE $temTb set initValue = '01' WHERE subSysNo = '99' AND level_1 = '3' AND level_2 = '3' ";
		//echo "<br>updSql=$updSql";
		$updRes = @mysqli_query($pubConn,$updSql);
		//echo "<br>updRes=$updRes";
	}

	for($j=0;$j<1;$j++)
	{
		if($sTimeOut)
			break;

		$sysId = $sysTypeNo."_99_99";
		$commType = $nSetComm;
		$objIdStr = "3.3";
		$flag = sysCommSend($sysId,$commType,$objIdStr);

		$i = 0;
		while(1)
		{
			if($i == 0)
				echo "<br>Save parapeter to system ";
			if($i > $maxNum){
				echo "<br><font size=\"2\" color=\"#FF0000\">Timeout</font>";
				$sTimeOut = 1;
				break;
			}
			echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
			flush();	

			$sysId = $sysTypeNo;
			$chRes=ChComm($sysId);
			//echo "<br>chRes=$chRes";
			if($chRes == $nOutProc || $chRes == $nProcFail){
				$pNext1 = 1;
				if($chRes == $nProcFail)
					echo "<br><font size=\"2\" color=\"#FF0000\">$remark</font><br>";
				elseif($remark == 'response timeout')
					echo "<br><font size=\"2\" color=\"#FF0000\">$remark</font><br>";
				else
					echo "<br><font size=\"2\" color=\"#0000FF\">$remark</font><br>";
				break;
			}
	
			sleep(1);
			$i++;
		}
	}
}

if($readSys || $saveSys || $saveComm){
	echo "<br>";
	echo "<p align=\"left\"><a href=\"$PHP_SELF\" target=\"contents\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">$strBack</a>";
	exit();
}

/************select value to see************/
$scSql = " SELECT level_1,level_2,level_3,instanceNo,initValue,editValue,readValue FROM $tb_2 WHERE subSysNo = '99' ".$wSc." ORDER BY level_3 "; 
if($DEBUG) echo "<br>scSql=$scSql";
$scRes = @mysqli_query($pubConn,$scSql);
$scRow = @mysqli_fetch_array($scRes);
echo mysqli_error($pubConn);
do
{
	$level_1 = $scRow[level_1];
	$level_2 = $scRow[level_2];
	$level_3 = $scRow[level_3];
	$instanceNo = $scRow[instanceNo];

	if($level_3 > 1){//if no.1=="--",then all="--"
		if($showValue[$level_2][$instanceNo][1] == '--'){
			$showValue[$level_2][$instanceNo][$level_3] = "--";
			continue;
		}
	}
	
	if($scRow[initValue] == $defaultValue[$level_2][$level_3] && (($level_2 == 2 && $level_3 < 7) OR ($level_2 == 3 && $level_3 < 8)) ){
		$showValue[$level_2][$instanceNo][$level_3] = "--";
		continue;
	}
		
	if($level_3 > 0 && $level_3 < 4){//name,sysid,password
		$valueLen = floor(strLen($scRow[initValue])/2);
		for($i=0;$i<$valueLen;$i++){
			$temValue[$i] = substr($scRow[initValue],$i*2,2);
			if($temValue[$i] == '00'){
				break;
			}else{
				$temValue[$i] = Chr(HexDec($temValue[$i]));
			}
		}
		$aNum = $i;
		$tValue = '';
		for($i=0;$i<$aNum;$i++){
			$tValue = $tValue.$temValue[$i];
		}
		if($DEBUG)echo "<br>tValue=$tValue";
		$showValue[$level_2][$instanceNo][$level_3] = $tValue;
	}elseif($level_3 == 4){
		$p4 = explode("E",$scRow[initValue]);
		$showValue[$level_2][$instanceNo][$level_3] = $p4[0];
	}elseif($level_3 == 6){
		if($level_2 == 2){
			$p6 = explode("E",$scRow[initValue]);
			$showValue[$level_2][$instanceNo][$level_3] = $p6[0];
		}elseif($level_2 == 3){
			$valueLen = floor(strLen($scRow[initValue])/2);
			for($i=0;$i<$valueLen;$i++){
				$temValue[$i] = HexDec(substr($scRow[initValue],$i*2,2));
			}
			$showValue[$level_2][$instanceNo][$level_3] = $temValue[0].".".$temValue[1].".".$temValue[2].".".$temValue[3];
		}
	}elseif($level_3 == 7){
		if($level_2 == 2){
			$showValue[$level_2][$instanceNo][$level_3]	= HexDec($scRow[initValue]);
		}else{
			$valueLen = floor(strLen($scRow[initValue])/2);
			for($i=0;$i<$valueLen;$i++){
				$temValue[$i] = HexDec(substr($scRow[initValue],$i*2,2));
			}
			$showValue[$level_2][$instanceNo][$level_3] = $temValue[0].".".$temValue[1].".".$temValue[2].".".$temValue[3];
		}
	}elseif($level_3 == 12){
		if($scRow[initValue]==0||HexDec($scRow[initValue])==16)
			$showValue[$level_2][$instanceNo][$level_3]='7bit';
		else
			$showValue[$level_2][$instanceNo][$level_3]='8bit';
	}else{
		$showValue[$level_2][$instanceNo][$level_3]	= HexDec($scRow[initValue]);
	}
	if($DEBUG) echo "<br>showValue[$level_2][$instanceNo][$level_3]={$showValue[$level_2][$instanceNo][$level_3]}";
}while($scRow = @mysqli_fetch_array($scRes));

echo "<BODY topmargin=\"5\" leftmargin=\"12\">";
echo "<form method=\"POST\" name=\"confForm\" action=\"$PHP_SELF\">";
?>
<?php 
echo "<table border=\"0\" width=\"100%\">";
echo "<tr>";
echo "<td width=\"50%\">";
echo "$strConfiguration > SMSC";
echo "</td>";
echo "<td width=\"50%\" align=\"right\" title=\"Help\">";
showHelp($helpId);
echo "</td>";
echo "</tr>";
echo "</table>";
?>
<br>&nbsp;
<table border="0" cellpadding="0" cellspacing="0" width="100%">           
    <tr>           
	<?php
		echo "<td width=200 valign=\"middle\"><nobr><b>System:</b>";
		echo "<input type=\"submit\" class=\"button\" value=$strGet name=\"readSys\" style=\"left: 1; top: 2; width: 70; height: 25; position: relative\">";
		echo "<input type=\"submit\" class=\"button\" value=$strSet name=\"saveSys\" style=\"left: 1; top: 2; width: 70; height: 25; position: relative\">";
	?>
	</nobr>
	</td>        
	<td width=200 valign="middle"><nobr><b>Disk:</b>	
	<?php
		echo "<input type=\"submit\" value=$strSave class=\"button\" name=\"saveComm\" style=\"left: 1; top: 2; width: 70; height: 25; position: relative\">";
	?>
	</td>    
<!--
	<td width=200 valign="middle"><nobr><b>Disk:</b>	
	<input type="submit" value="Get" name="readDisk" style="left: 1; top: 2; width: 70; height: 25; position: relative" class="button">
	<input type="submit" value="Save" name="saveDisk" style="left: 1; top: 2; width: 70; height: 25; position: relative" class="button">
	</nobr>
	</td>    
-->
  </tr>          
</table>   
<p></p>
<TABLE  border="1" width="100%" cellpadding="2" cellspacing="0" bordercolor="#666666" bordercolordark="#FFFFFF" bgcolor="#FFFFFF">     
<tr bgcolor="#E6E6E6">
<td width="4%" >No.</td>     
<td width="8.0%" >Name</td>     
<td width="8.0%" >System ID</td>     
<td width="8.0%" >Password</td>     
<td width="8.0%" >Service number</td> 
<td width="8.0%" >GTT number</td> 
<td width="7.0%" >Service IP</td>    
<td width="7.0%" >Local IP </td>    
<td width="7.0%" >Session timer</td>
<td width="7.0%" >Enquire link timer</td>
<td width="7.0%" >Inactivity timer</td>
<td width="7.0%" >Response timer</td>
<td width="7.0%" >DCS</td>
<td width="7.0%" >Time to live</td>
</tr>
<?php
echo "<tr bgcolor=\"#E6E6E6\">";
echo "<td colspan=\"14\">UDP parameters</td>";
echo "</tr>";
for($i=0;$i<8;$i++)
{
	echo "<tr>";
	echo "<td><a href=javascript:openSet(2,$i)>$i</a></td>";
	for($j=1;$j<14;$j++){
		if($j == 5||$j == 11){
			continue;
		}elseif($j == 7){
			echo "<td><nobr>--</td>";
			echo "<td><nobr>--</td>";
		}

		echo "<td><nobr>{$showValue[2][$i][$j]}&nbsp;</nobr></td>";
	}
	echo "</tr>";
}
echo "<tr bgcolor=\"#E6E6E6\">";
echo "<td colspan=\"14\">TCP parameters</td>";
echo "</tr>";

for($i=0;$i<32;$i++)
{
	echo "<tr>";
	echo "<td><a href=javascript:openSet(3,$i)>$i</a></td>";
	for($j=1;$j<14;$j++){
		if($j == 5){
			continue;
		}elseif($j == 6){
			echo "<td><nobr>--</td>";
		}

		echo "<td>{$showValue[3][$i][$j]}&nbsp;</nobr></td>";
	}
	echo "</tr>";
}
echo "</table>";
echo "<input type=\"hidden\" name=\"wDef\" value=\"$wDef\">";
echo "</form>";
?>
</body>
</html>
