<%@ Language=VBScript %>
<!--#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 Instr(1, Request.Form ("id"), "'", 1)>0 then
	ef=True
	ec="非法产品ID！"
ElseIf not IsNumeric(Request.Form ("id"))  Then
	ef=True
	ec="产品ID应该是数字！"
ElseIf not IsNumeric(Request.Form ("year"))  Then
	ef=True
	ec="时间应该是数字！"
ElseIf Request.Form ("sqlname")="" Then
	ef=True
	ec="请设置数据库用户名！"
ElseIf IsNumeric(left(Request.Form ("sqlname"),1))  Then
	ef=True
	ec="数据库名不能只是数字，必须用字母开头！"
ElseIf LCASE(trim(Request.Form ("sqlname")))="freehost" Then
	ef=True
	ec="不能用这个用户名，请用另一个用户名！"
ElseIf LCASE(trim(Request.Form ("sqlname")))="master" Then
	ef=True
	ec="不能用这个用户名，请用另一个用户名！"
ElseIf LCASE(trim(Request.Form ("sqlname")))="model" Then
	ef=True
	ec="不能用这个用户名，请用另一个用户名！"
ElseIf LCASE(trim(Request.Form ("sqlname")))="msdb" Then
	ef=True
	ec="不能用这个用户名，请用另一个用户名！"
ElseIf LCASE(trim(Request.Form ("sqlname")))="northwind" Then
	ef=True
	ec="不能用这个用户名，请用另一个用户名！"
ElseIf LCASE(trim(Request.Form ("sqlname")))="pubs" Then
	ef=True
	ec="不能用这个用户名，请用另一个用户名！"
ElseIf LCASE(trim(Request.Form ("sqlname")))="tempdb" Then
	ef=True
	ec="不能用这个用户名，请用另一个用户名！"
ElseIf LCASE(trim(Request.Form ("sqlname")))="root" Then
	ef=True
	ec="不能用这个用户名，请用另一个用户名！"
ElseIf LCASE(trim(Request.Form ("sqlname")))="sa" Then
	ef=True
	ec="不能用这个用户名，请用另一个用户名！"
ElseIf LCASE(trim(Request.Form ("sqlname")))="mysql" Then
	ef=True
	ec="不能用这个用户名，请用另一个用户名！"
ElseIf LCASE(trim(Request.Form ("sqlname")))="case" Then
	ef=True
	ec="不能用这个用户名，请用另一个用户名！"
ElseIf Request.Form ("sqlpassword")="" Then
	ef=True
	ec="请设置数据库密码！"
ElseIf Request.Form ("sqlpassword")<>Request.Form ("resqlpassword") Then
	ef=True
	ec="重复输入的数据库密码不一致！"
ElseIf RegExpTest("[^A-Za-z0-9]",Request.Form ("sqlname")) or Instr(1, Request.Form ("sqlname"), "'", 1)>0 or Instr(1, Request.Form ("sqlname"), "-", 1)>0 or Instr(1, Request.Form ("sqlname"), " ", 1)>0 or Instr(1, Request.Form ("sqlname"), "&", 1)>0 or Instr(1, Request.Form ("sqlname"), ".", 1)>0 or Instr(1, Request.Form ("sqlname"), "_", 1)>0 Then
	ef=True
	ec="数据库用户名中有非法字符！"
ElseIf RegExpTest("[^A-Za-z0-9]",Request.Form ("sqlpassword"))  or Request.Form ("sqlpassword")="123456" or Request.Form ("sqlpassword")="12345"  or len(Request.Form ("sqlpassword"))<4 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")&"'"
		if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
		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 dsSqlpdlist = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_SqlProductlist where id='"&Request.Form ("id")&"'"
	if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
	dsSqlpdlist.Open Sql,conn,1,1
if dsSqlpdlist.Eof then
	ef=True
	ec="产品不存在！"
else
   if dsSqlpdlist("isbyhost")="y" then
   	ef=True
	ec="赠送的只能自动开通！"
   end if

ServerlistID=dsSqlpdlist ("ServerlistID")
if  Request.Form("ServerlistID")=cstr(dsSqlpdlist ("ServerlistID2")) and dsSqlpdlist ("ServerlistID2")<>0 then ServerlistID=dsSqlpdlist ("ServerlistID2")
if  Request.Form("ServerlistID")=cstr(dsSqlpdlist ("ServerlistID3")) and dsSqlpdlist ("ServerlistID3")<>0 then ServerlistID=dsSqlpdlist ("ServerlistID3")
if  Request.Form("ServerlistID")=cstr(dsSqlpdlist ("ServerlistID4")) and dsSqlpdlist ("ServerlistID4")<>0 then ServerlistID=dsSqlpdlist ("ServerlistID4")
if  Request.Form("ServerlistID")=cstr(dsSqlpdlist ("ServerlistID5")) and dsSqlpdlist ("ServerlistID5")<>0 then ServerlistID=dsSqlpdlist ("ServerlistID5")
	Set agnserverid = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_SqlProductlist_AGN where id='"&dsSqlpdlist("id")&"' and agent1='"&ds("agent1")&"'"
	agnserverid.Open Sql,conn,1,1
