<html>
	<head><TITLE> 工商银行电子商务测试页面 </TITLE><head>
<?php
	/*加载PHP签名模块，详细参加部署文档*/
	if (!extension_loaded('infosec'))
	{
		if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
		{
			dl('php_infosec.dll');
		}
   	else
		{
			dl('infosec.so');
		}
 	}
	/*获取表单输入*/
	if(isset($_POST["submit"]))
	{
		//读取工行私钥文件
		$keyfile=$_POST["bankCertKey"];
		if(strlen($keyfile) <= 0)
		{
			echo "WARNING : no key data input<br/>";
			exit();
		}

		$fp = fopen($keyfile,"rb");
		if($fp == NULL)
		{
			echo "open file error<br/>";
			exit();
		}
		
		fseek($fp,0,SEEK_END);
		$filelen=ftell($fp);
		fseek($fp,0,SEEK_SET);
		$contents = fread($fp,$filelen);
		fclose($fp);
		
		$key = substr($contents,2);
		//获取密钥口令
		$pass=$_POST["bankCertKeyPasswd"];
		if(strlen($pass) <= 0)
		{
			echo "WARNING : no key password input<br/>";
			exit();
		}
		//获取订单提交数据
		$notifyData=$_POST["notifyData"];
		if(strlen($notifyData) <= 0)
		{
			echo "WARNING : no notifyData input<br/>";
			exit();
		}
		//读取工行公钥文件
		$bankCert=$_POST["bankCert"];
		if(strlen($bankCert) <= 0)
		{
			echo "WARNING : no bankCert input<br/>";
			exit();
		}
		else
		{
			$fp2 = fopen($bankCert,"rb");
			if($fp2 == NULL)
			{
				echo "open file error<br/>";
				exit();
			}
			fseek($fp2,0,SEEK_END);
			$filelen2=ftell($fp2);
			fseek($fp2,0,SEEK_SET);
			$cert = fread($fp2,$filelen2);
			fclose($fp2);
		}
		
	}
	/*签名*/
	$signature = sign($notifyData,$key,$pass);//签名
	$code = current($signature);//获取签名数据
	$len = next($signature);//获取签名数据长度
	$signcode = base64enc($code);//对签名数据BASE64编码
	$bankSignMsg = current($signcode);
	$notifyDataBase64 = base64enc($notifyData);//对通知数据BASE64编码
?>
	<body>
		<!-- 测试地址需向电子银行申请，将签名的数据提交银行 -->
		<FORM name=submitnotify action="verifynotify.php" method=post">
			<font face='Arial' size='4' color='white'>银行通知信息签名页面</font>
			<table width="98%"  border="1">
				<tr>
					<td width="9%">返回商户变量</td>
					<td width="91%"><INPUT ID="merVAR" NAME="merVAR" TYPE="text" value="test" size="120" ></td>
				</tr>
				<tr>
					<td width="9%">通知结果数据</td>
					<td width="91%"><textarea ID="notifyData" name="notifyData" cols="120" rows="5"><?php echo current($notifyDataBase64);?></textarea>
				</tr>
				<tr>
					<td width="9%">银行对通知结果的签名数据</td>
					<td width="91%"><INPUT ID="signMsg" NAME="signMsg" TYPE="text" size="120" value="<?php echo $bankSignMsg;?>">
				</tr>
			</table>
			<table>
				<tr>
					<td><INPUT TYPE="submit" name="submit" value=" submit "></td>
					<td><INPUT  type="button" value=" 返 回 修 改 " onClick="self.history.back();"></td>
				</tr>
			</table>
		</FORM>
	</body>
</html>