<?php

//验证登录方式
function MemberConnect_CheckApptype($apptype){
	global $empire,$dbtbpre;
	$appr=$empire->fetch1("select * from {$dbtbpre}enewsmember_connect_app where apptype='$apptype' and isclose=0 limit 1");
	if(!$appr['id'])
	{
		printerror2('请选择登录方式','../../../');
	}
	return $appr;
}

//验证openid
function MemberConnect_CheckOpenid($apptype,$openid){
	global $empire,$dbtbpre;
	$mcr['id']=0;
	$mcr['userid']=0;
	if(!$apptype||!trim($openid))
	{
		return $mcr;
	}
	$mcr=$empire->fetch1("select id,userid from {$dbtbpre}enewsmember_connect where apptype='$apptype' and bindkey='$openid' limit 1");
	return $mcr;
}

//处理登录
function MemberConnect_DoLogin($apptype,$openid){
	global $empire,$dbtbpre,$public_r;
	$apptype=RepPostVar($apptype);
	$openid=RepPostVar($openid);
	$mcr=MemberConnect_CheckOpenid($apptype,$openid);
	if($mcr['id'])
	{
		$lifetime=0;
		$r=$empire->fetch1("select ".eReturnSelectMemberF('*')." from ".eReturnMemberTable()." where ".egetmf('userid')."='".$mcr['userid']."' limit 1");
		DoEcmsMemberLogin($r,$lifetime);
		MemberConnect_UpdateBindLogin($mcr['id']);
		MemberConnect_ResetVar();
		printerrortourl('../../../user/');
	}
	else
	{
		
		if($apptype=='qq'&&$public_r[add_qqbind]){
		$nickname=$_SESSION['nickname'];
		$nickname=iconv('utf-8','gb2312',$nickname);
		$userpic=$_SESSION['headerimg'];
		$username=substr($openid,0,20);
		$password='123456';
		$salt=eReturnMemberSalt();
	    $password=eDoMemberPw($password,$salt);
		$groupid=1;
		$email='';
		$lasttime=time();
		$registertime=eReturnAddMemberRegtime();
		$rnd=make_password(20);//产生随机密码
		$userkey=eReturnMemberUserKey();
		$registertime=time();
		$regip=egetip();
		$checked=1;
		$spacestyleid=1;	
		$sql=$empire->query("insert into ".eReturnMemberTable()."(".eReturnInsertMemberF('username,password,rnd,email,registertime,groupid,userfen,userdate,money,zgroupid,havemsg,checked,salt,userkey').") values('$username','$password','$rnd','$email','$registertime','$groupid','$public_r[reggetfen]','0','0','0','0','$checked','$salt','$userkey');");
	//取得userid
		$userid=$empire->lastid();
	//附加表
		$addr=$empire->fetch1("select * from {$dbtbpre}enewsmemberadd where userid='$userid'");
		if(!$addr[userid])
		{
		$spacestyleid=1;
		$sql1=$empire->query("insert into {$dbtbpre}enewsmemberadd(userid,spacestyleid,regip,lasttime,lastip,loginnum,userpic,nickname) values('$userid','$spacestyleid','$regip','$lasttime','$regip','1','$userpic','$nickname');");
		}
	MemberConnect_CheckReBind($apptype,$userid);
	$empire->query("insert into {$dbtbpre}enewsmember_connect(userid,apptype,bindkey,bindtime,loginnum,lasttime) values('$userid','$apptype','$openid','$time',1,'$time');");		
		$r=$empire->fetch1("select ".eReturnSelectMemberF('*')." from ".eReturnMemberTable()." where ".egetmf('userid')."='$userid' limit 1");
		$set1=esetcookie("mlusername",$username,$logincookie);
		$set2=esetcookie("mluserid",$userid,$logincookie);
		$set3=esetcookie("mlgroupid",$groupid,$logincookie);
		$set4=esetcookie("mlrnd",$rnd,$logincookie);
		printerrortourl('../../../user/');
		}
		else{
		printerrortourl('../../../user/login.php?tobind=1');
		}
	}
}

