<?php
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == "newuser" ) {
		define( "SAXUE_NEED_SESSION", 1 );
} 
include "../core.php";
if ( !empty( $_SESSION['HTTP_REFERER'] ) && basename( $_SESSION['HTTP_REFERER'] ) != "register.php" ) {
		$jumpurl = $_SESSION['HTTP_REFERER'];
} elseif ( !empty( $_SERVER['HTTP_REFERER'] ) && basename( $_SERVER['HTTP_REFERER'] ) != "register.php" ) {
		$jumpurl = $_SERVER['HTTP_REFERER'];
} else {
		$jumpurl = '/member/';
} 
if ( !empty( $_SESSION['saxueUserId'] ) ) {
		header( "Location: " . $jumpurl );
		exit();
} 
saxue_getconfigs( 'configs', 'member' );
if ( !$saxueConfigs['member']['regallow'] ) {
		saxue_printfail( nl2br( $saxueConfigs['member']['regnotice'] ) );
} 
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == "newuser" ) {
		include SAXUE_ROOT_PATH . "/model/member_users.php";
		$users_handler = &saxuememberusershandler :: getinstance( "saxuememberusershandler" ); 
		// IP限制检查
		$_POST['regip'] = saxue_userip();
		$saxueConfigs['member']['regtimelimit'] = intval( $saxueConfigs['member']['regtimelimit'] );
		if ( 0 < $saxueConfigs['member']['regtimelimit'] ) {
				$sql = "SELECT * FROM " . saxue_dbprefix( "member_registerip" ) . " WHERE ip='" . saxue_dbslashes( $_POST['regip'] ) . "' AND regtime>" . ( SAXUE_NOW_TIME - $saxueConfigs['member']['regtimelimit'] * 3600 ) . " LIMIT 0,1";
				$res = $users_handler -> execute( $sql );
				if ( $users_handler -> getrow() ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => $saxueConfigs['member']['regtimelimit'] . '小时以内只能注册一次' ) ) );
				} 
		} 
		// 数据初始化
		$_POST['uname'] = saxue_htmlstr( trim( $_POST['uname'] ) );
		$_POST['email'] = saxue_htmlstr( trim( $_POST['email'] ) );
		$_POST['password'] = trim( $_POST['password'] );
		$_POST['repassword'] = trim( $_POST['repassword'] );
		$_POST['checkcode'] = saxue_htmlstr( trim( $_POST['checkcode'] ) );
		$closearr = array();
		if ( !empty( $saxueConfigs['member']['regclosewords'] ) ) {
				$closearr = explode( ',', $saxueConfigs['member']['regclosewords'] );
		} 
		// 用户名检查
		if ( strlen( $_POST['uname'] ) == 0 ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '用户名不能为空' ) ) );
		} else if ( !empty( $saxueConfigs['member']['regminname'] ) && strlen( $_POST['uname'] ) < intval( $saxueConfigs['member']['regminname'] ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '用户名长度不能少于' . $saxueConfigs['member']['regminname'] . '个字符' ) ) );
		} else if ( !empty( $saxueConfigs['member']['regmaxname'] ) && strlen( $_POST['uname'] ) > intval( $saxueConfigs['member']['regmaxname'] ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '用户名长度不能大于' . $saxueConfigs['member']['regmaxname'] . '个字符' ) ) );
		} else if ( preg_match( "/^\\s*\$|^c:\\con\\con\$|[@%,;:\\.\\|\\*\\\"'\\\\\\/\\s\\t\\<\\>\\&]|　/is", $_POST['uname'] ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '用户名包含不可接受字符' ) ) );
		} else if ( $saxueConfigs['member']['unamelimit'] == 1 && !preg_match( "/^[A-Za-z0-9]+\$/", $_POST['uname'] ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '用户名只能是英文和数字' ) ) );
		} else if ( !empty( $closearr ) && in_array( $_POST['uname'], $closearr ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '该用户名禁止注册' ) ) );
		} 
		// 邮箱检查
		if ( strlen( $_POST['email'] ) == 0 ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '邮箱不能为空' ) ) );
		} else if ( !preg_match( "/^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+([\\.][a-z0-9-]+)+\$/i", $_POST['email'] ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '邮箱格式错误' ) ) );
		} 
		// 密码检查
		if ( strlen( $_POST['password'] ) == 0 || strlen( $_POST['repassword'] ) == 0 ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '密码不能为空' ) ) );
		} else if ( $_POST['password'] != $_POST['repassword'] ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '密码和重复密码不一致' ) ) );
		} else if ( !empty( $saxueConfigs['member']['regminpwd'] ) && strlen( $_POST['password'] ) < intval( $saxueConfigs['member']['regminpwd'] ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '密码长度不能少于' . $saxueConfigs['member']['regminpwd'] . '个字符' ) ) );
		} else if ( !empty( $saxueConfigs['member']['regmaxpwd'] ) && strlen( $_POST['password'] ) > intval( $saxueConfigs['member']['regmaxpwd'] ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '密码长度不能大于' . $saxueConfigs['member']['regmaxpwd'] . '个字符' ) ) );
		} else if ( $saxueConfigs['member']['regnpdifferf'] == 1 && $_POST['password'] == $_POST['uname'] ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '密码和用户名不能相同' ) ) );
		} 
		// 密码复杂度检查
		if ( !empty( $saxueConfigs['member']['regchrpwd'] ) ) {
				$pwdrule = explode( ',', $saxueConfigs['member']['regchrpwd'] );
				foreach ( $pwdrule as $r ) {
						switch ( intval( $r ) ) {
								case 1:
										if ( !preg_match( "/[a-z]/", $_POST['password'] ) ) {
												exit( json_encode( array( 'flag' => 0, 'msg' => '密码中必须包含小写字母' ) ) );
										} 
										break;
								case 2:
										if ( !preg_match( "/[A-Z]/", $_POST['password'] ) ) {
												exit( json_encode( array( 'flag' => 0, 'msg' => '密码中必须包含大写字母' ) ) );
										} 
										break;
								case 3:
										if ( !preg_match( "/[0-9]/", $_POST['password'] ) ) {
												exit( json_encode( array( 'flag' => 0, 'msg' => '密码中必须包含数字' ) ) );
										} 
										break;
								case 4:
										if ( !preg_match( "/[^a-zA-Z0-9]/", $_POST['password'] ) ) {
												exit( json_encode( array( 'flag' => 0, 'msg' => '密码中必须包含符号' ) ) );
										} 
										break;
								default:
										break;
						} 
				} 
		} 
		// 验证码检查
		if ( !isset( $_POST['vcode'] ) && !empty( $saxueConfigs['member']['regcheckcode'] ) && ( empty( $_POST['checkcode'] ) || ( strtolower( $_POST['checkcode'] ) != strtolower( $_SESSION['saxueCheckCode'] ) ) ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '验证码错误' ) ) );
		} 
		// 用户名和邮箱重复检查
		if ( 0 < $users_handler -> getcount( new criteria( "uname", $_POST['uname'] ) ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '用户名已经被人注册' ) ) );
		} 
		if ( 0 < $users_handler -> getcount( new criteria( "email", $_POST['email'] ) ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '邮箱已经被人注册' ) ) );
		} 
		// 数据入库
		$newUser = $users_handler -> create();
		$newUser -> setvar( "password", $users_handler -> encryptpass( $_POST['password'] ) );
		// 用户状态
		if ( empty( $saxueConfigs['member']['regacttype'] ) ) {
				$newUser -> setvar( "status", 1 );
		} else {
				$newUser -> setvar( "status", 0 );
		}
		// 注册奖励
		$saxueConfigs['member']['regscore'] = intval( $saxueConfigs['member']['regscore'] );
		$saxueConfigs['member']['regmoney'] = saxue_dround( $saxueConfigs['member']['regmoney'] );
		$newUser -> setvar( "score", $saxueConfigs['member']['regscore'] );
		$newUser -> setvar( "money", $saxueConfigs['member']['regmoney'] );
		// 注册失败
		if ( !$users_handler -> insert( $newUser ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '注册失败，请稍后重试' ) ) );
		} 
		$_POST['uid'] = $newUser -> getvar( "uid", "n" );
		// 用户附表
		include SAXUE_ROOT_PATH . "/model/member_userdata.php";
		$userdata_handler = &saxuememberuserdatahandler :: getinstance( "saxuememberuserdatahandler" ); 
		$userdata = $userdata_handler -> create();
		$userdata -> setvar( "regtime", SAXUE_NOW_TIME );
		$userdata_handler -> insert( $userdata );
		// 注册奖励积分记录
		if ( $saxueConfigs['member']['regscore'] > 0 ) {
				include SAXUE_ROOT_PATH . "/model/member_score.php";
				$score_handler = &saxuememberscorehandler :: getinstance( "saxuememberscorehandler" );
				$_record = array( 'uid' => $_POST['uid'], 'amount' => $saxueConfigs['member']['regscore'], 'balance' => $saxueConfigs['member']['regscore'], 'type' => 1 );
				$score_handler -> addlog( $_record );
		}
		//  注册奖励资金记录
		if ( $saxueConfigs['member']['regmoney'] > 0 ) {
				include SAXUE_ROOT_PATH . "/model/member_money.php";
				$money_handler = &saxuemembermoneyhandler :: getinstance( "saxuemembermoneyhandler" );
				$_record = array( 'uid' => $_POST['uid'], 'amount' => $saxueConfigs['member']['regmoney'], 'balance' => $saxueConfigs['member']['regmoney'], 'type' => 1 );
				$money_handler -> addlog( $_record );
		}
		// IP注册限制
		if ( 0 < $saxueConfigs['member']['regtimelimit'] ) {
				$sql = "DELETE FROM " . saxue_dbprefix( "member_registerip" ) . " WHERE regtime<" . ( SAXUE_NOW_TIME - ( 72 < $saxueConfigs['member']['regtimelimit'] ? $saxueConfigs['member']['regtimelimit'] : 72 ) * 3600 );
				$users_handler -> execute( $sql );
				$sql = "INSERT INTO " . saxue_dbprefix( "member_registerip" ) . " (ip, regtime, count) VALUES ('" . saxue_dbslashes( $_POST['regip'] ) . "', '" . SAXUE_NOW_TIME . "', '0')";
				$users_handler -> execute( $sql );
		} 
		switch ( $saxueConfigs['member']['regacttype'] ) {
				case 1:
						// 发送注册验证邮件
						include SAXUE_ROOT_PATH . '/common/function.php';
						$data = $_POST['uid'] . $_POST['email'] . $newUser -> getvar( "password" );
						$data = $_POST['uid'] . '|' . SAXUE_NOW_TIME . '|' . md5( $data );
						$verifyurl = SAXUE_URL . '/member/activate.php?data=' . saxue_crypt( $data, 'ENCODE' );
						$content = $_POST['uname'] . '，<br>请点击以下链接激活您的帐号：<br>' . $verifyurl . '<br>(如无法打开，请将该地址手工粘贴到浏览器地址栏再访问)';
						$ret = saxue_sendmail( $_POST['email'], '注册验证 - ' . SAXUE_SITE_NAME, $content );
						if ( $ret['flag'] ) {
								exit( json_encode( array( 'flag' => 1 ) ) );
						}
						exit( json_encode( array( 'flag' => 3, 'msg' => $ret['msg'] ) ) );
				case 2:
						// 管理员审核
						exit( json_encode( array( 'flag' => 2 ) ) );
				case 0:
				default:
						// 无需审核时直接登录
						$_SESSION['saxueUserId'] = $newUser -> getvar( "uid", "n" );
						$_SESSION['saxueUserName'] = $newUser -> getvar( "uname", "n" );
						$_SESSION['saxueUserEmail'] = $newUser -> getvar( "email", "n" );
						$_SESSION['saxueUserScore'] = $newUser -> getvar( "score", "n" );
						$_SESSION['saxueUserMoney'] = $newUser -> getvar( "money", "n" );
						exit( json_encode( array( 'flag' => 200 ) ) );
		}
} 
include SAXUE_WEB_PATH . "/header.php";
$saxueTpl -> assign( "jumpurl", saxue_htmlstr( $jumpurl ) );
$saxueTpl -> assign( "config", $saxueConfigs['member'] );
$saxueTpl -> setcaching( 0 );
$saxueTset['saxue_page_template'] = SAXUE_THEME_PATH . '/member/register.html';
include SAXUE_WEB_PATH . "/footer.php";
