<!--#include file="../inc/AspCms_SettingClass.asp" -->
<%
'=====================================================================
'安全校验码
dim vercode

vercode=""   '此处取值请自行修改，必须与发布规则的参数取值中vercode参数一致

if vercode<>trim(request("vercode")) then
	response.Write("[err]vercode错误[/err]")
	response.End()
end if

'=====================================================================

sub echomsg(str)
	response.Write("[err]"&str&"[/err]")
	response.End()
End sub


sub login
	dim UserName,Password,sql,code,Rs,atype
	UserName = filterPara(getForm("username","post"))
	Password = md5(getForm("password","post"),16)

'	User UserGroup
'	LoginName
'Password
'GroupID
'IsAdmin
	sql = "select count(*) from {prefix}User where LoginName = '"& UserName &"' and Password='"&Password&"'"
	Dim rsObj : Set rsObj=Conn.Exec(sql,"r1")
	if rsObj(0)=1 then
	
		
		Set rsObj=Conn.Exec("select IsAdmin, GroupStatus,GroupName, UserStatus, UserID, GroupMenu,GroupSort, LanguageID,adminrand from {prefix}User as a, {prefix}UserGroup as b where LoginName='"&UserName&"' and a.GroupID=b.GroupID","r1")	
		if not rsObj.Eof Then
			if rsObj("IsAdmin")<>1 then echomsg"对不起，你不是管理员！"
			if rsObj("GroupStatus")<>1 then echomsg"对不起，您所在用户组已被禁用！"
			if rsObj("UserStatus")<>1 then echomsg"对不起，您的账号已被禁用！"
			Session("adminName")=UserName
			Session("GroupName")=rsObj("GroupName")
			'wCookie"adminpwd",getForm("Password","post")
			Session("adminpwd") = getForm("Password","post")
			Session("adminId")=rsObj("UserID")
			Session("groupMenu")=repnull(rsObj("GroupMenu"))
			Session("GroupSort")=repnull(rsObj("GroupSort"))
			'查找
			dim LanguageID
			LanguageID=rsObj("LanguageID")

			if isnul(LanguageID) then 				
				set rs=conn.exec("select LanguageID,LanguagePath,Alias from {prefix}Language where IsDefault=1","exe")
				if rs.eof then					
					echomsg "没有设置默认语言"
				else
					LanguageID=rs(0)
				end if
			else 	
				set rs=conn.exec("select LanguageID,LanguagePath,Alias from {prefix}Language where LanguageID="&LanguageID,"exe")				
				LanguageID=rs(0)	
			end if 
			randomize
			dim randnum
			randnum=clng(rnd*99999999)
			Conn.Exec"update {prefix}User set adminrand='"& randnum &"' where UserID="&rsObj("UserID"),"exe"	
			Session("adminrand")=rsObj("adminrand")	
			Session("languageID")=LanguageID			
			Session("LanguagePath")=rs("LanguagePath")
			Session("LanguageAlias")=rs("Alias")
			

			rs.close : set rs=nothing
		end if
	else
		echomsg "用户名或密码错误!"
	end if
	rsObj.Close() : set rsObj=Nothing	
End sub

login()

'die debugmode
dim zzmakelist,zzmakeindex

dim action : action=getForm("action","get")
dim ContentID, LanguageID, SortID, GroupID, Exclusive, Title, Title2, TitleColor, IsOutLink, OutLink, Author, ContentSource, ContentTag, Content, ContentStatus, IsTop, IsRecommend, IsImageNews, IsHeadline, IsFeatured, ContentOrder, IsGenerated, Visits, AddTime, ImagePath, IndexImage, DownURL, PageTitle, PageKeywords, PageDesc, PageFileName, spec, EditTime,DownGroupID,IsNoComment,Star,Timeing,TimeStatus,VideoGroupID,CHvalue,SpecCategory
'SpecCategory用于判断是哪个类型的自定义参数
dim sortType, keyword, page, psize, order, ordsc, sortTypeName
sortType=getForm("sortType","post")
if isnul(sortType) then sortType="2	"
sortid=getForm("sortid","post")	
if isnul(sortid) then sortid="1"
keyword=getForm("keyword","post")
DownGroupID=getForm("DownGroupID","post")
VideoGroupID=getForm("VideoGroupID","post")
	
if not isnum(sortType) then  response.Write("[err]内容类型sortType错误[/err]"):response.End()
	
select case sortType
	case "2"
		sortTypeName ="文章"
		SpecCategory = "C"
	case "3"
		sortTypeName ="产品"
		SpecCategory = "P"
	case "4"
		sortTypeName ="下载"
		SpecCategory = "DL"
	case "5"
		sortTypeName ="招聘"
		SpecCategory = "HR"
	case "6"
		sortTypeName ="相册"
		SpecCategory = "FO"

	case "8"
		sortTypeName = "视频"
		SpecCategory = "VI"
		
