<%@ Language="VBScript" %>

<!-- #include file="inc/dbconn.inc.asp" //-->
<%
dim formName,file,strFileExt,iRanNum
dim strSubAction,strTitle,strFileName,iFileSize,iType,iUploadFileSize
pid = Request("pid")
id = 0
if trim(Request("id")) <> "" then
	id = clng(request("id"))
end if

'判断文件类型是否合格
private function CheckFileExt(fileEXT)
	dim Forumupload,i
	Forumupload="dll,exe"
	Forumupload=split(Forumupload,",")
	for i=0 to ubound(Forumupload)
		if lcase(fileEXT)=lcase(trim(Forumupload(i))) then
			CheckFileExt=false
		else
			CheckFileExt=true
			exit function
		end if
	next
end function

'创建预览图片:call CreateView(原始文件的路径,预览文件名及路径)
Sub CreateView(imageName,tempFilename)
	'定义变量
	Dim ImageWidth, ImageHeight, ImageMode
	Dim PreviewImageFolderName
	Dim ogvbox,objFont
	Dim Logobox,LogoPath
	LogoPath = Server.MapPath(SystemUpload &"logo.gif")  '//加入图片所在路径及文件名

	'---------------------AspJpegV1.2---------------
		
	Set Logobox = Server.CreateObject("Persits.Jpeg")
	'*添加水印图片	添加时请关闭水印字体*
	'//读取添加的图片
	Logobox.Open LogoPath
	'//重新设置图片的大小
	Logobox.Width = 175		'// 加入图片的原宽度
	Logobox.Height = 60		'// 加入图片的原高度
	'*添加水印图片*
	ImageWidth = 165
	ImageHeight = 124
	ImageMode = ""
	Set ogvbox = Server.CreateObject("Persits.Jpeg")
	' 读取要处理的原文件
	ogvbox.Open Trim(Server.MapPath(imagename))
	If ogvbox.OriginalWidth<Cint(ImageWidth) or ogvbox.Originalheight<Cint(ImageHeight) Then
		Set ogvbox = Nothing
		Exit Sub
	Else
		if strFileExt <> "gif" Then
			'//关于修改字体及文字颜色的
			'ogvbox.Canvas.Font.Color	= &HF7A300		'// 文字的颜色
			'ogvbox.Canvas.Font.Family	= "Arial"	'// 文字的字体 monospace
			'ogvbox.Canvas.Font.Bold = True
			' Draw frame: black, 2-pixel width
			'ogvbox.Canvas.Print ogvbox.Width-210, ogvbox.Height-30, ImageMode		'// 加入文字的位置坐标
			ogvbox.Canvas.Pen.Color		= &H000000		'// 边框的颜色
			ogvbox.Canvas.Pen.Width		= 1				'// 边框的粗细
			ogvbox.Canvas.Brush.Solid	= False			'// 图片边框内是否填充颜色
			ogvbox.DrawImage ogvbox.Width-180, ogvbox.Height-65, Logobox,0.3,&H000000'// 加入图片的位置价坐标（添加水印图片）
			'ogvbox.Canvas.Bar 0, 0, ogvbox.Width, ogvbox.Height	'// 图片边框线的位置坐标
			ogvbox.Save Server.MapPath(imagename)		'// 生成文件
		End If
	End If
	Set Logobox=Nothing
	Set ogvbox = Nothing
End Sub

