Imports System.Net
Imports System.IO

Partial Class admin_Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'スマホ or PC
            If Request.Browser.IsMobileDevice Then
                vPage.Style.Add("width", "530px")
                PanelPC.Visible = False
                PanelMobile.Visible = True
            Else
                vPage.Style.Add("width", "1500px")
                PanelPC.Visible = True
                PanelMobile.Visible = False
            End If

            If Not IsPostBack Then
                'ユーザー認証
                Dim xIP As String = Request.ServerVariables("REMOTE_ADDR")
                LabelIP.Text = xIP
                Dim xCookie As String = ""
                If Request.Cookies("Local") IsNot Nothing Then
                    xCookie = HttpUtility.UrlDecode(Request.Cookies("Local").Value)
                    Session("cookie") = xCookie
                    Dim dt As Data.DataView = SqlDataSourceAdminByCookie.Select(DataSourceSelectArguments.Empty)
                    If dt IsNot Nothing AndAlso dt.Count <> 0 Then

                    Else
                        Response.Redirect("../NotAllowed.aspx?msg=システム管理者として認証されていません")
                        Response.End()
                    End If
                Else
                    If Not String.IsNullOrEmpty(Session("cookie")) Then
                        Dim dt As Data.DataView = SqlDataSourceAdminByCookie.Select(DataSourceSelectArguments.Empty)
                        If dt IsNot Nothing AndAlso dt.Count <> 0 Then
                            LabelMsg3.Text = "クッキー認証済み"
                        Else
                            Response.Redirect("../NotAllowed.aspx?msg=システム管理者として認証されていません")
                            Response.End()
                        End If
                    Else
                        Response.Redirect("../NotAllowed.aspx?msg=会員として認証されていません")
                        Response.End()
                    End If
                End If

                Dim ds As Data.DataView = SqlDataSourceMember.Select(DataSourceSelectArguments.Empty)
                LabelCount.Text = ds.Count.ToString
            End If
        End If

    End Sub

    Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
        If IsPostBack Then
            Dim dt As Data.DataView = SqlDataSourceMember.Select(DataSourceSelectArguments.Empty)
            Dim xdate As String = Format(Now, "yyyyMMdd").ToString
            Dim dtFilename As String = xdate & "_認証.csv"
            Dim dtPath As String = Server.MapPath("./Data/") & dtFilename
            Dim sw As New System.IO.StreamWriter(dtPath, False, System.Text.Encoding.GetEncoding("shift_jis"))
            Dim hData As String = "No.,日付,氏名,e-mail,IP,Status,Cookie1,Cookie2,Cookie3" & vbCrLf
            sw.Write(hData)
            For i = 0 To dt.Count - 1
                hData = ""
                hData = dt.Item(i).Row("id").ToString & ","
                hData += dt.Item(i).Row("date").ToString & ","
                hData += dt.Item(i).Row("name").ToString & ","
                hData += dt.Item(i).Row("mail").ToString & ","
                hData += dt.Item(i).Row("IP").ToString & ","
                hData += dt.Item(i).Row("status").ToString & ","
                hData += Replace(dt.Item(i).Row("cookie").ToString, ",", ";") & vbCrLf
                sw.Write(hData)
            Next
            sw.Close()

            If Request.Browser.Browser = "IE" Then
                dtFilename = HttpUtility.UrlEncode(dtFilename)
            End If
            Response.ContentType = "application/octet-stream"
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(dtFilename))
            Response.Flush()
            Response.TransmitFile(dtPath)
            Response.End()
        End If
    End Sub

    Protected Sub Button4_Click(sender As Object, e As System.EventArgs) Handles Button4.Click
        If IsPostBack Then
            If String.IsNullOrEmpty(TextBoxName.Text) Then LabelMsg3.Text = "名前が入ってません" : Exit Sub
            LabelMsg.Text = ""
            LabelMsg3.Text = ""
            Dim sw As Integer = 0
            If InStr(TextBoxName.Text, "@") <> 0 Then sw = 1 'Mail
            If Regex.IsMatch(TextBoxName.Text, "\d{1,3}(\.\d{1,3}){3}(/\d{1,2})?") Then sw = 2 'IP
            If IsNumeric(TextBoxName.Text) Then sw = 3 'id
            Select Case sw
                Case 1
                    Session("mail") = TextBoxName.Text
                    ListViewKaiin.DataSourceID = "SqlDataSourceMemberByMail"
                    ListViewKaiin.DataBind()
                Case 2
                    Session("IP") = TextBoxName.Text
                    ListViewKaiin.DataSourceID = "SqlDataSourceMemberByIP"
                    ListViewKaiin.DataBind()
                Case 3
                    Session("id") = TextBoxName.Text
                    ListViewKaiin.DataSourceID = "SqlDataSourceMemberByid"
                    ListViewKaiin.DataBind()
                Case Else 'Name
                    Session("name") = TextBoxName.Text
                    ListViewKaiin.DataSourceID = "SqlDataSourceMemberByName"
                    ListViewKaiin.DataBind()
            End Select
        End If
    End Sub

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        If IsPostBack Then
            If String.IsNullOrEmpty(TextBoxMail1.Text) Then LabelMsg.Text = "メールアドレスが入っていません" : Exit Sub
            LabelMsg.Text = ""
            LabelMsg3.Text = ""
            ListViewKaiin.DataSourceID = "SqlDataSourceKaiin"
            Session("mail") = TextBoxMail1.Text '認証された会員の情報を求める
            Dim dt As Data.DataView = SqlDataSourceMemberByMail.Select(DataSourceSelectArguments.Empty)
            If dt IsNot Nothing AndAlso dt.Count <> 0 Then
                Dim sw As Integer = 0
                For i = 0 To dt.Count - 1
                    If Not String.IsNullOrEmpty(dt.Item(i).Row("cookie").ToString) Then
                        Session("date") = Now.ToString
                        Session("name") = dt.Item(i).Row("name").ToString
                        Session("mail") = dt.Item(i).Row("mail").ToString
                        Session("IP") = dt.Item(i).Row("IP").ToString
                        Session("status") = dt.Item(i).Row("status").ToString
                        Session("cookie") = dt.Item(i).Row("cookie").ToString
                        Dim ds As Data.DataView = SqlDataSourceAdminByCookie.Select(DataSourceSelectArguments.Empty)
                        If ds IsNot Nothing AndAlso ds.Count <> 0 Then
                            Session("id") = ds.Item(0).Row("id").ToString
                            SqlDataSourceAdminByCookie.Update()
                            sw = 1
                            Exit For
                        Else
                            SqlDataSourceAdminByCookie.Insert()
                            sw = 2
                            Exit For
                        End If
                    End If
                Next
                If sw = 0 Then
                    LabelMsg.Text = "認証が完了していません" : Exit Sub
                End If
            Else
                LabelMsg.Text = "会員登録から見つけられませんでした"
            End If
        End If
    End Sub

    Protected Sub ListViewKaiin_ItemCommand(sender As Object, e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListViewKaiin.ItemCommand
        If IsPostBack Then
            LabelMsg3.Text = ""
            Dim xName As String = e.CommandName
            Dim xComm As String = e.CommandArgument
            If xName = "Sel" Then
                Session("id") = xComm
                Dim dt As Data.DataView = SqlDataSourceMemberByid.Select(DataSourceSelectArguments.Empty)
                If dt IsNot Nothing AndAlso dt.Count <> 0 Then
                    If Not String.IsNullOrEmpty(dt.Item(0).Row("cookie").ToString) Then
                        Dim enc As Encoding = Encoding.GetEncoding("UTF-8")
                        Dim wc As WebClient = New WebClient()
                        Dim xPath As String = "http://Local"
                        Dim st As Stream = wc.OpenRead(xPath & "/Log/IsStatus.aspx?code=" & dt.Item(0).Row("date").ToString & ";" & dt.Item(0).Row("cookie").ToString)
                        Dim sr As StreamReader = New StreamReader(st, enc)
                        Dim html As String = sr.ReadToEnd()
                        sr.Close()
                        st.Close()
                        LabelMsg3.Text = dt.Item(0).Row("name").ToString & " " & html
                    End If
                End If
            End If
        End If
    End Sub
End Class