end select 
'单篇1,文章2,产品3,下载4,招聘5,相册6,链接7，视频8

SortID=getForm("SortID", "post")
if not isnum(SortID) or SortID="0" then response.Write("[err]请选择分类（SortID）[/err]"):response.End()
Title=getForm("Title", "post")
if isnul(Title) then response.Write("[err]请填写标题[/err]"):response.End()

Sub addContent
Dim sql
	LanguageID=cint(session("languageID"))
	GroupID=getForm("GroupID", "post")
	Exclusive=getForm("Exclusive", "post")
	
	TitleColor=getForm("TitleColor", "post")
	Title2=getForm("Title2", "post")
	Author=getForm("Author", "post")
	ContentSource=getForm("ContentSource", "post")
	Content=getForm("Content", "post")
	
	PageTitle=getForm("PageTitle", "post")
	PageKeywords=getForm("PageKeywords", "post")
	PageDesc=getForm("PageDesc", "post")	
	
	OutLink=getForm("OutLink", "post")
	IsOutLink=getCheck(getForm("IsOutLink", "post"))
	ContentStatus=getForm("ContentStatus", "post")
	if isnul(ContentStatus) then ContentStatus=1
	
	
	IsTop=getCheck(getForm("IsTop", "post"))
	IsRecommend=getCheck(getForm("IsRecommend", "post"))
	IsHeadline=getCheck(getForm("IsHeadline", "post"))
	IsFeatured=getCheck(getForm("IsFeatured", "post"))
	IsGenerated=getCheck(getForm("IsGenerated", "post"))
	IsNoComment=getCheck(getForm("IsNoComment", "post"))
	IsNoComment=getCheck(getForm("IsNoComment", "post"))
	
	TimeStatus=getCheck(getForm("TimeStatus", "post"))
	Timeing=getForm("Timeing", "post")
	
	Star=getForm("Star", "post")
	ContentOrder=getForm("ContentOrder", "post")
	Visits=getForm("Visits", "post")
	AddTime=getForm("AddTime", "post")
	EditTime=now()
	ImagePath=getForm("ImagePath", "post")
	ImagePath=replace(ImagePath,vbCrLf,"|")
	IndexImage=getForm("IndexImage", "post")
	
	IsImageNews=getForm("IsImageNews", "post")
	if isnul(IsImageNews) then
		if IndexImage<>"" then
			IsImageNews=1
		else
			IsImageNews=0
		end if
	end if
	
	
	DownURL=getForm("DownURL", "post")
	PageFileName=getForm("PageFileName", "post")
	spec=split(getForm("spec","post"),",")
	DownGroupID=getForm("DownGroupID","post")
	if DownGroupID = "" then DownGroupID=2
	VideoGroupID=getForm("VideoGroupID","post")
	if VideoGroupID = "" then VideoGroupID=2
	
	dim specStr	: specStr=""
	dim specValue  : specValue=""
	dim valueStr	: valueStr=""
	
		dim i	:i=0
		dim rsObj
		sql = "select SpecName,SpecCategory+'_'+SpecField,SpecControlType,SpecOptions from {prefix}SpecSet where SpecCategory = '"&SpecCategory&"' order by SpecOrder,SpecID"
		sql = "select SpecCategory+'_'+SpecField,SpecControlType from {prefix}SpecSet order by SpecOrder,SpecID"
		Set rsObj=Conn.Exec(sql,"r1")
		'0 文本,1 数字,2 编辑器,3 附件,4 日期,5 颜色,6 单选,7 多选
		Do While not rsObj.Eof 		
			specStr = specStr&","&rsObj(0)
			if rsObj(1) = 2 then
			valueStr = valueStr & ",'" &encode(getForm(rsObj(0),"post")) & "'"
			else
			valueStr = valueStr & ",'" &getForm(rsObj(0),"post") & "'"
			end if
			
			'if ubound(spec,1) < i then i = ubound(spec,1)
			'echo ubound(spec,1) &"=" &i & "[" &spec(5) & "]"
			'valueStr = valueStr&",'"&trim(spec(i))&"'"			
			i=i+1
			rsObj.MoveNext
		Loop
		rsObj.Close	:	set rsObj=Nothing

	if not isnum(LanguageID) then LanguageID=1


	if not isnum(ContentOrder) then ContentOrder=0
	if not isnum(Visits) then Visits=0
	if not isdate(AddTime) then AddTime=now()
	if not isdate(Timeing) then Timeing=now()
	if not isnum(star) then star=1
	if not isnum(GroupID) then GroupID=2
	
	
	ContentTag=addTags(replace(replace(getForm("ContentTag", "post")," ",","),"，",","))	
	
	'die specStr
	sql = "insert into {prefix}Content(VideoGroupID,DownGroupID, LanguageID, SortID, GroupID, Exclusive, TitleColor, Title, Title2, Author, ContentSource, [Content], ContentTag, PageTitle, PageKeywords, PageDesc, OutLink, IsOutLink, ContentStatus, IsTop, IsRecommend, IsImageNews, IsHeadline, IsFeatured, IsGenerated, ContentOrder, Visits, AddTime, EditTime, ImagePath, IndexImage, DownURL"&specStr&", PageFileName, IsNoComment, Star,TimeStatus,Timeing) values("&VideoGroupID&","&DownGroupID&","&LanguageID&", "&SortID&", "&GroupID&", '"&Exclusive&"', '"&TitleColor&"', '"&Title&"', '"&Title2&"', '"&Author&"', '"&ContentSource&"', '"&Content&"', '"&ContentTag&"', '"&PageTitle&"', '"&PageKeywords&"', '"&PageDesc&"', '"&OutLink&"', "&IsOutLink&", "&ContentStatus&", "&IsTop&", "&IsRecommend&", "&IsImageNews&", "&IsFeatured&", "&IsFeatured&", "&IsGenerated&", "&ContentOrder&", "&Visits&", '"&AddTime&"', '"&EditTime&"', '"&ImagePath&"',  '"&IndexImage&"',  '"&DownURL&"' "&valueStr&", '"&PageFileName&"', "&IsNoComment&", "&Star&", "&TimeStatus&", '"&Timeing&"')"
	
	'die sql
	
	conn.exec sql,"exe"
	response.Write("[ok]")
