<!--#include file=conn.asp-->
<!--#include file="inc/const.asp" -->
<!--#include file="inc/plus_check.asp"-->
<!--#include file="Dv_Plus/Avatar/Plus_Avatar_Const.asp"-->
<!--#include file="inc/md5.asp"-->
<%
Dim RetFlag
If Request("action") = "" Then
	Call RebuildRemotePhoto()
	If v_FoundErr Then
		Response.Write v_ErrCodes
	End If
Else
	Dv_Plus.Name = "Plus_Avatar"
	Dv_Plus.CheckLogin
	Dvbbs.LoadTemplates "Avatar"
	If Request("action") = "3" Then
		Dvbbs.Stats = Template.Strings(46)
	Else
		Dvbbs.Stats = Template.Strings(45)
	End If
	Dvbbs.ActiveOnline
	v_FoundErr = False
	If Dvbbs.Forum_Setting(82) <> "0" Then
		v_ErrCodes = "<li>该论坛尚未开启虚拟形象功能，不能使用远程服务！"
		v_FoundErr = True
	ElseIf Dvbbs.UserID = 0 Then
		v_ErrCodes = "<li>您不是会员，不能使用远程服务的任何功能！"
		v_FoundErr = True
	Else
		RetFlag = False
		If Request("action") = "1" Then
			Call CreateRemoteAccount()
		ElseIf Request("action") = "2" Then
			Call UpdateRemoteAccount()
		ElseIf Request("action") = "3" Then
			Call ViewRemoteLogs()
		ElseIf Request("action") = "4" Then
			Call AddRemoteAccount()
		ElseIf Request("action") = "5" Then
			Call SaveRemoteAccount()
		ElseIf Request("action") = "6" Then
			Call DeleteLogs()
		Else
			v_ErrCodes = "<li>指定了错误的参数！"
			v_FoundErr = True
		End If
	End If
	If v_FoundErr Then
		Call ShowErr(RetFlag)
	End If
	Dvbbs.Footer
	Set Dv_Plus = Nothing
End If

