<%
*************************************************************
在支持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
%>
