<%session.Timeout=240%>
<!--#include virtual="/inc-freehost-function/FreeHostEmail.asp"-->
<!--#include virtual="/inc-freehost-function/FreeHostFunction.asp"-->
<!--#include virtual="/inc-freehost-function/FreeHostFunctionMain.asp"-->
<%

If Request("userid")="" Then
response.write "ret=0&err=auth-failure&freehosinfo=请输入代理用户名"
response.end
End If

If RegExpTest("[^A-Za-z0-9]",Request("userid")) Then
response.write "ret=0&err=auth-failure&freehosinfo=代理用户名非法"
response.end
End If

If RegExpTest("[^A-Za-z0-9]",Request("userstr")) Then
response.write "ret=0&err=auth-failure&freehosinfo=API密码非法"
response.end
End If

If Len(Request("userstr"))<>32 Then
response.write "ret=0&err=auth-failure&freehosinfo=API密码长度必须是32位"
response.end
End If

If RegExpTest("[^A-Za-z0-9]",Request("action")) Then
response.write "ret=0&err=auth-failure&freehosinfo=操作类型非法"
response.end
End If

If Request("action")<>"activate" And Request("action")<>"renew" And Request("action")<>"getinfo" And Request("action")<>"update" then
response.write "ret=0&err=auth-failure&freehosinfo=操作类型非法,目前此版本只支持开通及续费虚拟主机操作"
response.end
End if

Set conn=Server.CreateObject("ADODB.Connection")
	conn.Open Application("FreeHostDBlink")
  Set Sagent = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_AGENT where username='"&Request("userid")&"'"
		Sagent.Open Sql,conn,1,3
If Sagent.Eof Then
	ef=True
	ec="代理会员名不存在！"
end If

If Not ef Then
If IsNull(Sagent("canuseapi")) Or Sagent("canuseapi")<>true Then
	ef=True
	ec="代理"&Sagent("username")&"尚未开通API的权限"
end If
End If

If Not ef Then
If isnull(Sagent("Domain_Agent_nonemd5_6")) Then
	ef=True
	ec="代理"&Sagent("username")&"需要更新代理密码后才能使用本功能，请马上到上级系统中更新代理密码。"
End if
End if


If Not ef Then
If Sagent("agentdegree")>0 Or IsNull(Sagent("agentdegree")) Then
	ef=True
	ec="直接代理才能使用本功能。"'2016-11-22
end If
End If


If Not ef Then
If MD5(Sagent("Domain_Agent_nonemd5_6")+"7i24.com")<>Request("userstr") Then
	ef=True
	ec="md5检查非法"
End If
End if



If Not ef Then
If Request("action")="activate" then



session("username")=Sagent("username")
session("agentname")=Sagent("username")
session("agenttype")=Sagent("agenttype")
session("atspace")="API区"
if session("agentname")=session("username") then
session("AgnRunasUser")=True
else
session("AgnRunasUser")=false
end if
'登陆API区





if not ef Then
vpsname=LCase(Trim(Request ("vpsname")))
VPSpassword=Trim(Request ("VPSpassword"))
HostListpdid=Request ("productid")
ReqServerlistID=CStr(Request("idc"))
If Request("idc")="" Then ReqServerlistID="0"
If vpsname="" Then vpsname=Left(md5(now&"7i24.com"&now),8)
If VPSpassword="" Then VPSpassword=Left(md5(now&"7i24.com"),8)
End If


If session("username")="" Then
	ef=True
	ec="授权非法，有可能是超时造成的。"
ElseIf Request ("productid")="" Then
	ef=True
	ec="请选择产品！"
ElseIf not IsNumeric(Request ("productid"))  Then
	ef=True
	ec="产品ID应该是数字！"
ElseIf IsNumeric(vpsname)  Then
	ef=True
	ec="云服务器名不能是纯数字！"
ElseIf not IsNumeric(HostListpdid)  Then
	ef=True
	ec="产品ID应该是数字！"
ElseIf not IsNumeric(ReqServerlistID)  Then
	ef=True
	ec="机房ID应该是数字！"
ElseIf not IsNumeric(Request ("year"))  Then
	ef=True
	ec="时间应该是数字！"
ElseIf vpsname="" Then
	ef=True
	ec="请设置云服务器名！"
ElseIf Len(vpsname)<3 Then
	ef=True
	ec="云服务器名长度不能小于3位！"
ElseIf Trim(LCase(vpsname))="self" or Trim(LCase(vpsname))="adm" Or Trim(LCase(vpsname))="administrator" Or Trim(LCase(vpsname))="freehost" Or Trim(LCase(vpsname))="proxy" Then
	ef=True
	ec="您输入的VPS户名为保留用户名，请换一个云服务器名"
ElseIf RegExpTest("[^A-Za-z0-9]",vpsname) Then
	ef=True
	ec="云服务器名中有非法字符！"
ElseIf RegExpTest("[^A-Za-z0-9]",VPSpassword) Then
	ef=True
	ec="云服务器密码中有非法字符！"
end If

if not ef then
if DateDiff("s", Application("hostoptime"),NOW )<180 and Application("hostoplock")=true then
	ef=True
	ec="系统繁忙，前一个网站开通操作尚未处理完，请您稍等两分钟后再试，谢谢您的支持。"
end if
end if

if not ef then
Set conn=Server.CreateObject("ADODB.Connection")
	conn.Open Application("FreeHostDBlink")
	Set ds = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_USER where username='"&session("username")&"'"
		ds.Open Sql,conn,1,3
if ds.eof then
	ef=True
	ec="用户不存在!"
end if
end if

'if not ef then
'if ds("password")<>session("password") then
'	ef=True
'	ec="密码不正确!"
'end if
'end if

if not ef then
	Set dshost = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_VPSProductlist where id='"&HostListpdid&"'"
	dshost.Open Sql,conn,1,1
if dshost.Eof then
	ef=True
	ec="产品不存在！"
else
ServerlistID=dshost ("ServerlistID")'机房选择
if  ReqServerlistID=cstr(dshost ("ServerlistID2")) and dshost ("ServerlistID2")<>0 then ServerlistID=dshost ("ServerlistID2")
if  ReqServerlistID=cstr(dshost ("ServerlistID3")) and dshost ("ServerlistID3")<>0 then ServerlistID=dshost ("ServerlistID3")
if  ReqServerlistID=cstr(dshost ("ServerlistID4")) and dshost ("ServerlistID4")<>0 then ServerlistID=dshost ("ServerlistID4")
if  ReqServerlistID=cstr(dshost ("ServerlistID5")) and dshost ("ServerlistID5")<>0 then ServerlistID=dshost ("ServerlistID5")
if ds("agent1")<>"n" then
	Set agnserverid = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_VPSProductlist_AGN where id='"&HostListpdid&"' and agent1='"&ds("agent1")&"'"
	agnserverid.Open Sql,conn,1,1
dim isAgnHostSer
if not agnserverid.Eof then
if agnserverid ("ServerlistID")<>0 then
ServerlistID=agnserverid ("ServerlistID")
isAgnHostSer=true
else
isAgnHostSer=false
end if
end if
end if'isagn
end if
end If

'Response.write ReqServerlistID&ServerlistID
'Response.end
if not ef then
if dshost("cancloud")=false And Request ("CpuValue")<>"" then
	ef=True
	ec="本产品不允许弹性购买"'&Request ("CpuValue")&":"&dshost("cancloud")
end if
end If

if not ef then
if dshost("cancloud")=true then'是弹性购买产品
If not IsNumeric(Request ("CpuValue"))  Then
	ef=True
	ec="CPU应该是数字！"
ElseIf CInt(Request ("CpuValue"))<1 Then
	ef=True
	ec="CPU不能小于1个核心！"
ElseIf not IsNumeric(Request ("RamValue"))  Then
	ef=True
	ec="RAM应该是数字！"
ElseIf CInt(Request ("RamValue"))<256 Then
	ef=True
	ec="内存不能小于256M！"
ElseIf not IsNumeric(Request ("VHDValue"))  Then
	ef=True
	ec="虚拟硬盘大小应该是数字！"
ElseIf CInt(Request ("VHDValue"))<10 Then
	ef=True
	ec="虚拟硬盘大小不能小于10G！"
ElseIf not IsNumeric(Request ("MbpsValue"))  Then
	ef=True
	ec="带宽大小应该是数字！"
ElseIf CInt(Request ("MbpsValue"))<1 Then
	ef=True
	ec="带宽不能小于1Mbps！"
ElseIf not IsNumeric(Request ("SNAValue"))  Then
	ef=True
	ec="快照数量应该是数字！"
ElseIf CInt(Request ("SNAValue"))<0 Then
	ef=True
	ec="快照不能小于0个！"