Sub CreateRemoteAccount()
	Dim TempStr
	
	If Not Dvbbs.Plus_Execute("Select Top 1 ID From Dv_Plus_Avatar_RemoteUser Where UserID=" & Dvbbs.UserID).Eof Then
		v_ErrCodes = v_ErrCodes & "<li>您已经申请过远程服务帐号，无需再次申请！"
		v_FoundErr = True
		Exit Sub
	End If
	If Dvbbs.Master Then
		v_ErrCodes = v_ErrCodes & "<li>管理员不能申请远程服务帐号！"
		v_FoundErr = True
		Exit Sub
	End If
	Dvbbs.Head
	TempStr = Template.html(25)
	TempStr = Replace(TempStr, "{$UserName}", Dvbbs.MemberName)
	TempStr = Replace(TempStr, "{$MyMoney}", v_MyMoney)
	If v_MyVip <> 1 Then
		TempStr = Replace(TempStr, "{$RemotePrice}", AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").Attributes.getNamedItem("NormalRemoteCreatePrice").Text)
	Else
		TempStr = Replace(TempStr, "{$RemotePrice}", AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").Attributes.getNamedItem("VIPRemoteCreatePrice").Text)
	End If
	Response.Write TempStr
End Sub

Sub AddRemoteAccount()
	Dim Password1, Password2, IPAddress, IPSplit, Password, i, Rs
	
	If Not Dvbbs.Plus_Execute("Select Top 1 ID From Dv_Plus_Avatar_RemoteUser Where UserID=" & Dvbbs.UserID).Eof Then
		v_ErrCodes = v_ErrCodes & "<li>您已经申请过远程服务帐号，无需再次申请！"
		v_FoundErr = True
		Exit Sub
	End If
	If Dvbbs.Master Then
		v_ErrCodes = v_ErrCodes & "<li>管理员不能申请远程服务帐号！"
		v_FoundErr = True
		Exit Sub
	End If
	RetFlag = True
	Password1 = Dvbbs.CheckStr(Trim(Request.form("Password1")))
	If Password1 = "" Then
		v_ErrCodes = v_ErrCodes & "<li>远程服务密码不可为空！"
		v_FoundErr = True
		Exit Sub
	End If
	Password2 = Dvbbs.CheckStr(Trim(Request.form("Password2")))
	If Password2 = "" Then
		v_ErrCodes = v_ErrCodes & "<li>确认密码不可为空！"
		v_FoundErr = True
		Exit Sub
	End If
	If Password1 <> Password2 Then
		v_ErrCodes = v_ErrCodes & "<li>远程服务密码和确认密码必须一致！"
		v_FoundErr = True
		Exit Sub
	End If
	If Len(Password1) > 10 Or Len(Password1) < 6 Then
		v_ErrCodes = v_ErrCodes & "<li>远程服务密码的长度不能大于10小于6。"
		v_FoundErr = True
		Exit Sub
	End If
	Password = MD5(Password1, 16)
	IPAddress = Trim(Request.form("IPAddress"))
	If IPAddress <> "" Then
		IPSplit = Split(IPAddress, ".")
		If UBound(IPSplit) <> 3 Then
			v_ErrCodes = v_ErrCodes & "<li>请输入正确的IP地址。"
			v_FoundErr = True
			Exit Sub
		End If
		IPAddress = ""
		For i = 0 To 3
			If Not IsInteger(IPSplit(i)) Then
				v_ErrCodes = v_ErrCodes & "<li>请输入正确的IP地址。"
				v_FoundErr = True
				Exit Sub
			End If
			IPSplit(i) = CLng(IPSplit(i))
			If IPSplit(i) > 255 Then
				v_ErrCodes = v_ErrCodes & "<li>请输入正确的IP地址。"
				v_FoundErr = True
				Exit Sub
			End If
			If i > 0 Then IPAddress = IPAddress & "."
			IPAddress = IPAddress & IPSplit(i)
		Next
	Else
		IPAddress = ""
	End If
	Dvbbs.Plus_Execute("Insert Into Dv_Plus_Avatar_RemoteUser (UserID, UserPassword, UserIP) Values (" & Dvbbs.UserID & ",'" & Password & "','" & IPAddress & "')")
	SucMsg = "<li>已经成功地创建了远程服务帐号！"
	Call ShowSuc(SucMsg, False)
	Response.Write "<script>if(window.opener) window.opener.document.execCommand('Refresh');</script>"
End Sub

Sub UpdateRemoteAccount()
	Dim TempStr, Rs
	
	Set Rs = Dvbbs.Plus_Execute("Select Top 1 UserPassword,UserIP,PhotoCount,UseMoney From Dv_Plus_Avatar_RemoteUser Where UserID=" & Dvbbs.UserID)
	If Rs.Eof Then
		Rs.Close
		v_ErrCodes = v_ErrCodes & "<li>您尚未申请远程服务帐号，不能修改帐户信息！"
		v_FoundErr = True
		Exit Sub
	End If
	Dvbbs.Head
	TempStr = Template.html(26)
	TempStr = Replace(TempStr, "{$UserName}", Dvbbs.MemberName)
	TempStr = Replace(TempStr, "{$UserPassword}", Rs(0))
	If IsNull(Rs(1)) Then
		TempStr = Replace(TempStr, "{$UserIP}", "")
	Else
		TempStr = Replace(TempStr, "{$UserIP}", Rs(1))
	End If
	TempStr = Replace(TempStr, "{$PhotoCount}", Rs(2))
	TempStr = Replace(TempStr, "{$UseMoney}", Rs(3))
	TempStr = Replace(TempStr, "{$MyMoney}", v_MyMoney)
	If v_MyVip <> 1 Then
		TempStr = Replace(TempStr, "{$RemotePrice}", AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").Attributes.getNamedItem("NormalRemoteCreatePrice").Text)
	Else
		TempStr = Replace(TempStr, "{$RemotePrice}", AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").Attributes.getNamedItem("VIPRemoteCreatePrice").Text)
	End If
	Response.Write TempStr
End Sub

Sub SaveRemoteAccount()
	Dim Password1, Password2, Password, IPAddress, IPSplit, i
	
	If Dvbbs.Plus_Execute("Select Top 1 UserPassword,UserIP,PhotoCount,UseMoney From Dv_Plus_Avatar_RemoteUser Where UserID=" & Dvbbs.UserID).Eof Then
		v_ErrCodes = v_ErrCodes & "<li>您尚未申请远程服务帐号，不能修改帐户信息！"
		v_FoundErr = True
		Exit Sub
	End If
	Password1 = Dvbbs.CheckStr(Trim(Request.form("Password1")))
	If Password1 <> "" Then
		Password2 = Dvbbs.CheckStr(Trim(Request.form("Password2")))
		If Password2 = "" Then
			v_ErrCodes = v_ErrCodes & "<li>确认密码不可为空！"
			v_FoundErr = True
			Exit Sub
		End If
		If Password1 <> Password2 Then
			v_ErrCodes = v_ErrCodes & "<li>远程服务密码和确认密码必须一致！"
			v_FoundErr = True
			Exit Sub
		End If
		If Len(Password1) > 10 Or Len(Password1) < 6 Then
			v_ErrCodes = v_ErrCodes & "<li>远程服务密码的长度不能大于10小于6。"
			v_FoundErr = True
			Exit Sub
		End If
		Password = MD5(Password1, 16)
	Else
		Password = ""
	End If
	IPAddress = Trim(Request.form("IPAddress"))
	If IPAddress <> "" Then
		IPSplit = Split(IPAddress, ".")
		If UBound(IPSplit) <> 3 Then
			v_ErrCodes = v_ErrCodes & "<li>请输入正确的IP地址。"
			v_FoundErr = True
			Exit Sub
		End If
		IPAddress = ""
		For i = 0 To 3
			If Not IsInteger(IPSplit(i)) Then
				v_ErrCodes = v_ErrCodes & "<li>请输入正确的IP地址。"
				v_FoundErr = True
				Exit Sub
			End If
			IPSplit(i) = CLng(IPSplit(i))
			If IPSplit(i) > 255 Then
				v_ErrCodes = v_ErrCodes & "<li>请输入正确的IP地址。"
				v_FoundErr = True
				Exit Sub
			End If
			If i > 0 Then IPAddress = IPAddress & "."
			IPAddress = IPAddress & IPSplit(i)
		Next
	Else
		IPAddress = ""
	End If
	If Password = "" Then
		Dvbbs.Plus_Execute("Update Dv_Plus_Avatar_RemoteUser Set UserIP='" & IPAddress & "' Where UserID=" & Dvbbs.UserID)
	Else
		Dvbbs.Plus_Execute("Update Dv_Plus_Avatar_RemoteUser Set UserPassword='" & Password & "',UserIP='" & IPAddress & "' Where UserID=" & Dvbbs.UserID)
	End If
	SucMsg = "<li>已经成功地修改了远程服务帐号信息！"
	Call ShowSuc(SucMsg, False)
End Sub

Sub ViewRemoteLogs()
	Dim TotalRec, n, CurrentPage, Page_Count, PCount
	Dim TempStr, TempStr1, TempStr2
	Dim Rs, SQL, TempRs

	If Not Dvbbs.Master And Dvbbs.Plus_Execute("Select Top 1 ID From Dv_Plus_Avatar_RemoteUser Where UserID=" & Dvbbs.UserID).Eof Then
		v_ErrCodes = v_ErrCodes & "<li>您尚未申请远程服务帐号，不能查看远程服务日志！"
		v_FoundErr = True
		Exit Sub
	End If
	PCount = 0
	TotalRec = 0
	CurrentPage = Request("page")
	If CurrentPage = "" Or Not IsInteger(CurrentPage) Then
		CurrentPage = 1
	Else
		CurrentPage = CLng(CurrentPage)
	End If
	Dvbbs.Nav()
	Dvbbs.Head_var 2,0,"",""
	TempStr = Template.html(27)
	Set Rs = Server.CreateObject("ADODB.Recordset")
	If Dvbbs.Master Then
		SQL = "Select * From Dv_Plus_Avatar_RemoteLogs Order By UserID,ID Desc"
	Else
		SQL = "Select * From Dv_Plus_Avatar_RemoteLogs Where UserID=" & Dvbbs.UserID & " Order By ID Desc"
	End If
	If Cint(Dvbbs.Forum_Setting(92))=1 Then
		If Not IsObject(Plus_Conn) Then Plus_ConnectionDatabase
		Rs.Open SQL,Plus_Conn,1,1
	Else
		If Not IsObject(Conn) Then ConnectionDatabase
		Rs.Open SQL,Conn,1,1
	End If
	If Rs.Eof Then
		TempStr1 = Template.html(28)
		TempStr = Replace(TempStr, "{$pagelist}", "")
	Else
		Rs.PageSize = Dvbbs.Forum_Setting(11)
		Rs.AbsolutePage = CurrentPage
		Page_Count = 0
		TotalRec = Rs.RecordCount
		Do While Not Rs.Eof And Page_Count < Rs.PageSize
			TempStr2 = Template.html(29)
			TempStr2 = Replace(TempStr2, "{$CreateDate}", FormatDateTime(Rs("CreateDate"), 2))
			TempStr2 = Replace(TempStr2, "{$PhotoID}", Rs("PhotoID"))
			TempStr2 = Replace(TempStr2, "{$PhotoName}", Server.HTMLEncode(Rs("PhotoName")))
			TempStr2 = Replace(TempStr2, "{$PhotoSponsor}", Server.HTMLEncode(Rs("PhotoSponsor")))
			If IsNull(Rs("PhotoParticipator")) Or Rs("PhotoParticipator") = "" Then
				TempStr2 = Replace(TempStr2, "{$PhotoParticipator}", "<font color=gray>个人写真</font>")
			Else
				TempStr2 = Replace(TempStr2, "{$PhotoParticipator}", Server.HTMLEncode(Rs("PhotoParticipator")))
			End If
			TempStr2 = Replace(TempStr2, "{$PhotoDate}", FormatDateTime(Rs("PhotoDate"), 2))
			TempStr2 = Replace(TempStr2, "{$PhotoUserCount}", Rs("PhotoUserCount"))
			TempStr2 = Replace(TempStr2, "{$UseMoney}", Rs("UseMoney"))
			TempStr2 = Replace(TempStr2, "{$sid}", Rs("ID"))
			If Dvbbs.Master Then
				Set TempRs = Dvbbs.Execute("Select Top 1 UserName From Dv_User Where UserID=" & Rs("UserID"))
				If TempRs.Eof Then
					TempStr2 = Replace(TempStr2, "{$UserName}", "<font color=gray>未知</font>")
				Else
					TempStr2 = Replace(TempStr2, "{$UserName}", TempRs(0))
				End If
			Else
				TempStr2 = Replace(TempStr2, "{$UserName}", Dvbbs.MemberName)
			End If
			TempStr1 = TempStr1 & TempStr2
			Page_Count = Page_Count + 1
			Rs.MoveNext
		Loop
		PCount = Rs.PageCount
		Rs.Close
		Set Rs = Nothing	
	End If
	TempStr = Replace(TempStr, "{$logsloop}", TempStr1)
	TempStr = Replace(TempStr, "{$pagelist}", template.html(30))
	TempStr = Replace(TempStr, "{$CurPage}", CurrentPage)
	TempStr = Replace(TempStr, "{$PageSize}", Dvbbs.Forum_Setting(11))
	TempStr = Replace(TempStr, "{$TotalRec}", TotalRec)
	TempStr = Replace(TempStr, "{$URLStr}", "Plus_Avatar_RemoteCreate.asp?action=3")
	Response.Write TempStr
End Sub

Sub DeleteLogs()
	Dim sid, fixid
	
	If Dvbbs.Plus_Execute("Select Top 1 ID From Dv_Plus_Avatar_RemoteUser Where UserID=" & Dvbbs.UserID).Eof Then
		v_ErrCodes = v_ErrCodes & "<li>您尚未申请远程服务帐号，不能对远程服务日志进行管理！"
		v_FoundErr = True
		Exit Sub
	End If
	If Request.form("sid") = "" Then
		v_ErrCodes = v_ErrCodes & "<li>指定的参数有错误！"
		v_FoundErr = True
		Exit Sub
	Else
		sid = Replace(Request.Form("sid"), "'", "")
		sid = Replace(sid, ";", "")
		sid = Replace(sid, "--", "")
		sid = Replace(sid, ")", "")
		fixid = Replace(sid, " ", "")
		fixid = Replace(fixid, ",", "")
		If Not IsNumeric(fixid) Then
			v_ErrCodes = v_ErrCodes & "<li>指定的参数有错误！"
			v_FoundErr = True
			Exit Sub
		End If
	End If 	
	If Dvbbs.Master Then
		Dvbbs.Plus_Execute("Delete From Dv_Plus_Avatar_RemoteLogs Where ID In (" & sid & ")")
	Else
		Dvbbs.Plus_Execute("Delete From Dv_Plus_Avatar_RemoteLogs Where UserID=" & Dvbbs.UserID & " And ID In (" & sid & ")")
	End If
	Dvbbs.Nav()
	Dvbbs.Head_var 2,0,"",""
	Dvbbs.Dvbbs_Suc("已经成功地删除了指定的远程服务日志！")
End Sub

Sub RebuildRemotePhoto()
	Dim PhotoID, RetValue, MyURL, UserName, Password, Rs, CreatePhotoFee
	Dim UserID, RetSplit, ResponseData, LastLogID, UserTrueIP, IPSplit, i, AccountIP
	
	If AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").attributes.getNamedItem("AllowRemoteCreatePhoto").Text = "0" Then
		v_ErrCodes = "Err,远程服务器没有开启照片生成服务！"
		v_FoundErr = True
		Exit Sub
	End If
	PhotoID = Trim(Request("photoid"))
	If PhotoID = "" Then
		v_ErrCodes = "Err,提交的数据有错误！"
		v_FoundErr = True
		Exit Sub
	ElseIf Not isInteger(PhotoID) Then
		v_ErrCodes = "Err,提交的数据有错误！"
		v_FoundErr = True
		Exit Sub
	Else
		PhotoID = CLng(PhotoID)
	End If
	MyURL = Trim(Request("MyURL"))
	If MyURL = "" Then
		v_ErrCodes = "Err,提交的数据有错误！"
		v_FoundErr = True
		Exit Sub
	End If
	UserName = Dvbbs.CheckStr(Trim(Request("username")))
	If UserName = "" Then
		v_ErrCodes = "Err,提交的数据有错误！"
		v_FoundErr = True
		Exit Sub
	End If
	Password = Dvbbs.CheckStr(Trim(Request("password")))
	If Password = "" Then
		v_ErrCodes = "Err,提交的数据有错误！"
		v_FoundErr = True
		Exit Sub
	End If
	Password = MD5(Password, 16)
	If hasVipPlus = 1 Then
		Set Rs = Dvbbs.Execute("Select Top 1 " & VipFieldName & ",UserWealth,UserID From Dv_User Where UserName='" & UserName & "'")
		If Rs.Eof Then
			Rs.Close
			v_ErrCodes = "Err,用户不存在或用户密码错误！"
			v_FoundErr = True
			Exit Sub
		End If
		v_MyVip = Rs(0)
		If IsNull(v_MyVip) Or Not IsInteger(v_MyVip) Then
			v_MyVip = 0
		End If
	Else
		Set Rs = Dvbbs.Execute("Select Top 1 UserGroupID,UserWealth,UserID From Dv_User Where UserName='" & UserName & "'")
		If Rs.Eof Then
			Rs.Close
			v_ErrCodes = "Err,用户不存在或用户密码错误！"
			v_FoundErr = True
			Exit Sub
		End If
		If Rs(0) = 1 Or Rs(0) = 2 Or Rs(0) = 3 Or Rs(0) = 8 Then
			v_MyVip = 1
		ElseIf Not Dvbbs.Execute("Select Top 1 ParentGID From Dv_UserGroups Where UserGroupID=" & Rs(0) & " And ParentGID=5").Eof Then
			v_MyVip = 1
		Else
			v_MyVip = 0
		End If
	End If
	v_MyMoney = Rs(1)
	UserID = Rs(2)
	Rs.Close
	Set Rs = Dvbbs.Plus_Execute("Select Top 1 UserIP From Dv_Plus_Avatar_RemoteUser Where UserID=" & UserID & " And UserPassword='" & Password & "'")
	If Rs.Eof Then
		Rs.Close
		v_ErrCodes = "Err,用户不存在或用户密码错误！"
		v_FoundErr = True
		Exit Sub
	End If
	If Not IsNull(Rs(0)) And Rs(0) <> "" Then
		UserTrueIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
		If UserTrueIP = "" Then UserTrueIP = Request.ServerVariables("REMOTE_ADDR")
		UserTrueIP = Dvbbs.CheckStr(UserTrueIP)
		If UserTrueIP = "" Then
			Rs.Close
			v_ErrCodes = "Err,用户IP地址与远程服务帐号中设置的不符！1"
			v_FoundErr = True
			Exit Sub
		End If
		IPSplit = Split(UserTrueIP, ".")
		If UBound(IPSplit) <> 3 Then
			Rs.Close
			v_ErrCodes = "Err,用户IP地址与远程服务帐号中设置的不符！2"
			v_FoundErr = True
			Exit Sub
		End If
		UserTrueIP = ""
		For i = 0 To 3
			If i > 0 Then UserTrueIP = UserTrueIP & "."
			UserTrueIP = UserTrueIP & CInt(IPSplit(i))
		Next
		IPSplit = Split(Rs(0), ".")
		If UBound(IPSplit) <> 3 Then
			Rs.Close
			v_ErrCodes = "Err,用户IP地址与远程服务帐号中设置的不符！3"
			v_FoundErr = True
			Exit Sub
		End If
		AccountIP = ""
		For i = 0 To 3
			If i > 0 Then AccountIP = AccountIP & "."
			AccountIP = AccountIP & CInt(IPSplit(i))
		Next
		If UserTrueIP <> AccountIP Then
			Rs.Close
			v_ErrCodes = "Err,用户IP地址与远程服务帐号中设置的不符！"
			v_FoundErr = True
			Exit Sub
		End If
	End If
	Rs.Close
	If v_MyVip <> 1 Then
		CreatePhotoFee = CLng(AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").attributes.getNamedItem("NormalRemoteCreatePrice").Text)
	Else
		CreatePhotoFee = CLng(AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").attributes.getNamedItem("VIPRemoteCreatePrice").Text)
	End If
	If CreatePhotoFee < 0 Then
		If v_MyVip <> 1 Then
			v_ErrCodes = "Err,远程服务器没有开通普通会员生成照片的服务！"
		Else
			v_ErrCodes = "Err,远程服务器没有开通VIP会员生成照片的服务！"
		End If
		v_FoundErr = True
		Exit Sub
	End If
	If CreatePhotoFee > v_MyMoney Then
		v_ErrCodes = "Err,远程服务器上生成照片的费用是" & CreatePhotoFee & "，当前帐户的现金(" & v_MyMoney & ")不足，无法生成照片！"
		v_FoundErr = True
		Exit Sub
	End If
	RetValue = CreateRemoteAvatarPhoto(MyURL, PhotoID, ResponseData)
	If RetValue = "" Then
		v_FoundErr = True
	Else
		RetSplit = Split(RetValue, "|||")
		Call UpdateUserWealth(UserName, -CreatePhotoFee)
		Dvbbs.Plus_Execute("Update Dv_Plus_Avatar_RemoteUser Set PhotoCount=PhotoCount+1,UseMoney=UseMoney+" & CreatePhotoFee & " Where UserID=" & UserID)
		Dvbbs.Plus_Execute("Insert Into Dv_Plus_Avatar_RemoteLogs (UserID,PhotoID,PhotoName,PhotoDate,PhotoSponsor,PhotoParticipator,PhotoUserCount,UseMoney) Values (" & UserID & "," & RetSplit(0) & ",'" & RetSplit(1) & "','" & RetSplit(2) & "','" & RetSplit(3) & "','" & RetSplit(4) & "'," & RetSplit(5) & "," & CreatePhotoFee & ")")
		LastLogID = Dvbbs.Plus_Execute("Select Max(ID) From Dv_Plus_Avatar_RemoteLogs")(0)
		If Response.IsClientConnected Then
			Response.AddHeader "Content-Disposition", "filename=RemotePhoto_" & LastLogID & ".png"
			Response.BinaryWrite ResponseData
		End If
	End If
End Sub
%>