<%@language=vbscript codepage=936 %>
<!--#include file="Conn.asp"-->
<!--#include file="Inc/Cl_ClsSysTem.asp"-->
<%
Dim Action,ID,AID
Dim LinkConfig,IsViewer
Dim LinkUrl,Content
Action=Trim(Request("Action"))
if Action="Hit" then
	Hit_Main
else
	Get_Main
end if

CloseAllObj

Response.Redirect LinkUrl

Public Sub Hit_Main()
	if Cl.ChkIsOuter then
		Call Cl.OutMsg("请不要从外部访问本页面，此操作已被禁止！","../Index.asp")
	end if
	ID=Cl.ChkClng(Request("ID"))
	if ID=0 then LinkUrl="../Index.asp" : Exit Sub
	Dim Rs
	Set Rs=Cl.Execute("Select SiteUrl From Cl_LinkSite Where ID="&ID)
	if Not (Rs.Bof And Rs.Eof) then
		LinkUrl=Rs(0)
		If Request.Cookies("Hit_"&ID)<>"OK" then
			Cl.Execute("Update Cl_LinkSite Set Hits=Hits+1 where ID="&ID)
			Response.Cookies("Hit_"&ID)="OK"
		End If
	Else
		LinkUrl="../Index.asp"
	End if
	Rs.Close : Set Rs=Nothing
End Sub

Public Sub Get_Main()
	Cl.Name="Cl_LinkConfig"
	If Cl.ObjIsEmpty() Then LoadLinkConfig
	LinkConfig=Split(Cl.Value,"%%%")		
	LinkUrl=LinkConfig(0)
	ID=Cl.ChkClng(Request("ID"))
	AID=Cl.ChkClng(Request("AID"))
	If AID>0 Then
		Dim TempUrl
		AID=AID-1
		Content=Split(LinkConfig(1),"@@@")
		if Ubound(Content)>=AID then
			TempUrl=Split(Content(AID),"|||")
			if Ubound(TempUrl)>1 then
				if Trim(TempUrl(2))<>"" then LinkUrl=TempUrl(2)
			end if
		end if
	End If
	IsViewer=Trim(Request.Cookies("Viewer"))
	If Lcase(IsViewer)="isok" then
		RefreshView
		Exit Sub
	end if
	Response.Cookies("Viewer")="isok"
	If ID>0 Then
		ChkTrueVisitIP(Request.ServerVariables("REMOTE_ADDR"))
	End If
End Sub
Public Sub ChkTrueVisitIP(sIP)
	sIP=Trim(sIP)
	if sIP="" then Exit Sub
	Cl.Name="TrueVisitIP"
	if Cl.ObjIsEmpty() then
		Cl.Value=sIP
		RefreshVisit
		Exit Sub
	end if
	if Instr("#" & Cl.Value & "#","#" & sIP & "#")>0 then
		RefreshView   'IP已存在缓存列表，视为浏览
	else
		Cl.Value=VsaveIPS(Cl.Value,sIP) 
		RefreshVisit  'IP不存在则更新缓存及访问量
	end if
End Sub
' 更新要保存的IP
Public Function VsaveIPS(InIPS,sIP)
	Dim sInIPS
	sInIPS=split(InIPS,"#")
	'if ubound(sInIPS) < 500 then
		'VsaveIPS=InIPS & "#" & sIP
	'else
		'VsaveIPS=InIPS
		VsaveIPS=Replace(InIPS,sIP,"")
		VsaveIPS=VsaveIPS & "#" & sIP
		VsaveIPS=Replace(VsaveIPS,"##","#")
		VsaveIPS=Replace(VsaveIPS,"##","#")
	'end if
End Function

'更新浏览量
Public Sub RefreshView()
	Cl.Execute("Update [Cl_LinkSite] Set ViewAll=ViewAll+1 Where ID="&ID)
	Cl.Execute("Update [Cl_LinkConfig] Set ClickAll=ClickAll+1 Where ID=1")
End Sub
'更新访问量
Public Sub RefreshVisit()
	Cl.Execute("Update [Cl_LinkSite] Set View=View+1,ViewAll=ViewAll+1 Where ID="&ID)
	Cl.Execute("Update [Cl_LinkConfig] Set Click=Click+1,ClickAll=ClickAll+1 Where ID=1")
End Sub

Public Sub LoadLinkConfig()
	Dim Rs
	Set Rs=Cl.Execute("Select LinkUrl,Content,Cheat,Point,Money,Click,ClickAll From [Cl_LinkConfig] Where ID=1")
	Cl.Value=Rs.GetString(,,"%%%","","")
	Set Rs=Nothing
End Sub
%>