<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="key.asp"-->
<!--#include file="function.asp"-->
<!--#include file="../alipay/directlogin.asp"-->
<!--#include file="../alipay/class/alipay_notify.asp"-->
<%
Function RegExpTest(patrn, strng)
  Dim regEx
  Set regEx = New RegExp
  regEx.Pattern = patrn
  regEx.IgnoreCase = False
  RegExpTest = regEx.Test(strng)
End Function 

Function FreeHost_Loginlog(username,type1)
Set connX=Server.CreateObject("ADODB.Connection")
	connX.Open Application("FreeHostDBlink")
	Set clientlog = Server.CreateObject("ADODB.Recordset")	
	Sqlclientlog="Select * from FreeHost.FreeHost_Loginlog"
	clientlog.Open Sqlclientlog,connX,3,2
	clientlog.addnew
clientlog("username")=username
clientlog("type1")=type1
clientlog("logintime")=NOW
clientlog("ip")=Request.ServerVariables("REMOTE_ADDR")
If Request.ServerVariables("HTTP_FreeHostAPIIP")<>"" Then clientlog("ip")=Request.ServerVariables("HTTP_FreeHostAPIIP")
clientlog.update
clientlog.close
connX.close
set connX=nothing
set clientlog=nothing
End Function


Function UTF2GB(UTFStr)
    For Dig=1 to len(UTFStr)
        if mid(UTFStr,Dig,1)="%" then
            if len(UTFStr) >= Dig+8 then
                GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9))
                Dig=Dig+8
            else
                GBStr=GBStr & mid(UTFStr,Dig,1)
            end if
        else
            GBStr=GBStr & mid(UTFStr,Dig,1)
        end if
    Next
    UTF2GB=GBStr
End Function

code=LCase(Trim(Request.QueryString("code")))
If code="" or RegExpTest("[^A-Za-z0-9]",code) Then
response.write "code没填或非法！"
response.end
End If

state=LCase(Trim(Request.QueryString("state")))
If state="" or RegExpTest("[^A-Za-z0-9]",state) Then
response.write "state没填或非法！"
response.end
End If



'2.3.4 获取并存储access token以及openid
'成功登录后，即可发送请求来获取access token以及openid，这两个参数在调用OpenAPI访问和修改用户数据时必须传入，网站需自行绑定或存储：
 '（1）access token用来判断用户在本网站上的登录状态，具有3个月有效期，用户再次登录时自动刷新。
 '（2）openid是此网站上唯一对应用户身份的标识，网站可将此ID进行存储便于用户下次登录时辨识其身份，或将其与用户在网站上的原有账号进行绑定。 


'Session("Access_Token")="6F113C94FCE863D6D29DA1FBBBFF6375"

Dim qc, result, token, qqid, nickname,sex

SET qc = New QqConnet
If Session("Access_Token") = "" Then
    CheckLogin=qc.CheckLogin()
	If CheckLogin=False Then
	   Response.Write("登录失败！")
	   Response.End()
	Else
	   Session("Access_Token")=qc.GetAccess_Token()
'	   Response.Write "000mmmmmmmmmmmmmmmmmmmmmmmm<BR>"&apid
	End If
End If

If Application("fastlogin_temp_"&Session("Access_Token"))="" Or Application("fastlogin_temp_"&Session("Access_Token")&"_fasttime")="" Then
response.write "已超时,请重试,登陆操作必须在4分钟内完成！"&Application("fastlogin_temp_"&Session("Access_Token"))'&apptoken
Session("Access_Token")=""
Session("qq_oauth_token_secret") = ""
response.end
End If

FOR EACH apid IN Application.Contents
If InStr(apid,"_fasttime") Then
'Response.Write "<BR>"&apid
'Response.Write "<BR>"&Application(apid)
'Response.Write "<BR>"&DateDiff("s",Application(apid),NOW )
if DateDiff("s",Application(apid),NOW )>240 Then
Application.Contents.Remove(Replace(apid,"_fasttime",""))
Application.Contents.Remove(apid)
End if
End if
NEXT

If 	Application("fastlogin_temp_"&apptoken)<>"" then
If 	Application("fastlogin_temp_"&apptoken)<>Request.ServerVariables("SERVER_NAME")&":"&Request.ServerVariables("server_port") Then
realhost=Application("fastlogin_temp_"&apptoken)
'Application.Contents.Remove("fastlogin_temp_"&apptoken)
'Application.Contents.Remove("fastlogin_temp_"&apptoken&"_fasttime")
If Request.ServerVariables("server_port")="443" Then Response.Redirect "https://"&realhost&"/fastlogin/qq/return_url.asp?"&Request.QueryString
Response.Redirect "http://"&realhost&"/fastlogin/qq/return_url.asp?"&Request.QueryString
End if
End If

If Session("Access_Token") <> "" Then
qqid=qc.Getopenid()
Session("Openid")=qqid
UserInfo=qc.GetUserInfo()
'qqemail=qc.GetUserEmail(qc.Get_Info())
nickname = qc.GetUserName(UserInfo)(0)
sex = qc.GetUserName(UserInfo)(1)
icon = "http://qzapp.qlogo.cn/qzapp/"&qc.APP_ID&"/"&Session("Openid")&"/30"
Set qc=Nothing



user_id=qqid
token=Session("qq_oauth_token_secret")
'real_name=UTF2GB(nickname)