if not agnserverid.Eof then
if agnserverid ("ServerlistID")<>0 then
ServerlistID=agnserverid ("ServerlistID")
isAgnSqlSer=true
else
isAgnSqlSer=false
end if
end if
end if
end If

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

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

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

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

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


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

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

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

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

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

if not ef then
	Set dsphSQLname = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_Sql where sqlname='"&Request.Form ("sqlname")&"'"
			if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
	dsphSQLname.Open Sql,conn,1,1
if not dsphSQLname.Eof then
	ef=True
	ec="您输入的数据库用户名已存在，请另外输入一个！"
end if
dsphSQLname.close
end if

if not ef then
dim starttime,temptime,pdtype,isAgnHostSer
isAgnHostSer=isAgnSqlSer
pdtype=dsSqlpdlist("Sqltype")
starttime=Now
'结束时间
y=Request.Form ("year")
if y=1 then temptime=dateadd("yyyy",1,now)
if y=dsSqlpdlist("PAY_2year") then temptime=dateadd("yyyy",2,now)
if y=dsSqlpdlist("PAY_3year") then temptime=dateadd("yyyy",3,now)
if y=dsSqlpdlist("PAY_4year") then temptime=dateadd("yyyy",4,now)
if y=dsSqlpdlist("PAY_5year") then temptime=dateadd("yyyy",5,now)
if y=dsSqlpdlist("PAY_Month") then temptime=dateadd("y",31,now)
if y=dsSqlpdlist("PAY_Season") then temptime=dateadd("y",92,now)
if y=dsSqlpdlist("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=dsSqlpdlist("usermoney")
forpayyear=Request.Form ("year")
set RsUserds=ds
Useragentname=ds("agent1")
forpaytype="数据库产品"
forpayid=dsSqlpdlist("id")
logfor="用户区开通(数据库名"&trim(Request.Form ("sqlname"))&")"
pubisagnSer=isAgnSqlSer
AgnRunasUser=session("AgnRunasUser")
DoPayMoney
end if

if not ef then



dsphSql.addnew
dsphSql("username")=ds("username")
dsphSql("ServerlistID")=ServerlistID
dsphSql("Sqltype")=dsSqlpdlist("Sqltype")
dsphSql("starttime")=starttime
dsphSql("endtime")=temptime
if y=999 then
dsphSql("istest")="y"
else
dsphSql("istest")="n"
end if

'dsphSql("Sqlcount")=dsSqlpdlist("Sqlcount")
dsphSql("userspacelimt")=dsSqlpdlist("userspacelimt")
dsphSql("logspacelimt")=dsSqlpdlist("logspacelimt")
dsphSql("sqlname")=request.Form("sqlname")
dsphSql("sqlpassword")=request.Form("sqlpassword")
dsphSql("status")="等待开通"
if ds("agent1")<>"n" then
dsphSql("agent1")=ds("agent1")
else
dsphSql("agent1")="n"
end if

dsphSql("kefu")=ds("kefunow")

dsphSql.update

URL=dsserver("site")
FreeHostsharekey=dsserver("FreeHostsharekey")
userid=dsserver ("admname")
pwd=dsserver("admpass")
homedir=dsserver("homedir")
ip=dsserver("ip")
port=dsserver("port")
userspacelimt=dsphSql("userspacelimt")
logspacelimt=dsphSql("logspacelimt")
sqlname=dsphSql("sqlname")
sqlpassword=dsphSql("sqlpassword")
'ret=SqlAdd(URL,userid,pwd,homedir,userspacelimt,logspacelimt,sqlname,sqlpassword)
'response.write ret


dim softtype,MySQLver,MSSQLver
softtype=dsserver("softtype")
MySQLver=dsserver("MySQLver")
MSSQLver=dsserver("MSSQLver")


if softtype="SQL2000" then
retsql=SqlAdd(URL,FreeHostsharekey,userid,pwd,homedir,userspacelimt,logspacelimt,sqlname,sqlpassword)
else
retsql=SqlAddmysql(URL,FreeHostsharekey,userid,pwd,homedir,userspacelimt,logspacelimt,sqlname,sqlpassword)
end if
FreeHost_oplog session("username"),"信息","用户自己开通，数据库名:"&sqlname&" 返回信息："&retsql


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


dsphSql.close


'log
	Set dsphSql = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_Sql where starttime='"&starttime&"' and username='"&ds("username")&"'"
	dsphSql.Open Sql,conn,1,3
	dsphSql("status")="正常"
	dsphSql.update
Sql="UPDATE [FreeHost].[FreeHost_Moneylog] set pdid='"&dsphSql("id")&"' where pdid='"&session("moneylog_pdid")&"'"
    conn.Execute Sql

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

%>
<!--#include virtual="/inc-freehost-function/htmlwrite.asp"--><%=htmlwrite("html2")%>
　数据库开通
<%=htmlwrite("html3")%>
<br>
<br>
<br>
<table width="89%"  border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FF0000">
  <tr align="center" bordercolor="#000000" bgcolor="#999999">
    <td height="37" colspan="2" bgcolor="#999999"><font color="#000000">　　　</font><font color="#990000"><font color="#FF0000">恭喜您的数据库开通成功，以下是您的资料：</font></font></td>
  </tr>
  <tr align="center" bordercolor="#000000" bgcolor="#999999">
    <td width="36%" height="37" align="right" bgcolor="#eeeeee">用户的原金额：</td>
    <td width="64%" height="37" align="left" bgcolor="#eeeeee">　<%=olduser_money%>元</td>
  </tr>
  <tr align="center" bordercolor="#000000" bgcolor="#eeeeee">
    <td height="37" align="right" bgcolor="#eeeeee">用户金额变化：</td>
    <td height="37" align="left" bgcolor="#eeeeee"><font color=red>　<%=moneych%>元</font></td>
  </tr>
  <tr align="center" bordercolor="#000000" bgcolor="#eeeeee">
    <td height="40" align="right">用户的新金额：</td>
    <td height="40" align="left" bgcolor="#eeeeee">　<%=newuser_money%>元</td>
  </tr>
  <tr align="center" bordercolor="#000000" bgcolor="#eeeeee">
    <td height="40" colspan="2"><font color="#000000">数据库产品已开通，请记下连接方式：</font></td>
  </tr>
  <tr align="center" bordercolor="#000000" bgcolor="#eeeeee">
    <td height="40" align="right"><font color="#000000">数据库用户名：</font></td>
    <td height="40" align="left" bgcolor="#eeeeee"><font color="#000000">　<%=sqlname%></font></td>
  </tr>
  <tr align="center" bordercolor="#000000" bgcolor="#eeeeee">
    <td height="40" align="right"><font color="#000000">数据库密码：</font></td>
    <td height="40" align="left" bgcolor="#eeeeee"><font color="#000000">　<%=sqlpassword%></font></td>
  </tr>
  <tr align="center" bordercolor="#000000" bgcolor="#eeeeee">
    <td height="40" align="right"><font color="#000000">数据库名称：</font></td>
    <td height="40" align="left" bgcolor="#eeeeee"><font color="#000000">　<%=sqlname%></font></td>
  </tr>
  <tr align="center" bordercolor="#000000" bgcolor="#eeeeee">
    <td height="40" align="right"><font color="#000000">数据库所在IP</font>：</td>
    <td height="40" align="left" bgcolor="#eeeeee"><font color="#000000">　<%=ip%></font></td>
  </tr>
  <tr align="center" bordercolor="#000000" bgcolor="#eeeeee">
    <td height="40" align="right"><font color="#000000">所用的连接端口：</font></td>
    <td height="40" align="left" bgcolor="#eeeeee"><font color="#000000">　<%=port%></font></td>
  </tr>
<%if softtype="SQL2000" then%>
  <tr align="center" bordercolor="#000000" bgcolor="#eeeeee">
    <td height="40" align="right"><font color="#000000">典型的ASP调用连接字符串</font>：</td>
    <td height="40" align="left" bgcolor="#eeeeee"><font color="#000000">"provider=sqloledb;data source=<%=ip%>,<%=port%>;User ID=<%=sqlname%>;pwd=<%=sqlpassword%>;Initial Catalog=<%=sqlname%>" </font></td>
  </tr>
<%end if%>
  <tr align="center" bordercolor="#000000" bgcolor="#9C9A9C">
    <td height="39" colspan="2"><font color="#990000">　我　们　为　您　提　供　优　质　的　服　务！</font></td>
  </tr>
</table>
<br>
<br>
<br>
<br>

<br>
<%=htmlwrite("html4")%>
<%end if
if ef then
Response.Redirect "e.asp?e="&ec
end if%>
