Private objAPI
Private objCore
Private objXML
Private objUser
Private objMatter

Private Sub Class_Initialize()
    Set objAPI = New WAPmoAPI
    Set objCore = objAPI.Core
    Set objXML = objCore.XMLParser
    Set objUser = objCore.User
End Sub

Private Sub Class_Terminate()
    Set objUser = Nothing
    Set objXML = Nothing
    Set objCore = Nothing
    Set objAPI = Nothing
End Sub

Public Sub main()
    If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
        Call doPost
    Else
        Call doGet
    End If
End Sub

Private Sub doGet()
    Dim intType
    Dim intStaple
    Dim intContent
    Dim hexMatter
    Dim strReferer
    intType = atoi(Request.QueryString("Type"))
    intStaple = atoi(Request.QueryString("Staple"))
    intContent = atoi(Request.QueryString("Content"))
    hexMatter = Request.QueryString("Matter")
    strReferer = Request.QueryString("Referer")
    If strReferer = "" Then strReferer = "index.asp"
    Set objMatter = MyKernel.Table(T_MATTER)
    If objMatter.Exec("*", "Mark=" & atol("&H" & hexMatter)) = False Then
        objCore.Redirect strReferer
    ElseIf objMatter.Item("Hidden") = 1 Then
        objCore.Redirect strReferer
    Else
        objCore.Export = False
        If ExportMatter(objMatter) = False Then
            objCore.Export = True
            objCore.Redirect strReferer
        Else
            Call UpdateRecord(intType)
        End If
    End If
    Set objMatter = Nothing
End Sub

Private Sub doPost()
    objXML.Println "֧POST"
End Sub

Private Function ExportMatter(objPtr)
    Dim strExt1
    Dim strExt2
    Dim strPath1
    Dim strPath2
    Dim strExt
    Dim intWidth
    Dim intHeight
    strExt = Request.QueryString("Ext")
    intWidth = atoi(Request.QueryString("Width"))
    intHeight = atoi(Request.QueryString("Height"))
    If objPtr.Item("Category") = WAPMO_IMAGE Then
        strExt1 = objPtr.Item("Ext")
        strExt2 = objCore.getImageExt(strExt1)
        strPath1 = getMatterFile(objPtr.Item("Category"), objPtr.Item("Intime"), objPtr.Item("Mark"), strExt1)
        strPath2 = getMatterCacheFile(objPtr.Item("Category"), objPtr.Item("Intime"), objPtr.Item("Mark"), "D", strExt2)
        ImageConvert MyKernel.MapPath(strPath1), MyKernel.MapPath(strPath2)
        If vbsre.fso.FileExists(MyKernel.MapPath(strPath2)) = False Then
            ExportMatter = False
        Else
            If intWidth <= 0 Or intHeight <= 0 Then
                If MyKernel.Modlist(19) = "1" Then
                    If getUAConfig(intWidth, intHeight) = False Then
                        intWidth = objPtr.Item("Width")
                        intHeight = objPtr.Item("Height")
                    End If
                Else
                    intWidth = objPtr.Item("Width")
                    intHeight = objPtr.Item("Height")
                End If
            ElseIf intWidth > atoi(MyKernel.Config("KeepWidth")) Or intHeight > atoi(MyKernel.Config("KeepHeight")) Then
                intWidth = objPtr.Item("Width")
                intHeight = objPtr.Item("Height")
            End If
            ImageResize1 MyKernel.MapPath(strPath2), intWidth, intHeight
            ExportMatter = ExportFile(MyKernel.MapPath(strPath2))
        End If
    ElseIf UCase(strExt) = "JAD" Then
        strPath1 = getMatterFile(objPtr.Item("Category"), objPtr.Item("Intime"), objPtr.Item("Mark"), "jad")
        ExportMatter = ExportFile(MyKernel.MapPath(strPath1))
    Else
        strPath1 = getMatterFile(objPtr.Item("Category"), objPtr.Item("Intime"), objPtr.Item("Mark"), objPtr.Item("Ext"))
        ExportMatter = ExportFile(MyKernel.MapPath(strPath1))
    End If
End Function

