<!--#include file="../conn.asp"-->
<% 
 '===========================================================================================
	'ƣħMacCMS
	'ߣMagicBlack    'ٷվhttp://www.maccms.com/
	'Copyright (C) 2009 - 2010 ... maccms.com  All Rights Reserved.
	'֣:
	'    1κθ˻֯ӯΪĿķ,޸,һйڰȨϢ
	'    2˱ķ׷Ȩ
'==========================================================================================
%>
<%
Response.Buffer = true
Server.ScriptTimeOut=999999
dim xmlhttpOBj : set xmlhttpOBj= new AppXMLHTTP
dim action,rpage,cjurl,rtype

action = safeData("all","action")
rtype = safeData("all","rtype") : rtype=chkSql(rtype,true)
rpage= safeData("all","rpage") : rpage=chkSql(rpage,true)
cjurl = safeData("all","cjurl") : cjurl=chkSql(cjurl,true)

if not isNum(rtype) then rtype = 0 else rtype = clng(rtype)
if not isNum(rpage) then rpage = 1 else rpage = clng(rpage)

select case action
	case "cjday"
		call cjday
	case "cjtype"
		call cjtype
	case "cjall"
		call cjall
	case else
		call main
end select

function main
	writeE ""
end function

function cut(str,start,start_num,over,over_num)
	cut=mid(str,Instr(str,start)+start_num,Instr(str,over)+over_num-Instr(str,start)-start_num)
end function

sub cjday
	dim url
	url = cjurl & "?action=cjday&rpage="&rpage
	call insertdata(url,"cjday")
end sub

sub cjtype
	dim url,backurl,flag
	backurl = request.ServerVariables("HTTP_REFERER")
	flag = false
	if isN(rtype) then 
		flag=true
	else
		if isNum(rtype) then 
			if clng(rtype) =0 then flag = true
	    end if
	end if
	if flag then writee "Ƚ,޷ʹòɼ!"
	url = cjurl & "?action=cjtype&rpage="&rpage & "&rtype="&rtype
	call insertdata(url,"cjtype")
end sub

sub cjall
	dim url
	url = cjurl & "?action=cjall&rpage="&rpage
	call insertdata(url,"cjall")
end sub

sub insertdata(byval url,byval cjtype)
	dim objxml,xmlnode,html,i,backurl,str1,strpic
	dim sql,rs,rsarea,rslanguage,pagecount,rsurl,resultdes,AutoID,DU_Sort,CJ_ID,CJ_Source
    dim vodid,vodname,vodenname,vodstate,vodtype,vodstarring,vodpic,vodtime,vodyear,vodarea,vodlanguage,vodfrom,vodurl,voddes
    backurl = request.ServerVariables("HTTP_REFERER")
    
    set objxml = new AppXmlDom
	html = xmlhttpOBj.getpage(url,"gbk")
	objxml.LoadXml(html)
	set xmlnode = objxml.Root
	if isN(xmlnode) then writee ""
	pagecount = xmlnode.getElementsByTagName("pagecount").item(0).text
	if not isNum(pagecount) then pagecount=1 else pagecount=clng(pagecount)
	set xmlnode = objxml.FindNodes("maccms/vods/vod")
	
	if xmlnode.length=0 then
		writeE ""
	end if
	
	for i=0 to xmlnode.length-1
		vodid=xmlnode(i).selectNodes("id")(0).text
		vodname=xmlnode(i).selectNodes("name")(0).text
		vodstate=xmlnode(i).selectNodes("state")(0).text
		vodtype=xmlnode(i).selectNodes("type")(0).text
		vodstarring=xmlnode(i).selectNodes("starring")(0).text
		vodpic=xmlnode(i).selectNodes("pic")(0).text
		vodtime=xmlnode(i).selectNodes("time")(0).text
		vodyear=xmlnode(i).selectNodes("year")(0).text
		vodarea=xmlnode(i).selectNodes("area")(0).text
		vodlanguage=xmlnode(i).selectNodes("language")(0).text
		vodfrom=xmlnode(i).selectNodes("from")(0).text
		vodurl=xmlnode(i).selectNodes("url")(0).text
		voddes=xmlnode(i).selectNodes("des")(0).text
		
		vodstate = replaceStr(vodstate,"[","") : vodstate = replaceStr(vodstate,"]","")
		if not isNum(vodstate) then vodstate = 0
		vodname = replaceStr(vodname,"'","") : 	if len(vodname) > 128 then vodname = left(vodname,126)
		vodstarring = replaceStr(vodstarring,"'","") : 	if len(vodstarring) > 255 then vodstarring = left(vodstarring,250)
		vodenname = hanzi2pinyin(vodname) : if len(vodenname) > 255 then vodenname = left(vodenname,250)
		
		voddes = replaceStr(voddes,"'","")
		if len(vodyear) > 32 then vodyear = left(vodyear,30)
		
		vodurl= replaceStr(vodurl,chr(10),chr(13))
		if right(vodurl, 1) = chr(13) Then vodurl = Mid(vodurl, 1, Len(vodurl) - 1)
		vodurl= replaceStr(vodurl,chr(13),"#") : 	vodurl= replaceStr(vodurl,"##","#")
		
		
		vodtype = getTypeID(vodtype) : 	if not isNum(vodtype) then vodtype = 0
		vodarea = getAreaID(vodarea) : 	if not isNum(vodarea) then vodarea = 0
		vodlanguage = getLanguageID(vodlanguage) : if not isNum(vodlanguage) then vodlanguage = 0
		
		str1=""
		if CJ_ID <> "" then  str1 = " or CJ_ID= '"&CJ_ID&"' "
		Set Rs=server.CreateObject("ADODB.RecordSet")
		set rsurl=server.CreateObject("ADODB.RecordSet")
		
		Sql="Select * from Tbl_Vod Where D_Name ='"&vodname&"' " & str1
		Rs.Open Sql,objdb.dbConn,1,2
		if Rs.Eof Or Rs.Bof Then
			objdb.db "insert into Tbl_Vod(D_Type,D_Name,D_EnName,D_State,D_Content,D_Pic,D_Level,D_Hits,D_AddTime,D_Starring,D_Year,D_Area,D_Language) values("&vodtype&",'"&vodname&"','"&vodenname&"','"&vodstate&"','"&voddes&"','"&vodpic&"',0,0,'"&now()&"','"&vodstarring&"','"&vodyear&"','"&vodarea&"','"&vodlanguage&"')","exe"
			AutoID = objdb.AutoID("Tbl_Vod") -1 
			objdb.db "insert into Tbl_Vod_Url(DU_VodID,DU_From,DU_ServerID,DU_URL,DU_Sort,CJ_Source,CJ_ID) values("&AutoID&",'"&vodfrom&"','0','"&vodurl&"','1','"&CJ_Source&"','"&CJ_ID&"')","exe"
			wTips vodname,"ݿûм¼,ɹ"
		else
			strpic = RS("D_Pic")
			if instr(strpic,"http:") > 0 then RS("D_Pic")= vodpic
			RS("D_State") = vodstate
			Rs("D_Content")= voddes
			Rs("D_Starring")= vodstarring
			Rs("D_Year")= vodyear
			Rs("D_Area")= vodarea
			Rs("D_Language")= vodlanguage
			Rs("D_AddTime")=Now
			RS.Update
			
			DU_Sort= objdb.DB("select max(DU_Sort) from Tbl_vod_url where DU_VodID="&rs("d_id"),"exe")(0)+1
			if isN(DU_Sort) then DU_Sort=1
			sql = "select du_id,du_from,DU_URL from Tbl_Vod_Url where  DU_VodID="&rs("d_id")& " and du_from = '" &vodfrom&"'"
			set rsurl = objdb.db(sql,"rs3")
			if rsurl.eof and rsurl.bof then
				objdb.db "insert into Tbl_Vod_Url(DU_VodID,DU_From,DU_ServerID,DU_URL,DU_Sort,CJ_Source,CJ_ID) values("&rs("d_id")&",'"&vodfrom&"',0,'"&vodurl&"',"&DU_Sort&",'"&CJ_Source&"','"&CJ_ID&"')","exe"
				resultdes = "Ѿв,Ѿӵ²"
			else
				rsurl("DU_URL")=vodurl
				rsurl.update
				resultdes = "Ϣ"
			end if
			wTips vodname,resultdes
		end if
	next
	objdb.c(rsurl)
	objdb.c(rs)
	set xmlnode = nothing
    set objxml = nothing
	
