<!-- #include file="Conn.asp" -->
<!-- #include file="inc/const.asp" -->
<!-- #include file="inc/chkinput.asp" -->
<!-- #include file="inc/UPIConfig.asp" -->
<!-- #include file="inc/md5.asp" -->
<%
'完整调用形式：
'<script src="Interface.asp?action=操作类型&name=用户名&Md5Info=验证串&pass=密码&lock=是否锁定&question=提示问题&answer=答案&email=电子邮件&sex=性别&save=Cookies保存选项"
'action、name、Md5Info是必填参数
'删除用户时不需要其它参数
'更新资料时，提供所要更新的信息即可
'注意：这里传递的用户密码为32位MD5加密
Dim Action,UpiUserName,UpiUserPass,UpiMd5Info,UpiQuestion,UpiAnswer,UpiEmail,UpiUserLock,UpiUserSex,UpiSaveCookies
Dim PEMd5Info
Action = Trim(Request("action"))
UpiUserName = Dvbbs.CheckStr(Trim(Request("name")))
UpiUserPass = Trim(Request("pass"))
If Len(UpiUserPass) = 32 Then UpiUserPass = Mid(UpiUserPass,9,16)
UpiUserPass = Dvbbs.CheckStr(UpiUserPass)
UpiMd5Info = UCase(Trim(Request("Md5Info")))
UpiQuestion = Dvbbs.CheckStr(Trim(Request("question")))
UpiAnswer = Dvbbs.CheckStr(Trim(Request("answer")))
If Len(UpiAnswer) = 32 Then UpiAnswer = Mid(UpiAnswer,9,16)
UpiEmail = Dvbbs.CheckStr(Trim(Request("email")))
UpiUserSex = Trim(Request("sex"))
UpiUserLock = Trim(Request("lock"))
UpiSaveCookies = Trim(Request("save"))

If Not (Action = "" or UpiUserName = "" or UpiMd5Info = "") Then
	PEMd5Info = MD5(UpiUserName&SecurityKey,32)
	If UpiMd5Info = UCase(PEMd5Info) Then
		Select Case Action
		Case "AddUser"
			Call AddUser()
		Case "LoginUser"
			Call LoginUser()
		Case "LogoutUser"
			Call LogoutUser()
		Case "ModifyInfo"
			Call ModifyUserInfo()
		Case "DelUser"
			Call DelUser()
		End Select
	End If
End If

