<?php
if(!defined('R_P')) exit('Access Denied');
if(!$_SET['enablemember']) qbase::purviewCheckDie( '对不起，本站不允许会员注册！' );
if($user_info['username']) qbase::showmsg( "你已经本站会员了！", $_SET['skin'], "index.php" );
if ( $_POST['myFormAc'] == "update"){
	require_once( R_P.'/include/class.login.php' );
	$login = new login();
	$verify_code  = trim($_POST['verify_code']);
	$username  = trim($_POST['username']);
	$password  = trim($_POST['password']);
	$pwdconfirm  = trim($_POST['pwdconfirm']);
	$email = trim($_POST['email']);

	if($password != $pwdconfirm) output_error( "密码和确认密码不一致" );
	if(empty($email) || strlen($email) > 64 ||  !ereg("^[-a-zA-Z0-9_\.]+\@([0-9A-Za-z_][0-9A-Za-z-]+\.)+[A-Za-z]{2,5}$",$email )) output_error( "请输入有效的邮件地址！" );
	if(function_exists('checkdnsrr')){
		if(!checkdnsrr(array_pop(explode("@",$email)),"MX")){
			output_error( "请输入有效的邮件地址！" );
		}
	}
	if(intval($_SET['reg_code']) > 0){
		$code = strtolower(trim($_SESSION['code']));
		$verify_code = strtolower(trim($_POST['verify_code']));
		unset($_SESSION['code']);
		if(!$code || $code != $verify_code) output_error("出错了，验证码错误");
	}	
	$checkInfo = $login->check($username);
	if( $checkInfo['status'] != 200){
		output_error('长度不对或者存在非法字符');
	}else{
		$checkInfo = $login->checkOnly( $username);
		if($num){
			output_error("该用户名已经存在");	
		}
	}

	if($login->checkEmail($email)){
		output_error("该Email已经有人使用过了，请换一个");	
	}
	
	$m = $login->checkIp();
	if($_SERVER['REQUEST_TIME'] - $m < $_SET['reg_ip'] * 60){
		$t = $m + $_SET['reg_ip'] * 60 - $_SERVER['REQUEST_TIME'];
		$n = intval($t / 60);
		$s = $t % 60;
		output_error("系统不允许同一个IP在{$_SET['reg_ip']}分钟内多次注册\n\n你需要再等{$n}分{$s}秒后才可以注册");	
	}
	
	$param = array();

	if(U_C){
		$uc_user = uc_user_checkname($username);
		if($uc_user > 0) {
			
		} elseif($uc_user == -1) {
			output_error('用户名不合法');
		} elseif($uc_user == -2) {
			output_error('用户名包含不允许注册的词语');
		} elseif($uc_user == -3) {
			output_error('用户名已经存在');
		} else {
			output_error('system error');
		}

		$uc_uid = uc_user_register($username, $password, $email);
		if($uc_uid <= 0) {
			if($uc_uid == -1) {
				output_error( '用户名不合法');
			} elseif($uc_uid == -2) {
				output_error( '包含要允许注册的词语');
			} elseif($uc_uid == -3) {
				output_error( '用户名已经存在');
			} elseif($uc_uid == -4) {
				output_error( 'Email 格式有误');
			} elseif($uc_uid == -5) {
				output_error( 'Email 不允许注册');
			} elseif($uc_uid == -6) {
				output_error( '该 Email 已经被注册');
			} else {
				output_error('system error');
			}
		} else {
			$param['uid'] = $uc_uid;
		}
	}

	$param['password'] = md5(stripslashes($password));
	$param['username'] = $username;
	$param['email'] = $email;
	$param['email_verify'] = 0;
	$param['regtime'] = $param['logintime'] = $_SERVER['REQUEST_TIME'];
	$param['regip'] = $param['loginip'] = qbase::get_ip();
	$param['logintimes'] = 1;
	$uid = qdb::query(qbase::createSql(TABLEPRE."user", $param, "replace"), true);

	$param_extend = array();
	$param_extend['gid'] = intval($_SET['user_verify']) ? 1 : 2;
	$param_extend['username'] = $param['username'];
	$param_extend['uid'] = $uid;
	$param_extend['point'] = 0;
	qdb::query(qbase::createSql("".TABLEPRE."user_extend", $param_extend, "insert"));
	
	$session = substr(md5(qbase::random(6) . $param['regip'] . $uid), 0, 12);
	$user_info =array(
		'u' => $uid,
		's'=> $session
	);
	qdb::query("update ".TABLEPRE."user set session = '{$session}' where uid = '{$uid}' ");

	if($_SET['mail_verify']){
		//need mail verify
		$url = "user.php?ac=mail_verify&uid={$uid}&session={$session}";
		echo qbase::xmlPrint(qbase::jsonencode(array('status'=>302, 'message'=>$url)));
	}else if($_SET['user_verify']){
		//need user verify
		$url = "user.php?ac=user_verify&uid={$uid}&session={$session}";
		echo qbase::xmlPrint(qbase::jsonencode(array('status'=>302, 'message'=>$url)));
	}else{
		qdb::ChangePoint($verify_data['uid'], $usergroup[2]['point']);
		$user_info_string = qbase::authcode(qbase::array2string($user_info), "ENCODE");
		qbase::phpsetcookie("qmecms_auth", $user_info_string, 0);
		qbase::get_object("sendmail", $param['username'], $param['email'])->op_reg($uid, $session);
		echo qbase::xmlPrint(qbase::jsonencode(array('status'=>200)));
	}
}else if( $_POST['check'] == 1){
	$login = qbase::get_object('login');
	$username  = trim($_POST['username']);
	$checkInfo = $login->check( $username);
	if( $checkInfo['status'] == 200){
		unset($checkInfo);
		$num = $login->checkOnly( $username);
		if($num){
			$msg = array( 'status'=>417, 'message'=>'该用户已经存在') ;
		}else{
			if(U_C){
				$uc_user = uc_user_checkname($username);
				if($uc_user > 0) {
					$msg = array( 'status'=>200);
				} elseif($uc_user == -1) {
					$msg = array( 'status'=>417, 'message'=>'用户名不合法');
				} elseif($uc_user == -2) {
					$msg = array( 'status'=>417, 'message'=>'用户名包含不允许注册的词语');
				} elseif($uc_user == -3) {
					$msg = array( 'status'=>417, 'message'=>'用户名已经存在');
				} else {
					$msg = array( 'status'=>417, 'message'=>'system error');
				}
			}else{
				$msg = array( 'status'=>200);
			}
		}
	}else{
		$msg = array( 'status'=>417, 'message'=>'长度不对/存在非法字符');
	}
	echo qbase::xmlPrint(qbase::jsonencode($msg));
}else{
	$forward = urlencode($_GET['forward']);
	$forward_encode = addslashes($_GET['forward']);
	include qbase::template($_SET['skin']  . '/user/reg');
}

function output_error($msg){
	$data = array('status'=>404, 'message'=>$msg);
	echo qbase::xmlPrint(qbase::jsonencode($data));
	exit();
}
?>