End Sub
		


Function addTags(tags)
	dim sql,rs,tag,tagIDs
	tags=split(tags,",")
	for each tag in tags
		if not isnul(tag) then 	
			sql="select TagID from {prefix}Tag where TagName='"&tag&"'"
			set rs=conn.exec(sql,"r1")
			if not rs.eof then 
				tagIDs=tagIDs&"{"&rs(0)&"}"
			else
				sql="insert into {prefix}Tag(TagName, TagCount, SortType, SortID, TagVisits, LanguageID, AddTime) values('"&tag&"',0,"&SortType&","&SortID&",0,"&LanguageID&",'"&now()&"')"
				conn.exec sql,"exe"
				tagIDs=tagIDs&"{"&conn.exec("select @@identity","r1")(0)&"}"
			end if
		end if		
	next
	addTags=tagIDs	
End Function

'生成内容页
Sub makenewContent
	dim rs,sql
	
	sql="select TOP 1 * from {prefix}Content where ContentStatus=1 and GroupID<3 and LanguageID="&cint(session("languageID"))&" and SortID="&SortID&" order by ContentID desc"
	set rs=conn.Exec(sql,"r1")	
	Do While not rs.Eof
		'echo "生成"""&rs("ContentID")&"""成功<br>"
		makeContent rs("ContentID"), 1, 1
		rs.movenext
	Loop
	rs.close : set rs =nothing
	
	
End Sub


'按条件生成列表页
Sub makeListBySort()
	dim rs,sql
	sql="select SortID,SortType,SortName from {prefix}Sort where SortType<>1 and SortType<>7 and GroupID<3 and LanguageID="&cint(session("languageID"))&" and SortID = "&SortID&" order by SortID"
	set rs=conn.Exec(sql,"r1")
	Do While not rs.Eof
		makeList( rs("SortID"))
		'echo "生成"""&rs("SortName")&"""成功<br>"
		rs.movenext
	Loop
	rs.close : set rs =nothing
End Sub


'生成首页
Sub makeIndex
	dim templateobj,templatePath : set templateobj = new TemplateClass	
	templatePath=sitePath&"/"&"templates/"&setting.defaultTemplate&"/"&setting.htmlFilePath&"/index.html"	
	'die templatePath
	if not CheckTemplateFile(templatePath) then echomsg "首页模板未找到，无法生成首页"
	with templateObj 
		.content=loadFile(templatePath) 
		.parseHtml()		
		.parseCommon		
		createTextFile  .content, sitePath&setting.LanguagePath&"index"&FileExt,""
	end with	
	set templateobj =nothing
	
End Sub


addContent

if runmode=1 then
	makenewContent
	
	zzmakelist=getCheck(getForm("zzmakelist", "post"))
	zzmakeindex=getCheck(getForm("zzmakeindex", "post"))
	if zzmakelist=1 then
		makeListBySort
	end if
	if zzmakeindex=1 then
		makeIndex
	end if
end if

%>