<!--#include file="base.asp"-->
<!--#include file="common.asp"-->
<%
''' EYCMS 模板
''' ==================================================================
	dim syncnum,syncmsg
	sub adddb()
		err.clear
		on error resume next
		dim folder:folder=eycms.fget("folder",0)
		if not(eycms.checkstr(folder,"file")) then
			eycms.ajaxjson "folder is wrong act.list1",0
			eycms.die
		end if

		dim islocal:islocal=0
		dim folder_root:folder_root=folder
		if instr(folder_root,"/") then folder_root=split(folder_root,"/")(0)
		if eycms.isfolder(webroot&"theme/"&folder_root&"/model") then islocal=1
		if islocal=1 then
			if not(eycms.isfolder("../theme/"&folder)) then
				eycms.ajaxjson "folder is wrong act.list2",0
				eycms.die
			end if
		else
			if not(eycms.isfolder("../encry/theme/"&folder)) then
				eycms.ajaxjson "folder is wrong act.list3",0
				eycms.die
			end if
		end if

		dim filename:filename=folder & "/eycms_" & eycms.fpost("t0",0)
		filename=replace(lcase(filename),"eycms_eycms_","eycms_")
		if right(filename,4)<>".asp" then filename=filename&".asp"
		if not(eycms.checkstr(filename,"filename")) then
			eycms.ajaxjson "filename is wrong1",0
			exit sub
		end if

		if islocal=1 then
			if eycms.isfile("../theme/"&filename) then
				eycms.ajaxjson "文件名已存在",0
				exit sub
			end if
		else
			if eycms.isfile("../encry/theme/"&filename) then
				eycms.ajaxjson "文件名已存在",0
				exit sub
			end if
		end if

		if instr(filename,"./") then
			eycms.ajaxjson "filename is wrong2",0
			exit sub
		end if
		dim isasp:isasp=1
		dim t1:t1=eycms.enhtml(eycms.fpost("t1",0))
		dim t2:t2=eycms.fpost("t2",1)

		if isasp=1 then
			if chkhack(t2) Then
				eycms.ajaxjson "模板内容含有疑似木马的代码" & hackstr,0
				exit sub
			end If
		end If
		'木马检测结束************************************************

		dim arr:arr=split(filename,"/")
		dim t3:t3=replace("../theme/"&filename,arr(ubound(arr)),"")

		dim ffname:ffname=arr(ubound(arr))
		dim ffpath:ffpath=replace(webroot&"encry/theme/"&filename,ffname,"")
		dim sname:sname=replace(filename,arr(0)&"/","")
		dim sfolder:sfolder=replace(sname,ffname,"")

		dim saveflag
		if islocal=0 then 
			saveflag=remoteSaveTheme(sname,site_root,t2)
		else
			if isasp=1 then
				dim is_end:is_end="<%"
				is_end=is_end&"if not in_eycms then response.write(""template load fail""):response.end() end if"
				is_end=is_end&"%"
				is_end=is_end&">"
				if instr(t2,is_end)<=0 then
					t2=is_end&t2
				end if
			end if
			eycms.newfile t3,arr(ubound(arr)),t2,""
		end if

		if left(saveflag,3)<>"ERR" then
			dim fileStr:fileStr=getRemoteEncry_new(t2,arr(0),sfolder)
			if left(fileStr,3)<>"ERR" and eycms.strlen(fileStr)>0 then
				eycms.newfile ffpath,ffname,fileStr,""
				updateBaseXml filename,t1
				eycms.ajaxjson "保存成功",1
			else
				eycms.ajaxjson "保存失败1",0
			end if
		else
			eycms.ajaxjson "保存失败 "&saveflag,0
		end if

		if err then
			eycms.errlog "文件名：ey_theme.asp<br>函数：adddb<br>详情："&err.description,1
			err.clear
		end if
	end sub

	sub editdb()
		err.clear
		on error resume next
		dim filename:filename=eycms.fget("filename",0)
		if not(eycms.checkstr(filename,"filename")) then
			eycms.ajaxjson "filename is wrong1",0
			exit sub
		end if
		if not(eycms.isfile("../theme/"&filename)) then
			eycms.ajaxjson "filename is wrong2",0
			exit sub
		end if
		dim isasp:isasp=0
		if lcase(right(filename,4))=".asp" then isasp=1
		dim t0:t0=eycms.enhtml(eycms.fpost("t0",0))
		dim t1:t1=eycms.fpost("t1",1)

		'木马检测开始************************************************
		if chkhack(t1) Then
			eycms.ajaxjson "模板内容含有疑似木马的代码" & hackstr,0
			exit sub
		end If
		'木马检测结束************************************************

		dim arr:arr=split(filename,"/")
		dim t2:t2=replace("../theme/"&filename,arr(ubound(arr)),"")
		if isasp=1 then
			dim is_end:is_end="<%"
			is_end=is_end&"if not in_eycms then response.write(""template load fail""):response.end() end if"
			is_end=is_end&"%"
			is_end=is_end&">"
			if instr(t0,is_end)<=0 then
				t1=is_end&t1
			end if
		end if
		eycms.newfile t2,arr(ubound(arr)),t1,""
		if isasp=1 then
			dim ffname:ffname=arr(ubound(arr))
			dim ffpath:ffpath=replace("../encry/theme/"&filename,ffname,"")
			dim sname:sname=replace(filename,arr(0)&"/","")
			dim sfolder:sfolder=replace(sname,ffname,"")
			dim fileStr:fileStr=getRemoteEncry_new(t1,arr(0),sfolder)
			if left(fileStr,3)<>"ERR" and eycms.strlen(fileStr)>0 then
				eycms.newfile ffpath,ffname,fileStr,""
				updateBaseXml filename,t0
				eycms.ajaxjson "保存成功",1
			else
				eycms.ajaxjson "保存失败1",0
			end if
		else
			eycms.ajaxjson "保存成功",1
		end if

		if err then
			eycms.errlog "文件名：ey_theme.asp<br>函数：editdb<br>详情："&err.description,1
			err.clear
		end if
	end sub

	sub editdb_remote()
		err.clear
		on error resume next
		dim filename:filename=eycms.fget("filename",0)
		if not(eycms.checkstr(filename,"filename")) then
			eycms.ajaxjson "filename is wrong1",0
			exit sub
		end if
		if not(eycms.isfile("../encry/theme/"&filename)) then
			eycms.ajaxjson "filename is wrong2",0
			exit sub
		end if
		dim isasp:isasp=0
		if lcase(right(filename,4))=".asp" then isasp=1
		dim t0:t0=eycms.enhtml(eycms.fpost("t0",0))
		dim t1:t1=eycms.fpost("t1",1)

		'木马检测开始************************************************
		if chkhack(t1) Then
			eycms.ajaxjson "模板内容含有疑似木马的代码" & hackstr,0
			exit sub
		end If
		'木马检测结束************************************************

		dim arr:arr=split(filename,"/")
		dim ffname:ffname=arr(ubound(arr))
		dim ffpath:ffpath=replace("../encry/theme/"&filename,ffname,"")
		dim sname:sname=replace(filename,arr(0)&"/","")
		dim saveflag
		saveflag=remoteSaveTheme(sname,site_root,t1)

		if left(saveflag,3)<>"ERR" then
			dim fileStr:fileStr=getRemoteEncry_site(sname,site_root)

			if left(fileStr,3)<>"ERR" and eycms.strlen(fileStr)>0 then
				eycms.newfile ffpath,ffname,fileStr,""
				updateBaseXml filename,t0
				eycms.ajaxjson "保存成功",1
			else
				eycms.ajaxjson "保存失败1",0
			end if
		else
			eycms.ajaxjson "保存失败2",0
		end if
		if err then
			eycms.errlog "文件名：ey_theme.asp<br>函数：editdb_remote<br>详情："&err.description,1
			err.clear
		end if
	end sub

	dim picsrc,edittype
	sub check_file(byval t0)
		select case lcase(t0)
		case "htm","tml"
			picsrc="html"
			edittype=true
		case "asp","css",".js"
			picsrc="txt"
			edittype=true
		case "rar","swf","pdf","psd","ppt","xls"
			picsrc=t0
		case "bmp","jpg","gif","png":
			picsrc="bmp"
		case else
			picsrc="txt"
		end select
	end sub
	
	sub load_theme_config(byval t0)
		'读取模板配置
		dim t1,t2,t3,t4,t5,t6
		t1=1:t2=1:t3=1:t4=1:t5=1:t6=1
		if eycms.isfile("../theme/"&t0&"/config.xml") Then
			dim xml,item
			set xml=server.createobject("microsoft.xmldom")
			xml.async=false
			xml.load(server.mappath("../theme/"&t0&"/config.xml"))
			set item=xml.getElementsByTagName("skin")
			t1 = item.item(0).getElementsByTagName("UserOpen").item(0).Text
			t2 = item.item(0).getElementsByTagName("NewsOpen").item(0).Text
			t3 = item.item(0).getElementsByTagName("PhotoOpen").item(0).Text
			t4 = item.item(0).getElementsByTagName("VideoOpen").item(0).Text
			t5 = item.item(0).getElementsByTagName("DownOpen").item(0).Text 
			t6 = item.item(0).getElementsByTagName("ProOpen").item(0).Text 
			set xml=nothing
			set item=Nothing
			eycms.db.dbupdate "ey_admin_menu","title='会员'",array(array("islock",t1,0,0))
			eycms.db.dbupdate "ey_admin_menu","title='订单'",array(array("islock",t1,0,0))
			eycms.db.dbupdate "ey_model","tablename='ey_model_news'",array(array("islock",t2,0,0))
			eycms.db.dbupdate "ey_model","tablename='ey_model_photo'",array(array("islock",t3,0,0))
			eycms.db.dbupdate "ey_model","tablename='ey_model_video'",array(array("islock",t4,0,0))
			eycms.db.dbupdate "ey_model","tablename='ey_model_down'",array(array("islock",t5,0,0))
			eycms.db.dbupdate "ey_model","tablename='ey_model_pro'",array(array("islock",t6,0,0))
		end if
	end Sub

	function lablename(t0)
		if eycms.strlen(t0)=0 then lablename="":exit function
		select case t0
		case 1
			lablename="全局标签"
		case 2
			lablename="列表页标签"
		case 3
			lablename="内容页标签"
		case 4
			lablename="自定义标签"
		end select
	end function

	function filterCode(t0)
		if eycms.strlen(t0)=0 then filterCode="":exit function
		dim t1:t1=t0
		t1=replace(t1,"{","")
		t1=replace(t1,"}","")
		t1=replace(t1,"[","(")
		t1=replace(t1,"]",")")
		filterCode=t1
	end function

	function getthemename(t0)
		if eycms.strlen(t0)=0 then getthemename="":exit function
		select case t0
		case "common"
			getthemename="通用css、js目录"
		case "css"
			getthemename="样式表目录"
		case "js"
			getthemename="js目录"
		case "images","upfile","image"
			getthemename="图片目录"
		case "user"
			getthemename="会员中心"
		case "xml"
			getthemename="xml数据文件"
		case "model"
			getthemename="模型文件目录"
		case "mobile"
			getthemename="手机版"
		case "news"
			getthemename="文章模型"
		case "photo"
			getthemename="图片模型"
		case "pro"
			getthemename="商品模型"
		case "down"
			getthemename="下载模型"
		case "video"
			getthemename="视频模型"
		case "gift"
			getthemename="礼品模型"
		case "page"
			getthemename="单页模型"
		end select
	end function

	function get_tree()
		dim mdata,jj,ji
		if eycms.strlen(eycms.getsys("eycms.plug.lable"))>0 then
			for jj=1 to 4
				get_tree=get_tree&""&eycms.iif(jj=1,"",",")
				get_tree=get_tree&"{id:"&jj&",pId:0,name:"""&lablename(jj)&""",val:""""}"
				mdata=eycms.db.dbload("","id,shuomin,title","ey_plug_lable","classid="&jj&"","")
				if ubound(mdata)>=0 then
					for ji=0 to ubound(mdata,2)
						get_tree=get_tree&",{"
						get_tree=get_tree&"id:"&mdata(0,ji)+4&""
						get_tree=get_tree&",pId:"&jj&""
						get_tree=get_tree&",name:"""&mdata(1,ji)&""""
						get_tree=get_tree&",val:"""&filterCode(mdata(2,ji))&""""
						get_tree=get_tree&"}"
					next
				end if
			next
		end if
	end function

	sub tosync(folder)
        dim fso,fsofolder,fsocount
		dim fsocontent,fsoitem,fname,fcontent
		dim fileStr

		dim islocal:islocal=0
		dim folder_root:folder_root=folder
		if instr(folder_root,"/") then folder_root=split(folder_root,"/")(0)
		if eycms.isfolder(webroot&"theme/"&folder_root&"/model") then islocal=1

        set fso=createobject("scripting.filesystemobject")
        if islocal=0 then
        	set fsofolder=fso.getfolder(server.mappath(webroot&"encry/theme/"&folder))
        else
        	set fsofolder=fso.getfolder(server.mappath(webroot&"theme/"&folder))
        end if
        for each fsocount in fsofolder.subfolders      '遍历目录下的子目录
        	if instr("css|images|image|img|js|font|upfile",lcase(fsocount.name))<=0 then
        		tosync folder&"/"&fsocount.name
        	end if
		next

		set fsocontent=fsofolder.files      '遍历目录下的文件
		for each fsoitem in fsocontent
			if lcase(fsoitem.name)<>"base.xml" and lcase(fsoitem.name)<>"config.xml" and lcase(fsoitem.name)<>"default.gif" then
				fname=folder&"/"&fsoitem.name
				if islocal=0 then
					fileStr=getRemoteEncry_site(replace(fname,folder_root&"/",""),site_root)
				else
					fcontent=eycms.loadfile("../theme/"&fname)
					fileStr=getRemoteEncry_new(fcontent,folder_root,replace(folder&"/",folder_root&"/",""))
				end if
				if left(fileStr,3)<>"ERR" then
					eycms.newfile webroot&"encry/theme/"&folder&"/",fsoitem.name,fileStr,"utf-8"
					syncnum=syncnum+1
				else 
					syncmsg=fileStr
				end if
			end if
		next
		set fsocontent=nothing

        set fso=nothing
        set fsofolder=nothing
	End sub

	function themeisUse(t0)
		themeisUse=false
		if eycms.isfile("../site/config.xml") Then
			dim xml,i,rootNode
			set xml=server.createobject("microsoft.xmldom")
			xml.async=false
			xml.load(server.mappath("../site/config.xml"))
			set rootNode = xml.documentelement.selectnodes("site[@theme='"&t0&"']")
			If rootNode.length>0 then
				themeisUse=true
			end if
			set rootNode=nothing
			set xml=nothing
		end If
	end function

	islogin
	checkpagelever 30
	dim islocal:islocal=0
	dim folder_root
	dim act:act=lcase(eycms.fget("act",0))
	select case act
		case "modpic"
			dim imgurl:imgurl=eycms.enhtml(eycms.fget("url",0))
			If InStr(imgurl,"?")>0 Then imgurl=Split(imgurl,"?")(0)
			dim imgid:imgid=eycms.enhtml(eycms.fget("id",0))
			load theme_theme_modpic
		case "aply"
			dim get_basexml
			dim rootdir:rootdir=eycms.fpost("t0",0)
			if not(eycms.isfolder("../theme/"&rootdir&"/")) then
				eycms.echo "0&#24212;&#29992;&#22833;&#36133;&#65292;&#27169;&#26495;&#19981;&#23384;&#22312;"
			Else
				getbasexml(rootdir)
				dim html:html="<?xml version=""1.0"" encoding=""utf-8""?>"
				html=html&"<config>"
				html=html&get_basexml
				html=html&"</config>"
				eycms.newfile "../theme/"&rootdir&"/","base.xml",html,""
				load_theme_config rootdir
				if eycms.isfile("../site/config.xml") Then
					dim xml,item,ii
					set xml=server.createobject("microsoft.xmldom")
					xml.async=false
					xml.load(server.mappath("../site/config.xml"))
					set item=xml.getElementsByTagName("site")
					for ii=0 to (item.length-1)
						if item.item(ii).getAttribute("tag")=site_root then 
							item.item(ii).setattribute "theme", rootdir
							eycms.clearcache
						end if
					Next
					xml.save(server.mappath("../site/config.xml")) 
					set xml=nothing
					set item=nothing
				End If
				if not(eycms.isfolder("../encry/theme/"&rootdir)) then
					tosync rootdir
				end if
				eycms.echo "1"
			end if
		case "list"
			dim folder:folder=eycms.fget("folder",0)
			if not(eycms.checkstr(folder,"file")) then
				eycms.echo "folder is wrong act.list1"
				eycms.die
			end if
			
			folder_root=folder
			if instr(folder_root,"/") then folder_root=split(folder_root,"/")(0)
			if eycms.isfolder(webroot&"theme/"&folder_root&"/model") then islocal=1
			if islocal=1 then
				if not(eycms.isfolder("../theme/"&folder)) then
					eycms.echo "folder is wrong act.list2"
					eycms.die
				end if
			end if

			dim arr:arr=split(folder,"/")
			dim position,i,str
			for i=0 to ubound(arr)
				if i=0 then
					str=arr(i)
				else
					str=str&"/"&arr(i)
				end if
				position=position&" > <a href=""?act=list&folder="&str&""">"&arr(i)&"</a>"
			next
			load theme_theme_folder
		case "add"
			folder=eycms.fget("folder",0)
			if not(eycms.checkstr(folder,"file")) then
				eycms.echo "folder is wrong act.list1"
				eycms.die
			end if
			
			folder_root=folder
			if instr(folder_root,"/") then folder_root=split(folder_root,"/")(0)
			if eycms.isfolder(webroot&"theme/"&folder_root&"/model") then islocal=1
			if islocal=1 then
				if not(eycms.isfolder("../theme/"&folder)) then
					eycms.echo "folder is wrong act.list2"
					eycms.die
				end if
			else
				if not(eycms.isfolder("../encry/theme/"&folder)) then
					eycms.echo "folder is wrong act.list2"
					eycms.die
				end if
			end if
			arr=split(folder,"/")
			for i=0 to ubound(arr)
				if i=0 then
					str=arr(i)
				else
					str=str&"/"&arr(i)
				end if
				position=position&" > <a href=""?act=list&folder="&str&""">"&arr(i)&"</a>"
			next
			load theme_theme_add
		case "adddb":adddb
		case "edit"
			dim filename:filename=eycms.fget("filename",0)
			if not(eycms.checkstr(filename,"filename")) then
				eycms.echo "filename is wrong act.edit1"
				eycms.die
			end if
			if not(eycms.isfile("../theme/"&filename)) then
				eycms.echo "filename is wrong act.edit2"
				eycms.die
			end if
			arr=split(filename,"/")
			folder=arr(0)
			for i=0 to ubound(arr)-1
				if i=0 then
					str=arr(i)
				else
					str=str&"/"&arr(i)
				end if
				position=position&" > <a href=""?act=list&folder="&str&""">"&arr(i)&"</a>"
			next
			dim iscss:iscss=0
			dim troot:troot=replace(filename,"/"&arr(ubound(arr)),"")
			if lcase(right(filename,4))=".css" then iscss=1
			dim temp_content:temp_content=eycms.loadfile("../theme/"&filename)
			dim r_mid:r_mid="<%"
			r_mid=r_mid&"if not in_eycms then response.write(""template load fail""):response.end() end if"
			r_mid=r_mid&"%"
			r_mid=r_mid&">"
			temp_content=replace(temp_content,r_mid,"")
			dim block_txt:block_txt=getblockname(replace(filename,theme_root&"/",""))

			load theme_theme_edit
		case "editdb":editdb

		case "editremote"
			filename=eycms.fget("filename",0)
			if not(eycms.checkstr(filename,"filename")) then
				eycms.echo "filename is wrong act.edit1"
				eycms.die
			end if
			if not(eycms.isfile("../encry/theme/"&filename)) then
				eycms.echo "filename is wrong act.edit2"
				eycms.die
			end if
			arr=split(filename,"/")
			folder=arr(0)
			for i=0 to ubound(arr)-1
				if i=0 then
					str=arr(i)
				else
					str=str&"/"&arr(i)
				end if
				position=position&" > <a href=""?act=list&folder="&str&""">"&arr(i)&"</a>"
			next
			iscss=0
			troot=replace(filename,"/"&arr(ubound(arr)),"")
			if lcase(right(filename,4))=".css" then iscss=1
			dim fname:fname=replace(filename,theme_root&"/","")
			temp_content=getRemoteTheme(fname,site_root)
			r_mid="<%"
			r_mid=r_mid&"if not in_eycms then response.write(""template load fail""):response.end() end if"
			r_mid=r_mid&"%"
			r_mid=r_mid&">"
			temp_content=replace(temp_content,r_mid,"")
			block_txt=getblockname(replace(filename,theme_root&"/",""))

			load theme_theme_edit_remote
		case "editdb_remote":editdb_remote
		case "sync"
			dim mfolder:mfolder=eycms.fget("folder",0)
			if eycms.strlen(mfolder)>0 then
				if not(eycms.isfolder("../encry/theme/"&mfolder)) then
					eycms.newfolder "../encry/theme/"&mfolder
				end if
				syncnum=0
				tosync mfolder
				if syncnum>0 then
					eycms.ajaxjson "同步成功",1
				else
					eycms.ajaxjson "同步失败 "&syncmsg,0
				end if
			end if

		case else
			load theme_theme
	end select
	
	eycms.db.dbclose
%>