Private Function getUAConfig(ByRef intWidth, ByRef intHeight)
    getUAConfig = False
    Dim strUA
    strUA = Request.ServerVariables("HTTP_USER_AGENT")
    If strUA = "" Then Exit Function
    Dim strSQL
    Dim rs
    strSQL = getLimitSQL(1, "ImageWidth,ImageHeight", T_USER_AGENT, "MobileFace LIKE '%$(UserAgent)%'", "", "")
    strSQl = Replace(strSQL, "$(UserAgent)", SafeString(strUA))
    Set rs = MyKernel.Execute(strSQL)
    getUAConfig = CBool(rs.EOF = False)
    If getUAConfig Then
        ingWidth = atoi(rs("ImageWidth"))
        ingHeight = atoi(rs("ImageHeight"))
    End If
    rs.Close
    Set rs = Nothing
End Function

Private Function ExportFile(ByVal strPath)
    ExportFile = False
    On Error Resume Next
    Response.Buffer = True
    Response.ContentType = getMIMEType(strPath)
    Dim objFile
    Dim strdata
    Set objFile = Server.CreateObject("ADODB.Stream")
    objFile.Type = adTypeBinary
    objFile.Open
    objFile.LoadFromFile strPath
    Response.AddHeader "Content-Length", objFile.Size
    strData = objFile.Read(-1)
    objFile.Close
    Set objFile = Nothing
    If Err.Number Then
        Err.Clear
        Exit Function
    End If
    Response.BinaryWrite strData
    Response.Flush
    ExportFile = False
End Function

Private Function getMIMEType(ByVal strPath)
    Dim objMIME
    Set objMIME = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
    objMIME.Async = False
    objMIME.Load MyKernel.MapPath("config/mime.xml")
    Dim objNode
    Set objNode = objMIME.documentElement.selectSingleNode("mime_" & LCase(vbsre.fso.GetExtensionName(strPath)))
    If TypeName(objNode) <> "Nothing" Then
        getMIMEType = objNode.Text
    Else
        getMIMEType = "application/octet-stream"
    End If
    Set objMIME = Nothing
End Function

Private Sub UpdateRecord(ByVal x)
    Dim strSQL
    Dim lngId
    strSQL = getLimitSQL(1, "SeqId", T_MATTER_LOG, "MatterId=" & objMatter.Item("SeqId") & " AND Intime>=" & getTime(Date()), "", "")
    lngId = atol(getRecord(strSQL))
    If lngId > 0 Then
        strSQL = "UPDATE $(Table) SET $(Column)=$(Column)+1,Outime=$(Timeval) WHERE SeqId=$(SeqId)"
        strSQL = Replace(strSQL, "$(SeqId)", lngId)
        strSQL = Replace(strSQL, "$(Column)", IIf(x = 0, "Hit", "Download"))
    Else
        strSQL = "INSERT INTO $(Table)(MatterId,Title,Hit,Download,Intime,Outime) VALUES ($(StapleId),'$(Title)',$(Hit),$(Download),$(Timeval),$(Timeval))"
        strSQL = Replace(strSQL, "$(StapleId)", objMatter.Item("SeqId"))
        strSQL = Replace(strSQL, "$(Title)", SafeString(objMatter.Item("Title")))
        strSQL = Replace(strSQL, "$(Hit)", IIf(x = 0, 1, 0))
        strSQL = Replace(strSQL, "$(Download)", IIf(x = 0, 0, 1))
    End If
    strSQL = Replace(strSQL, "$(Table)", T_MATTER_LOG)
    strSQL = Replace(strSQL, "$(Timeval)", getTime(Now()))
    SafeExecute strSQL
    strSQL = "UPDATE $(Table) SET $(Column)=$(Column)+1 WHERE SeqId=$(SeqId)"
    strSQL = Replace(strSQL, "$(Table)", T_MATTER)
    strSQL = Replace(strSQL, "$(Column)", IIf(x = 0, "View", "Download"))
    strSQL = Replace(strSQL, "$(SeqId)", objMatter.Item("SeqId"))
    SafeExecute strSQL
    UserRecord "D"
    Call MISCRecord
    Call ClearCache
End Sub