'处理提交信息 ========================================
strSubAction = Request("subaction")
if strSubAction = "edit" then
	dim upload
	Server.ScriptTimeOut = 300

	sql = "SELECT uploadFileSize FROM bs_fileSize"
	set rs = conn.Execute(sql)
	if not rs.EOF then
		iUploadFileSize = rs("uploadFileSize")
	end if
	rs.Close

	set upload = new UpFile_Class	'建立上传对象
	upload.GetDate(1024000)	'取得上传数据,不限大小
	iFileSize = Int(Request.TotalBytes / 1024 + .5)
	if Request.TotalBytes/1024 > CLng(iUploadFileSize) then
		Response.Write "<script language=""javascript"">"& vbCrLf
		Response.Write "	alert('您上传的文件大小已超过限制！');"& vbCrLf
		Response.Write  "</script>"& vbCrLf
		Response.End
	end if

	if upload.err > 0 then
		select case upload.err
			case 1
				errorMsg = "请先选择你要上传的文件　[ <a href=""#"" onclick=""history.go(-1)"">重新上传</a> ]"
			case 2
				errorMsg = "图片大小超过了限制 "& iUploadFileSize &"K　[ <a href=""#"" onclick=""history.go(-1)"">重新上传</a> ]"
		end select
		Response.End
	else
		'在目录后加(/)
		strTitle = upload.form("title")
		iType = 0
		if trim(upload.form("type")) <> "" then
			iType = clng(upload.form("type"))
		end if
		for each formName in upload.file '列出所有上传了的文件
			set file = upload.file(formName)  '生成一个文件对象
			if (file.filesize<100) and (id = 0) then
				errorMsg = "请先选择你要上传的图片　[ <a href=""#"" onclick=""history.go(-1)"">重新上传</a> ]"
			end if

			strFileExt = lcase(file.FileExt)
			if CheckFileExt(strFileExt)=false then
				errorMsg = "文件格式不正确　[ <a href=""#"" onclick=""history.go(-1)"">重新上传</a> ]"
			end if

			if (file.filesize>100) and (errorMsg = "") then
				randomize
				iRanNum = int(90000*rnd)+10000
				strFileName = year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&iRanNum&"."&strFileExt
				if file.FileSize>0 then
					file.SaveToFile Server.mappath(SystemUpload & strFileName)   '保存文件
					if strFileExt = "jpg" then
						Call CreateView(SystemUpload & strFileName, "")
					end if
				end if
			end if
			set file = nothing
		next
		if strFileName = "" then
			strFileName = upload.form("oldFileName")
			iFileSize = clng(upload.form("oldFileSize"))
		end if
		set upload = nothing
	end if

	if errorMsg = "" then
		if id > 0 then
			sql = "UPDATE cm_attachment SET title='"& strTitle &"',fileName='"& strFileName &"',fileSize='"& iFileSize &"',type="& iType &" WHERE id="& id
		else
			sql = "INSERT INTO cm_attachment(pid,title,fileName,fileSize,type)	VALUES('"& pid &"','"& strTitle &"','"& strFileName &"','"& iFileSize &"',"& iType &")"
		end if
		conn.Execute(sql)
	end if
elseif strSubAction = "del" then
	idList = split(trim(Request("SID")),",")
	for i=0 to ubound(idList)
		sql = "delete from cm_attachment where pid="& pid &" and id="& idList(i)
		conn.Execute(sql)
	next
	errorMsg = "删除成功！"
end if
%>
<html>
<head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  <link rel="stylesheet" href="Common/main.css" type="text/css">
  <script language="javascript" src="Common/FunDefine.js"></script>
</head>

<body bgcolor="#EFEFEF" text="#000000" leftmargin="0" marginwidth="0" topmargin="5">
<%
if errorMsg <> "" then
	DisplayMessage errorMsg,True
end if
'初始化信息 ========================================
if (strSubAction = "add") or (strSubAction = "preEdit") then
	iFileSize = 0
	if id > 0 then
		sql = "select title,fileName,fileSize,type from cm_attachment where id="& id
		rs.Open sql,conn,1,1
		if not rs.EOF then
			strTitle = rs("title")
			strFileName = rs("fileName")
			iFileSize = rs("fileSize")
			iType = rs("type")
		end if
		rs.Close
	end if
%>
<table width="100%" border="1" cellspacing="0" cellpadding="0" bordercolorlight="#CCE0CF" bordercolordark="#FFFFFF">
<form name="InfoEdit" action="attachment.asp?subaction=edit&pid=<% =pid %>&id=<% =id %>" enctype="multipart/form-data" method="post" onSubmit="return checkForm();">
<input type="hidden" name="oldFileName" value="<%=strFileName%>">
<input type="hidden" name="oldFileSize" value="<%=iFileSize%>">
  <tr class="InputTableRow">
    <td align="right" width="60"><b>标题：</b></td>
    <td><input type="text" name="title" value="<% =strTitle %>" size="17" class="ButtonEdit"></td>
    <td align="right" width="80"><b>文件：</b></td>
    <td><input type="file" name="fileName" size="10" class="ButtonEdit"></td>
	<td align="right" width="32"><input type="checkbox" name="type" value="1"<%if iType = 1 then response.write " checked" end if%>></td>
    <td width="60">图片新闻</td>
    <td><input type="submit" name="Submit" value=" 提交 " class="ButtonReply"></td>
  </tr>
