<%
'**************************************
'* 最后更新 2014/4/16 by 56770.kingdge
'* Copyright www.56770.com
'**************************************
Dim user_id,user_password,verifycode,sql,lasturl,backurl,loginerror,error_type,cklogin_mes,m_u,m_time,other_main,CookiesId,path
Showpath = "<a href='"& Eshop_url & LangPath &"'>"& Lebi_word(0) &"</a> → "& Lebi_user(24)
CookiesId = CheckNum(request.cookies(cookiesname&"_user")("cookiesid"),GetUserId())
path=LCase(trim(request.form("path")))
If InStr(path,"//") > 0 Or InStr(path,"http") > 0 Then path = ""
If path = "" Then path = LCase(HTTP_REFERER)
user_id=safereplace(request("user_id"))
user_password=safereplace(request("user_password"))
if user_id="" or user_password="" Then
        loginerror=True
        error_type = error_type &",1,"
Else
		if CInt(Config_Lebi_Eshop(12)) = 1 Then
				verifycode	= request.form("verifycode")
				If verifycode <> cstr(Session("verifycode")) Then
						loginerror=True
						error_type = error_type &",2,"
				End If
		End If
'		If loginerror <> True Then
'				If LCase(user_id) = "admin" Then
'						loginerror=True
'						error_type = error_type &",3,"
'				End If
'		End If
		If loginerror <> True Then
				sql="select user_id from [Lebi_user] where user_id='"& user_id &"' and user_password='"& md5(user_password) &"'"
				Set rs=conn.execute(sql)
				if rs.eof or rs.bof Then
						loginerror=True
						error_type = error_type &",3,"
				End If
				rs.close : Set rs = Nothing
		End If
End If
If loginerror=true Then
        response.redirect Eshop_url & PagePath(1) &"?action=cklogin&error_type="& error_type
		response.End
