<!--#include file="conn.asp"-->
<!--#include file="Check_Login.asp"-->
<!--#include file="../Inc/Config.asp"-->
<!--#include file="../Inc/Functionwu.asp"-->
<!--#include file="../Inc/Ubbcode.asp"-->
<!--#include file="Inc/Function.asp"-->
<!-- #include file="Inc/Head.asp" -->
<%
ID=Request.Form("ID")
Title=Trim(request.form("Title"))
BigClassName=trim(request.form("BigClassName"))
SmallClassName=trim(request.form("SmallClassName"))
User=Trim(request.form("User"))
FirstImageName=trim(request.form("FirstImageName"))
Ok=Trim(request.form("Ok"))
AddDate=trim(request.form("AddDate"))
Hits=trim(request.form("Hits"))
mContent=trim(request.form("Content"))
Action=trim(request("Action"))
SavePic=request.Form("SavePic")
ChangesLink=request.Form("ChangesLink")
If CInt(SavePic) = 1 Then
	Dim SaveFilePath
	  SaveFilePath = "upload/down/" & Year(Now()) & Right("0" & Month(Now()), 2) & "/"
	  Call CreateFolder(SaveFilePath)
	  mContent =ReplaceUrl(mContent, SaveFilePath)
End If
If Request.Form("dellink") =1  Then mContent=RemoveHref(mContent)
Function RemoveHref(HTMLstr)
	Dim ra
	Set ra = New RegExp
	ra.IgnoreCase = True
	ra.Global = True
	ra.Pattern = "<a[^>]+>(.+?)<\/a>"
	RemoveHref= ra.replace(HTMLstr,"$1")
End Function
if BigClassName="" then
	founderr=true
	errmsg=errmsg+"<li>未指定新闻所属大类</li>"
end if
if Title="" then
	founderr=true
	errmsg="<li>新闻标题不能为空</li>"
end if


if founderr=false then
	Title=dvhtmlencode(Title)	
	if AddDate<>"" and IsDate(AddDate)=true then
		AddDate=CDate(AddDate)
	else
		AddDate=now()
	end if
	
	if Hits<>"" then
		Hits=CLng(Hits)
	else
		Hits=0
	end if
	
	set rs=server.createobject("adodb.recordset")
	select case Action
	  case "Add"	
		sql="select * from News where (id is null)" 
		rs.open sql,conn,1,3
		rs.addnew
		call SaveData()		
		rs.update	
		idtp=rs("id")
		rs.close
		set rs=nothing
		set rstp=server.CreateObject("adodb.recordset")
		sqltp="select * from mood where (id if null)"
		rstp.open sqltp,conn,1,3
		rstp.addnew
		rs("classid")="news"
		rstp("moodid")=idtp
		rstp.update
		rstp.close
		set rstp=nothing
		response.redirect "News_Manage.asp"
	 case "Modify"	
  		if ID<>"" then
			sql="select * from News where id="&ID
			rs.open sql,conn,1,3
			if not (rs.bof and rs.eof) then
			    
				call SaveData()				
				rs.update
				rs.close
				set rs=nothing
	            response.redirect "News_Manage.asp"
 			else
				founderr=true
				errmsg=errmsg+"<li>找不到此新闻，可能已经被删除。</li>"
				call WriteErrMsg()
			end if
		else
			founderr=true
			errmsg=errmsg+"<li>不能确定ID的值</li>"
			call WriteErrMsg()
		end if
	 Case else
		founderr=true
		errmsg=errmsg+"<li>没有选定参数</li>"
		call WriteErrMsg()
	end select
	call CloseConn()
else
	WriteErrMsg
end if
%>

<%
sub SaveData()  
  rs("Title")=Title
  rs("Content")=mContent
  rs("User")=User
  rs("BigClassName")=BigClassName
  rs("SmallClassName")=SmallClassName
  rs("AddDate")=AddDate
  rs("Hits")=Hits
  if Ok="Yes" then
	rs("Ok")=True
  else
	rs("Ok")=False
  end if 
   
  '***************************************
	'删除无用的上传文件
	if ObjInstalled=True and UploadFiles<>"" then
		dim fso,strRubbishFile
		Set fso = Server.CreateObject("Scripting.FileSystemObject")
		if instr(UploadFiles,"|")>1 then
			dim arrUploadFiles,intTemp
			arrUploadFiles=split(UploadFiles,"|")
			UploadFiles=""
			for intTemp=0 to ubound(arrUploadFiles)
				if instr(Content,arrUploadFiles(intTemp))<=0 and arrUploadFiles(intTemp)<>FirstImageName then
					strRubbishFile=server.MapPath("../" & arrUploadFiles(intTemp))
					if fso.FileExists(strRubbishFile) then
						fso.DeleteFile(strRubbishFile)
						response.write "<br><li>" & arrUploadFiles(intTemp) & "在文章中没有用到，也没有被设为首页图片，所以已经被删除！</li>"
					end if
				else
					if intTemp=0 then
						UploadFiles=arrUploadFiles(intTemp)
					else
						UploadFiles=UploadFiles & "|" & arrUploadFiles(intTemp)
					end if
				end if
			next
		else
			if instr(Content,UploadFiles)<=0 and UploadFiles<>FirstImageName then
				strRubbishFile=server.MapPath("../" & UploadFiles)
				if fso.FileExists(strRubbishFile) then
					fso.DeleteFile(strRubbishFile)
					response.write "<br><li>" & UploadFiles & "在文章中没有用到，也没有被设为首页图片，所以已经被删除！</li>"
				end if
				UploadFiles=""
			end if
		end if
		set fso=nothing
	end If
	'结束
	'***************************************
  rs("FirstImageName")=FirstImageName     
end sub
%>

