<%
''' EYCMS 模板解析
''' ==================================================================

dim html_page_total:html_page_total=1
class eycms_template

	private isgzip_,iscache_,isencry_,createncry_,encryexists,templatepath_,templatecode_,cachepath_,htmlpath_,templatename_,cachename_,htmlname_,cacheext_,templateext_,htmlext_,cname_
	private templatedata,html,reg,labeldata,matches,matchess,match,matchs,fso,i,j,k,pi
	private t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10
	private u0,u1,u2
	private p,votearray,resultarray,percentarray

	public property let isgzip(t0)
		isgzip_=t0
	end property
	
	public property let iscache(t0)
		iscache_=t0
	end property
	
	public property let isencry(t0)
		isencry_=t0
	end Property
	
	public property let createncry(t0)
		createncry_=t0
	end Property

	public property let cname(t0)
		if t0<>"" then t0="_"&t0
		cname_=t0
	end property

	public property let templatepath(t0)
		templatepath_=t0
	end property
	
	public property let templatecode(t0)
		templatecode_=t0
	end property

	public property let cachepath(t0)
		cachepath_=t0
	end property
	
	public property let htmlpath(t0)
		htmlpath_=t0
	end property

	public property let templatename(t0)
		templatename_=t0
	end property
	
	public property let cachename(t0)
		cachename_=t0
	end property
	
	public property let htmlname(t0)
		htmlname_=t0
	end property
	
	public property let templateext(t0)
		templateext_=t0
	end property
	
	public property let cacheext(t0)
		cacheext_=t0
	end property
	
	public property let htmlext(t0)
		htmlext_=t0
	end property
	
	private sub class_initialize()
		isgzip_=false
		iscache_=false
		isencry_=False
		createncry_=false
		encryexists=false
		cname_=""
		templateext="html"
		cacheext="asp"
		htmlext="html"
		set reg=new regexp
		reg.ignorecase=true
		reg.global=true
		set labeldata=server.createobject("scripting.dictionary")
		set fso=server.createobject("scripting.filesystemobject")
	end sub
	
	private sub class_terminate()
		set fso=nothing
		set labeldata=nothing
		set reg=nothing
	end sub
	
	public sub assign(byval t0,byval t1)
		if len(t0)<=0 then exit sub
		if labeldata.exists(t0) then
			labeldata.item(t0)=t1
		else
			labeldata.add t0,t1
		end if
	end sub

	public sub load()
		if templatepath_="" or templatename_="" then exit Sub
		if isencry_ Then
			dim encryfilename:encryfilename=Replace(templatepath_,"theme/","encry/theme/")&templatename_&"."&templateext_
			encryfilename=Replace(encryfilename,"module/encry/theme/","module/theme/")
			if fso.fileexists(server.mappath(encryfilename)) then
				templatedata=getfile(encryfilename)
				encryexists=true
			else
				get_templatedata
			end if
		end if
	end sub
	
	public sub loadcode()
		if templatecode_="" then exit sub
		get_tempdate()
	end sub
	
	public sub display()
		get_html
		eycms.echo html
	end sub
	
	public function gethtml()
		get_html
		gethtml = html
	end function
	
	public sub createhtml()
		if htmlpath_="" or htmlname_="" then exit sub
		if html="" then get_html
		if not fso.folderexists(server.mappath(htmlpath_)) then
			eycms.newfolder htmlpath_
		end if
		createfile htmlpath_,htmlname_&"."&htmlext_,html
		if html_page_total=0 then html_page_total=1
		eycms.echo "1"&html_page_total
	end sub
	
	public sub get_templatedata()
		dim templatefilename:templatefilename=templatepath_&templatename_&"."&templateext_
		if fso.fileexists(server.mappath(templatefilename)) then
			templatedata=getfile(templatefilename)
		else
			eycms.echo templatefilename&":&#27169;&#26495;&#25991;&#20214;&#19981;&#23384;&#22312;":eycms.die
		end if
	end sub
	
	public sub get_tempdate()
		templatedata=templatecode_
	end sub
	
	public function regexists(byval t0,byval t1)
		reg.pattern=t0
		regexists=reg.test(t1)
	end function
	
	private function getfile(t0)
		if t0="" then
			getfile=""
			exit function
		end if
		getfile=eycms.loadfile(t0)
	end function
	
	private sub createfile(t0,t1,t2)
		if t0="" then exit sub
		eycms.newfile t0,t1,t2,""
	end sub

	private function aspexecute(byval t0)
		on error resume next
		execute(t0)
		if err then
			eycms.echo err.description:err.clear:eycms.die
		end if
	end function

	private function analysis_include(byval t0)
		reg.pattern="#include\(['""](.+?)['""]\)#"
		dim includename,includefile,myfname,mytheme_root
		mytheme_root=replace(theme_root,"/mobile","")
		set matches=reg.execute(t0)
		if matches.count>0 then
			for each match In matches
				if instr(match.submatches(0),"eycms_") then
					includename="eycms_" & split(match.submatches(0),"eycms_")(1)
				else
					includename=split(match.submatches(0),"/block/")(1)
				end if
				myfname=match.submatches(0)
				myfname=replace(myfname,site_root&"/",mytheme_root&"/")
				myfname=webroot & "encry/" & split(myfname,"encry/")(1)
				includefile=getfile(myfname)
				get_include_html includefile
				assign match.value, html
				analysis_include includefile
			next
		else
			exit function
		end if
	end function

	private sub analysis_static
		dim labeltag:labeltag=labeldata.keys
		dim labelval:labelval=labeldata.items
		if labeldata.count>0 then
			for i=0 to labeldata.count-1
				html=replace(html,labeltag(i),labelval(i))
			next
		end if
	end sub

	private function gzip(byval t0)
		dim reg
		set reg=new regexp
			reg.ignorecase=true
			reg.global=true
			reg.pattern=" +":t0=reg.replace(t0,chr(32))
			'reg.pattern="\t+|\r+|\n+":t0=reg.replace(t0,"")
			'reg.pattern="\r+":t0=reg.replace(t0,"")
			'reg.pattern="\n+":t0=reg.replace(t0,"")
			reg.pattern="\t+|\n+":t0=reg.replace(t0,"")
		set reg=nothing
		gzip=t0
	end function
	
	public function getcode()
		on error resume Next
		if templatedata="" then exit function
		dim this_url:this_url=eycms.getthisurl
		dim asp,templatedata_my
		html=""
		If isencry_ and encryexists Then 
			asp=eycms.eycmsdecode(Replace(Split(templatedata,"<%")(1),"%"&">",""))
			asp=replace(asp,"eycms.page.page=1","eycms.page.page=page")
			asp=replace(asp,"eycms.page.pagesize=20","eycms.page.pagesize=pagenum")
			templatedata_my=Split(templatedata,"<%")(0)
		End If
		getcode = templatedata & "<%" & eycms.eycmsencode(asp) & "%"&">"
	end function

	private sub get_html()
		on error resume next
		if templatedata="" then exit Sub
		dim language:language=eycms.loadcookie("language")
		dim this_url:this_url=eycms.getthisurl
		dim asp,templatedata_my
		If isencry_ and encryexists Then 
			asp=eycms.eycmsdecode(Replace(Split(templatedata,"<%")(1),"%"&">",""))
			asp=replace(asp,"eycms.page.page=1","eycms.page.page=page")
			asp=replace(asp,"eycms.page.pagesize=20","eycms.page.pagesize=pagenum")
			templatedata_my=Split(templatedata,"<%")(0)
		End If
		analysis_include templatedata_my
		html=""
		templatedata=templatedata_my
		aspexecute asp

		analysis_static()
		if isgzip_ then
			html=gzip(html)
		end If
		if (language="big5" or cachedate="61") And InStr(this_url,"admin")<=0 Then
			dim etrans
			set etrans=new eycms_lang
			html=etrans.tobig5(html)
			set etrans=nothing
		end if
		if err then
			eycms.errlog "文件：eycms.temp.asp<br>函数：get_html<br>模板文件： "&templatename_&"."&templateext_&"<br>详情："&err.description,0
			err.clear
		end if
	end sub

	private function get_include_html(byval t0)
		on error resume next
		if instr(t0,"<%") then
			dim language:language=eycms.loadcookie("language")
			dim this_url:this_url=eycms.getthisurl
			dim asp
			html=""
			If isencry_ and encryexists Then 
				asp=eycms.eycmsdecode(Replace(Split(t0,"<%")(1),"%"&">",""))
				asp=replace(asp,"eycms.page.page=1","eycms.page.page=page")
				asp=replace(asp,"eycms.page.pagesize=20","eycms.page.pagesize=pagenum")
				templatedata=Split(t0,"<%")(0)
			End If
			html=""
			aspexecute asp

			if isgzip_ then
				html=gzip(html)
			end If
			if (language="big5" or cachedate="61") And InStr(this_url,"admin")<=0 Then
				dim etrans
				set etrans=new eycms_lang
				html=etrans.tobig5(html)
				set etrans=nothing
			end if
		else
			html=t0
		end if
		if err then
			eycms.errlog "文件：eycms.temp.asp<br>函数：get_include_html<br>模板文件： "&templatename_&"."&templateext_&"<br>详情："&err.description,0
			err.clear
		end if
	end function
end class
%>