end sub

sub wTips(vname,vdes)
	'writeF "&nbsp;"&vname&"(<font color='#FF0000'>OK</font>)&nbsp;"&vdes&"<br>"
end sub

function gettypeID(byval servertype)
	dim sql,rs,unionids,unionarr,i
	sql = "select * from Tbl_Vod_Type where T_UnionIDS is not null "
	set rs = objdb.DB(sql,"rs1")
	do while not rs.eof
		unionids = rs("T_UnionIDS")
		unionarr = split(unionids,",")
		for i=0 to ubound(unionarr)
			if trim(unionarr(i)) = servertype then
				gettypeID = rs("t_id")
				exit function
			end if
		next
	rs.movenext
	loop
	objdb.c(rs)
	gettypeID = ""
end function

function getAreaID(byval vodarea)
	dim rsarea,A_Sort,result
	if isN(vodarea) then getAreaID=0 : exit function
	set rsarea = objdb.GetRecordDetail("Tbl_Vod_Area","A_Name like '%" & vodarea &"%'")
	if rsarea.eof then
		A_Sort= objdb.DB("select max(A_Sort) from Tbl_Vod_Area","rs1")(0)+1
		if isN(A_Sort) then A_Sort=0
		result = objdb.AddRecord("Tbl_Vod_Area",Array("A_Name:"&vodarea, "A_Sort:"&A_Sort))
		getAreaID= objdb.AutoID("Tbl_Vod_Area")
	else 
		getAreaID = rsarea("A_ID")
	end if
	objdb.c(rsarea)
end function

function getLanguageID(byval vodlanguage)
	dim rslanguage,L_Sort,result
	if isN(vodlanguage) then getLanguageID=0 : exit function
	set rslanguage = objdb.GetRecordDetail("Tbl_Vod_Language","L_Name like '%" & vodlanguage &"%'")
	if rslanguage.eof then
		L_Sort= objdb.DB("select max(L_Sort) from Tbl_Vod_Language","rs1")(0)+1
		if isN(L_Sort) then L_Sort=0
		result = objdb.AddRecord("Tbl_Vod_Language",Array("L_Name:"&vodlanguage, "L_Sort:"&L_Sort))
		getLanguageID= objdb.AutoID("Tbl_Vod_Language")
	else 
		getLanguageID = rslanguage("L_ID")
	end if
	objdb.c(rslanguage)
end function

%>