<!-- #include file=../inc/Upload_Setup.jsp -->
<%
Dim PhotoDirectory,UploadPhotoUrl
PhotoDirectory = DEF_BBS_HomeUrl & DEF_BBS_UploadPhotoUrl
UploadPhotoUrl = DEF_BBS_HomeUrl & DEF_BBS_UploadPhotoUrl
Dim GBL_FileType
GBL_FileType = 2
Dim GBL_UploadFlag
GBL_UploadFlag = 0 '0-头像,1-上传文件
Dim GBL_Width,GBL_Height

Const EnableLeadBBSInfo = 1


Function SaveSmallPic(LoadFile,SaveFile,SaveW,SaveH)

	'On Error Resume Next
	Dim ResizeFlag
	Dim MaxWidth
	Dim MaxHeight
	MaxWidth = SaveW
	MaxHeight = SaveH
	Dim Img_Height,Img_Width
	ResizeFlag = 0
	
	Dim MyObj
	Set MyObj = Server.CreateObject("GflAx190.GflAx")
	MyObj.EnableLZW = True
	MyObj.LoadBitmap(LoadFile)
	if err Then
		GBL_FileType = 2
		SaveSmallPic = 0
		Set MyObj = Nothing
		err.clear
		Exit Function
	End If
	
	Img_Height = MyObj.Height
	Img_Width = MyObj.Width
	
	GBL_Width = Img_Width
	GBL_Height = Img_Height
	
	Dim per
	If Img_Height > MaxHeight Or Img_Width > MaxWidth Then	
		If DEF_ResizeMode = 0 Then
			If Img_Height > MaxHeight Then
				per = (Img_Height - MaxHeight)/Img_Height
			End If
	
			If Img_Width > MaxWidth Then
				If (Img_Width - MaxWidth)/Img_Width > per Then per = (Img_Width - MaxWidth)/Img_Width
			End If
			
			Img_Height = Img_Height - Img_Height * per
			Img_Width = Img_Width - Img_Width * per
		Else
			Dim t1,t2
			t1 = Img_Height / MaxHeight
			t2 = Img_Width / MaxWidth
			If t1 > Fix(t1) Then T1 = Fix(t1) + 1
			If t2 > Fix(t2) Then T2 = Fix(t2) + 1
			If T1 < T2 Then T1  = T2
			Img_Height = Img_Height / T1
			Img_Width = Img_Width / T1
		End If
		
		GBL_Width = Fix(Img_Width)
		GBL_Height = Fix(Img_Height)
		MyObj.Resize Img_Width,Img_Height
		ResizeFlag = 1
	Else
		If MyObj.SaveFormat > 3 or inStr(":.gif:.jpg:jpeg:.jpe:.png:",":" & Right(SaveFile,4) & ":") = False Then
			MyObj.Resize Img_Width,Img_Height
		Else
			'Set MyObj = Nothing
			SaveSmallPic = 2 '原文件不动
			GBL_FileType = 0
			If MyObj.NumberOfImages > 1 Then
				Set MyObj = Nothing
				Exit Function
			End If
			If inStr(PhotoDirectory,"/face/") = 0 and inStr(PhotoDirectory,"\face\") = 0 Then
				If MyObj.BitmapType >= 4 Then
					MyObj.FontName = "宋体"
					MyObj.FontSize = 12
					MyObj.TextOut DEF_UploadVersionString,4,4,100*256*256+100*256+100
					MyObj.TextOut DEF_UploadVersionString,3,3,255*256*256+255*256+255
				End If
				MyObj.SaveBitmap(LoadFile)
				Set MyObj = Nothing
				Exit Function
			End If
		End If
	End If

	Dim m,n,TA
	TA = Array("-0---000-000-00--00--00--000-","010-0111011101100110011001110","010-0100010101010101010101000","010-0111011101010111011101110","01000100010101010101010100010","01110111010101100111011101110","-000-000-0-0-00--0000000-000-")
	If MyObj.BitmapType >= 4 and MyObj.NumberOfColorsUsed > 255 Then
		MyObj.SaveFormat = 1
		Rem 小的图片总是最优化存储
		If GBL_Width < 120 and GBL_Height < 120 Then
			MyObj.SaveJPEGQuality = 100
		End If
		MyObj.SaveJPEGProgressive = True
		If inStr(PhotoDirectory,"/face/") and GBL_Width > 32 and EnableLeadBBSInfo = 1 Then
			For m = 0 to Ubound(TA,1)
				For n = 1 to 29
					Select Case Mid(Ta(m),n,1)
						Case "1":MyObj.DrawPoint GBL_Width-31 + n,GBL_Height - 9 + m,0
						Case "0":MyObj.DrawPoint GBL_Width-31 + n,GBL_Height - 9 + m,0,RGB(177,177,177)
					End Select
				Next
			Next
		End If
		If MyObj.BitmapType >= 4 and inStr(PhotoDirectory,"/face/") = 0 and inStr(PhotoDirectory,"\face\") = 0 Then
			MyObj.TextOut DEF_UploadVersionString,4,4,100*256*256+100*256+100
			MyObj.TextOut DEF_UploadVersionString,3,3,255*256*256+255*256+255
		End If
		MyObj.SaveBitmap(SaveFile)
		SaveSmallPic = 1 '正常转换,文件名不改变
	Else
		If ResizeFlag = 1 Then '如果是小于256色图像,并且有缩略图的,采用jpg查看
			MyObj.SaveFormat = 1
			If GBL_Width < 120 and GBL_Height < 120 Then
				MyObj.SaveJPEGQuality = 100
			End If
			SaveSmallPic = 4 '文件扩展名可能有变动
			If (inStr(PhotoDirectory,"/face/") or inStr(PhotoDirectory,"\face\")) and EnableLeadBBSInfo = 1 Then
				For m = 0 to Ubound(TA,1)
					For n = 1 to 29
						Select Case Mid(Ta(m),n,1)
							Case "1":MyObj.DrawPoint GBL_Width-31 + n,GBL_Height - 9 + m,0
							'Case "0":MyObj.DrawPoint GBL_Width-31 + n,GBL_Height - 9 + m,0,RGB(177,177,177)
						End Select
					Next
				Next
			End If
		Else
			MyObj.SaveFormat = 2
			'MyObj.SaveFormat = 1
			SaveSmallPic = 3 '文件扩展名可能有变动
		End If
		If MyObj.BitmapType >= 4 and inStr(PhotoDirectory,"/face/") = 0 and inStr(PhotoDirectory,"\face\") = 0 Then
			MyObj.TextOut DEF_UploadVersionString,4,4,100*256*256+100*256+100
			MyObj.TextOut DEF_UploadVersionString,3,3,255*256*256+255*256+255
		End If
		MyObj.SaveBitmap(SaveFile)
	End If
	Set MyObj = Nothing
	GBL_FileType = 0

End Function

Function CheckUploadDatabase(BigFile,SmallFile)

	If BigFile = "" and SmallFile = "" Then Exit Function
	Dim NBigFile,NSmallFile,Temp,i
	NBigFile = ""
	NSmallFile = ""
	If BigFile <> "" Then
		Temp = Replace(BigFile,"/","\")
		i = 0
		Do while inStr(Temp,"\\")
			Temp = Replace(Temp,"\\","\")
			i = i + 1
			If i > 100 Then Exit do
		Loop
		
		i = inStrRev(Temp,"\")		
		If i > 0 Then
			NBigFile = Mid(Temp,i)
			Temp = Left(Temp,i-1)
		End If

		i = inStrRev(Temp,"\")		
		If i > 0 Then
			NBigFile = Mid(Temp,i) & NBigFile
			Temp = Left(Temp,i-1)
		End If

		i = inStrRev(Temp,"\")
		If i > 0 Then
			NBigFile = Mid(Temp,i) & NBigFile
			Temp = Left(Temp,i-1)
		End If

		i = inStrRev(Temp,"\")		
		If i > 0 Then
			NBigFile = Mid(Temp,i+1) & NBigFile
			Temp = Left(Temp,i-1)
		End If
	End If
	
	
	If SmallFile <> "" Then
		Temp = Replace(SmallFile,"/","\")
		i = 0
		Do while inStr(Temp,"\\")
			Temp = Replace(Temp,"\\","\")
			i = i + 1
			If i > 100 Then Exit do
		Loop
		
		i = inStrRev(Temp,"\")		
		If i > 0 Then
			NSmallFile = Mid(Temp,i)
			Temp = Left(Temp,i-1)
		End If

		i = inStrRev(Temp,"\")		
		If i > 0 Then
			NSmallFile = Mid(Temp,i) & NSmallFile
			Temp = Left(Temp,i-1)
		End If

		i = inStrRev(Temp,"\")
		If i > 0 Then
			NSmallFile = Mid(Temp,i) & NSmallFile
			Temp = Left(Temp,i-1)
		End If

		i = inStrRev(Temp,"\")		
		If i > 0 Then
			NSmallFile = Mid(Temp,i+1) & NSmallFile
			Temp = Left(Temp,i-1)
		End If
	End If

	If GBL_UserID < 1 and GBL_UploadFlag = 0 Then
		If BigFile <> "" Then deletefiles BigFile
		If SmallFile <> "" Then deletefiles SmallFile
		Response.Write "alert('错误，您未登录，不能上传头像．');"
		CloseDatabase
		Response.End
	End If

	Dim SQL,Rs,UserID
	UserID = Left(Request.QueryString("UserID"),14)
	If isNumeric(UserID) = 0 Then UserID = 0
	UserID = cCur(UserID)
	If GBL_UserID>0 and CheckSupervisorUserName = 1 and UserID > 0 Then
		SQL = "Select top 1 * from LeadBBS_UserFace Where UserID=" & UserID
	Else
		UserID = GBL_UserID
		SQL = "Select top 1 * from LeadBBS_UserFace Where UserID=" & UserID
	End If

	If Lcase(Right(NBigFile,4)) = ".swf" then GBL_FileType = 1

	If GBL_UploadFlag = 0 Then
		Set Rs = Server.CreateObject("ADODB.RecordSet")
		Rs.Open SQL,con,2,2
		GBL_DBNum = GBL_DBNum + 1
		If Rs.Eof Then
			Rs.Close
			Set Rs = Nothing
			SQL = "insert into LeadBBS_UserFace(UserID,PhotoDir,SPhotoDir,ndatetime,FileType) Values(" & UserID & ",'" & Replace(NBigFile,"'","''") & "','" & Replace(NSmallFile,"'","''") & "'," & GetTimeValue(DEF_Now) & "," & GBL_FileType & ")"
			Con.Execute(SQL)
			GBL_DBNum = GBL_DBNum + 1
		Else
			If Replace(Lcase(Right(PhotoDirectory,6)),"\","/") <> "/face/" Then PhotoDirectory = PhotoDirectory & "face/"
			If DEF_FSOString <> "" Then deletefiles Server.Mappath(PhotoDirectory & Rs("PhotoDir"))
			NBigFile = LCase(Replace(NBigFile,"/","\"))
			If inStr(NBigFile,Replace(LCase(DEF_BBS_UploadPhotoUrl),"/","\") & "face\") Then
				NBigFile = Replace(NBigFile,"/","\")
				Rs("PhotoDir") = Mid(NBigFile,inStrRev(NBigFile,Replace(LCase(DEF_BBS_UploadPhotoUrl),"/","\") & "face\")+Len(Replace(LCase(DEF_BBS_UploadPhotoUrl),"/","\") & "face\"))
			Else
				Rs("PhotoDir") = NBigFile
			End If
			Rs("NdateTime") = GetTimeValue(DEF_Now)
			Rs.Update
			Rs.Close
			Set Rs = Nothing
		End If
	Else
		NBigFile = LCase(Replace(NBigFile,"/","\"))
		If inStr(NBigFile,Replace(LCase(DEF_BBS_UploadPhotoUrl),"/","\")) < 3 and inStr(NBigFile,Replace(LCase(DEF_BBS_UploadPhotoUrl),"/","\")) Then
			NBigFile = Replace(NBigFile,"/","\")
			NBigFile = Mid(NBigFile,inStrRev(Replace(NBigFile,"/","\"),"\")+1)
		End If
		If inStr(NSmallFile,Replace(LCase(DEF_BBS_UploadPhotoUrl),"/","\")) < 3 and inStr(NSmallFile,Replace(LCase(DEF_BBS_UploadPhotoUrl),"/","\")) Then
			NSmallFile = Replace(NSmallFile,"/","\")
			NSmallFile = Mid(NSmallFile,inStrRev(Replace(NSmallFile,"/","\"),"\")+1)
		End If
		SQL = "insert into LeadBBS_Upload(UserID,PhotoDir,SPhotoDir,ndatetime,FileType) Values(" & UserID & ",'" & Replace(NBigFile,"'","''") & "','" & Replace(NSmallFile,"'","''") & "'," & GetTimeValue(DEF_Now) & "," & GBL_FileType & ")"
		Con.Execute(SQL)
		GBL_DBNum = GBL_DBNum + 1
		If UserID > 0 Then
			If DEF_UploadSpendPoints <> 0 Then
				SQL = "Update LeadBBS_User Set UploadNum=UploadNum+1,Points=Points-" & DEF_UploadSpendPoints & " Where ID=" & UserID
			Else
				SQL = "Update LeadBBS_User Set UploadNum=UploadNum+1 Where ID=" & UserID
			End If
			Con.ExeCute(SQL)
			GBL_DBNum = GBL_DBNum + 1
			SQL = "Update LeadBBS_SiteInfo Set UploadNum=UploadNum+1"
			Con.ExeCute(SQL)
			UpdateStatisticDataInfo 1,5,1
			GBL_DBNum = GBL_DBNum + 1
		End If
	End if

End Function

Function DeleteFiles(path)

	If DEF_FSOString = "" Then Exit Function
    on error resume next
    Dim fs
    Set fs = Server.CreateObject(DEF_FSOString)
	If err <> 0 Then
		Err.Clear
		Response.Write "<br>服务器不支持FSO，硬盘文件未删除．"
		Exit Function
	End If
    If fs.FileExists(path) Then
      fs.DeleteFile path,True
      DeleteFiles = 1
    Else
      DeleteFiles = 0
    End If
    Set fs = Nothing

End Function%>
