<?php
header("Content-type:text/html;charset=utf-8"); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>帐号注册</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=7; IE=9" />
<link type="text/css" rel="stylesheet" href="css/base.css" />
<script src="js/md5.js" type="text/javascript"></script>
<style type="text/css">
.td1 { padding:5px 0 0 0;width:70px;height:20px;vertical-align:top; }
.td2 { width:230px;vertical-align:top; }
.td3 { padding:5px 0 0 0;width:180px;vertical-align:top; }
.errstr { font-size:13px;color:#33cc00; }
</style>
<script type="text/javascript">
String.prototype.trim=function() {
     return this.replace(/(^\s*)(\s*$)/g, '');
}
function chkform() {
	var form = document.getElementById('form');

	if(form.pwd1.value.trim() == "") { 
      document.getElementById('errstr_pwd1').innerText = "登录密码不能为空！";   
	  document.getElementById('errstr_pwd1').style.color = '#ff0000';
      return false; 
    }
	document.getElementById('errstr_pwd1').innerText = "请使用高强度的密码";
	document.getElementById('errstr_pwd1').style.color = '#33cc00';

	if(form.pwd2.value.trim() == "") { 
      document.getElementById('errstr_pwd2').innerText = "密码确认不能为空！";   
	  document.getElementById('errstr_pwd2').style.color = '#ff0000';
      return false; 
    } 
	document.getElementById('errstr_pwd2').innerText = ""; 
	document.getElementById('errstr_pwd2').style.color = '#33cc00';

	if(form.pwd1.value.trim() != form.pwd2.value.trim()) { 
      document.getElementById('errstr_pwd2').innerText = "两次输入的密码不一样！";   
	  document.getElementById('errstr_pwd2').style.color = '#ff0000';
      return false; 
    } 
	document.getElementById('errstr_pwd2').innerText = "";   
	document.getElementById('errstr_pwd2').style.color = '#33cc00';

	var md5Value = hex_md5(form.pwd1.value.trim());
	md5Value = md5Value.toUpperCase();
	if(md5Value == "") { 
      alert('登录密码MD5加密出错！'); 
      return false; 
    }
	form.pwd.value = md5Value;
    if(form.question1.value.trim() == "") { 
      document.getElementById('errstr_question1').innerText = "问题不能为空！";   
	  document.getElementById('errstr_question1').style.color = '#ff0000';
      return false; 
    } 
	document.getElementById('errstr_question1').innerText = "用于找回密码";   
	document.getElementById('errstr_question1').style.color = '#33cc00';

	if(form.answer1.value.trim() == "") { 
      document.getElementById('errstr_answer1').innerText = "答案不能为空！";   
	  document.getElementById('errstr_answer1').style.color = '#ff0000';
      return false; 
    } 
	document.getElementById('errstr_answer1').innerText = ""; 
	document.getElementById('errstr_answer1').style.color = '#33cc00';

    return true; 
}//chkform
</script>
</head>
<body style="overflow:hidden;border:none;" scroll="no" oncontextmenu="return false;">

<?php
$step = isset($_GET['step']) ? $_GET['step'] : '1'; 

function step1() {
	$agehtml = '';
	for ($i=0;$i<=119;$i++) {
		$agehtml .= '<option value="' . $i . '">' . $i . ' 岁</option>';
	}//for

echo<<<EOT
<div style="clear:both;margin:0 auto;padding:8px 0 0 8px;width:492px;height:22px;text-align:left;font-size:15px;font-weight:bold;color:#009ee8;">填写注册信息:</div>
<form style="margin:0;padding:0;" action="register.php?step=2" method="post" id="form" onSubmit="return chkform();">
<input type="hidden" name="pwd" value=""/>
<div style="clear:both;margin:0 auto;padding:20px 0 0 20px;width:480px;height:260px;text-align:left;font-size:14px;color:#330000;">
	<table border="0">
	<tr>
		<td class="td1">昵称:</td>
		<td class="td2"><input style="width:200px;" type="text" name="nickname" value=""></td>
		<td class="td3"><span id="errstr_nickname" class="errstr">最大长度为20个字符</span></td>
	</tr>
	<tr>
		<td class="td1">性别:</td>
		<td class="td2">
			<select name="sex" style="width:206px;">
			<option value="0">请选择</option>
			<option value="1">男</option>
			<option value="2">女</option>
			</select>
		</td>
		<td class="td3"></td>
	</tr>
	<tr>
		<td class="td1" style="height:40px;">年龄:</td>
		<td class="td2">
			<select name="age" style="width:206px;">
			<option value="-1">请选择</option>
			$agehtml
			</select>
		</td>
		<td class="td3"></td>
	</tr>
	<tr>
		<td class="td1">登录密码:</td>
		<td class="td2"><input style="width:200px;" type="password" name="pwd1" value=""><span style="color:#ff0000;">&nbsp;*</span></td>
		<td class="td3"><span id="errstr_pwd1" class="errstr">请使用高强度的密码</span></td>
	</tr>
	<tr>
		<td class="td1">密码确认:</td>
		<td class="td2" style="height:40px;"><input style="width:200px;" type="password" name="pwd2" value=""><span style="color:#ff0000;">&nbsp;*</span></td>
		<td class="td3"><span id="errstr_pwd2" class="errstr"></span></td>
	</tr>
	<tr>
		<td class="td1">密保问题:</td>
		<td class="td2"><input style="width:200px;" type="text" name="question1" value=""><span style="color:#ff0000;">&nbsp;*</span></td>
		<td class="td3"><span id="errstr_question1" class="errstr">可用于找回忘记的密码</span></td>
	</tr>
	<tr>
		<td class="td1">密保答案:</td>
		<td class="td2" style="height:40px;"><input style="width:200px;" type="text" name="answer1" value=""><span style="color:#ff0000;">&nbsp;*</span></td>
		<td class="td3"><span id="errstr_answer1" class="errstr"></span></td>
	</tr>
	</table>
</div>
<div style="clear:both;margin:0 auto;padding:8px 10px 0 0;width:490px;height:32px;text-align:right;">
	<input class="btn80" type="submit" value="注册">
</div>
</form>
EOT;
}//step1

function step2() {
	require("common.php");

	function GetIP(){
	   if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))   
			   $ip = getenv("HTTP_CLIENT_IP");   
		   else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))   
			   $ip = getenv("HTTP_X_FORWARDED_FOR");   
		   else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))   
			   $ip = getenv("REMOTE_ADDR");   
		   else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))   
			   $ip = $_SERVER['REMOTE_ADDR'];   
		   else  
			   $ip = "unknown";   
	   return($ip);   
	}

	$ip = GetIP();

	$nickname = isset($_POST['nickname']) ? $_POST['nickname'] : ''; 
	$password = isset($_POST['pwd']) ? $_POST['pwd'] : ''; 
	$sex = isset($_POST['sex']) ? $_POST['sex'] : '0'; 
	$age = isset($_POST['age']) ? $_POST['age'] : '-1'; 
	$question1 = isset($_POST['question1']) ? $_POST['question1'] : ''; 
	$answer1 = isset($_POST['answer1']) ? $_POST['answer1'] : ''; 
	$question2 = ''; 
	$answer2 = ''; 
	$question3 = ''; 
	$answer3 = ''; 

	$methodname = 'UserCenter.Register';
	$requestinfo = '<?xml version="1.0" encoding="utf-8"?><request>' . 
		'<nickname>' . $nickname . '</nickname>' . 
		'<password>' . $password . '</password>' . 
		'<sex>' . $sex . '</sex>' . 
		'<age>' . $age . '</age>' . 
		'<question1>' . $question1 . '</question1>' . 
		'<answer1>' . $answer1 . '</answer1>' . 
		'<question2>' . $question2 . '</question2>' . 
		'<answer2>' . $answer2 . '</answer2>' . 
		'<question3>' . $question3 . '</question3>' . 
		'<answer3>' . $answer3 . '</answer3>' . 
		'<registeripaddr>' . $ip . '</registeripaddr>' . 
		'</request> ';
	$responseinfo = '';
	$responsexml = null;
	$errorcode = ERRORCODE_UNKNOWN;


	$cmd = new COM("OpenCom.Command");
	$methodname = iconv('UTF-8', 'GBK//IGNORE', $methodname);
	$requestinfo = iconv('UTF-8', 'GBK//IGNORE', $requestinfo);
	if ($cmd->Execute($methodname, $requestinfo, $responseinfo, 5000)) {
		$responseinfo = iconv('GBK', 'UTF-8//IGNORE', $responseinfo);
		$responsexml = simplexml_load_string($responseinfo);
		$errorcode = $responsexml->errorcode;
	}
	$cmd = null;

	switch($errorcode) {
		case ERRORCODE_OK: 
echo<<<EOT
	<div style="clear:both;margin:0 auto;padding:8px 0 0 8px;width:492px;height:22px;text-align:left;font-size:15px;font-weight:bold;color:#009ee8;">注册成功</div>
	<div style="clear:both;margin:0 auto;padding:0;width:500px;height:280px;">
		<div style="float:left;margin:0;padding:120px 0 0 0;width:500px;text-align:center;font-size:16px;">
			<img border="0" width="40px" height="40px" align="absmiddle" src="images/ok.gif" />&nbsp;你的帐号为 <span style="color:#ff0000">$responsexml->userid</span>。
		</div>
	</div>
EOT;
		break;
		case ERRORCODE_INVALIDPARAMS: 
echo<<<EOT
	<div style="clear:both;margin:0 auto;padding:8px 0 0 8px;width:492px;height:22px;text-align:left;font-size:15px;font-weight:bold;color:#009ee8;">注册失败</div>
	<div style="clear:both;margin:0 auto;padding:0;width:500px;height:280px;">
		<div style="float:left;margin:0;padding:120px 0 0 0;width:500px;text-align:center;font-size:16px;">
			<img border="0" width="40px" height="40px" align="absmiddle" src="images/warning.gif" />&nbsp;参数错误！
		</div>
	</div>
EOT;
		break;
		case ERRORCODE_OVERSTEP: 
echo<<<EOT
	<div style="clear:both;margin:0 auto;padding:8px 0 0 8px;width:492px;height:22px;text-align:left;font-size:15px;font-weight:bold;color:#009ee8;">注册失败</div>
	<div style="clear:both;margin:0 auto;padding:0;width:500px;height:280px;">
		<div style="float:left;margin:0;padding:120px 0 0 0;width:500px;text-align:center;font-size:16px;">
			<img border="0" width="40px" height="40px" align="absmiddle" src="images/warning.gif" />&nbsp;同一IP已超过当日注册数量！
		</div>
	</div>
EOT;
		break;
		case ERRORCODE_NOACCOUNT: 
echo<<<EOT
	<div style="clear:both;margin:0 auto;padding:8px 0 0 8px;width:492px;height:22px;text-align:left;font-size:15px;font-weight:bold;color:#009ee8;">注册失败</div>
	<div style="clear:both;margin:0 auto;padding:0;width:500px;height:280px;">
		<div style="float:left;margin:0;padding:120px 0 0 0;width:500px;text-align:center;font-size:16px;">
			<img border="0" width="40px" height="40px" align="absmiddle" src="images/warning.gif" />&nbsp;没有帐号资源！
		</div>
	</div>
EOT;
		break;
		default: 
echo<<<EOT
	<div style="clear:both;margin:0 auto;padding:8px 0 0 8px;width:492px;height:22px;text-align:left;font-size:15px;font-weight:bold;color:#009ee8;">注册失败</div>
	<div style="clear:both;margin:0 auto;padding:0;width:500px;height:280px;">
		<div style="float:left;margin:0;padding:120px 0 0 0;width:500px;text-align:center;font-size:16px;">
			<img border="0" width="40px" height="40px" align="absmiddle" src="images/warning.gif" />&nbsp;未知错误！
		</div>
	</div>
EOT;
	}//switch
}//step2


if ($step == 1) {
	step1();
}
else if ($step == 2) {
	step2();
}
?>

</body>
</html>
