Imports System.Net
Imports System.IO

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Function IsValidMailAddress(address As String) As Boolean   'メールアドレスの有効性
        Try
            Dim a As New System.Net.Mail.MailAddress(address)
        Catch ex As FormatException
            'FormatExceptionがスローされた時は、正しくない
            Return False
        End Try
        Return True
    End Function

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        '送信ボタン
        If IsPostBack Then
            If String.IsNullOrEmpty(TextBoxMail.Text) Then LabelMsg.Text = "メールアドレスが入っていません" : Exit Sub
            If Not IsValidMailAddress(TextBoxMail.Text) Then LabelMsg.Text = "メールアドレスが間違っているようです" : Exit Sub
            If String.IsNullOrEmpty(TextBoxName.Text) Then LabelMsg.Text = "名前が入っていません" : Exit Sub
            Dim xSequrity As String = Format(Now, "hhyyyymmMMssddfff").ToString
            Session("date") = Now.ToString
            Session("name") = TextBoxName.Text
            Session("mail") = TextBoxMail.Text
            Session("IP") = System.Web.HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")
            Session("security") = xSequrity
            Session("status") = xSequrity 'セキュリティーコードの書込み
            Session("cookie") = ""
            SqlDataSourceAsMemberByCookie.Insert()

            LabelMsg.Text = "http://" & System.Net.Dns.GetHostName & "/Approve/SQCode.aspx?sqcode=" & xSequrity

        End If
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        'スマホ or PC
        If Request.Browser.IsMobileDevice Then
            PanelPC.Visible = False
            PanelMobile.Visible = True
        Else
            PanelPC.Visible = True
            PanelMobile.Visible = False
        End If

        '認証レベル
        Dim xCode As String() = IsStatus.Split(",") 'No,date,name,mail,IP,Status,cookie(Class:mail),sw(Level)
        Dim n As Integer = 0
        Try
            n = Integer.Parse(xCode(xCode.Length - 1))
        Catch ex As Exception
            Response.Redirect("../NotAllowed.aspx?msg=認証されていません DBに無し")
            Response.End()
        End Try
        Select Case n
            Case 0
                LabelApprove.Text = "認証されていません。"
            Case 1
                LabelApprove.Text = "会員として認証されています。"
            Case 3
                LabelApprove.Text = "管理者として認証されています。"
            Case 7
                LabelApprove.Text = "システムとして認証されています。"
            Case Else
                LabelApprove.Text = "不完全な認証です。"
        End Select

    End Sub

    'LocalHost Arranged
    Protected Function IsStatus() As String
        '管理者確認の問合せ  No,date,name,mail,IP,status,cookie,Level
        Dim xIP As String = Request.ServerVariables("REMOTE_ADDR")
        LabelDspIP.Text = xIP
        Dim xCookie As String = ""
        Dim html As String = "不明,0,0,0,0,0,0,0"
        If Request.Cookies("Local") IsNot Nothing Then
            xCookie = HttpUtility.HtmlDecode(Request.Cookies("Local").Value)
            LabelDspCookie.Text = xCookie
            Dim xTime As String = Format(Now, "hhmmss").ToString
            Dim x As String() = xCookie.Split(",")
            Dim enc As Encoding = Encoding.GetEncoding("UTF-8")
            Dim wc As WebClient = New WebClient()
            Dim host As String = System.Net.Dns.GetHostName
            Dim xPath As String = "http://" & host
            Dim st As Stream = wc.OpenRead(xPath & "/Approve/IsStatus.aspx?code=" & xTime & "," & xCookie & "&IP=" & xIP)
            Dim sr As StreamReader = New StreamReader(st, enc)
            html = sr.ReadToEnd()
            sr.Close()
            st.Close()
        End If
        Return html
    End Function

    Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
        If IsPostBack Then
            If String.IsNullOrEmpty(TextBoxMail.Text) Then LabelMsg.Text = "メールアドレスが必要です" : Exit Sub
            If Request.Cookies("Local") IsNot Nothing Then
                'クッキーの消込
                Response.Cookies("Local").Value = ""
                Response.Cookies("Local").Expires = DateTime.Now.AddDays(-3)
                Response.Cookies("AccessDateTime").Value = DateTime.Now.ToString()
                Response.Redirect(Request.Url.AbsoluteUri)
                Response.End()
            Else
                LabelMsg.Text = "認証を取り消しました"
                LabelApprove.Text = ""
            End If
        End If
    End Sub

    Protected Sub Button3_Click(sender As Object, e As System.EventArgs) Handles Button3.Click
        'クッキーだけの書込み
        If IsPostBack Then
            If String.IsNullOrEmpty(TextBoxMail.Text) Then LabelMsg.Text = "メールアドレスが入っていません" : Exit Sub
            If Not IsValidMailAddress(TextBoxMail.Text) Then LabelMsg.Text = "メールアドレスが間違っているようです" : Exit Sub
            LabelMsg.Text = ""
            LabelApprove.Text = ""
            '
            Dim xCookie As String = "Class_1" & ":" & TextBoxMail.Text
            'クッキーへの書込み
            Response.Cookies("Local").Value = xCookie
            Response.Cookies("Local").Expires = DateTime.Now.AddDays(365)
            Response.Cookies("AccessDateTime").Value = DateTime.Now.ToString()
            'クッキーの読込
            Dim xCookie2 As String = ""
            If Request.Cookies("Local") IsNot Nothing Then
                xCookie2 = Request.Cookies("Local").Value
                If xCookie = xCookie2 Then
                    LabelMsg.Text = "書込みに成功しました"
                Else
                    LabelMsg.Text = "書込みに失敗しました"
                End If
            End If

            '認証
            Dim xCode2 As String() = IsStatus.Split(",") 'No,date,name,mail,IP,Status,cookie(No,mail,date),sw(Level)
            Dim n As Integer = 0
            Try
                n = Integer.Parse(xCode2(xCode2.Length - 1))
            Catch ex As Exception

            End Try
            Select Case n
                Case 0
                    LabelApprove.Text = "認証されていません。"
                Case 1
                    LabelApprove.Text = "会員として認証されています。"
                Case 3
                    LabelApprove.Text = "管理者として認証されています。"
                Case 7
                    LabelApprove.Text = "システムとして認証されています。"
                Case Else
                    LabelApprove.Text = "不完全な認証です。"
            End Select
        End If
    End Sub

End Class
