<!--#include file="Conn.asp"-->
<!--#include file="inc/const.asp" -->
<!--#include file="inc/md5.asp" -->
<!--#include file="inc/myadmin.asp" -->
<!--#include file="inc/Upiconfig.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,UpiSaveCookies
Dim HLMd5Info,rs,sql
Dim TrueIP
Dim TempRs,TempSql
Action = Trim(Request("action"))
UpiUserName = Replace(Trim(Request("name")),"'","")
UpiUserPass = Trim(Request("pass"))
If Len(UpiUserPass) = 32 Then UpiUserPass = Mid(UpiUserPass,9,16)
UpiUserPass=UpiUserPass
UpiMd5Info = UCase(Trim(Request("Md5Info")))
UpiSaveCookies = Trim(Request("save"))
If UpiSaveCookies="" Then UpiSaveCookies=0
TrueIP = Trim(Request.ServerVariables("HTTP_X_FORWARDED_FOR"))
If TrueIP = "" Then 
TrueIP = Request.ServerVariables("REMOTE_ADDR")
end if
	
If Not (Action = "" or UpiUserName = "" or UpiMd5Info = "") And HLAdminIntegration = True Then
       Md5OLD=0          '兼容PHP整合中文同步设置
 	   HLMd5Info = MD5(UpiUserName&AdminSecurityKey,32)
	If UpiMd5Info <> UCase(HLMd5Info) Then	
	   Md5OLD=1          '不兼容PHP整合中文同步设置
 	   HLMd5Info = MD5(UpiUserName&AdminSecurityKey,32)
	   If UpiMd5Info <> UCase(HLMd5Info) Then
	      Response.End()
	   End IF
	End IF	
	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

Sub AddUser()
dim flag,TempRs,rs2

flag="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37"
	set	rs=Dvbbs.Execute("select username from "&Admintable&" where	username='"&UpiUserName&"'")
    if not (rs.eof and rs.bof) then	exit sub
	Dvbbs.Execute("insert into "&Admintable&" (username,[password],adduser,flag)	values ('"&UpiUserName&"','"&UpiUserPass&"','admin','"&flag&"')")
		
	set rs2=Dvbbs.Execute("select username from [Dv_User] where username='"&UpiUserName&"'")
	if not (rs2.eof and rs2.bof) then exit sub
	'============添加前台用户并设置为管理员权限	=======================
	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		
		'插入新记录
		TempRs.addnew
		TempRs("UserName")=UpiUserName
		TempRs("UserPassword")=UpiUserPass
		TempRs("UserEmail")=UpiUserName&"@howave.com"
		TempRs("Userclass")="管理员"
		TempRs("TitlePic")="level20.gif"
		TempRs("UserQuesion")="请输入你的密码"
		TempRs("UserAnswer")=UpiUserPass
		TempRs("TruePassWord")="3N163U7ggb314hwb"
		TempRs("UserIM")="||||||||||||||||||"
		TempRs("UserPost")=0
		TempRs("Usersex")=1
		TempRs("Lockuser")=0
		TempRs("UserGroupID")=1		
		TempRs("JoinDate")=NOW()
		TempRs("UserFace")="images/userface/image1.gif"
		TempRs("UserWidth")=32
		TempRs("Usertoday")="0|0|0|0|0"
		TempRs("UserHeight")=32
		TempRs("UserLogins")=1
		TempRs("LastLogin")=NOW()
		TempRs("userWealth")=100
		TempRs("userEP")=60
		TempRs("usercP")=30
		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
  '=================添加前台管理员结束====================================
End Sub '添加新管理员

Sub LoginUser()
  
  Set TempRs=Dvbbs.Execute("Select top 1 * from Dv_admin where username='"&UpiUserName&"' and password='"&UpiUserPass&"'")
     If not TempRs.Eof  Then	
      Dvbbs.Execute("Update Dv_admin Set LastLogin="&SqlNowString&",LastLoginIP='"&TrueIP&"' Where UserName='"&UpiUserName&"'")
	  session("bbs")="989889"
	  Session("flag")=TempRs("flag")                             
	  Session("MemberName")=TempRs("adduser")
	 end if
	 TempRs.close
	 set TempRs=nothing
	 Call ChkUserLogin(UpiUserName, UpiUserPass, "", UpiSaveCookies, 1)
