<?php
CURSCRIPT != 'wap' && exit('FORBIDDEN');

$mayimobile['register'] != 1 && errormsg('本站手机版已关闭注册功能！如需注册，请打开 '.$mymps_global[SiteUrl].' 网页后再进行注册！');
$authcodesettings = read_static_cache('authcodesettings');

if($do_action == 'register'){

	include MYMPS_ROOT.'/member/include/common.func.php';
	
	if(!empty($mymps_global['cfg_forbidden_reg_ip'])){
		$ip = GetIP();
		foreach(explode(",", $mymps_global['cfg_forbidden_reg_ip']) as $ctrlip) {
			if(preg_match("/^(".preg_quote(($ctrlip = trim($ctrlip)), '/').")/", $ip)) {
				$ctrlip = $ctrlip.'%';
				errormsg("您当前的IP <b style='color:red'>".$ip."</b> 已被管理员加入黑名单，不允许注册网站会员！");
				exit;
			}
		}
		unset($ctrlip);
	}
	
	if(!$mixcode || $mixcode != md5($cookiepre)){
		errormsg('系统判断您的来路不正确！');
	}

	if($authcodesettings['register'] == 1 && !mymps_chk_randcode($checkcode)){
		errormsg('验证码输入错误，请返回重新输入');
	}
	
	if($mymps_global['cfg_member_verify'] == 4){
		if(!$mobile) write_msg('请输入您的手机号码!');
		if(!is_mobile($mobile)) write_msg('手机号格式不正确!');
		if($db->getOne("SELECT COUNT(id) FROM `{$db_mymps}member` WHERE mobile = '$mobile'") > 0) errormsg('该手机号已被注册请换一个手机号！');
		if(!mymps_chk_smsrandcode($smscheckcode,$mobile)) errormsg('手机验证码输入不正确或与手机号不匹配！');
		$userid = $mymps_global['cfg_member_mobile']==1?$mobile:rand(20,100).$timestamp;
		$email = $userid.'@139.com';
		$mobiler = " sj_certify = 1,"; 
	}else{
		strlen($userid) > 20 && errormsg('您的用户名多于20个字符，不允许注册!');
		$rs	= CheckUserID($userid,'用户名');
		$rs != 'ok' && redirectmsg($rs,$mayimobile['mobiledomain'].'/index.php?mod=register');
		!is_email($email) && errormsg('Email格式不正确！');
		$mobiler = " "; 
	}
	
	(strlen($userid) < 3 || strlen($userpwd) < 5) && errormsg('你的用户名或密码过短(不能少于3个字符)，不允许注册!');
	$db->getOne("SELECT id FROM `{$db_mymps}member` WHERE userid = '$userid' ") && errormsg('你指定的用户名 '.$userid.' 已存在，请使用别的用户名!');
	
	if(PASSPORT_TYPE == 'ucenter'){
		//uc整合
		require MYMPS_ROOT.'/uc_client/client.php';
		//在UCenter注册用户信息
		if($activation && ($activeuser = uc_get_user($activation))) {
			list($uid, $userid) = $activeuser;
		} else {
			$user = $db -> getRow("SELECT id,userid FROM `{$db_mymps}member` WHERE userid = '$userid'");
			if(uc_get_user($userid) && !$user['userid']) {
				//判断需要注册的用户如果是需要激活的用户，则需跳转到登录页面
				redirectmsg("该用户无需注册，请重新登录",$mymps_global[SiteUrl]."/m/index.php?m=login");
			}
			$uid = uc_user_register($userid,$userpwd, $email);
			if($uid <= 0) {
				if($uid == -1) {
					errormsg('用户名不合法');
				} elseif($uid == -2) {
					errormsg( '包含不允许注册的词语');
				} elseif($uid == -3) {
					errormsg( '用户名已经存在');
				} elseif($uid == -4) {
					errormsg( 'Email 格式有误');
				} elseif($uid == -5) {
					errormsg( 'Email 不允许注册');
				} elseif($uid == -6) {
					errormsg( '该 Email 已经被注册');
				} else {
					errormsg( '未定义');
				}
			} else {
				$userid  = trim($userid);
				$userpwd = trim($userpwd);
				$email 	 = trim($email);
			}
		}
	}
	
	$score_change = get_credit_score();
	$score_changer = $score_change['score']['rank']['register'];
	$score_changer = $score_changer === 0 ? ' +0' : $score_changer;
	
	$invite = mgetcookie('invite');
	member_reg($userid,md5($userpwd),$email,'','','','','','','','',$invite);
	$uid = $db->insert_id();
	
	if($reg_corp == 1 && $tname && $introduce){
		if(is_array($catid)){
			foreach($catid as $kids => $vids){
				$db->query("INSERT INTO `{$db_mymps}member_category` (userid,catid)VALUES('$userid','$vids')");
			}
			$catids  = implode(',',$catid);
		}
		$db->query("UPDATE `{$db_mymps}member` SET ".$mobiler."tname = '$tname' , catid = '$catids',areaid = '$areaid',streetid='$streetid', if_corp = '1' ,tel = '$tel' , introduce = '$introduce',score = score".$score_changer."  WHERE userid = '$userid'");
	} else {
		$db->query("UPDATE `{$db_mymps}member` SET ".$mobiler."mobile='$mobile',score = score".$score_changer." WHERE userid = '$userid'");
	}
	
	if($mymps_global['cfg_member_verify'] == 1 || $mymps_global['cfg_member_verify'] == 4){
		$member_log -> in($userid,md5($userpwd),'off','noredirect');
		redirectmsg('恭喜! 您已经注册成功！',$mayimobile['mobiledomain'].'/index.php?mod=member');
		
	}elseif($mymps_global['cfg_member_verify'] == 3){
		require MYMPS_INC.'/email.fun.php';
		$code = base64_encode($uid.".".md5($uid.'+'.md5($userpwd)).'.'.$timestamp);
		$webmail = NULL;
		if(send_validate_email($uid, $userid, $email, $code)){
			$error = '2';
			$mailweb = explode('@',$email);
			$mailweb = strtoupper($mailweb[1]);
			switch($mailweb){
				case 'QQ.COM': $webmail = 'http://mail.qq.com';$webname = 'QQ邮箱'; break;
				case '163.COM': $webmail = 'http://mail.163.com';$webname = '163邮箱'; break;
				case '126.COM': $webmail = 'http://www.126.com';$webname = '126邮箱'; break;
				case '188.COM': $webmail = 'http://www.188.com';$webname = '188邮箱'; break;
				case 'YEAH.NET': $webmail = 'http://www.yeah.net';$webname = '网易yeah邮箱'; break;
				case 'ALIYUN.COM': $webmail = 'http://mail.aliyun.com';$webname = '阿里云邮箱'; break;
				case 'GMAIL.COM': $webmail = 'http://www.gmail.com';$webname = 'Gmail邮箱'; break;
				case 'SOHU.COM': $webmail = 'http://mail.sohu.com';$webname = '搜狐邮箱'; break;
				case 'SINA.COM.CN': $webmail = 'http://mail.sina.com.cn';$webname = '新浪邮箱'; break;
				case 'SINA.NET': $webmail = 'http://mail.sina.net';$webname = '新浪邮箱'; break;
				case 'FOXMAIL.COM': $webmail = 'http://mail.foxmail.com';$webname = 'foxmail邮箱'; break;
				case 'HOTMAIL.COM': $webmail = 'http://mail.hotmail.com';$webname = '微软邮箱'; break;
				case 'LIVE.COM': $webmail = 'http://mail.live.com';$webname = '微软邮箱'; break;
			}
		}
		redirectmsg('审核邮件已发送，请登录'.$mailweb.'查收！',$mayimobile['mobiledomain'].'/index.php?mod=index');
	}else{
		redirectmsg('注册成功！审核通过后可正常登录！',$mayimobile['mobiledomain'].'/index.php?mod=index');
	}
} else {
	/*if($iflogin ==1 && $action == 'store'){
		write_msg('',$mayimobile['mobiledomain'].'/index.php?mod=member&action=upgradecorp');
		exit;
	}elseif($iflogin == 1){
		redirectmsg('您已登录，请退出后再提交注册 ^_^',$mayimobile['mobiledomain'].'/index.php?mod=member');
		exit;
	}*/
	if($iflogin ==1 ){
		$row = $db -> getRow("SELECT * FROM `{$db_mymps}member` WHERE userid = '$s_uid'");
		if($row['if_corp'] == 1){
			redirectmsg('您已是商家会员了 ^_^',$mayimobile['mobiledomain'].'/index.php?mod=member');
		}else{
			redirectmsg('请到用户中心，完善资料，升级商家会员 ^_^',$mayimobile['mobiledomain'].'/index.php?mod=member&action=editbase');
		}
		exit;
	}
	$action = in_array($action,array('person','store')) ? $action : 'person';
	$mixcode = md5($cookiepre);
	globalassign();
	$mymps_global['SiteXieyi']=str_replace('{sitename}',$mymps_global['SiteName'],$mymps_global['SiteXieyi']);
	include mayi_tpl('member_register');
}
?>