Else
        sql="select * from [Lebi_user] where user_id='"& user_id &"'"
        Set rs=Server.CreateObject("ADODB.RecordSet")
        rs.open sql, conn, 1, 3
	    Response.cookies(cookiesname&"_user")("user_grade") = rs("user_grade")
	    Response.cookies(cookiesname&"_user"&"b")("JsCookieUserGrade") = trim(rs("user_grade"))
		session(cookiesname&"_user"&"user_grade")=rs("user_grade")
		cklogin_mes = cklogin_mes &"<tr><td height=""25"" class=""list"">·"& Lebi_user(69) &"</td></tr>"
		If Datediff("d",rs("lastlogintime"),now())=1 Then
				If cache_login_jifen(CInt(rs("user_grade"))) <> 0 Then
						conn.execute("insert into [Lebi_message](to_user,user_id,subject,msg,idate,ifview,ifre)values('"& user_id &"','Admin','"& Lebi_user(73) & cache_login_jifen(CInt(rs("user_grade"))) &"','"& Lebi_user(73) & cache_login_jifen(CInt(rs("user_grade"))) &"',"& sqlnowstring &",0,0)")
						rs("jifen") = rs("jifen") + cache_login_jifen(CInt(rs("user_grade")))
				End If
		Elseif Datediff("d",rs("lastlogintime"),now())>1 Then
				If cache_login_del_jifen(CInt(rs("user_grade"))) <> 0 Then
						conn.execute("insert into [Lebi_message](to_user,user_id,subject,msg,idate,ifview,ifre)values('"& user_id &"','Admin','"& Replace(Replace(Lebi_user(71),"{day}",datediff("d",rs("lastlogintime"),now())),"{score}",(cache_login_del_jifen(CInt(rs("user_grade"))) * (datediff("d",rs("lastlogintime"),now())-1))) &"','"& Replace(Replace(Lebi_user(71),"{day}",datediff("d",rs("lastlogintime"),now())),"{score}",(cache_login_del_jifen(CInt(rs("user_grade"))) * (datediff("d",rs("lastlogintime"),now())-1))) &"',"& sqlnowstring &",0,0)")
						rs("jifen") = rs("jifen") - (cache_login_del_jifen(CInt(rs("user_grade"))) * (datediff("d",rs("lastlogintime"),now())-1))
				End If
		Elseif Datediff("d",rs("lastlogintime"),now())=0 Then
		End If
		'-----------------------------------------------------------------
		'系统整合
		'-----------------------------------------------------------------
		Dim API_LEBI,API_SaveCookie,SysKey
		If API_Enable Then
				Set API_LEBI = New API_Conformity
				API_LEBI.NodeValue "action","login",0,False
				API_LEBI.NodeValue "username",user_id,1,False
				Md5OLD = 1
				SysKey = Md5(API_LEBI.XmlNode("username") & API_ConformKey)
				Md5OLD = 0
				API_LEBI.NodeValue "syskey",SysKey,0,False
				API_LEBI.NodeValue "password",user_password,0,False
				API_LEBI.SendHttpData
				If API_LEBI.Status = "1" Then
						Response.Write "<script>alert('" & API_LEBI.Message & "');history.back();</script>"
						response.End
				Else
						Dim usercookies:usercookies=1
						API_SaveCookie = API_LEBI.SetCookie(SysKey,user_id,md5(user_password),usercookies)
				End If
				Set API_LEBI = Nothing
		End If
		'-----------------------------------------------------------------
        rs("lastloginIP") = request.servervariables("remote_addr")
        rs("logins") = rs("logins")+1
		rs("lastlogintime") = now()
		rs("LangName") = LangName
		rs("LangTxt") = LangTxt
		rs("RndPass") = md5(md5(user_password))
		rem	<-{ 自动开启资金帐号 by 56770.WangLei
		Cookies_Lebi_UserGrade = trim(rs("user_grade"))
		If rs("ifpay") <> 1 And CInt(getGradePurview(6))=1 And (clng(rs("jifen"))>clng(getGradePurview(7)) Or clng(rs("sumpay"))>clng(getGradePurview(8))) Then
				rs("ifpay") = 1
				conn.execute("insert into [Lebi_message](to_user,user_id,subject,msg,idate,ifview,ifre)values('"& user_id &"','Admin','"& Lebi_user(201) &"','"& Lebi_user(201) &"',"& sqlnowstring &",0,0)")
		End If
		rem	}->
		rem	<-{ 自动降级 2008-3-10 by 56770.WangLei
		If CInt(getGradePurview(14))=1 And CInt(rs("user_grade")) > 1 Then
				Dim degrade_days,degrade_cache
				degrade_days = CInt(getGradePurview(15))
				degrade_cache = Cdbl(getGradePurview(16))
				If DateDiff("d", rs("update_grade_time"),date()) > degrade_days Then
						Dim sql_dis_sum,Sum_dis_sum
						sql_dis_sum = "select sum(dis_sum) from [Lebi_orders] where 1=1"
						If IsSqlDataBase = 1 Then
								sql_dis_sum = sql_dis_sum &" and datediff(d,order_time,'"& rs("update_grade_time") &"')<=0 and datediff(d,order_time,"&  sqlnowstring &")>=0"
						Else
								sql_dis_sum = sql_dis_sum &" and datediff('d',order_time,'"& rs("update_grade_time") &"')<=0 and datediff('d',order_time,"& sqlnowstring &")>=0 "
						End If
						sql_dis_sum = sql_dis_sum &" And orderzt like '%2%' And user_id='"& user_id &"'"
						Sum_dis_sum = conn.execute(sql_dis_sum)(0)
						If Sum_dis_sum = "" Or isnull(Sum_dis_sum) Then Sum_dis_sum = 0
						If degrade_cache > Sum_dis_sum Then
								rs("user_grade") = rs("user_grade") -1
								rs("update_grade_time") = Now()
								Cookies_Lebi_UserGrade = rs("user_grade")
								Set rsgrade = conn.execute("select grade_name"& LangTxt &" from [Lebi_user_grade] where grade_dj = "& rs("user_grade") &"")
								If Not rsgrade.eof Then 
									grade_name = rsgrade(0)
								End If 
								rsgrade.close : Set rsgrade = Nothing 
								conn.execute("insert into [Lebi_message](to_user,user_id,subject,msg,idate,ifview,ifre)values('"& user_id &"','Admin','"& Replace(Lebi_user(206),"{grade_name}",grade_name) &"','"& Replace(Replace(Replace(Lebi_user(203),"{grade_name}",grade_name),"{grade_days}",degrade_days),"{grade_cache}",degrade_cache) &"',"& sqlnowstring &",0,0)")
						End If
				End If
		End If
		rem	}->
		rem	<-{ 自动升级高等级 2008-3-10 by 56770.WangLei
		If CInt(getGradePurview(10))=1 And CInt(rs("user_grade")) < CInt(cache_ugrade_dj(UserGroupNumber)) Then
				If cdbl(rs("jifen")) >= cdbl(getGradePurview(11)) Then
						If CInt(getGradePurview(12)) = 1 Then rs("jifen") = rs("jifen")-cdbl(getGradePurview(11))
						rs("user_grade") = CInt(rs("user_grade")) + 1
						rs("update_grade_time") = Now()
						Cookies_Lebi_UserGrade = rs("user_grade")
						Set rsgrade = conn.execute("select grade_name"& LangTxt &" from [Lebi_user_grade] where grade_dj = "& rs("user_grade") &"")
						If Not rsgrade.eof Then 
							grade_name = rsgrade(0)
						End If 
						rsgrade.close : Set rsgrade = Nothing 
						conn.execute("insert into [Lebi_message](to_user,user_id,subject,msg,idate,ifview,ifre)values('"& user_id &"','Admin','"& Replace(Lebi_user(202),"{grade_name}",grade_name) &"','"& Replace(Lebi_user(202),"{grade_name}",grade_name) &"',"& sqlnowstring &",0,0)")
				End If
		End If
		rem	}->
        'rs("lock")=1
        rs.Update
		User_jifen_product_state()
		rem 删除24小时前的临时用户记录 2009-7-30 by 56770.kingdge
	    If IsSqlDataBase = 1 Then
	            conn.execute("Delete from [Lebi_lshy] where datediff(d,basket_date,"& sqlnowstring &")>0")
                conn.execute("Delete from [Lebi_lsbuy] where datediff(d,basket_date,"& sqlnowstring &")>0 and User_Id=''")
        Else
	            conn.execute("Delete from [Lebi_lshy] where datediff('d',basket_date,"& sqlnowstring &")>0")
				conn.execute("Delete from [Lebi_lsbuy] where datediff('d',basket_date,"& sqlnowstring &")>0 and User_Id=''")
	    End If
		Dim cookies_path
	    cookies_path = replace(lcase(Request.ServerVariables("URL")),"cklogin/","")
	    Response.cookies(cookiesname&"_user")("user_id") = rs("user_id")
	    Response.cookies(cookiesname&"_user")("user_grade") = rs("user_grade")
	    Response.cookies(cookiesname&"_user")("user_key") = md5(md5(user_password))
	    Response.Cookies(cookiesname&"_user").Expires = Date() + 1
	    'Response.Cookies(cookiesname&"_user").path = cookies_path
	    rem 写入JsCookie 2007-3-27 by 56770.kingdge
	    Response.cookies(cookiesname&"_user"&"a")("JsCookieUserId") = trim(rs("user_id"))
	    Response.cookies(cookiesname&"_user"&"b")("JsCookieUserGrade") = trim(rs("user_grade"))
		session(cookiesname&"_user"&"user_id")=rs("user_id")
		session(cookiesname&"_user"&"user_grade")=rs("user_grade")
		session(cookiesname&"_user"&"user_key") = md5(md5(user_password))
        rs.Close : set rs=Nothing
		'-----------------------------------------------------------------
		'系统整合
		'-----------------------------------------------------------------
		If API_Enable Then
				other_main = API_SaveCookie
				Response.Flush
				If API_LoginUrl <> "0" Then
						other_main = other_main &"<font color=red><span id=""backtime"">0</span></font> <a href='"& API_LoginUrl &"'>"&Lebi_user(75)&"</a><script language=""JavaScript"">Url="""& API_LoginUrl &""";if (!Url){Url=document.referrer}function countDown(secs){document.getElementById(""backtime"").innerHTML=secs;if(--secs>0) {setTimeout(""countDown(""+secs+"")"",1000);}else {window.location.href=Url;}}countDown(1);</script>"
				End If
		End If
		'-----------------------------------------------------------------
		If session("oldUrl")="" Then
				Dim lsbuy
				set rs = conn.execute("select cpid,color,chima from [Lebi_lsbuy] where sessionid="& CookiesId &"")
				If Not rs.eof Then
						Do While Not rs.eof
								Set lsbuy = server.CreateObject("adodb.recordset")
								lsbuy.open "select user_id from [Lebi_lsbuy] where sessionid="& CookiesId &" and user_id = '"& user_id &"' and cpid = "& rs(0) &" and color= '"& rs(1) &"' and chima = '"& rs(2) &"'",conn,1,3
								If Not lsbuy.eof Then
										lsbuy(0) = user_id
										lsbuy.update
								End If
								lsbuy.close
						rs.movenext
						Loop
						backurl = Eshop_url & PagePath(3)
				End If
				rs.Close : Set rs=Nothing
		End If
		If InStr(path,"/login")>0 Or InStr(path,"/error")>0 Or InStr(path,"/reg")>0 Then
				If session("oldUrl")="" Then
						backurl = Eshop_url & LangPath
				Else
						backurl = session("oldUrl")
				End If
		Else
				backurl = path
		End If
		If API_Enable Then
				other_main = other_main &"<font color=red><span id=""backtime"">0</span></font> <a href='"& backurl &"'>"&Lebi_user(75)&"</a><script language=""JavaScript"">Url="""& backurl &""";if (!Url){Url=document.referrer}function countDown(secs){document.getElementById(""backtime"").innerHTML=secs;if(--secs>0) {setTimeout(""countDown(""+secs+"")"",1000);}else {window.location.href=Url;}}countDown(1);</script>"
		Else
				response.redirect backurl
		End If
End If
%>