Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports CharterDotCom.WebSite.Library

Public Class NonSecureModule
    Implements IHttpModule

    Private Const orderFolder As String = "/order/"
    Private Const accountFolder As String = "/account/"
    Private Const cbAcountFolder As String = "/cbaccount/"
    'Private Const affliateFolder As String = "/affliate/"
    Private Const httpsPort As String = "https:"
    Private Const httpPort As String = "http:"
    Private Const vbFileExtenssion As String = ".aspx"

    Public Sub Dispose() Implements System.Web.IHttpModule.Dispose

    End Sub

    Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
        AddHandler context.BeginRequest, New EventHandler(AddressOf Me.OnBeginRequest)
    End Sub

	Public Sub OnBeginRequest(ByVal o As Object, ByVal e As EventArgs)
		If Not HttpContext.Current Is Nothing Then
			Dim cUrl As String = HttpContext.Current.Request.Url.ToString.ToLower()



			'#################################################################################################
			'####################   M O D I F I C A T I O N        8/15/2007    ROD COPELAND #################
			'#################################################################################################
			'THIS FUNCTIONALITY WAS ADDED FOR THE URL REDIRECT SOLUTION.
			'################################################################################
			'DETERMINE IF THIS IS A REDIRECT URL FIRST, IF SO, REDIRECT TO THE MATCHING URL
			'################################################################################
			Try
				Dim lRedirectURLCollection As Dictionary(Of String, String) = HttpContext.Current.Application("URL_REDIRECT_MAPPING_COLLECTION")
				Dim trimURL As String = cUrl.ToLower()
				'################################################################################
				'MODIFICATION   SIVA DOLA   MARCH 11, 2008
				'THIS IS FOR THE CHARTER BUSINESS DEVELOPER LOCAL MACHINE SO THEY CAN DEBUG, 
				'THEY ARE USING THE WEB SERVER AS PART OF VS.NET RATHER THAN (IIS WHICH DEFAULTS 
				'TO PORT 80) RATHER THAN THE VS.NET WEB SERVER PORT 1801
				'################################################################################
				trimURL = trimURL.Replace("http://localhost:1801/ui", "")
				'################################################################################
				trimURL = trimURL.Replace("http://localhost/ui", "")
				trimURL = trimURL.Replace("http://localhost", "")
				trimURL = trimURL.Replace("http://kstlmweb65", "")
				trimURL = trimURL.Replace("http://kstlmweb66", "")
				trimURL = trimURL.Replace("http://kstlmweb67", "")
				trimURL = trimURL.Replace("http://kstlmcc2p01", "")
				trimURL = trimURL.Replace("http://kstlmcc2p02", "")
				trimURL = trimURL.Replace("http://charter.com", "")
				trimURL = trimURL.Replace("http://www.charter.com", "")
				trimURL = trimURL.Replace("http://www.cc2uatx.chartercom.com", "")
				trimURL = trimURL.Replace("http://cc2uatx.chartercom.com", "")
				trimURL = trimURL.Replace("http://cc2uatx", "")
				trimURL = trimURL.Replace("http://cc2uata", "")


				'*****************
				' Jonathan Parks - 5/7/08
				' inspect the URL/QueryString for invalid string(s)
				' redirect to the MissingCatalog error page if any are found
				'*****************
				Try
					Dim lXSSStringCollection As List(Of String) = HttpContext.Current.Application(ApplicationKey.XSSStringList)

					If Not (lXSSStringCollection Is Nothing) Then
						For Each item As String In lXSSStringCollection
							If trimURL.Contains(item) Then
								' redirect to the MissingCatalog error page
								HttpContext.Current.Response.Redirect("~/Errors/MissingCatalog.aspx?error=XSSViolation", True)
							End If
						Next
					End If

				Catch ex As Exception
					'*****************
					' Do not fail just because we errored out trying to do XSS validation, just continue on
					'*****************
				End Try

				'######################################################################################################
				'ROD COPELAND   4/13/2010   SYCH PROJECT PREP CHANGES
				'HIGH LEVEL REQUIREMENT IS TO REDIRECT THIS APPLICATIONS MARKETING PAGES TO SYNCHRONOSS SERVERS. 
				'######################################################################################################
				'JPIRKEY 4/2010 Moved the Synch project redirection to a central location for ease of maintenance
				'Adding more functionality which requires code to be located lower in the stack.

				'SyncHandler.CheckSyncRedirectURL(trimURL)
				'######################################################################################################


				If Not (lRedirectURLCollection Is Nothing) Then
					If lRedirectURLCollection.ContainsKey(trimURL) Then
						'##############################
						'REDIRECT TO THE MATCHING URL
						'##############################
						Dim redirectURL As String = lRedirectURLCollection(trimURL)
						If Not (redirectURL Is Nothing) Then
							If (redirectURL.Trim() <> "") Then
								HttpContext.Current.Response.Redirect(redirectURL, True)
							Else
								HttpContext.Current.Response.Redirect("~/Default.aspx", True)
							End If
						Else
							HttpContext.Current.Response.Redirect("~/Default.aspx", True)
						End If

					End If
				End If
			Catch ex As Exception
				'################################################################################
				'FOR NOW, DONT FAIL THE ENTIRE REQUEST JUST BECAUSE A REDIRECT ISN'T WORKING.
				'################################################################################
			End Try
			'#################################################################################################
			'#################################################################################################




			'#####################################################################
			'IN SUPPORT OF GOOGLE SEARCH ISSUE ID 3373, THE GOOGLE JAVASCRIPT IS 
			'CASE SENSITIVE ON ONE OF ITS FORM VALUES EXPECTED. FORCE UPPERCASE 
			'ON IT.
			'#####################################################################
			cUrl = cUrl.Replace("cof=forid", "cof=FORID")

            'If cUrl.ToLower.Contains(vbFileExtenssion) Then
            '    If cUrl.Contains(httpsPort) Then
            '        If Not (cUrl.ToLower.Contains(orderFolder) OrElse cUrl.ToLower.Contains(accountFolder) OrElse cUrl.ToLower.Contains(cbAcountFolder)) Then
            '            ' issue(26556)
            '            If HttpContext.Current.Request.IsAuthenticated() Then
            '                ' force https
            '                cUrl = cUrl.Replace(httpPort, httpsPort)
            '            Else
            '                cUrl = cUrl.Replace(httpsPort, httpPort)
            '            End If
            '            HttpContext.Current.Response.Redirect(cUrl, True)
            '        End If
            '    End If
            'End If

        End If
	End Sub

End Class