Sub AddUser()
	Dvbbs.Stats = "用户注册"
	On Error Resume Next
	If UpiQuestion = "" Then UpiQuestion = "答案为您的密码"
	If UpiAnswer = "" Then UpiAnswer = UpiUserPass
	If UpiEmail = "" Then UpiEmail = UpiUserName & "@PowerEasy.cn"
	If UpiUserSex = "" Then UpiUserSex = 0
	If Not IsNumeric(UpiUserSex) Then UpiUserSex = 0
	If UpiUserSex < 0 or UpiUserSex > 1 Then UpiUserSex = 0
	If UpiUserLock = "" Then UpiUserLock = 0
	If Not IsNumeric(UpiUserLock) Then UpiUserLock = 0
	Dim UpiUserExist
	UpiUserExist = True
	
	Dvbbs.LoadTemplates("login")
	'通行注册要产生的字段
	Dim RegUserFace,RegTitlePic,RegClassName
	'随机产生用户头像
	Dim ForumAllFace,FaceTotalNum,RegUserFaceNum
	ForumAllFace = Split(Dvbbs.Forum_userface,"|||")
	FaceTotalNum = Ubound(ForumAllFace)-1
	Randomize
	RegUserFaceNum = Int(Rnd * FaceTotalNum)
	RegUserFace = ForumAllFace(0)&ForumAllFace(RegUserFaceNum)
	'产生随机密码
	Dim TruePassword
	TruePassword = Dvbbs.Createpass
	'判断用户所属用户组
	Dim TempRs
	Set TempRs = Dvbbs.Execute("Select UserTitle,GroupPic,UserGroupID,IsSetting,ParentGID From Dv_UserGroups Where ParentGID=3 Order By MinArticle")
	RegTitlePic=TempRs(1)
	RegClassName=TempRs(0)
	Dvbbs.UserGroupID = TempRs(2)
	TempRs.Close
	Set TempRs = Nothing
	
	Set TempRs = Server.CreateObject("Adodb.Recordset")
	TempRs.Open "SELECT * FROM Dv_User WHERE UserName='" & UpiUserName & "'",Conn,1,3
	If TempRs.Eof and TempRs.Bof Then
		UpiUserExist = False
		'插入新记录
		TempRs.addnew
		TempRs("UserName")=UpiUserName
		TempRs("UserPassword")=UpiUserPass
		TempRs("UserEmail")=UpiEmail
		TempRs("Userclass")=RegClassName
		TempRs("TitlePic")=RegTitlePic
		TempRs("UserQuesion")=UpiQuestion
		TempRs("UserAnswer")=UpiAnswer
		TempRs("TruePassWord")=TruePassword
		TempRs("UserIM")="||||||||||||||||||"
		TempRs("UserPost")=0
		TempRs("Usersex")=UpiUserSex
		TempRs("Lockuser")=0
		If UpiUserLock = 1 Then
			TempRs("UserGroupID")=5
		Else
			TempRs("UserGroupID")=Dvbbs.UserGroupID
		End If
		TempRs("JoinDate")=NOW()
		TempRs("UserFace")=RegUserFace
		TempRs("UserWidth")=32
		TempRs("UserHeight")=32
		TempRs("UserLogins")=1
		TempRs("LastLogin")=NOW()
		TempRs("userWealth")=dvbbs.Forum_user(0)
		TempRs("userEP")=dvbbs.Forum_user(5)
		TempRs("usercP")=dvbbs.Forum_user(10)
		TempRs("UserInfo")="||||||||||||||||||||||||||||||||||||||||||"
		TempRs("Usersetting")="||||||1"
		TempRs("UserPower")=0
		TempRs("UserDel")=0
		TempRs("UserIsbest")=0
		TempRs("UserFav")="陌生人,我的好友,黑名单"
		TempRs("IsChallenge")=0
		TempRs("UserHidden")=0
		TempRs("UserLastIP")=Request.ServerVariables("REMOTE_ADDR")
		TempRs.update
	End If
	TempRs.close
	
	If Not UpiUserExist Then
		Dvbbs.execute("UpDate Dv_Setup Set Forum_UserNum=Forum_UserNum+1,Forum_lastUser='"&Dvbbs.HtmlEncode(UpiUserName)&"'")
		Dvbbs.ReloadSetupCache UpiUserName,14
		Dvbbs.ReloadSetupCache (CLng(Dvbbs.CacheData(10,0))+1),10 
		Set TempRs=Dvbbs.execute("select top 1 userid from [Dv_user] order by userid desc")
			Dvbbs.userid=rs(0)
		TempRs.close
		Set TempRs=nothing
	
	
		If cint(Dvbbs.Forum_Setting(23))=1 or cint(Dvbbs.Forum_Setting(25))=1 Then
	
		Else
			Response.Cookies(Dvbbs.Forum_sn).path=Dvbbs.cookiepath
			Response.Cookies(Dvbbs.Forum_sn)("username")=""
			Response.Cookies(Dvbbs.Forum_sn)("password")=""
			Response.Cookies(Dvbbs.Forum_sn)("userclass")=""
			Response.Cookies(Dvbbs.Forum_sn)("userid")=""
			Response.Cookies(Dvbbs.Forum_sn)("userhidden")=""
			Response.Cookies(Dvbbs.Forum_sn)("usercookies")=""
			Dim StatUserID,UserSessionID
			StatUserID = Dvbbs.checkStr(Trim(Request.Cookies(Dvbbs.Forum_sn)("StatUserID")))
			If IsNumeric(StatUserID) = 0 or StatUserID = "" Then
				StatUserID = Replace(Dvbbs.UserTrueIP,".","")
				UserSessionID = Replace(Startime,".","")
				If IsNumeric(StatUserID) = 0 or StatUserID = "" Then StatUserID = 0
				StatUserID = Ccur(StatUserID) + Ccur(UserSessionID)
			End If
			StatUserID = Ccur(StatUserID)
			Dvbbs.Execute("Delete From dv_online where username='"&dvbbs.membername&"' Or id="&StatUserID&"")
			'客人=SessionID+活动时间+发贴时间+版面ID
			Session(Dvbbs.CacheName & "UserID") = Split(StatUserID & "_" & Now & "_" & Now & "_" & Dvbbs.BoardID,"_")
			Response.Cookies(Dvbbs.Forum_sn)("StatUserID") = StatUserID
			Response.Cookies(Dvbbs.Forum_sn)("usercookies") = 0
			Response.Cookies(Dvbbs.Forum_sn)("username") = UpiUserName
			Response.Cookies(Dvbbs.Forum_sn)("password") = TruePassWord
			Response.Cookies(Dvbbs.Forum_sn)("userclass") = RegClassName
			Response.Cookies(Dvbbs.Forum_sn)("userid") = Dvbbs.userid
			Response.Cookies(Dvbbs.Forum_sn)("userhidden") = 2
			Response.Cookies(Dvbbs.Forum_sn).path=Dvbbs.cookiepath
			Dvbbs.membername=UpiUserName
			Dvbbs.userhidden=2
			Dvbbs.MemberClass=RegClassName
		End If
		session("regtime")=now()
		Call LoginUser()
	End If