ElseIf not IsNumeric(Request ("BakValue"))  Then
	ef=True
	ec="备份数量应该是数字！"
ElseIf CInt(Request ("BakValue"))<0 Then
	ef=True
	ec="备份不能小于0个！"
ElseIf not IsNumeric(Request ("NGBValue"))  Then
	ef=True
	ec="流量上限应该是数字！"
ElseIf CInt(Request ("NGBValue"))<0 Then
	ef=True
	ec="流量上限不能小于0个！"
ElseIf not IsNumeric(Request ("IPValue"))  Then
	ef=True
	ec="IP上限应该是数字！"
ElseIf CInt(Request ("IPValue"))<1 Then
	ef=True
	ec="IP上限不能小于1个！"
ElseIf not IsNumeric(Request ("IopsValue"))  Then
	ef=True
	ec="IOPS上限应该是数字！"
ElseIf CInt(Request ("IopsValue"))<0 Then
	ef=True
	ec="IOPS上限不能小于1个！"
ElseIf not IsNumeric(Request ("OSAllList"))  Then
	ef=True
	ec="操作系统ID应该是数字！"
ElseIf CInt(Request ("OSAllList"))<1 Or CInt(Request ("OSAllList"))>16 Then
	ef=True
	ec="操作系统ID不能小于1或大于16！"
end If
end if
end If

if not ef then
If Request ("year")<>999 and Request ("year")<>dshost("PAY_DAY") and Request ("year")<>dshost("PAY_WEEK") and Request ("year")<>dshost("PAY_Month") and Request ("year")<>dshost("PAY_Season")  and Request ("year")<>dshost("PAY_halfyear") and Request ("year")<>1 and Request ("year")<>dshost("PAY_2year") and Request ("year")<>dshost("PAY_3year") and Request ("year")<>dshost("PAY_4year") and Request ("year")<>dshost("PAY_5year") Then
	ef=True
	ec="请选择时间！"
end If
end if

if not ef then
if dshost("usermoney")=0 then
	Set dsph = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_VPS where username='"&session("username")&"'"
	dsph.Open Sql,conn,1,1
if not dsph.Eof then
	ef=True
	ec="金额为零的产品，一个用户只能申请一个！"
end if
dsph.close
end if
end if

if not ef then
if Not IsNull(dshost("onlyusebyagn")) Then
If session("agentname")<>dshost("onlyusebyagn") then
	ef=True
	ec="本产品只允许特定的代理使用！"
End if
end if
end If

if not ef then
if dshost("canbyenduser")="n" And session("agentname")="n" then
	ef=True
	ec="本产品不允许最终用户开通！"
end if
end If

if not ef then
if dshost("canbyagnenduser")="n" And session("agentname")<>"n" then
	ef=True
	ec="本产品不允许用户开通！"
end if
end If

if not ef then
if dshost("canbyagn")="n" And session("AgnRunasUser")=true then
	ef=True
	ec="本产品不允许代理开通！"
end if
end if

if not ef then
if Request ("year")=999 and dshost("cantest")="n" then
	ef=True
	ec="很抱歉,本产品不可以试用！"
end if
end If

if not ef then
if Request ("year")=999 and Application("vpscantest")=True And ds("usermoney")<0.01 then
	ef=True
	ec="很抱歉,余款为零的用户不可以试用！"
end if
end If

if not ef then
if Request ("year")=999 and (Hour(now)<Application("vpscantestST") or Hour(now)>Application("vpscantestEN"))then
	ef=True
	ec="很抱歉,现在的时间段不可以试用！"
end if
end If

if not ef then
if Request ("year")=dshost("PAY_DAY") and dshost("canday")="n" then
	ef=True
	ec="很抱歉,本产品不可以日付！"
end if
end If

if not ef then
if Request ("year")=dshost("PAY_WeeK") and dshost("canweek")="n" then
	ef=True
	ec="很抱歉,本产品不可以周付！"
end if
end If

if not ef then
if Request ("year")=dshost("PAY_Month") and dshost("canmonth")="n" then
	ef=True
	ec="很抱歉,本产品不可以月付！"
end if
end if
if not ef then
if Request ("year")=dshost("PAY_Season") and dshost("canseason")="n" then
	ef=True
	ec="很抱歉,本产品不可以季付！"
end if
end if
if not ef then
if Request ("year")=dshost("PAY_halfyear") and dshost("canhalfyear")="n" then
	ef=True
	ec="很抱歉,本产品不可以半年付！"
end if
end if


'IP黑名单检查
for iX=0 to ubound(Application("blockiparry"))
lenip=Len(Application("blockiparry")(iX))
If lenip>4 then

if Application("blockiparry")(iX)=Left(Request.ServerVariables("REMOTE_ADDR"),lenip) then
	ef=True
	ec="您的IP或IP段"&Request.ServerVariables("REMOTE_ADDR")&"属于黑名单IP:"&Application("blockiparry")(iX)&"@，不允许开通！"
exit for
end If

End if
Next

if not ef then
	Set dsserver = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_ServerVPSlist where id='"&ServerlistID&"'"
	dsserver.Open Sql,conn,1,1
if dsserver.Eof then
	ef=True
	ec="服务器不存在！"
else
    netby=dsserver("netby")
	ip=dsserver("ip")
	hostip=dsserver("ip")
'	ftpdomain=dsserver("ftpdomain")
end if
end If

if not ef then
if dshost("canADSL")=True then
Set dsVPSADSL = Server.CreateObject("ADODB.Recordset")	
		Sql="Select count(*) from FreeHost.FreeHost_Product_VPS_ADSL where ServerlistID="&dsserver("id")&""
		dsVPSADSL.Open Sql,conn,1,1
If dsVPSADSL(0)<1 Then
	ef=True
	ec="很抱歉,您选取的服务器的云服务器拨号资源已用光,请联系管理员处理."'&sql
End If
dsVPSADSL.close
End If
End If

if not ef then
Set dsVPSip = Server.CreateObject("ADODB.Recordset")	
		Sql="Select count(*) from FreeHost.FreeHost_Product_VPS_IP where ServerlistID="&dsserver("id")&" and VPSID=0"
		dsVPSip.Open Sql,conn,1,1
If dsVPSip(0)=0 Then
	ef=True
	ec="很抱歉,您选取的服务器的IP资源已用光,管理员尚未分配可用IP,请联系管理员处理."
End If
if dshost("cancloud")=true then'是弹性购买产品
If dsVPSip(0)<CInt(Request ("IPValue")) Then
	ef=True
	ec="很抱歉,您选取的服务器的IP资源已用光,请联系管理员处理."'&Request ("IPValue")&dsVPSip(0)
End If
End If
End if


'检查内存有没有用光
if not ef then
Set dsVPSMax = Server.CreateObject("ADODB.Recordset")	
		Sql="Select sum(maxram) from FreeHost.FreeHost_Product_VPS where ServerlistID="&dsserver("id")&""
		dsVPSMax.Open Sql,conn,1,1
If (dsVPSMax(0)+dshost("MaxRAM"))>dsserver("maxram") Then
	ef=True
	ec="很抱歉,您选取的服务器的内存资源已用光,请联系管理员处理."
End If
if dshost("cancloud")=true then'是弹性购买产品
If dsVPSMax(0)+Request ("RamValue")>dsserver("maxram") Then
	ef=True
	ec="很抱歉,您选取的服务器的内存资源已用光,请联系管理员处理."
End If
End If
dsVPSMax.close
End if

'检查space有没有用光
if not ef then
Set dsVPSMax = Server.CreateObject("ADODB.Recordset")	
		Sql="Select sum(Maxspace) from FreeHost.FreeHost_Product_VPS where ServerlistID="&dsserver("id")&""
		dsVPSMax.Open Sql,conn,1,1
If dsVPSMax(0)+dshost("Maxspace")>dsserver("Maxspace") Then
	ef=True
	ec="很抱歉,您选取的服务器的硬盘资源已用光,请联系管理员处理."
End If
if dshost("cancloud")=true then'是弹性购买产品
If dsVPSMax(0)+Request ("VHDValue")>dsserver("Maxspace") Then
	ef=True
	ec="很抱歉,您选取的服务器的硬盘资源已用光,请联系管理员处理."
End If
End If
dsVPSMax.close
End If

if not ef then
Set dsVPSMax = Server.CreateObject("ADODB.Recordset")	
		Sql="Select count(*) from FreeHost.FreeHost_Product_VPS where ServerlistID="&dsserver("id")&""
		dsVPSMax.Open Sql,conn,1,1
If dsVPSMax(0)+1>dsserver("MaxVPS") Then
	ef=True
	ec="很抱歉,您选取的服务器的VPS数量资源已用光,请联系管理员处理."
End If
dsVPSMax.close
End if





