'=====================================================================
'= This script will add an ISAPI filter to a single site and add the =
'= file to the Webservice Extenstions in IIS 6                       =
'=====================================================================

'Declare some vars
Dim FiltersObj, IIsWebServer
Dim FilterObj 
Dim LoadOrder 
Dim FilterName 
Dim FilterPath 
Dim FilterDesc
Dim WebSitePath
Dim WebSiteName
Dim ScriptArgs

'Args: 0 = Website Name (aka displayed in IIS) | 1 = Jk Connector dll folder path
ScriptArgs = Split(Session.Property("CustomActionData"),"|")

'Init vars
WebSitePath = ""
WebSiteName = ScriptArgs(0)
FilterName = "Tomcat" 
FilterPath = ScriptArgs(1) & "isapi_redirect.dll" 
FilterDesc = "Apache Jakarta Connector"

'Get IIS6 management provider (for queries and Create method)
Set providerObj = GetObject("winmgmts://localhost/root/MicrosoftIISv2")

'Find the site path (compare displayed names found in IISWebServerSetting.ServerComment property)
' *Note: I use the display name, but any other property of IIsWebServer or IIsWebServerSetting 
'        could be used
For Each item In providerObj.ExecQuery("SELECT * FROM IIsWebServer")
	set IIsWebServerSettingObj = providerObj.get("IIsWebServerSetting='" & item.name & "'")
	if instr(IIsWebServerSettingObj.ServerComment,WebSiteName) > 0 then
		'Storing the path (W3SVC/...) for later use
		WebSitePath = item.name
	End If
Next

'If site found, keep going
If WebSitePath <> "" Then
	
	' *Note: 4 steps process:
	'		    1) Check if IIsFilters (container of IIsFilter objects) exits, else create it
	'			2) Check if IIsFilter object exist, else create it
	'			3) Add Filter to load order (if not already present)
	'			4) Add Filter to Web Service Extensions (if not already present)
	
	'Select or Create IIsFilters (with an "s") object
	FiltersNodeExist WebSitePath, providerObj, fExist
	If fExist Then
		Set FiltersObj = GetObject("IIS://localhost/" & WebSitePath & "/Filters")
	Else
		Set mySite = GetObject("IIS://localhost/" & WebSitePath)
		Set FiltersObj = mySite.Create("IIsFilters", "Filters")
		FiltersObj.SetInfo
	End If
	
	'Create IIsFilter (without an "s") object
	JkFilterNodeExist WebSitePath, FilterPath, providerObj, fExist
	If not fExist Then
		Set FilterObj = FiltersObj.Create("IIsFilter", FilterName) 
		FilterObj.FilterPath = FilterPath 
		FilterObj.FilterDescription = FilterDesc 
		FilterObj.SetInfo
	End If
	
	'Add IIsFilter object in load order of IIsFilters object.  This is done through
	'a method of the IIsFilters (with an "s") object
	If not instr(FiltersObj.FilterLoadOrder,FilterName) Then
		LoadOrder = FiltersObj.FilterLoadOrder 
		If LoadOrder <> "" Then 
		  LoadOrder = LoadOrder & "," 
		End If 
		LoadOrder = LoadOrder & FilterName 
		FiltersObj.FilterLoadOrder = LoadOrder 
		FiltersObj.SetInfo
	End If
	
	' Enable JK Connector in Web Service Extensions
	Set IIsWebServiceObj = GetObject("IIS://localhost/W3SVC") 
	JkWebServiceExtensionExist FilterPath, providerObj, wseExist
	If not wseExist Then
		IISWebServiceObj.AddExtensionFile FilterPath, true, "", true, FilterDesc
		IIsWebServiceObj.SetInfo
	End If
Else
	'Random error code... Im not catching it anyway, but I want the installer to stop 
	'and roolback.
	Err.Raise 1337
End If

' =============== Functions ===============
' *Note: The functions implementations are not perfect and not realy optimised.
'        I am sure there is a better way to do those, but it's late and I want
'        to move on with this project.

'Checks if the IIsFilters (with an "s") object exist for this site
Function FiltersNodeExist(webSitePath, providerObj, outParam)
	Set colItems = providerObj.ExecQuery("Select * from IIsFilters where name like '" & webSitePath & "/Filters'")
	outParam = colItems.Count > 0
End Function

'Checks if an IIsFilter (Without an "s") already exists according to the website path (W3SVC/...)
'and the ISAPI filter file path
Function JkFilterNodeExist(webSitePath, filePath, providerObj, outParam)
	outParam = false
	Set colItems = providerObj.ExecQuery("Select * from IIsFilterSetting")
	For Each objItem in colItems
		If instr(objItem.name, webSitePath) > 0 And objItem.FilterPath = filePath Then
			outParam = true
			Exit For
		End If
	Next
End Function

'Checks if the ISAPI filter (DLL) exists in IIS Web Service Extensions
Function JkWebServiceExtensionExist(filePath, providerObj, outParam)
	outParam = false
	Set colItems = providerObj.ExecQuery("Select * from IIsWebServiceSetting")
	For Each objItem in colItems
		For i = 0 to Ubound(objItem.WebSvcExtRestrictionList)
			If objItem.WebSvcExtRestrictionList(i).FilePath = filePath Then
				outParam = true
				Exit For
			End If
		Next
		If outParam Then
			Exit For
		End If
	Next
End Function


