<%@ Language=VBScript %>
<!--#include virtual="/inc-freehost-function/FreeHostFunctionMain.asp"-->
<%

      Dim FormData,FormSize
      FormSize=Request.TotalBytes
      FormData=Request.BinaryRead(FormSize)
      Set Form = GetUpload(FormData)

      if Form("Filedata1_FileName")="" then
      Response.Redirect "../e.asp?e=您上传了一个空文件！或者，必须接顺序上传！"
      elseif Form("Filedata1_Length")>2048000 then
      Response.Redirect "../e.asp?e=上传大小超出了2M上限了！"
      end if

gotodir=Form("gotodir_Value")
If session("agentusername")="" Then
	ef=True
	ec="授权非法，有可能是超时造成的。"
elseif not Application("canagnimg")=true then
	ef=True
	ec="管理员设置不允许上传图片。"
elseIf RegExpTest("[^A-Za-z0-9]",replace(gotodir,"\","")) Then
	ef=True
	ec="目录有非法字符！"
end if


if not ef then
for i=1 to 5

tempstr=lcase(Form("Filedata"&i&"_FileName"))
if tempstr<>"" then
tempstrend=right(tempstr,4)
tempstrend2=right(tempstr,3)
if tempstrend<>".jpg" And tempstrend<>".png" and  tempstrend<>".gif" and tempstrend<>".swf" and  tempstrend<>".htm" and  tempstrend<>"html" and  tempstrend<>".css" and  tempstrend<>".doc" and  tempstrend2<>".js" then
	ef=True
	ec="文件类型只能是.jpg,.gif,.png,.swf,.htm,.css,.doc,.js,.html格式！"
ElseIf  Instr(tempstr,"..")>0 Or Instr(tempstr,"\")>0 Or Instr(tempstr,"/")>0 Then
	ef=True
	ec="文件名不能有特殊字符！"&tempstr
end If
if not ef then
tempstrstart=left(tempstr,len(tempstr)-4)
If RegExpTest("[^A-Za-z0-9--_]",tempstrstart) Then
	ef=True
	ec="文件名有非法字符，必须是英文与数字！"&tempstrstart
end If
If tempstrend2=".js" Then
tempstrstart=left(tempstr,len(tempstr)-3)
If RegExpTest("[^A-Za-z0-9--_]",tempstrstart) Then
	ef=True
	ec="文件名有非法字符，必须是英文与数字！"&tempstrstart
end If
End If
end if
end if
next
end if

if not ef then

memuser=session("agentusername")
root=Application("agnimgpath")&"\"&memuser


'Response.Write Form("gotodir_Value")
curdir=root&gotodir

    Set fso = CreateObject("Scripting.FileSystemObject")
    if not fso.FolderExists(curdir) then Response.Redirect "../e.asp?ec=此目录不存在或被删除了！"
set fso=nothing

if Form("Filedata1_Length")-1>2 then
curdir=root&gotodir&"\"&Form("Filedata1_FileName") 
set stmA=CreateObject("Adodb.Stream")
with stmA
.Mode=3
.Type=1
.Open
.Write formdata
.Position=Form("Filedata1_DataStart")-1
set stmB=CreateObject("Adodb.Stream") 
stmB.Mode=3
stmB.Type=1
stmB.Open
.CopyTo stmB,Form("Filedata1_Length")-1
stmB.SaveToFile curdir,2
'Response.Write curdir
.Close
end with
set stmA=nothing
stmB.Close
set stmB=nothing
end if

if Form("Filedata2_Length")-1>2 then
curdir=root&gotodir&"\"&Form("Filedata2_FileName")
set stmA=CreateObject("Adodb.Stream")
with stmA
.Mode=3
.Type=1
.Open
.Write formdata
.Position=Form("Filedata2_DataStart")-1
set stmB=CreateObject("Adodb.Stream") 
stmB.Mode=3
stmB.Type=1
stmB.Open
.CopyTo stmB,Form("Filedata2_Length")-1
stmB.SaveToFile curdir,2
'Response.Write curdir
.Close
end with
set stmA=nothing
stmB.Close
set stmB=nothing
end if
if Form("Filedata3_Length")-1>2 then
curdir=root&gotodir&"\"&Form("Filedata3_FileName")
set stmA=CreateObject("Adodb.Stream")
with stmA
.Mode=3
.Type=1
.Open
.Write formdata
.Position=Form("Filedata3_DataStart")-1
set stmB=CreateObject("Adodb.Stream") 
stmB.Mode=3
stmB.Type=1
stmB.Open
.CopyTo stmB,Form("Filedata3_Length")-1
stmB.SaveToFile curdir,2
'Response.Write curdir
.Close
end with
set stmA=nothing
stmB.Close
set stmB=nothing
end if


if Form("Filedata4_Length")-1>2 then
curdir=root&gotodir&"\"&Form("Filedata4_FileName")
set stmA=CreateObject("Adodb.Stream")
with stmA
.Mode=3
.Type=1
.Open
.Write formdata
.Position=Form("Filedata4_DataStart")-1
set stmB=CreateObject("Adodb.Stream") 
stmB.Mode=3
stmB.Type=1
stmB.Open
.CopyTo stmB,Form("Filedata4_Length")-1
stmB.SaveToFile curdir,2
'Response.Write curdir
.Close
end with
set stmA=nothing
stmB.Close
set stmB=nothing
end if


if Form("Filedata5_Length")-1>2 then
curdir=root&gotodir&"\"&Form("Filedata5_FileName")
set stmA=CreateObject("Adodb.Stream")
with stmA
.Mode=3
.Type=1
.Open
.Write formdata
.Position=Form("Filedata5_DataStart")-1
set stmB=CreateObject("Adodb.Stream") 
stmB.Mode=3
stmB.Type=1
stmB.Open
.CopyTo stmB,Form("Filedata5_Length")-1
stmB.SaveToFile curdir,2
'Response.Write curdir
.Close
end with
set stmA=nothing
stmB.Close
set stmB=nothing
end if
Response.Redirect Request.ServerVariables("HTTP_REFERER")

end if
if ef then
Response.Redirect "../e.asp?e="&ec
end if

Function GetUpload(FormData)
    Dim DataStart,DivStr,DivLen,DataSize,FormFieldData
    '分隔标志串(+CRLF)
    DivStr = LeftB(FormData,InStrB(FormData,str2bin(VbCrLf)) + 1)
    '分隔标志串长度
    DivLen = LenB(DivStr)
    PosOpenBoundary = InStrB(FormData,DivStr)
    PosCloseBoundary = InStrB(PosOpenBoundary + 1,FormData,DivStr)
	Set Form = CreateObject("Scripting.Dictionary")
    While PosOpenBoundary > 0 And PosCloseBoundary > 0
       'name起始位置(name="xxxxx")，加6是因为[name="]长度为6
       FieldNameStart = InStrB(PosOpenBoundary,FormData,str2bin("name=")) + 6
       FieldNameSize = InStrB(FieldNameStart,FormData,ChrB(34)) - FieldNameStart '(")的ASC值=34
       FormFieldName = bin2str(MidB(FormData,FieldNameStart,FieldNameSize))
       
       'filename起始位置(filename="xxxxx")
       FieldFileNameStart = InStrB(PosOpenBoundary,FormData,str2bin("filename=")) + 10
       If FieldFileNameStart < PosCloseBoundary And FieldFileNameStart > PosopenBoundary Then
          FieldFileNameSize = InStrB(FieldFileNameStart,FormData,ChrB(34)) - FieldFileNameStart '(")的ASC值=34
          FormFileName = bin2str(MidB(FormData,FieldFileNameStart,FieldFileNameSize))
       Else
          FormFileName = ""
       End If
       
       'Content-Type起始位置(Content-Type: xxxxx)
       FieldFileCTStart = InStrB(PosOpenBoundary,FormData,str2bin("Content-Type:")) + 14
       If FieldFileCTStart < PosCloseBoundary  And FieldFileCTStart > PosOpenBoundary Then
          FieldFileCTSize = InStrB(FieldFileCTStart,FormData,str2bin(VbCrLf & VbCrLf)) - FieldFileCTStart
          FormFileCT = bin2str(MidB(FormData,FieldFileCTStart,FieldFileCTSize))
       Else
          FormFileCT = ""
       End If
       
       '数据起始位置：2个CRLF开始
       DataStart = InStrB(PosOpenBoundary,FormData,str2bin(VbCrLf & VbCrLf)) + 4
       If FormFileName <> "" Then
          '数据长度，减1是因为数据文件的存取字节数问题(可能是AppendChunk方法的问题)：
          '由于字节数为奇数的图象存到数据库时会去掉最后一个字符导致图象不能正确显示,
          '字节数为偶数的数据文件就不会出现这个问题，因此必须保持字节数为偶数。
          DataSize = InStrB(DataStart,FormData,DivStr) - DataStart - 1
          FormFieldData = MidB(FormData,DataStart,DataSize)
       Else
          '数据长度，减2是因为分隔标志串前有一个CRLF
          DataSize = InStrB(DataStart,FormData,DivStr) - DataStart - 2
          FormFieldData = bin2str(MidB(FormData,DataStart,DataSize))
       End If

  		Form.add FormFieldName&"_FilePath",FormFileName
		Form.add FormFieldName&"_FileName",GetFileName(FormFileName)
		Form.add FormFieldName&"_DataStart",DataStart
		Form.add FormFieldName&"_ContentType",FormFileCT
		Form.add FormFieldName&"_Length",LenB(FormFieldData)
		Form.add FormFieldName&"_Value",FormFieldData
  
       
       PosOpenBoundary = PosCloseBoundary
       PosCloseBoundary = InStrB(PosOpenBoundary + 1,FormData,DivStr)
    Wend
    Set GetUpload = Form
End Function

'把二进制字符串转换成普通字符串函数
Function bin2str(binstr)
   Dim varlen,clow,ccc,skipflag
   '中文字符Skip标志
   skipflag=0
   ccc = ""
   If Not IsNull(binstr) Then
      varlen=LenB(binstr)
      For i=1 To varlen
          If skipflag=0 Then
             clow = MidB(binstr,i,1)
             '判断是否中文的字符
             If AscB(clow) > 127 Then
                'AscW会把二进制的中文双字节字符高位和低位反转，所以要先把中文的高低位反转
                ccc =ccc & chrw(AscW(MidB(binstr,i+1,1) & clow))
                skipflag=1
             Else
                ccc = ccc & chrw(AscB(clow))
             End If
          Else
             skipflag=0
          End If
      Next
   End If
   bin2str = ccc
End Function 


'把普通字符串转成二进制字符串函数
Function str2bin(varstr)
   str2bin=""
   For i=1 To Len(varstr)
       varchar=mid(varstr,i,1)
       varasc = Asc(varchar)
       ' asc对中文字符求出来的值可能为负数，
       ' 加上65536就可求出它的无符号数值
       ' -1在机器内是用补码表示的0xffff,
       ' 其无符号值为65535,65535=-1+65536
       ' 其他负数依次类推。
       If varasc<0 Then
          varasc = varasc + 65535
       End If
       '对中文的处理：把双字节低位和高位分开
       If varasc>255 Then
          varlow = Left(Hex(Asc(varchar)),2)
          varhigh = right(Hex(Asc(varchar)),2)
          str2bin = str2bin & chrB("&H" & varlow) & chrB("&H" & varhigh)
       Else
          str2bin = str2bin & chrB(AscB(varchar))
       End If
   Next
End Function 

'取得文件名(去掉Path)
Function GetFileName(FullPath)
   If FullPath <> "" Then
      FullPath = StrReverse(FullPath)
If InStr(FullPath,"\") Then FullPath = Left(FullPath, InStr(1, FullPath, "\") - 1)'IE8
      GetFileName = StrReverse(FullPath)
   Else
      GetFileName = ""
   End If
End Function
%>