Attribute VB_Name = "LogInLogOut"
Private Headers As String
Private LoginData As String

Public Function LogOut()

FrmConnect.cmd_logout.Enabled = False
FrmPackets.txt_packetlogs_dec.Text = ""
FrmPackets.txt_packetlogs_enc.Text = ""
FrmBrowser.Hide
FrmConnect.txt_status.Text = "Status: Logged out"
FrmConnect.cbo_hotel.Enabled = True
FrmConnect.txt_username.Enabled = True
FrmConnect.txt_password.Enabled = True
FrmConnect.cmd_logout.Enabled = False
FrmConnect.Show
FrmConnect.txt_status.Text = "Status: Logging out..."
' Navigate to LogOut
GetUrlSource (FrmConnect.Hotel & "/account/logout")
FrmConnect.txt_status.Text = "Status: Logged out"
GetUrlSource (FrmConnect.Hotel & "/account/logout_ok")

End Function

Public Function LogIn(Hotel As String, Username As String, Password As String)

Dim HTTPS_Hotel As String
Dim Source As String
Dim PageTicket As String
    
    FrmConnect.Hotel = Hotel
    FrmConnect.Username = Username
    FrmConnect.Password = Password
    
        ' Write to hostsfile
        If fileexists(GetWinDir & "\system32\drivers\etc\hosts") = False Then
        txt_WriteAll GetWinDir & "\system32\drivers\etc\hosts", "# Copyright (c) 1993-1999 Microsoft Corp."
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "#"
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "# Dies ist eine HOSTS-Beispieldatei, die von Microsoft TCP/IP"
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "# für Windows 2000 verwendet wird."
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "#"
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "# Diese Datei enthält die Zuordnungen der IP-Adressen zu Hostnamen."
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "# Jeder Eintrag muss in einer eigenen Zeile stehen. Die IP-"
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "# Adresse sollte in der ersten Spalte gefolgt vom zugehörigen"
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "# Hostnamen stehen."
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "# Die IP-Adresse und der Hostname müssen durch mindestens ein"
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "# Leerzeichen getrennt sein."
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "#"
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "# Zusätzliche Kommentare (so wie in dieser Datei) können in"
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "# einzelnen Zeilen oder hinter dem Computernamen eingefügt werden,"
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "# aber müssen mit dem Zeichen '#' eingegeben werden."
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "#"
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "# Zum Beispiel:"
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "#"
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "#      102.54.94.97     rhino.acme.com          # Quellserver"
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "#       38.25.63.10     x.acme.com              # x-Clienthost"
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", ""
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", "127.0.0.1       localhost"
        End If
        
        ' Check if there's already an entry for SnGScriptConsole
        If StrCount(txt_ReadAll(GetWinDir & "\system32\drivers\etc\hosts"), "127.0.0.1" & "       " & "SnGScriptConsole") < 1 Then
            ' Check WriteProtection of hostsfile
            If SchreibschutzEntfernen(GetWinDir & "\system32\drivers\etc\hosts") = False Then
            FrmConnect.txt_status.Text = "Status: " & GetWinDir & "\system32\drivers\etc\hosts is write-protected. Can't write new host to it."
            Exit Function
            End If
        ' Writing new Host to hosts file
        FrmConnect.txt_status.Text = "Status: Writing new host to hosts file..."
        txt_AppendLine GetWinDir & "\system32\drivers\etc\hosts", vbNewLine & "127.0.0.1" & "       " & "SnGScriptConsole"
        FrmConnect.txt_status.Text = "Status: Wrote new host to hosts file"
        End If
        
        ' Set Habbo Browser
        FrmBrowser.Hide
        FrmBrowser.HabboBrowser.Silent = True
    
        ' LogOut
        GetUrlSource (FrmConnect.Hotel & "/account/logout")
        FrmConnect.txt_status.Text = "Status: Logged out"
        GetUrlSource (FrmConnect.Hotel & "/account/logout_ok")
        
        ' Split HTTPS Hotel
        HTTPS_Hotel = Split(Hotel, "http://www.")(1)
        HTTPS_Hotel = "https://www." & HTTPS_Hotel
        
        'Gets the Html source
        Source = GetUrlSource(HTTPS_Hotel & "/account/login?service=%2Fsecurity_check")
        
        ' If redirect instead of Login Page
        If InStr(Source, "URL has moved") Then
        GetUrlSource (FrmConnect.Hotel & "/account/logout")
        FrmConnect.txt_status.Text = "Status: Logged out"
        SleepUntil (2000)
        Source = GetUrlSource(HTTPS_Hotel & "/account/login?service=%2Fsecurity_check")
        End If
        
        'Reauthentication
        If InStr(GetUrlSource(Hotel & "/client"), "reauthenticate") Then
        FrmConnect.txt_status.Text = "Reauthentication..."
        GetUrlSource (HTTPS_Hotel & "/account/reauthenticate?page=/&credentials.password=" & Password)
            ' Check if LogIn was successfully
            If InStr(GetUrlSource(Hotel & "/client"), "login-submit") Then
            ' Login failed
            GoTo LoginFailed:
            Else
            ' Login successfully
            ' Create Loader
            Call FrmBrowser.SplitIt(Hotel & "/client")
            ' Adjust Habbo Browser
            FrmBrowser.HabboBrowser.Height = 8643
            FrmBrowser.HabboBrowser.Width = 15008
            ' Clear Packetlogs
            FrmPackets.txt_packetlogs_dec.Text = ""
            FrmPackets.txt_packetlogs_enc.Text = ""
                ' If an old Loader exists, delete it
                If fileexists(App.path & "\" & "login.html") = True Then
                fDelete App.path & "\" & "login.html", False, False
                End If
            Exit Function
            End If
        End If
        
        ' LogIn
        LoginData = "page=/&service=/security_check&credentials.username=" & Username & "&credentials.password=" & Password
        GetUrlSource (HTTPS_Hotel & "/account/submit?" & LoginData)
        
        ' Check if LogIn was successfully
        If InStr(GetUrlSource(Hotel & "/client"), "login-submit") Then
        ' Login failed
        GoTo LoginFailed:
        Else
        ' Login successfully
        ' Create Loader
        Call FrmBrowser.SplitIt(Hotel & "/client")
        ' Adjust Habbo Browser
        FrmBrowser.HabboBrowser.Height = 8643
        FrmBrowser.HabboBrowser.Width = 15008
        ' Clear Packetlogs
        FrmPackets.txt_packetlogs_dec.Text = ""
        FrmPackets.txt_packetlogs_enc.Text = ""
            ' If an old Loader exists, delete it
            If fileexists(App.path & "\" & "login.html") = True Then
            fDelete App.path & "\" & "login.html", False, False
            End If
        Exit Function
        End If
    
LoginFailed:
        FrmConnect.txt_status.Text = "Status: Login failed..."
        FrmBrowser.Hide
        FrmConnect.cbo_hotel.Enabled = True
        FrmConnect.txt_username.Enabled = True
        FrmConnect.txt_password.Enabled = True
        FrmConnect.cmd_logout.Enabled = False
        FrmConnect.Show
        Exit Function
    
End Function