//更新登录绑定
function MemberConnect_UpdateBindLogin($id){
	global $empire,$dbtbpre;
	$id=(int)$id;
	$lasttime=time();
	$empire->query("update {$dbtbpre}enewsmember_connect set loginnum=loginnum+1,lasttime='$lasttime' where id='$id' limit 1");
}

//写入登陆绑定
function MemberConnect_InsertBind($apptype,$openid,$userid){
	global $empire,$dbtbpre;
	$apptype=RepPostVar($apptype);
	$openid=RepPostVar($openid);
	$userid=(int)$userid;
	$time=time();
	//验证是否重复
	MemberConnect_CheckReBind($apptype,$userid);
	MemberConnect_checkNicknameAndUserpic($userid);
	$empire->query("insert into {$dbtbpre}enewsmember_connect(userid,apptype,bindkey,bindtime,loginnum,lasttime) values('$userid','$apptype','$openid','$time',1,'$time');");
}

//验证是否绑定过
function MemberConnect_CheckReBind($apptype,$userid){
	global $empire,$dbtbpre;
	$num=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsmember_connect where userid='$userid' and apptype='$apptype' limit 1");
	if($num)
	{
		printerror2("此帐号已绑定过，不能重复绑定","history.go(-1)");
	}
}

//删除登陆绑定
function MemberConnect_DelBind($id){
	global $empire,$dbtbpre,$public_r;
	$user_r=islogin();//是否登陆
	$id=(int)$id;
	$sql=$empire->query("delete from {$dbtbpre}enewsmember_connect where id='$id' and userid='$user_r[userid]';");
	if($sql)
	{
		printerror2("已解除绑定","../memberconnect/ListBind.php");
	}
	else
	{
		printerror("DbError","history.go(-1)",1);
	}
}

//原帐号绑定登录
function MemberConnect_BindUser($userid){
	global $empire,$dbtbpre,$public_r;
	$apptype=RepPostVar($_SESSION['apptype']);
	$openid=RepPostVar($_SESSION['openid']);
	if(!trim($apptype)||!trim($openid))
	{
		printerror2('接口参数出错','../../../');
	}
	$appr=MemberConnect_CheckApptype($apptype);//验证登录方式
	MemberConnect_CheckBindKey($apptype,$openid);
	MemberConnect_InsertBind($apptype,$openid,$userid);
	MemberConnect_ResetVar();

}
function MemberConnect_checkNicknameAndUserpic($userid){
	global $empire,$dbtbpre,$public_r;
		$nickname=$_SESSION['nickname'];
		$nickname=iconv('utf-8','gb2312',$nickname);
		$userpic=$_SESSION['headerimg'];
	if(!trim($nickname)||!trim($userpic))
	{
		printerror2('未获得昵称和头像','../../../');
	}
	$ur=$empire->fetch1("select userpic,userid,nickname from {$dbtbpre}enewsmemberadd where userid='$userid'");
	if(!$ur[nickname]){
		$up=$empire->query("update {$dbtbpre}enewsmemberadd set nickname='$nickname' where userid='$userid'");
	}
	if(!$ur[userpic]){
		$up=$empire->query("update {$dbtbpre}enewsmemberadd set userpic='$userpic' where userid='$userid'");
	}
}
//绑定验证符
function MemberConnect_GetBindKey($apptype,$openid){
	global $ecms_config;
	$checkpass=md5(md5('check-'.$apptype.'-empirecms-'.$openid).'-#empire.cms!-'.$openid.'-|-empirecms-|-'.$ecms_config['cks']['ckrndtwo']);
	return $checkpass;
}

//验证绑定验证符
function MemberConnect_CheckBindKey($apptype,$openid){
	global $ecms_config;
	$pass=md5(md5('check-'.$apptype.'-empirecms-'.$openid).'-#empire.cms!-'.$openid.'-|-empirecms-|-'.$ecms_config['cks']['ckrndtwo']);
	$checkpass=$_SESSION['openidkey'];
	if($pass!=$checkpass)
	{
		printerror2('来自的链接不存在','../../../');
	}
}

//重置变量
function MemberConnect_ResetVar(){
	$_SESSION['state']='';
	$_SESSION['openid']='';
	$_SESSION['apptype']='';
	$_SESSION['openidkey']='';
}
?>