Imports System.Drawing
Imports System.Net
Imports System.IO
Imports System.Xml

Partial Class admin_Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Request.Browser.IsMobileDevice Then
            vPage.Style.Add("width", "520px")
            PanelPC.Visible = False
            PanelMobile.Visible = True
        Else
            vPage.Style.Add("width", "980px")
            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

            'タイトルとメニューの色
            Image1.ImageUrl = "../img/Top01.jpg"
            Image2.ImageUrl = "../img/Top02.jpg"
            TextBoxTitleColor.Text = "White"
            TextBoxMobileMenuColor.Text = "White"
            TextBoxTitleColor2.Text = "White"
            TextBoxMobileMenuColor2.Text = "White"
            If System.IO.File.Exists(Server.MapPath("../img/Color.txt")) Then
                Dim sr As New System.IO.StreamReader(Server.MapPath("../img/Color.txt"), System.Text.Encoding.GetEncoding("shift_jis"))
                Dim s As String = sr.ReadToEnd()
                sr.Close()
                Dim x As String() = s.Split(vbLf)
                For i = 0 To x.Length - 1
                    If Len(x(i)) > 2 Then
                        Dim y As String() = x(i).Split(vbTab)
                        Select Case y(0)
                            Case "タイトル"
                                TextBoxTitleColor.Text = y(1)
                            Case "スマホメニュー"
                                TextBoxMobileMenuColor.Text = y(1)
                            Case "タイトル２"
                                TextBoxTitleColor2.Text = y(1)
                            Case "スマホメニュー２"
                                TextBoxMobileMenuColor2.Text = y(1)
                        End Select
                    End If
                Next
            Else

            End If

            '文章の表示
            If System.IO.File.Exists(Server.MapPath("../img/text.txt")) Then
                Dim sr As New System.IO.StreamReader(Server.MapPath("../img/text.txt"), System.Text.Encoding.GetEncoding("shift_jis"))
                Dim s As String = sr.ReadToEnd()
                sr.Close()
                TextBoxBody.Text = s
            End If
            If System.IO.File.Exists(Server.MapPath("../img/text2.txt")) Then
                Dim sr As New System.IO.StreamReader(Server.MapPath("../img/text2.txt"), System.Text.Encoding.GetEncoding("shift_jis"))
                Dim s As String = sr.ReadToEnd()
                sr.Close()
                TextBoxBody2.Text = s
            End If

        End If
    End Sub

    Protected Function IsStatus() As String
        '管理者確認の問合せ
        Dim xCookie As String = ""
        If Request.Cookies("Local") IsNot Nothing Then
            xCookie = 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 & "/Approve/IsStatus.aspx?code=" & xTime & "," & xCookie)
            Dim sr As StreamReader = New StreamReader(st, enc)
            Dim html As String = sr.ReadToEnd()
            sr.Close()
            st.Close()
            Return html
        Else
            Return "不明,0,0,0,0,0,0,0,0,0,0"
        End If
    End Function

    Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
        If IsPostBack Then
            If FileUpload2.HasFile Then
                If System.IO.Path.GetExtension(FileUpload2.FileName) <> ".jpg" Then
                    LabelMsg.Text = "画像の拡張子は、.jpg でなければなりません"
                    Exit Sub
                Else
                    LabelMsg.Text = ""
                End If
                FileUpload2.PostedFile.SaveAs(Server.MapPath("../img/Temp.jpg"))
                Dim bmp As System.Drawing.Bitmap = New System.Drawing.Bitmap(Server.MapPath("../img/Temp.jpg"))
                If bmp.Width < 1920 Then
                    LabelMsg.Text = "画像が小さすぎます"
                Else
                    LabelMsg.Text = ""
                End If
                Dim xHeight As Integer = bmp.Height / bmp.Width * 1920
                Dim xHeights As Integer = xHeight / 2
                Dim canvas As New Bitmap(1920, xHeight)
                Dim canvas2 As New Bitmap(960, xHeights)
                Dim g As Graphics = Graphics.FromImage(canvas)
                Dim g2 As Graphics = Graphics.FromImage(canvas2)
                Dim img As Image = Image.FromFile(Server.MapPath("../img/Temp.jpg"))
                g.DrawImage(img, 0, 0, 1920, xHeight)
                canvas.Save(Server.MapPath("../img/Top02.jpg"))
                g2.DrawImage(img, 0, 0, 960, xHeights)
                canvas2.Save(Server.MapPath("../img/Top02s.jpg"))
                img.Dispose()
                g.Dispose()
                g2.Dispose()
                FileUpload2.Dispose()
                bmp.Dispose()
                canvas.Dispose()
                canvas2.Dispose()
                System.IO.File.Delete(Server.MapPath("../img/Temp.jpg"))
                Image2.ImageUrl = "../img/Top02.jpg"
            End If
        End If
    End Sub

    Protected Sub Button3_Click(sender As Object, e As System.EventArgs) Handles Button3.Click
        If IsPostBack Then
            Dim sw As New System.IO.StreamWriter(Server.MapPath("../img/text.txt"), False, System.Text.Encoding.GetEncoding("shift_jis"))
            sw.Write(TextBoxBody.Text)
            sw.Close()
        End If
    End Sub

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        If IsPostBack Then
            If FileUpload1.HasFile Then
                If System.IO.Path.GetExtension(FileUpload1.FileName) <> ".jpg" Then
                    LabelMsg.Text = "画像の拡張子は、.jpg でなければなりません"
                    Exit Sub
                Else
                    LabelMsg.Text = ""
                End If
                FileUpload1.PostedFile.SaveAs(Server.MapPath("../img/Temp.jpg"))
                Dim bmp As System.Drawing.Bitmap = New System.Drawing.Bitmap(Server.MapPath("../img/Temp.jpg"))
                If bmp.Width < 1920 Then
                    LabelMsg.Text = "画像が小さすぎます"
                Else
                    LabelMsg.Text = ""
                End If
                Dim xHeight As Integer = bmp.Height / bmp.Width * 1920
                Dim xHeights As Integer = xHeight / 2
                Dim canvas As New Bitmap(1920, xHeight)
                Dim canvas2 As New Bitmap(960, xHeights)
                Dim g As Graphics = Graphics.FromImage(canvas)
                Dim g2 As Graphics = Graphics.FromImage(canvas2)
                Dim img As Image = Image.FromFile(Server.MapPath("../img/Temp.jpg"))
                g.DrawImage(img, 0, 0, 1920, xHeight)
                canvas.Save(Server.MapPath("../img/Top01.jpg"))
                g2.DrawImage(img, 0, 0, 960, xHeights)
                canvas2.Save(Server.MapPath("../img/Top01s.jpg"))
                img.Dispose()
                g.Dispose()
                g2.Dispose()
                FileUpload1.Dispose()
                bmp.Dispose()
                canvas.Dispose()
                canvas2.Dispose()
                System.IO.File.Delete(Server.MapPath("../img/Temp.jpg"))
                Image1.ImageUrl = "../img/Top01.jpg"
            End If
        End If
    End Sub

    Protected Sub Button5_Click(sender As Object, e As System.EventArgs) Handles Button5.Click
        If IsPostBack Then
            If String.IsNullOrEmpty(TextBoxTitleColor.Text) Then LabelMsg2.Text = "タイトル色が無い" : Exit Sub
            If String.IsNullOrEmpty(TextBoxMobileMenuColor.Text) Then LabelMsg2.Text = "スマホメニュー色が無い" : Exit Sub
            If String.IsNullOrEmpty(TextBoxTitleColor2.Text) Then LabelMsg2.Text = "タイトル色２が無い" : Exit Sub
            If String.IsNullOrEmpty(TextBoxMobileMenuColor2.Text) Then LabelMsg2.Text = "スマホメニュー色２が無い" : Exit Sub
            Dim sw As New System.IO.StreamWriter(Server.MapPath("../img/Color.txt"), False, System.Text.Encoding.GetEncoding("shift_jis"))
            Dim x As String = "タイトル" & vbTab & TextBoxTitleColor.Text & vbLf
            x = x & "スマホメニュー" & vbTab & TextBoxMobileMenuColor.Text & vbLf
            x = x & "タイトル２" & vbTab & TextBoxTitleColor2.Text & vbLf
            x = x & "スマホメニュー２" & vbTab & TextBoxMobileMenuColor2.Text
            sw.Write(x)
            sw.Close()
            LabelMsg2.Text = "書き込みました"
        End If
    End Sub

    Protected Sub Button4_Click(sender As Object, e As System.EventArgs) Handles Button4.Click
        If IsPostBack Then
            Dim sw As New System.IO.StreamWriter(Server.MapPath("../img/text2.txt"), False, System.Text.Encoding.GetEncoding("shift_jis"))
            sw.Write(TextBoxBody2.Text)
            sw.Close()
        End If
    End Sub
End Class