End Sub '注册新用户

Sub LoginUser()
	Dvbbs.stats = "登陆页面"
	'On Error Resume Next
	Dim TempRs,TempSQL,iMyUserInfo
	Dim Article,UserClass,TitlePic
	Dim UserHidden,LastIP,UserLastLogin
	Dim GroupID,ClassSql,FoundGrade
	Dim GroupID_Q
	
	If UpiSaveCookies = "" Then UpiSaveCookies=0
	
	'产生随机密码
	Dim TruePassword
	TruePassword = Dvbbs.Createpass
	
	UserHidden = 2
	
	'判断更新cookies目录
	Dim cookies_path_s,cookies_path_d,cookies_path,i
	cookies_path_s=split(Request.ServerVariables("PATH_INFO"),"/")
	cookies_path_d=ubound(cookies_path_s)
	cookies_path="/"
	For i=1 to cookies_path_d-1
		If not (cookies_path_s(i)="upload" or cookies_path_s(i)="admin") Then cookies_path=cookies_path&cookies_path_s(i)&"/"
	Next
	If dvbbs.cookiepath<>cookies_path Then
		cookies_path=replace(cookies_path,"'","")
		Dvbbs.execute("update dv_setup set Forum_Cookiespath='"&cookies_path&"'")
		Dim setupData 
		Dvbbs.CacheData(26,0)=cookies_path
		Dvbbs.Name="setup"
		Dvbbs.value=Dvbbs.CacheData
	End If
	
	TempSQL = "Select UserID,UserName,UserPassword,UserEmail,UserPost,UserTopic,UserSex,UserFace,UserWidth,UserHeight,JoinDate,LastLogin,UserLogins,Lockuser,Userclass,UserGroupID,UserGroup,userWealth,userEP,userCP,UserPower,UserBirthday,UserLastIP,UserDel,UserIsBest,UserHidden,UserMsg,IsChallenge,UserMobile,TitlePic,UserTitle,TruePassWord,UserToday,UserMoney,UserTicket,FollowMsgID,Vip_StarTime,Vip_EndTime"
	TempSQL = TempSQL & " From [Dv_User] Where UserName='" & UpiUserName & "'"
	Set TempRs = Dvbbs.Execute(TempSQL)
	If TempRs.eof and TempRs.bof Then
		TempRs.Close:Set TempRs = Nothing
		Exit Sub
	Else
		iMyUserInfo=TempRs.GetString(,1, "|||", "", "")
		TempRs.Close:Set TempRs = Nothing
	End If
	iMyUserInfo = "Dvbbs|||"& Now & "|||" & Now &"|||"& Dvbbs.BoardID &"|||"& iMyUserInfo &"|||"&Dvbbs.FoundUserPermission_All()&"|||0||||||Dvbbs"
	iMyUserInfo = Split(iMyUserInfo,"|||")
	If iMyUserInfo(17)=1 or iMyUserInfo(19)=5 Then
		Exit Sub
	Else
		Session(Dvbbs.CacheName & "UserID") = iMyUserInfo
		Dvbbs.UserID = iMyUserInfo(4)
		UpiUserName = iMyUserInfo(5)
		LastIP = Dvbbs.UserTrueIP
		Article = iMyUserInfo(8)
		UserLastLogin = iMyUserInfo(15)
		UserClass = iMyUserInfo(18)			
		GroupID = iMyUserInfo(19)
		TitlePic = iMyUserInfo(34)
		If Article<0 Then Article=0
	End If

	'自动更新用户数据
	set TempRs=Dvbbs.Execute("Select MinArticle,IsSetting,ParentGID,UserTitle,GroupPic From Dv_UserGroups Where UserGroupID="&GroupID)
	If Not (TempRs.eof and TempRs.bof) Then
		If TempRs(2)=1 Or TempRs(2)=2 Or TempRs(2)=4 Or TempRs(2)=5 Then
			'用户等级不按照文章升级，用户为系统或特殊或多属性组
			UserClass=TempRs(3)
			TitlePic=TempRs(4)
			FoundGrade=True
		End If
	End If
	If Not FoundGrade Then
		'如果不属于系统或特殊或多属性组,则将该用户属于注册用户组且按照其文章数自动更新其用户组(等级)
		Set TempRs=Dvbbs.Execute("Select Top 1 usertitle,GroupPic,UserGroupID From Dv_UserGroups Where ParentGID=3 And Minarticle<="&Article&" Order By MinArticle Desc,UserGroupID")
		If Not (TempRs.Eof And TempRs.Bof) Then
			UserClass=TempRs(0)
			TitlePic=TempRs(1)
			GroupID=TempRs(2)
			FoundGrade=True
		End If
	End If
	Set TempRs = Nothing
	If Datediff("d",UserLastLogin,Now())=0 Then
		TempSQL="update [Dv_User] set LastLogin="&SqlNowString&",UserLogins=UserLogins+1,UserLastIP='"&lastip&"',userclass='"&userclass&"',titlepic='"&titlepic&"',UserGroupID="&GroupID&",TruePassWord='"&TruePassWord&"' where userid="&dvbbs.UserID
	Else
		TempSQL="update [Dv_User] set userWealth=userWealth+"&Dvbbs.Forum_user(4)&",userEP=userEP+"&Dvbbs.Forum_user(9)&",userCP=userCP+"&Dvbbs.Forum_user(14)&",LastLogin="&SqlNowString&",UserLogins=UserLogins+1,UserLastIP='"&lastip&"',userclass='"&userclass&"',titlepic='"&titlepic&"',UserGroupID="&GroupID&",TruePassWord='"&TruePassWord&"' where userid="&dvbbs.UserID
	End If
	Dvbbs.Execute(TempSQL)
	Dim StatUserID,UserSessionID
	StatUserID = Dvbbs.checkStr(Trim(Request.Cookies(Dvbbs.Forum_sn)("StatUserID")))
	If IsNumeric(StatUserID) = 0 or StatUserID = "" Then
		StatUserID = Replace(Dvbbs.UserTrueIP,".","")
		UserSessionID = Replace(Startime,".","")
		If IsNumeric(StatUserID) = 0 or StatUserID = "" Then StatUserID = 0
		StatUserID = Ccur(StatUserID) + Ccur(UserSessionID)
	End If
	StatUserID = Ccur(StatUserID)
	Dvbbs.Execute("delete from dv_online where  id="&StatUserID&"")
	If trim(UpiUserName)<>trim(Dvbbs.Membername) Then
		Response.Cookies(Dvbbs.Forum_sn)("username")=""
		Response.Cookies(Dvbbs.Forum_sn)("password")=""
		Response.Cookies(Dvbbs.Forum_sn)("userclass")=""
		Response.Cookies(Dvbbs.Forum_sn)("userid")=""
		Response.Cookies(Dvbbs.Forum_sn)("userhidden")=""
		Response.Cookies(Dvbbs.Forum_sn)("usercookies")=""
		Dvbbs.Execute("delete from dv_online where username='"&Dvbbs.membername&"'")
	End If
	If isnull(UpiSaveCookies) or UpiSaveCookies="" Then UpiSaveCookies="0"
	Select Case UpiSaveCookies
	Case "0"
		Response.Cookies(Dvbbs.Forum_sn)("usercookies") = UpiSaveCookies
	Case 1
   		Response.Cookies(Dvbbs.Forum_sn).Expires=Date+1
		Response.Cookies(Dvbbs.Forum_sn)("usercookies") = UpiSaveCookies
	Case 2
		Response.Cookies(Dvbbs.Forum_sn).Expires=Date+31
		Response.Cookies(Dvbbs.Forum_sn)("usercookies") = UpiSaveCookies
	Case 3
		Response.Cookies(Dvbbs.Forum_sn).Expires=Date+365
		Response.Cookies(Dvbbs.Forum_sn)("usercookies") = UpiSaveCookies
	End Select
	Response.Cookies(Dvbbs.Forum_sn).path = Dvbbs.cookiepath
	Response.Cookies(Dvbbs.Forum_sn)("username") = UpiUserName
	Response.Cookies(Dvbbs.Forum_sn)("userid") = Dvbbs.UserID
	Response.Cookies(Dvbbs.Forum_sn)("password") = TruePassWord
	Response.Cookies(Dvbbs.Forum_sn)("userclass") = UserClass
	Response.Cookies(Dvbbs.Forum_sn)("userhidden") = UserHidden
	rem 清除图片上传数的限制
	Response.Cookies("upNum")=0
	Dim iUserInfo
	iUserInfo = Session(Dvbbs.CacheName & "UserID")
	iUserInfo(35) = TruePassWord
	Session(Dvbbs.CacheName & "UserID") = iUserInfo
	Dvbbs.membername=UpiUserName
	Dvbbs.userhidden=UserHidden
	Dvbbs.MemberClass=UserClass
