<!--#include file="../../lib/base.asp"-->
<!--#include file="../../lib/cmd.asp"-->
<%
''' 排名监控插件
''' ==================================================================
	eycms.stophack
	'注释：插件是否安装
	is_plug_install "eycms.plug.seo"
	
	'注释：管理权限检查
	is_plug_login
	
	'注释：定义插件模板路径
	dim theme_admin_root:theme_admin_root="admin_leo"
	dim plug_theme,plug_theme_add,plug_theme_edit
	plug_theme="module/plug_seo_view/list.html"
	plug_theme_add="module/plug_seo_view/add.html"
	plug_theme_edit="module/plug_seo_view/edit.html"
	
	'接收fget传值
	dim act:act=lcase(eycms.fget("act",0))
	dim id:id=eycms.getint(eycms.fget("id",0),0)
	dim data
			
	select case act
		case "add"
			load plug_theme_add
		case "adddb":adddb
		case "edit"
			data=eycms.db.dbload(1,"seoWord,seoUrl,seoDesc,seoType,ordnum","ey_plug_seo","id="&id&"","")
			load plug_theme_edit
		case "editdb":editdb
		case "order":ordernum
		case "del":deldb
		case "getkey":getkey
		case else
			load plug_theme
	end select	

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

	sub getkey()
		dim at,wd,dn,id,mypai
		at=eycms.enhtml(eycms.fget("at",0))
		wd=eycms.enhtml(eycms.fget("wd",0))
		dn=eycms.enhtml(eycms.fget("dn",0))
		id=eycms.getint(eycms.fget("htmlid",0),0)
		mypai=GetSeo(at,wd,dn,id)
		if clng(mypai)=0 then
			getjson 0,"<span>50名以外</span>"
		else
			getjson 0,"<span>"&mypai&"</span>"
		end if
	end sub

	'保存新增字段
	sub adddb()
		dim t0,t1,t2,t3,t4,t5,i
		t0=eycms.enhtml(eycms.fpost("t0",0))
		t1=eycms.enhtml(eycms.fpost("t1",0))
		t2=eycms.enhtml(eycms.fpost("t2",0))
		t3=eycms.getint(eycms.fpost("t3",0),0)
		t4=eycms.getint(eycms.fpost("t4",0),0)
		if eycms.strlen(t0)=0 then eycms.ajaxjson "关键词不能为空",0:exit sub

		t0=replace(t0,chr(13),"")
		t5=split(t0,chr(10))
		for i=0 to ubound(t5)
			if eycms.strlen(t5(i))>0 then
				add_key_one array(t5(i),t1,t2,t3,t4)
			end if
		next
		eycms.ajaxjson "保存成功",1
	end sub	

	sub add_key_one(byval str)
		dim t0,t1,t2,t3,t4
		if not isarray(str) then exit sub
		t0=str(0)
		t1=str(1)
		t2=str(2)
		t3=str(3)
		t4=str(4)
		dim addtime
		addtime=now()
		dim data,ddata
		data=array(array("seoWord",t0,50,1),array("seoUrl",t1,50,1),array("seoDesc",t2,255,1),array("seoType",t3,1,0),array("ordnum",t4,0,0),array("AddTime",addtime,50,1))
		if eycms.db.dbnew("ey_plug_seo",data,"seoWord='"&t0&"'")=0 then
			eycms.ajaxjson "关键词已存在，请换个试试",0:exit sub
		end if
	end sub	

	'编辑
	sub editdb
		dim t0,t1,t2,t3,t4
		t0=eycms.enhtml(eycms.fpost("t0",0))
		t1=eycms.enhtml(eycms.fpost("t1",0))
		t2=eycms.enhtml(eycms.fpost("t2",0))
		t3=eycms.getint(eycms.fpost("t3",0),0)
		t4=eycms.getint(eycms.fpost("t4",0),0)
		dim data,id,addtime,ddata
		addtime=now()
		id=eycms.getint(eycms.fget("id",0),0)
		data=array(array("seoUrl",t1,50,1),array("seoDesc",t2,255,1),array("seoType",t3,1,0),array("ordnum",t4,0,0),array("AddTime",addtime,50,1))
		if eycms.db.dbupdate("ey_plug_seo","id="&id&"",data)=1 then
			eycms.ajaxjson "保存成功",1
		end if
	end sub
	
	'排序
	sub ordernum()
		dim t0,t1,t2,t3,i
		t0=eycms.enhtml(eycms.fpost("fid",0))
		t1=eycms.enhtml(eycms.fpost("ordnum",0))
		t2=split(t0,",")
		t3=split(t1,",")
		if ubound(t2)-ubound(t3)<>0 then eycms.ajaxjson "0&#21442;&#25968;&#38169;&#35823;",0:exit sub
		for i=0 to ubound(t2)
			if eycms.isnum(t2(i)) then
				eycms.db.dbupdate "ey_plug_seo","id="&t2(i)&"",array(array("ordnum",t3(i),10,0,0))
			end if
		next
		eycms.ajaxjson "保存成功",1
	end sub
	
	'删除自定义字段
	sub deldb()
		dim fid:fid=eycms.getint(eycms.fget("fid",0),0)
		'删除ey_plug_filed中的字段
		eycms.db.dbdel "ey_plug_seo","id="&fid&""
		'打印处理结果		
		eycms.echo "1&#21024;&#38500;&#25104;&#21151;"
	end sub
	
	'获取对应的字段类型
	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="长尾词"
		end select
	end function

	Function GetSeo(at,wd,dn,id)
	    If at="" or IsNull(at) Or wd="" or IsNull(wd) Or dn="" or IsNull(dn) Then
			GetSeo=0
			Exit Function
	    End If
	    dn=replace(dn,"www.","")
		Dim ContentStr, ContentStr_v, C_baidu_v, C_360, C_360_v, C_soso, C_soso_v, b, y, i
		if at="baidu" Then   '百度收录
			if eycms.checkcache("check_pai_baidu_"&id,1) then
				GetSeo=eycms.loadcache("check_pai_baidu_"&id,1)
			else
				ContentStr=eycms.gethttp("http://www.baidu.com/s?rn=50&wd="&wd&"&pn=0","get")
				If Instr(ContentStr,"推广</a>") > 0 Then
					ContentStr_v = Split(ContentStr,">百度推广</a>")
					ContentStr = ContentStr_v(ubound(ContentStr_v))
				End if

				'利用快照确定位置,注意搜索引擎修改这块的参数
				If Instr(ContentStr,dn) <> 0 then
					C_baidu_v = Split(ContentStr,"快照</a>")
					b = ubound(C_baidu_v)
					For y = 0 to b - 1
						If Instr(C_baidu_v(y),dn) <> 0 then
							GetSeo = y+1
							eycms.setcache "check_pai_baidu_"&id,y+1,1
							Exit For
						End if
					Next
				Else
					GetSeo=0
					eycms.setcache "check_pai_baidu_"&id,0,1
				End if
			End if
		elseif at="360" then
			if eycms.checkcache("check_pai_360_"&id,1) then
				GetSeo=eycms.loadcache("check_pai_360_"&id,1)
			else
				C_360 = ""
				for i=1 to 5
					ContentStr=eycms.gethttp("http://www.so.com/s?ie=utf-8&shb=1&src=360sou_newhome&q="&wd&"&pn="&i,"get")
					If Instr(ContentStr,"广告</em>") > 0 Then
						ContentStr_v = Split(ContentStr,"广告</em>")
						ContentStr = ContentStr_v(ubound(ContentStr_v))
					End if
					C_360 = C_360 & ContentStr
				next

				'利用快照确定位置,注意搜索引擎修改这块的参数
				If Instr(C_360,dn) <> 0 then
					C_360_v = Split(C_360,"快照</a>")
					b = ubound(C_360_v)
					For y = 0 to b - 1
						If Instr(C_360_v(y),dn) <> 0 then
							GetSeo = y+1
							eycms.setcache "check_pai_360_"&id,y+1,1
							Exit For
						End if
					Next
				Else
					GetSeo=0
					eycms.setcache "check_pai_360_"&id,0,1
				End if
			End if
		elseif at="soso" then
			if eycms.checkcache("check_pai_soso_"&id,1) then
				GetSeo=eycms.loadcache("check_pai_soso_"&id,1)
			else
				C_soso = ""
				for i=0 to 5
					ContentStr=eycms.gethttp("http://m.baidu.com/s?wd="&wd&"&pn="&cstr(i*10),"get")
					If Instr(ContentStr,"<div id=""results"" class=""results"">") > 0 Then
						ContentStr_v = Split(ContentStr,"<div id=""results"" class=""results"">")
						ContentStr = ContentStr_v(ubound(ContentStr_v))
						If Instr(ContentStr,"<div id=""page-relative"">") > 0 Then
							ContentStr_v = Split(ContentStr,"<div id=""page-relative"">")
							ContentStr = ContentStr_v(0)
						End if
					End if
					C_soso = C_soso & ContentStr
				next

				'利用快照确定位置,注意搜索引擎修改这块的参数
				If Instr(C_soso,dn) <> 0 then
					C_soso_v = Split(C_soso,"<div class=""result c-result")
					b = ubound(C_soso_v)
					For y = 0 to b - 1
						If Instr(C_soso_v(y),dn) <> 0 then
							GetSeo = y+1
							eycms.setcache "check_pai_soso_"&id,y+1,1
							Exit For
						End if
					Next
				Else
					GetSeo=0
					eycms.setcache "check_pai_soso_"&id,0,1
				End if
			End if
		end If
	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="50名以外"
			else
				get_cache="-"
			end if
		elseif t0="360" then
			if eycms.checkcache("check_pai_360_"&t1,1) then
				get_cache=eycms.loadcache("check_pai_360_"&t1,1)
				if get_cache=0 then get_cache="50名以外"
			else
				get_cache="-"
			end if
		else
			if eycms.checkcache("check_pai_soso_"&t1,1) then
				get_cache=eycms.loadcache("check_pai_soso_"&t1,1)
				if get_cache=0 then get_cache="50名以外"
			else
				get_cache="-"
			end if
		end if
	end function

	eycms.db.dbclose
%>