forked from the-linck/ASP-Dynamic-Include
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicInclude.asp
More file actions
214 lines (200 loc) · 6.35 KB
/
DynamicInclude.asp
File metadata and controls
214 lines (200 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<%
' Previous path for recursive file importing
DynamicInclude_PreviousPath = ""
' Current path for recursive file importing
DynamicInclude_CurrentPath = "./"
' Executes an imported file in the global namespace.
'
' @param {string} File
Sub ExecuteFile( File )
Dim Parsed
' Path operations for recursive file importing
DynamicInclude_PreviousPath = DynamicInclude_CurrentPath
'DynamicInclude_NextPath = FilePath(File)
DynamicInclude_CurrentPath = DynamicInclude_CurrentPath & FilePath(File)
File = FileName(File)
' Parsing ASP file
Parsed = ParseFile(DynamicInclude_CurrentPath & File)
' Always importing in global namespace (to prevent errors)
ExecuteGlobal Trim(Parsed)
' Restoring path operation
DynamicInclude_CurrentPath = "./"
DynamicInclude_PreviousPath = ""
End Sub
' Checks if File does exist in System.
'
' @param {string} File
' @param {FileSystemObject} System
' @return {bool}
Function FileDoExist( File, System )
FileDoExist = System.FileExists(Server.MapPath(File))
End Function
' Checks if File exist.
'
' @param {string} File
' @return {bool}
Function FileExists(File)
Set System = FileSystem()
FileExists = FileDoExist(File, System)
Set System = Nothing
End Function
' Gets the name of the file.
'
' @param {string} File
' @return {string}
Function FileName(File)
Dim BarIndex
File = Replace(File, "\", "/")
BarIndex = InStrRev(File, "/")
FileName = Mid(File, BarIndex + 1, LEN(File) - BarIndex)
End Function
' Gets the path of a file.
'
' @param {string} File
' @return {string}
Function FilePath(File)
Dim BarIndex
File = Replace(File, "\", "/")
BarIndex = InStrRev(File, "/")
if IsNull(BarIndex) or BarIndex = 0 then
FilePath = ""
else
FilePath = Mid(File, 1, BarIndex)
end if
End Function
' Syntax sugar.
'
' @return {FileSystemObject}
Function FileSystem( )
Set FileSystem = Server.CreateObject("Scripting.FileSystemObject")
End Function
' Gets the absolute equivalent of Path.
'
' @param {string} Path
' @return {string}
Function MapPath( Path )
if Path = "" then
Path = "./"
end if
MapPath = Server.MapPath(Path)
End Function
' Reads File as ASP code.
'
' @param {string} File
' @return {string}
Function ParseFile( File )
Dim CloseTag
Dim JoinSize
Dim LineJoin
Dim Match
Dim Rows
Dim OpenTag
Dim PlainRow
Dim Regex
Dim WriteTag
CloseTag = Chr(37)&Chr(62)
LineJoin = """ & VbCrLf & """
JoinSize = LEN(LineJoin)
OpenTag = Chr(60)&Chr(37)
WriteTag = OpenTag & "="
Set Regex = new RegExp
With Regex
.Global = True
.MultiLine = True
.IgnoreCase = True
End With
' Reading file
ParseFile = ReadFile(File)
' Adding nem line separator in tags (otherwise the rows regex will fail)
ParseFile = Replace(ParseFile, CloseTag & OpenTag, CloseTag & VbCrLf & OpenTag)
ParseFile = Replace(ParseFile, OpenTag & CloseTag, OpenTag & VbCrLf & CloseTag)
' Replacing write tag with expanded command
ParseFile = Replace(ParseFile, WriteTag, OpenTag & " Response.Write ")
' Converting ASP imports to Require calls
Regex.Pattern = "<!--\s*#include file=(""[^""]+"")\s*-->"
ParseFile = Regex.Replace(ParseFile, OpenTag & " Require($1) " & CloseTag)
' Adding ASP tags when needed
If LEFT(ParseFile, 2) <> OpenTag Then
ParseFile = OpenTag & CloseTag & VbCrLf & ParseFile
End If
If RIGHT(ParseFile, 2) <> CloseTag Then
ParseFile = ParseFile & VbCrLf & OpenTag & CloseTag
End If
' Trimming fist open tag and last close tag
ParseFile = Mid(ParseFile, 3, LEN(ParseFile) -4)
' Replacing free percentage symbols (but keeping ASP Tags)
ParseFile = Replace(ParseFile, "%", "%")
ParseFile = Replace(ParseFile, "<%", OpenTag)
ParseFile = Replace(ParseFile, "%>", CloseTag)
' Inserting plain texts in Response.write commands
Regex.Pattern = CloseTag & "([^%])+" & OpenTag
Set Rows = Regex.Execute(ParseFile)
For Each Match in Rows
' Removing delimiting close and open tags
PlainRow = Mid(Match.value, 3, LEN(Match.value) -4)
if (LEFT(PlainRow, 2) = VbCrLf) Then
PlainRow = Right(PlainRow, LEN(PlainRow) - 2)
end if
' Doubling quotes
PlainRow = Replace(PlainRow, """", """""")
' Replacing new lines with line joins
PlainRow = Replace(PlainRow, VbCrLf, LineJoin)
if (Right(PlainRow, JoinSize) = LineJoin) Then
' Keeping last quote
PlainRow = LEFT(PlainRow, LEN(PlainRow) - JoinSize)
end if
PlainRow = VbCrLf & "Response.write """ & PlainRow & """" & VbCrLf
' Replacing original line with corrected one
ParseFile = Replace(ParseFile, Match.value, PlainRow)
Next
' Removing empty Response.write commands
ParseFile = Replace(ParseFile, "Response.write """"" & VbCrLf, "")
' Removing duplicate new lines
Regex.Pattern = "(?:\s*\n)+"
ParseFile = Regex.Replace(ParseFile, VbCrLf)
Set Regex = Nothing
End Function
' Reads File as plain text.
'
' @param {string} File
' @return {string}
Function ReadFile(File)
Set System = FileSystem()
Set FileData = System.OpenTextFile(MapPath(File), 1)
if FileDoExist(File, System) then
ReadFile = FileData.ReadAll
else
Err.Raise 53
end if
Set FileData = Nothing
Set System = Nothing
End Function
' Tries to include File on current script.
' In case of failure, continues silently.
'
' @param {string} File
Sub Include(File)
On Error Resume Next
' Execute the code or fails silently
ExecuteFile File
On Error Goto 0
End Sub
' Tries to include File on current script.
' In case of failure, ends script execution with error message.
'
' @param {string} File
Sub Require(File)
On Error Resume Next
' Execute the code
ExecuteFile File
' If an error occurs, detect it here and stop execution.
If Err.Number > 0 Then
Response.Write "---FATAL ERROR: while trying to execute <b>" & File & "</b>---<br>" & VbCrLf
Response.Write "---Error description: " & Err.Description & "<br>" & VbCrLf
Response.Write "---Error Source: " & Err.Source & "<hr>" & VbCrLf
Response.Write "---Error Line: " & Err.Line & "<hr>" & VbCrLf
Response.End
End If
On Error Goto 0
End Sub
%>