Imports System.Collections.Generic
Imports CharterDotCom.CommonEntities.Library
Imports CharterDotCom.CommonEntities.Library.Enumerations
Imports CharterDotCom.CommonEntities.Library.EntityMemberServices
Imports CharterDotCom.CommonEntities.Library.EntityEBSServices
Imports CharterDotCom.CommonEntities.Library.EntitySupport
Imports CharterDotCom.CommonEntities.Library.EntitySearch
Imports CharterDotCom.CommonEntities.Library.EntityWebConfig
Imports CharterDotCom.CommonEntities.Library.EntityG2BServices
Imports CharterDotCom.CommonEntities.Library.Common
Imports CharterDotCom.WebSite.Library

Partial Class Controls_CustomContent
	Inherits System.Web.UI.UserControl

	Public Property ContentKey() As String
		Get
			If ViewState(Me.ID & "_ContentKey") Is Nothing Then
				Return Nothing
			Else
				Return CType(ViewState(Me.ID & "_ContentKey"), String)
			End If
		End Get

		Set(ByVal value As String)
			ViewState(Me.ID & "_ContentKey") = value
		End Set
	End Property

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Page.User.IsInRole(RoleName.Author) Then
			pnlContent.Style("border") = "red solid 1px"
			pnlContent.Attributes("onclick") = String.Format("top.editor.location.href='{0}/Authors/Editor.aspx?{1}={2}&{3}={4}&{5}={6}'", Request.ApplicationPath, QueryStringName.OwnerTypeID, OwnerType.Other.GetHashCode(), QueryStringName.OwnerID, 0, QueryStringName.ControlID, ContentKey)
			'pContent.Style("border") = "red solid 1px"
			'pContent.Attributes("onclick") = String.Format("top.editor.location.href='{0}/Authors/Editor.aspx?{1}={2}&{3}={4}&{5}={6}'", _
			'Request.ApplicationPath, QueryStringName.OwnerTypeID, OwnerType.Other.GetHashCode(), _
			'QueryStringName.OwnerID, 0, QueryStringName.ControlID, ContentKey)
		Else
			pnlContent.Style("border") = "0"
			pnlContent.Attributes.Remove("onclick")
			'pContent.Style("border") = "0"
			'pContent.Attributes.Remove("onclick")
		End If
	End Sub

	Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
		If ContentKey IsNot Nothing Then


            '##########################################################################
            'ROD  COPELAND  MODIFICATION    FEB 4, 2009 CREATIVE LIFT REDESIGN
            '##########################################################################
            'AT THE TIME THIS WAS WRITTEN, A SIMULAR BLOCK OF CODE EXISTS IN THE BLCONTENTMANAGER.GETCONTENT()
            'ENTERPRISE SECURITY HAS INDICATED THAT WE SHOULD NOT ALLOW PEOPLE TO PASS 
            'UNVALIDATED INPUT INTO THE DB CALLS FOR CONTENT RECORDS, THIS CHANGE STRIPS 
            'VALUES THAT CURRENT DONT MAKE SENSE FOR VALID CONTENT RECORD NAMES.  NORMAL NAMES 
            'AT THIS POINT ARE LIKE    
            'HOME:PROMO:CABLE:OFFER    OR CABLESUPPORTPAGERECORD
            '##########################################################################
            If Not (ContentKey Is Nothing) Then
                ContentKey = ContentKey.Replace("~", "")
                ContentKey = ContentKey.Replace("`", "")
                ContentKey = ContentKey.Replace("!", "")
                'ContentKey = ContentKey.Replace("@", "")
                'ContentKey = ContentKey.Replace("#", "")
                ContentKey = ContentKey.Replace("$", "")
                ContentKey = ContentKey.Replace("%", "")

                ContentKey = ContentKey.Replace("^", "")
                ContentKey = ContentKey.Replace("&", "")
                'ContentKey = ContentKey.Replace("*", "")
                ContentKey = ContentKey.Replace("(", "")
                ContentKey = ContentKey.Replace(")", "")
                ContentKey = ContentKey.Replace("--", "")
                ContentKey = ContentKey.Replace("__", "")

                ContentKey = ContentKey.Replace("+", "")
                ContentKey = ContentKey.Replace("=", "")
                ContentKey = ContentKey.Replace("<", "")
                ContentKey = ContentKey.Replace(">", "")
                ContentKey = ContentKey.Replace(";", "")
                ContentKey = ContentKey.Replace("""", "")
                ContentKey = ContentKey.Replace("'", "")
                ContentKey = ContentKey.Replace(",", "")
                ContentKey = ContentKey.Replace(".", "")

                ContentKey = ContentKey.Replace("?", "")
                ContentKey = ContentKey.Replace("/", "")
                ContentKey = ContentKey.Replace("{", "")
                ContentKey = ContentKey.Replace("[", "")

                ContentKey = ContentKey.Replace("}", "")
                ContentKey = ContentKey.Replace("]", "")
                ContentKey = ContentKey.Replace("|", "")
                ContentKey = ContentKey.Replace("\", "")
            End If
            '##########################################################################



            '#############################################################################
            'M O D I F I C A T I O N   9/24/2007     ROD COPELAND
            '#############################################################################
            'PERFORMANCE CHANGE FOR THIS CONTROL IN THE MYACCOUNT SECTION. RIGHT NOW, IT 
            'LOOKS LIKE THIS CONTROL DOES A REMOTING CALL TO GET CONTENT EVERY TIME 
            'THE PAGE_LOAD IS CALLED, LOOKS TO BE UNNECESSARY. TO RESOLVE THIS ISSUE FOR 
            'THE MOST CRITICAL PLACE THAT DOESNT USE OUTPUT CACHING (THE MYACCOUNT SECTION 
            'AND ALL ITS PERSONALIZATION), DETERMINE IF THIS CONTROL IS BEING USED IN THE 
            'MYACCOUNT AREA, IF SO, THEN USE A CACHED COPY OF THE VALUE RATHER THAN DOING 
            'A REMOTING CALL TO GET THE SAME THING.  THIS LOGIC APPLIES TO MYACCOUNT PAGES 
            'ONLY RIGHT NOW SINCE THE TIME HASN'T BEEN ALLOCATED TO RESEARCH THE IMPLICATIONS 
            'OF CACHING THE CONTROL ALL OVER THE SITE.  THE CURRENT VERSION OF THE CONTROL 
            'IS FOR A GROUP OF USERS, NOT INDIVIDUALS SO, ITS USED IN A APPLICATION VARIABLE 
            'RATHER THAN SESSION.
            '#############################################################################
            Select Case True
                Case Request.Url.ToString().ToLower().Contains("order/login.aspx")
                    '###############################################
                    'GET THE GENERALIZED CONTENT, NOT USER SPECIFIC
                    '###############################################
                    ltrContent.Text = ContentManager.Instance.GetContent(ContentKey, 1, OwnerType.Other)
                Case Request.Url.ToString().ToLower().Contains("visitors/forgot.aspx")
                    '###############################################
                    'GET THE GENERALIZED CONTENT, NOT USER SPECIFIC
                    '###############################################
                    ltrContent.Text = ContentManager.Instance.GetContent(ContentKey, 1, OwnerType.Other)
                Case Request.Url.ToString().ToLower().Contains("account/myaccount.aspx")
                    '###############################################
                    'GET THE GENERALIZED CONTENT, NOT USER SPECIFIC
                    '###############################################
                    ltrContent.Text = ContentManager.Instance.GetContent(ContentKey, 1, OwnerType.Other)
                Case Request.Url.ToString().ToLower().Contains("account/manageemailaccounts.aspx")
                    '###############################################
                    'GET THE GENERALIZED CONTENT, NOT USER SPECIFIC
                    '###############################################
                    ltrContent.Text = ContentManager.Instance.GetContent(ContentKey, 1, OwnerType.Other)
                Case Request.Url.ToString().ToLower().Contains("account/highspeedsecurity.aspx")
                    '###############################################
                    'GET THE GENERALIZED CONTENT, NOT USER SPECIFIC
                    '###############################################
                    ltrContent.Text = ContentManager.Instance.GetContent(ContentKey, 1, OwnerType.Other)
                Case Request.Url.ToString().ToLower().Contains("account/managelogin.aspx")
                    '###############################################
                    'GET THE GENERALIZED CONTENT, NOT USER SPECIFIC
                    '###############################################
                    ltrContent.Text = ContentManager.Instance.GetContent(ContentKey, 1, OwnerType.Other)
                Case Request.Url.ToString().ToLower().Contains("account/managewebspace.aspx")
                    '###############################################
                    'GET THE GENERALIZED CONTENT, NOT USER SPECIFIC
                    '###############################################
                    ltrContent.Text = ContentManager.Instance.GetContent(ContentKey, 1, OwnerType.Other)
                Case Request.Url.ToString().ToLower().Contains("visitors/products.aspx")
                    '###############################################
                    'GET THE GENERALIZED CONTENT, NOT USER SPECIFIC
                    '###############################################
                    ltrContent.Text = ContentManager.Instance.GetContent(ContentKey, 1, OwnerType.Other)
                Case Else
                    '#########################################
                    'GET THE SESSION SPECIFIC CONTENT RECORD.
                    '#########################################
                    ltrContent.Text = ContentManager.Instance.GetContent(ContentKey, 1, OwnerType.Other)
            End Select
            '#############################################################################


            '########################################################################
            'MODIFICATION   ROD COPELAND    DIGITAL REPACKAGING     OCT 21, 2008
            'ADDITIONAL MOD FOR ISSUE ID 4819
            '########################################################################
            'THIS SECTION OF CODE IS INTENDED TO SUPPORT THE EXTERNAL LINKING TO THE 
            'BUNDLE BUILDER THAT HAS BEEN HOSTED IN KNOTICE'S DOMAIN. WE'RE TO PASS 
            'THE PARMS OF ADDRESS IN THE QUERYSTRING.  THE LINK AND PARMS ARE TO BE 
            'HOSTED IN THE CMS AS A CONTENT RECORD, WE WILL REPLACE THE PLACEHOLDERS 
            'IN THE STRING WITH THE RUNTIME ADDRESS VALUES.
            '########################################################################
            If Me.ContentKey.ToLower().Contains("offer:allproducts:buildyourown") Then
                If Not (ltrContent.Text Is Nothing) Then
                    If (ltrContent.Text.Trim() <> "") Then
                        Dim _dataSource As IAddress = AccountManager.Instance.GetAccount.Address
                        Dim street1 As String = _dataSource.Street1
                        Dim street2 As String = _dataSource.Street2
                        Dim zip As String = _dataSource.Zip

                        If Not (street1 Is Nothing) Then street1 = Server.HtmlEncode(street1)
                        If Not (street2 Is Nothing) Then street2 = Server.HtmlEncode(street2)
                        If Not (zip Is Nothing) Then zip = Server.HtmlEncode(zip)

                        If Not (_dataSource Is Nothing) Then
                            ltrContent.Text = ltrContent.Text.Replace("{StreetAddress}", street1)
                            ltrContent.Text = ltrContent.Text.Replace("{Apt}", street2)
                            ltrContent.Text = ltrContent.Text.Replace("{Zipcode}", zip)
                        Else
                            '###################################################################
                            'IN THE EVENT THAT THE ADDRESS CANT BE COLLECTED AND PASSED TO 
                            'THE BUNDLE BUILDER, JUST STRIP OUT THE ADDRESS VALUE PLACEHOLDERS 
                            'SO THE EMPTY PARMS WILL BE SENT IN THE QUERYSTRING.
                            '###################################################################
                            ltrContent.Text = ltrContent.Text.Replace("{StreetAddress}", "")
                            ltrContent.Text = ltrContent.Text.Replace("{Apt}", "")
                            ltrContent.Text = ltrContent.Text.Replace("{Zipcode}", "")
                        End If
                    End If
                End If
            ElseIf Not Page.IsPostBack And ContentKey.ToLower.Contains("ccs:ms:landingpage:mainbody") Then

                '########################################################################
                'MODIFICATION   SHEILA SHAHAPRI                     Sept 14, 2009
                'MOD TO ACCOUNT FOR ADDITION OF NEW INTERNET TOOLS CMS ZONE
                '########################################################################
                'THE CCS:MS:LANDINGPAGE:MAINBODY CMS ZONE IS INTENDED TO BE USED ON THE 
                'INTERNETTOOLS.ASPX PAGE.  IT CONTAINS AN OSSAUTHSTRING TOKEN TO BE USED
                'AS A PLACEHOLDER FOR THE OSS SYSTEM'S ENCRYPTED AUTHENTICATION STRING
                'THAT IS USED FOR SINGLE SIGN-ON IMPLEMENTATION.  THIS ENCRYPTED STRING 
                'IS CURRENTLY RETRIEVED VIA A HTTP WEB CALL TO THE OSS API WEB SERVICE.
                'IF THE STRING IS SUCCESSFULLY RETRIEVED, THE {OSSAuthStRING} TOKEN IS 
                'REPLACED WITH THIS VALUE.
                '########################################################################

                If ltrContent.Text.Contains("{OSSAuthString}") Then
                    Dim authString As String
                    Dim ipChecker As New Utilities.IPRestriction

                    Dim sourceIPAddress As String = ipChecker.getF5SourceIP()
                    If sourceIPAddress Is Nothing Then
                        Select Case True
                            Case HttpContext.Current.Request.Url.ToString().ToLower().Contains("cc2uat")
                                '####################################################################
                                'SKIP OVER AS THE F5 PROVIDED SOURCE IP SHOULD BE USED IN THIS CASE
                                '####################################################################
                            Case HttpContext.Current.Request.Url.ToString().ToLower().Contains("charter.com")
                                '####################################################################
                                'SKIP OVER AS THE F5 PROVIDED SOURCE IP SHOULD BE USED IN THIS CASE
                                '####################################################################
                            Case Else
                                '###########################################################################
                                'THIS SHOULD BE WE APPROACHING THE BOXES DIRECTLY, IN THAT CASE, 
                                'WE DONT NEED THE F5 PROVIDED SOURCE IP, JUST USE THE NORMAL REMOTE ADDRESS.
                                '###########################################################################
                                sourceIPAddress = HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")
                        End Select
                    End If
                    Try
                        authString = AccountManager.Instance.GetOSSAuthString(sourceIPAddress)
                    Catch ex As Exception
                    End Try
                    If authString IsNot Nothing Then
                        ltrContent.Text = ltrContent.Text.Replace("{OSSAuthString}", authString)
                    End If

                End If


            End If
            '########################################################################


            If ltrContent.Text = String.Empty Then
                ltrContent.Text = "ControlID : " & ContentKey
            End If

            txtContentKey.Value = ContentKey
        End If

    End Sub
End Class