<!-- #Include File="Config.asp" -->
<!--#include file="Cls_Upfile.asp"-->
<%
'=======================================================
' 欢迎使用《阿赛大型门户网站系统V3.0》，简称“AsaiMHV3”
' 技术支持、商业版咨询、官方唯一指定联系QQ“4941172”
' 官方网站：http://77ya.com      程序负责人：阿赛
'=======================================================

tit="上传文件"
Dim strSetting,strJs
Dim IsReName,IsAutoSort
Dim strFilePath,InceptType,InceptMaxSize,Today_num
Dim Upload,FormName,FilePath,ChildFilePath
Dim File,F_FileName,F_ViewName,F_Filesize,F_FileExt,F_Type
Dim Previewpath,DrawInfo,InceptMaxFile
Dim ErrMsg,FoundErr,IsTrueUpfiles,ComeUrl
Dim JsObjName,JsObjName2,TObjName
Dim Channel_ID,ModuleID,UpFileType
Dim Up_Face,Up_Type,Up_Filters,Is_Upload
strSetting=Cls.Web_UploadType
Up_Face=strSetting(17)
Up_Type=strSetting(18)
Channel_ID=Cls.Code_Query("ID")
UpFileType=Cls.Code_Query("Type")
JsObjName=Cls.Code_Query("Obj")
TObjName=Cls.Code_Query("TObj")	
ErrMsg="":FoundErr=false
IsTrueUpfiles=0:IsReName=1:IsAutoSort=1  '默认

Call Upload_Head()

Select Case action
Case "save"
	Call Remote_Save()
Case "up"
	Call Upfile_Main()
Case Else
	Call UpLoad_Main()
End Select

If FoundErr=True Then
	Call Cls.Out_Msg(ErrMsg,ComeUrl)
End If

Call Upload_End()

Sub Upload_Head()
%>
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
<TITLE><%=Cls.web_name%> - <%=tit%></TITLE>
<style type="text/css">
/* Css */
</style>
<SCRIPT language=javascript>
if (top.location==self.location) { top.location="./"; }
function upload_submit()
{
    var strFileName=document.frm_upload.FileName.value;
	if (strFileName=="")
	{
    	alert("请选择要上传的文件");
		document.frm_upload.FileName.focus();
    	return false;
  	}
}
function SaveFilesUrl(sModuleIDs,type,arr,objname,objname2)
{
	if(objname!='')
	{
		var obj = parent.document.getElementById(objname);
		var strT=arr.split('|');
		if (sModuleIDs=='2'&type!=''){
			var url='下载地址'+(obj.length+1)+'|'+strT[0];
			obj.options[obj.length]=new Option(url,url);
		}
		else if (sModuleIDs=='5'&type!=''){
			var url='图片名称'+(obj.length+1)+'|'+strT[0];
			obj.options[obj.length]=new Option(url,url);
		}
		else if (sModuleIDs=='6'&type!=''){
			var url='影片名称'+(obj.length+1)+'|'+strT[0];
			obj.options[obj.length]=new Option(url,url);
		}		
		else if (sModuleIDs=='4'||sModuleIDs=='9'||(sModuleIDs=='10'&type=='4'))
		{		
      obj.value+=strT[0]+'\n';
		}
		else
		{
			obj.value=strT[0];
		}
		if (objname2!='')
		{
			parent.document.getElementById(objname2).value+=strT[1];
		}
	}
}
function AddUploadFiles(objname,files,filesv){
	if(objname!=''){
		//alert("OK!");
		var obj = parent.document.getElementById(objname);
		obj.options[obj.length]=new Option(filesv,files);
	}
}
</SCRIPT>
</HEAD>
<BODY topmargin=0 leftmargin=0 rightmargin=0 bottommargin=0 marginwidth=0 marginheight=0>
<TABLE border=0 cellspacing=2 cellpadding=0 WIDTH="100%" HEIGHT="100%">
<%
End Sub

Sub Upload_End()
%>
</TABLE>
</BODY>
</HTML>
<%
End Sub

Sub UpLoad_Main()
	Dim PostRanNum
	Randomize
	PostRanNum = Int(900*rnd)+1000
	Session("UploadCode"&Channel_ID&UpFileType) = Cstr(PostRanNum)
	%>
	<FORM name=frm_upload ACTION="Upload.Asp?action=up&id=<%=Channel_ID%>&type=<%=UpFileType%>&obj=<%=JsObjName%>&Tobj=<%=TObjName%>" method=post ENCTYPE="multipart/form-data" onSubmit="javascript:return upload_submit();">
	<INPUT type=hidden NAME="UploadCode<%=Channel_ID%><%=UpFileType%>" VALUE="<%=PostRanNum%>">
	<TR><TD><INPUT type=file name=FileName VALUE="" size=30 class=txt>&nbsp;<INPUT type=submit name=submit VALUE="上传" class=btn></TD></TR>
	</FORM>
	<%
