#Region "asp2dotnet converter header"
' File Protected: false
#End Region

Public Class clsMessageVAR

    Inherits PageClass

#Region "Global Variables"

    ' ==============================================================================================
    ' '' THESE are the ONLY settings that should be adjusted in the ASP file.
    ' '' Everything else is in the Access Database
    ' '' ------ BEGIN ADJUSTMENTS ALLOWED
    Public Property BDebug As Object

    ' toggle this to redirect incoming http:// requests to https://
    Public Property BUseSSL As Object

    ' toggle this, and set the appropriate variables, to toggle from sSqlString or ACCESS
    Public Property BUsesSqlString As Object

    ' bUsesSqlString = false
    ' toggle this to true to take the application offline for maintenance
    Public Property BOffline As Object

    ' bOffline = true
    ' sSqlString server connection information
    Public Property sSqlStringSERVER As Object
    Public Property sSqlStringDBNAME As Object
    Public Property sSqlStringUSER As Object
    Public Property sSqlStringPW As Object

    ' Active Directory service account for ADsDSOObject
    Public Property uAccessId As Object
    Public Property uAccessPassword As Object
    Public Property GProgrammer As Object
    Public Property AdminEmailAddress As Object
    Public Property WebURL As Object
    Public Property WebAdminURL As Object
    Public Property UserVISN As Object
    Public Property DCPath As Object
    Public Property WebName As Object
    Public Property EmailActive As Object
    Public Property EmailServer As Object
    Public Property NewURL As Object
    Public Property HttpHost As Object
    Public Property HttpURL As Object
    Public Property HttpQS As Object
    Public Property NumSec As Object
    Public Property Gl_overrideLName As Boolean
    Public Property Gl_overrideFName As Boolean
    Public Property Gl_overrideL4 As Boolean
    Public Property Gl_overridePhone As Boolean
    Public Property Gl_overrideRCall As Boolean
    Public Property Gl_overrideReason As Boolean
    Public Property Gl_overrideComment As Boolean
    Public Property Gl_overrideDOB As Boolean
    Public Property Gl_overrideTeamColorize As Boolean

    ' database variables
    Private Property Db As Object
    Private Property sSqlString As Object
    Private Property Rs As Object
    Public Property StrConnect As Object
    Public Property StrServerName As Object
    Public Property StrDatabaseName As Object
    Public Property StrUID As Object
    Public Property StrPWD As Object
    Public Property Path As Object
    Private Property Fso As Object
#End Region

#Region "Used Modules"
    Private __Vb6 As Vb6 = Nothing
    Private ReadOnly Property _Vb6 As Vb6
        Get
            If __Vb6 Is Nothing Then
                __Vb6 = createInstance(GetType(Vb6))
            End If
            Return __Vb6
        End Get
    End Property
