日期:2013-11-11  浏览次数:20885 次

<%
'*************************************************************
'在支持FSO的情况下,可以显示本站内的所有ASP页面的代码
'适用于代码演示时在效果页面上直接显示该页面的代码而不用再对代码制作专门的页面
'使用方法:ViewSource.asp?file=要显示的文件名
'如:ViewSource.asp?file=x.asp
'modify By : Babyt
'*************************************************************
%>
<B Style="font-size:12px;font-family:Courier New">HTML/ASP Source Code:</B>
<HR SIZE=1>
<%
Dim objFSO, objInFile
Dim strIn, strTemp
Dim I, J
Dim strFileName
Dim ProcessString
Dim bCharWritten
Dim bInsideScript
Dim bInsideString
Dim iInsideComment

ProcessString = 0
bCharWritten = False
bInsideScript = False
bInsideString = False
iInsideComment = 0
linecount = 1

strFileName = Request.QueryString("file")

'为了保护你的其他页面,进行简单保护,只允许访问当前目录下的文件
'你可以根据实际需要增加更过规则
If InStr(1, strFileName, "\", 1) Then strFileName=""
If InStr(1, strFileName, "/", 1) Then strFileName=""

If strFileName <> "" Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
'判断文件是否存在
If objFSO.FileExists(Server.MapPath(strFileName))=False Then
Response.Write "文件不存在"
Response.End
End If
'打开文件
Set objInFile = objFSO.OpenTextFile(Server.MapPath(strFileName))
Response.Write "<PRE Style='font-size:12px;font-family:Courier New'>" & vbCRLF
'按行读取文本流
Do While Not objInFile.AtEndOfStream
'进行编码
strIn = Server.HTMLEncode(objInFile.ReadLine)
strTemp = ""
'判断起始 < %
'对整个脚本快加亮,n默认蓝色
For I = 1 to Len(strIn)
bCharWritten = False
If InStr(I, strIn, "<%", 1) = I Then
strTemp = strTemp & "<FONT COLOR=#0000EE>"
bInsideScript = True
Else
'判断结束标志位 % >
If InStr(I, strIn, "%>", 1) = I Then
strTemp = strTemp & "%></FONT>"
bCharWritten = True
' so we dont get the trailing end of this tag again!
' ie. Len("%>") - 1 = 4
I = I + 4
bInsideScript = False
End If
End If
' Toggle Inside String if needed!
If bInsideScript And iInsideComment = 0 And InStr(I, strIn, """, 1) = I Then bInsideString = Not bInsideString
'判断可能的注释,主要是为了改变其颜色(默认绿色)
If bInsideScript And Not bInsideString And (InStr(I, strIn, "'", 1) OR InStr(I, strIn, "//", 1)) = I Then
strTemp = strTemp & "<FONT COLOR=#009900>"
iInsideComment = iInsideComment + 1
End If
' 结束注释文字处理
If iInsideComment > 0 And I = Len(strIN) Then
strTemp = strTemp & Mid(strIn, I, 1)
For J = 1 to iInsideComment
strTemp = strTemp & "</FONT>"
Next 'J
bCharWritten = True
iInsideComment = 0
End If
If bCharWritten = False Then
strTemp = strTemp & Mid(strIn, I, 1)
End If
Next
'此句写行号,可以把行号去掉
Response.Write "<FONT COLOR=#666666>" & linecount & "</font>  " & strTemp & vbCRLF
linecount = linecount + 1
Loop
Response.Write "</PRE>" & vbCRLF

objInFile.Close
Set objInFile = Nothing
Set objFSO = Nothing
End If
%>