' PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
' Copyright (c) 2007 Symantec Corporation. All rights reserved.

Option Explicit

Dim oArgs
Dim DefaultServerName, SymantecServerName
Dim objWMIService, objItem, colItems
Dim VerMaj, VerMin, VerTemp, Ver2K3, IIS7
Dim strSEPMBinDir
Dim fso, objFolder
Dim AnonymousUsername

'
' The following are for debug only.  Hand Edit and change to bLogToFile = True
' to get log file output to: c:\IISConfig.log
' Everything normally output to stdout will also go to the log file.
'
Dim bLogToFile
bLogToFile = FALSE

'
' The follow script error codes can be returned
'
Dim Error_Success
Dim ERROR_Bad_Arguments
Dim ERROR_Unsupported_OS
Dim ERROR_Missing_SepmBinFolder
Dim ERROR_Bad_PhpSessionFolder
Dim ERROR_Bad_PhpIniFile
Dim ERROR_ExecutingCacls
Dim ERROR_UnableToOpenW3SVCObject
Dim ERROR_CreateWebServer
Dim ERROR_GetAnonymousUsername
Dim ERROR_SettingHomeDirectory
Dim ERROR_SettingPort
Dim ERROR_CreateVirtualDirectory
Dim ERROR_SaveConfiguration
Dim ERROR_SetProperty
Dim ERROR_CreateMimeMapList
Dim ERROR_SettingHTTPErrorPages

Error_Success                 = 0
ERROR_Bad_Arguments           = 1
ERROR_Unsupported_OS          = 2
ERROR_Missing_SepmBinFolder   = 3
ERROR_Bad_PhpSessionFolder    = 4
ERROR_Bad_PhpIniFile          = 5
ERROR_ExecutingCacls          = 6
ERROR_UnableToOpenW3SVCObject = 7
ERROR_CreateWebServer         = 8
ERROR_GetAnonymousUsername    = 9
ERROR_SettingHomeDirectory    = 10
ERROR_SettingPort             = 11
ERROR_CreateVirtualDirectory  = 12
ERROR_SaveConfiguration       = 13
ERROR_SetProperty             = 14
ERROR_CreateMimeMapList       = 15
ERROR_SettingHTTPErrorPages   = 16

DefaultServerName = "Default Web Site"
SymantecServerName = "Symantec Web Server"

Set oArgs = WScript.Arguments
If oArgs.Count < 1 Then
	EndScript ERROR_Bad_Arguments, "Invalid cmdline arguments."
End If

' WMI Connection to the object in the CIM namespace
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

' WMI Query to the Win32_OperatingSystem
Set colItems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")

' Should only be one...
For Each objItem in colItems

	VerMaj = Left( objItem.Version, InStr( objItem.Version, ".") - 1 )
	VerTemp = Right( objItem.Version, Len( objItem.Version ) - InStr( objItem.Version, ".") )
	VerMin = Left( VerTemp, InStr( VerTemp, ".") - 1 )
	
	Dump("VerMaj: " & VerMaj)
	Dump("VerMin: " & VerMin)
	
	If VerMaj = "6" Then
		IIS7 = True
		Ver2K3 = False
	Else 
		IIS7 = False
		If VerMaj = "5" and VerMin = "2" Then
			Ver2K3 = True
		Else
			Ver2K3 = False
		End If
	End If
	Dump("Ver2K3: " & Ver2K3)
Next

Dump("VerMaj: " & LCase(oArgs(0)))

Select Case LCase(oArgs(0))
	Case "-install":
		If oArgs.Count <> 10 Then
		    EndScript ERROR_Bad_Arguments, "Usage: cscript IISConfig.vbs -install <UseDefaultWebSite> <InetPubDir> <SepmBinDir> <PHPFile> <CaclsApplication> <PHPIniFile> <PHPSessionFolder> <WebsiteTcpPort> <ForceStopDefaultWebsite>"
		End If

		If Ver2K3 = True Then
			strSEPMBinDir = oArgs(3)
		Else
			Set fso=CreateObject("Scripting.FileSystemObject")
			If fso.FolderExists(oArgs(3)) Then
				Set objFolder = fso.GetFolder(oArgs(3))
				strSEPMBinDir = objFolder.ShortPath & "\"
			Else
			    EndScript ERROR_Missing_SepmBinFolder, "SEPM Bin dir does not exist."
			End If
		End If

		Install oArgs(1), oArgs(2), strSEPMBinDir, oArgs(4), oArgs(5), oArgs(6), oArgs(7), oArgs(8), oArgs(9)

		If Ver2K3 = True or IIS7 = True Then
			If IIS7 = True Then
				Dump("Config IIS7.")
			End If
			SetMimeMap oArgs(1)
			ConfigWin2003()
		End If
		EndScript ERROR_Success, "Symantec Web Site install sucess!"
	

	Case "-migrate":
		If oArgs.Count <> 8 Then
			EndScript ERROR_Bad_Arguments, "Usage: cscript IISConfig.vbs -migrate <UseDefaultWebSite> <InetPubDir> <SepmBinDir> <PHPFile> <CaclsApplication> <PHPIniFile> <PHPSessionFolder>"
		End If

		If Ver2K3 = True Then
			strSEPMBinDir = oArgs(3)
		Else
			Set fso=CreateObject("Scripting.FileSystemObject")
			If fso.FolderExists(oArgs(3)) Then
				Set objFolder = fso.GetFolder(oArgs(3))
				strSEPMBinDir = objFolder.ShortPath & "\"
			Else
				EndScript ERROR_Missing_SepmBinFolder, "SEPM Bin dir does not exist."
			End If
		End If

		Migrate oArgs(1), oArgs(2), strSEPMBinDir, oArgs(4), oArgs(5), oArgs(6), oArgs(7)

		If Ver2K3 = True or IIS7 = True Then
			If IIS7 = True Then
				Dump("Config IIS7.")
			End If
			SetMimeMap oArgs(1)
			ConfigWin2003()
		End If
		EndScript ERROR_Success, "Symantec Web Site migration sucess!"

	Case "-uninstall":
		Uninstall ""
		EndScript ERROR_Success, "All Gone!"
		
	Case "-postinstall":
	    Dump("Hisd")
		If (oArgs.Count < 7) and (oArgs.Count > 8) Then
			EndScript ERROR_Bad_Arguments, "Usage: cscript IISConfig.vbs -postinstall <UseDefaultWebSite> <InetPubDir> <CaclsApplication> <PHPSessionFolder> <AccountName> <AccountPassword> <(OptionalDoubleQuoteIndicies>"
		End If

	If oArgs.Count = 8 Then	
	        PostInstallOrMigrate oArgs(1), oArgs(2), oArgs(3), oArgs(4), oArgs(5), oArgs(6), oArgs(7)
	Else
	        PostInstallOrMigrate oArgs(1), oArgs(2), oArgs(3), oArgs(4), oArgs(5), oArgs(6), ""
	End If

        EndScript ERROR_Success, "Reconfigure for Windows Authentication completed"

	Case Else:
		EndScript ERROR_Bad_Arguments, "Usage: cscript IISConfig.vbs -install/-migrate <Coop> <InetPubDir> <SepmBinDir> <PHPFile> <PHPIniFile> <PHPSessionFolder>  <WebsiteTcpPort>|-uninstall"
End Select