End Sub

Sub LogoutUser()
	Response.Cookies(Dvbbs.Forum_sn).path=Dvbbs.cookiepath
	Response.Cookies(Dvbbs.Forum_sn)("username")=""
	Response.Cookies(Dvbbs.Forum_sn)("password")=""
	Response.Cookies(Dvbbs.Forum_sn)("userclass")=""
	Response.Cookies(Dvbbs.Forum_sn)("userid")=""
	Response.Cookies(Dvbbs.Forum_sn)("userhidden")=""
	Response.Cookies(Dvbbs.Forum_sn)("usercookies")=""
	Session(Dvbbs.CacheName & "UserID")=Empty
	Session("flag")=Empty
End Sub

Sub ModifyUserInfo()
	On Error Resume Next
	Dim tempSQL
	tempSQL = "UPDATE Dv_User Set "
	If UpiUserPass <> "" and Len(UpiUserPass) = 16 Then
		tempSQL = tempSQL & "UserPassWord='" & UpiUserPass & "'"
	Else
		tempSQL = tempSQL & "UserPassword=UserPassword"
	End If
	If UpiQuestion <> "" Then
		tempSQL = tempSQL & ",UserQuesion='" & UpiQuestion & "'"
	End If
	If UpiAnswer <> "" and Len(UpiAnswer) = 16 Then
		tempSQL = tempSQL & ",UserAnswer='" & UpiAnswer & "'"
	End If
	If UpiEmail <> "" Then
		tempSQL = tempSQL & ",UserEmail='" & UpiEmail & "'"
	End If
	If UpiUserSex <> "" and IsNumeric(UpiUserSex) Then
		If UpiUserSex <0 or UpiUserSex > 1 Then UpiUserSex = 0
		tempSQL = tempSQL & ",UserSex=" & UpiUserSex
	End If
	If UpiUserLock <> "" and IsNumeric(UpiUserLock) Then
		If UpiUserLock <0 or UpiUserLock > 1 Then UpiUserLock = 0
		tempSQL = tempSQL & ",lockuser=" & UpiUserLock
	End If

	tempSQL = tempSQL & " WHERE UserName='" & UpiUserName & "'"

	Dvbbs.Execute(TempSQL)
	