if not ef then
	Set dsph = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_VPS where vpsname='"&trim(vpsname)&"'"
	dsph.Open Sql,conn,1,3
if not dsph.Eof then
	ef=True
	ec="您输入的云服务器名已存在，请另外输入一个！"
end if
'dsph.close
end if


if not ef then
URL=dsserver("site")
FreeHostsharekey=dsserver("FreeHostsharekey")
temp=VPSOP(URL,FreeHostsharekey,"list all now",0)
if instr(temp,"名称: "&LCase(trim(vpsname)))>0 Then
	ef=True
	ec="服务器上已存在同名的VPS,请您联系管理员处理,或换一个云服务器名重试。"
End if
if not instr(temp,"成功")>1 Or instr(temp,"失败")>1 Then
	ef=True
	ec="系统暂时不可用，请联系管理员在管理区自检来了解原因，也可以稍后再试。"
end if
end If

'if not ef then
'Set dsph = Server.CreateObject("ADODB.Recordset")	
'	Sql="Select * from FreeHost.FreeHost_Product_VPS where username='"&session ("username")&"' and istest='y'"
'	dsph.Open Sql,conn,1,3
'if Request ("year")=999 then
'if not dsph.Eof then
'	ef=True
'	ec="一个用户只能试用一个产品！"
'end if
'end if
'end If

if not ef then
if Request ("year")=999 And dshost("cantest")="n" and Application("Agentcantest")=false then
	ef=True
	ec="很抱歉,管理员设置为不能设为试用！"
end if
end if

if not ef then
If dsserver("OStype")="Windows 2008R2" And dshost("Cpunum")>4 Then
	ef=True
	ec="操作系统是win2012才能设置超过4个CPU！"
end if
end If

if not ef then

dim starttime,temptime,pdtype
pdtype=dshost("VPStype")
starttime=NOW
'结束时间
y=Request ("year")
if y=1 then temptime=dateadd("yyyy",1,now)
if y=dshost("PAY_2year") then temptime=dateadd("yyyy",2,now)
if y=dshost("PAY_3year") then temptime=dateadd("yyyy",3,now)
if y=dshost("PAY_4year") then temptime=dateadd("yyyy",4,now)
if y=dshost("PAY_5year") then temptime=dateadd("yyyy",5,now)
if y=dshost("PAY_DAY") then temptime=dateadd("s",86400,now)
if y=dshost("PAY_WEEK") then temptime=dateadd("n",10080,now)
if y=dshost("PAY_Month") then temptime=dateadd("y",31,now)
if y=dshost("PAY_Season") then temptime=dateadd("y",92,now)
if y=dshost("PAY_halfyear") then temptime=dateadd("y",182,now)
if y=999 then
temptime=dateadd("y",Application("testtime"),now)
If Application("VPStestHour")>0 Then temptime=dateadd("n",Application("VPStestHour")*60,now)
dim isfortest
isfortest=true
end if
end If

if not ef then
if dshost("cancloud")=true then'是弹性购买产品'计算增量部分的普通用户情况下的金额

	'PAY_1yearvar=dsHost("usermoney")
	
'默认最小值addcpuMIN
'当前值addcpuNow
'增量每步addcpuNum
'每步需要钱数addCpuCost
if CInt(Request("CpuValue"))>dsHost("addCpuLim") Then
	ef=True
	ec="CPU超出上限！"
End If
if CInt(Request("RamValue"))>dsHost("addRamLim") Then
	ef=True
	ec="内存超出上限！"
End If
if CInt(Request("VHDValue"))>dsHost("addVHDLim") Then
	ef=True
	ec="虚拟硬盘超出上限！"
End If
if CInt(Request("MbpsValue"))>dsHost("addMbpsLim") Then
	ef=True
	ec="带宽超出上限！"
End If
If dsHost("CDNMaxMb")>0 then
if CInt(Request("NGBValue"))>dsHost("addNGBLim") Then
	ef=True
	ec="流量超出上限！"
End If
End If
If dsHost("MaxIOPS")>0 then
if CInt(Request("IopsValue"))>dsHost("addIOPSLim") Then
	ef=True
	ec="IOPS超出上限！"
End If
End if
if CInt(Request("SNAValue"))>dsHost("addSnaLim") Then
	ef=True
	ec="快照超出上限！"
End If
if CInt(Request("BakValue"))>dsHost("addBakLim") Then
	ef=True
	ec="备份数量超出上限！"
End If
if CInt(Request("IPValue"))>dsHost("addIPLim") Then
	ef=True
	ec="IP数量超出上限！"
End If
if (CInt(Request("CpuValue"))<dsHost("OSLimbyCPU")) Or (CInt(Request("RamValue"))<dsHost("OSLimbyRam")) Then
If InStr(","&dsHost("OSlim"),","&Request("OSAllList")&",")>0 then
	ef=True
	ec="由于内存或CPU限制不能预装这个操作系统！"
End If
End If

Dim cloudAddmoney
cloudAddmoney=0
if (CInt(Request("CpuValue"))>dsHost("CpuNum")) then	cloudAddmoney=cloudAddmoney+((Request("CpuValue")-dsHost("CpuNum"))/dsHost("addCpuNum"))*dsHost("addCpuCost")
if (CInt(Request("RamValue"))>dsHost("MAXRAM")) then	cloudAddmoney=cloudAddmoney+((Request("RamValue")-dsHost("MAXRAM"))/dsHost("addRamNum"))*dsHost("addRamCost")
if (CInt(Request("VHDValue"))>dsHost("Maxspace")) then	cloudAddmoney=cloudAddmoney+((Request("VHDValue")-dsHost("Maxspace"))/dsHost("addVHDNum"))*dsHost("addVHDCost")
if (CInt(Request("MbpsValue"))>dsHost("Maxmbps8")) then	cloudAddmoney=cloudAddmoney+((Request("MbpsValue")-dsHost("Maxmbps8"))/dsHost("addMbpsNum"))*dsHost("addMbpsCost")
If dsHost("CDNMaxMb")>0 then'如果限制流量
if (CInt(Request("NGBValue"))>dsHost("CDNMaxMb")) then	cloudAddmoney=cloudAddmoney+((Request("NGBValue")-dsHost("CDNMaxMb"))/1000/dsHost("addNGBNum"))*dsHost("addNGBCost")
End If
If dsHost("MaxIOPS")>0 then'如果限制IOPS
if (CInt(Request("IopsValue"))>dsHost("MaxIOPS")) then	cloudAddmoney=cloudAddmoney+((Request("IopsValue")-dsHost("MaxIOPS"))/dsHost("addIOPSNum"))*dsHost("addIOPSCost")
End if
if (CInt(Request("SNAValue"))>dsHost("MaxSNA")) then	cloudAddmoney=cloudAddmoney+((Request("SNAValue")-dsHost("MaxSNA"))/dsHost("addSnaNum"))*dsHost("addSnaCost")
if (CInt(Request("BakValue"))>dsHost("MaxBak")) then	cloudAddmoney=cloudAddmoney+((Request("BakValue")-dsHost("MaxBak"))/dsHost("addBakNum"))*dsHost("addBakCost")
if (CInt(Request("IPValue"))>dsHost("MaxIP")) then	cloudAddmoney=cloudAddmoney+((Request("IPValue")-dsHost("MaxIP"))/dsHost("addIpNum"))*dsHost("addIPCost")

cloudAddmoney=Clng(FormatNumber(cloudAddmoney,0,-1,0,0))'四舍五入

end If
end if

if not ef then
dim ef,ec,Userpaymoney,forpaytype,olduser_money,moneych,newuser_money,oldagn_money,mymoneych,newagn_money,Useragentname,agent_UP,agent_thisgree_cost,agent_this_name,logfor,pubisagnSer,AgnRunasUser
Userpaymoney=dsHost("usermoney")
forpayyear=Request ("year")
set RsUserds=ds
Useragentname=ds("agent1")
forpaytype="云服务器产品"
forpayid=dshost("id")
logfor="API区开通(云服务器名"&trim(vpsname)&")"
pubisagnSer=isAgnHostSer
AgnRunasUser=session("AgnRunasUser")
DoPayMoney
end if

