<?php
//表前缀
if(!defined('InEmpireCMS'))
{$user_tbpre="phome_";}
else
{$user_tbpre=$dbtbpre;}

//---------------------- 下面开始会员相关设置 ----------------------

$user_tablename=$user_tbpre."enewsmember";                //用户表
$user_userid="userid";                                    //用户ID字段
$user_username="username";                                //用户名字段
$user_password="password";                                //密码字段
$user_dopass=0;                                           //密码保存形式,0为md5,1为明码,2为双重加密,3为16位md5
$user_rnd="rnd";                                          //随机密码
$user_email="email";                                      //邮箱字段
$user_checked="checked";                                  //审核状态字段
$user_registertime="registertime";                        //注册时间字段
$user_regcookietime=0;									  //注册信息保存时间(秒)
$user_register=0;                                         //注册时间保存形式,0为正常时间,1为数值型
$user_group="groupid";                                    //会员组字段
$user_userfen="userfen";                                  //点数字段
$user_userdate="userdate";                                //包月字段
$user_zgroup="zgroupid";								  //到期转向会员组
$user_money="money";                                      //帐户余额
$user_todaydate="todaydate";                              //下载日期
$user_todaydown="todaydown";                              //当天下载次数
$user_havemsg="havemsg";                                  //当天下载次数
$user_groupid=$public_r[defaultgroupid];                  //注册时会员组ID(ecms的会员组)

//会员页面
$changeregisterurl="";								      //多会员组中转注册地址
$registerurl="";										  //注册地址
$eloginurl="";											  //登陆地址
$equiturl="";											  //退出地址

//特殊设置(如vbb,molyx)
$user_salt="salt";                                        //salt
$user_saltnum=3;                                          //salt随机码字符数

$utfdata=0;    //数据是否是UTF8编码,0为正常数据,1为UTF8编码

//---------------------- 会员相关设置结束 ----------------------

//编码转换
function doUtfAndGbk($str,$phome=0){
	global $utfdata;
	if(empty($utfdata))//正常编码
	{
		return $str;
    }
	if(!function_exists("iconv"))//是否支持iconv
	{
		$fun="DoIconvVal";
		$code="GB2312";
		$targetcode="UTF8";
	}
	else
	{
		$fun="iconv";
		$code="GBK";
		$targetcode="UTF-8";
	}
	if(empty($phome))//gbk转utf
	{
		$str=$fun($code,$targetcode,$str);
	}
	else//utf转gbk
	{
		$str=$fun($targetcode,$code,$str);
	}
	return $str;
}

//登录附加cookie
function AddLoginCookie($r){
}

//取得表单id
function GetMemberFormId($groupid){
	global $empire,$dbtbpre;
	$r=$empire->fetch1("select formid from {$dbtbpre}enewsmembergroup where groupid='$groupid'");
	return $r['formid'];
}

//验证会员组是否可注册
function CheckMemberGroupCanReg($groupid){
	global $empire,$dbtbpre;
	$r=$empire->fetch1("select groupid from {$dbtbpre}enewsmembergroup where groupid='$groupid' and canreg=1");
	if(empty($r['groupid']))
	{
		printerror('ErrorUrl','',1);
	}
}

//后台修改资料
function admin_EditMember($add,$logininid,$loginin){
	global $empire,$user_tablename,$user_username,$user_userid,$user_password,$user_dopass,$user_group,$user_email,$user_userfen,$user_money,$user_userdate,$user_saltnum,$user_salt,$user_zgroup,$dbtbpre,$user_checked;
	if(!trim($add[userid])||!trim($add[email])||!trim($add[username])||!$add[groupid])
	{
		printerror("EmptyEmail","history.go(-1)");
	}
    CheckLevel($logininid,$loginin,$classid,"member");//验证权限
	//变量
	$add[userid]=(int)$add[userid];
	$add[checked]=(int)$add[checked];
	//编码转换
	$dousername=$add[username];
	$add[username]=doUtfAndGbk($add[username],0);
	$add[oldusername]=doUtfAndGbk($add[oldusername],0);
	$add[password]=doUtfAndGbk($add[password],0);
	$add[email]=doUtfAndGbk($add[email],0);
	//修改密码
	if($add[password])
	{
		if(empty($user_dopass))//单重md5
		{
		   $add[password]=md5($add[password]);
	    }
		elseif($user_dopass==2)//双重md5
		{
			$salt=make_password($user_saltnum);
			$add[password]=md5(md5($add[password]).$salt);
			$sa=",".$user_salt."='$salt'";
		}
		elseif($user_dopass==3)//16位md5
		{
			$add[password]=substr(md5($add[password]),8,16);
		}
		else
		{}
		$add1=",".$user_password."='".$add[password]."'".$sa;
    }
	//修改用户名
	if($add[oldusername]<>$add[username])
	{
		$num=$empire->gettotal("select count(*) as total from ".$user_tablename." where ".$user_username."='$add[username]' and ".$user_userid."<>".$add[userid]." limit 1");
		$add1.=",".$user_username."='$add[username]'";
		if($num)
		{
			printerror("ReUsername","history.go(-1)");
		}
	}
	//包月
	$add[zgroupid]=(int)$add[zgroupid];
	if($add[userdate]>0)
	{
		$userdate=time()+$add[userdate]*24*3600;
	}
	else
	{
		$add[zgroupid]=0;
	}
	//变量
	$add[groupid]=(int)$add[groupid];
	$add[userfen]=(int)$add[userfen];
	$userdate=(int)$userdate;
	$add[money]=(float)$add[money];
	$add[spacestyleid]=(int)$add[spacestyleid];
	$sql=$empire->query("update ".$user_tablename." set ".$user_email."='$add[email]',".$user_group."=$add[groupid],".$user_userfen."=$add[userfen],".$user_money."=$add[money],".$user_userdate."=$userdate,".$user_zgroup."=$add[zgroupid],".$user_checked."=$add[checked]".$add1." where ".$user_userid."='$add[userid]'");
	//附加表
	$addr=$empire->fetch1("select * from {$dbtbpre}enewsmemberadd where userid='$add[userid]'");
	$fid=GetMemberFormId($add[groupid]);
	if(empty($addr[userid]))
	{
		$member_r=ReturnDoMemberF($fid,$_POST,$mr,0);
		$sql1=$empire->query("insert into {$dbtbpre}enewsmemberadd(userid,spacestyleid".$member_r[0].") values($add[userid],$add[spacestyleid]".$member_r[1].");");
    }
	else
	{
		$member_r=ReturnDoMemberF($fid,$_POST,$addr,1);
		$sql1=$empire->query("update {$dbtbpre}enewsmemberadd set spacestyleid=$add[spacestyleid]".$member_r[0]." where userid='$add[userid]'");
    }
	if($sql)
	{
	   insert_dolog("userid=".$add[userid]."<br>username=".$dousername);//操作日志
	   printerror("EditMemberSuccess","ListMember.php");
	}
    else
	{
		printerror("DbError","history.go(-1)");
	}
}