End Sub

Sub DelUser()
	On Error Resume Next
	Dim AllPostTable
	Dim Trs
	Set Trs=Dvbbs.Execute("select * from [Dv_TableList]")
	AllPostTable=""
	Do While Not TRs.EOF
		If AllPostTable=""  Then 
			AllPostTable=TRs("TableName")
		Else
			AllPostTable=AllPostTable&"|"&TRs("TableName")
		End If
	TRs.MoveNext
	Loop 
	Trs.Close
	AllPostTable=Split(AllPostTable,"|")
	Dvbbs.Execute("DELETE FROM dv_message WHERE incept='" & UpiUserName & "' or sender='" & UpiUserName & "'")
	Dvbbs.Execute("delete from dv_friend where F_username='" & UpiUserName & "'") 
	Dvbbs.Execute("delete from dv_bookmark where username='" & UpiUserName & "'") 
	Dvbbs.Execute("DELETE FROM dv_User WHERE UserName='" & UpiUserName & "'")
	Dvbbs.Execute("DELETE FROM dv_BestTopic WHERE PostUserName='" & UpiUserName & "'")
	Dim iNum
	For iNum = 0 to Ubound(AllPostTable)
		Dvbbs.Execute("DELETE FROM " & AllPostTable(iNum) & " WHERE UserName='" & UpiUserName & "'")
	Next
	Dvbbs.Execute("DELETE FROM dv_Topic WHERE PostUserName='" & UpiUserName & "'")
	Dvbbs.Execute("DELETE FROM dv_Upfile WHERE F_Username='" & UpiUserName & "'")
End Sub
%>