#End Region


    Sub ResetAppPool()
        Dim appScript
        Dim appPath, appPoolName

        ' IIS server is server 2003 or server 2008
        Dim IISSERVER
        'IISSERVER = "2003"
        IISSERVER = "2008"

        appPath = Server.MapPath("message.aspx")
        Select Case UCase(appPath)
            ' Create a Case statement to reflect the appropriate IIS Application Pool name for each ASP file.

            ' vhamadwebphone Test
            Case "D:\WWWTEST\MESSAGE\TEST3\MESSAGE.ASP"
                appPoolName = "Test"
            Case "D:\WWWTEST\MESSAGE\TEST3\MESSAGEADMIN\MESSAGE.ASP"
                appPoolName = "Test"

            ' vhamadwebmsg Production
            Case "E:\WWWROOT\VISN23\APPLICATIONS\MESSAGEMANAGER\MESSAGE.ASPX"
                appPoolName = "MessageManager"

            ' vhamadwebmsg Test
            Case "D:\WWWTEST\TEST\MESSAGE.ASP"
                appPoolName = "MessageTestAppPool"
            Case "D:\WWWTEST\TEST\MESSAGEADMIN\MESSAGE.ASP"
                appPoolName = "MessageTestAppPool"

            ' vhamadwebmsg Test2
            Case "D:\WWWTEST\TEST2\MESSAGE.ASP"
                appPoolName = "MessageTestAppPool"
            Case "D:\WWWTEST\TEST2\MESSAGEADMIN\MESSAGE.ASP"
                appPoolName = "MessageTestAppPool"

            ' vhamadwebmsg test3
            Case "D:\WWWTEST\TEST3\MESSAGE.ASP"
                appPoolName = "MessageTestAppPool"
            Case "D:\WWWTEST\TEST3\MESSAGEADMIN\MESSAGE.ASP"
                appPoolName = "MessageTestAppPool"

            ' vhamadwebmsg demo
            Case "D:\WWWTEST\DEMO\MESSAGE.ASP"
                appPoolName = "MessageTestAppPool"
            Case "D:\WWWTEST\DEMO\MESSAGEADMIN\MESSAGE.ASP"
                appPoolName = "MessageTestAppPool"

            Case Else
                appPoolName = "MessageManager.Net"

        End Select

        ' 2003:
        ' cscript //NoLogo c:\windows\system32\iisapp.vbs /a [APPPOOLNAME] /r
        If IISSERVER = "2003" Then
            appScript = "cscript //NoLogo c:\windows\system32\iisapp.vbs /a " & appPoolName & " /r"
        Else
            ' 2008:    
            '   C:\Windows\System32\inetsrv>appcmd list sites
            '   SITE "MessageTest" (id:1,bindings:http/10.102.1.12:80:,state:Started)
            '   SITE "Message" (id:2,bindings:http/*:80:,state:Started)
            '
            '   C:\Windows\System32\inetsrv>appcmd list apppool
            '   APPPOOL "MessageTestAppPool" (MgdVersion:,MgdMode:Integrated,state:Started)
            '   APPPOOL "MessageAppPool" (MgdVersion:,MgdMode:Integrated,state:Started)
            appScript = "c:\windows\system32\inetsrv\appcmd.exe recycle apppool " & appPoolName
        End If


        Dim wshell
        On Error Resume Next
        wshell = Vb6.CreateObject("wscript.shell")
        If Err.Number <> 0 Then
            Response_WriteLine("AppPool Failure. Could not create wscript.schell for Application Pool """ & appPoolName & """.")
        Else
            On Error Resume Next
            wshell.run(appScript)
            If Err.Number = 0 Then
                Response_WriteLine("AppPool Success. Application Pool """ & appPoolName & """ Reset.  Hit REFRESH")
            Else
                Response_WriteLine("AppPool Failure.<br>")
                Response_WriteLine("			" & Err.Number & ": " & Err.Description & "<br>")
                Response_WriteLine("			Application Pool """ & appPoolName & """ Not Reset.")
                ' try again through the batch file
                On Error Resume Next
                wshell.run("c:\iis-RecycleAP.cmd")
                If Err.Number = 0 Then
                    Response_WriteLine("AppPool 2nd try Success. Application Pool Reset from iis-RecycleAP.cmd batch file.  Hit REFRESH")
                Else
                    Response_WriteLine("AppPool 2nd try Failure.<br>")
                    Response_WriteLine("				" & Err.Number & ": " & Err.Description & "<br>")
                    Response_WriteLine("				Application Pool """ & appPoolName & """ Not Reset.")
                End If
            End If
        End If
        On Error GoTo 0
        wshell = Nothing
    End Sub


    '************************************************
    '
    ' Sub: New
    ' Sub is called when the class is instantiated 
    '
    '************************************************
    Sub New(Page As AspPage)

        MyBase.New(Page)

        'BDebug = True
        Me.BDebug = False
        'bUseSSL = true
        Me.BUseSSL = False
        Me.BUsesSqlString = True
        Me.BOffline = False
        Me.sSqlStringSERVER = "WIN-EVP1LB6MC7J"
        Me.sSqlStringDBNAME = "MM"
        Me.sSqlStringUSER = "sa"
        Me.sSqlStringPW = "!pa$$word1"

        Me.uAccessId = "VHA12\VHAMADMSGSVC"
        Me.uAccessPassword = "SaveFiles3!2026"
        Me.GProgrammer = "timothy.dempsey2@va.gov" ' do not adjust this value, it's for emailing error trap debug info to developer
        Me.AdminEmailAddress = "V17MessageMgr@va.gov"
        Me.WebURL = "http://localhost:56764/"
        Me.WebAdminURL = "http://localhost:56764/messageadmin/"
        Me.WebName = "VISN 23 Phone Message Manager"
        Me.EmailActive = "Yes"
        ' SMTP SERVERS: "smtp.va.gov", "vhav12msga1.v12.med.va.gov", "vhaishexci", "vhav12msga1"
        Me.EmailServer = "smtp.va.gov"
        ' ------ END ADJUSTMENTS ALLOWED
        ' ==============================================================================================
        If Me.BOffline = True Then
            Response_WriteLine("<HTML>")
            Response_WriteLine("<HEAD>")
            Response_WriteLine("<title>Message Manager Temporarily Offline for Maintenance</title>")
            Response_WriteLine("</HEAD>")
            Response_WriteLine("<BODY>")
            Response_WriteLine("<p><center>")
            Response_WriteLine("<big>Message Manager temporarily offline for maintenance.  Check back in a few minutes.</big><br>")
            Response_WriteLine("<br>")
            Response_WriteLine("<br>")
            Response_WriteLine("<a href=""MessageManagerOfflineTemplate.docx"">Click Here to print Offline Message Template</a> and submit later when system online.")
            Response_WriteLine("</center></p>")
            Response_WriteLine("</BODY>")
            Response_WriteLine("</HTML>")
            ' terminate script processing
            Response.End()
        End If
        If Me.BUseSSL = True Then
            If Request.ServerVariables("HTTPS") = "off" Or Request.ServerVariables("SERVER_PORT") = "80" Or Request.ServerVariables("SERVER_PORT_SECURE") = 0 Then
                ' NOT secure so redirect
                ' SSL will be:
                '    HTTPS = "on"
                '    SERVER_PORT = "443"
                '    SERVER_PORT_SECURE = "0"

                ' redirect OLD:  http://xxx.yyy.med.va.gov/message.asp
                '       to NEW:  https://xxx.yyy.med.va.gov/message.asp
                ' redirect OLD:  http://xxx.yyy.med.va.gov/messageAdmin/message.asp?A=Admin&S=537&I=63640
                '       to NEW:  https://xxx.yyy.med.va.gov/messageAdmin/message.asp?A=Admin&S=537&I=63640
                ' compute new URL
                ' newURL = "https://" & httpHost & "/" & httpURL & "?" & httpQS

                ' Sample: httpHost = "vhamadwebx" or "message.madison.med.va.gov"
                Me.HttpHost = Request.ServerVariables("HTTP_HOST")
                ' V12 specific URL redirection:
                If Me.HttpHost = "message" Then
                    Me.HttpHost = "message.madison.med.va.gov"
                End If
                If Me.HttpHost = "phonetest" Then
                    Me.HttpHost = "phonetest.madison.med.va.gov"
                End If
                If Me.HttpHost = "message.visn12.med.va.gov" Then
                    Me.HttpHost = "message.madison.med.va.gov"
                End If
                ' Sample: httpURL = "" or "/message.aspx" or "/messageAdmin/message.asp"
                Me.HttpURL = Request.ServerVariables("URL")
                ' Sample: httpQS = "" or "A=Admin&S=537&I=63640"
                Me.HttpQS = Request.QueryString.ToString()
                If Me.HttpHost = "" Then
                    Me.NewURL = Replace(Me.WebURL, "http://", "https://")
                Else
                    Me.NewURL = "https://" & Me.HttpHost
                    If Me.HttpURL = "" Then
                        ' fine as it is
                    Else
                        Me.NewURL = Me.NewURL & Me.HttpURL
                        If Me.HttpQS = "" Then
                            ' fine as it is
                        Else
                            Me.NewURL = Me.NewURL & "?" & Me.HttpQS
                        End If
                    End If
                End If
                Me.NumSec = 1
                Response_WriteLine("<HTML>")
                Response_WriteLine("<HEAD>")
                Response_WriteLine("<META HTTP-EQUIV=""refresh"" CONTENT=""" & Me.NumSec & ";URL=" & Me.NewURL & """>")
                Response_WriteLine("</HEAD>")
                Response_WriteLine("<BODY>")
                Response_WriteLine("<p><center>")
                Response_WriteLine("<big>This website requires SSL.</big><br>")
                Response_WriteLine("<br>")
                Response_WriteLine("Redirecting to:<br>")
                Response_WriteLine("<a href=""" & Me.NewURL & """>" & Me.NewURL & "</a>")
                Response_WriteLine("</center></p>")
                Response_WriteLine("</BODY>")
                Response_WriteLine("</HTML>")
                ' terminate script processing
                Response.End()

            Else
                ' port is secure, do not redirect
            End If ' SERVER_PORT = "80"
        End If ' bUseSSL = true

        If Me.BUsesSqlString = True Then
            ' sSqlString database setup
            ' http://databases.aspfaq.com/database/what-should-my-connection-string-look-like.html
            Db = CreateObject("ADODB.Connection")

            ' Josh's sample mehtod:	
            'Set cnSched = CreateObject( "ADODB.connection" )
            'cnSched.Open "Provider=sSqlStringOLEDB;Data Source=xxxxxxx;Initial Catalog=Scheduler;User ID=xxxxxx;Password=xxxxxx;"

            'strConnect = "Provider=sSqlStringOLEDB;Data Source=" & sSqlStringSERVER & ";Initial Catalog=" & sSqlStringDBNAME & ";User ID=" & sSqlStringUSER & ";Password=" & sSqlStringPW & ";"
            'strConnect = "Driver={sSqlString Server}; Server=xxxxxxxx; Database=xxxxxxxx; UID=xxxxxxx; PWD=xxxxxxx"
            ' http://msdn.microsoft.com/en-us/library/ms130978.aspx
            ' nVarChar(MAX) may have issues with ADO if field names are referenced in out-of-order calls
            ' Working String
            Me.StrConnect = "Driver={sSqlString Server}; Server=" & Me.sSqlStringSERVER & "; Database=" & Me.sSqlStringDBNAME & "; UID=" & Me.sSqlStringUSER & "; PWD=" & Me.sSqlStringPW

            On Error Resume Next
            Db.Open(Me.StrConnect)
            If Err.Number <> 0 Then
                ErrorHandler(Err.Number & ": " & Err.Description & "<br><br>The sSqlString database connection is failing<br><br>", True, True)
            End If
            On Error GoTo 0

        Else
            ' Access database setup

            ' PER http://support.microsoft.com/kb/299973/en-us
            ' When you run Microsoft Jet in an IIS environment, 
            ' it is recommended that you use the native Jet OLE DB Provider 
            ' in place of the Microsoft Access ODBC driver.
            Me.Path = Server.MapPath("Message90210.mdb")
            Fso = Vb6.CreateObject("Scripting.FileSystemObject")
            If Fso.FileExists(Me.Path) Then
                ' good - local path
            Else
                Me.Path = Server.MapPath("MessageAdmin\Message90210.mdb")
            End If
            Fso = Nothing

            Db = CreateObject("ADODB.Connection")
            On Error Resume Next
            ' ACCESS way which has limits:
            '   db.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & path
            ' JETDB way which is better:
            '   "Provider=Microsoft.Jet.OLEDB.4.0;" &     '         "Data Source=C:\...\JetPassword.MDB;" &     '         "Jet OLEDB:Database Password=MyPwd"
            Db.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.Path & ";Jet OLEDB")
            If Err.Number <> 0 Then
                ErrorHandler(Err.Number & ": " & Err.Description & "<br><br>The IIS account needs read/write access to the database folder, or .mdb file missing<br><br>", True, True)
            End If
            On Error GoTo 0
        End If
    End Sub

    Private Sub ErrorHandler(v1 As String, v2 As Boolean, v3 As Boolean)
        Throw New NotImplementedException()
    End Sub

    Protected Overrides Sub Finalize()
    End Sub

End Class