End Sub

Sub Upfile_Main()
	ComeUrl=Request.ServerVariables("HTTP_REFERER")
	If ComeUrl="" Then ComeUrl="javascript:history.go(-1)"
	If strSetting(0)=999 Then
		Call Cls.Out_Msg("系统未开放文件上传功能。",ComeUrl)
	End If
	If Channel_ID<0 or Channel_ID>999 Then
		Call Cls.Out_Msg("本频道上传功能已禁用！",ComeUrl)
	End If
	If not Cls.Post_Chk Then
	        Call Cls.Out_Msg("您从外部提交信息，操作已被禁止。",ComeUrl)
	End If
	'载入用户信息
	Call Cls.LoadUserInfo()
	'检测是否为后台管理员
	If Login_Mode ="" Then
		If Cls.Session_Get(Cls.Web_Cookies&"_Admin")="Admin" Then
			Login_Username = Cls.Session_Get(Cls.Web_Cookies&"_Admins")
			Login_Mode = "admin"		'管理员的权限
			Is_LoginTrue = True
		End If
	End If
	If UserGroup_Config(8)=0 Then
		Call Cls.Out_Msg("此用户组上传功能已关闭！",ComeUrl)
	End If	
	If Is_LoginTrue = False Then
		Call Cls.Out_Msg("你还没有登陆，请登陆后再上传。",ComeUrl)
	End If	
	If Up_Today_Num()=false Then
		Call Cls.Out_Msg("Error：您还没有登陆 或者 您今天已上传"&Today_num&"个文件，每天只能上传"&UserGroup_Config(18)&"个文件。",ComeUrl)
	End If
	Call Cls.Channel_Initialize(Channel_ID)
	If IsNumeric(UpFileType) Then
		'If Cint(UpFileType)<>0 Then UpFileType=0
		ModuleID = Cls.ChannelModuleType
		IsTrueUpfiles=Cls.ChannelUpType
		InceptMaxSize=Cls.ChannelUpMax			'上传大小
		If Channel_ID=0 Then				'用户头像图片上传
			strFilePath=Up_Face			'头像上传路径
			InceptType=Up_Type			'上传类型
			IsReName=1
			IsAutoSort=1
			InceptMaxSize=UserGroup_Config(9)	'上传大小
			If UserGroup_Config(7)=0 Then
				Call Cls.Out_Msg("用户头像上传功能已禁用！",ComeUrl)
			End If
		End If
		If Channel_ID>0 Then
			strFilePath=Trim(Cls.ChannelUploadDir)
		  	If Channel_ID = "4" Then
				InceptType=Cls.ChannelUpFileType(0)&Cls.ChannelUpFileType(1)&Cls.ChannelUpFileType(2)&Cls.ChannelUpFileType(3)		'上传类型
			Else
				InceptType=Cls.ChannelUpFileType(Cint(UpFileType))	'上传类型
			End If
			IsReName=Cls.Get_ChannElsetup(Cls.ChannElsetup,15)
			IsAutoSort=Cls.Get_ChannElsetup(Cls.ChannElsetup,16)
			InceptMaxSize=Cls.ChannelUpMax		'上传大小
			If IsTrueUpfiles=0 Then
				Call Cls.Out_Msg("本频道上传功能已禁用！",ComeUrl)
			End If
		End If
	Else
		IsTrueUpfiles=Cls.ChannelUpType
		InceptType=Cls.ChannelUpFileType(0)&"|"&Cls.ChannelUpFileType(1)&"|"&Cls.ChannelUpFileType(3)	'上传类型
		InceptType	= FiltFileName(InceptType)
		InceptType	= FiltFileName2(InceptType)
	End If
	strFilePath=Cls.Web_Dir &Cls.Web_Upload &"/"&strFilePath&"/"
	FilePath=strFilePath
	If IsAutoSort=1 Then
		FilePath = CreatePath(FilePath)			'按年月分类目录
	End If
	If strSetting(5)=1 Then
		DrawInfo = strSetting(6)
	ElseIf strSetting(5)=2 Then
		DrawInfo = Replace(strSetting(11),"{$web_dir}",Cls.Web_Dir)
		'DrawInfo = strSetting(11)
	Else
		DrawInfo = ""
	End If
	If DrawInfo = "" or Cls.Get_ChannElsetup(Cls.ChannElsetup,11) = 0 Then strSetting(5) = 0
	Server.ScriptTimeOut=999999					'要是上传的文件比较大，就必须设置。
	Set Upload = New UpFile_Cls
	Upload.UploadType		= strSetting(0)			'设置上传组件类型
	Upload.UploadPath		= FilePath			'设置上传路径
	Upload.InceptFileType		= Replace(InceptType,"|",",")	'设置上传文件限制
	Upload.MaxSize			= InceptMaxSize			'单位 KB
	Upload.InceptMaxFile		= 10				'每次上传文件个数上限
	'Upload.ChkSessionName		= "UploadCode"&Channel_ID&UpFileType	'防止重复提交。
	Upload.IsReName			= IsReName                	'是否重命名。
	Upload.PreviewType		= strSetting(1)	        	'设置预览图片组件类型
	Upload.PreviewImageWidth	= strSetting(2)		    	'设置预览图片宽度
	Upload.PreviewImageHeight	= strSetting(3)		    	'设置预览图片高度
	Upload.DrawImageWidth		= strSetting(14)		'设置水印图片或文字区域宽度
	Upload.DrawImageHeight		= strSetting(15)		'设置水印图片或文字区域高度
	Upload.DrawGraph		= strSetting(12)		'设置水印透明度
	Upload.DrawFontColor		= "#"&strSetting(8)		'设置水印文字颜色
	Upload.DrawFontFamily		= strSetting(9)			'设置水印文字字体格式
	Upload.DrawFontSize		= strSetting(7)			'设置水印文字字体大小
	Upload.DrawFontBold		= strSetting(10)		'设置水印文字是否粗体
	Upload.DrawInfo			= DrawInfo			'设置水印文字信息或图片信息
	Upload.DrawType			= strSetting(5)			'0=不加载水印 ，1=加载水印文字，2=加载水印图片
	Upload.DrawXYType		= strSetting(16)		'"0" =左上，"1"=左下,"2"=居中,"3"=右上,"4"=右下
	Upload.DrawSizeType		= strSetting(4)		    	'"0"=固定缩小，"1"=等比例缩小，"2"=关闭
	If strSetting(13)<>"" Then
		Upload.TransitionColor	= "#"&strSetting(13)		'透明度颜色设置
	End If
	If Upload.ErrCodes<>0 Then
		FoundErr=True
		ErrMsg="系统发现以下错误：\n\n"& Upload.Description
		Exit Sub
	Else
		Upload.SaveUpFile
		If Upload.ErrCodes<>0 Then
			FoundErr=True
			ErrMsg="系统发现以下错误：\n\n"& Upload.Description
			Exit Sub
		End If
	End If
	If Upload.Count > 0 Then
		For Each FormName In Upload.UploadFiles
		Set File = Upload.UploadFiles(FormName)
		F_FileName = FilePath & File.FileName
		'Call Cls.Out_Msg(""&F_FileName&"  ,  "&F_Viewname&"",ComeUrl)
		Call Tmp_Upload(f_filename,strSetting(19))		'检查上传文件是否存在非法代码
		If Upload.PreviewType<>999 and File.FileType=1 Then	'创建预览及水印图片
			If strSetting(4)<>2 Then
				F_Viewname = FilePath & "Pre" & Replace(File.FileName,File.FileExt,"") & "jpg"
			End If
			'创建预览图片(原始文件的路径,预览文件名及路径,原文件后缀)
			Upload.CreateView F_FileName,F_Viewname,File.FileExt
		End If
		dim strSaveUrl,strSaveUrl1,strSaveUrl2,strFileSize,strFileName,UpID,F_ID
		If Lcase(left(F_FileName,Len(Cls.Web_Dir & Cls.Web_Upload)))=Lcase(Cls.Web_Dir & Cls.Web_Upload) Then
			strSaveUrl="{$download_dir}"  & mid(F_FileName,Len(Cls.Web_Dir & Cls.Web_Upload)+1)
		ElseIf Lcase(left(F_FileName,Len(Cls.Web_Upload)))=Lcase(Cls.Web_Upload) Then
			strSaveUrl="{$upload_dir}" & mid(F_FileName,Len(Cls.Web_Upload)+1)
		ElseIf left(F_FileName,1)="/" Then
			strSaveUrl="{$web_dir}" & mid(F_FileName,2)
		Else
			strSaveUrl=Trim(F_FileName)
		End If
		strFileSize=Cls.GetSize(File.FileSize,"b")'cstr(round(File.FileSize/1024))
		Call Cls.exec("insert into AsaiMHUpload(UserName,UserIP,UpFileName,UpFileType,UpFileUrl,UpFileSize,UpFileTime) values('"&login_username&"','"&Cls.ip_sys(0)&"','"&File.FileName&"','"&File.FileExt&"','"&strSaveUrl&"','"&strFileSize&"','"&Cls.now_time&"')",0)
		Sql="Select id from AsaiMHUpload where UpFileUrl='"&strSaveUrl&"'"
		Set rs=Cls.exec(sql,1)
		UpID=rs(0)
		rs.close
		F_ID=Encrypt(UpID)		'防盗链ID生成
		If int(Format_Mid_Num(45))=0 Then
			If Channel_ID=0 Then
				strSaveUrl1="{$download_dir}"&mid(F_FileName,Len(Cls.Web_Dir & Cls.Web_Upload)+1)&""
			ElseIf ModuleID=4 or ModuleID=9 or (ModuleID=10 and UpFileType="4") then
				'strSaveUrl1="[upload="&File.FileExt&"]"&strSaveUrl&"[/upload]"
				strSaveUrl1="[upload="&File.FileExt&"]"&strSaveUrl&"[/upload][文件大小："&strfilesize&"]"
			Else
		  		strSaveUrl1=strSaveUrl
			End If
		Else
			If Channel_ID=0 Then
				strSaveUrl1="{$web_dir}Common/ResourcesUp.Asp?id="&F_ID&""
			ElseIf ModuleID=4 or ModuleID=9 or (ModuleID=10 and UpFileType="4") then
				'strSaveUrl1="[upload="&File.FileExt&"]{$web_dir}Common/ResourcesUp.Asp?id="&F_ID&"[/upload]"
				strSaveUrl1="[upload="&File.FileExt&"]{$web_dir}Common/ResourcesUp.Asp?id="&F_ID&"[/upload][文件大小："&strfilesize&"]"
			Else
		  		strSaveUrl1="{$web_dir}Common/ResourcesUp.Asp?id="&F_ID&""
			End If
		End If
		strFileName=File.OldFileName
		If UpFileType="" or UpFileType="0" Then
			UpFileType=""
			JsObjName2=""
		ElseIf ModuleID=1 Then
			JsObjName2="mlpic"
			strSaveUrl2=F_Viewname
		ElseIf ModuleID=2 Then
			JsObjName2="sizes"
			strSaveUrl2=strFileSize
		ElseIf ModuleID=5 Then
			JsObjName2="PhotoSize"
			strSaveUrl2=strFileSize
		End If
		If int(Format_Mid_Num(45))=0 Then
			Response.Write "<SCRIPT language=javascript>SaveFilesUrl('"&ModuleID&"','"&UpFileType&"','"&strSaveUrl1&"|"&strSaveUrl2&"','"&JsObjName&"','"&JsObjName2&"');AddUploadFiles('"&TObjName&"','"&strSaveUrl&"','"&File.FileName&"');</script>"
		Else
			Response.Write "<SCRIPT language=javascript>SaveFilesUrl('"&ModuleID&"','"&UpFileType&"','"&strSaveUrl1&"|"&strSaveUrl2&"','"&JsObjName&"','"&JsObjName2&"');AddUploadFiles('"&TObjName&"','{$web_dir}Common/ResourcesUp.Asp?id="&F_ID&"','"&File.FileName&"');</script>"
		End If
		Set File = Nothing
		Next
	Else
		FoundErr=True
		ErrMsg="请正确选择要上传的文件或文件不符合网站配置规格。"
		Exit Sub
	End If
	Set Upload = Nothing
	Call Cls.Out_Msg("上传文件成功！",ComeUrl)
