Imports Microsoft.VisualBasic
Imports System.Net.Mail
Imports System.Net

Public Class CommonFunctions

    Public ReadOnly Property MetaTag(ByVal url As String) As String
        Get
            Return "<meta http-equiv=""Refresh"" content=""2;URL=" & url & """ />"
        End Get
    End Property

    Public Function SendEmail(ByVal fromAddress As String, ByVal toAddress As String, ByVal subject As String, ByVal body As String)

        Dim dMessage As New MailMessage
        Dim dSmtpClient As New SmtpClient

        'Dim dSmtpHost As String = "69.46.105.56"
        Dim dSmtpHost As String = "cliff.netsafemail.com"
        Dim dSmtpPort As Integer = 25

        Try
            dMessage.From = New MailAddress(fromAddress)
            dMessage.ReplyTo = New MailAddress(fromAddress)
            dMessage.To.Add(New MailAddress(toAddress))
            dMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
            dMessage.Priority = MailPriority.Normal
            dMessage.Subject = subject
            dMessage.BodyEncoding = System.Text.Encoding.ASCII
            dMessage.IsBodyHtml = True
            dMessage.Body = body
            dSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
            dSmtpClient.Credentials = New Net.NetworkCredential("aspnet", "!!epowersdc!!")
            dSmtpClient.Host = dSmtpHost
            dSmtpClient.Port = dSmtpPort
            'dSmtpClient.EnableSsl = True
            dSmtpClient.Send(dMessage)
        Catch ex As Exception
            Return False
        End Try

        Return True

    End Function


    Public Function FTPConnection()
        Try
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Function GetDocumentName(ByVal documentPath As String, ByVal includeFileExtension As Boolean, ByVal separator As String) As String
        Dim documentPathArray() As String = documentPath.Split(separator)
        If (includeFileExtension) Then
            Return documentPathArray(documentPathArray.Length - 1)
        Else
            Dim documentNameArray() As String = documentPathArray(documentPathArray.Length - 1).Split(".")
            Dim documentName As String = ""
            Dim i As Integer
            For i = 0 To documentNameArray.Length - 1
                If documentName = "" Then
                    documentName = documentNameArray(i)
                Else
                    documentName = documentName & "." & documentNameArray(i)
                End If
                i = i + 1
            Next
            Return documentName
        End If
    End Function

    Public Function GetDocumentNameAlt(ByVal documentPath As String, ByVal includeFileExtension As Boolean) As String
        Dim documentPathArray() As String = documentPath.Split("\\")
        If (includeFileExtension) Then
            Return documentPathArray(documentPathArray.Length - 1)
        Else
            Dim documentNameArray() As String = documentPathArray(documentPathArray.Length - 1).Split(".")
            Dim documentName As String = ""
            Dim i As Integer
            For i = 0 To documentNameArray.Length - 1
                If documentName = "" Then
                    documentName = documentNameArray(i)
                Else
                    documentName = documentName & "." & documentNameArray(i)
                End If
                i = i + 1
            Next
            Return documentName
        End If
    End Function

    Public Function TestFTP(ByVal _host As String, ByVal _port As String, ByVal _path As String, ByVal _userName As String, ByVal _password As String, ByVal _remoteFile As String, ByVal _localFile As String) As Boolean
        Try
            ' Declare all properties.
            Dim host As String = _host
            Dim port As String = _port
            Dim path As String = _path
            Dim username As String = _userName
            Dim password As String = _password
            Dim remoteFile As String = _remoteFile
            Dim ftp As FTPclient

            Dim localFile As String = _localFile

            If (Not System.IO.File.Exists(localFile)) Then
                Try
                    Dim _streamWriter As System.IO.StreamWriter
                    _streamWriter = System.IO.File.CreateText(localFile)
                    _streamWriter.WriteLine("test")
                    _streamWriter.Close()
                Catch ex As Exception
                    Throw New Exception("[100] " & localFile & " Local System Error, Please Contact Support!")
                End Try
            End If

            ' Create new FTPClient object. ( Connection. )
            Try
                ftp = New FTPclient(host & ":" & port, username, password)
            Catch ex As Exception
                Throw New Exception("[101] " & ex.Message)
            End Try

            ' Attempt to upload test file.
            Try
                If (Not ftp.Upload(localFile, path & "/" & remoteFile)) Then
                    Throw New Exception("[104] Unable to connect and upload test document.")
                End If
            Catch ex As Exception
                Throw New Exception("[102] " & ex.Message)
            End Try

            ' Attempt to delete test file.
            Try
                If (Not ftp.FtpDelete(path & "/" & remoteFile)) Then
                    Throw New Exception("[105] Unable to connect and upload test document.")
                End If
            Catch ex As Exception
                Throw New Exception("[103] " & ex.Message)
            End Try
        Catch ex As Exception
            Throw New Exception("[150] " & ex.Message)
        End Try
        Return True
    End Function

    Private ALPHABET As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_#." ' .,!@#$%^&*(){}[]?:;
    Private MIXED_ALPHABET As String = "rw6S8R730CmtYaEb#UuzknxqcPO.H4FKivVlA2L_9IGW1MoBhy-ZQXJdjTDgNpfse5" ' .}(]#,&;#]{?:*$^)%@

    Public Function Enciper(ByVal simpleMessage As String, ByVal key As String) As String
        Dim result As String = ""

        If (simpleMessage Is DBNull.Value Or simpleMessage = "") And (key Is DBNull.Value Or key = "") Then
            'TODO error message 
        Else

            Dim keyLenght As Integer = key.Length()
            Dim i As Integer
            For i = 0 To simpleMessage.Length - 1
                Dim k As Integer = ALPHABET.IndexOf(simpleMessage.Substring(i, 1))
                If k > -1 Then
                    Dim keyShift As Integer = ALPHABET.IndexOf(key.Substring(i Mod keyLenght, 1))
                    Dim ShiftedMixedAlphabet As String = MIXED_ALPHABET.Substring(keyShift) & MIXED_ALPHABET.Substring(0, keyShift)
                    result = result & ShiftedMixedAlphabet.Substring(k, 1)
                Else
                    result = result & simpleMessage.Substring(i, 1)
                End If
            Next

        End If
        Return result
    End Function


    Public Function Decipher(ByVal encodedMessage As String, ByVal key As String) As String
        Dim result As String = ""

        If (encodedMessage Is DBNull.Value Or encodedMessage = "") And (key Is DBNull.Value Or key = "") Then
            'TODO error message 
        Else
            Dim keyLenght As Integer = key.Length()
            Dim i As Integer
            For i = 0 To encodedMessage.Length - 1
                Dim keyShift As Integer = ALPHABET.IndexOf(key.Substring(i Mod keyLenght, 1))
                Dim ShiftedMixedAlphabet As String = MIXED_ALPHABET.Substring(keyShift) & MIXED_ALPHABET.Substring(0, keyShift)
                Dim k As Integer = ShiftedMixedAlphabet.IndexOf(encodedMessage.Substring(i, 1))
                If k > -1 Then
                    result = result & ALPHABET.Substring(k, 1)
                Else
                    result = result & encodedMessage.Substring(i, 1)
                End If
            Next
        End If
        Return result

    End Function

End Class