//后台删除会员
function admin_DelMember($userid,$loginuserid,$loginusername){
	global $empire,$user_tablename,$user_username,$user_userid,$dbtbpre,$user_group;
	$userid=(int)$userid;
	if(empty($userid))
	{
		printerror("NotDelMemberid","history.go(-1)");
	}
    CheckLevel($loginuserid,$loginusername,$classid,"member");//验证权限
	$r=$empire->fetch1("select ".$user_username.",".$user_group." from ".$user_tablename." where ".$user_userid."='$userid'");
	if(empty($r[$user_username]))
	{
		printerror("NotDelMemberid","history.go(-1)");
	}
    $sql=$empire->query("delete from ".$user_tablename." where ".$user_userid."='$userid'");
	$dousername=doUtfAndGbk($r[$user_username],1);
	//删除附加表
	$fid=GetMemberFormId($r[$user_group]);
	DoDelMemberF($fid,$userid,$dousername);
	//删除收藏
	$del=$empire->query("delete from {$dbtbpre}enewsfava where userid='$userid'");
	$del=$empire->query("delete from {$dbtbpre}enewsfavaclass where userid='$userid'");
	//删除短信息
	$del=$empire->query("delete from {$dbtbpre}enewsqmsg where to_username='".$dousername."'");
	//删除购买记录
	$del=$empire->query("delete from {$dbtbpre}enewsbuybak where userid='$userid'");
	//删除下载记录
	$del=$empire->query("delete from {$dbtbpre}enewsdownrecord where userid='$userid'");
	//删除好友记录
	$del=$empire->query("delete from {$dbtbpre}enewshy where userid='$userid'");
	$del=$empire->query("delete from {$dbtbpre}enewshyclass where userid='$userid'");
	//删除留言
	$del=$empire->query("delete from {$dbtbpre}enewsmembergbook where userid='$userid'");
	//删除反馈
	$del=$empire->query("delete from {$dbtbpre}enewsmemberfeedback where userid='$userid'");
    if($sql)
	{
	    insert_dolog("userid=".$userid."<br>username=".$dousername);//操作日志
		printerror("DelMemberSuccess","ListMember.php");
	}
    else
	{
		printerror("DbError","history.go(-1)");
	}
}