End Sub

'远程上传（编辑器使用）
Sub Remote_Save()
End Sub

'检测用户每日上传文件数量。
Function Up_Today_Num()
	If Login_Mode = "admin" Then 
		Up_Today_Num = True
		Exit Function
	Else
		If Login_Username = "" Then
			Up_Today_Num=false
			Exit Function
		End If
	End If
	dim ta
	ta="#"
	If SqlType=1 Then ta="'"
	Up_Today_Num=true
	sql="select count(*) from AsaiMHUpload where UserName='"&login_username&"' and UpFileTime>="&ta&formatdatetime(formatdatetime(Cls.now_time,2))&ta
	set rs=Cls.exec(sql,1)
	Today_num=rs(0)
	rs.close
	If not(isnumeric(Today_num)) Then Today_num=0
	If int(Today_num)>=int(UserGroup_Config(18)) Then
		Up_Today_Num=false
	End If
end Function

'按月份自动明名上传文件夹,需要ＦＳＯ组件支持。
Private Function CreatePath(PathValue)
	Dim objFSO,uploadpath
	uploadpath=year(now)&"-"&month(now)	'以年月创建上传文件夹，格式：2003－8
	If Right(PathValue,1)<>"/" Then PathValue = PathValue&"/"
	On Error Resume Next
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
		If objFSO.FolderExists(Server.MapPath(PathValue))=False Then
			objFSO.CreateFolder Server.MapPath(PathValue)
		End If
		If objFSO.FolderExists(Server.MapPath(PathValue & uploadpath))=False Then
			objFSO.CreateFolder Server.MapPath(PathValue & uploadpath)
		End If
		If Err.Number = 0 Then
			CreatePath = PathValue & uploadpath & "/"
		Else
			CreatePath = PathValue
		End If
	Set objFSO = Nothing
End Function
%>