Imports System.Web
Imports System.Text
Imports System.Text.RegularExpressions

Public Class Qianbo_Safe

    Private Const StrRegex As String = "^\+/v(8|9)|\b(and|or)\b.{1,6}?(=|>|<|\bin\b|\blike\b)|/\*.+?\*/|<\s*script\b|\bEXEC\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\s+(TABLE|DATABASE)"

    Public Shared Function PostData() As Boolean
        Dim result As Boolean = False
        For i As Integer = 0 To HttpContext.Current.Request.Form.Count - 1
            result = CheckData(HttpContext.Current.Request.Form(i).ToString())
            If result Then
                Exit For
            End If
        Next
        Return result
    End Function

    Public Shared Function GetData() As Boolean
        Dim result As Boolean = False
        For i As Integer = 0 To HttpContext.Current.Request.QueryString.Count - 1
            result = CheckData(HttpContext.Current.Request.QueryString(i).ToString())
            If result Then
                Exit For
            End If
        Next
        Return result
    End Function

    Public Shared Function CookieData() As Boolean
        Dim result As Boolean = False
        For i As Integer = 0 To HttpContext.Current.Request.Cookies.Count - 1
            result = CheckData(HttpContext.Current.Request.Cookies(i).Value.ToLower())
            If result Then
                Exit For
            End If
        Next
        Return result
    End Function

    Public Shared Function referer() As Boolean
        Dim result As Boolean = False
        Return InlineAssignHelper(result, CheckData(HttpContext.Current.Request.UrlReferrer.ToString()))
    End Function

    Public Shared Function CheckData(ByVal inputData As String) As Boolean
        If Regex.IsMatch(inputData, StrRegex) Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
        target = value
        Return value
    End Function

End Class