//后台批量删除会员
function admin_DelMember_all($userid,$logininid,$loginin){
	global $empire,$user_tablename,$user_username,$user_userid,$dbtbpre,$level_r,$user_group;
    CheckLevel($logininid,$loginin,$classid,"member");//验证权限
    $count=count($userid);
    if(!$count)
	{
		 printerror("NotDelMemberid","history.go(-1)");
	}
	for($i=0;$i<$count;$i++)
	{
		$dh=",";
		if($i==0)
		{
			$dh="";
		}
		//删除短信息
		$ur=$empire->fetch1("select ".$user_username.",".$user_group." from ".$user_tablename." where ".$user_userid."='".$userid[$i]."'");
		if(empty($ur[$user_username]))
		{
			continue;
		}
		$dousername=doUtfAndGbk($ur[$user_username],1);
		//删除附加表
		$fid=GetMemberFormId($ur[$user_group]);
		DoDelMemberF($fid,$userid[$i],$dousername);
		$del=$empire->query("delete from {$dbtbpre}enewsqmsg where to_username='".$dousername."'");
		//集合
		$inid.=$dh.$userid[$i];
    }
	if(empty($inid))
	{
		printerror("NotDelMemberid","history.go(-1)");
	}
	$add=$user_userid." in (".$inid.")";
	$adda="userid in (".$inid.")";
	$sql=$empire->query("delete from ".$user_tablename." where ".$add);
	//删除收藏
	$del=$empire->query("delete from {$dbtbpre}enewsfava where ".$adda);
	$del=$empire->query("delete from {$dbtbpre}enewsfavaclass where ".$adda);
	//删除购买记录
	$del=$empire->query("delete from {$dbtbpre}enewsbuybak where ".$adda);
	//删除下载记录
	$del=$empire->query("delete from {$dbtbpre}enewsdownrecord where ".$adda);
	//删除好友记录
	$del=$empire->query("delete from {$dbtbpre}enewshy where ".$adda);
	$del=$empire->query("delete from {$dbtbpre}enewshyclass where ".$adda);
	//删除留言
	$del=$empire->query("delete from {$dbtbpre}enewsmembergbook where ".$adda);
	//删除反馈
	$del=$empire->query("delete from {$dbtbpre}enewsmemberfeedback where ".$adda);
	if($sql)
	{
	    insert_dolog("");//操作日志
		printerror("DelMemberSuccess","ListMember.php");
    }
	else
	{
		printerror("DbError","history.go(-1)");
    }
}