If user_id="" or RegExpTest("[^A-Za-z0-9]",user_id) Or Len(user_id)<3 Then
	ef=True
	ec="user_id没填或非法！"
End if
If token="" or RegExpTest("[^A-Za-z0-9]",token) Then
	ef=True
	ec="token没填或非法！"
End if
If Not Application("fastlogin_useqq")=True Then
	ef=True
	ec="管理员没启用本功能！"
End If


If not ef then
Set conn=Server.CreateObject("ADODB.Connection")
	conn.Open Application("FreeHostDBlink")
  Set dsreg = Server.CreateObject("ADODB.Recordset")	
		Sql="Select * from FreeHost.FreeHost_USER where user_id='"&user_id&"' and fastlogin=2"'qq为2
		if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
		dsreg.Open Sql,conn,1,3
If dsreg.Eof Then

If Len(user_id)>1 Then
session("fastlogin_token")=token
session("fastlogin_real_name")=real_name
session("fastlogin_user_id")=user_id
'Response.Write UserInfo&"<br>"
'response.write user_id&"<br>"
'response.write nickname&"<br>"
'response.write UTF2GB(nickname)&"<br>"
'response.write qqemail&"<br>"不存在
'response.write token
End If

'"会员名不存在！或没关联"
'如果没有id用户'
'提示:您首次在本网站使用支付宝快捷登陆,请您选择:

'让用户选:如果您以前已在本网站注册过用户名,请您将支付宝快捷登陆和原来的用户名关联,您以后就直接可以用支付宝号登陆.

'让用户选:如果您以前没有在本网站注册过用户名,请您在这儿创建立用户名,您以后就直接可以用支付宝号登陆.
''自动进入注册页-------------------------------------
'生成用户名密码
'自动提交
If Application("fastlogin_autoreg") Then
QQtempname="qq"&LCase(Left(user_id,12))
Set dsreg = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_USER where username='"&QQtempname&"'"
		if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
		dsreg.Open Sql,conn,1,3
If not dsreg.Eof Then
		ef=True
		ec="QQ登陆后生成的临时用户名已存在请联系管理员处理！"
end If
If not ef Then
dsreg.AddNew
dsreg("username")=QQtempname
dsreg("password")=md5(user_id&now,"")
dsreg("email")="notset@notset.not"
dsreg("truename")="QQ登陆生成"
'dsreg("mobi")=0
dsreg("waitmoney")=0
dsreg("regtime")=NOW
dsreg("lastuse")=NOW
dsreg("regip")=Request.ServerVariables("REMOTE_ADDR")
If Request.ServerVariables("HTTP_FreeHostAPIIP")<>"" Then dsreg("regip")=Request.ServerVariables("HTTP_FreeHostAPIIP")
dsreg("usermoney")="0.00"
dsreg("hostphcount")=0
dsreg("emailphcount")=0
dsreg("domainphcount")=0
dsreg("haveupdate")="y"'3.0
if session("agentname")="n" or session("agentname")="" then
dsreg("agent1")="n"
else
dsreg("agent1")=session("agentname")
end if
dsreg("agent2")="n"
If session("agentusername")<>"" Then dsreg("agent1")=session("agentusername") 
If session("admau")<>"" and Request.Form ("agent1")<>"" Then dsreg("agent1")=Request.Form ("agent1")

'支付宝
If Application("fastlogin_usealipay")=true Or Application("fastlogin_useqq")=True Then
if Request.ServerVariables("HTTP_FreeHostAPI")<>"7i24.com" Then
If session("fastlogin_user_id")<>"" Then
If Len(session("fastlogin_user_id"))>3 Then
dsreg("user_id")=session("fastlogin_user_id")
dsreg("fastlogin")=1'支付宝
If Len(dsreg("user_id"))>20 Then dsreg("fastlogin")=2'QQ
session("fastlogin_user_id")=""
dsreg("token")=session("fastlogin_token")
dsreg("real_name")=session("fastlogin_real_name")
'dsreg.update
End if
End if
End If
End if
dsreg.update
dsreg.close
Set dsreg = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_USER where username='"&QQtempname&"'"
		dsreg.Open Sql,conn,1,3

'进入用户区
session("fastlogin_goto")=""
dologinuserFast
End if'用户名不冲突



End if
%>

<center><% Response.Write("欢迎你！<img src=""" &icon& """/> <b><font color='green'>"& nickname&"</font></b> [" &sex&"]<br/>") %></center>

<!--#include file="../alipay/logincontent.asp"-->

<%

Else
'调用登陆

If session("fastlogin_goto")="agent" then'进入代理区
fastlogin_agent=dsreg("username")
dsreg.close
session("fastlogin_goto")=""
dologinAgentFast
else'进入用户区
session("fastlogin_goto")=""
dologinuserFast
End if

end if
end If


'检查user_id合法性

'session("alipay_user_id")=user_id

if ef then
Response.Redirect "/user/e.asp?e="&ec
end if

'	Response.Write "你的用户名称:" & UTF2GB(nickname) & "<br/><br/><br/>"
'	Response.Write "QQID:" & qqid & "<br/><br/><br/>"
'	Response.Write "头像:<img src='http://qzapp.qlogo.cn/qzapp/" & QQ_OAUTH_CONSUMER_KEY & "/" & qqid & "/100' width='100'/>"
Else
	Response.Write "登录失败"
End If
%>
