<%@ Language=VBScript %>
<%server.scripttimeout=240%>
<!--#include virtual="/inc-freehost-function/FreeHostEmail.asp"-->
<!--#include virtual="/inc-freehost-function/FreeHostFunction.asp"-->
<!--#include virtual="/inc-freehost-function/FreeHostFunctionMain.asp"-->
<%
If session("username")="" Then
	ef=True
	ec="授权非法，有可能是超时造成的。"
ElseIf Request.Form ("id")="" Then
	ef=True
	ec="请选择产品！"
ElseIf not IsNumeric(Request.Form ("id"))  Then
	ef=True
	ec="产品ID应该是数字！"
ElseIf IsNumeric(Request.Form ("CDNname"))  Then
	ef=True
	ec="CDN名不能是纯数字！"
ElseIf not IsNumeric(Request.Form ("year"))  Then
	ef=True
	ec="时间应该是数字！"
ElseIf Request.Form ("CDNname")="" Then
	ef=True
	ec="请设置CDN名！"
ElseIf Len(Request.Form ("CDNname"))<3 Then
	ef=True
	ec="CDN名长度不能小于3位！"
ElseIf Trim(LCase(Request.Form ("CDNname")))="self" or Trim(LCase(Request.Form ("CDNname")))="adm" Or Trim(LCase(Request.Form ("CDNname")))="administrator" Or Trim(LCase(Request.Form ("CDNname")))="freehost" Or Trim(LCase(Request.Form ("CDNname")))="proxy" Then
	ef=True
	ec="您输入的CDN户名为保留用户名，请换一个CDN名"
ElseIf RegExpTest("[^A-Za-z0-9]",Request.Form ("CDNname")) Then
	ef=True
	ec="CDN名中有非法字符！"
ElseIf RegExpTest("[^A-Za-z0-9]",Request.Form ("CDNpassword")) Then
	ef=True
	ec="CDN密码中有非法字符！"
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_CDNProductlist where id='"&Request.Form ("id")&"'"
	dshost.Open Sql,conn,1,1
if dshost.Eof then
	ef=True
	ec="产品不存在！"
else
'ServerlistID=dshost ("ServerlistID")'机房选择
'if  Request.Form("ServerlistID")=cstr(dshost ("ServerlistID2")) and dshost ("ServerlistID2")<>0 then ServerlistID=dshost ("ServerlistID2")
'if  Request.Form("ServerlistID")=cstr(dshost ("ServerlistID3")) and dshost ("ServerlistID3")<>0 then ServerlistID=dshost ("ServerlistID3")
'if  Request.Form("ServerlistID")=cstr(dshost ("ServerlistID4")) and dshost ("ServerlistID4")<>0 then ServerlistID=dshost ("ServerlistID4")
'if  Request.Form("ServerlistID")=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_CDNProductlist_AGN where id='"&Request.Form ("id")&"' and agent1='"&ds("agent1")&"'"
	agnserverid.Open Sql,conn,1,1
dim isAgnHostSer
if not agnserverid.Eof then
isAgnHostSer=false
end if
end if'isagn
end if
end If

