<!--#include file="../../lib/base.asp"-->
<!--#include file="../../lib/cmd.asp"-->
<%
''' SEO云链系统客户端
''' ==================================================================
	eycms.stophack
	'注释：插件是否安装
	is_plug_install "eycms.plug.links_user"
	
	'注释：管理权限检查
	is_plug_login

	'注释：定义插件模板路径
	dim theme_admin_root:theme_admin_root="admin_leo"
	dim plug_theme,plug_theme_site,plug_theme_add,plug_theme_edit,plug_theme_edit_site,plug_theme_siteadd
	plug_theme="module/plug_links_user/list.html"
	plug_theme_site="module/plug_links_user/site.html"
	plug_theme_add="module/plug_links_user/add.html"
	plug_theme_edit="module/plug_links_user/edit.html"
	plug_theme_edit_site="module/plug_links_user/edit_site.html"
	plug_theme_siteadd="module/plug_links_user/add_site.html.html"
	dim apiUrl_
	apiUrl_="http://www.eycms.cn/"
	'***************************************************************'
	'接收fget传值
	dim act:act=lcase(eycms.fget("act",0))
	dim id:id=eycms.getint(eycms.fget("id",0),0)
	dim zt
	dim data

	dim m_site,m_name,m_token,m_seokey,m_url,m_cate,m_br,m_baidu
	data=eycms.db.dbload(1,"siteid,name,token,seokey,url,cate","ey_plug_links_site","sid>0","")
	if ubound(data)>=0 then
		m_site=data(0,0)
		m_name=data(1,0)
		m_token=data(2,0)
		m_seokey=data(3,0)
		m_url=data(4,0)
		m_cate=data(5,0)
	end if

	if act="linkeadd" or act="siteadd" then
		if eycms.checkcache("check_my_br",1) then
			m_br=eycms.loadcache("check_my_br",1)
			m_baidu=eycms.loadcache("check_my_baidu",1)
		else
			'm_br=GetSeo("br",m_url,"mysite")
			'm_baidu=GetSeo("baidu",m_url,"mysite")
			'eycms.setcache "check_my_br",m_br,1
			'eycms.setcache "check_my_baidu",m_baidu,1
			'eycms.db.dbupdate "ey_plug_links_site","sid>0",array(array("br",m_br,0,0),array("baidu",m_baidu,0,0))
		end if
	end if

	select case act
		case "linkeadd"
			dim tosid, tosid_name, tosid_key, tosid_url
			tosid=eycms.enhtml(eycms.fget("tosid",0))
			if eycms.strlen(tosid)>0 then
				data=eycms.db.dbload(1,"name,seokey,url","ey_plug_links_server","siteid='"&tosid&"'","")
				if ubound(data)>=0 then
					tosid_name=data(0,0)
					tosid_key=data(1,0)
					tosid_url=data(2,0)
				end if
			end if
			load plug_theme_add
		case "adddb":adddb
		case "linkedit"
			data=eycms.db.dbload(1,"linklx,sid,tosid,linktxt,linkurl","ey_plug_links_linkto","linkid="&id&"","")
			load plug_theme_edit
		case "editdb":editdb
		case "del":deldb
		case "chk":chkdb
		case "linksh":linkshdb
		case "linktb":linktbdb
		case "siteadd"
			dim my_site,my_name,my_cate,my_seokey,my_url,my_exchange,my_cross,my_lockcate,my_bend,my_limit,my_br,my_baidu,my_need_br,my_need_baidu,my_zt,my_qq,my_token
			data=eycms.db.dbload(1,"siteid,name,cate,seokey,url,exchange,iscross,lockcate,bend,limit,br,baidu,need_br,need_baidu,zt,qq,token","ey_plug_links_site","sid>0","")
			if ubound(data)>=0 then
				my_site=data(0,0)
				my_name=data(1,0)
				my_cate=data(2,0)
				my_seokey=data(3,0)
				my_url=data(4,0)
				my_exchange=data(5,0)
				my_cross=data(6,0)
				my_lockcate=data(7,0)
				my_bend=data(8,0)
				my_limit=data(9,0)
				my_br=data(10,0)
				my_baidu=data(11,0)
				my_need_br=data(12,0)
				my_need_baidu=data(13,0)
				my_zt=data(14,0)
				my_qq=data(15,0)
				my_token=data(16,0)
			else
				my_name=eycms.getsys("webname")
				my_url=weburl
				my_exchange=1
				my_cross=0
				my_lockcate=0
				my_bend=3
				my_limit=30
			end if

			load plug_theme_siteadd
		case "siteadddb":siteadddb
		case "siteedit"
			data=eycms.db.dbload(1,"name,cate,seokey,url,exchange,iscross,br,baidu,zt,bz","ey_plug_links_server","id="&id&"","")
			load plug_theme_edit_site
		case "siteeditdb":siteeditdb
		case "sitetousu":sitetousudb
		case "getkey":getkey
		case "go":response.redirect replace(eycms.fget("url",0),"!","?")
		case "site":
			dim wherestr,cate
			dim key:key=eycms.enhtml(eycms.fpost("key",0))
			cate=eycms.enhtml(eycms.fpost("cate",0))
			zt=eycms.enhtml(eycms.fget("zt",0))
			wherestr="siteid<>'"&m_site&"'"
			if eycms.strlen(m_cate)>0 then
				wherestr=wherestr&" and (lockcate=0 or (lockcate=1 and cate="&m_cate&"))"
			else
				wherestr=wherestr&" and lockcate=0"
			end if
			if eycms.strlen(zt)>0 then
				wherestr=wherestr&" and zt="&zt
			else
				wherestr=wherestr&" and zt<4"
			end if
			if eycms.strlen(cate)>0 then
				wherestr=wherestr&" and cate="&cate
			end if
			if eycms.strlen(key)>0 then 
				if datatype then
					wherestr=wherestr&" and (instr(lcase(name),lcase('"&key&"'))>0 or instr(lcase(url),lcase('"&key&"'))>0)"
				else
					wherestr=wherestr&" and (name like '%"&key&"%' or url like '%"&key&"%')"
				end if
			end if
			load plug_theme_site
		case else
			dim lx:lx=eycms.getint(eycms.fpost("lx",0),0)
			dim keyword:keyword=eycms.enhtml(eycms.fpost("key",0))
			zt=eycms.enhtml(eycms.fget("zt",0))
			dim wherekey,siteid
			wherekey="1=1"
			if eycms.strlen(keyword)>0 then 
				if datatype then
					siteid=eycms.db.dbloadone("id","ey_plug_links_server","instr(lcase(ey_plug_links_server.name),lcase('"&keyword&"'))>0 or instr(lcase(ey_plug_links_server.url),lcase('"&keyword&"'))>0")
				else
					siteid=eycms.db.dbloadone("id","ey_plug_links_server","ey_plug_links_server.name like '%"&keyword&"%' or ey_plug_links_server.url like '%"&keyword&"%'")
				end if
				if eycms.strlen(siteid)>0 then
					if lx=0 then
						wherekey=wherekey&" and ey_plug_links_linkto.sid="&siteid&" or ey_plug_links_linkto.tosid="&siteid
					elseif lx=1 then
						wherekey=wherekey&" and ey_plug_links_linkto.sid="&siteid
					elseif lx=2 then
						wherekey=wherekey&" and ey_plug_links_linkto.tosid="&siteid
					end if
				end if
			end if
			if eycms.strlen(zt)>0 then
				wherekey=wherekey&" and ey_plug_links_linkto.linkzt="&zt
			end if
			load plug_theme
	end select	

	sub getjson(byval t0,byval t1)
		eycms.echo "{""state"":"""&t0&""",""msg"":"""&t1&"""}"
	end sub

	sub getkey()
		dim at,url,id,mypai
		at=eycms.enhtml(eycms.fget("at",0))
		url=eycms.enhtml(eycms.fget("url",0))
		id=eycms.getint(eycms.fget("htmlid",0),0)
		mypai=GetSeo(at,url,id)
		if clng(mypai)=0 then
			getjson 0,"<span>0</span>"
			select case at
			case "br"
				eycms.db.dbupdate "ey_plug_links_server","id="&id&"",array(array("br",0,0,0))
			case "baidu"
				eycms.db.dbupdate "ey_plug_links_server","id="&id&"",array(array("baidu",0,0,0))
			end select
		else
			select case at
			case "br"
				getjson 0,"<a href='?act=go&url=http://rank.chinaz.com/"&url&"' target=_blank><span>"&mypai&"</span></a>"
				eycms.db.dbupdate "ey_plug_links_server","id="&id&"",array(array("br",mypai,0,0))
			case "baidu"
				getjson 0,"<span>"&mypai&"</span>"
				eycms.db.dbupdate "ey_plug_links_server","id="&id&"",array(array("baidu",mypai,0,0))
			end select
		end if
	end sub

	'保存新增链接
	sub adddb()
		dim t0,t1,t2,t3,t4,t5,i
		t0=eycms.getint(eycms.fpost("t0",0),0)
		t1=eycms.enhtml(eycms.fpost("t1",0))
		t2=eycms.enhtml(eycms.fpost("t2",0))
		t3=eycms.enhtml(eycms.fpost("t3",0))
		t4=eycms.enhtml(eycms.fpost("t4",0))
		if eycms.strlen(t1)=0 then eycms.ajaxjson "链出站点不能为空",0:exit sub
		if t0=0 then
			if eycms.strlen(t2)=0 then eycms.ajaxjson "被链站点不能为空",0:exit sub
			if t2=m_site then eycms.ajaxjson "请不要跟自己链接",0:exit sub
		end if
		if eycms.strlen(t3)=0 then eycms.ajaxjson "关键字不能为空",0:exit sub
		if eycms.strlen(t4)=0 then eycms.ajaxjson "链接网址不能为空",0:exit sub
		if left(t4,7)<>"http://" then t4 = "http://" & t4
		dim data,addtime,backmsg,json
		addtime=now()

		dim linkid
		dim linkclassid,webname,weburl
		linkclassid=eycms.db.dbloadone("classid","ey_expand_class","followid=1 and classname='云链'")
		data=eycms.db.dbload(1,"seokey,url","ey_plug_links_server","siteid='"&t2&"'","")
		if ubound(data)>=0 then
			webname=data(0,0)
			weburl=data(1,0)
		end if
		dim exchange
		backmsg=SendLinkInfo(t0,t1,t2,t3,t4,0)
		if backmsg<>"ERR" then
			set json=toobject(backmsg)
			select case json.state
			case "1"   '云端返回成功信息
				exchange=json.exchange
				if exchange="" then exchange=0
				dim linkzt
				if exchange=1 then linkzt=1 else linkzt=3
				data=array(array("linklx",t0,1,0),array("sid",t1,255,1),array("tosid",t2,255,1),array("linktxt",t3,255,1),array("linkurl",t4,255,1),array("syno",0,1,0),array("linkzt",linkzt,1,0),array("linkTime",addtime,50,1))

				if t0=0 then
					if eycms.db.dbnew("ey_plug_links_linkto",data,"(sid='"&t1&"' and tosid='"&t2&"') or (tosid='"&t1&"' and sid='"&t2&"')")=0 then
						eycms.ajaxjson "该链接已存在！",0:exit sub
					end if
				else
					if eycms.db.dbnew("ey_plug_links_linkto",data,"sid='"&t1&"' and linkurl='"&t4&"'")=0 then
						eycms.ajaxjson "该链接已存在！",0:exit sub
					end if
				end if
				linkid=eycms.db.insertid("linkid","ey_plug_links_linkto")
				if exchange=1 then
					if eycms.strlen(webname)>0 and eycms.strlen(weburl)>0 then
						data=array(array("webname",webname,52,1),array("weburl",weburl,255,1),array("weblogo",linkid,255,1),array("islogo",0,1,0),array("classid",linkclassid,0,0),array("islock",1,0,0))
						eycms.db.insert "ey_expand_link",data
					end if
				end if

				backmsg=" 云端："&json.msg
				eycms.db.dbupdate "ey_plug_links_linkto","linkid="&linkid,array(array("linkzt",linkzt,1,0))
				if t0=0 then   '互换链，同步到被链站点
					dim tositeurl
					tositeurl=eycms.db.dbloadone("url","ey_plug_links_server","siteid='"&t2&"'")
					if eycms.strlen(tositeurl)>0 and eycms.strlen(json.token)>0 then
						dim backmsg2,json2
						backmsg2=SendLinktoSite(tositeurl,t1,t2,json.token,0)
						if backmsg2<>"ERR" then
							set json2=toobject(backmsg2)
							select case json2.state
							case "1","2"
								eycms.db.dbupdate "ey_plug_links_linkto","linkid="&linkid,array(array("syno",1,1,0))
							case "101","102"
							end select
							backmsg=backmsg&" 被链："&json2.msg
							set json2=nothing
						else
							backmsg=backmsg&" 与被链站点通信失败"
						end if
					end if
				end if
				eycms.ajaxjson "保存成功！"&backmsg,1
			case "101","102"
				eycms.ajaxjson "提示！"&json.msg,0
			case "103"
				eycms.db.dbdel "ey_plug_links_server","siteid='"&t2&"'"   '删除被链站点
				eycms.ajaxjson "提示！"&json.msg,0
			end select
			set json=nothing
		else
			eycms.ajaxjson "与云端通信异常！",0
		end if
	end sub	

	'与云端链接同步数据
	function SendLinkInfo(linklx,sid,tosid,linktxt,linkurl,istb)
		dim surl:surl=apiUrl_&"plug/links/send/index.asp?act=sendlink&istb="&istb
		dim str:str=""
			str=str&"{"
			str=str&"	""linklx"":"""&linklx&""","
			str=str&"	""sid"":"""&sid&""","
			str=str&"	""tosid"":"""&tosid&""","
			str=str&"	""linktxt"":"""&linktxt&""","
			str=str&"	""linkurl"":"""&linkurl&""","
			str=str&"	""token"":"""&m_token&""""
			str=str&"}"
		SendLinkInfo=eycms.sethttp(surl,str)
	end function

	function SendLinktoSite(siteurl,sid,tosid,site_token,istb)
		dim remoteUrl:remoteUrl=siteurl
		if right(remoteUrl,1)<>"/" then remoteUrl=remoteUrl&"/"
		if left(lcase(remoteUrl),7)<>"http://" then remoteUrl="http://"&remoteUrl
		dim surl:surl=remoteUrl&"plug/links_user/back/index.asp?act=sendlink&istb="&istb
		dim str:str=""
			str=str&"{"
			str=str&"	""sid"":"""&tosid&""","
			str=str&"	""tosid"":"""&sid&""","
			str=str&"	""linktxt"":"""&m_seokey&""","
			str=str&"	""linkurl"":"""&m_url&""","
			str=str&"	""token"":"""&site_token&""""
			str=str&"}"
		SendLinktoSite=eycms.sethttp(surl,str)
	end function

	'链接同步
	sub linktbdb()
		on error resume Next
		dim fid:fid=eycms.getint(eycms.fget("fid",0),0)
		dim data,linklx,sid,tosid,linktxt,linkurl
		data=eycms.db.dbload(1,"linklx,sid,tosid,linktxt,linkurl","ey_plug_links_linkto","linkid="&fid&"","")
		if ubound(data)>=0 then
			linklx=data(0,0)
			sid=data(1,0)
			tosid=data(2,0)
			linktxt=data(3,0)
			linkurl=data(4,0)
		end if

		dim siteurl,backmsg,backmsg2,errmsg,json,json2,issucc
		issucc=0
		'通知云端************
		if eycms.strlen(sid)>0 and eycms.strlen(tosid)>0 then
			backmsg=SendLinkInfo(linklx,sid,tosid,linktxt,linkurl,1)
			if backmsg<>"ERR" then
				set json=toobject(backmsg)
				select case json.state
				case "1","2"
					issucc=issucc+1
					errmsg=errmsg&" 云端："&json.msg
					if linklx=0 then
						'通知被链站点************
						siteurl=eycms.db.dbloadone("url","ey_plug_links_server","siteid='"&tosid&"'")
						if eycms.strlen(siteurl)>0 and eycms.strlen(json.token)>0 then
							backmsg2=SendLinktoSite(siteurl,sid,tosid,json.token,1)
							if backmsg2<>"ERR" then
								set json2=toobject(backmsg2)
								select case json2.state
								case "1","2"
									issucc=issucc+1
								case "101","102"
								end select
								errmsg=errmsg&" 被链："&json2.msg
								set json2=nothing
							else
								errmsg=errmsg&" 与被链站点通信失败"
							end if
						end if
					else
						issucc=issucc+1
					end if
				case "101","102"
					errmsg=errmsg&" 云端："&json.msg
				end select
				set json=nothing
			end if
		end if

		if issucc=2 then
			eycms.db.dbupdate "ey_plug_links_linkto","linkid="&fid,array(array("syno",1,1,0))
			eycms.ajaxjson "操作成功！" & errmsg,1
		else
			eycms.ajaxjson "操作失败！" & errmsg,0
		end if
		if err then
			eycms.errlog "函数：linktbdb<br>详情："&err.description,0
			eycms.echo "0发生错误："&err.description
			err.clear
		End if
	end sub	

	'与云端链接同步数据:审核链接
	function shenheLinkInfo(linklx,sid,tosid,linktxt,linkurl)
		dim surl:surl=apiUrl_&"plug/links/send/index.asp?act=shenhelink"
		dim str:str=""
			str=str&"{"
			str=str&"	""linklx"":"""&linklx&""","
			str=str&"	""sid"":"""&sid&""","
			str=str&"	""tosid"":"""&tosid&""","
			str=str&"	""linktxt"":"""&linktxt&""","
			str=str&"	""linkurl"":"""&linkurl&""","
			str=str&"	""token"":"""&m_token&""""
			str=str&"}"
		shenheLinkInfo=eycms.sethttp(surl,str)
	end function

	function shenheLinktoSite(siteurl,sid,tosid,site_token)
		dim remoteUrl:remoteUrl=siteurl
		if right(remoteUrl,1)<>"/" then remoteUrl=remoteUrl&"/"
		if left(lcase(remoteUrl),7)<>"http://" then remoteUrl="http://"&remoteUrl
		dim surl:surl=remoteUrl&"plug/links_user/back/index.asp?act=shenhelink"
		dim str:str=""
			str=str&"{"
			str=str&"	""sid"":"""&tosid&""","
			str=str&"	""tosid"":"""&sid&""","
			str=str&"	""linktxt"":"""&m_seokey&""","
			str=str&"	""linkurl"":"""&m_url&""","
			str=str&"	""token"":"""&site_token&""""
			str=str&"}"
		shenheLinktoSite=eycms.sethttp(surl,str)
	end function

	'链接审核
	sub linkshdb()
		on error resume Next
		dim fid:fid=eycms.getint(eycms.fget("fid",0),0)
		dim data,linklx,sid,tosid,linktxt,linkurl
		data=eycms.db.dbload(1,"linklx,sid,tosid,linktxt,linkurl","ey_plug_links_linkto","linkid="&fid&"","")
		if ubound(data)>=0 then
			linklx=data(0,0)
			sid=data(1,0)
			tosid=data(2,0)
			linktxt=data(3,0)
			linkurl=data(4,0)
		end if

		dim siteurl,backmsg,backmsg2,errmsg,json,json2,issucc
		issucc=0
		'通知云端************
		if eycms.strlen(sid)>0 and eycms.strlen(tosid)>0 then
			backmsg=shenheLinkInfo(linklx,sid,tosid,linktxt,linkurl)
			if backmsg<>"ERR" then
				set json=toobject(backmsg)
				select case json.state
				case "1","2"
					issucc=issucc+1
					errmsg=errmsg&" 云端："&json.msg
					if linklx=0 then
						'通知被链站点************
						siteurl=eycms.db.dbloadone("url","ey_plug_links_server","siteid='"&tosid&"'")
						if eycms.strlen(siteurl)>0 and eycms.strlen(json.token)>0 then
							backmsg2=shenheLinktoSite(siteurl,tosid,sid,json.token)
							if backmsg2<>"ERR" then
								set json2=toobject(backmsg2)
								select case json2.state
								case "1","2"
									issucc=issucc+1
								case "101","102"
								end select
								errmsg=errmsg&" 被链："&json2.msg
								set json2=nothing
							else
								errmsg=errmsg&" 与被链站点通信失败"
							end if
						end if
					else
						issucc=issucc+1
					end if
				case "101","102"
					errmsg=errmsg&" 云端："&json.msg
				end select
				set json=nothing
			end if
		end if

		if issucc=2 then
			dim linkclassid
			linkclassid=eycms.db.dbloadone("classid","ey_expand_class","followid=1 and classname='云链'")
			data=array(array("webname",linktxt,52,1),array("weburl",linkurl,255,1),array("weblogo",fid,255,1),array("islogo",0,1,0),array("classid",linkclassid,0,0),array("islock",1,0,0))
			eycms.db.insert "ey_expand_link",data
			eycms.db.dbupdate "ey_plug_links_linkto","linkid="&fid,array(array("linkzt",1,1,0))
			eycms.ajaxjson "操作成功！" & errmsg,1
		else
			eycms.ajaxjson "操作失败！" & errmsg,0
		end if
		if err then
			eycms.errlog "函数：linkshdb<br>详情："&err.description,0
			eycms.echo "0发生错误："&err.description
			err.clear
		End if
	end sub	

	'编辑链接
	sub editdb
		dim t0
		t0=eycms.enhtml(eycms.fpost("t0",0))
		if eycms.strlen(t0)=0 then eycms.ajaxjson "关键词不能为空",0:exit sub
		dim data,id,ddata
		id=eycms.getint(eycms.fget("id",0),0)
		data=array(array("linktxt",t0,255,1))
		if eycms.db.dbupdate("ey_plug_links_linkto","linkid="&id&"",data)=1 then
			dim cid:cid=eycms.db.dbloadone("classid","ey_expand_class","followid=1 and classname='云链'")
			if cid>0 then
				eycms.db.dbupdate "ey_expand_link","classid="&cid&" and weblogo='"&id&"'",array(array("webname",t0,255,1))
			end if
			eycms.ajaxjson "保存成功",1
		end if
	end sub
	
	'删除链接
	sub deldb()
		dim fid:fid=eycms.getint(eycms.fget("fid",0),0)
		dim data,sid,tosid
		data=eycms.db.dbload(1,"sid,tosid","ey_plug_links_linkto","linkid="&fid&"","")
		if ubound(data)>=0 then
			sid=data(0,0)
			tosid=data(1,0)
		end if
		if eycms.strlen(sid)>0 and eycms.strlen(tosid)>0 then
			'通知云端************
			dim backmsg,json
			backmsg=SendLinkDel(sid,tosid)
			if backmsg<>"ERR" then
				set json=toobject(backmsg)
				select case json.state
				case "1","2"
					backmsg=" 云端："&json.msg
					if eycms.strlen(tosid)>0 then
						'通知互链站点************
						dim tositeurl
						tositeurl=eycms.db.dbloadone("url","ey_plug_links_server","siteid='"&tosid&"'")
						if eycms.strlen(tositeurl)>0 and eycms.strlen(json.token)>0 then
							dim backmsg2,json2
							backmsg2=SendLinkDelTosite(tositeurl,sid,tosid,json.token)
							if backmsg2<>"ERR" then
								set json2=toobject(backmsg2)
								select case json2.state
								case "1"
								case "101","102"
								end select
								backmsg=backmsg&" 被链："&json2.msg
								set json2=nothing
							else
								backmsg=backmsg&" 与被链站点通信失败"
							end if
						end if
					end if

					'删除链接记录************
					eycms.db.dbdel "ey_plug_links_linkto","linkid="&fid&""
					dim cid:cid=eycms.db.dbloadone("classid","ey_expand_class","followid=1 and classname='云链'")
					if cid>0 then
						eycms.db.dbdel "ey_expand_link","classid="&cid&" and weblogo='"&fid&"'"
					end if

					eycms.ajaxjson "保存成功！"&backmsg,1
				case "101","102"
					eycms.ajaxjson "提示！"&json.msg,0
				end select
				set json=nothing
			end if
		else
			eycms.ajaxjson "参数错误！",0
		end if
	end sub

	'检验链接
	sub chkdb()
		dim fid:fid=eycms.getint(eycms.fget("fid",0),0)
		dim data,linklx,sid,tosid,linktxt,linkurl,linkzt
		data=eycms.db.dbload(1,"linklx,sid,tosid,linktxt,linkurl,linkzt","ey_plug_links_linkto","linkid="&fid&"","")
		if ubound(data)>=0 then
			linklx=data(0,0)
			sid=data(1,0)
			tosid=data(2,0)
			linktxt=data(3,0)
			linkurl=data(4,0)
			linkzt=data(5,0)
		end if
		if linkzt<>0 and linkzt<>3 then
			if linklx=0 then
				dim tositeurl,sitehtml
				tositeurl=eycms.db.dbloadone("url","ey_plug_links_server","siteid='"&tosid&"'")
				if eycms.strlen(tositeurl)>0 then
					'检验链接有效性************
					sitehtml=eycms.gethttp(tositeurl,"get")
					if sitehtml="ERR" then
						getjson 0,"<span>异常</span>"
						eycms.db.dbupdate "ey_plug_links_linkto","linkid="&fid,array(array("linkzt",4,1,0))
					else
						sitehtml=RegRemoveHref(sitehtml)
						If Instr(sitehtml,m_url) > 0 Then
							getjson 0,"<span>正常</span>"
							eycms.db.dbupdate "ey_plug_links_linkto","linkid="&fid,array(array("linkzt",1,1,0))
						else
							getjson 0,"<span>失效</span>"
							eycms.db.dbupdate "ey_plug_links_linkto","linkid="&fid,array(array("linkzt",2,1,0))
						end if
					end if
				else
					getjson 0,"<span>参数错误</span>"
				end if
			else
				getjson 0,"<span>正常</span>"
			end if
		else
			if linkzt=0 then getjson 0,"<a href='javascript:;' class='linksh' rel='"&fid&"'><font color=blue>请求</font></a>"
			if linkzt=3 then getjson 0,"<font color=gray>待审</font>"
		end if
	end sub

	Function RegRemoveHref(HTMLstr) 
		dim regex,matches,match
		Set regex = New RegExp 
		regex.IgnoreCase = True 
		regex.Global = True 
		regex.Pattern = "<a[^>]+>(.+?)<\/a>" 
		set matches = regex.execute(HTMLstr)
		if matches.count>0 then
			for each match in matches
				if instr(match.value,"nofollow") then
					HTMLstr = Replace(HTMLstr, match.value, "")
				end if
			next
		end If
		set matches = nothing
		set regex=nothing
		RegRemoveHref=HTMLstr
	end Function 

	'通知云端删除链接
	function SendLinkDel(sid,tosid)   
		dim surl:surl=apiUrl_&"plug/links/send/index.asp?act=linkdel"
		dim str:str=""
			str=str&"{"
			str=str&"	""sid"":"""&sid&""","
			str=str&"	""tosid"":"""&tosid&""","
			str=str&"	""token"":"""&m_token&""""
			str=str&"}"
		SendLinkDel=eycms.sethttp(surl,str)
	end function

	'通知互联站点删除链接
	function SendLinkDelTosite(siteurl,sid,tosid,site_token)   
		dim remoteUrl:remoteUrl=siteurl
		if right(remoteUrl,1)<>"/" then remoteUrl=remoteUrl&"/"
		if left(lcase(remoteUrl),7)<>"http://" then remoteUrl="http://"&remoteUrl
		dim surl:surl=remoteUrl&"plug/links_user/back/index.asp?act=sendlinkdel"
		dim str:str=""
			str=str&"{"
			str=str&"	""sid"":"""&tosid&""","
			str=str&"	""tosid"":"""&sid&""","
			str=str&"	""token"":"""&site_token&""""
			str=str&"}"
		SendLinkDelTosite=eycms.sethttp(surl,str)
	end function

	'链接库站点备注
	sub siteeditdb()
		dim t0
		t0=eycms.enhtml(eycms.fpost("t0",0))
		dim data,id,ddata
		id=eycms.getint(eycms.fget("id",0),0)
		data=array(array("bz",t0,50,1))
		if eycms.db.dbupdate("ey_plug_links_server","id="&id&"",data)=1 then
			eycms.ajaxjson "保存成功",1
		end if
	end sub
	
	'投诉
	sub sitetousudb()
		dim fid:fid=eycms.enhtml(eycms.fget("id",0))
		dim t0:t0=eycms.enhtml(eycms.fget("t0",0))
		dim backmsg,json
		backmsg=SendLinkTouSu(t0,fid)
		if backmsg<>"ERR" then
			set json=toobject(backmsg)
			select case json.state
			case "1"
				eycms.echo "1投诉成功！"&json.msg
			case "101","102"
				eycms.echo "0投诉失败！"&json.msg
			end select
			set json=nothing
		else
			eycms.echo "0投诉失败！与云端通信失败"
		end if
	end sub

	'通知云端投诉站点
	function SendLinkTouSu(msg,tosid)   
		dim surl:surl=apiUrl_&"plug/links/send/index.asp?act=linktousu"
		dim str:str=""
			str=str&"{"
			str=str&"	""msg"":"""&msg&""","
			str=str&"	""sid"":"""&m_site&""","
			str=str&"	""tosid"":"""&tosid&""","
			str=str&"	""token"":"""&m_token&""""
			str=str&"}"
		SendLinkTouSu=eycms.sethttp(surl,str)
	end function

	'保存本站信息设置
	sub siteadddb()
		dim t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,i
		t0=eycms.enhtml(eycms.fpost("t0",0))     '站点名称
		t1=eycms.getint(eycms.fpost("t1",0),0)   '所属行业
		t2=eycms.enhtml(eycms.fpost("t2",0))     '关键字
		t3=eycms.enhtml(eycms.fpost("t3",0))     '链接网址
		t4=eycms.getint(eycms.fpost("t4",0),0)     '自动互换
		t5=eycms.getint(eycms.fpost("t5",0),0)     '自动交叉
		t6=eycms.getint(eycms.fpost("t6",0),0)     'br要求
		t7=eycms.getint(eycms.fpost("t7",0),0)     '收录要求
		t8=eycms.enhtml(eycms.fpost("t8",0))       'QQ
		t9=eycms.getint(eycms.fpost("t9",0),0)     '仅限同行
		t10=eycms.getint(eycms.fpost("t10",0),3)   '换链频率
		t11=eycms.getint(eycms.fpost("t11",0),30)  '换链上限
		if eycms.strlen(t0)=0 then eycms.ajaxjson "站点名称不能为空",0:exit sub
		if t1=0 then eycms.ajaxjson "请选择所属行业",0:exit sub
		if eycms.strlen(t2)=0 then eycms.ajaxjson "关键字不能为空",0:exit sub
		if eycms.strlen(t3)=0 then eycms.ajaxjson "链接网址不能为空",0:exit sub
		if left(t3,7)<>"http://" then t3 = "http://" & t3
		if left(t3,11)<>"http://www." then eycms.ajaxjson "只有绑定正式域名的站点才可接入",0:exit sub
		if instr(weburl,filterurl(t3))<=0 then eycms.ajaxjson "链接网址与本站网址不同",0:exit sub
		if eycms.strlen(t8)=0 then eycms.ajaxjson "联系QQ不能为空",0:exit sub

		dim data,siteid,br,baidu,addtime,backmsg,json
		if eycms.db.dbcount("ey_expand_class","followid=1 and classname='云链'")=0 then
			eycms.db.insert "ey_expand_class",array(array("followid",1,1,0),array("classname","云链",0,1),array("ordnum",0,0,0))
		end if

		siteid=m_site
		if eycms.strlen(siteid)=0 then siteid=getsiteid
		addtime=now()
		br=GetSeo("br",t3,"mysite")
		baidu=GetSeo("baidu",t3,"mysite")
		data=array(array("siteid",siteid,255,1),array("name",t0,255,1),array("cate",t1,0,0),array("seokey",t2,255,1),array("url",t3,255,1),array("exchange",t4,1,0),array("iscross",t5,1,0),array("lockcate",t9,1,0),array("bend",t10,0,0),array("limit",t11,0,0),array("br",br,1,0),array("baidu",baidu,10,0),array("need_br",t6,1,0),array("need_baidu",t7,10,0),array("qq",t8,20,1),array("zt",0,1,0),array("AddTime",addtime,50,1))

		if eycms.db.dbnew("ey_plug_links_site",data,"sid>0")=0 then
			data=array(array("name",t0,255,1),array("cate",t1,0,0),array("seokey",t2,255,1),array("url",t3,255,1),array("exchange",t4,1,0),array("iscross",t5,1,0),array("lockcate",t9,1,0),array("bend",t10,0,0),array("limit",t11,0,0),array("br",br,1,0),array("baidu",baidu,10,0),array("need_br",t6,1,0),array("need_baidu",t7,10,0),array("qq",t8,20,1),array("zt",0,1,0))
			if eycms.db.dbupdate("ey_plug_links_site","sid>0",data)=1 then
				backmsg=SendSiteInfo(siteid,t0,t1,t2,t3,t4,t5,br,baidu,t6,t7,t8,t9,t10,t11)
				if backmsg<>"ERR" then
					set json=toobject(backmsg)
					select case json.state
					case "1","2"
						if eycms.strlen(json.token)>0 then
							eycms.db.dbupdate "ey_plug_links_site","sid>0",array(array("zt",3,1,0),array("token",json.token,255,1))
						else
							eycms.db.dbupdate "ey_plug_links_site","sid>0",array(array("zt",3,1,0))
						end if
						eycms.ajaxjson "保存成功！"&json.msg,1
					case "101","102"
						eycms.db.dbupdate "ey_plug_links_site","sid>0",array(array("zt",2,1,0))
						eycms.ajaxjson "提示！"&json.msg,1
					case "103"
						eycms.db.dbupdate "ey_plug_links_site","sid>0",array(array("zt",4,1,0))
						eycms.ajaxjson "提示！"&json.msg,1
					end select
					set json=nothing
				else
					eycms.db.dbupdate "ey_plug_links_site","sid>0",array(array("zt",4,1,0))
					eycms.ajaxjson "保存成功！但与云端通信异常，请稍后再试",1
				end if
			end if
		else
			backmsg=SendSiteInfo(siteid,t0,t1,t2,t3,t4,t5,br,baidu,t6,t7,t8,t9,t10,t11)
			if backmsg<>"ERR" then
				set json=toobject(backmsg)
				select case json.state
				case "1","2"
					if eycms.strlen(json.token)>0 then
						eycms.db.dbupdate "ey_plug_links_site","sid>0",array(array("zt",3,1,0),array("token",json.token,255,1))
					else
						eycms.db.dbupdate "ey_plug_links_site","sid>0",array(array("zt",3,1,0))
					end if
					eycms.ajaxjson "保存成功！"&json.msg,1
				case "101","102"
					eycms.db.dbupdate "ey_plug_links_site","sid>0",array(array("zt",2,1,0))
					eycms.ajaxjson "提示！"&json.msg,1
				case "103"
					eycms.db.dbupdate "ey_plug_links_site","sid>0",array(array("zt",4,1,0))
					eycms.ajaxjson "提示！"&json.msg,1
				end select
				set json=nothing
			else
				eycms.db.dbupdate "ey_plug_links_site","sid>0",array(array("zt",4,1,0))
				eycms.ajaxjson "保存成功！但与云端通信异常，请稍后再试",1
			end if
		end if
	end sub	

	function SendSiteInfo(siteid,name,cate,seokey,url,exchange,iscross,br,baidu,need_br,need_baidu,qq,lockcate,bend,limit)
		dim surl:surl=apiUrl_&"plug/links/send/index.asp?act=sendsite"
		dim str:str=""
			str=str&"{"
			str=str&"	""siteid"":"""&siteid&""","
			str=str&"	""name"":"""&name&""","
			str=str&"	""cate"":"""&cate&""","
			str=str&"	""seokey"":"""&seokey&""","
			str=str&"	""url"":"""&url&""","
			str=str&"	""exchange"":"""&exchange&""","
			str=str&"	""iscross"":"""&iscross&""","
			str=str&"	""br"":"""&br&""","
			str=str&"	""baidu"":"""&baidu&""","
			str=str&"	""need_br"":"""&need_br&""","
			str=str&"	""need_baidu"":"""&need_baidu&""","
			str=str&"	""qq"":"""&qq&""","
			str=str&"	""lockcate"":"""&lockcate&""","
			str=str&"	""bend"":"""&bend&""","
			str=str&"	""limit"":"""&limit&""""
			str=str&"}"
		SendSiteInfo=eycms.sethttp(surl,str)
	end function

	'获取对应的字段类型
	function get_settype(byval t0)
		if not isnumeric(t0) then get_settype="参数不正确":exit function
		select case t0
			case "1":get_settype="服装、饰品、个人护理"
			case "2":get_settype="文化、广告、设计服务"
			case "3":get_settype="五金、设备、工业制品"
			case "4":get_settype="IT、互联网、行业门户"
			case "5":get_settype="教育、政府、机构组织"
			case "6":get_settype="化工、原材料、农畜牧"
			case "7":get_settype="金融、运输、工商服务"
			case "8":get_settype="食品、茶饮、养生保健"
			case "9":get_settype="数码、家具、家居百货"
			case "10":get_settype="婚庆、摄影、生活服务"
			case "11":get_settype="餐饮、酒店、旅游服务"
			case "12":get_settype="礼品、玩具、小商品"
			case "13":get_settype="其他"
		end select
	end function

	Function GetSeo(sl,sw,id)
	    If sl="" or IsNull(sl)=True Or sw="" or IsNull(sw)=True Then
			GetSeo="0"
			Exit Function
	    End If
		Dim ContentStr,StartB,OverB,CutsStr,url
		url=filterurl(sw)
		if sl="baidu" Then   '百度收录
			ContentStr = eycms.gethttp("http://www.baidu.com/s?wd=site:"&url&"&cl=3","get")		 '要提取页面的url地址
			StartB="找到相关结果数约"
			OverB="个"
			CutsStr=GetBody(ContentStr,StartB,OverB,false,false)
			If CutsStr="" Then 
				StartB="该网站共有"
				OverB="个网页被百度收录"
				CutsStr=GetBody(ContentStr,StartB,OverB,false,false)
				CutsStr=eycms.nohtml(CutsStr)
				CutsStr=replace(CutsStr,",","")
				CutsStr=replace(CutsStr," ","")
				CutsStr=replace(CutsStr,chr(10),"")
				If CutsStr="" Then CutsStr=0
			end if
		elseif sl="br" then
			ContentStr = sendhttp("http://api.eycms.cn/getbr.php","host="&url) 
			if ContentStr<>"ERR" then
				dim obj,ii
				Set obj = toArray(ContentStr)
				For ii=0 To obj.Count-1
				  if obj(ii)("typeName")="baidupc" then CutsStr=obj(ii)("br") 
				next
				Set obj = Nothing
				if CutsStr="--" then CutsStr=0
			else
				ContentStr = eycms.gethttp("http://br.links.cn/getbr.asp?url="&url&"&style=5","get") 
				if ContentStr<>"ERR" then
					CutsStr=eycms.dishtml(ContentStr)
					CutsStr=replace(CutsStr,"document.write(""","")
					CutsStr=replace(CutsStr,""");","")
				end if
			end if
			If CutsStr="" Then CutsStr=0
		end If
		GetSeo=CutsStr
	end Function

	function sendhttp(byval t0,byval t1)
		on error resume next
		dim http
		set http=server.createobject("MSXML2.ServerXMLHTTP")
			http.SetTimeOuts 20000, 20000, 30000, 30000
			http.open "POST",t0,False
			http.setrequestheader "Referer",t0
			http.setrequestheader "User-Agent", "Mozilla/4.0"
			http.setrequestheader "Connection", "Keep-Alive"
			http.setrequestheader "Content-Length",len(t1)
			http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"			
			http.send(t1)
			If http.Readystate<>4 then
				Set http=nothing
				sendhttp="ERR"
				Exit function
			End if
			if http.status=200 then
				sendhttp=http.responsetext
			else
				sendhttp="ERR"
				Set http=nothing
				Exit function
			end if
		set http=nothing
		if err then 
			sendhttp="ERR"
			eycms.errlog "网址："&t0&"<br>函数：sendhttp<br>详情："&err.description,0
			err.clear
		end if
	end Function

	Function GetBody(ConStr,StartStr,OverStr,IncluL,IncluR)
	   If ConStr="" or IsNull(ConStr)=True Or StartStr="" or IsNull(StartStr)=True Or OverStr="" or IsNull(OverStr)=True Then
		  GetBody="0"
		  Exit Function
	   End If
	   Dim ConStrTemp
	   Dim Start,Over
	   ConStrTemp=Lcase(ConStr)
	   StartStr=Lcase(StartStr)
	   OverStr=Lcase(OverStr)
	   Start = InStrB(1, ConStrTemp, StartStr, vbBinaryCompare)
	   If Start<=0 then
		  GetBody=""
		  Exit Function
	   Else
		  If IncluL=False Then
			 Start=Start+LenB(StartStr)
		  End If
	   End If
	   Over=InStrB(Start,ConStrTemp,OverStr,vbBinaryCompare)
	   If Over<=0 Or Over<=Start then
		  GetBody=""
		  Exit Function
	   Else
		  If IncluR=True Then
			 Over=Over+LenB(OverStr)
		  End If
	   End If
	   GetBody=MidB(ConStr,Start,Over-Start)
	End Function

	function get_cache(byval t0,byval t1)
		if t0="baidu" then
			if eycms.checkcache("check_pai_baidu_"&t1,1) then
				get_cache=eycms.loadcache("check_pai_baidu_"&t1,1)
				if get_cache=0 then get_cache="0"
			else
				get_cache="-"
			end if
		else
			if eycms.checkcache("check_pai_br_"&t1,1) then
				get_cache=eycms.loadcache("check_pai_br_"&t1,1)
				if get_cache=0 then get_cache="0"
			else
				get_cache="-"
			end if
		end if
	end function

	function getcustomer(byval t0)
		getcustomer=""
		if eycms.strlen(t0)<=0 then
			exit function
		end If
		getcustomer=eycms.db.dbloadone("name","ey_plug_links_server","siteid='"&t0&"'")
	end Function

	function getislink(byval t0)
		getislink=false
		if eycms.strlen(t0)<=0 then
			exit function
		end If
		if eycms.db.dbcount("ey_plug_links_linkto","tosid='"&t0&"'")>0 Then
			getislink=true
		end if
	end Function

	function filterurl(byval t0)
		filterurl=t0
		if eycms.strlen(t0)<=0 then
			exit function
		end If
		filterurl=replace(t0,"http://","")
		if instr(filterurl,"/") then filterurl=split(filterurl,"/")(0)
	end Function

	function getsiteid()
		dim dtmnow, m_strdate, m_lngtime
		dtmnow = date()
		m_strdate = right(year(dtmnow),2)&right("0"&month(dtmnow),2)&right("0"&day(dtmnow),2)
		m_lngtime = clng(timer()*1000)+1
		getsiteid = "site" & m_strdate & right("00000000" & m_lngtime,8)
	end function

	function isnewversion()
		isnewversion=false
		dim myvs,remotevs
		if eycms.isfile(webroot&"plug/links_user/xml/"&site_root&"/version.txt") then
			myvs=eycms.loadfile(webroot&"plug/links_user/xml/"&site_root&"/version.txt")
			remotevs=eycms.gethttp(apiUrl_&"plug/links/xml/version.txt","")
			if remotevs="ERR" then remotevs=""
			if eycms.strlen(remotevs)>0 then
				if DateDiff("d",myvs,remotevs)>0 then isnewversion=true
			end if
		else
			isnewversion=true
		end if
	end function

	eycms.db.dbclose
%>
<script language="jscript" runat="server">
function toobject(json)
{
	eval("var o="+json);return o;
}
function toArray(json){
    var dic = Server.CreateObject("Scripting.Dictionary")
    eval("var a=" + json);
    for(var i=0;i<a.length;i++){
        var obj = Server.CreateObject("Scripting.Dictionary")
        for(x in a[i]) obj.Add(x,a[i][x])
        dic.Add(i, obj);
    }
    return dic
}
</script>