%
'****************************************************************************************
'** Copyright Notice
'**
'** Web Wiz Forums(TM)
'** http://www.webwizforums.com
'**
'** Copyright (C)2001-2008 Web Wiz(TM). All Rights Reserved.
'**
'** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS UNDER LICENSE FROM 'WEB WIZ'.
'**
'** IF YOU DO NOT AGREE TO THE LICENSE AGREEMENT THEN 'WEB WIZ' IS UNWILLING TO LICENSE
'** THE SOFTWARE TO YOU, AND YOU SHOULD DESTROY ALL COPIES YOU HOLD OF 'WEB WIZ' SOFTWARE
'** AND DERIVATIVE WORKS IMMEDIATELY.
'**
'** If you have not received a copy of the license with this work then a copy of the latest
'** license contract can be found at:-
'**
'** http://www.webwizguide.com/license
'**
'** For more information about this software and for licensing information please contact
'** 'Web Wiz' at the address and website below:-
'**
'** Web Wiz, Unit 10E, Dawkins Road Industrial Estate, Poole, Dorset, BH15 4JD, England
'** http://www.webwizguide.com
'**
'** Removal or modification of this copyright notice will violate the license contract.
'**
'****************************************************************************************
'*************************** SOFTWARE AND CODE MODIFICATIONS ****************************
'**
'** MODIFICATION OF THE FREE EDITIONS OF THIS SOFTWARE IS A VIOLATION OF THE LICENSE
'** AGREEMENT AND IS STRICTLY PROHIBITED
'**
'** If you wish to modify any part of this software a license must be purchased
'**
'****************************************************************************************
'Upload global variables
Dim strImageName 'Holds the file name
Dim blnExtensionOK 'Set to false if the extension of the file is not allowed
Dim lngErrorFileSize 'Holds the file size if the file is not saved because it is to large
Dim dblErrorAllotedFileSpace 'Holds the alloted space size error
Dim blnFileExists 'Set to true if the file already exists
Dim blnFileSpaceExceeded 'Set to true if the alloted file space is exceeded
'Intiliase global variables
blnExtensionOK = True
blnFileExists = False
blnFileSpaceExceeded = False
lngErrorFileSize = 0
dblErrorAllotedFileSpace = 0
'******************************************
'*** File Upload Function ****
'******************************************
'Function to upload a file
Private Function fileUpload(ByVal strUploadType)
'Dimension variables
Dim objUpload
Dim strNewFileName
Dim strOriginalFileName
Dim objFSO
Dim objTextStream
Dim strTempFile
Dim strExtension
Dim saryFileUploadTypes
Dim lngMaxFileSize
Dim lngLoopCounter
'Make sure the user has a folder to upload to
createUserFolder(strUploadFilePath)
'First check the user has not gone over their alloted space
'Get used space
dblErrorAllotedFileSpace = folderSize(strUploadFilePath)
'Check to see if the user has gone over the alloted space
If CDbl(dblErrorAllotedFileSpace) > CDbl(intUploadAllocatedSpace) OR blnDemoMode Then
blnFileSpaceExceeded = True
Exit Function
End If
'If we get here which we shouldn't kick user if in demo mode
If blnDemoMode Then Exit Function
'Get the file types we are uploading
If strUploadType = "file" Then
lngMaxFileSize = intUploadMaxFileSize
saryFileUploadTypes = Split(Trim(strUploadFileTypes), ";")
ElseIf strUploadType = "image" Then
lngMaxFileSize = intUploadMaxImageSize
saryFileUploadTypes = Split(Trim(strImageTypes), ";")
End If
'If no file type of extensions set then leave now
If isArray(saryFileUploadTypes) = False Then
blnExtensionOK = False
Exit Function
End If
'******************************************
'*** Upload components ****
'******************************************
'Select which upload component to use
Select Case strUploadComponent
'******************************************
'*** Persits AspUpload component ****
'******************************************
'Persits AspUpload upload component - tested with version 3.0
Case "AspUpload"
'Set error trapping
On Error Resume Next
'Create upload object
Set objUpload = Server.CreateObject("Persits.Upload.1")
'Check to see if an error has occurred
'If an error has occurred write an error to the page
If Err.Number <> 0 Then Call errorMsg("An error has occurred while uploading file/image.
Please check the Persits AspUpload Component is installed on the server.", "create_AspUpload_object", "functions_upload.asp")
'Disable error trapping
On Error goto 0
With objUpload
'make sure files arn't over written
.OverwriteFiles = False
'We need to save the file before we can find out anything about it
'** Save the file to the hard drive as saving to memory is often disabled by the web host **
'Save to temp position to prevent errors at a later stage
.SaveVirtual strUploadOriginalFilePath
'Get the file name
strNewFileName = .Files(1).ExtractFileName
'Filter file name to remove anything that isn't allowed by the filters
strNewFileName = formatFileName(strNewFileName)
'Check the file size is not above the max allowed size, this is done using a function not the compoent to stop an exception error
lngErrorFileSize = fileSize(.Files(1).Size, lngMaxFileSize)
'Loop through all the allowed extensions and see if the file has one
blnExtensionOK = fileExtension(strNewFileName, saryFileUploadTypes)
'Check if file exsists
blnFileExists = .FileExists(Server.MapPath(strUploadFilePath) & "\" & strNewFileName)
'If the file is OK save it to disk
If lngErrorFileSize = 0 AND blnExtensionOK AND blnFileExists = False Then
'Save the file to disk with new file name
'** Copy is used as we have already saved the file, just need to move it to it's correct location **
.Files(1).CopyVirtual strUploadFilePath & "/" & strNewFileName
'As a new copy of the file is saved we need to get rid of the old copy
.Files(1).Delete
'Pass the filename back
fileUpload = strNewFileName
'Else if it is not OK delete the uploaded file
Else
.Files(1).Delete
End If
End With
'Clean up
Set objUpload = Nothing
'******************************************
'*** Dundas Upload component ****
'******************************************
'Dundas upload component free from http://www.dundas.com - tested with version 2.0
Case "Dundas"
'Set error trapping
On Error Resume Next
'Create upload object
Set objUpload = Server.CreateObject("Dundas.Upload")
'Check to see if an error has occurred
'If an error has occurred write an error to the page
If Err.Number <> 0 Then Call errorMsg("An error has occurred while uploading file/image.
Please check the Dundas Upload Component is installed on the server.", "create_Dundas_Upload_object", "functions_upload.asp")
'Disable error trapping
On Error goto 0
With objUpload
'Make sure we are using a virtual directory for script
.UseVirtualDir = True
'Make sure the file names are not unique at this time
.UseUniqueNames = False
'Save the file first to memory
.SaveToMemory()
'Get the file name, the path mehod will be empty as we are saving to memory so use the original file path of the users system to get the name
strNewFileName = .GetFileName(.Files(0).OriginalPath)
'Filter file name to remove anything that isn't allowed by the filters
strNewFileName = formatFileName(strNewFileName)
'Check the file size is not above the max allowed size, this is done using a function not the compoent to stop an exception error
lngErrorFileSize = fileSize(.Files(0).Size, lngMaxFileSize)
'Loop through all the allowed extensions and see if the file has one
blnExtensionOK = fileExtension(strNewFileName, saryFileUploadTypes)
'Check if file exists
blnFileExists = .FileExists(strUploadFilePath & "\" & strNewFileName)
'If the file is OK save it to disk
If lngErrorFileSize = 0 AND blnExtensionOK AND blnFileExists = False Then
'Save the file to disk
.Files(0).SaveAs strUploadFilePath & "/" & strNewFileName
'Pass the filename back
fileUpload = strNewFileName
End If
End With
'Clean up
Set objUpload = Nothing
'******************************************
'*** SoftArtisans FileUp component ****
'******************************************
'SA FileUp upload component - tested with version 4
Case "fileUp"
'Set error trapping
On Error Resume Next
'Create upload object
Set objUpload = Server.CreateObject("SoftArtisans.FileUp")
'Check to see if an error has occurred
'If an error has occurred write an error to the page
If Err.Number <> 0 Then Call errorMsg("An error has occurred while uploading file/image.
Please check the SoftArtisans FileUp Component is installed on the server.", "create_SoftArtisans_FileUp_object", "functions_upload.asp")
'Disable error trapping
On Error goto 0
With objUpload
'Over write files or an exception will occur if it already exists
.OverWriteFiles = True
'Set the upload path
.Path = Server.MapPath(strUploadFilePath)
'Get the file name, the path mehod will be empty as we are saving to memory so use the original file path of the users system to get the name
strNewFileName = Mid(.UserFilename, InstrRev(.UserFilename, "\") + 1)
'Filter file name to remove anything that isn't allowed by the filters
strNewFileName = formatFileName(strNewFileName)
'Check the file size is not above the max allowed size, this is done using a function not the compoent to stop an exception error
lngErrorFileSize = fileSize(.TotalBytes, lngMaxFileSize)
'Loop through all the allowed extensions and see if the file has one
blnExtensionOK = fileExtension(strNewFileName, saryFileUploadTypes)
'Create the file system object
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Check if file exsists
blnFileExists = objFSO.FileExists(Server.MapPath(strUploadFilePath) & "\" & strNewFileName)
'Drop FSO as no longer needed
Set objFSO = Nothing
'If the file is OK save it to disk
If lngErrorFileSize = 0 AND blnExtensionOK AND blnFileExists = False Then
'Save the file to disk
.SaveAs strNewFileName
'Pass the filename back
fileUpload = strNewFileName
End If
End With
'Clean up
Set objUpload = Nothing
'******************************************
'*** AspSmartUpload component ****
'******************************************
'AspSmartUpload upload component free from http://www.aspsmart.com
Case "aspSmart"
'Set error trapping
On Error Resume Next
'Create upload object
Set objUpload = Server.CreateObject("aspSmartUpload.SmartUpload")
'Check to see if an error has occurred
'If an error has occurred write an error to the page
If Err.Number <> 0 Then Call errorMsg("An error has occurred while uploading file/image.
Please check the Asp Smart Upload Component is installed on the server.", "create_AspSmartUpload_object", "functions_upload.asp")
'Disable error trapping
On Error goto 0
With objUpload
'Make sure we are using a virtual directory
.DenyPhysicalPath = True
'Save the file first to memory
.Upload
'Get the file name, the path mehod will be empty as we are saving to memory so use the original file path of the users system to get the name
strNewFileName = .Files(1).Filename
'Filter file name to remove anything that isn't allowed by the filters
strNewFileName = formatFileName(strNewFileName)
'Check the file size is not above the max allowed size
lngErrorFileSize = fileSize(.Files(1).Size, lngMaxFileSize)
'Loop through all the allowed extensions and see if the file has one
blnExtensionOK = fileExtension(strNewFileName, saryFileUploadTypes)
'Create the file system object
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Check if file exsists
blnFileExists = objFSO.FileExists(Server.MapPath(strUploadFilePath) & "\" & strNewFileName)
'Drop FSO as no longer needed
Set objFSO = Nothing
'If the file is OK save it to disk
If lngErrorFileSize = 0 AND blnExtensionOK AND blnFileExists = False Then
'Save the file to disk
.Files(1).SaveAs strUploadFilePath & "/" & strNewFileName
'Pass the filename back
fileUpload = strNewFileName
End If
End With
'Clean up
Set objUpload = Nothing
'******************************************
'*** AspSimpleUpload component ****
'******************************************
'ASPSimpleUpload component
Case "AspSimple"
'Dimension variables
Dim file 'Holds the FSO file object
'Set error trapping
On Error Resume Next
'Create upload object
Set objUpload = Server.CreateObject("ASPSimpleUpload.Upload")
'Check to see if an error has occurred
'If an error has occurred write an error to the page
If Err.Number <> 0 Then Call errorMsg("An error has occurred while uploading file/image.
Please check the ASPSimpleUpload is installed on the server.", "create_AspSimpleUpload_object", "functions_upload.asp")
'Disable error trapping
On Error goto 0
With objUpload
'Get the file name
strOriginalFileName = .ExtractFileName(.Form("file"))
'Save the amended file name
strNewFileName = "TMP" & hexValue(7) & "_" & strOriginalFileName
'Filter file name to remove anything that isn't allowed by the filters
strNewFileName = formatFileName(strNewFileName)
'Save the file to disk first so we can check it
Call .SaveToWeb ("file", strUploadFilePath & "\" & strNewFileName)
'Create the file system object
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Create a file object with the file details
Set file = objFSO.GetFile(Server.MapPath(strUploadFilePath) & "\" & strNewFileName)
'Check the file size is not above the max allowed size, this is done using a function not the compoent to stop an exception error
lngErrorFileSize = fileSize(file.Size, lngMaxFileSize)
'Place the original file name back in the new filename variable
strNewFileName = strOriginalFileName
'Filter file name to remove anything that isn't allowed by the filters
strNewFileName = formatFileName(strNewFileName)
'Loop through all the allowed extensions and see if the file has one
blnExtensionOK = fileExtension(strNewFileName, saryFileUploadTypes)
'Check if file exsists
blnFileExists = objFSO.FileExists(Server.MapPath(strUploadFilePath) & "\" & strNewFileName)
'If the file is OK save it to disk
If lngErrorFileSize = 0 AND blnExtensionOK AND blnFileExists = False Then
'Save the file to disk
Call .SaveToWeb("file", strUploadFilePath & "/" & strNewFileName)
'Pass the filename back
fileUpload = strNewFileName
End If
'Delete the original file
file.Delete
End With
'Clean up
Set file = Nothing
Set objFSO = Nothing
Set objUpload = Nothing
End Select
'******************************************
'*** Security check for MIME change ****
'******************************************
'Read in the uploaded file to make sure that the user is not trying to sneak through a change of content type in an image etc.
'Get the file extension
If InStr(strNewFileName, ".") Then
strExtension = Mid(strNewFileName, InStrRev(strNewFileName, "."), 5)
Else
strExtension = "."
End If
'Don't run if text based file
If strExtension <> ".txt" AND strExtension <> ".text" AND strExtension <> ".xml" AND strExtension <> ".css" AND strExtension <> ".htm" AND strExtension <> ".html" Then
'Create the file system object
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Check to make sure file exsists
If objFSO.FileExists(Server.MapPath(strUploadFilePath) & "\" & strNewFileName) Then
'Create a file object with the file details
Set file = objFSO.GetFile(Server.MapPath(strUploadFilePath) & "\" & strNewFileName)
' Open the file for reading (1) as an ascii file (0)
Set objTextStream = file.OpenAsTextStream(1, 0)
'Read in line by line and check the content type is not altered
Do While Not objTextStream.AtEndOfStream
strTempFile = strTempFile & objTextStream.readline
Loop
'Clean up (done now to prevent a permissions error later)
Set objTextStream = nothing
'Trim and set as lower case
strTempFile = Trim(strTempFile)
'For adobe created files
If InStr(strTempFile, "adobe:ns:meta") Then
If InStr(strTempFile, "") Then strTempFile = Replace(strTempFile, Mid(strTempFile, InStr(strTempFile, "")), "")
End If
'LCase
strTempFile = LCase(strTempFile)
'Remove spaces and tabs
strTempFile = Replace(strTempFile, Chr(9), "", 1, -1, 1) 'Tabs
strTempFile = Replace(strTempFile, " ", "", 1, -1, 1)
%><%
'See if the file is attempting to change the content type
If InStr(strTempFile, "contenttype") Then
blnExtensionOK = False
ElseIf InStr(strTempFile, "content-type") Then
blnExtensionOK = False
ElseIf InStr(strTempFile, "addtype") Then
blnExtensionOK = False
ElseIf InStr(strTempFile, "doctype") Then
blnExtensionOK = False
'If the file type is an image do some futher checking
ElseIf strExtension = ".gif" OR strExtension = ".jpg" OR strExtension = ".png" OR strExtension = ".jpeg" OR strExtension = ".jpe" OR strExtension = ".tiff" OR strExtension = ".bmp" Then
'Loop through the array of disallowed HTML tags
For lngLoopCounter = LBound(saryUnSafeHTMLtags) To UBound(saryUnSafeHTMLtags)
'If the disallowed HTML is found remove it and start over
If Instr(1, strTempFile, saryUnSafeHTMLtags(lngLoopCounter), 1) Then
blnExtensionOK = False
End If
Next
End If
'If the file extension is not OK then delete file
If blnExtensionOK = False Then file.Delete
End If
'Clean up
Set file = Nothing
Set objFSO = Nothing
End If
End Function
'******************************************
'*** Check file size function ****
'******************************************
Function fileSize(ByVal lngFileSize, ByVal lngMaxFileSize)
'If the file size is to large place the present file size in then return the file size
If CLng(lngFileSize / 1024) > lngMaxFileSize Then
fileSize = CLng(lngFileSize / 1024)
'Else set the return value to 0
Else
fileSize = 0
End If
End Function
'******************************************
'*** Check file ext. function ****
'******************************************
Function fileExtension(ByVal strFileName, ByVal saryFileUploadTypes)
'Dimension varibles
Dim intExtensionLoopCounter
'Intilaise return value
fileExtension = False
'Loop through all the allowed extensions and see if the file has one
For intExtensionLoopCounter = 0 To UBound(saryFileUploadTypes)
If LCase(Right(strFileName, Len(saryFileUploadTypes(intExtensionLoopCounter)))) = LCase(saryFileUploadTypes(intExtensionLoopCounter)) Then fileExtension = True
Next
End Function
'******************************************
'*** Format file names ****
'******************************************
'Format file names to strip caharacters that will otherwise be stripped by the filters producing dead links
Private Function formatFileName(ByVal strInputEntry)
'Dimension variable
Dim intLoopCounter 'Holds the loop counter
'Loop through the ASCII characters 0 to 31
For intLoopCounter = 0 to 31
strInputEntry = Replace(strInputEntry, CHR(intLoopCounter), "", 1, -1, 0)
Next
'Windows illegal filename characters
strInputEntry = Replace(strInputEntry, "/", "", 1, -1, 0)
strInputEntry = Replace(strInputEntry, "\", "", 1, -1, 0)
strInputEntry = Replace(strInputEntry, ":", "", 1, -1, 0)
strInputEntry = Replace(strInputEntry, "*", "", 1, -1, 0)
strInputEntry = Replace(strInputEntry, "?", "", 1, -1, 0)
strInputEntry = Replace(strInputEntry, """", "", 1, -1, 0)
strInputEntry = Replace(strInputEntry, "<", "", 1, -1, 0)
strInputEntry = Replace(strInputEntry, ">", "", 1, -1, 0)
strInputEntry = Replace(strInputEntry, "|", "", 1, -1, 0)
strInputEntry = Replace(strInputEntry, "'", "", 1, -1, 1)
strInputEntry = Replace(strInputEntry, ",", "", 1, -1, 1)
'Replace space with underscore
strInputEntry = Replace(strInputEntry, " ", "_", 1, -1, 1)
'Strip others that would otherwise later be stripped by the image/file link filters and prevent the file/image displaying
strInputEntry = Replace(strInputEntry, "[", "", 1, -1, 1)
strInputEntry = Replace(strInputEntry, "]", "", 1, -1, 1)
strInputEntry = Replace(strInputEntry, "(", "", 1, -1, 1)
strInputEntry = Replace(strInputEntry, ")", "", 1, -1, 1)
strInputEntry = Replace(strInputEntry, "{", "", 1, -1, 1)
strInputEntry = Replace(strInputEntry, "}", "", 1, -1, 1)
'Return
formatFileName = strInputEntry
End Function
'**********************************************
'*** Create a folder for uploads ****
'**********************************************
Private Sub createUserFolder(ByVal strFolder)
Dim objFSO
Dim objUserXMLfile
Dim strFolderUserName
Dim lngFolderUserID
'Set error trapping
On Error Resume Next
'Creat an instance of the FSO object
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Check to see if an error has occurred
'If an error has occurred write an error to the page
If Err.Number <> 0 Then Call errorMsg("An error has occurred while uploading file/image.
Please check the File System Object (FSO) is installed on the server.", "create_FSO_object", "functions_upload.asp")
'Disable error trapping
On Error goto 0
'If a folder doesn't exist for this user create one
If NOT objFSO.FolderExists(Server.MapPath(strFolder)) Then
'Get the user ID from the end of the file path
lngFolderUserID = CLng(Right(strFolder, (Len(strFolder) - Instr(strFolder, "/"))))
'If the user dosen't have a folder create them one
'Make sure the folder doesn't already exsist (we already do this above, but some people still get an error, so we do it again)
If Not objFSO.FolderExists(Server.MapPath(strFolder)) Then objFSO.CreateFolder(Server.MapPath(strFolder))
'Read in the username of this user from the database as it is needed for the XML file containing data on the folder
strSQL = "SELECT " & strDbTable & "Author.Username " & _
"FROM " & strDbTable & "Author" & strDBNoLock & " " & _
"WHERE " & strDbTable & "Author.Author_ID = " & lngFolderUserID & ";"
'Query the database
rsCommon.Open strSQL, adoCon
If NOT rsCommon.EOF Then strFolderUserName = rsCommon("Username")
'Close RS
rsCommon.Close
'Create an XML file with user details; TODO, add feature to be able to add notes
Set objUserXMLfile = objFSO.CreateTextFile(Server.MapPath(strFolder) & "\folder_info.xml", True)
'Add Contents
objUserXMLfile.WriteLine("" & _
vbCrLf & "" & _
vbCrLf & " " & internationalDateTime(Now()) & "" & _
vbCrLf & " " & _
vbCrLf & " " & lngFolderUserID & "" & _
vbCrLf & " " & strFolderUserName & "" & _
vbCrLf & " " & _
vbCrLf & "")
'Close
objUserXMLfile.Close
Set objUserXMLfile = Nothing
End If
'Release the FSO object
Set objFSO = Nothing
End Sub
'**********************************************
'*** Check if user has upload folder ****
'**********************************************
Private Function userUploadFolder(ByVal strFolder)
Dim objFSO
'Set error trapping
On Error Resume Next
'Creat an instance of the FSO object
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Check to see if an error has occurred
'If an error has occurred write an error to the page
If Err.Number <> 0 Then Call errorMsg("An error has occurred while uploading file/image.
Please check the File System Object (FSO) is installed on the server.", "create_FSO_object", "functions_upload.asp")
'Disable error trapping
On Error goto 0
'If a folder doesn't exist for this user create one
If objFSO.FolderExists(Server.MapPath(strFolder)) Then
userUploadFolder = True
Else
userUploadFolder = False
End If
'Release the FSO object
Set objFSO = Nothing
End Function
'**********************************************
'*** Check allocated space ****
'**********************************************
Private Function folderSize(ByVal strFolder)
Dim objFSO
'Set error trapping
On Error Resume Next
'Creat an instance of the FSO object
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Check to see if an error has occurred
'If an error has occurred write an error to the page
If Err.Number <> 0 Then Call errorMsg("An error has occurred while uploading file/image.
Please check the File System Object (FSO) is installed on the server.", "create_FSO_object", "functions_upload.asp")
'Disable error trapping
On Error goto 0
'Now lets check the size of the folder (it's returned in bytes so converet to MB with 2 decimal places)
folderSize = FormatNumber(CDbl(objFSO.GetFolder(Server.MapPath(strFolder)).Size / 1024 / 1024), 2)
'Release the FSO object
Set objFSO = Nothing
End Function
%>