if not ef then
dim Subject,body'send mail
vpsname=lcase(trim(vpsname))
dsph.AddNew
dsph("username")=ds ("username")
dsph("ServerlistID")=dsserver("id")
dsph("VPStype")=dshost("VPStype")
dsph("starttime")=starttime
dsph("endtime")=temptime
dsph("Cpunum")=dshost("Cpunum")
dsph("MaxRAM")=dshost("MaxRAM")
dsph("StartRam")=dshost("StartRam")
dsph("Maxmbps8")=dshost("Maxmbps8")
dsph("Minmbps8")=dshost("Minmbps8")
dsph("MaxCPU")=dshost("MaxCPU")
dsph("MaxSpace")=dshost("MaxSpace")
dsph("allowdomain")=dshost("allowdomain")
dsph("CDNMaxMb")=dshost("CDNMaxMb")
dsph("vpsname")=vpsname
dsph("VPSpassword")=VPSpassword
dsph("vhdpath")=replace(dsserver("homedir")&"\"&dsph("vpsname")&"."&dsserver("vhdtype"),"\\","\")
dsph("canbackupvhd")=dshost("canbackupvhd")
dsph("MaxIOPS")=dshost("MaxIOPS")
dsph("MaxIOPS2")=dshost("MaxIOPS2")
dsph("MaxIP")=dshost("MaxIP")
dsph("VLANid")=dshost("VLANid")
NetCardlimtype=dshost("NetCardlimtype")
'Response.write "test"
'Response.end

if dshost("cancloud")=true then'是弹性购买产品
dsph("Cpunum")=Request("CpuValue")
dsph("MaxRAM")=Request("RamValue")
dsph("MaxSpace")=Request("VHDValue")
dsph("Maxmbps8")=Request("MbpsValue")
dsph("MaxSNA")=Request("SNAValue")
dsph("MaxBak")=Request("BakValue")
dsph("MaxIP")=Request("IPValue")
If dsHost("MaxIOPS")>0 And Request("IopsValue")>0 Then dsph("MaxIOPS2")=Request("IopsValue")'只限制第二硬盘速度'就算用户选不限制产品默认值更优先
If dsHost("CDNMaxMb")>0 Then dsph("CDNMaxMb")=Request("NGBValue")*1000
dsph("FirstOSid")=Request("OSAllList")
end if

dsph("status")="准备开通"
if y=999 then
dsph("istest")="y"
else
dsph("istest")="n"
end if

if ds("agent1")<>"n" then
dsph("agent1")=ds("agent1")
else
dsph("agent1")="n"
end if


dsph("caninstallos")=dshost("caninstallos")
dsph("canbackupvhd")=dshost("canbackupvhd")


dsph("kefu")=ds("kefunow")

If RegExpTest("[^A-Za-z0-9]",Request("attach")) Then
isname=False
Else
dsph("Agnattach")=Request("attach")
End If

dsph.Update


'LOG
Set dsph = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_VPS where vpsname='"&vpsname&"'"
	dsph.Open Sql,conn,1,3
pdid=dsph("id")
Sql="UPDATE [FreeHost].[FreeHost_Moneylog] set pdid='"&dsph("id")&"' where pdid='"&session("moneylog_pdid")&"'"
    conn.Execute Sql

if Not dshost("isagentpd")=true Then 
'if dshost("canADSL")=True Then conn.Execute "UPDATE FreeHost.FreeHost_Product_VPS_ADSL SET  VPSID='"&dsph("id")&"' WHERE ID in (SELECT TOP (1) Id FROM FreeHost.FreeHost_Product_VPS_ADSL WHERE VPSID=0 ORDER BY orderbyid DESC);"
if dshost("canADSL")=True then
Set dsVPSADSL = Server.CreateObject("ADODB.Recordset")	
		Sql="Select top 1 * from FreeHost.FreeHost_Product_VPS_ADSL where ServerlistID="&dsserver("id")&" and VPSID=0 order by orderbyid desc"
		dsVPSADSL.Open Sql,conn,1,3
if Not dsVPSADSL.Eof then
dsVPSADSL("VPSID")=dsph("id")
If dsVPSADSL("VLANid")>0 Then dsph("VLANid")=dsVPSADSL("VLANid")
dsVPSADSL.update
dsVPSADSL.close
End If
End If

Set dsVPSip = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Product_VPS_IP where ServerlistID="&dsserver("id")&" and VPSID=0  and ipbyid=0 order by orderbyid desc"
		dsVPSip.Open Sql,conn,1,3
IP2="0"
IP2mask=0
IP2gateway=0
IP2dns1=0
IP2dns2=0
ipcount=0

havenextIP=false
Set dsVPSip2 = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Product_VPS_IP where ipbyid='"&dsVPSip("ID")&"' and VPSID=0 order by orderbyid desc"
		dsVPSip2.Open Sql,conn,1,3
If Not dsVPSip2.Eof Then
'有附属IP时
ip=dsVPSip("ip")
IPmask=dsVPSip("IPmask")
IPgateway=dsVPSip("IPgateway")
IPdns1=dsVPSip("IPdns1")
IPdns2=dsVPSip("IPdns2")
dsVPSip("VPSID")=dsph("id")
dsVPSip.update

IP2=dsVPSip2("IP")
IP2mask=dsVPSip2("IPmask")
IP2gateway=dsVPSip2("IPgateway")
IP2dns1=dsVPSip2("IPdns1")
IP2dns2=dsVPSip2("IPdns2")
dsVPSip2("VPSID")=dsph("id")
dsVPSip2.update
dsVPSip2.close
havenextIP=true
end if

'dsVPSip.movefirst
If havenextIP=false then
While Not dsVPSip.Eof
If ipcount<dsph("MaxIP") then'如果选中2014
dsVPSip("VPSID")=dsph("id")
ipcount=ipcount+1
If IP2="0" And ip<>"" then
IP2=dsVPSip("IP")
IP2mask=dsVPSip("IPmask")
IP2gateway=dsVPSip("IPgateway")
IP2dns1=dsVPSip("IPdns1")
IP2dns2=dsVPSip("IPdns2")
End if
If IP="" then
IP=dsVPSip("IP")
IPmask=dsVPSip("IPmask")
IPgateway=dsVPSip("IPgateway")
IPdns1=dsVPSip("IPdns1")
IPdns2=dsVPSip("IPdns2")
End if
dsVPSip.update
End If
dsVPSip.movenext
Wend
dsVPSip.movefirst
End If

End if



'调用开通function

URL=dsserver("site")
FreeHostsharekey=dsserver("FreeHostsharekey")
username=dsph ("vpsname")
username=dsph ("username")
homedir=dsserver("homedir")
starttime=dsph("starttime")
endtime=dsph("endtime")
type1=dsph("VPStype")


MAXcpu=dsph("MAXCPU")
CpuNum=dsph("CpuNum")
MAXRam=dsph("MAXRam")
StartRam=dsph("StartRam")
Maxmbps8=dsph("Maxmbps8")
Minmbps8=dsph("Minmbps8")

MAXspace=dsph("MAXspace")
vhdpath=dsph("vhdpath")
MaxIOPS=dsph("MaxIOPS")
MaxIOPS2=dsph("MaxIOPS2")
VLANid=dsph("VLANid")
'IP=dsph("ip")'分配的IP

'if y=999 and Application("testsame")<>true then'如果是试用
'MaxConnections=Application("MaxConnections")
'spacelimit=Application("spacelimit")
'MaxBandwidth=Application("MaxBandwidth")
'end if


If Application("smsVPSopen")=True And (ds("agent1")="n" Or Application("agncangetsms")=True) Then
Dim username
username=ds("username")
mobi=Trim(ds("mobi"))
smsContent=Application("smsVPSopencon")
'恭喜！您的邮箱产品已开通了！用户名 ：#用户名#产品类型 ：#产品类型#到期时间是 ：#到期时间#您的帐户余款是 ：#金额# 元
smsContent=Replace(smsContent,"#用户名#",ds("username"))
smsContent=Replace(smsContent,"#产品类型#",dsph("VPStype"))
smsContent=Replace(smsContent,"#到期时间#",formatdatetime(dsph("endtime"),1))
smsContent=Replace(smsContent,"#金额#",newuser_money)
ret=SendSms(mobi,smsContent,0,Now,"")
End if

'dim Subject,body
emailVPSopen
ret=Send2Mail(conn,ds("username"),Subject,body)

'dsph.close


Application.lock
Application("hostoptime")=now
Application("hostoplock")=true
Application.unlock
varcon="makevps "&username&" "&vpsname&" "&CpuNum&" "&MAXcpu&" "&MAXRam&" "&MAXspace&" "&vhdpath&" "&StartRam
If dsserver("OStype")="Windows Server 2012" Then varcon=varcon&" 0 "&Maxmbps8*1024*1024
If dsserver("OStype")="Windows Server 2012R2" Or dsserver("OStype")="Windows Server 2016" Then varcon=varcon&" 0 "&Maxmbps8*1024*1024&" "&dsserver("SwitchName")&" "&StartRam&" "&Replace(vhdpath,".vhd","-2.vhd")&" 10 "&(MAXspace-10)&" defaultClusterNameby7i24 "&MaxIOPS&" "&MaxIOPS2&" NULLswitchname "&VLANid&" "&NetCardlimtype
If dsserver("IsCluster")=true Then varcon=Replace(varcon,"defaultClusterNameby7i24",dsserver("ClusterName"))
If Not isnull(dsserver("SwitchName2")) Then varcon=Replace(varcon,"NULLswitchname",dsserver("SwitchName2"))

temp=VPSOP(URL,FreeHostsharekey,varcon,0)

If Not isnull(dsserver("SwitchName2")) Then '处理switch
If InStr(temp,vbcrlf&vbcrlf&vbcrlf&"SwitchName : ") Then
arysw=Split(temp,vbcrlf&vbcrlf&vbcrlf&"SwitchName : ")
arysw2=Split(arysw(1),vbcrlf&vbcrlf&vbcrlf)
arysw3=Split(arysw2(0),vbcrlf)
If dsserver("SwitchName")=arysw3(0) Then
dsph("MAC")=replace(arysw3(1),"MacAddress : ","")
dsph("MAC2")=replace(arysw3(4),"MacAddress : ","")
Else
dsph("MAC")=replace(arysw3(4),"MacAddress : ","")
dsph("MAC2")=replace(arysw3(1),"MacAddress : ","")
End If
End if
End if'完成switch

dsph.update

Application.lock
Application("hostoplock")=false
Application.unlock

Sql="update FreeHost.FreeHost_Product_VPS set status='等待安装操作系统' where vpsname='"&vpsname&"'"
    conn.Execute Sql

FreeHost_oplog session("username"),"信息","API区，用户开通云服务器产品:("&vpsname&") "&temp


conn.close
Set conn=Nothing

response.write "ret=ok&vpsname="&vpsname&"&vpspassword="&VPSpassword&"&endtime="&year(endtime)&"-"&month(endtime)&"-"&day(endtime)&" "&hour(endtime)&":"&minute(endtime)&":"&second(endtime)&"&cpurl="&Application("VPScpdomain")&"&attach="&request("attach")
End if'正常开通



End if'开通操作.
End If



If Not ef Then
If Request("action")="renew" then'续费接口--------------------------------------------------------------------------------------

session("username")=Sagent("username")
session("agentname")=Sagent("username")
session("agenttype")=Sagent("agenttype")
session("atspace")="API区"
if session("agentname")=session("username") then
session("AgnRunasUser")=True
else
session("AgnRunasUser")=false
end if
'登陆API区
vpsname=LCase(Trim(Request ("vpsname")))

If session("username")="" Then
	ef=True
	ec="请您先登陆API区，再续费，在主机面板中不能续费。也有可能是超时造成了出错。"
ElseIf IsNumeric(vpsname)  Then
	ef=True
	ec="VPS用户名不能是纯数字！"
ElseIf not IsNumeric(Request ("year"))  Then
	ef=True
	ec="时间应该是数字！"
ElseIf vpsname="" Then
	ef=True
	ec="请设置VPS用户名！"
ElseIf Trim(LCase(vpsname))="self" or Trim(LCase(vpsname))="adm" or Trim(LCase(vpsname))="admin" or Trim(LCase(vpsname))="sefreehost" Or Trim(LCase(vpsname))="administrator" Or Trim(LCase(vpsname))="freehost" Or Trim(LCase(vpsname))="proxy" Then
	ef=True
	ec="您输入的VPS用户名为保留用户名，请换一个VPS用户名"
ElseIf RegExpTest("[^A-Za-z0-9]",vpsname) Then
	ef=True
	ec="VPS用户名中有非法字符！"
end If


if not ef then
Set conn=Server.CreateObject("ADODB.Connection")
	conn.Open Application("FreeHostDBlink")
	Set ds = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_USER where username='"&session("username")&"'"
		ds.Open Sql,conn,1,3
if ds.eof then
	ef=True
	ec="用户不存在!"
end if
end if

'if not ef then
'if ds("password")<>session("password") then
'	ef=True
'	ec="密码不正确!"
'end if
'end if

if not ef then
	Set dsph = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_VPS where username='"&session("username")&"' and vpsname='"&vpsname&"'"
			if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
	dsph.Open Sql,conn,1,3
if dsph.Eof then
	ef=True
	ec="云服务器名不存在！"
end if
end if

if not ef then
	Set dshost = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_VPSProductlist where VPStype='"&dsph ("VPStype")&"'"
		if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
	dshost.Open Sql,conn,1,1
if dshost.Eof then
	ef=True
	ec="产品类型不存在！"
Else
'读取月付
PAY_Day=dshost("PAY_Day")
PAY_Week=dshost("PAY_Week")
PAY_Month=dshost("PAY_Month")
PAY_Season=dshost("PAY_Season")
PAY_halfyear=dshost("PAY_halfyear")
PAY_Nextyear=dshost("PAY_Nextyear")
if dsph("istest")="y" then PAY_Nextyear="1"
PAY_2year=dshost("PAY_2year")
PAY_3year=dshost("PAY_3year")
PAY_4year=dshost("PAY_4year")
PAY_5year=dshost("PAY_5year")
end if
end if

'if not ef then
'if dshost("isbyhost")="y" then
'	ef=True
'	ec="赠送的类型不能延期，只能由主产品延期。"
'end if
'end if


if not ef then
If Request ("year")<>PAY_Day and Request ("year")<>PAY_Week and Request ("year")<>PAY_Month and Request ("year")<>PAY_Season  and Request ("year")<>PAY_halfyear and Request ("year")<>PAY_Nextyear and Request ("year")<>PAY_2year and Request ("year")<>PAY_3year and Request ("year")<>PAY_4year and Request ("year")<>PAY_5year Then
	ef=True'不包含一年,不包含试用,只有续费
	ec="请选择时间！"
end If
end If

if not ef then
if Request ("year")=PAY_Day and dshost("canday")="n" then
	ef=True
	ec="很抱歉,本产品不可以日付！"
end if
end if

if not ef then
if Request ("year")=PAY_Week and dshost("canweek")="n" then
	ef=True
	ec="很抱歉,本产品不可以周付！"
end if
end if

if not ef then
if Request ("year")=PAY_Month and dshost("canmonth")="n" then
	ef=True
	ec="很抱歉,本产品不可以月付！"
end if
end if

if not ef then
if Request ("year")=PAY_Season and dshost("canseason")="n" then
	ef=True
	ec="很抱歉,本产品不可以季付！"
end if
end if
if not ef then
if Request ("year")=PAY_halfyear and dshost("canhalfyear")="n" then
	ef=True
	ec="很抱歉,本产品不可以半年付！"
end if
end if

if not ef Then
if dshost("cancloud")=true then'是弹性购买产品
'Dim cloudAddmoney
cloudAddmoney=0
if (dsph("CpuNum")>dsHost("CpuNum")) then	cloudAddmoney=cloudAddmoney+((dsph("CpuNum")-dsHost("CpuNum"))/dsHost("addCpuNum"))*dsHost("addCpuCost")
if (dsph("MAXRAM")>dsHost("MAXRAM")) then	cloudAddmoney=cloudAddmoney+((dsph("MAXRAM")-dsHost("MAXRAM"))/dsHost("addRamNum"))*dsHost("addRamCost")
if (dsph("Maxspace")>dsHost("Maxspace")) then	cloudAddmoney=cloudAddmoney+((dsph("Maxspace")-dsHost("Maxspace"))/dsHost("addVHDNum"))*dsHost("addVHDCost")
if (dsph("Maxmbps8")>dsHost("Maxmbps8")) then	cloudAddmoney=cloudAddmoney+((dsph("Maxmbps8")-dsHost("Maxmbps8"))/dsHost("addMbpsNum"))*dsHost("addMbpsCost")
If dsHost("CDNMaxMb")>0 then'如果限制流量
if (dsph("CDNMaxMb")>dsHost("CDNMaxMb")) then	cloudAddmoney=cloudAddmoney+((dsph("CDNMaxMb")-dsHost("CDNMaxMb"))/1000/dsHost("addNGBNum"))*dsHost("addNGBCost")
End If
If dsHost("MaxIOPS")>0 then'如果限制IOPS
if (dsph("MaxIOPS")>dsHost("MaxIOPS")) then	cloudAddmoney=cloudAddmoney+((dsph("MaxIOPS")-dsHost("MaxIOPS"))/dsHost("addIOPSNum"))*dsHost("addIOPSCost")
End if
if (dsph("MaxSNA")>dsHost("MaxSNA")) then	cloudAddmoney=cloudAddmoney+((dsph("MaxSNA")-dsHost("MaxSNA"))/dsHost("addSnaNum"))*dsHost("addSnaCost")
if (dsph("MaxBak")>dsHost("MaxBak")) then	cloudAddmoney=cloudAddmoney+((dsph("MaxBak")-dsHost("MaxBak"))/dsHost("addBakNum"))*dsHost("addBakCost")
if (dsph("MaxIP")>dsHost("MaxIP")) then	cloudAddmoney=cloudAddmoney+((dsph("MaxIP")-dsHost("MaxIP"))/dsHost("addIpNum"))*dsHost("addIPCost")
cloudAddmoney=Clng(FormatNumber(cloudAddmoney,0,-1,0,0))'四舍五入
end If
End if

if not ef then
	Set dsserver = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Servervpslist where id='"&dsph ("ServerlistID")&"'"
	dsserver.Open Sql,conn,1,1
if dsserver.Eof then
	ef=True
	ec="服务器不存在！"
else
if dsserver("agent1")="n" then
isAgnSqlSer=false
else
isAgnSqlSer=true
end if
end if
end if

if not ef then
'dim starttime,Dim temptime',pdtype
Dim isAgnSqlSer
isAgnHostSer=isAgnSqlSer
starttime=dsph("endtime")
pdtype=dsph ("VPStype")
'结束时间
oldtime=dsph("endtime")
if dsph("istest")="y" then  oldtime=now
y=Request ("year")
if y=PAY_Day then temptime=dateadd("s",86400,oldtime)
if y=PAY_Week then temptime=dateadd("n",10080,oldtime)
if y=PAY_Nextyear then temptime=dateadd("yyyy",1,oldtime)
if y=PAY_2year then temptime=dateadd("yyyy",2,oldtime)
if y=PAY_3year then temptime=dateadd("yyyy",3,oldtime)
if y=PAY_4year then temptime=dateadd("yyyy",4,oldtime)
if y=PAY_5year then temptime=dateadd("yyyy",5,oldtime)
if y=PAY_Month then temptime=dateadd("y",31,oldtime)
if y=PAY_Season then temptime=dateadd("y",92,oldtime)
if y=PAY_halfyear then temptime=dateadd("y",182,oldtime)
end if



if not ef then
'dim ef,ec,Userpaymoney,forpaytype,olduser_money,moneych,newuser_money,oldagn_money,mymoneych,newagn_money,Useragentname,agent_UP,agent_thisgree_cost,agent_this_name,logfor,pubisagnSer,AgnRunasUser
Dim isRepaymoney,Repaymoney
Userpaymoney=dsHost("usermoney")
forpayyear=Request ("year")
set RsUserds=ds
Useragentname=ds("agent1")
forpaytype="云服务器产品"
forpayid=dshost("id")
logfor="API区延期(云服务器名"&dsph ("vpsname")&")"
pubisagnSer=isAgnSqlSer
AgnRunasUser=session("AgnRunasUser")
if not isnull(dsph("repaymoney")) and not isAgnHostSer then'管理员单独设置续费价格
logfor="API区延期(单独价格)(云服务器名"&dsph("vpsname")&")"
Repaymoney=dsph("repaymoney")
isRepaymoney=true
else
isRepaymoney=false
end if
DoPayMoney
Sql="UPDATE [FreeHost].[FreeHost_Moneylog] set pdid='"&dsph("id")&"' where pdid='"&session("moneylog_pdid")&"'"
    conn.Execute Sql
end if


if not ef then


'更新产品类别
'dsph ("Hosttype")=dshost("Hosttype")
'if dsph("istest")="y" then
dsph ("endtime")=temptime
dsph("istest")="n"
dsph("emailstatus")=Null
dsph("smsstatus")=null
'end if
'dsph.update

endtime=dsph ("endtime")



dsph("status")="正常"
dsph.update
dsph.close


conn.close
Set conn=Nothing

response.write "ret=ok&vpsname="&vpsname&"&endtime="&year(endtime)&"-"&month(endtime)&"-"&day(endtime)&" "&hour(endtime)&":"&minute(endtime)&":"&second(endtime)&"&cpurl="&Application("VPScpdomain")&"&attach="&request("attach")
End if'正常开通


End if'续费操作.
End If


If Not ef Then
If Request("action")="getinfo" then'查询接口--------------------------------------------------------------------------------------

session("username")=Sagent("username")
session("agentname")=Sagent("username")
session("agenttype")=Sagent("agenttype")
session("atspace")="API区"
if session("agentname")=session("username") then
session("AgnRunasUser")=True
else
session("AgnRunasUser")=false
end if
'登陆API区
vpsname=LCase(Trim(Request ("vpsname")))

If session("username")="" Then
	ef=True
	ec="请您先登陆API区，再续费，在主机面板中不能续费。也有可能是超时造成了出错。"
ElseIf IsNumeric(vpsname)  Then
	ef=True
	ec="VPS用户名不能是纯数字！"
ElseIf not IsNumeric(Request ("year"))  Then
	ef=True
	ec="时间应该是数字！"
ElseIf vpsname="" Then
	ef=True
	ec="请设置VPS用户名！"
ElseIf Trim(LCase(vpsname))="self" or Trim(LCase(vpsname))="adm" or Trim(LCase(vpsname))="admin" or Trim(LCase(vpsname))="sefreehost" Or Trim(LCase(vpsname))="administrator" Or Trim(LCase(vpsname))="freehost" Or Trim(LCase(vpsname))="proxy" Then
	ef=True
	ec="您输入的VPS用户名为保留用户名，请换一个VPS用户名"
ElseIf RegExpTest("[^A-Za-z0-9]",vpsname) Then
	ef=True
	ec="VPS用户名中有非法字符！"
end If


if not ef then
Set conn=Server.CreateObject("ADODB.Connection")
	conn.Open Application("FreeHostDBlink")
	Set ds = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_USER where username='"&session("username")&"'"
		ds.Open Sql,conn,1,3
if ds.eof then
	ef=True
	ec="用户不存在!"
end if
end if

'if not ef then
'if ds("password")<>session("password") then
'	ef=True
'	ec="密码不正确!"
'end if
'end if

if not ef then
	Set dsph = Server.CreateObject("ADODB.Recordset")	
'	Sql="Select * from FreeHost.FreeHost_Product_VPS where vpsname='"&vpsname&"' and username<>'test'"
	Sql="Select * from FreeHost.FreeHost_Product_VPS where username='"&session("username")&"' and vpsname='"&vpsname&"'"
			if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
	dsph.Open Sql,conn,1,3
if dsph.Eof then
	ef=True
	ec="云服务器名不存在！"
end if
end if

if not ef then
	Set dshost = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_VPSProductlist where VPStype='"&dsph ("VPStype")&"'"
		if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
	dshost.Open Sql,conn,1,1
if dshost.Eof then
	ef=True
	ec="产品类型不存在！"
Else
'读取月付
PAY_Day=dshost("PAY_Day")
PAY_Week=dshost("PAY_Week")
PAY_Month=dshost("PAY_Month")
PAY_Season=dshost("PAY_Season")
PAY_halfyear=dshost("PAY_halfyear")
PAY_Nextyear=dshost("PAY_Nextyear")
if dsph("istest")="y" then PAY_Nextyear="1"
PAY_2year=dshost("PAY_2year")
PAY_3year=dshost("PAY_3year")
PAY_4year=dshost("PAY_4year")
PAY_5year=dshost("PAY_5year")
end if
end if

if not ef then
	Set dsserver = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Servervpslist where id='"&dsph ("ServerlistID")&"'"
	dsserver.Open Sql,conn,1,1
if dsserver.Eof then
	ef=True
	ec="服务器不存在！"
Else
isONEip=dsserver("isONEip")	
if dsserver("agent1")="n" then
isAgnSqlSer=false
else
isAgnSqlSer=true
end if
end if
end if



if not ef then

vpspassword=dsph ("vpspassword")
status=dsph ("status")
endtime=dsph ("endtime")
ip=""
	Set dsX = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Product_VPS_IP where VPSID="&dsph("id")
		dsX.Open Sql,conn,1,1
while not dsX.eof
ip=dsX("IP")&"|"&IP

if dsX("portstart")>0 then
if isONEip=true then
	Set dsPORTserver = Server.CreateObject("ADODB.Recordset")	
Sql="Select * from FreeHost.FreeHost_ServerPORTlist where id='"&dsserver("useportserver")&"'"
		dsPORTserver.Open Sql,conn,1,1
If Not dsPORTserver.eof then
ip=dsPORTserver("IP")
end if
end If
End if

dsX.movenext
wend

'dsph.update
dsph.close


conn.close
Set conn=Nothing

If Right(ip,1)="|" Then ip=Left(IP,Len(ip)-1)

response.write "ret=ok&vpsname="&vpsname&"&vpspassword="&vpspassword&"&ip="&ip&"&status="&status&"&endtime="&year(endtime)&"-"&month(endtime)&"-"&day(endtime)&" "&hour(endtime)&":"&minute(endtime)&":"&second(endtime)&"&cpurl="&Application("VPScpdomain")&"&attach="&request("attach")
End if'正常开通


End if'查询操作.
End If


If Not ef Then
If Request("action")="update" then'升级接口-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

session("username")=Sagent("username")
session("agentname")=Sagent("username")
session("agenttype")=Sagent("agenttype")
session("atspace")="API区"
if session("agentname")=session("username") then
session("AgnRunasUser")=True
else
session("AgnRunasUser")=false
end if
'登陆API区
vpsname=LCase(Trim(Request ("vpsname")))

If session("username")="" Then
	ef=True
	ec="请您先登陆API区，再续费，在主机面板中不能续费。也有可能是超时造成了出错。"
ElseIf IsNumeric(vpsname)  Then
	ef=True
	ec="VPS用户名不能是纯数字！"
ElseIf not IsNumeric(Request ("productid"))  Then
	ef=True
	ec="productid应该是数字！"
ElseIf vpsname="" Then
	ef=True
	ec="请设置VPS用户名！"
ElseIf Trim(LCase(vpsname))="self" or Trim(LCase(vpsname))="adm" or Trim(LCase(vpsname))="admin" or Trim(LCase(vpsname))="sefreehost" Or Trim(LCase(vpsname))="administrator" Or Trim(LCase(vpsname))="freehost" Or Trim(LCase(vpsname))="proxy" Then
	ef=True
	ec="您输入的VPS用户名为保留用户名，请换一个VPS用户名"
ElseIf RegExpTest("[^A-Za-z0-9]",vpsname) Then
	ef=True
	ec="VPS用户名中有非法字符！"
ElseIf Request ("productid")="" Then
	ef=True
	ec="请选择产品！"
end If

if not ef then
Set conn=Server.CreateObject("ADODB.Connection")
	conn.Open Application("FreeHostDBlink")
	Set ds = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_USER where username='"&session("username")&"'"
		ds.Open Sql,conn,1,3
if ds.eof then
	ef=True
	ec="用户不存在!"
end if
end if

'if not ef then
'if ds("password")<>session("password") then
'	ef=True
'	ec="密码不正确!"
'end if
'end if

if not ef then
	Set dshost = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_VPSProductlist where id='"&Request ("productid")&"'"
			if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
	dshost.Open Sql,conn,1,1
if dshost.Eof then
	ef=True
	ec="产品不存在！"
end if
end if

if not ef then
if Not IsNull(dshost("onlyusebyagn")) Then
If session("agentname")<>dshost("onlyusebyagn") then
	ef=True
	ec="本产品只允许特定的代理使用！"
End if
end if
end If

if not ef then
	Set dsph = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_VPS where username='"&session("username")&"' and vpsname='"&vpsname&"'"
			if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
	dsph.Open Sql,conn,1,3
if dsph.Eof then
	ef=True
	ec="要升级的云服务器产品不存在！"
'else
 '  if Not IsNull(dsph("byhost")) then
  ' 	ef=True
'	ec="赠送的不能升级！"
 '  end if
end if
end if

if not ef then
if not isnull(dsph("repaymoney")) then
	ef=True
	ec="由于此产品被管理员设置了单独的续费价格，因此，请您联系管理员帮您手工升级。"
end if
end if

if not ef then
	Set dsserver = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_ServerVPSlist where id='"&dsph ("ServerlistID")&"'"
	dsserver.Open Sql,conn,1,1
if dsserver.Eof then
	ef=True
	ec="服务器不存在！"
Else
'dim isAgnHostSer
if dsserver("agent1")="n" Then
isAgnHostSer=false
else
isAgnHostSer=true
end if
end if
end If

if not ef then
if dshost("cancloud")=true then'是弹性购买产品
	ef=True
	ec="弹性购买产品暂时不能使用本功能。"
end if
end If

if not ef then
if ds("agent1")<>"n" then

if not ef then
  Set dsagent = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_AGENT where username='"&ds("agent1")&"'"
		if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
		dsagent.Open Sql,conn,1,3
If dsagent.Eof Then
		ef=True
		ec="代理会员名不存在或密码出错了！"
end if
end if

if not ef then
	Set dsagnettype = Server.CreateObject("ADODB.Recordset")	
Sql="Select * from FreeHost.FreeHost_Agent_pers where agenttype='"& session("agenttype")&"'"
		dsagnettype.Open Sql,conn,1,1
if dsagnettype.Eof then
	ef=True
	ec="代理类型折扣为空，请先设置它！"
end if
end if

if not ef then
	Set dsagnpri = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Agent_price where prtype='云服务器产品' and username='"&session ("agentname")&"' and id="&dshost("id")&""
		dsagnpri.Open Sql,conn,1,1
if dsagnpri.eof then
myrealcost=dshost("usermoney")*dsagnettype("pers")
else
myrealcost=dsagnpri("usermoney")
end if
dsagnpri.close

	Set dsendmoney = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Agent_user_price where prtype='云服务器产品' and username='"&session("agentname")&"' and id="&dshost("id")&""
		dsendmoney.Open Sql,conn,1,1
if dsendmoney.eof then
usermoney=dshost("usermoney")
else
usermoney=dsendmoney("usermoney")
end if
dsendmoney.close
end if
end if
end if

if not ef then
if dsph("istest")="y" then
	ef=True
	ec="试用状态的产品必须延期后成为正式产品才可以升级到高级别！"
end if
end if

if not ef then
	Set dshostold = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_VPSProductlist where VPStype='"&dsph ("VPStype")&"'"
	dshostold.Open Sql,conn,1,1
if dshostold.Eof then
	ef=True
	ec="旧产品不存在！"
end if
end if

if not ef then
if (dshostold("updatebytypeid")="y" and dshost("typeid")=dshostold("typeid")) or isnull(dshostold("updatebytypeid")) or dshostold("updatebytypeid")="n" then
else
	ef=True
	ec="此产品只能在同一类型号中升级！类型号为："&typeid
end if
end if

if not ef then
if ds("agent1")<>"n" then
if not ef then
	Set olddsagnpri = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Agent_price where prtype='云服务器产品' and username='"&session ("agentname")&"' and id="&dshostold("id")&""
		olddsagnpri.Open Sql,conn,1,1
if olddsagnpri.eof then
oldmyrealcost=dshostold("usermoney")*dsagnettype("pers")
else
oldmyrealcost=olddsagnpri("usermoney")
end if
olddsagnpri.close

	Set olddsendmoney = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_Agent_user_price where prtype='云服务器产品' and username='"&session("agentname")&"' and id="&dshostold("id")&""
		olddsendmoney.Open Sql,conn,1,1
'response.write sql
if olddsendmoney.eof then
oldusermoney=dshostold("usermoney")
else
oldusermoney=olddsendmoney("usermoney")
end if
olddsendmoney.close
end if
end if
end if

if not ef then
if dsph("istest")<>"y" then
if dshost("VPStype")=dsph("VPStype") then
	ef=True
	ec="同级别不可以升级!"
end if
end if
end if

if not ef then
if dshostold("usermoney")>dshost("usermoney") then
	ef=True
	ec="不能向低别升级!"
end if
end if

if not ef Then
daytoend=DateDiff("d",NOW,dsph("endtime"))
if daytoend<0 Then
	ef=True
	ec="您原来的产品已过期了，请您先延期原产品后才能升级。"
End If
End If

if not ef then
if dsph("istest")="y" then  daytoend=0'试用期的产品也要设置为0

if ds("agent1")="n" then'不是代理也不是代理名下用户
'不是代理也不是代理名下用户
oldusermoneyleft=dshostold("usermoney")/365*daytoend
if oldusermoneyleft<0 then oldusermoneyleft=0'防止出现特殊情况
'后面的天数用新产品所用的钱
newtypeusemoneyforuser=dshost("usermoney")/365*daytoend
oldusermoneytake=newtypeusemoneyforuser-oldusermoneyleft
else'是代理名下用户或代理自己操作自己

oldusermoneyleft=oldusermoney/365*daytoend

'response.write "(旧产品类型代理公开售价/365 x未到期天数) <br>"
'response.write oldusermoney&"<br>"
'response.write oldusermoney/365&"<br>"
'response.write "还有"&daytoend&"天<br>"
'response.write "为:"&oldusermoneyleft&"<br>"

if oldusermoneyleft<0 then oldusermoneyleft=0'防止出现特殊情况
'后面的天数用新产品所用的钱
newtypeusemoneyforuser=usermoney/365*daytoend

'response.write "(新产品类型代理公开售价/365 x未到期天数) <br>"
'response.write usermoney&"<br>"
'response.write usermoney/365&"<br>"
'response.write "还有"&daytoend&"天<br>"
'response.write "为:"&newtypeusemoneyforuser&"<br>"

oldusermoneytake=newtypeusemoneyforuser-oldusermoneyleft

'response.write "用户升级需要支付的金额"
'response.write oldusermoneytake&"<br>"

if session("AgnRunasUser") Then oldusermoneytake=0'代理自己登陆用户区操作自己用户角色为0

'计算代理角色成本
oldAgentrmoneyleft=oldmyrealcost/365*daytoend

'response.write "<br><br>(旧产品类型代理成本售价/365 x未到期天数)  <br>"
'response.write oldmyrealcost&"<br>"
'response.write oldmyrealcost/365&"<br>"
'response.write "还有"&daytoend&"天<br>"
'response.write "为:"&oldAgentrmoneyleft&"<br>"


if oldAgentrmoneyleft<0 then oldAgentrmoneyleft=0'防止出现特殊情况--代理
'后面的天数用新产品所用的钱--代理
newtypeAgentmoneyforuser=myrealcost/365*daytoend


'response.write "(新产品类型代理成本售价/365 x未到期天数)  <br>"
'response.write myrealcost&"<br>"
'response.write myrealcost/365&"<br>"
'response.write "还有"&daytoend&"天<br>"
'response.write "为:"&newtypeAgentmoneyforuser&"<br>"

oldAgentrmoneytake=newtypeAgentmoneyforuser-oldAgentrmoneyleft

'response.write "代理的升级成本价"
'response.write oldAgentrmoneytake&"<br>"

If isAgnHostSer Then oldAgentrmoneytake=0'独立模式服务器

end If'结束代理判断
end If


if not ef Then
'oldusermoneytake=round(oldusermoneytake,2)'处理格式
oldusermoneytake=round(oldusermoneytake,0)'处理格式
if oldusermoneytake>ds("usermoney") then'检查用户角色上是否有足够的钱
	ef=True
	ec="很抱歉,API上级的余额不足！升级产品所需金额为:"&oldusermoneytake&"元，当前您的余额为："&ds("usermoney")&"元"
End If
End If

if not ef Then
if ds("agent1")<>"n" then'代理自己所需成本检查
'oldAgentrmoneytake=round(oldAgentrmoneytake,2)'处理格式
oldAgentrmoneytake=round(oldAgentrmoneytake,0)'处理格式
if oldAgentrmoneytake-oldusermoneytake>dsagent("usermoney") then
	ef=True
	ec="很抱歉,API上级上级余额不足！升级产品所需金额为:"&oldAgentrmoneytake-oldusermoneytake&"元，可操作的余额为："&dsagent("usermoney")&"元"
End If
End If
End if


if not ef Then
moneych=oldusermoneytake
if session("AgnRunasUser") Then moneych=oldAgentrmoneytake
If moneych<0 Then
	ef=True
	ec="很抱歉,API上级余额为负("&moneych&"元)！有可能是这两个产品成本价一样造成,请联系管理员处理."
End if
End if

if not ef then

'开始扣费
olduser_money=ds("usermoney")
ds("usermoney") =ds("usermoney") -oldusermoneytake
newuser_money=ds("usermoney")
moneych=-oldusermoneytake
ds.update'扣用户角色费用

if ds("agent1")<>"n" then
oldagn_money=dsagent("usermoney")
dsagent("usermoney")=dsagent("usermoney")-oldAgentrmoneytake+oldusermoneytake
mymoneych=-oldAgentrmoneytake+oldusermoneytake
newagn_money=dsagent("usermoney")
dsagent.update
'response.write "新产品价格："&usermoney&" 旧产品价格："&oldusermoney&" 用户原："&ds("usermoney")&" 代理成本折算旧："&oldRmoney&" 用户折算旧："&oldmoney&" tem："&(usermoney-oldmoney)&" tem2："&(myrealcost-oldRmoney)
'response.end
end if


'dim starttime,temptime,pdtype
pdtype="原类型："&dsph ("VPStype")&" 新类型："&dshost("VPStype")

'更新产品类别
dsph ("VPStype")=dshost("VPStype")

dsph("MAXCPU")=dshost("MAXCPU")
dsph("CpuNum")=dshost("CpuNum")
dsph("MAXRam")=dshost("MAXRam")
dsph("StartRam")=dshost("StartRam")
dsph("Maxmbps8")=dshost("Maxmbps8")
dsph("Minmbps8")=dshost("Minmbps8")


dsph("MAXspace")=dshost("MAXspace")

dsph("allowdomain")=dshost("allowdomain")
dsph("CDNMaxMb")=dshost("CDNMaxMb")


dsph("status")="正常"
starttime=dsph ("endtime")
temptime=dsph ("endtime")
dsph.update

'isAgnHostSer=isAgnSqlSer
logfor="用户区升级VPS:"&dsph("vpsname")
if session("AgnRunasUser")=false then 
if isAgnHostSer then logfor="自动开通升级云服务器产品独立模式"
FreeHost_moneyNewlog conn,"y","n","n","n","n",isAgnHostSer,-moneych,dsph ("username"),dsph ("username"),dsph ("username"),olduser_money,newuser_money,agnusername,agnStmoney,agnEndmoney,supusername,logfor,logfor,"云服务器产品",dsph("id"),pingzheng,type1,bank,haveget,havecheck
if ds("agent1")<>"n" then FreeHost_moneyNewlog conn,"n","y","n","n","y",isAgnHostSer,mymoneych,dsph ("agent1"),dsph ("username"),norusername,norStmoney,norEndmoney,dsph ("agent1"),oldagn_money,newagn_money,supusername,logfor&"代理收入",logfor&"代理收入","云服务器产品",dsph("id"),pingzheng,type1,bank,haveget,havecheck
else
FreeHost_moneyNewlog conn,"n","y","n","n","n",isAgnHostSer,-mymoneych,dsph ("username"),dsph ("username"),norusername,norStmoney,norEndmoney,dsph ("username"),oldagn_money,newagn_money,supusername,logfor&"扣代理成本",logfor&"扣代理成本","云服务器产品",dsph("id"),pingzheng,type1,bank,haveget,havecheck
olduser_money=oldagn_money
moneych=newagn_money-oldagn_money
newuser_money=newagn_money
end if


URL=dsserver("site")
FreeHostsharekey=dsserver("FreeHostsharekey")
vpsname=dsph ("vpsname")
username=dsph ("username")
homedir=dsserver("homedir")
starttime=dsph("starttime")
endtime=dsph("endtime")
type1=dsph("VPStype")

MAXcpu=dsph("MAXCPU")
CpuNum=dsph("CpuNum")
MAXRam=dsph("MAXRam")
StartRam=dsph("StartRam")
Maxmbps8=dsph("Maxmbps8")
Minmbps8=dsph("Minmbps8")

MAXspace=dsph("MAXspace")
vhdpath=dsph("vhdpath")
MaxIOPS=dsph("MaxIOPS")
MaxIOPS2=dsph("MaxIOPS2")

varcon="modi "&username&" "&vpsname&" "&CpuNum&" "&MAXcpu&" "&MAXRam&" "&MAXspace&" "&vhdpath&" "&StartRam
If dsserver("OStype")="Windows Server 2012" Then varcon=varcon&" 0 "&Maxmbps8*1024*1024
If dsserver("OStype")="Windows Server 2012R2" Or dsserver("OStype")="Windows Server 2016" Then varcon=varcon&" 0 "&Maxmbps8*1024*1024&" "&dsserver("SwitchName")&" "&StartRam&" "&Replace(vhdpath,".vhd","-2.vhd")&" 10 "&(MAXspace-10)&" defaultClusterNameby7i24 "&MaxIOPS&" "&MaxIOPS2&" "&dsHost("NetCardlimtype")&" "&dsserver("SwitchName2")
If dsserver("IsCluster")=true Then varcon=Replace(varcon,"defaultClusterNameby7i24",dsserver("ClusterName"))
'modi test 1 10 1024 90 c:\vps\hjkhjh.vhd 
temp=VPSOP(URL,FreeHostsharekey,varcon,0)'0=now

'Response.Write varcon
'Response.Write temp

FreeHost_oplog session("username"),"信息","用户自己升级，云服务器名:"&vpsname&" 返回信息："&temp



response.write "ret=ok&vpsname="&vpsname&"&statue="&dsph("status")&"&endtime="&year(temptime)&"-"&month(temptime)&"-"&day(temptime)&"&cpurl="&Application("VPScpdomain")&"&attach="&request("attach")
End if'正常完成


End if'升级操作.
End If




If ef Then
response.write "ret=0&err=auth-failure&freehosinfo="&ec
FreeHost_oplog session("username"),"出错","云服务器API，云服务器名:"&vpsname&" 原因："&ec
Sagent.close
Set Sagent=Nothing
conn.close
Set conn=Nothing
session.Abandon()
response.end
End if


session.Abandon()

%>
