Imports System.Net
Imports System.IO

Namespace PCSolution

    Public Class HTML
        Function OdstranitHTMLzTXT(ByVal str As String) As String
            'Odstrani HTML tagy z retezce
            Dim cA As Char
            Dim strA As String = ""
            Dim bNezapisovat As Boolean = False

            For Each cA In str
                If cA.ToString = "<" Then bNezapisovat = True 'Jestlize jsme v tagu, tak nezapisovat do retezce
                If bNezapisovat = False Then strA = strA + cA.ToString 'Retezec
                If cA.ToString = ">" Then bNezapisovat = False 'Jestlize tag skonci, je povoleno zapisovat
            Next
            Return strA
        End Function

        Function OdstranitFontZHTML(ByVal str As String) As String
            Dim s As String
            Dim n As Integer
            Dim iDelStart, iDelEnd As Integer 'Od jakeho do jakeho znaku se ma vymazat text

            Do
                s = str.ToLower
                n = s.IndexOf("font-family:")
                iDelStart = n
                If n = -1 Then Exit Do
                Dim c As String = ""
                Do
                    If n = s.Length Then Exit Do
                    c = s.Substring(n, 1)
                    If c = """" Or c = ";" Then
                        Exit Do
                    End If
                    n += 1
                Loop
                If c = ";" Then
                    iDelEnd = n + 1
                Else
                    iDelEnd = n
                End If
                str = str.Remove(iDelStart, iDelEnd - iDelStart)
            Loop

            Do
                s = str.ToLower
                n = s.IndexOf("<font face")
                iDelStart = n
                If n = -1 Then Exit Do
                Dim c As String = ""
                Do
                    If n = s.Length Then Exit Do
                    c = s.Substring(n, 1)
                    If c = ">" Then
                        Exit Do
                    End If
                    n += 1
                Loop
                If c = ">" Then
                    iDelEnd = n + 1
                End If
                str = str.Remove(iDelStart, iDelEnd - iDelStart)
            Loop

            Do
                s = str.ToLower
                n = s.IndexOf("<font size")
                iDelStart = n
                If n = -1 Then Exit Do
                Dim c As String = ""
                Do
                    If n = s.Length Then Exit Do
                    c = s.Substring(n, 1)
                    If c = ">" Then
                        Exit Do
                    End If
                    n += 1
                Loop
                If c = ">" Then
                    iDelEnd = n + 1
                End If
                str = str.Remove(iDelStart, iDelEnd - iDelStart)
            Loop

            str.Replace("</font>", "")

            Do
                s = str.ToLower
                n = s.IndexOf("@font-face")
                iDelStart = n
                If n = -1 Then Exit Do
                Dim c As String = ""
                Do
                    If n = s.Length Then Exit Do
                    c = s.Substring(n, 1)
                    If c = "}" Then
                        Exit Do
                    End If
                    n += 1
                Loop
                If c = "}" Then
                    iDelEnd = n + 1
                End If
                str = str.Remove(iDelStart, iDelEnd - iDelStart)
            Loop

            Return str

        End Function

        ''' <summary>
        ''' Uloží požadovanou externí stránku do proměnné
        ''' </summary>
        ''' <param name="url">WWW adresa</param>
        ''' <returns>Řetězec s obsahem stránky</returns>
        ''' <remarks></remarks>
        Function SaveExternalHTMLPageToString(ByVal url As String) As String

            Dim ret As String = ""

            Dim objWebClient As New WebClient()
            Dim objUTF8 As New UTF8Encoding()
            Try
                ret = objUTF8.GetString(objWebClient.DownloadData(url))
                Return ret
            Catch
                Return ""
            End Try


        End Function

        ''' <summary>
        ''' Uloží požadovaný externí soubor do proměnné
        ''' </summary>
        ''' <param name="url">WWW adresa k souboru</param>
        ''' <param name="SaveFile">Do ktereho souboru se ma soubor z webu ulozit</param>
        ''' <remarks></remarks>
        Sub SaveExternalFileToString(ByVal url As String, ByVal SaveFile As String)

            Dim ret As String = ""

            Dim objWebClient As New WebClient()
            'Dim objUTF8 As New UTF7Encoding()
            Dim FS As FileStream
            Dim SW As BinaryWriter

            FS = New FileStream(SaveFile, FileMode.Create)
            SW = New BinaryWriter(FS)
            SW.Write(objWebClient.DownloadData(url))
            SW.Close() : FS.Close()

        End Sub

        ''' <summary>
        ''' Uloží požadovanou externí stránku zabezpečenou heslem do proměnné
        ''' </summary>
        ''' <param name="url">WWW adresa</param>
        ''' <returns>Řetězec s obsahem stránky</returns>
        ''' <remarks></remarks>
        Function SaveExternalHTMLPageWithPasswordToString(ByVal url As String, ByVal username As String, ByVal password As String, ByVal domain As String) As String

            Dim ret As String = ""

            Dim objWebClient As New WebClient()
            Dim objUTF8 As New UTF8Encoding()
            objWebClient.Credentials = New System.Net.NetworkCredential(username, password, domain)

            Try
                ret = objUTF8.GetString(objWebClient.DownloadData(url))
                Return ret
            Catch ex As Exception
                Return ex.ToString
                Return ""
            End Try


        End Function

    End Class

End Namespace