</form>
</table>
<script language="JavaScript">
<!--
  function checkForm()
  {
          if (document.InfoEdit.title.value.length == 0)
          {
                  alert("请输入附件名称！");
                  document.InfoEdit.title.focus();
                  return false;
          }
          return true;
  }
//-->
</script>
<br>
<%
end if
%>
<table width="100%" border="1" cellspacing="0" cellpadding="0" bordercolorlight="#CCE0CF" bordercolordark="#FFFFFF">
<form name="listForm" action="attachment.asp?pid=<%=pid%>" method="post">
<input type="hidden" name="subaction">
<input type="hidden" name="LOGINID" value="<%=LOGINID%>">
  <tr class="ListTableHeader">
    <td nowrap background="images/TableHeaderBg.gif" width="21"><input type="checkBox" name="checkAll" value="on" OnClick="OnCheckAll(this.form)"></td>
    <td nowrap background="images/TableHeaderBg.gif" align="center">标题</td>
    <td nowrap background="images/TableHeaderBg.gif" width="180" align="center">链接地址</td>
    <td nowrap background="images/TableHeaderBg.gif" width="60" align="center">尺寸</td>
    <td nowrap background="images/TableHeaderBg.gif" width="60" align="center">图片</td>
    <td nowrap background="images/TableHeaderBg.gif" width="42" align="center"></td>
  </tr>
<%
	'从数据库中读出附件列表 ========================================
	bFirst = false
	sql = "select id,title,fileName,fileSize,type from cm_attachment where pid="& pid &" order by id Desc"
	rs.Open sql,conn,1,1
	do while not rs.Eof
%>
  <tr class="InputTableRow" id="Row<%=rs("id")%>" onClick="javascript:setRowFocus(this,false,<%=rs("id")%>)">
    <td nowrap align="center"><input type="checkBox" name="SID" value="<%=rs("id")%>"></td>
    <td nowrap>&nbsp;<%=rs("title")%></td>
    <td nowrap>&nbsp;<%=rs("fileName")%></td>
    <td nowrap align="right">&nbsp;<%=rs("fileSize")%></td>
    <td nowrap align="center">&nbsp;<%if rs("type") then response.write "<font color=""RED"">是</font>" else response.write "否" end if%></td>
    <td nowrap align="center"><span class="OperateIcon" onClick="javascript:setRowFocus(Row<%=rs("id")%>,false,<%=rs("id")%>);attachEdit('attachment',<%=pid%>,<%=rs("id")%>)" title="修改"><img src="images/IconEdit.gif" border="0" width="16" height="16" alt="修改"></span>&nbsp;<span class="OperateIcon" onClick="javascript:setRowFocus(Row<%=rs("id")%>,false,<%=rs("id")%>);attachDel('attachment',<%=pid%>,<%=rs("id")%>,'您正要删除【<%=rs("title")%>】。\n本操作将永久删除该记录。请确认！')" title="删除"><img src="images/IconDelete.gif" border="0" width="16" height="16" alt="删除"></span></td>
  </tr>
<%
		if bFirst = false then
%>
  <script LANGUAGE="javascript">
  <!--
	setRowFocus(Row<%=rs("id")%>,true,<%=rs("id")%>);
  //-->
  </script>
<%
			bFirst = true
		end if
		rs.MoveNext
	loop
%>
</form>
</table>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<form name="PageForm" action="attachment.asp" method="post">
  <tr height="22">
    <td class="MainPageBar" style="PADDING-TOP: 2px;padding-left:4px;" valign="middle" width="20"><a href="attachment.asp?pid=<%=pid%>&subaction=add&id=0"><img src="images/IconAdd.gif" width="16" height="16" border="0" alt="新建"></a></td>
    <td class="MainPageBar" style="PADDING-TOP: 2px;padding-left:4px;" valign="middle">&nbsp;</td>
  </tr>
</form>
</table>
</body>
</html>