''''''''''''''''''''''''''
' Install Function
''''''''''''''''''''''''''
Function Install(DefaultWebSite, InetPubDir, SepmBinDir, PHPFile, CaclsApp, PHPIniFile, PHPSessionFolder, WebsiteTcpPort, ForceStopDefaultWebsite)
	On Error Resume Next

	' First, clean up any leftover stuff.
	Uninstall DefaultWebSite

	Dim oServer
	Dim WebServer
	Dim ServerNum, ServerPort, UsingPort
	Dim NewWebServer, NewBindings, Index, SiteObj, bDone
	Dim RootDir, vDir
	Dim SymantecBindingString

	' start W3srv since Uninstall will stop it
	Dim WshShell, oExec

	Set WshShell = CreateObject("WScript.Shell")

	Set oExec = WshShell.Exec("net start w3svc")

	Do While oExec.Status = 0
		WScript.Sleep 100
	Loop

	Dump("Net Start W3svc status: " & oExec.Status)
	
	SymantecBindingString = ":" & WebsiteTcpPort & ":"
	
	Dump("DefaultWebSite: " & DefaultWebSite)
	Dump("SymantecBindingString: " & SymantecBindingString)

	
	Set oServer = GetObject("IIS://LocalHost/W3SVC")
	If Err <> 0 Then
		EndScript ERROR_UnableToOpenW3SVCObject, "Unable to open IIS://LocalHost/W3SVC. " & GetErrorDescription()
	End If

	If DefaultWebSite <> "1" Then
	    '
	    ' If running on XP or Win2K Workstation the SEPM install would have set 
	    ' ForceStopDefaultWebsite to 1 to tell us that we need to stop the Default Website
	    ' before we can configure the custom website
	    '
	    If ForceStopDefaultWebsite <> "0" Then
	        StopDefaultWebSite
	    End If
	    
		Index = 2
		bDone = False
		While (Not bDone)
			Err.Clear
			Set SiteObj = GetObject("IIS://LocalHost/W3SVC/" & Index)
			If (Err.Number = 0) Then
				' Index already been used by a web server
				Index = Index + 1
			Else
				Err.Clear
				Set NewWebServer = oServer.Create("IIsWebServer", Index)
				NewWebServer.SetInfo
				If (Err.Number <> 0) Then
					' If call to Create failed then try the next number
					Index = Index + 1
				Else
					Err.Clear
					Set SiteObj = GetObject("IIS://LocalHost/W3SVC/" & Index)
					If (Err.Number = 0) Then
						bDone = True
					Else
						Index = Index + 1
					End If
				End If
			End If
	
			If (Index > 1000) Then
			    EndScript ERROR_CreateWebServer, "Unable to create new web server.  Server number is " & Index & "."
			End If
		Wend
	
		NewBindings = Array(0)
		NewBindings(0) = SymantecBindingString

		NewWebServer.ServerBindings = NewBindings
		NewWebServer.ServerComment = SymantecServerName
		NewWebServer.SetInfo
	
		' Now create the root directory object.
		Set RootDir = NewWebServer.Create("IIsWebVirtualDir", "ROOT")
		RootDir.Path = InetPubDir
		RootDir.AccessRead = true
		Err.Clear
		RootDir.SetInfo
		RootDir.AppCreate (True)
		If (Err.Number <> 0) Then
		    EndScript ERROR_SettingHomeDirectory, "Error setting home directory. " & GetErrorDescription()
		End If
	Else
	
		Set NewWebServer = GetObject("IIS://LocalHost/W3SVC/1")
		Set RootDir = GetObject("IIS://LocalHost/W3SVC/1/ROOT")
		RootDir.AccessRead = true
		Err.Clear
		RootDir.SetInfo
	End If
	
	'Create the virtual directory secars
	Set vDir = RootDir.Create("IIsWebVirtualDir", "secars")
	If (Err <> 0) Then
	    EndScript ERROR_CreateVirtualDirectory, "Unable to create " & RootDir.ADsPath & "/secars. " & GetErrorDescription()
	Else
		'Set the new virtual directory path
		vDir.AccessRead = true
		vDir.Path = InetPubDir & "secars"
		Err.Clear

		vDir.AppCreate2 0
		vDir.AppEnable
		vDir.AppFriendlyName = "secars"
		If Ver2K3 = True Then
			vDir.ScriptMaps = "*,""" & SepmBinDir & "secars.dll" & """,1,GET,POST"
		Else
			vDir.ScriptMaps = "*," & SepmBinDir & "secars.dll" & ",1,GET,POST"
		End If
		vDir.UploadReadAheadSize = 1048576
		vDir.Put "AccessScript", true
		vDir.Put "AccessExecute", true
		vDir.Put "DontLog", 1

		'Save the changes
		vDir.SetInfo
		If (Err.Number <> 0) Then
		    EndScript ERROR_SaveConfiguration, "Unable to save configuration for " & RootDir.ADsPath & "/secars. " & GetErrorDescription()
		End If
	End If
	
	'Create the virtual directory secreg
	Set vDir = RootDir.Create("IIsWebVirtualDir", "secreg")
	If (Err <> 0) Then
	    EndScript ERROR_CreateVirtualDirectory, "Unable to create " & RootDir.ADsPath & "/secreg. " & GetErrorDescription()
	Else
		'Set the new virtual directory path
		vDir.AccessRead = true
		vDir.Path = InetPubDir & "secreg"
		Err.Clear

		vDir.AppCreate2 0
		vDir.AppEnable
		vDir.AppFriendlyName = "secreg"
		If Ver2K3 = True Then
			vDir.ScriptMaps = "*,""" & SepmBinDir & "secreg.dll" & """,1,GET,POST"
		Else
			vDir.ScriptMaps = "*," & SepmBinDir & "secreg.dll" & ",1,GET,POST"
		End If
		vDir.UploadReadAheadSize = 1048576
		vDir.Put "AccessScript", true
		vDir.Put "AccessExecute", true
		vDir.Put "DontLog", 1

		'Save the changes
		vDir.SetInfo
		If (Err.Number <> 0) Then
		    EndScript ERROR_SaveConfiguration, "Unable to save configuration for " & RootDir.ADsPath & "/secreg. " & GetErrorDescription()
		End If
	End If

	'Create the virtual directory Reporting
	Set vDir = RootDir.Create("IIsWebVirtualDir", "Reporting")
	If (Err <> 0) Then
	    EndScript ERROR_CreateVirtualDirectory, "Unable to create " & RootDir.ADsPath & "/Reporting. " & GetErrorDescription()
	Else
		'Set the new virtual directory path
		vDir.AccessRead = true
		vDir.Path = InetPubDir & "Reporting"
		Err.Clear

		vDir.AppCreate2 0
		vDir.AppEnable
		vDir.AppFriendlyName = "Reporting"
		'
		' For IIS7 setting the Script Map here does not fully work.
		' because we still get ISAPI/CGI restriction type errors so
		' the responsibility for this is pushed into the EnableIISExtension
		' custom action.
		'
		If ( IIS7 <> True ) Then
			vDir.ScriptMaps = ".php,""" & PHPFile & """,1"
		End If
		vDir.DefaultDoc = "index.php"
		'vDir.UploadReadAheadSize = 1048576
		vDir.Put "AccessScript", true
		vDir.Put "AccessExecute", true
		vDir.Put "DontLog", 1

		'Save the changes
		vDir.SetInfo
		If (Err.Number <> 0) Then
		    EndScript ERROR_SaveConfiguration, "Unable to save configuration for " & RootDir.ADsPath & "/Reporting. " & GetErrorDescription()
		End If
		
		SetErrorPages vDir, InetPubDir
	End If

	'Create the virtual directory ClientPackages
	Set vDir = RootDir.Create("IIsWebVirtualDir", "ClientPackages")
	If (Err <> 0) Then
	    EndScript ERROR_CreateVirtualDirectory, "Unable to create " & RootDir.ADsPath & "/ClientPackages. " & GetErrorDescription()
	Else
		'Set the new virtual directory path
		vDir.AccessRead = true
		vDir.Path = InetPubDir & "ClientPackages"
		Err.Clear

		vDir.AppCreate2 0
		vDir.AppEnable
		vDir.AppFriendlyName = "ClientPackages"
		vDir.ScriptMaps = ""
		vDir.Put "DontLog", 1

		'Save the changes
		vDir.SetInfo
		If (Err.Number <> 0) Then
		    EndScript ERROR_SaveConfiguration, "Unable to save configuration for " & RootDir.ADsPath & "/ClientPackages. " & GetErrorDescription()
		End If
	End If

	'Create the virtual directory content
	Set vDir = RootDir.Create("IIsWebVirtualDir", "content")
	If (Err <> 0) Then
	    EndScript ERROR_CreateVirtualDirectory, "Unable to create " & RootDir.ADsPath & "/content. " & GetErrorDescription()
	Else
		'Set the new virtual directory path
		vDir.AccessRead = true
		vDir.Path = InetPubDir & "content"
		Err.Clear

		vDir.AppCreate2 0
		vDir.AppEnable
		vDir.AppFriendlyName = "content"
		vDir.ScriptMaps = ""
		vDir.Put "DontLog", 1

		'Save the changes
		vDir.SetInfo
		If (Err.Number <> 0) Then
		    EndScript ERROR_SaveConfiguration, "Unable to save configuration for " & RootDir.ADsPath & "/content. " & GetErrorDescription()
		End If

	End If

	NewWebServer.Start
	
	'Configure Php
    AnonymousUsername = GetSepmWebsiteAnonymousUsername(DefaultWebSite = "1")
    ConfigurePhp phpIniFile, phpSessionFolder
    AssignFolderACLs phpSessionFolder, AnonymousUsername, CaclsApp
    AssignFolderACLs InetPubDir+"Reporting", AnonymousUsername, CaclsApp
	
	Dump("IIS://LocalHost/W3SVC/" & Index & " created.")
End Function


''''''''''''''''''''''''''
' ConfigWin2003 Function
''''''''''''''''''''''''''
Function ConfigWin2003()
	On Error Resume Next

	Dim IIsObject

	Set IIsObject = GetObject("IIS://LocalHost/W3SVC/AppPools/DefaultAppPool")
	If (Err.Number <> 0) Then
	    EndScript ERROR_UnableToOpenW3SVCObject, "Error Trying To Get the Object: IIS://localhost/W3SVC/AppPools/DefaultAppPool. " & GetErrorDescription()
	End If

	IIsObject.Put "PeriodicRestartTime", 0
	IIsObject.Setinfo

	If (Err.Number <> 0) Then
	    EndScript ERROR_SetProperty, "Error setting the property: PeriodicRestartTime. " & GetErrorDescription()
	End If

	Dump("Recycle work processes for DefaultAppPool disabled.")
	
	IIsObject.Put "IdleTimeout", 0
		IIsObject.Setinfo
	
		If (Err.Number <> 0) Then
		    EndScript ERROR_SetProperty, "Error setting the property: IdleTimeout. " & GetErrorDescription()
		End If
	
	Dump("Idle timeout for DefaultAppPool disabled.")
	
	IIsObject.Put "AppPoolQueueLength", 65535
		IIsObject.Setinfo
	
		If (Err.Number <> 0) Then
		    EndScript ERROR_SetProperty, "Error setting the property: AppPoolQueueLength.  " & GetErrorDescription()
		End If
	
	Dump("Request queue limit for DefaultAppPool disabled.")

	ConfigWin2003 = 0 ' Success
End Function


''''''''''''''''''''''''''
' SetMimeMap Function
''''''''''''''''''''''''''
Function SetMimeMap(DefaultWebSite)
	On Error Resume Next

	Dim oServer
	Dim WebServer
	Dim ServerNum
	Dim RootDir

	Dim MimeMapList()
	Dim MimeEntry

	' Create a new MimeMapList of Mime Entries
	ReDim MimeMapList(18)
	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "Batch File"
	MimeEntry.Extension = ".bat"
	Set MimeMapList(0) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "Configuration File"
	MimeEntry.Extension = ".cfg"
	Set MimeMapList(1) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "DAT File"
	MimeEntry.Extension = ".dat"
	Set MimeMapList(2) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "application/x-SymantecEndpointProtectionManager"
	MimeEntry.Extension = ".dax"
	Set MimeMapList(3) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "DB File"
	MimeEntry.Extension = ".db"
	Set MimeMapList(4) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "GRD File"
	MimeEntry.Extension = ".grd"
	Set MimeMapList(5) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "HST File"
	MimeEntry.Extension = ".hst"
	Set MimeMapList(6) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "Configuration Settings"
	MimeEntry.Extension = ".ini"
	Set MimeMapList(7) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "KC File"
	MimeEntry.Extension = ".kc"
	Set MimeMapList(8) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "LOC File"
	MimeEntry.Extension = ".loc"
	Set MimeMapList(9) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "MANIFEST File"
	MimeEntry.Extension = ".manifest"
	Set MimeMapList(10) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "PLG File"
	MimeEntry.Extension = ".plg"
	Set MimeMapList(11) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "Policy File"
	MimeEntry.Extension = ".policy"
	Set MimeMapList(12) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "REG File"
	MimeEntry.Extension = ".reg"
	Set MimeMapList(13) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "RUL File"
	MimeEntry.Extension = ".rul"
	Set MimeMapList(14) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "SIG File"
	MimeEntry.Extension = ".sig"
	Set MimeMapList(15) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "SPM File"
	MimeEntry.Extension = ".spm"
	Set MimeMapList(16) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "SYS File"
	MimeEntry.Extension = ".sys"
	Set MimeMapList(17) = MimeEntry

	Set MimeEntry = CreateObject("MimeMap")
	MimeEntry.MimeType = "WLT File"
	MimeEntry.Extension = ".wlt"
	Set MimeMapList(18) = MimeEntry

	If (Err.Number <> 0) Then
	    EndScript ERROR_CreateMimeMapList, "Error createint the Mime Map List. " & GetErrorDescription()
	End If

	If DefaultWebSite = "1" Then
		Set RootDir = GetObject("IIS://LocalHost/W3SVC/1/ROOT")
		If (Err.Number <> 0) Then
		    EndScript ERROR_UnableToOpenW3SVCObject, "Error trying to get the Object: IIS://LocalHost/W3SVC/1/ROOT. " & GetErrorDescription()
		End If

		RootDir.MimeMap = MimeMapList
		RootDir.Setinfo

		If (Err.Number <> 0) Then
		    EndScript ERROR_SetProperty, "Error setting the object's ""MimeMap"" property to the new mimemap list. " & GetErrorDescription()
		End If

		Dump("Set MimeType sucess.")
	Else
		Set oServer = GetObject("IIS://LocalHost/W3SVC")
		If Err <> 0 Then
		    EndScript ERROR_UnableToOpenW3SVCObject, "Unable to open IIS://LocalHost/W3SVC. " & GetErrorDescription()
		End If
	
		For Each WebServer In oServer
			If (Err.Number <> 0) Then Exit For
	
			If WebServer.Class = "IIsWebServer" Then
				If (WebServer.ServerComment = SymantecServerName) Then
					ServerNum = Right(WebServer.AdsPath, Len(WebServer.AdsPath) - 6)
					ServerNum = Right(ServerNum, Len(ServerNum) - InStr(ServerNum, "/"))
					ServerNum = Right(ServerNum, Len(ServerNum) - InStr(ServerNum, "/"))
	
					Set RootDir = GetObject("IIS://LocalHost/W3SVC/" & ServerNum & "/ROOT")
					If (Err.Number <> 0) Then
					    EndScript ERROR_UnableToOpenW3SVCObject, "Error trying to get the Object: IIS://LocalHost/W3SVC/" & ServerNum & "/ROOT. " & GetErrorDescription()
					End If
	
					RootDir.MimeMap = MimeMapList
					RootDir.Setinfo
	
					If (Err.Number <> 0) Then
					    EndScript ERROR_SetProperty, "Error setting the object's ""MimeMap"" property to the new mimemap list. " & GetErrorDescription()
					End If
	
					Dump("Set MimeType sucess.")
				End If
			End If
		Next
	End If

	SetMimeMap = 0 ' Assume Success
End Function

''''''''''''''''''''''''''
' UnSetDefaultMimeMap Function
''''''''''''''''''''''''''
Function UnSetDefaultMimeMap()
	On Error Resume Next

	Dim WebServer
	Dim MimeEntry, OldMimeMap
	Dim NewMimeMap()
	
	' Only need to do the default web site, since we delete the symantec web server	
	Set WebServer = GetObject("IIS://LocalHost/W3SVC/1/ROOT")
	
	Dim i
	i = -1
	
	OldMimeMap = WebServer.MimeMap
	
	For Each MimeEntry In OldMimeMap 
		If _
			MimeEntry.Extension <> ".bat" And _
			MimeEntry.Extension <> ".cfg" And _
			MimeEntry.Extension <> ".dat" And _
			MimeEntry.Extension <> ".dax" And _
			MimeEntry.Extension <> ".db" And _
			MimeEntry.Extension <> ".grd" And _
			MimeEntry.Extension <> ".hst" And _
			MimeEntry.Extension <> ".ini" And _
			MimeEntry.Extension <> ".kc" And _
			MimeEntry.Extension <> ".loc" And _
			MimeEntry.Extension <> ".manifest" And _
			MimeEntry.Extension <> ".plg" And _
			MimeEntry.Extension <> ".policy" And _
			MimeEntry.Extension <> ".reg" And _
			MimeEntry.Extension <> ".rul" And _
			MimeEntry.Extension <> ".sig" And _
			MimeEntry.Extension <> ".spm" And _
			MimeEntry.Extension <> ".sys" And _
			MimeEntry.Extension <> ".wlt" _
		Then
			i = i + 1
			ReDim Preserve NewMimeMap(i)
			Set NewMimeMap(i) = MimeEntry
		End If
	Next
	
	If i = -1 Then
		WebServer.MimeMap = Array()
	Else
		WebServer.MimeMap = NewMimeMap
	End If

	WebServer.SetInfo
	
	If (Err.Number <> 0) Then
		Dump("Error Trying to set the Object's ""MimeMap"" property to the new mimemap list. ErrorCode: " & Err.Number)
		Err.Clear
	End If

End Function

''''''''''''''''''''''''''
' SetErrorPages Function
''''''''''''''''''''''''''
Function SetErrorPages(vDir, WebDirectory)
	On Error Resume Next

	Dim ErrorList, ListItem
	
	If Ver2K3 = True Then
		ErrorList = Array(_
		"400,*,URL,/Reporting/CustomErrors/400.htm",_
		"401,1,URL,/Reporting/CustomErrors/401-1.htm",_
		"401,2,URL,/Reporting/CustomErrors/401-2.htm",_
		"401,3,URL,/Reporting/CustomErrors/401-3.htm",_
		"401,4,URL,/Reporting/CustomErrors/401-4.htm",_
		"401,5,URL,/Reporting/CustomErrors/401-5.htm",_
		"401,7,URL,/Reporting/CustomErrors/401-7.htm",_
		"403,1,URL,/Reporting/CustomErrors/403-1.htm",_
		"403,2,URL,/Reporting/CustomErrors/403-2.htm",_
		"403,3,URL,/Reporting/CustomErrors/403-3.htm",_
		"403,4,URL,/Reporting/CustomErrors/403-4.htm",_
		"403,5,URL,/Reporting/CustomErrors/403-5.htm",_
		"403,6,URL,/Reporting/CustomErrors/403-6.htm",_
		"403,7,URL,/Reporting/CustomErrors/403-7.htm",_
		"403,8,URL,/Reporting/CustomErrors/403-8.htm",_
		"403,9,URL,/Reporting/CustomErrors/403-9.htm",_
		"403,10,URL,/Reporting/CustomErrors/403-10.htm",_
		"403,11,URL,/Reporting/CustomErrors/403-11.htm",_
		"403,12,URL,/Reporting/CustomErrors/403-12.htm",_
		"403,13,URL,/Reporting/CustomErrors/403-13.htm",_
		"403,14,URL,/Reporting/CustomErrors/403-14.htm",_
		"403,15,URL,/Reporting/CustomErrors/403-15.htm",_
		"403,16,URL,/Reporting/CustomErrors/403-16.htm",_
		"403,17,URL,/Reporting/CustomErrors/403-17.htm",_
		"403,18,URL,/Reporting/CustomErrors/403-18.htm",_
		"403,19,URL,/Reporting/CustomErrors/403-19.htm",_
		"403,20,URL,/Reporting/CustomErrors/403-20.htm",_
		"404,*,URL,/Reporting/CustomErrors/404.htm",_
		"404,2,URL,/Reporting/CustomErrors/404-2.htm",_
		"404,3,URL,/Reporting/CustomErrors/404-3.htm",_
		"405,*,URL,/Reporting/CustomErrors/405.htm",_
		"406,*,URL,/Reporting/CustomErrors/406.htm",_
		"407,*,URL,/Reporting/CustomErrors/407.htm",_
		"412,*,URL,/Reporting/CustomErrors/412.htm",_
		"414,*,URL,/Reporting/CustomErrors/414.htm",_
		"415,*,URL,/Reporting/CustomErrors/414.htm",_
		"500,*,URL,/Reporting/CustomErrors/500.htm",_
		"500,12,URL,/Reporting/CustomErrors/500-12.htm",_
		"500,13,URL,/Reporting/CustomErrors/500-13.htm",_
		"500,15,URL,/Reporting/CustomErrors/500-15.htm",_
		"500,16,URL,/Reporting/CustomErrors/500-16.htm",_
		"500,18,URL,/Reporting/CustomErrors/500-17.htm",_
		"500,18,URL,/Reporting/CustomErrors/500-18.htm",_
		"500,19,URL,/Reporting/CustomErrors/500-19.htm",_
		"501,*,URL,/Reporting/CustomErrors/501.htm",_
		"502,*,URL,/Reporting/CustomErrors/502.htm")
	Else
		ErrorList = Array(_
		"400,*,URL,/Reporting/CustomErrors/400.htm",_
		"401,1,URL,/Reporting/CustomErrors/401-1.htm",_
		"401,2,URL,/Reporting/CustomErrors/401-2.htm",_
		"401,3,URL,/Reporting/CustomErrors/401-3.htm",_
		"401,4,URL,/Reporting/CustomErrors/401-4.htm",_
		"401,5,URL,/Reporting/CustomErrors/401-5.htm",_
		"403,1,URL,/Reporting/CustomErrors/403-1.htm",_
		"403,2,URL,/Reporting/CustomErrors/403-2.htm",_
		"403,3,URL,/Reporting/CustomErrors/403-3.htm",_
		"403,4,URL,/Reporting/CustomErrors/403-4.htm",_
		"403,5,URL,/Reporting/CustomErrors/403-5.htm",_
		"403,6,URL,/Reporting/CustomErrors/403-6.htm",_
		"403,7,URL,/Reporting/CustomErrors/403-7.htm",_
		"403,8,URL,/Reporting/CustomErrors/403-8.htm",_
		"403,9,URL,/Reporting/CustomErrors/403-9.htm",_
		"403,10,URL,/Reporting/CustomErrors/403-10.htm",_
		"403,11,URL,/Reporting/CustomErrors/403-11.htm",_
		"403,12,URL,/Reporting/CustomErrors/403-12.htm",_
		"403,13,URL,/Reporting/CustomErrors/403-13.htm",_
		"403,14,URL,/Reporting/CustomErrors/403-14.htm",_
		"403,15,URL,/Reporting/CustomErrors/403-15.htm",_
		"403,16,URL,/Reporting/CustomErrors/403-16.htm",_
		"403,17,URL,/Reporting/CustomErrors/403-17.htm",_
		"404,*,URL,/Reporting/CustomErrors/404.htm",_
		"405,*,URL,/Reporting/CustomErrors/405.htm",_
		"406,*,URL,/Reporting/CustomErrors/406.htm",_
		"407,*,URL,/Reporting/CustomErrors/407.htm",_
		"412,*,URL,/Reporting/CustomErrors/412.htm",_
		"414,*,URL,/Reporting/CustomErrors/414.htm",_
		"500,*,URL,/Reporting/CustomErrors/500.htm",_
		"500,12,URL,/Reporting/CustomErrors/500-12.htm",_
		"500,13,URL,/Reporting/CustomErrors/500-13.htm",_
		"500,15,URL,/Reporting/CustomErrors/500-15.htm",_
		"501,*,URL,/Reporting/CustomErrors/501.htm",_
		"502,*,URL,/Reporting/CustomErrors/502.htm")
	End If
	
	Dump("Setting HTTP Error pages")
	For Each ListItem In ErrorList
		Dump(ListItem)
	Next
	
	vDir.HttpErrors = ErrorList
	vDir.SetInfo
	
	If (Err.Number <> 0) Then
	    EndScript ERROR_SettingHTTPErrorPages, "Error setting HTTP Error pages. " & GetErrorDescription()
	End If

End Function


''''''''''''''''''''''''''
' Migrate Function
''''''''''''''''''''''''''
Function Migrate(DefaultWebSite, InetPubDir, SepmBinDir, PHPFile, CaclsApp, PHPIniFile, PHPSessionFolder)
	On Error Resume Next
	
	Dim oServer
	Dim WebServer
	Dim ServerNum
	Dim RootDir, vDir

	Set oServer = GetObject("IIS://LocalHost/W3SVC")
	If Err <> 0 Then
	    EndScript ERROR_UnableToOpenW3SVCObject, "Unable to open IIS://LocalHost/W3SVC. " & GetErrorDescription()
	End If

	If DefaultWebSite <> "1" Then

		For Each WebServer In oServer
			If (Err.Number <> 0) Then Exit For
	
			If WebServer.Class = "IIsWebServer" Then
				If (WebServer.ServerComment = SymantecServerName) Then
					ServerNum = Right(WebServer.AdsPath, Len(WebServer.AdsPath) - 6)
					ServerNum = Right(ServerNum, Len(ServerNum) - InStr(ServerNum, "/"))
					ServerNum = Right(ServerNum, Len(ServerNum) - InStr(ServerNum, "/"))
	
					Set RootDir = GetObject("IIS://LocalHost/W3SVC/" & ServerNum & "/ROOT")
					If (Err.Number <> 0) Then
					    EndScript ERROR_UnableToOpenW3SVCObject, "Error trying to get the Object: IIS://LocalHost/W3SVC/" & ServerNum & "/ROOT. " & GetErrorDescription()
					End If
	
					' Now update the root directory object.
					RootDir.Path = InetPubDir
					RootDir.AccessRead = true
					Err.Clear
					RootDir.SetInfo
					RootDir.AppCreate (True)
					If (Err.Number <> 0) Then
					    EndScript ERROR_SettingHomeDirectory, "Error setting home directory. " & GetErrorDescription()
					End If
				End If
			End If
		Next

	Else
		
		Set RootDir = GetObject("IIS://LocalHost/W3SVC/1/ROOT")
		RootDir.AccessRead = true
		Err.Clear
		RootDir.SetInfo

	End If
	
	'Create the virtual directory secars
	RootDir.Delete "IIsWebVirtualDir", "secars"
	Err.Clear
	Set vDir = RootDir.Create("IIsWebVirtualDir", "secars")
	If (Err <> 0) Then
	    EndScript ERROR_CreateVirtualDirectory, "Unable to create " & RootDir.ADsPath & "/secars. " & GetErrorDescription()
	Else
		'Set the new virtual directory path
		vDir.AccessRead = true
		vDir.Path = InetPubDir & "secars"
		Err.Clear

		vDir.AppCreate2 0
		vDir.AppEnable
		vDir.AppFriendlyName = "secars"
		If Ver2K3 = True Then
			vDir.ScriptMaps = "*,""" & SepmBinDir & "secars.dll" & """,1,GET,POST"
		Else
			vDir.ScriptMaps = "*," & SepmBinDir & "secars.dll" & ",1,GET,POST"
		End If
		vDir.UploadReadAheadSize = 1048576
		vDir.Put "AccessScript", true
		vDir.Put "AccessExecute", true
		vDir.Put "DontLog", 1

		'Save the changes
		vDir.SetInfo
		If (Err.Number <> 0) Then
		    EndScript ERROR_SaveConfiguration, "Unable to save configuration for " & RootDir.ADsPath & "/secars. " & GetErrorDescription()
		End If
	End If
	
	'Create the virtual directory secreg
	RootDir.Delete "IIsWebVirtualDir", "secreg"
	Err.Clear
	Set vDir = RootDir.Create("IIsWebVirtualDir", "secreg")
	If (Err <> 0) Then
	    EndScript ERROR_CreateVirtualDirectory, "Unable to create " & RootDir.ADsPath & "/secreg. " & GetErrorDescription()
	Else
		'Set the new virtual directory path
		vDir.AccessRead = true
		vDir.Path = InetPubDir & "secreg"
		Err.Clear

		vDir.AppCreate2 0
		vDir.AppEnable
		vDir.AppFriendlyName = "secreg"
		If Ver2K3 = True Then
			vDir.ScriptMaps = "*,""" & SepmBinDir & "secreg.dll" & """,1,GET,POST"
		Else
			vDir.ScriptMaps = "*," & SepmBinDir & "secreg.dll" & ",1,GET,POST"
		End If
		vDir.UploadReadAheadSize = 1048576
		vDir.Put "AccessScript", true
		vDir.Put "AccessExecute", true
		vDir.Put "DontLog", 1

		'Save the changes
		vDir.SetInfo
		If (Err.Number <> 0) Then
		    EndScript ERROR_SaveConfiguration, "Unable to save configuration for " & RootDir.ADsPath & "/secreg. " & GetErrorDescription()
		End If
	End If

	'Create the virtual directory Reporting
	RootDir.Delete "IIsWebVirtualDir", "Reporting"
	Err.Clear
	Set vDir = RootDir.Create("IIsWebVirtualDir", "Reporting")
	If (Err <> 0) Then
	    EndScript ERROR_CreateVirtualDirectory, "Unable to create " & RootDir.ADsPath & "/Reporting. " & GetErrorDescription()
	Else
		'Set the new virtual directory path
		vDir.AccessRead = true
		vDir.Path = InetPubDir & "Reporting"
		Err.Clear

		vDir.AppCreate2 0
		vDir.AppEnable
		vDir.AppFriendlyName = "Reporting"
		vDir.ScriptMaps = ".php,""" & PHPFile & """,1"
		vDir.DefaultDoc = "index.php"
		'vDir.UploadReadAheadSize = 1048576
		vDir.Put "AccessScript", true
		vDir.Put "AccessExecute", true
		vDir.Put "DontLog", 1

		'Save the changes
		vDir.SetInfo
		If (Err.Number <> 0) Then
		    EndScript ERROR_SaveConfiguration, "Unable to save configuration for " & RootDir.ADsPath & "/Reporting. " & GetErrorDescription()
		End If
		
		SetErrorPages vDir, InetPubDir
	End If

	'Create the virtual directory ClientPackages
	RootDir.Delete "IIsWebVirtualDir", "ClientPackages"
	Err.Clear
	Set vDir = RootDir.Create("IIsWebVirtualDir", "ClientPackages")
	If (Err <> 0) Then
	    EndScript ERROR_CreateVirtualDirectory, "Unable to create " & RootDir.ADsPath & "/ClientPackages. " & GetErrorDescription()
	Else
		'Set the new virtual directory path
		vDir.AccessRead = true
		vDir.Path = InetPubDir & "ClientPackages"
		Err.Clear

		vDir.AppCreate2 0
		vDir.AppEnable
		vDir.AppFriendlyName = "ClientPackages"
		vDir.ScriptMaps = ""
		vDir.Put "DontLog", 1

		'Save the changes
		vDir.SetInfo
		If (Err.Number <> 0) Then
		    EndScript ERROR_SaveConfiguration, "Unable to save configuration for " & RootDir.ADsPath & "/ClientPackages. " & GetErrorDescription()
		End If
	End If

	'Create the virtual directory content
	RootDir.Delete "IIsWebVirtualDir", "content"
	Err.Clear
	Set vDir = RootDir.Create("IIsWebVirtualDir", "content")
	If (Err <> 0) Then
	    EndScript ERROR_CreateVirtualDirectory, "Unable to create " & RootDir.ADsPath & "/content. " & GetErrorDescription()
	Else
		'Set the new virtual directory path
		vDir.AccessRead = true
		vDir.Path = InetPubDir & "content"
		Err.Clear

		vDir.AppCreate2 0
		vDir.AppEnable
		vDir.AppFriendlyName = "content"
		vDir.ScriptMaps = ""
		vDir.Put "DontLog", 1

		'Save the changes
		vDir.SetInfo
		If (Err.Number <> 0) Then
		    EndScript ERROR_SaveConfiguration, "Unable to save configuration for " & RootDir.ADsPath & "/content. " & GetErrorDescription()
		End If

	End If

	'Configure Php
    AnonymousUsername = GetSepmWebsiteAnonymousUsername(DefaultWebSite = "1")
    ConfigurePhp phpIniFile, phpSessionFolder
    AssignFolderACLs phpSessionFolder, AnonymousUsername, CaclsApp
    AssignFolderACLs InetPubDir+"Reporting", AnonymousUsername, CaclsApp
    
	
End Function


''''''''''''''''''''''''''
' Uninstall Function
' If DefaultWebSite not passed in will get from registry
''''''''''''''''''''''''''
Function Uninstall(DefaultWebSite)
	On Error Resume Next

	Dim oServer
	Dim WebServer
	Dim ServerNum
	Dim WshShell, oExec

	Set WshShell = CreateObject("WScript.Shell")

	Set oExec = WshShell.Exec("net start w3svc")

	Do While oExec.Status = 0
		WScript.Sleep 100
	Loop

	Dump("Net Start W3svc status: " & oExec.Status)

	Set oServer = GetObject("IIS://LocalHost/W3SVC")
	If Err <> 0 Then
		Dump("Unable to open IIS://LocalHost/W3SVC. ErrorCode: " & Err.Number)
	End If
	
	If (DefaultWebSite = "") Then
		DefaultWebSite = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Symantec\Symantec Endpoint Protection\SEPM\DefaultWebSite")
	End If

	If (DefaultWebSite = "1") Then
		DefaultCleanUp
		If (Ver2K3 = True) Then
			UnSetDefaultMimeMap
		End If
		Dump("Contents of IIS://LocalHost/W3SVC/1 deleted.")
		StartDefaultWebSite
	Else
		Err.Clear
		For Each WebServer In oServer
				If (Err.Number <> 0) Then
					Dump("Exiting WebServer For Loop, Err.Number: " & Err.Number)
					Exit For
				End If

			If WebServer.Class = "IIsWebServer" Then
				If (WebServer.ServerComment = SymantecServerName) Or (WebServer.ServerComment = "Sygate Web Server") Then
					ServerNum = Right(WebServer.AdsPath, Len(WebServer.AdsPath) - 6)
					ServerNum = Right(ServerNum, Len(ServerNum) - InStr(ServerNum, "/"))
					ServerNum = Right(ServerNum, Len(ServerNum) - InStr(ServerNum, "/"))
					WebServer.Stop
					If (Err.Number <> 0) Then
						Dump("Unable to Stop IIS://LocalHost/W3SVC/" & ServerNum & ". ErrorCode: " & Err.Number)
					End If
					oServer.Delete "IIsWebServer", ServerNum
					If (Err.Number <> 0) Then
						Dump("Unable to Delete IIS://LocalHost/W3SVC/" & ServerNum & ". ErrorCode: " & Err.Number)
					Else
						Dump("IIS://LocalHost/W3SVC/" & ServerNum & " deleted.")
					End If
				End If
			End If
		Next
		
	End IF

	Set oExec = WshShell.Exec("net stop w3svc")

	Do While oExec.Status = 0
		WScript.Sleep 100
	Loop

	Dump("Net Stop W3svc status: " & oExec.Status)
	
	Err.Clear
End Function


''''''''''''''''''''''''''
'	Re-Set the IIS Anonymous account name and assign folder permissions to that account
'   This allows for WindowsAuthentication for the ODBC connection/the reporting components
''''''''''''''''''''''''''
Function PostInstallOrMigrate(DefaultWebSite, InetPubDir, CaclsApp, PHPSessionFolder, AccountName, AccountPassword, DoubleQuoteIndicies)
    On Error Resume Next


    'Put back th double quotes
    If DoubleQuoteIndicies <> "" Then
         AccountPassword = DecodeInput(AccountPassword, DoubleQuoteIndicies)
    End If

    SetSepmWebsiteAnonymousAccount DefaultWebSite = "1", AccountName, AccountPassword
    AssignFolderACLs phpSessionFolder, AccountName, CaclsApp
    AssignFolderACLs InetPubDir+"Reporting", AccountName, CaclsApp

End Function

''''''''''''''''''''''''''
'	Cleanup Default Web Site
''''''''''''''''''''''''''
Function DefaultCleanUp
	On Error Resume Next

	Dim WebServer
	Dim ServerNum

	Set WebServer = GetObject("IIS://LocalHost/W3SVC/1/ROOT")
	If (Err.Number <> 0) Then
		Dump( "Error Trying To Get the Object: IIS://localhost/W3SVC/1. ErrorCode: " & Err.Number)
		Err.Clear
	End If

	WebServer.Delete "IIsWebVirtualDir", "secars"
	If (Err.Number <> 0) Then
		Dump("Unable to delete secars from default site. ErrorCode: " & Err.Number)
		Err.Clear
	End If
	WebServer.Delete "IIsWebVirtualDir", "secreg"
	If (Err.Number <> 0) Then
		Dump("Unable to delete secreg from default site. ErrorCode: " & Err.Number)
		Err.Clear
	End If
	WebServer.Delete "IIsWebVirtualDir", "Reporting"
	If (Err.Number <> 0) Then
		Dump("Unable to delete Reporting from default site. ErrorCode: " & Err.Number)
		Err.Clear
	End If
	WebServer.Delete "IIsWebVirtualDir", "ClientPackages"
	If (Err.Number <> 0) Then
		Dump("Unable to delete ClientPackages from default site. ErrorCode: " & Err.Number)
		Err.Clear
	End If
	WebServer.Delete "IIsWebVirtualDir", "content"
	If (Err.Number <> 0) Then
		Dump("Unable to delete content from default site. ErrorCode: " & Err.Number)
		Err.Clear
	End If

End Function

''''''''''''''''''''''''''
'	Start Default Web Site
''''''''''''''''''''''''''
Function StartDefaultWebSite()
	On Error Resume Next

	Dim WebServer

	Set WebServer = GetObject("IIS://LocalHost/W3SVC/1")
	If (Err.Number <> 0) Then
		Dump( "Error Trying To Get the Object: IIS://localhost/W3SVC/1. ErrorCode: " & Err.Number)
		Err.Clear
	End If

	WebServer.Start
	If (Err.Number <> 0) Then
		Dump("Unable to Start IIS://LocalHost/W3SVC/1" & ". ErrorCode: " & Err.Number)
		Err.Clear
	End If
	Dump("IIS://LocalHost/W3SVC/1" & " started.")

End Function

''''''''''''''''''''''''''
'	Stop Default Web Site
''''''''''''''''''''''''''
Function StopDefaultWebSite()
	On Error Resume Next

	Dim WebServer

	Set WebServer = GetObject("IIS://LocalHost/W3SVC/1")
	If (Err.Number <> 0) Then
		Dump( "Error Trying To Get the Object: IIS://localhost/W3SVC/1. ErrorCode: " & Err.Number)
		Err.Clear
	End If

	WebServer.Stop
	If (Err.Number <> 0) Then
		Dump("Unable to Stop IIS://LocalHost/W3SVC/1" & ". ErrorCode: " & Err.Number)
		Err.Clear
	End If
	Dump("IIS://LocalHost/W3SVC/1" & " stopped.")

End Function

'****************************************************************************************
' Modify the ACL's for the PHP session folder.
' 
' Inputs:
'  
'   strFolder   - the fully qualified path to folder to modify permissions for.
'   strIISUser  - the user to be granted rights to this folder. This will be the 
'                 anonymous user that is configured for the SEPM website.
'
'****************************************************************************************
Sub AssignFolderACLs(strFolder, strIISUser, strCaclsApp)
    Dim oShell
    Dim oFSO
    Dim cmd
    Dim cmdStatus
    Dim lastChar

    Set oShell = CreateObject("Wscript.Shell")
    Set oFSO = CreateObject("Scripting.FileSystemObject")

    '
    ' the path we are getting from SESM.ism has a trailing backslash and cacls command
    ' seems to choke on that so this code will that character from the path
    '
    lastChar = Right(strFolder, 1)
    If lastChar = "\" Then
	strFolder = Left(strFolder, Len(strFolder) - 1)
    End If

    If oFSO.FolderExists(strFolder) Then
	If VerMaj = "6" Then
	    cmd = chr(34) & strCaclsApp & chr(34) & " " & chr(34) & strFolder & chr(34) & " -u s:S-1-5-32-544|s:S-1-5-32-568|u:" & strIISUser
	Else
	    cmd = chr(34) & strCaclsApp & chr(34) & " " & chr(34) & strFolder & chr(34) & " -u s:S-1-5-32-544|u:" & strIISUser
	End If
        Dump("Executing command: " & cmd)
        cmdStatus = oShell.Run(cmd, 2, True)
        If (cmdStatus <> 0) Then
            EndScript ERROR_ExecutingCacls, "Error ( " & cmdStatus &" ) returned from executing cacls.exe."
        End If
    Else
        EndScript ERROR_Bad_strFolder, "In function AssignFolderACLs() - The strFolder param points to a folder that does not exist: " & strFolder
    End If
    Dump("ACL's have been set for folder: " & strFolder)
End Sub

'****************************************************************************************
' Modify php.ini settings to direct IIS to use a custom location for the session.
'
' Inputs:
'  
'   phpIniFile       - the fully qualified path to the installed Php.ini file.
'   phpSessionFolder - the fully qualified path to the folder to use for the Php session.
'
' Notes: This code adds a "session.save_path=xxx" entry to the Php.ini file pointing
'        to the passed in tempFolderPath.
'****************************************************************************************
Sub ConfigurePhp(phpIniFile, phpSessionFolder)
    On Error Resume Next
  
    Dim oFSO
    Dim inFile
    Dim outFile
    Dim bFound
    Dim php
    Dim line
    Dim bReadonly
    Dim READ_ONLY
    
    READ_ONLY = 1
  
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    
    '
    ' If Php.ini is readonly remove this attribute.
    '
    Set inFile = oFSO.GetFile(phpIniFile)
    If inFile.attributes & READ_ONLY Then
        inFile.attributes = inFile.attributes and not READ_ONLY
        bReadonly = True
    Else
        bReadonly = False
    End If
    
    
    Set inFile = oFSO.OpenTextFile(phpIniFile, 1)
    
    If (Err.Number <> 0) Then
        ENdScript ERROR_Bad_PhpIniFile, "In function ConfigPhpIni() - The phpIniFile param points to a folder that does not exist: " & phpIniFile
    End If

    php = Split(inFile.ReadAll, vbNewline)
    
    set inFile = Nothing
    
    Set outFile = oFSO.OpenTextFile(phpIniFile, 2,True)
    If (Err.Number <> 0) Then
        EndScript ERROR_Bad_PhpIniFile, "In function ConfigPhpIni() - Error opening file for writing: " & phpIniFile
    End If
    
    For each line In php
        If InStr(line, "session.save_path") Then
            line = "session.save_path=" & phpSessionFolder
            bFound = True
        End If
      outFile.WriteLine line
    Next
    
    If bFound <> True Then
        outFile.WriteLine "session.save_path=" & phpSessionFolder
    End If
    
    set outFile = Nothing
    
    '
    ' If Php.ini was readonly restore this attribute.
    '
    If bReadonly Then
        Set inFile = oFSO.GetFile(phpIniFile)
        inFile.attributes = inFile.attributes or READ_ONLY
    End If
    
    Dump("The Php.ini file has been configured: " & phpIniFile)
End Sub



'****************************************************************************************
' Set the anonymous user for the SEPM website.
'
' Inputs:
'  
'   UseDefaultWebsite - boolean value True for default website, or False for custom website
'   UserName - String 
'   Password - String 
'
'****************************************************************************************
Function SetSepmWebsiteAnonymousAccount(UseDefaultWebsite, UserName, Password)

    Dim oRoot
    Set oRoot = GetSepmWebsite(UseDefaultWebsite)
    
    oRoot.AnonymousUsername = UserName
    oRoot.AnonymousUserPass = Password
    
    oRoot.SetInfo()
    

End Function

'****************************************************************************************
' Get the configured anonymous user for the SEPM website.
'
' Inputs:
'  
'   UseDefaultWebsite - boolean value True for default website, or False for custom website
'
'****************************************************************************************
Function GetSepmWebsiteAnonymousUsername(UseDefaultWebsite)

    Dim oRoot
    Dim AnonymousUsername 
    
    
    Set oRoot = GetSepmWebsite(UseDefaultWebsite)
    AnonymousUsername = oRoot.AnonymousUsername  'oRoot.get("anonymoususername")
    Dump("Website's AnonymousUsername: " & AnonymousUsername)
    
    If AnonymousUsername = "" Then
        EndScript ERROR_GetAnonymousUsername, "Unable to get the anonymous username!"
    Else
        GetSepmWebsiteAnonymousUsername = AnonymousUsername
    End If


End Function

'****************************************************************************************
' Get the SEPM website object.
'
' Inputs:
'  
'   UseDefaultWebsite - boolean value True for default website, or False for custom website
'
'****************************************************************************************
Function GetSepmWebsite(UseDefaultWebsite)
Dim oRoot

If UseDefaultWebsite Then
    '
    ' This code gets the Default Web Site
    '
    Dim oSite
    Dump("Default Website")

    Set oSite = GetObject("IIS://LocalHost/W3SVC/1")
    If (Err.Number <> 0) Then
        EndScript ERROR_UnableToOpenW3SVCObject, "Unable to get the object IIS://LocalHost/W3SVC/1. " & GetErrorDescription()
    End If


    Set oRoot = GetObject("IIS://LocalHost/W3SVC/1/ROOT")
Else
    Dim oServer
    Dim oWebServer
    Dump("Custom Website")

    '
    ' Get W3SVC object
    '
    Set oServer = GetObject("IIS://LocalHost/W3SVC")
    If Err <> 0 Then
        EndScript ERROR_UnableToOpenW3SVCObject, "Unable to open IIS://LocalHost/W3SVC. " & GetErrorDescription()
    End If

    '
    ' This code gets the Custom Web Site
    '
    '   We have to iterate all the IIsWebServer objects in oServer looking
    '   for the one whose "servercomment" property is "Symantec Web Server"
    '
    For Each oWebServer In oServer
        If (Err.Number <> 0) Then Exit For
        If oWebServer.Class = "IIsWebServer" Then
            Dump("Found IIsWebServer: " & oWebServer.ServerComment)
            If (oWebServer.ServerComment = "Symantec Web Server") Then
                Dim ServerNum
                ServerNum = Right(oWebServer.AdsPath, Len(oWebServer.AdsPath) - 6)
                ServerNum = Right(ServerNum, Len(ServerNum) - InStr(ServerNum, "/"))
                ServerNum = Right(ServerNum, Len(ServerNum) - InStr(ServerNum, "/"))
                Set oRoot = GetObject("IIS://LocalHost/W3SVC/" & ServerNum & "/ROOT")
                If (Err.Number <> 0) Then
                    EndScript ERROR_UnableToOpenW3SVCObject, "Unable to get the object IIS://LocalHost/W3SVC/" & ServerNum & "/ROOT. " & GetErrorDescription()
                End If

                Exit For
            End If
        End If
    Next
End If

Set GetSepmWebsite = oRoot

End Function


'****************************************************************************************
' Verifies the SepmBinDirPath
'****************************************************************************************
Function VerifySepmBinDirPath(Path, Ver2K3)
    Dim strSEPMBinDir
    If Ver2K3 = True Then
			strSEPMBinDir = oArgs(3)
		Else
			Set fso=CreateObject("Scripting.FileSystemObject")
			If fso.FolderExists(oArgs(3)) Then
				Set objFolder = fso.GetFolder(oArgs(3))
				strSEPMBinDir = objFolder.ShortPath & "\"
			Else
			    EndScript ERROR_Missing_SepmBinFolder, "SEPM Bin dir does not exist."
			End If
		End If

    VerifySepmBinDirPath = strSEPMBinDir
End Function


'****************************************************************************************
' This code replaces all previous calls to WScript.Echo().  This gives us a single
' point of message output. We take advantage of this by allowing logging to a file
' if you edit the script and change bLogToFile to be True.
'****************************************************************************************
Sub Dump(msg)
    '
    ' Echo to stdout
    '    
	WScript.Echo msg
	
    '
    ' Conditionally log to file ( only enabled by hand-edit of script to set bLogToFile = True )
    '    
	If (bLogToFile) Then
        Dim oFSO
        Dim oLogFile

        Set oFSO = CreateObject("Scripting.FileSystemObject")

        Set oLogFile = oFSO.OpenTextFile("c:\IISConfig.log", 8, True)
        oLogFile.WriteLine(msg)
        oLogFile.Close

        Set oLogFile = Nothing
        Set oFSO = Nothing
    End If
End Sub

'****************************************************************************************
' Return a description string for the error stored in VBScript Err object.
'****************************************************************************************
Function GetErrorDescription()
	Dim ErrorDescription

	ErrorDescription = "The reported error was " & Err.Number & " - "
	
	Select Case (Err.Number)
			Case &H80070003
					ErrorDescription = ErrorDescription & "The path requested could not be found."
			Case &H80070005
					ErrorDescription = ErrorDescription & "Access is denied for the requested path or property."
			Case &H80070094
					ErrorDescription = ErrorDescription & "The requested path is being used by another application."
			Case Else
					ErrorDescription = ErrorDescription & Err.Description
	End Select

	GetErrorDescription = ErrorDescription
End Function

'****************************************************************************************
' Provide a single exit mechanism for the IISConfig.vbs script.  
'
' Inputs:
'  
'   endCode - integer representing success or failure code. You should only pass 
'             one of the defined ERROR_XXX values that are defined at the top of
'             this file.  ERROR_Success for successful execution or any of the other
'             defines depending on the exact failure.
'
'   endMsg - text message that describes the success or failure condition.

' Notes: This code replaces all previous calls to WScript.Quit(n) to give a single
'        exit point for the script. If the endCode is not ERROR_Success this code
'        will create an application error event in the windows event log. This is
'        done because we don't have access to the MSI install log file.  In the calling
'        code which is InstallerCA.dll, we test the return value from the script and
'        if it is non-zero, which means failure, InstallerCA code writes a line to 
'        the install log instructing to look at the Windows Event Log for the failure event.
'
'****************************************************************************************
Sub EndScript(endCode, endMsg)
    Dim oShell
    Dim strError
    
    If endCode = Error_Success Then
        Dump(endMsg)
    Else
        strError = "IISConfig.vbs error( " & endCode & " ) - " & endMsg
	    Dump(strError)
        Set oShell = WScript.CreateObject("WScript.Shell")
        oShell.LogEvent 1, strError
        WScript.Quit(endCode)
    End If
End Sub


'****************************************************************************************
' Decode an argument passed from SEPM
' 
' Inputs:
'
'   inputString - Encoded string.
'   indicies - comma-separated integer array
'
' Return:
'   Decoded String
'
' Notes: SEPM replaces all double quotes with question marks, and provide another array
'        to indicate which characters are supposed to be double quotes
'
'****************************************************************************************
Function DecodeInput(inputString, indicies)

	DecodeInput = inputString

	Dim indiciesArray, strIndex
	indiciesArray = Split(indicies, ",")
	For Each strIndex In indiciesArray
		Dim index
		index = CInt(strIndex)

		If index = 0 Then
			DecodeInput = """" & Right(DecodeInput, Len(DecodeInput) - 1 )
		ElseIf index = Len(DecodeInput) - 1 Then
			DecodeInput = Left(DecodeInput, Len(DecodeInput) - 1 ) & """"
		Else
			DecodeInput = Left(DecodeInput, index) & """" & Right(DecodeInput, Len(DecodeInput) - index - 1)
		End If
	Next

	
End Function
