<%
'=========================================================
' 文件：iXs_ClsFileUp.asp
' 版本：爱雪儿图文管理系统 PAMS Ver 1.0.0
' 全称：iXuEr Photo & Article Management System Version 1.0.0
' 时间：2006-01-05
' 作者：Guidy
' 版权：iXuEr Studio
'=========================================================
' Copyright (C) 2005-2008 iXuEr.Net All Rights Reserved .
' 官方网站：http://www.ixuer.net/
' 技术论坛：http://bbs.ixuer.net/
' 电子信箱：guidy@qq.com
'=========================================================
' 爱雪儿图文管理系统 无组件上传类
'=========================================================
Class iXs_ClsUp
	Private p_MaxSize, p_TotalSize, p_FileType, p_SavePath, p_AutoSave, p_Error
	Private ObjForm, BinForm, BinItem, LngTime
	Public	FormItem, FileItem, StrDate, p_UpLoadPID
	' ============================================
	' 返回版本信息
	' ============================================
	Public Property Get Version
		Version = "无组件上传类 Version 1.1.0"
	End Property
	' ============================================
	' 返回错误信息
	' ============================================
	Public Property Get Error
		Error = p_Error
	End Property
	' ============================================
	' 返回上传文件最大值 单个文件
	' ============================================
	Public Property Get MaxSize
		MaxSize = p_MaxSize
	End Property
	' ============================================
	' 设置上传文件最大值 单个文件
	' ============================================
	Public Property Let MaxSize(LngSize)
		If IsNumeric(LngSize) Then
			p_MaxSize = Clng(LngSize)
		End If
	End Property
	' ============================================
	' 返回上传文件最大值 全局
	' ============================================
	Public Property Get TotalSize
		TotalSize = p_TotalSize
	End Property
	' ============================================
	' 设置上传文件最大值 全局
	' ============================================
	Public Property Let TotalSize(LngSize)
		If IsNumeric(LngSize) Then
			p_TotalSize = Clng(LngSize)
		End If
	End Property
	' ============================================
	' 返回允许上传的文件类型 扩展名
	' ============================================
	Public Property Get FileType
		FileType = p_FileType
	End Property
	' ============================================
	' 设置允许上传的文件类型 扩展名
	' ============================================
	Public Property Let FileType(strType)
		p_FileType = strType
	End Property
	' ============================================
	' 返回文件保存路径
	' ============================================
	Public Property Get SavePath
		SavePath = p_SavePath
	End Property
	' ============================================
	' 设置文件保存路径
	' ============================================
	Public Property Let SavePath(StrPath)
		p_SavePath = Replace(StrPath, chr(0), "")
	End Property
	' ============================================
	' 返回自动保存文件
	' ============================================
	Public Property Get AutoSave
		AutoSave = p_AutoSave
	End Property
	' ============================================
	' 设置文件保存路径
	' ============================================
	Public Property Let UpLoadPID(PID)
		p_UpLoadPID = PID
	End Property
	Public Property Get UpLoadPID()
		UpLoadPID = p_UpLoadPID
	End Property
	' ============================================
	' 设置Open方法处理文件的方式，对其他方法无效
	' ============================================
	Public Property Let AutoSave(byVal Flag)
		Select Case Flag
			Case 0 ' 取无重复的服务器时间字符串为文件名自动保存文件
			Case 1 ' 取源文件名自动保存文件
			Case 2 ' 不自动保存文件，Open之后请用Save/GetData方法保存文件
			Case False Flag = 2
			Case Else Flag = 0
		End Select
		p_AutoSave = Flag
	End Property
	' ============================================
	' 类初始化
	' ============================================
	Private Sub Class_Initialize
		p_Error	    = -1
		p_MaxSize   = 1536000 ' 单位：字节
		p_FileType  = "gif/jpg/jpeg/bmp/png/rar/txt/zip/mid"
		p_SavePath  = "UploadFile/"
		p_AutoSave  = 0
		p_TotalSize = 536870912 ' 这里直接设置为500M，不必在类中构建接口了。
		StrDate	    = FormatTime(Now(), 1)
		Randomize Timer()
		LngTime	    = Clng(10000 + Rnd() * 89999)
		' 生成上传ID编号
		p_UpLoadPID = StrDate & LngTime
		Set BinForm = Server.CreateObject(ServerObject_003)
		Set BinItem = Server.CreateObject(ServerObject_003)
		Set ObjForm = Server.CreateObject(ServerObject_004)
		ObjForm.CompareMode = 1
	End Sub
	' ============================================
	' 类结束
	' ============================================
	Private Sub Class_Terminate
		On Error Resume Next
		ObjForm.RemoveAll
		Set ObjForm = Nothing
		Set BinItem = Nothing
		If p_Error <> 4 Then BinForm.Close()
		Set BinForm = Nothing
	End Sub
	' ============================================
	' 打开对象，打开之前可以重定义对象参数属性MaxSize等
	' ============================================
	Public Sub Open()
		If p_Error = -1 Then
			p_Error = 0
		Else
			Exit Sub
		End If
		Dim LngRequestSize, LngReadSize, BinRequestData, StrFormItem, StrFileItem ,p_ChunkByte, IntTemp, StrTemp
		Const StrSplit = "'"">"
		LngRequestSize = Request.TotalBytes
		If (LngRequestSize < 1) Or ((LngRequestSize > p_TotalSize) And p_TotalSize <> 0) Then
			p_Error = 4
			Exit Sub
		End If
		BinForm.Type = 1
		BinForm.Open
		LngReadSize = 0
		p_ChunkByte = 102400
		BinItem.Type = 2
		BinItem.Charset = "GB2312"
		BinItem.Open
		Response.Flush()
		Do
			On Error Resume Next
			BinForm.Write Request.BinaryRead(p_ChunkByte)
			LngReadSize = LngReadSize + p_ChunkByte
			If  LngReadSize >= LngRequestSize Then Exit Do
			BinItem.WriteText "lngTotalSize=" & LngRequestSize & ";lngReadSize=" & LngReadSize & ";"
			BinItem.SaveToFile Server.MapPath("UpLoadData/" & p_UpLoadPID & ".js"), 2
			Response.flush()
		Loop
		BinItem.WriteText "lngTotalSize=" & LngRequestSize & ";lngReadSize=" & LngReadSize & ";"
		BinItem.SaveToFile Server.MapPath("UpLoadData/" & p_UpLoadPID & ".js"), 2
		BinItem.Close()
		Response.Flush()
		BinForm.Position = 0
		BinRequestData = BinForm.Read()

		Dim bCrLf, StrSeparator, IntSeparator
		bCrLf = ChrB(13) & ChrB(10)

		IntSeparator = InStrB(1, BinRequestData, bCrLf)-1
		StrSeparator = LeftB(BinRequestData, IntSeparator)

		Dim p_Start, p_End, StrItem, StrInam
		Dim StrFtyp, StrFnam, StrFext, LngFsiz
		p_Start = IntSeparator + 2
		Do
			p_End  = InStrB(p_Start, BinRequestData, bCrLf & bCrLf) + 3
			BinItem.Type=1
			BinItem.Open
			BinForm.Position = p_Start
			BinForm.CopyTo BinItem, p_End - p_Start
			BinItem.Position = 0
			BinItem.Type = 2
			BinItem.Charset = "GB2312"
			StrItem = BinItem.ReadText
			BinItem.Close()

			p_Start = p_End
			p_End  = InStrB(p_Start, BinRequestData, StrSeparator)-1
			BinItem.Type = 1
			BinItem.Open
			BinForm.Position = p_Start
			LngFsiz = p_End-p_Start-2
			BinForm.CopyTo BinItem, LngFsiz

			IntTemp = InStr(39, StrItem, """")
			StrInam = Mid(StrItem, 39, IntTemp-39)

			If InStr(IntTemp, StrItem, "filename=""") <> 0 Then
				If Not ObjForm.Exists(StrInam & "_From") Then
					StrFileItem = StrFileItem & StrSplit & StrInam
					If BinItem.Size <> 0 Then
						IntTemp = IntTemp + 13
						StrFtyp = Mid(StrItem, InStr(IntTemp, StrItem, "Content-Type: ") + 14)
						StrTemp = Mid(StrItem, IntTemp, InStr(IntTemp, StrItem, """") - IntTemp)
						IntTemp = InStrRev(StrTemp, "\")
						StrFnam = Mid(StrTemp, IntTemp + 1)
						ObjForm.Add StrInam & "_Type", Replace(StrFtyp, vbCrLF, "")
						ObjForm.Add StrInam & "_Name", StrFnam
						ObjForm.Add StrInam & "_Path", Left(StrTemp, IntTemp)
						ObjForm.Add StrInam & "_Size", LngFsiz
						If InStr(IntTemp, StrTemp, ".") <> 0 Then
							StrFext = Mid(StrTemp, InStrRev(StrTemp, ".") + 1)
						Else
							StrFext = ""
						End If
						If Left(StrFtyp, 6) = "image/" Then
							BinItem.Position = 0
							BinItem.Type = 1
							StrTemp = BinItem.Read(10)
							' 更改了原来的判断方法，兼容性更强
							If InStr(StrFtyp, "jpeg") > 0 Then
								If LCase(StrFext) <> "jpg" Then StrFext = "jpg"
								BinItem.Position = 3
								Do While Not BinItem.EOS
									Do
										IntTemp = AscB(BinItem.Read(1))
									Loop While IntTemp = 255 And Not BinItem.EOS
									If IntTemp < 192 Or IntTemp > 195 Then
										BinItem.Read(Bin2Val(BinItem.Read(2))-2)
									Else
										Exit Do
									End If
									Do
										IntTemp = AscB(BinItem.Read(1))
									Loop While IntTemp < 255 And Not BinItem.EOS
								Loop
								BinItem.Read(3)
								ObjForm.Add StrInam & "_Height", Bin2Val(BinItem.Read(2))
								ObjForm.Add StrInam & "_Width", Bin2Val(BinItem.Read(2))
							ElseIf InStr(StrFtyp, "/png") > 0 Then
								If LCase(StrFext) <> "png" Then StrFext = "png"
								BinItem.Position = 18
								ObjForm.Add StrInam & "_Width", Bin2Val(BinItem.Read(2))
								BinItem.Read(2)
								ObjForm.Add StrInam & "_Height", Bin2Val(BinItem.Read(2))
							ElseIf InStr(StrFtyp, "/gif") > 0 Then
								If LCase(StrFext) <> "gif" Then StrFext = "gif"
								BinItem.Position = 6
								ObjForm.Add StrInam & "_Width", BinVal2(BinItem.Read(2))
								ObjForm.Add StrInam & "_Height", BinVal2(BinItem.Read(2))
							ElseIf InStr(StrFtyp, "/bmp") > 0 Then
								If LCase(StrFext) <> "bmp" Then StrFext = "bmp"
								BinItem.Position = 18
								ObjForm.Add StrInam & "_Width", BinVal2(BinItem.Read(4))
								ObjForm.Add StrInam & "_Height", BinVal2(BinItem.Read(4))
							Else
								' **************************************
								' 其他图片，强制返回 4 : 3 
								' **************************************
									ObjForm.Add StrInam & "_Width", 200
									ObjForm.Add StrInam & "_Height", 150
								' **************************************
							End If
						ElseIf InStr(StrFtyp, "shockwave-flash") > 0 Then
							' **************************************
							' 增加检测FLASH文件宽度和高度的方法
							' MIME:application/x-shockwave-flash
							' **************************************
							If LCase(StrFext) <> "swf" Then StrFext = "swf"
							Dim BinData, sConv, nBits
							BinItem.Position = 0
							BinItem.Type = 1
							BinItem.Read(8)
							BinData = BinItem.Read(1)
							sConv = Num2Str(AscB(BinData), 2 ,8)
							nBits = Str2Num(Left(sConv, 5), 2)
							sConv = Mid(sConv, 6)
							While (Len(sConv) < nBits * 4)
								BinData = BinItem.Read(1)
								sConv = sConv & Num2Str(AscB(BinData), 2 ,8)
							Wend
							ObjForm.Add StrInam & "_Width", Int(Abs(Str2Num(Mid(sConv, 1 * nBits + 1, nBits), 2) - Str2Num(Mid(sConv, 0 * nBits + 1, nBits), 2)) / 20)
							ObjForm.Add StrInam & "_Height", Int(Abs(Str2Num(Mid(sConv, 3 * nBits + 1, nBits), 2) - Str2Num(Mid(sConv, 2 * nBits + 1, nBits), 2)) / 20)
						Else
							' **************************************
							' 当获取的对象不是图片和FLASH的时候，强制返回 0
							' **************************************
								ObjForm.Add StrInam & "_Width", 0
								ObjForm.Add StrInam & "_Height", 0
							' **************************************
						End If
						ObjForm.Add StrInam & "_Ext", StrFext
						ObjForm.Add StrInam & "_From", p_Start
						IntTemp = GetFerr(LngFsiz, StrFext)
						If p_AutoSave <> 2 Then
							ObjForm.Add StrInam & "_Err", IntTemp
							If IntTemp = 0 Then
								If p_AutoSave = 0 Then
									StrFnam = GetTimeStr()
									If StrFext <> "" Then StrFnam = StrFnam & "." & StrFext
								End If

								'Dim fso00, tx00
								'Set fso00 = Server.CreateObject(ServerObject_005)
								'Set tx00 = fso00.CreateTextFile("G:\oBlog-4.0\20060927\cm-mov\1.txt")
								'tx00.Write("run...1")
								'tx00.Close
								'Set tx00 = Nothing
								'Set fso00 = Nothing

								BinItem.SaveToFile Server.MapPath(p_SavePath & StrFnam), 2
								ObjForm.Add StrInam, StrFnam
							End If
						End If
					Else
						ObjForm.Add StrInam & "_Err", -1
					End If
				End If
			Else
				BinItem.Position = 0
				BinItem.Type = 2
				BinItem.Charset = "GB2312"
				StrTemp = BinItem.ReadText
				If ObjForm.Exists(StrInam) Then
					ObjForm(StrInam) = ObjForm(StrInam) & "," & StrTemp
				Else
					StrFormItem = StrFormItem & StrSplit & StrInam
					ObjForm.Add StrInam, StrTemp
				End If
			End If

			BinItem.Close()
			p_Start = p_End + IntSeparator + 2
		Loop Until p_Start + 3 > LngRequestSize
		FormItem = split(StrFormItem, StrSplit)
		FileItem = split(StrFileItem, StrSplit)
	End Sub
	' ============================================
	' 获取时间整数串 作为文件名称
	' ============================================
	Private Function GetTimeStr()
		LngTime = LngTime + 1
		GetTimeStr = StrDate & LngTime
	End Function

	Private Function GetFerr(LngFsiz, StrFext)
		Dim IntFerr
		IntFerr = 0
		If LngFsiz > p_MaxSize And p_MaxSize > 0 Then
			If p_Error = 0 Or p_Error = 2 Then p_Error = p_Error + 1
			IntFerr = IntFerr + 1
		End If
		If InStr(1, LCase("/" & p_FileType & "/"), LCase("/" & StrFext & "/")) = 0 And p_FileType <> "" Then
			If p_Error < 2 Then p_Error = p_Error + 2
			IntFerr = IntFerr + 2
		End If
		GetFerr = IntFerr
	End Function
	' ============================================
	' 保存文件，成功保存返回Ture，否则返回False
	' ============================================
	Public Function Save(Item, StrFnam)
		Rem ******************************************
		Rem Item是表单中file元素
		Rem StrFnam是保存的文件名，可选值：
		Rem 　　０：自动取无重复的服务器时间字符串为文件名
		Rem 　　１：自动取源文件名
		Rem ******************************************
		Save = False
		If ObjForm.Exists(Item & "_From") Then
			Dim IntFerr, StrFext
			StrFext = ObjForm(Item & "_Ext")
			IntFerr = GetFerr(ObjForm(Item & "_Size"), StrFext)
			If ObjForm.Exists(Item & "_Err") Then
				If IntFerr = 0 Then
					ObjForm(Item & "_Err") = 0
				End If
			Else
				ObjForm.Add Item & "_Err", IntFerr
			End If
			If IntFerr <> 0 Then Exit Function
			If VarType(StrFnam) = 2 Then
				Select Case StrFnam
					Case 0
						StrFnam = GetTimeStr()
						If StrFext <> "" Then StrFnam = StrFnam & "." & StrFext
					Case 1
						StrFnam = ObjForm(Item & "_Name")
				End Select
			End If
			BinItem.Type = 1
			BinItem.Open
			BinForm.Position = ObjForm(Item & "_From")
			BinForm.CopyTo BinItem,ObjForm(Item & "_Size")
			'Response.Write(p_SavePath & StrFnam)
								'Dim fso00, tx00
								'Set fso00 = Server.CreateObject(ServerObject_005)
								'Set tx00 = fso00.CreateTextFile("G:\oBlog-4.0\20060927\cm-mov\5.txt")
								'tx00.Write("run...5" & VBCRLF & p_SavePath & VBCRLF & StrFnam)
								'tx00.Close
								'Set tx00 = Nothing
								'Set fso00 = Nothing
			if InStr(p_SavePath, ":") > 1 then
				BinItem.SaveToFile p_SavePath & StrFnam, 2
			else
				BinItem.SaveToFile Server.MapPath(p_SavePath & StrFnam), 2
			End if

			BinItem.Close()
			If ObjForm.Exists(Item) Then
				ObjForm(Item) = StrFnam
			Else
				ObjForm.Add Item, StrFnam
			End If
			Save = True
		End If
	End Function
	' ============================================
	' 获取数据流，可用来保存文件到数据库，试图获取非文件数据时返回空值
	' ============================================
	Public Function GetData(Item)
		GetData = ""
		If ObjForm.Exists(Item & "_From") Then
			If GetFerr(ObjForm(Item & "_Size"), ObjForm(Item & "_Ext")) <> 0 Then Exit Function
			BinForm.Position = ObjForm(Item & "_From")
			GetData = BinFormStream.Read(ObjForm(Item & "_Size"))
		End If
	End Function
	' ============================================
	' 返回文件信息
	' ============================================
	Public Function Form(Item)
		If ObjForm.Exists(Item) Then
			Form = ObjForm(Item)
		Else
			Form = ""
		End If
	End Function
	' ============================================
	' 转换到二进制
	' ============================================
	Private Function BinVal2(Bin)
		Dim LngValue,i
		LngValue = 0
		For i = LenB(Bin) to 1 Step -1
			LngValue = LngValue * 256 + AscB(MidB(Bin, i, 1))
		Next
		BinVal2 = LngValue
	End Function
	' ============================================
	' 二进制转换
	' ============================================
	Private Function Bin2Val(Bin)
		Dim LngValue, i
		LngValue = 0
		For i = 1 To LenB(Bin)
			LngValue = LngValue * 256 + AscB(MidB(Bin, i, 1))
		Next
		Bin2Val = LngValue
	End Function

    Private Function Num2Str(Num, Base, Lens)
        Dim Ret
        Ret = ""
        While(Num >= Base)
            Ret = (Num Mod Base) & Ret
            Num = (Num - Num Mod Base) / Base
        Wend
        Num2Str = Right(String(Lens, "0") & Num & Ret, Lens)
    End Function

    Private Function Str2Num(Str, Base)
        Dim Ret, I
        Ret = 0 
        For I = 1 To Len(Str)
            Ret = Ret * base + Cint(Mid(Str, I, 1))
        Next
        Str2Num = Ret
    End Function
End Class
%>