End Sub

Sub LogoutUser()
	Session("flag")=""
	Session("MemberName")=""
End Sub

Sub ModifyUserInfo()
	'On Error Resume Next
	Dim Rs,sql,i
Set Rs=Dvbbs.Execute("Select H_Content From Dv_Help Where H_ID=1")
	set	rs=server.createobject("adodb.recordset")
	sql="select	* from "&admintable&" where	username='"&UpiUserName&"'"
	rs.open	sql,conn,1,3
	if not rs.eof and not rs.bof then
	Rs("password") = UpiUserPass
	rs.update
	End	if
	rs.close
	set	rs=nothing
End Sub

Sub DelUser()
    On Error Resume Next
    
    Dvbbs.Execute("delete from [dv_admin] where UserName='" & UpiUserName & "'")
End Sub

Function ChkUserLogin(username,password,mobile,usercookies,ctype)

	Dim rsUser,article,userclass,titlepic
	Dim userhidden,lastip,UserLastLogin
	Dim GroupID,ClassSql,FoundGrade
	Dim regname,iMyUserInfo
	Dim sql,sqlstr,OLDuserhidden

	FoundGrade=False
	lastip=Dvbbs.UserTrueIP
	userhidden=request.form("userhidden")
	If userhidden <> "1" Then userhidden=2
	ChkUserLogin=false
	If mobile<>"" Then
		sqlstr=" Passport='"&mobile&"'"
	Else
		sqlstr=" UserName='"&username&"'"
	End If
	Sql="Select UserID,UserName,UserPassword,UserEmail,UserPost,UserTopic,UserSex,UserFace,UserWidth,UserHeight,JoinDate,LastLogin,lastlogin as cometime , LastLogin as activetime,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,userid as boardid"
	Sql=Sql & " From [Dv_User] Where "&sqlstr&""

	set rsUser=Dvbbs.Execute(sql)
	If rsUser.eof and rsUser.bof Then
		ChkUserLogin=False
		Exit Function
	Else
		If rsUser("Lockuser") =1 Or rsUser("UserGroupID") =5 Then
			ChkUserLogin=False
			Exit Function
		Else
			If Trim(password)=Trim(rsUser("UserPassword")) Then
				ChkUserLogin=True
				Dvbbs.UserID=RsUser("UserID")
				RegName = RsUser("UserName")
				Article= RsUser("UserPost")
				UserLastLogin = RsUser("cometime")
				UserClass = RsUser("Userclass")		
				GroupID = RsUser("userGroupID")
				OLDuserhidden=RsUser("UserHidden")
				TitlePic = RsUser("UserTitle")
				If Article < 0  Then Article=0
				Set Dvbbs.UserSession=Dvbbs.RecordsetToxml(rsUser,"userinfo","xml")
				Dvbbs.UserSession.documentElement.selectSingleNode("userinfo/@cometime").text=Now()
				Dvbbs.UserSession.documentElement.selectSingleNode("userinfo/@activetime").text=DateAdd("s",-3600,Now())
				Dvbbs.UserSession.documentElement.selectSingleNode("userinfo/@boardid").text=0
				Dvbbs.UserSession.documentElement.selectSingleNode("userinfo").attributes.setNamedItem(Dvbbs.UserSession.createNode(2,"isuserpermissionall","")).text=Dvbbs.FoundUserPermission_All()
				If OLDuserhidden <> CLng(userhidden) Then
					Dvbbs.UserSession.documentElement.selectSingleNode("userinfo/@userhidden").text=userhidden
					Dvbbs.Execute("update Dv_user set userhidden="&userhidden&" where UserId=" & Dvbbs.UserID)
				End If
				Dim BS
				Set Bs=Dvbbs.GetBrowser()
				Dvbbs.UserSession.documentElement.appendChild(Bs.documentElement)
				If EnabledSession Then 	Session(Dvbbs.CacheName & "UserID")=Dvbbs.UserSession.xml
			Else
				ChkUserLogin=False
				Exit Function
			End If
		End If
	End If
	If ChkUserLogin Then
	REM 判断用户组(等级)资料，当用户级别为跟随文章数增长则自动更新用户组(等级)
	REM 自动更新用户数据
	REM 如果属于系统或特殊或多属性组
	Set rsUser=Dvbbs.Execute("Select MinArticle,IsSetting,ParentGID,UserTitle,GroupPic From Dv_UserGroups Where UserGroupID="&GroupID)
	If Not (rsUser.Eof And rsUser.Bof) Then
		If rsUser(2)=1 Or rsUser(2)=2 Or rsUser(2)=4 Or rsUser(2)=5 Then
			'用户等级不按照文章升级，用户为系统或特殊或多属性组
			UserClass=rsUser(3)
			TitlePic=rsUser(4)
			FoundGrade=True
		End If
	End If
	If Not FoundGrade Then
		'如果不属于系统或特殊或多属性组,则将该用户属于注册用户组且按照其文章数自动更新其用户组(等级)
		Set rsUser=Dvbbs.Execute("Select Top 1 usertitle,GroupPic,UserGroupID From Dv_UserGroups Where ParentGID=3 And Minarticle<="&Article&" Order By MinArticle Desc,UserGroupID")
		If Not (rsUser.Eof And rsUser.Bof) Then
			UserClass=rsUser(0)
			TitlePic=rsUser(1)
			GroupID=rsUser(2)
			FoundGrade=True
		End If
	End If
	Set rsUser=nothing
	If Not FoundGrade Then Response.Redirect "showerr.asp?ErrCodes=<li>系统没有找到您的注册用户组资料，请联系管理员进行修正。&action=OtherErr"
	select case ctype
	case 1
		If Datediff("d",UserLastLogin,Now())=0 Then
			sql="update [Dv_User] set LastLogin="&SqlNowString&",UserLogins=UserLogins+1,UserLastIP='"&lastip&"',userclass='"&userclass&"',titlepic='"&titlepic&"',UserGroupID="&GroupID&",TruePassWord='"&TruePassWord&"' where userid="&dvbbs.UserID
		Else
			sql="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
	case 2
		sql="update [Dv_User] set UserPost=UserPost+1,UserTopic=UserTopic+1,userWealth=userWealth+"&Dvbbs.Forum_user(1)&",userEP=userEP+"&Dvbbs.Forum_user(6)&",userCP=userCP+"&Dvbbs.Forum_user(11)&",LastLogin="&SqlNowString&",UserLastIP='"&lastip&"',userclass='"&userclass&"',titlepic='"&titlepic&"',UserGroupID="&GroupID&",TruePassWord='"&TruePassWord&"' where userid="&dvbbs.UserID
	case 3
		sql="update [Dv_User] set UserPost=UserPost+1,userWealth=userWealth+"&Dvbbs.Forum_user(2)&",userEP=userEP+"&Dvbbs.Forum_user(7)&",userCP=userCP+"&Dvbbs.Forum_user(12)&",LastLogin="&SqlNowString&",UserLastIP='"&lastip&"',userclass='"&userclass&"',titlepic='"&titlepic&"',UserGroupID="&GroupID&",TruePassWord='"&TruePassWord&"' where userid="&dvbbs.UserID
	end select
	Dvbbs.Execute(sql)
	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(username)<>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(usercookies) or usercookies="" Then usercookies="0"
	
	select case usercookies
	case "0"
		Response.Cookies(Dvbbs.Forum_sn)("usercookies") = usercookies
	case 1
   		Response.Cookies(Dvbbs.Forum_sn).Expires=Date+1
		Response.Cookies(Dvbbs.Forum_sn)("usercookies") = usercookies
	case 2
		Response.Cookies(Dvbbs.Forum_sn).Expires=Date+31
		Response.Cookies(Dvbbs.Forum_sn)("usercookies") = usercookies
	case 3
		Response.Cookies(Dvbbs.Forum_sn).Expires=Date+365
		Response.Cookies(Dvbbs.Forum_sn)("usercookies") = usercookies
	end select
	Response.Cookies(Dvbbs.Forum_sn).path = Dvbbs.cookiepath
	Response.Cookies(Dvbbs.Forum_sn)("username") = regname
	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
	Dvbbs.UserSession.documentElement.selectSingleNode("userinfo/@truepassword").text= TruePassWord
	Dvbbs.Membername=Dvbbs.Checkstr(regname)
	Dvbbs.Memberclass=Dvbbs.Checkstr(userclass)
	Dvbbs.UserGroupID=GroupID
	End If
End Function
%>