//审核会员
function admin_DoCheckMember_all($userid,$logininid,$loginin){
	global $empire,$user_tablename,$user_userid,$dbtbpre,$user_checked;
    CheckLevel($logininid,$loginin,$classid,"member");//验证权限
    $count=count($userid);
    if(!$count)
	{
		 printerror("NotChangeDoCheckMember","history.go(-1)");
	}
	for($i=0;$i<$count;$i++)
	{
		$dh=",";
		if($i==0)
		{
			$dh="";
		}
		//集合
		$inid.=$dh.$userid[$i];
	}
	$sql=$empire->query("update ".$user_tablename." set ".$user_checked."=1 where ".$user_userid." in (".$inid.")");
	if($sql)
	{
		insert_dolog("");//操作日志
		printerror("DoCheckMemberSuccess","ListMember.php");
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

//取得邮件地址
function GetUserEmail($userid,$username){
	global $empire,$user_tablename,$user_email,$user_userid;
	$r=$empire->fetch1("select ".$user_email." from ".$user_tablename." where ".$user_userid."='$userid' limit 1");
	return doUtfAndGbk($r[$user_email],1);
}

//返回修改资料
function ReturnUserInfo($userid){
	global $empire,$user_tablename,$user_userid,$user_username,$user_email,$user_group,$user_userfen,$user_money,$user_userdate,$user_todaydown,$user_zgroup,$user_checked;
	$r=$empire->fetch1("select ".$user_username.",".$user_email.",".$user_group.",".$user_userfen.",".$user_money.",".$user_userdate.",".$user_todaydown.",".$user_zgroup.",".$user_checked." from ".$user_tablename." where ".$user_userid."='$userid' limit 1");
	$re[username]=doUtfAndGbk($r[$user_username],1);
	$re[email]=doUtfAndGbk($r[$user_email],1);
	$re[userfen]=$r[$user_userfen];
	$re[money]=$r[$user_money];
	$re[groupid]=$r[$user_group];
	$re[userdate]=$r[$user_userdate];
	$re[zgroupid]=$r[$user_zgroup];
	$re[todaydown]=$r[$user_todaydown];
	$re[checked]=$r[$user_checked];
	return $re;
}

//返回是否审核
function ReturnGroupChecked($groupid){
	global $level_r;
	if($level_r[$groupid]['regchecked']==1)
	{
		$checked=0;
	}
	else
	{
		$checked=1;
	}
	return $checked;
}

//选择空间模板
function ChangeSpaceStyle($add){
	global $empire,$dbtbpre;
	$user_r=islogin();//是否登陆
	$styleid=intval($add['styleid']);
	if(!$styleid)
	{
		printerror('NotChangeSpaceStyleId','',1);
	}
	$sr=$empire->fetch1("select styleid,membergroup from {$dbtbpre}enewsspacestyle where styleid='$styleid'");
	if(!$sr['styleid']||($sr['membergroup']&&!strstr($sr['membergroup'],','.$user_r[groupid].',')))
	{
		printerror('NotChangeSpaceStyleId','',1);
	}
	$sql=$empire->query("update {$dbtbpre}enewsmemberadd set spacestyleid='$styleid' where userid='$user_r[userid]' limit 1");
	if($sql)
	{
		printerror('ChangeSpaceStyleSuccess','../member/mspace/ChangeStyle.php',1);
	}
	else
	{
		printerror('DbError','',1);
	}
}

//设置空间
function DoSetSpace($add){
	global $empire,$dbtbpre;
	$user_r=islogin();//是否登陆
	$spacename=htmlspecialchars($add['spacename']);
	$spacegg=htmlspecialchars($add['spacegg']);
	$sql=$empire->query("update {$dbtbpre}enewsmemberadd set spacename='$spacename',spacegg='$spacegg' where userid='$user_r[userid]' limit 1");
	if($sql)
	{
		printerror('SetSpaceSuccess','../member/mspace/SetSpace.php',1);
	}
	else
	{
		printerror('DbError','',1);
	}
}

//用户注册
function register($username,$password,$repassword,$email){
	global $empire,$user_tablename,$public_r,$user_groupid,$user_username,$user_userid,$user_email,$user_password,$user_dopass,$user_rnd,$user_registertime,$user_register,$user_group,$user_saltnum,$user_salt,$user_seting,$forumgroupid,$registerurl,$dbtbpre,$user_regcookietime,$user_userfen,$user_checked;
	if($public_r['register_ok'])//关闭
	{
		printerror("CloseRegister","history.go(-1)",1);
	}
	if(!empty($registerurl))
	{
		Header("Location:$registerurl");
		exit();
    }
	$add=$_POST;
	$username=trim($username);
	$password=trim($password);
	$username=RepPostVar($username);
	$password=RepPostVar($password);
	if(!$username||!$password||!$email)
	{
		printerror("EmptyMember","history.go(-1)",1);
	}
	$user_groupid=(int)$user_groupid;
	$groupid=(int)$add[groupid];
	$groupid=empty($groupid)?$user_groupid:$groupid;
	CheckMemberGroupCanReg($groupid);
	//用户字数
	$pr=$empire->fetch1("select min_userlen,max_userlen,min_passlen,max_passlen from {$dbtbpre}enewspublic limit 1");
	$userlen=strlen($username);
	if($userlen<$pr[min_userlen]||$userlen>$pr[max_userlen])
	{
		printerror("FaiUserlen","history.go(-1)",1);
	}
	//密码字数
	$passlen=strlen($password);
	if($passlen<$pr[min_passlen]||$passlen>$pr[max_passlen])
	{
		printerror("FailPasslen","history.go(-1)",1);
	}
	if($repassword!=$password)
	{
		printerror("NotRepassword","history.go(-1)",1);
	}
	if(!chemail($email))
	{
		printerror("EmailFail","history.go(-1)",1);
	}
	if(strstr($username,"|")||strstr($username,"*"))
	{
		printerror("NotSpeWord","history.go(-1)",1);
	}
	$username=RepPostStr($username);
	$num=$empire->gettotal("select count(*) as total from ".$user_tablename." where ".$user_username."='$username' limit 1");
	if($num)
	{
		printerror("ReUsername","history.go(-1)",1);
	}
	//注册时间
	if($user_register)
	{
		$registertime=time();
	}
	else
	{
		$registertime=date("Y-m-d H:i:s");
	}
	$birthday=$y.$m.$d;
	$rnd=make_password(12);//产生随机密码
	//密码
	if(empty($user_dopass))//单重md5
	{
		$password=md5($password);
	}
	elseif($user_dopass==2)//双重md5
	{
		$salt=make_password($user_saltnum);
		$password=md5(md5($password).$salt);
	}
	elseif($user_dopass==3)//16位md5
	{
		$password=substr(md5($password),8,16);
	}
	$email=RepPostStr($email);
	$checked=ReturnGroupChecked($groupid);
	$sql=$empire->query("insert into ".$user_tablename."(".$user_username.",".$user_password.",".$user_email.",".$user_registertime.",".$user_group.",".$user_rnd.",".$user_userfen.",".$user_checked.") values('$username','$password','$email','$registertime',$groupid,'$rnd','$public_r[reggetfen]',$checked);");
	//取得userid
	$userid=$empire->lastid();
	//附加表
	$addr=$empire->fetch1("select * from {$dbtbpre}enewsmemberadd where userid='$userid'");
	if(!$addr[userid])
	{
		$fid=GetMemberFormId($groupid);
		$member_r=ReturnDoMemberF($fid,$add,$mr,0);
		$sql1=$empire->query("insert into {$dbtbpre}enewsmemberadd(userid".$member_r[0].") values($userid".$member_r[1].");");
	}
	if($sql)
	{
		$logincookie=0;
		if($user_regcookietime)
		{
			$logincookie=time()+$user_regcookietime;
		}
		$set1=esetcookie("mlusername",$username,$logincookie);
		$set2=esetcookie("mluserid",$userid,$logincookie);
		$set3=esetcookie("mlgroupid",$groupid,$logincookie);
		$set4=esetcookie("mlrnd",$rnd,$logincookie);
		$location="../../";
		$returnurl=getcvar('returnurl');
		if($returnurl&&!strstr($returnurl,"e/member/iframe"))
		{
			$location=$returnurl;
		}
		$set5=esetcookie("returnurl","");
		printerror("RegisterSuccess",$location,1);
	}
	else
	{printerror("DbError","history.go(-1)",1);}
}

//信息修改
function EditInfo($password,$repassword,$oldpassword,$email){
	global $empire,$user_tablename,$public_r,$user_userid,$user_username,$user_password,$user_dopass,$user_email,$user_salt,$user_saltnum,$dbtbpre,$user_group;
	$user_r=islogin();//是否登陆
	$userid=$user_r[userid];
	$username=$user_r[username];
	$rnd=$user_r[rnd];
	$groupid=$user_r[groupid];
	if(!$userid||!$username||!trim($email))
	{
		printerror("NotEmpty","history.go(-1)",1);
	}
	if(!chemail($email))
	{
		printerror("EmailFail","history.go(-1)",1);
	}
	$email=RepPostStr($email);
	$post=$_POST;
	//编码转换
	$username=doUtfAndGbk($username,0);
	$email=doUtfAndGbk($email,0);
	//修改密码
	if($password)
	{
		if($password!=$repassword)
		{
			printerror("NotRepassword","history.go(-1)",1);
		}
		$password=RepPostVar($password);
		$oldpassword=RepPostVar($oldpassword);
		$password=doUtfAndGbk($password,0);
		$oldpassword=doUtfAndGbk($oldpassword,0);
		if(empty($user_dopass))//单重加密
		{
			$password=md5($password);
			$oldpassword=md5($oldpassword);
	    }
		elseif($user_dopass==2)//双重加密
		{
			$salt=make_password($user_saltnum);
			$password=md5(md5($password).$salt);
			$sa=",".$user_salt."='$salt'";
		}
		elseif($user_dopass==3)//16位md5
		{
			$password=substr(md5($password),8,16);
			$oldpassword=substr(md5($oldpassword),8,16);
		}
		else
		{}
	    $num=0;
		//双重md5
		if($user_dopass==2)
		{
			$ur=$empire->fetch1("select ".$user_userid.",".$user_salt.",".$user_password." from ".$user_tablename." where ".$user_username."='$username'");
			$oldpassword=md5(md5($oldpassword).$ur[$user_salt]);
			$num=0;
			if($oldpassword==$ur[$user_password])
			{$num=1;}
			if(empty($ur[$user_userid]))
			{$num=0;}
		}
		else
		{
			$num=$empire->gettotal("select count(*) as total from ".$user_tablename." where ".$user_username."='$username' and ".$user_password."='".$oldpassword."'");
		}
		if(!$num)
		{
			printerror("FailOldPassword","history.go(-1)",1);
		}
		$add=",".$user_password."='".$password."'".$sa;
	}
    $sql=$empire->query("update ".$user_tablename." set ".$user_email."='$email'".$add." where ".$user_userid."='$userid'");
	$user_r=$empire->fetch1("select ".$user_group." from ".$user_tablename." where ".$user_userid."='$userid'");
	//附加表
	$addr=$empire->fetch1("select * from {$dbtbpre}enewsmemberadd where userid='$userid'");
	$fid=GetMemberFormId($user_r[$user_group]);
	if(empty($addr[userid]))
	{
		$member_r=ReturnDoMemberF($fid,$post,$mr,0);
		$sql1=$empire->query("insert into {$dbtbpre}enewsmemberadd(userid".$member_r[0].") values($userid".$member_r[1].");");
    }
	else
	{
		$member_r=ReturnDoMemberF($fid,$post,$addr,1);
		$sql1=$empire->query("update {$dbtbpre}enewsmemberadd set userid='$userid'".$member_r[0]." where userid='$userid'");
    }
    if($sql)
    {
		printerror("EditInfoSuccess","../member/EditInfo",1);
	}
    else
    {printerror("DbError","history.go(-1)",1);}
}

//----------------------------------是否登陆
function islogin($uid=0,$uname='',$urnd=''){
	global $empire,$public_r,$editor,$user_tablename,$user_userid,$user_username,$user_email,$user_userfen,$user_money,$user_group,$user_groupid,$user_rnd,$user_zgroup,$user_userdate,$user_todaydown,$user_havemsg,$user_todaydate,$ecmsreurl,$eloginurl,$user_checked;
	if($uid)
	{$userid=(int)$uid;}
	else
	{$userid=(int)getcvar('mluserid');}
	if($urnd)
	{$rnd=$urnd;}
	else
	{$rnd=getcvar('mlrnd');}
	if($eloginurl)
	{$gotourl=$eloginurl;}
	else
	{$gotourl=$public_r['newsurl']."e/member/login";}
	$petype=1;
	if(!$userid)
	{
		if(!getcvar('returnurl'))
		{
			esetcookie("returnurl",$_SERVER['HTTP_REFERER'],0);
		}
		if($ecmsreurl==1)
		{
			$gotourl="history.go(-1)";
			$petype=9;
		}
		printerror("NotLogin",$gotourl,$petype);
	}
	$rnd=RepPostVar($rnd);
	$cr=$empire->fetch1("select ".$user_userid.",".$user_username.",".$user_email.",".$user_group.",".$user_userfen.",".$user_money.",".$user_userdate.",".$user_todaydown.",".$user_zgroup.",".$user_havemsg.",".$user_todaydate.",".$user_checked." from ".$user_tablename." where ".$user_userid."='$userid' and ".$user_rnd."='$rnd' limit 1");
	if(!$cr[$user_userid])
	{
		EmptyEcmsCookie();
		if(!getcvar('returnurl'))
		{
			esetcookie("returnurl",$_SERVER['HTTP_REFERER'],0);
		}
		if($ecmsreurl==1)
		{
			$gotourl="history.go(-1)";
			$petype=9;
		}
		printerror("NotSingleLogin",$gotourl,$petype);
	}
	if($cr[$user_checked]==0)
	{
		EmptyEcmsCookie();
		if($ecmsreurl==1)
		{
			$gotourl="history.go(-1)";
			$petype=9;
		}
		printerror("NotCheckedUser",'',$petype);
	}
	//默认会员组
	if(empty($cr[$user_group]))
	{
		$usql=$empire->query("update ".$user_tablename." set ".$user_group."='$user_groupid' where ".$user_userid."='".$cr[$user_userid]."'");
		$cr[$user_group]=$user_groupid;
	}
	//是否过期
	if($cr[$user_userdate])
	{
		if($cr[$user_userdate]-time()<=0)
		{
			OutTimeZGroup($cr[$user_userid],$cr[$user_zgroup]);
			$cr[$user_userdate]=0;
			if($cr[$user_zgroup])
			{
				$cr[$user_group]=$cr[$user_zgroup];
				$cr[$user_zgroup]=0;
			}
		}
	}
	$re[userid]=$cr[$user_userid];
	$re[rnd]=$rnd;
	$re[username]=doUtfAndGbk($cr[$user_username],1);
	$re[email]=doUtfAndGbk($cr[$user_email],1);
	$re[userfen]=$cr[$user_userfen];
	$re[money]=$cr[$user_money];
	$re[groupid]=$cr[$user_group];
	$re[userdate]=$cr[$user_userdate];
	$re[zgroupid]=$cr[$user_zgroup];
	$re[todaydown]=$cr[$user_todaydown];
	$re[todaydate]=$cr[$user_todaydate];
	$re[havemsg]=$cr[$user_havemsg];
	/*
	if($cr[$user_havemsg])
	{
		echo"<script>window.status='您有新的悄悄话,请注意查收!';</script>";
	}
	*/
	return $re;
}

//-------------------------------------退出登陆
function loginout1($userid,$username,$rnd){
	global $empire,$public_r,$equiturl;
	//是否登陆
	$user_r=islogin();
	if($equiturl)
	{
		Header("Location:$equiturl");
		exit();
	}
	EmptyEcmsCookie();
	$dopr=1;
	if($_GET['prtype'])
	{
		$dopr=9;
	}
	$gotourl="../../";
	if(strstr($_SERVER['HTTP_REFERER'],"e/member/iframe"))
	{
		$gotourl=$public_r['newsurl']."e/member/iframe";
	}
	printerror("ExitSuccess",$gotourl,$dopr);
}

//-----------------------------------清空COOKIE
function EmptyEcmsCookie(){
	$set1=esetcookie("mlusername","",0);
	$set2=esetcookie("mluserid","",0);
	$set3=esetcookie("mlgroupid","",0);
	$set4=esetcookie("mlrnd","",0);
}

//----------------------------------------登陆
function login1($username,$password,$lifetime,$key,$location){
	global $empire,$user_tablename,$user_userid,$user_username,$user_password,$user_dopass,$user_group,$user_groupid,$user_rnd,$public_r,$user_salt,$user_saltnum,$dbtbpre,$eloginurl,$user_checked;
	if($eloginurl)
	{
		Header("Location:$eloginurl");
		exit();
	}
	$dopr=1;
	if($_POST['prtype'])
	{
		$dopr=9;
	}
	if(!trim($username)||!trim($password))
	{printerror("EmptyLogin","history.go(-1)",$dopr);}
	//验证码
	if($public_r['loginkey_ok'])
	{
		$checkplkey=getcvar('checkplkey');
		if($key<>$checkplkey||empty($checkplkey))
		{printerror("FailKey","history.go(-1)",$dopr);}
	}
	$username=RepPostVar($username);
	$password=RepPostVar($password);
	//编码转换
	$utfusername=doUtfAndGbk($username,0);
	$password=doUtfAndGbk($password,0);
	//密码
	if(empty($user_dopass))//单重md5
	{
		$password=md5($password);
	}
	if($user_dopass==3)//16位md5
	{
		$password=substr(md5($password),8,16);
	}
	//双重md5
	$num=0;
	if($user_dopass==2)
	{
	    $ur=$empire->fetch1("select ".$user_userid.",".$user_salt.",".$user_password." from ".$user_tablename." where ".$user_username."='$utfusername'");
		$password=md5(md5($password).$ur[$user_salt]);
		$num=0;
		if($password==$ur[$user_password])
		{$num=1;}
		if(empty($ur[$user_userid]))
		{$num=0;}
    }
	else
	{
		$num=$empire->gettotal("select count(*) as total from ".$user_tablename." where ".$user_username."='$utfusername' and ".$user_password."='".$password."' limit 1");
	}
	if(!$num)
	{
		printerror("FailPassword","history.go(-1)",$dopr);
	}
	$r=$empire->fetch1("select * from ".$user_tablename." where ".$user_username."='$utfusername' limit 1");
	if($r[$user_checked]==0)
	{
		printerror("NotCheckedUser",'',1);
	}
	$time=date("Y-m-d H:i:s");
	$rnd=make_password(12);//取得随机密码
	//默认会员组
	if(empty($r[$user_group]))
	{$r[$user_group]=$user_groupid;}
	$r[$user_group]=(int)$r[$user_group];
	$usql=$empire->query("update ".$user_tablename." set ".$user_rnd."='$rnd',".$user_group."=".$r[$user_group]." where ".$user_userid."='$r[$user_userid]'");
	//设置cookie
	$logincookie=0;
	if($lifetime)
	{
		$logincookie=time()+$lifetime;
	}
	$set1=esetcookie("mlusername",$username,$logincookie);
	$set2=esetcookie("mluserid",$r[$user_userid],$logincookie);
	$set3=esetcookie("mlgroupid",$r[$user_group],$logincookie);
	$set4=esetcookie("mlrnd",$rnd,$logincookie);
	//登录附加cookie
	AddLoginCookie($r);
	$location="../../";
	$returnurl=getcvar('returnurl');
	if($returnurl)
	{
		$location=$returnurl;
	}
	if(strstr($_SERVER['HTTP_REFERER'],"e/member/iframe"))
	{$location="../member/iframe";}
	$set5=esetcookie("checkplkey","");
	$set6=esetcookie("returnurl","");
	if($set1&&$set2)
	{
		$location=DoingReturnUrl($location,$_POST['ecmsfrom']);
		printerror("LoginSuccess",$location,$dopr);
    }
	else
	{
		printerror("NotCookie","history.go(-1)",$dopr);
	}
}

//----------------------------------批量赠送点数
function GetFen_all($cardfen,$userid,$username){
	global $empire,$user_tablename,$user_userfen;
	$cardfen=(int)$cardfen;
	if(!$cardfen)
	{printerror("EmptyGetFen","history.go(-1)");}
	//验证权限
	CheckLevel($userid,$username,$classid,"card");
	$sql=$empire->query("update ".$user_tablename." set ".$user_userfen."=".$user_userfen."+$cardfen");
	if($sql)
	{
		//操作日志
		insert_dolog("cardfen=$cardfen");
		printerror("GetFenSuccess","GetFen.php");
	}
	else
	{printerror("DbError","history.go(-1)");}
}

//----------------------------------增加点数
function AddInfoFen($cardfen,$userid){
	global $empire,$user_tablename,$user_userfen,$user_userid;
	$cardfen=(int)$cardfen;
	$sql=$empire->query("update ".$user_tablename." set ".$user_userfen."=".$user_userfen."+".$cardfen." where ".$user_userid."='$userid'");
}

//转向会员组
function OutTimeZGroup($userid,$zgroupid){
	global $empire,$user_tablename,$user_group,$user_zgroup,$user_userdate,$user_userid;
	if($zgroupid)
	{
		$sql=$empire->query("update ".$user_tablename." set ".$user_group."='".$zgroupid."',".$user_userdate."=0 where ".$user_userid."='$userid'");
	}
	else
	{
		$sql=$empire->query("update ".$user_tablename." set ".$user_userdate."=0 where ".$user_userid."='$userid'");
	}
}

//处理注册字段值
function DoMemberFValue($val){
	$val=htmlspecialchars($val);
	return $val;
}

//删除会员字段附件
function DelYMemberTranFile($file,$tf){
	global $empire,$dbtbpre;
	if(empty($file)){
		return "";
	}
	$r=explode("/",$file);
	$count=count($r);
	$filename=$r[$count-1];
	$fr=$empire->fetch1("select filename,path,fileid,fpath,classid from {$dbtbpre}enewsfile where no='Member[".$tf."]' and filename='$filename' limit 1");
	if($fr['fileid'])
	{
		$sql=$empire->query("delete from {$dbtbpre}enewsfile where fileid='$fr[fileid]'");
		DoDelFile($fr);
	}
}

//返回会员字段
function ReturnDoMemberF($fid,$add,$mr,$ecms=0){
	global $empire,$dbtbpre,$tranpicturetype,$public_r;
	$pr=$empire->fetch1("select openmembertranimg,memberimgsize,memberimgtype,openmembertranfile,memberfilesize,memberfiletype from {$dbtbpre}enewspublic limit 1");
	$formr=$empire->fetch1("select fid,enter,mustenter,filef,imgf from {$dbtbpre}enewsmemberform where fid='$fid'");
	//检测必填字段
	$mustr=explode(",",$formr['mustenter']);
	$mustcount=count($mustr);
	for($i=1;$i<$mustcount-1;$i++)
	{
		$mf=$mustr[$i];
		if(strstr($formr['filef'],",".$mf.",")||strstr($formr['imgf'],",".$mf.","))//附件
		{
			$mfilef=$mf."file";
			//上传文件
			if($_FILES[$mfilef]['name'])
			{
				if(strstr($formr['imgf'],",".$mf.","))//图片
				{
					if(!$pr['openmembertranimg'])
					{
						printerror("CloseQTranPic","",1);
					}
				}
				else//附件
				{
					if(!$pr['openmembertranfile'])
					{
						printerror("CloseQTranFile","",1);
					}
				}
			}
			elseif(!trim($add[$mf])&&!$mr[$mf])
			{
				printerror("EmptyQMustF","",1);
			}
		}
		else
		{
			if(!trim($add[$mf]))
			{
				printerror("EmptyQMustF","",1);
			}
		}
	}
	//字段处理
	$dh="";
	$tranf="";
	$record="<!--record-->";
	$field="<!--field--->";
	$fr=explode($record,$formr['enter']);
	$count=count($fr);
	for($i=0;$i<$count-1;$i++)
	{
		$fr1=explode($field,$fr[$i]);
		$f=$fr1[1];
		//附件
		if(strstr($formr['filef'],",".$f.",")||strstr($formr['imgf'],",".$f.","))
		{
			//上传附件
			$filetf=$f."file";
			if($_FILES[$filetf]['name'])
			{
				$filetype=GetFiletype($_FILES[$filetf]['name']);//取得文件类型
				if($filetype==".php")
				{
					printerror("NotQTranFiletype","",1);
				}
				if(strstr($formr['imgf'],",".$f.","))//图片
				{
					if(!$pr['openmembertranimg'])
					{
						printerror("CloseQTranPic","",1);
					}
					if(!strstr($pr['memberimgtype'],"|".$filetype."|"))
					{
						printerror("NotQTranFiletype","",1);
					}
					if($_FILES[$filetf]['size']>$pr['memberimgsize']*1024)
					{
						printerror("TooBigQTranFile","",1);
					}
					if(!strstr($tranpicturetype,','.$filetype.','))
					{
						printerror("NotQTranFiletype","",1);
					}
				}
				else//附件
				{
					if(!$pr['openmembertranfile'])
					{
						printerror("CloseQTranFile","",1);
					}
					if(!strstr($pr['memberfiletype'],"|".$filetype."|"))
					{
						printerror("NotQTranFiletype","",1);
					}
					if($_FILES[$filetf]['size']>$pr['memberfilesize']*1024)
					{
						printerror("TooBigQTranFile","",1);
					}
				}
				$tranf.=$dh.$f;
				$dh=",";
				$fval="[!#@-".$f."-@!]";
			}
			else
			{
				$fval=$add[$f];
				if($ecms==1&&$mr[$f]&&!trim($fval))
				{
					$fval=$mr[$f];
				}
			}
		}
		else
		{
			$fval=$add[$f];
		}
		$fval=DoMemberFValue($fval);
		$fval=addslashes($fval);
		if($ecms==0)//添加
		{
			$ret_r[0].=",`".$f."`";
			$ret_r[1].=",'".$fval."'";
		}
		else//编辑
		{
			$ret_r[0].=",`".$f."`='".$fval."'";
		}
	}
	//上传附件
	if($tranf)
	{
		$infoid=0;
		$filepass=0;
		$classid=0;
		$tranr=explode(",",$tranf);
		$count=count($tranr);
		for($i=0;$i<$count;$i++)
		{
			$tf=$tranr[$i];
			$tffile=$tf."file";
			$tfr=DoTranFile($_FILES[$tffile]['tmp_name'],$_FILES[$tffile]['name'],$_FILES[$tffile]['type'],$_FILES[$tffile]['size'],$classid);
			if($tfr['tran'])
			{
				if(strstr($formr['imgf'],",".$tf.","))//图片
				{
					$type=1;
				}
				else//附件
				{
					$type=0;
				}
				//写入数据库
				$filetime=date("Y-m-d H:i:s");
				$filesize=(int)$_FILES[$tffile]['size'];
				$sql=$empire->query("insert into {$dbtbpre}enewsfile(filename,filesize,adduser,path,filetime,classid,no,type,id,cjid,fpath) values('$tfr[filename]',$filesize,'[EditInfo]','$tfr[filepath]','$filetime',$classid,'Member[".$tf."]',$type,$infoid,$filepass,'$public_r[fpath]');");
				//删除旧文件
				if($ecms==1&&$mr[$tf])
				{
					DelYMemberTranFile($mr[$tf],$tf);
				}
				$repfval=$tfr['url'];
			}
			else
			{
				$repfval=$mr[$tf];
			}
			if($ecms==0)//添加
			{
				$ret_r[1]=str_replace("[!#@-".$tf."-@!]",$repfval,$ret_r[1]);
			}
			else//编辑
			{
				$ret_r[0]=str_replace("[!#@-".$tf."-@!]",$repfval,$ret_r[0]);
			}
		}
	}
	return $ret_r;
}

//删除会员附件
function DoDelMemberF($fid,$userid,$username){
	global $empire,$dbtbpre;
	$r=$empire->fetch1("select * from {$dbtbpre}enewsmemberadd where userid='$add[userid]'");
	$sql=$empire->query("delete from {$dbtbpre}enewsmemberadd where userid='$add[userid]'");
	if(empty($r[userid]))
	{
		return '';
	}
	$formr=$empire->query("select fid,filef,imgf from {$dbtbpre}enewsmemberform where fid='$fid'");
	if(empty($formr['filef']))
	{
		$formr['filef']=',';
	}
	if(empty($formr['imgf']))
	{
		$formr['imgf']=',';
	}
	$fields=substr($formr['filef'],0,strlen($formr['filef'])-1).$formr['imgf'];
	$fr=explode(',',$fields);
	$count=count($fr);
	for($i=1;$i<$count-1;$i++)
	{
		$f=$fr[$i];
		if($r[$f])
		{
			DelYMemberTranFile($r[$f],$f);
		}
	}
}

if($utfdata&&!function_exists("iconv"))
{
	@include_once("doiconv.php");
}
?>