<?php
//引入公共模块
	require("foundation/module_scrip.php");
	require("foundation/aanti_refresh.php");
	require("foundation/adata_count.php");
	require("foundation/module_users.php");
	require("foundation/aintegral.php");

//引入语言包
$re_langpackage=new reglp;

function checkmail($nmail){   /// 验证电子邮件地址
	if(ereg("^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+",$nmail))
		return true;
	else
		return false;
}

if(get_argp("u_name")==NULL){
   action_return(0,$re_langpackage->re_empty_name,"-1");
}

if(!login_check(get_argp("u_name"))){
	 action_return(0,$re_langpackage->re_right_name,"-1");
}

if(!checkmail(get_argp("u_email"))||(!login_check(get_argp("u_email")))) {
    action_return(0,$re_langpackage->re_right_email,"-1");
}

if(strlen(get_argp("u_pws"))<6){
	  action_return(0,$re_langpackage->re_pass_limit,"-1");
}

//数据表定义区
$t_users=$tablePreStr."users";
$t_online=$tablePreStr."online";
$t_pals_def_sort=$tablePreStr."pals_def_sort";
$t_pals_sort=$tablePreStr."pals_sort";
$t_data_count=$tablePreStr."data_count";
$t_mypals=$tablePreStr."pals_mine";

$user_name=short_check(get_argp("u_name"));
$user_pws=short_check(get_argp("u_pws"));
$user_sex=short_check(get_argp("u_sex"));
$user_email=short_check(get_argp("u_email"));
$user_pass=1;
$user_vericode=get_argp("veriCode");
$invite_fromuid=0;
$b_year=short_check(get_argp("birth_year"));
$b_month=short_check(get_argp("birth_month"));
$b_day=short_check(get_argp("birth_day"));
$inv_id=short_check(get_argp('uid'));

if(get_session('InviteFromUid')){
	  $invite_fromuid=get_session('InviteFromUid');
}

if(strtolower($_SESSION['verifyCode'])!=strtolower($user_vericode)){
  action_return(0,$re_langpackage->re_wrong_val,"-1");
}

unset($_SESSION['verifyCode']);

//读取数据
dbtarget('r',$dbServs);
$dbo=new dbex;
$sql="select * from $t_users where user_email='$user_email'";
$user_info=$dbo->getRow($sql);

$sql="select * from $t_pals_def_sort";
$sort_rs=$dbo->getRs($sql);

if($user_info){
	  action_return(0,$re_langpackage->re_rep_mail,"-1");
}
else{
//写入数据
   $user_ico=($user_sex==0)?"skin/$skinUrl/images/d_ico_0_small.gif":"skin/$skinUrl/images/d_ico_1_small.gif";
   dbtarget('w',$dbServs);
   $sql="insert into $t_users (user_name,user_pws,user_sex,user_email,user_add_time,user_ico,invite_from_uid,user_pass,lastlogin_datetime,birth_year , birth_month , birth_day )"
                    ." values('$user_name','$user_pws',$user_sex,'$user_email',NOW(),'$user_ico',$invite_fromuid,$user_pass,NOW(),'$b_year','$b_month','$b_day')";
   
  if(!$dbo->exeUpdate($sql)){
  	  action_return(0,$re_langpackage->re_reg_false,"-1");
  }
  
  $user_id=mysql_insert_id();
  set_sess_userid($user_id);
  set_sess_usersex($user_sex);
  set_sess_username($user_name);
  set_sess_useremail($user_email);
  set_sess_userico($user_ico);
  set_sess_online('0');   
  $now_time=time();
  
	$sql="insert into $t_online (user_id,user_name,user_sex,user_ico,active_time,hidden) values ($user_id,'$user_name',$user_sex,'$user_ico','$now_time',0)";
	$dbo->exeUpdate($sql);
	$sql="insert into $t_data_count (user_id) values($user_id)";
	$dbo->exeUpdate($sql);
	 
	foreach($sort_rs as $rs){
		$sort_id=$rs['id'];
		$sort_name=$rs['name'];
		$sql="insert into $t_pals_sort ( name , user_id ) values ( '$sort_name' , $user_id )";
		$dbo->exeUpdate($sql);
	}
	
	if($invite_fromuid){
	  increase_integral($dbo,$int_invited,$invite_fromuid);
	//取得介绍人的资料信息
	  $select_items=array("user_id","user_name","user_sex","user_ico","palsreq_limit");
	  $user_row=get_user_info_item($dbo,$select_items,$t_users,$invite_fromuid);
	  if($user_row){
			$touser_id=$user_row['user_id'];
			$touser_name=$user_row['user_name'];
			$touser_sex=$user_row['user_sex'];
			$touser_ico=$user_row['user_ico'];
			$touser_pals_limit=$user_row['palsreq_limit'];
	  }
	  if($touser_pals_limit==0){
		  $sql="insert into $t_mypals (user_id,pals_id,pals_name,pals_sex,add_time,pals_ico,accepted) values ($user_id,$invite_fromuid,'$touser_name','$touser_sex',NOW(),'$touser_ico',1)";
		  $dbo->exeUpdate($sql);
		  set_sess_mypals($invite_fromuid);
			send_scrip($dbo,$user_name,$user_id,$userico,$invite_fromuid,3);
			update_count($dbo,$user_id,"pals_num",1);
	  }
}	
	
?>
   <script type='text/javascript'>
    location.href='./main.php';
    var Then=new Date();
    Then.setTime(Then.getTime() +3600*1000*24*300 );
  	document.cookie="IsReged=Y;expires="+ Then.toGMTString();
   </script>
<?php
   }

?>