-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathEJscreen_SOE_report.aspx.vb
420 lines (361 loc) · 16.5 KB
/
EJscreen_SOE_report.aspx.vb
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
Imports System.Net
Imports System.IO
Imports System.Web.Services
Imports System.Drawing.Imaging
Imports Svg
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.Xml
Imports System.Collections
Imports System.Text
Imports System.Math
Partial Class EJscreen_SOE_report
Inherits System.Web.UI.Page
Public coordstr As String = ""
Public feattype As String = ""
Public dist As String = "0"
Public bunit As String = "miles"
Public ptitle As String = ""
Public pdesc As String = ""
Public namestr As String = ""
Public areaid As String = ""
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Try
'Dim regexString As String = System.Configuration.ConfigurationManager.AppSettings("regexString")
'Dim RegexObj As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(regexString, System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace)
Dim z As Integer
Dim st As String
Dim pcounter As Integer = Context.Request.Params.Count
'start new invalid url code - 10/31/23
Dim regexString As String = System.Configuration.ConfigurationManager.AppSettings("regexString")
Dim RegexObj As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(regexString, System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace)
Dim method_get As Boolean = (Request.ServerVariables("Request_Method").ToUpper = "GET")
Dim currentContext As HttpContext = HttpContext.Current
Try
If method_get Then
Dim requestUrlCurrent As Uri = currentContext.Request.Url
Dim requestUrlString As String = requestUrlCurrent.ToString()
Dim isValid As Boolean = IsValidURL(requestUrlString)
' If RegexObj.IsMatch(requestUrlString) Then
If Not isValid Then
Context.Response.Write("Invalid characters in URL!")
Context.Response.Flush()
Response.End()
Else
ValidateForInvalidCharacters(requestUrlString, isValid)
End If
Else
Dim postParams As NameValueCollection = currentContext.Request.Form
Dim queryStringBuilder As New StringBuilder()
For Each key As String In postParams.AllKeys
Dim value As String = postParams(key)
If queryStringBuilder.Length > 0 Then
queryStringBuilder.Append("&")
End If
queryStringBuilder.AppendFormat("{0}={1}", key, value)
Next
'If RegexObj.IsMatch(queryStringBuilder.ToString()) Then
Dim isValid As Boolean = IsValidURL(queryStringBuilder.ToString())
If Not isValid Then
Context.Response.Write("Invalid characters in URL!")
Context.Response.Flush()
Response.End()
Else
ValidateForInvalidCharacters(queryStringBuilder.ToString(), isValid)
End If
End If
Catch ex As HttpRequestValidationException
Response.Write("Invalid URL Detected: " & ex.ToString())
Response.Write(vbNewLine)
Response.End()
End Try
'end new invalid url code
'old method
' Dim method_get As Boolean = (Request.ServerVariables("Request_Method").ToUpper = "GET")
' If method_get Then
' pcounter = Context.Request.QueryString.Count
' Else
' pcounter = Context.Request.Form.Count
' End If
' For z = 0 To pcounter - 1
' If method_get Then
' st = Context.Request.QueryString(z)
' Else
' st = Context.Request.Form(z)
' End If
' If RegexObj.IsMatch(st) Then
' Context.Response.Write("Invalid characters in URL!")
' Context.Response.Flush()
' Response.End()
' End If
' Next
coordstr = Request.Params.Get("geometry")
If (coordstr.Length > 1) Then
coordstr = Server.UrlDecode(coordstr)
End If
' Dim coordreg As String = "^(-?\d(\.\d+|),?\s?)+$"
'Dim regLat As RegularExpressions.Regex = New RegularExpressions.Regex(coordreg, RegularExpressions.RegexOptions.IgnorePatternWhitespace)
' If regLat.IsMatch(coordstr) Then
' Else
' Response.Write("alert('Invalid coordinates input!')")
' Response.End()
' End If
feattype = Request.Params.Get("areatype")
If (feattype.Length > 1) Then
If feattype Is Nothing Then
feattype = ""
End If
Dim typereg As String = "^(blockgroup|tract|city|county)$"
Dim regtype As RegularExpressions.Regex = New RegularExpressions.Regex(typereg, RegularExpressions.RegexOptions.IgnorePatternWhitespace)
If regtype.IsMatch(feattype) Then
feattype = feattype.toLower
Else
Response.Write("alert('Invalid areatype input!')")
Response.End()
End If
End If
areaid = Request.Params.Get("areaid")
If areaid.Length > 1 Then
Dim areaidreg As String = "^\d{5,12}$"
Dim regareaid As RegularExpressions.Regex = New RegularExpressions.Regex(areaidreg, RegularExpressions.RegexOptions.IgnorePatternWhitespace)
If regareaid.IsMatch(areaid) Then
Else
Response.Write("alert('Invalid areaid input!')")
Response.End()
End If
namestr = Request.Params.Get("namestr")
If namestr Is Nothing Then
namestr = areaid
End If
End If
Dim distreg As String = "^\d*(\.\d+|)$"
Dim regdist As RegularExpressions.Regex = New RegularExpressions.Regex(distreg, RegularExpressions.RegexOptions.IgnorePatternWhitespace)
dist = Request.Params.Get("distance")
If dist Is Nothing Then
dist = "0"
End If
If regdist.IsMatch(dist) Then
Else
Response.Write("alert('Invalid radius input!')")
Response.End()
End If
bunit = Request.Params.Get("unit")
ptitle = Request.Params.Get("ptitle")
namestr = Request.Params.Get("namestr")
If namestr Is Nothing Then
namestr = coordstr
End If
Catch ex As Exception
Context.Response.Write("<br>error occurred: " & ex.Message)
Context.Response.Flush()
Response.End()
End Try
End If
End Sub
Public Function locationStrExistsOld(ByVal url As String, ByVal locationList As String()) As Boolean
' Iterate over the locations and check if any of them is part of the URL
For Each location As String In locationList
If url.Contains(location) Then
' If the URL contains any of the locations, return True to indicate that validation should be skipped
Return True
End If
Next
' If none of the locations are part of the URL, return False to indicate that validation should proceed
Return False
End Function
Public Function IsValidURL(urlString As String) As Boolean
Dim pattern As String = "[*$|<|>|(|)]"
Dim locations As String() = {
"(Webster Springs)", "(Payapai)", "(Nieves)", "(balance)", "(Alvin)",
"(Berkeley Springs)", "(Hensel)", "(Thurman)", "(Marpo Valley)", "(Paso Robles)",
"(Biscoe)", "(Brinkhaven)", "(Kaan)", "(Chudan)", "(Croton)", "(Boelus)",
"(Marlin)", "(Minachage)", "(Behrend)", "(New Raymer)", "(Stout)", "(Ventura)",
"(Oleai)", "(Tinian Municipality)", "(Union)", "(Uniontown)", "(Farallon de Pajaros)",
"(Big Park)", "(Coalton)", "(Farallon de Pajaros)", "(Big Park)", "(Coalton)"
}
urlString = urlString.Replace("+", " ")
Dim locationExists As Boolean = LocationStrExists(urlString, locations)
If locationExists Then
Return ValidateUrlParts(urlString, locations, pattern)
Else
Return ValidateWithoutLocations(urlString, pattern)
End If
End Function
Private Function ValidateUrlParts(urlString As String, locations As String(), pattern As String) As Boolean
If Not HandleApostrophes(urlString) Then
Return False
End If
Dim urlParts As List(Of String) = urlString.Split(locations, StringSplitOptions.None).ToList()
For Each part As String In urlParts
If Regex.IsMatch(part, pattern) Then
Return False
End If
Next
Return True
End Function
Private Function ValidateWithoutLocations(urlString As String, pattern As String) As Boolean
If HandleApostrophes(urlString) AndAlso Not Regex.IsMatch(urlString, pattern) Then
Return True
Else
Return False
End If
End Function
Private Function HandleApostrophes(urlString As String) As Boolean
Dim ignoreList As String() = {
"Prince George's County", "Queen Anne's County", "St. Mary's County", "O'Brien County"
}
urlString = urlString.Replace("+", " ")
Dim idx As Integer = urlString.IndexOf("'")
While idx >= 0
Dim apostropheIsPartOfIgnoreList As Boolean = False
For Each ignoreStr In ignoreList
Dim ignoreIndex As Integer = urlString.IndexOf(ignoreStr, StringComparison.OrdinalIgnoreCase)
If idx >= ignoreIndex AndAlso idx < ignoreIndex + ignoreStr.Length Then
apostropheIsPartOfIgnoreList = True
Exit For
End If
Next
If Not apostropheIsPartOfIgnoreList Then
Return False
End If
idx = urlString.IndexOf("'", idx + 1)
End While
Return True
End Function
Private Function LocationStrExists(url As String, locationList As String()) As Boolean
'url = url.Replace("+", " ")
For Each location As String In locationList
If url.Contains(location) Then
Return True
End If
Next
Return False
End Function
Public Function IsValidURLOld(urlString As String) As Boolean
Dim isValid As Boolean = True
Dim pattern As String = "[*$|<|>|(|)]"
Dim locations As String() = New String() {
"(Webster Springs)",
"(Payapai)",
"(Nieves)",
"(balance)",
"(Alvin)",
"(Berkeley Springs)",
"(Hensel)",
"(Thurman)",
"(Marpo Valley)",
"(Paso Robles)",
"(Biscoe)",
"(Brinkhaven)",
"(Kaan)",
"(Chudan)",
"(Croton)",
"(Boelus)",
"(Marlin)",
"(Minachage)",
"(Behrend)",
"(New Raymer)",
"(Stout)",
"(Ventura)",
"(Oleai)",
"(Tinian Municipality)",
"(Union)",
"(Uniontown)",
"(Farallon de Pajaros)",
"(Big Park)",
"(Coalton)",
"(Farallon de Pajaros)",
"(Big Park)",
"(Coalton)"
}
Dim locationExists As Boolean = locationStrExists(urlString, locations) 'Check if the url has any of the strings
If locationExists Then
Dim urlParts As List(Of String) = urlString.Split(locations, StringSplitOptions.None).ToList()
For Each part As String In urlParts
If Regex.IsMatch(part, pattern) Then
isValid = False
Exit For
End If
Next
Else
Dim ignoreList As String() = {"Prince George's County", "Queen Anne's County", "St. Mary's County", "O'Brien County"}
'Skipping the validation when url has certain strings enclosed in paranthesis
' Dim ignoreValidation As Boolean = ShouldSkipValidation(urlString)
urlString = urlString.Replace("+", " ")
' Check each apostrophe in the URL
Dim idx As Integer = urlString.IndexOf("'")
While idx >= 0
Dim apostropheIsPartOfIgnoreList As Boolean = False
' Check if the substring around the apostrophe is part of the ignore list
For Each ignoreStr In ignoreList
Dim ignoreIndex As Integer = urlString.IndexOf(ignoreStr, StringComparison.OrdinalIgnoreCase)
While ignoreIndex >= 0
If idx >= ignoreIndex AndAlso idx < ignoreIndex + ignoreStr.Length Then
apostropheIsPartOfIgnoreList = True
Exit For
End If
ignoreIndex = urlString.IndexOf(ignoreStr, ignoreIndex + 1, StringComparison.OrdinalIgnoreCase)
End While
If apostropheIsPartOfIgnoreList Then
Exit For
End If
Next
If Not apostropheIsPartOfIgnoreList Then
'Return False
isValid = False
Exit While
End If
idx = urlString.IndexOf("'", idx + 1)
End While
' Check if the URL matches the regex pattern
If Regex.IsMatch(urlString, pattern) Then
' Return False
isValid = False
End If
' If all checks are passed, return True
'Return True
End If
Return isValid
End Function
Public Function CheckURL(ByVal HostAddress As String) As Boolean
Dim rtCheckURL As Boolean = False
Try
Dim req As WebRequest = WebRequest.Create(HostAddress)
req.Credentials = CredentialCache.DefaultCredentials
Dim wResponse As WebResponse = req.GetResponse()
Dim mtype As String = wResponse.ContentType
If mtype.IndexOf("image") > -1 Then
rtCheckURL = True
End If
wResponse.Close()
Catch ex As Exception
rtCheckURL = False
End Try
Return rtCheckURL
End Function
'start new validation methods 10/31/23
Private Sub ValidateForInvalidCharacters(requestUrlString As String, isValid As Boolean)
GetDecodedUrl(requestUrlString, isValid)
End Sub
Sub GetDecodedUrl(ByVal url As String, isValid As Boolean)
' Dim regexString As String = System.Configuration.ConfigurationManager.AppSettings("regexString")
'Dim RegexObj As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(regexString, System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace)
Dim firstDecodedUrl As String = HttpUtility.UrlDecode(url)
Dim secondDecodedUrl As String = HttpUtility.UrlDecode(firstDecodedUrl)
'Return secondDecodedUrl = firstDecodedUrl ' Returns True if the URL was not encoded, False otherwise
If firstDecodedUrl = secondDecodedUrl Then
If Not isValid Then
'If RegexObj.IsMatch(url) Then
Context.Response.Write("Invalid characters in URL!")
Context.Response.Flush()
Response.End()
End If
Else
Context.Response.Write("Invalid URL!")
Context.Response.Flush()
Response.End()
End If
End Sub
'end new validation methods
End Class