if not ef then
If Request.Form ("year")<>999 and Request.Form ("year")<>dshost("PAY_Month") and Request.Form ("year")<>dshost("PAY_Season")  and Request.Form ("year")<>dshost("PAY_halfyear") and Request.Form ("year")<>1 and Request.Form ("year")<>dshost("PAY_2year") and Request.Form ("year")<>dshost("PAY_3year") and Request.Form ("year")<>dshost("PAY_4year") and Request.Form ("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_CDN 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.Form ("year")=999 and dshost("cantest")="n" then
	ef=True
	ec="很抱歉,本产品不可以试用！"
end if
end if
if not ef then
if Request.Form ("year")=dshost("PAY_Month") and dshost("canmonth")="n" then
	ef=True
	ec="很抱歉,本产品不可以月付！"
end if
end if
if not ef then
if Request.Form ("year")=dshost("PAY_Season") and dshost("canseason")="n" then
	ef=True
	ec="很抱歉,本产品不可以季付！"
end if
end if
if not ef then
if Request.Form ("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_ServerCDNlist 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
	Set dsph = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_CDN where ftpname='"&trim(Request.Form ("CDNname"))&"'"
	dsph.Open Sql,conn,1,1
if not dsph.Eof then
	ef=True
	ec="您输入的CDN名已存在，请另外输入一个！"
end if
dsph.close
end if


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

if not ef then

dim starttime,temptime,pdtype
pdtype=dshost("CDNtype")
starttime=NOW
'结束时间
y=Request.Form ("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_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)
dim isfortest
isfortest=true
end if

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.Form ("year")
set RsUserds=ds
Useragentname=ds("agent1")
forpaytype="CDN产品"
forpayid=dshost("id")
logfor="用户区开通(CDN名"&trim(Request.Form ("CDNname"))&")"
pubisagnSer=isAgnHostSer
AgnRunasUser=session("AgnRunasUser")
DoPayMoney
end if

if not ef then
dim Subject,body'send mail
CDNname=lcase(trim(Request.Form ("CDNname")))
dsph.AddNew
dsph("username")=ds ("username")
'dsph("ServerlistID")=dsserver("id")
dsph("CDNtype")=dshost("CDNtype")
dsph("starttime")=starttime
dsph("endtime")=temptime



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

dsph("ftpname")=LCase(Trim(Request.Form("cdnname")))
dsph("ftppassword")=Request.Form ("CDNpassword")


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("kefu")=ds("kefunow")

dsph.Update


'LOG
Set dsph = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_CDN where ftpname='"&CDNname&"'"
	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



FreeHost_oplog session("username"),"信息","用户区，用户开通CDN产品:("&CDNname&") "

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


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

%>
<!--#include virtual="/inc-freehost-function/htmlwrite.asp"--><%=htmlwrite("html2")%>
　CDN开通成功
<%=htmlwrite("html3")%>
<br>
　　　　　　　　　　　　　<br>
<table width="80%"  border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FF0000">
  <tr align="center" bordercolor="#000000" bgcolor="#999999">
    <td height="39" colspan="2"><font color="#000000">　　　</font><font color="#990000"><font color="#FF0000">恭喜您的CDN开通成功，以下是您的资料：</font></font></td>
  </tr>
  <tr bordercolor="#000000" bgcolor="#EEEEEE">
    <td width="39%" height="30"><font color="#000000"> 　　　 您的原金额：</font></td>
    <td width="61%"><font color="#000000"><%=olduser_money%>元</font></td>
  </tr>
  <tr bordercolor="#000000" bgcolor="#EEEEEE">
    <td height="41"><font color="#000000">　　　 您的金额变化：</font></td>
    <td><font color=#000000><%=moneych%> 元</font></td>
  </tr>
  <tr bordercolor="#000000" bgcolor="#EEEEEE">
    <td height="39"><font color="#000000">　　　 您的新金额：</font></td>
    <td><font color="#000000"><%=newuser_money%> 元</font></td>
  </tr>
  <tr bordercolor="#000000">
    <td height="37" bgcolor="#eeeeee"><font color="#000000">　　　 您的CDN名称：</font></td>
    <td bgcolor="#eeeeee"><%=Request.Form ("CDNname")%></td>
  </tr>
  <tr bordercolor="#000000">
    <td height="37" bgcolor="#eeeeee"><font color="#000000"> 　　　 到期时间是：</font></td>
    <td bgcolor="#eeeeee"><%=temptime%></td>
  </tr>
<%if y=999 then%>
    <tr bordercolor="#000000">
    <td height="60" bgcolor="#eeeeee"><font color="#000000"> 　　　 试用说明：</font></td>
    <td bgcolor="#eeeeee">由于您开通的是试用CDN，它的流量<br>
      及速度等都受到限制。如果您要使用全部的功能，<br>
      请您升级到正式的CDN产品。</td>
  </tr>
<%end if%>
    <tr bordercolor="#000000">
    <td height="60" bgcolor="#eeeeee"><font color="#000000"> 　　　 使用说明：</font></td>
    <td bgcolor="#eeeeee"><a href="/user/CDNadm2.asp?id=<%=pdid%>&go=e"><font color="#000000">CDN产品虽然已开通,但是您要绑定域名后才能使用>>>>>></font></a></td>
  </tr>
  <tr align="center" bordercolor="#000000">
    <td height="34" colspan="2" bgcolor="#999999"> <font color="#990000">　我　们　为　您　提　供　优　质　的　服　务！</font></td>
  </tr>
</table>
<br>
<%=htmlwrite("html4")%>
<%end if
if ef then
Response.Redirect "e.asp?e="&ec
end if%>