Private Sub UserRecord(ByVal c)
    Dim strSQL
    Dim lngId
    Dim lngUserId
    lngUserId = MyKernel.Memory("UserId")
    If MyKernel.Memory("UserId") = "" Then
        Dim arrTemp
        arrTemp = vbsre.cache("WAPmo.Guest")
        If IsEmpty(arrTemp) Then Exit Sub
        lngUserId = arrTemp(3)
    End If
    lngUserId = atol(lngUserId)
    strSQL = getLimitSQL(1, "SeqId", T_USER_LOG, "UserId=" & lngUserId & " AND Intime>=" & getTime(Date()), "", "")
    lngId = atol(getRecord(strSQL))
    If lngId > 0 Then
        strSQL = "UPDATE $(Table) SET $(Char)Hit=$(Char)Hit+1,Outime=$(Timeval) WHERE SeqId=$(SeqId)"
        strSQL = Replace(strSQL, "$(Char)", c)
        strSQL = Replace(strSQL, "$(SeqId)", lngId)
    Else
        strSQL = "INSERT INTO $(Table) (UserId,SHit,CHit,VHit,DHit,Intime,Outime) VALUES ($(UserId),$(SHit),$(CHit),$(VHit),$(DHit),$(Timeval),$(Timeval))"
        strSQL = Replace(strSQL, "$(UserId)", lngUserId)
        strSQL = Replace(strSQL, "$(SHit)", IIf(c = "S", 1, 0))
        strSQL = Replace(strSQL, "$(CHit)", IIf(c = "C", 1, 0))
        strSQL = Replace(strSQL, "$(VHit)", IIf(c = "V", 1, 0))
        strSQL = Replace(strSQL, "$(DHit)", IIf(c = "D", 1, 0))
    End If
    strSQL = Replace(strSQL, "$(Table)", T_USER_LOG)
    strSQL = Replace(strSQL, "$(Timeval)", getTime(Now()))
    SafeExecute strSQL
End Sub

Private Sub MISCRecord()
    If MyKernel.Modlist(16) <> "1" Then Exit Sub
    Dim lngStapleId
    Dim lngContentId
    Dim strMark
    Dim strMobile
    lngStapleId = atol(Request.QueryString("StapleId"))
    lngContentId = atol(Request.QueryString("ContentId"))
    If lngStapleId <= 0 Then Exit Sub
    If lngContentId <= 0 Then Exit Sub
    strMobile = Request.ServerVariables("HTTP_X_UP_CALLING_LINE_ID")
    If strMobile = "" Then
        strMobile = Request.QueryString("MISC_MSISDN")
    End If
    If strMobile = "" Then
        strMobile = Request.QueryString("MISC_MID")
    End If
    If strMobile = "" Then Exit Sub
    If IsEmpty(vbsre.cache("WAPmo.MISC")) Then Exit Sub
    strMark = getRecord(getLimitSQL(1, "Mark", T_STAPLE, "SeqId=" & lngStapleId, "", ""))
    If strMark = "" Then Exit Sub
    Dim arrTemp
    Dim strTemp
    Dim i
    arrTemp = vbsre.cache("WAPmo.MISC")
    For i = 0 To UBound(arrTemp, 2)
        strTemp = arrTemp(2, i) & "_"
        If arrTemp(0, i) = lngStapleId Then
            MISCRecordX lngStapleId, lngContentId, arrTemp(5, i), strMobile
            Exit For
        ElseIf Left(strMark, Len(strTemp)) = strTemp Then
            MISCRecordX lngStapleId, lngContentId, arrTemp(5, i), strMobile
            Exit For
        End If
    Next
End Sub

Private Sub MISCRecordX(ByVal lngSID, ByVal lngCID, ByVal strSID, ByVal strMobile)
    Dim strSQL
    strSQL = "INSERT INTO $(Table) (StapleId,ContentId,MatterId,Title,Mobile,ServiceId,Intime) VALUES ($(StapleId),$(ContentId),$(MatterId),'$(Title)','$(Mobile)','$(ServiceId)',$(Timeval))"
    strSQL = Replace(strSQL, "$(Table)", T_MISC_LOG)
    strSQL = Replace(strSQL, "$(StapleId)", lngSID)
    strSQL = Replace(strSQL, "$(ContentId)", lngCID)
    strSQL = Replace(strSQL, "$(MatterId)", objMatter.Item("SeqId"))
    strSQL = Replace(strSQL, "$(Title)", SafeString(objMatter.Item("Title")))
    strSQL = Replace(strSQL, "$(Mobile)", SafeString(strMobile))
    strSQL = Replace(strSQL, "$(ServiceId)", SafeString(strSID))
    strSQL = Replace(strSQL, "$(Timeval)", getTime(Now()))
    SafeExecute strSQL
End Sub

Private Sub ClearCache()
    If objCore.CheckStaple() = False Then Exit Sub
    IF objCore.CheckContent() = False Then Exit Sub
    Dim objContent
    Set objContent = objCore.Content
    doRemoveSingleContent objContent.Item("SeqId"), objContent.Item("StapleId"), objContent.Item("Intime")
    Set objContent = Nothing
End Sub
