Imports System.Data
Imports System.Net
Imports System.IO

Partial Class system_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", "1400px")
                PanelPC.Visible = True
                PanelMobile.Visible = False
            End If

            If Not IsPostBack Then

                ''認証
                '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
                'If n >= 7 Then '
                'Else
                '    Response.Redirect("../NotAllowed.aspx?msg=認証されていません 資格が有りません")
                '    Response.End()
                'End If

            End If
        End If
    End Sub

    'LocalHost
    Protected Function IsStatus() As String
        '管理者確認の問合せ  No,date,name,mail,IP,status,cookie,Level
        Dim xIP As String = Request.ServerVariables("REMOTE_ADDR")
        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)
            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 & "/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 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 = ""
    '        LabelMsg2.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 = SqlDataSourceCertifyByCookie.Select(DataSourceSelectArguments.Empty)
    '                    If ds IsNot Nothing AndAlso ds.Count <> 0 Then
    '                        Session("id") = ds.Item(0).Row("id").ToString
    '                        SqlDataSourceCertify.Update()
    '                        sw = 1
    '                        Exit For
    '                    Else
    '                        SqlDataSourceCertify.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 Button3_Click(sender As Object, e As System.EventArgs) Handles Button3.Click
        If IsPostBack Then
            If String.IsNullOrEmpty(TextBoxMail2.Text) Then LabelMsg2.Text = "メールアドレスが入っていません" : Exit Sub
            'LabelMsg.Text = ""
            LabelMsg2.Text = ""
            LabelMsg3.Text = ""
            ListViewKaiin.DataSourceID = "SqlDataSourceMember"
            Session("mail") = TextBoxMail2.Text '認証された会員の情報を求める
            Session("name") = TextBoxMail2.Text
            Dim dt As Data.DataView = SqlDataSourceMemberByNameMail.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 = SqlDataSourceAdmin.Select(DataSourceSelectArguments.Empty)
                        If ds IsNot Nothing AndAlso ds.Count <> 0 Then
                            Session("id") = ds.Item(0).Row("id").ToString
                            SqlDataSourceAdmin.Update()
                            sw = 1
                            Exit For
                        Else
                            SqlDataSourceAdmin.Insert()
                            sw = 2
                            Exit For
                        End If
                    End If
                Next
                If sw = 0 Then
                    LabelMsg2.Text = "認証が完了していません" : Exit Sub
                End If
            Else
                LabelMsg2.Text = "会員登録から見つけられませんでした"
            End If
        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
            LabelMsg2.Text = ""
            LabelMsg3.Text = ""
            Session("name") = TextBoxName.Text
            Session("mail") = TextBoxName.Text
            ListViewKaiin.DataSourceID = "SqlDataSourceMemberByNameMail"
            ListViewKaiin.DataBind()
        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 Button5_Click(sender As Object, e As System.EventArgs) Handles Button5.Click
        If IsPostBack Then
            'クリーンアップ
            Dim xNow As Date = Now.AddMonths(-1) '有効期限1か月
            Dim dt As Data.DataView = SqlDataSourceMember.Select(DataSourceSelectArguments.Empty)
            If dt IsNot Nothing AndAlso dt.Count <> 0 Then
                Dim delSW As Integer = 0
                Dim delSX As Integer = 0
                Dim xName As String = dt.Item(0).Row("name").ToString
                For i = 0 To dt.Count - 1
                    For j = i To dt.Count - 1
                        If xName = dt.Item(j).Row("name").ToString Then
                            If String.IsNullOrEmpty(dt.Item(j).Row("cookie").ToString) Then
                                Dim xDate As Date = Date.Parse(dt.Item(j).Row("date").ToString)
                                If xDate < xNow Then
                                    '古い未認証
                                    Session("id") = dt.Item(j).Row("id").ToString
                                    SqlDataSourceMember.Delete()
                                End If
                            Else
                                If delSW = 0 Then
                                    delSW = 1
                                Else
                                    'ダブりの削除
                                    Session("id") = dt.Item(j).Row("id").ToString
                                    SqlDataSourceMember.Delete()
                                End If
                            End If
                        Else
                            '次の人
                            delSW = 0
                            xName = dt.Item(j).Row("name").ToString
                            i = j - 1
                            Exit For
                        End If
                    Next
                Next
            End If
            ListViewKaiin.DataBind()
        End If
    End Sub

    Protected Sub Button6_Click(sender As Object, e As System.EventArgs) Handles Button6.Click
        If IsPostBack Then
            'Member
            Dim cn As New SqlClient.SqlConnection
            Dim cd As New SqlClient.SqlCommand
            Dim SQL As String

            'データベース接続
            cn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
            cn.Open()
            'SQL文生成
            SQL = "TRUNCATE TABLE Member"
            cd.CommandText = SQL
            cd.Connection = cn

            cd.ExecuteNonQuery()
            'クローズ解放
            cd.Dispose()
            cn.Close()
            cn.Dispose()

            'Admin
            Dim cn2 As New SqlClient.SqlConnection
            Dim cd2 As New SqlClient.SqlCommand
            Dim SQL2 As String

            'データベース接続
            cn2.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
            cn2.Open()
            'SQL文生成
            SQL2 = "TRUNCATE TABLE Admin"
            cd2.CommandText = SQL2
            cd2.Connection = cn2

            cd2.ExecuteNonQuery()
            'クローズ解放
            cd2.Dispose()
            cn2.Close()
            cn2.Dispose()

            'System
            Dim cn3 As New SqlClient.SqlConnection
            Dim cd3 As New SqlClient.SqlCommand
            Dim SQL3 As String

            'データベース接続
            cn3.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
            cn3.Open()
            'SQL文生成
            SQL3 = "TRUNCATE TABLE System"
            cd3.CommandText = SQL3
            cd3.Connection = cn3

            cd3.ExecuteNonQuery()
            'クローズ解放
            cd3.Dispose()
            cn3.Close()
            cn3.Dispose()
        End If
    End Sub
End Class
