This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
InjectAnything.ascx.vb
255 lines (206 loc) · 9.99 KB
/
InjectAnything.ascx.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
Imports DotNetNuke
Imports System.Text.RegularExpressions
Namespace DNNStuff.InjectAnything
Partial Class InjectAnything
Inherits DotNetNuke.Entities.Modules.PortalModuleBase
Implements DotNetNuke.Entities.Modules.IActionable
Dim _ms As ModuleSettings
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
Try
' process file or text
Process()
Catch ex As Exception
DotNetNuke.Services.Exceptions.ProcessModuleLoadException(Me, ex, True)
End Try
End Sub
#End Region
#Region " Injection"
Private Sub Process()
' get settings
_ms = New ModuleSettings(ModuleId, PortalSettings, Me)
' disabled?
If Not _ms.Enable Then Exit Sub
Dim injectedText As String = ""
Select Case _ms.Source
Case "Standard"
Dim filename As String = _ms.FullFilePath
If filename.Length = 0 Then Exit Sub
Dim fi As IO.FileInfo = New IO.FileInfo(filename)
If _ms.LinkFile Then
injectedText = GetFileLink(fi)
Else
Dim script As StandardScript = DNNUtilities.GetStandardScript(fi)
injectedText = script.Items(0).Content
End If
Case "File"
Dim filename As String = _ms.FullFilePath
If filename.Length = 0 Then Exit Sub
Dim fi As IO.FileInfo = New IO.FileInfo(filename)
If _ms.LinkFile Then
injectedText = GetFileLink(fi)
Else
injectedText = GetFileContents(fi)
End If
Case Else
' use the text
injectedText = _ms.Text
End Select
If injectedText.Length > 0 Then InjectText(injectedText)
End Sub
Private Function GetFileLink(ByVal fi As IO.FileInfo) As String
' returns html to link file as is
If Not fi.Exists Then Return ""
Select Case fi.Extension.ToLower
Case ".css"
Return String.Format("<link href=""{0}"" rel=""stylesheet"" type=""text/css"" />", ResolveUrl(MapURL(fi.FullName)))
Case ".js"
Return String.Format("<script src=""{0}"" type=""text/javascript"" />", ResolveUrl(MapURL(fi.FullName)))
Case ".vbs"
Return String.Format("<script src=""{0}"" type=""text/vbscript"" />", ResolveUrl(MapURL(fi.FullName)))
Case Else
Return GetFileContents(fi)
End Select
Return ""
End Function
Private Function GetFileContents(ByVal fi As IO.FileInfo) As String
' retrieve contents of a file as a string
Dim fileStream As IO.StreamReader = Nothing
Dim fileContents As String
Try
fileStream = IO.File.OpenText(fi.FullName)
fileContents = fileStream.ReadToEnd
Return fileContents
Finally
If fileStream IsNot Nothing Then
fileStream.Close()
End If
End Try
Return ""
End Function
Public Function MapURL(ByVal Path As String) As String
Dim AppPath As String = _
HttpContext.Current.Server.MapPath("~")
Dim url As String = String.Format("~{0}" _
, Path.Replace(AppPath, "").Replace("\", "/"))
Return url
End Function
Private Sub InjectText(ByVal s As String)
' inject text into page at desired point
Dim replacementText As String = MakeReplacements(Server.HtmlDecode(s))
If _ms.Debug Then
Dim injectInto As Control = Me
injectInto = New HtmlGenericControl("pre")
InjectAtPoint(injectInto)
injectInto.Controls.Add(New LiteralControl(Server.HtmlEncode(replacementText)))
Else
InjectAtPoint(New LiteralControl(replacementText))
End If
End Sub
Private Sub InjectAtPoint(ByVal ctl As Control)
' inject control at the proper insertion point
Dim point As Control
Select Case _ms.InsertionPoint
Case "Module"
Me.Controls.Add(ctl)
Case "Head"
point = Page.FindControl("head")
If point IsNot Nothing Then
point.Controls.Add(ctl)
End If
Case "AfterForm"
point = Page.FindControl("form")
If point IsNot Nothing Then
point.Controls.AddAt(0, ctl)
End If
Case "BeforeForm"
point = Page.FindControl("form")
If point IsNot Nothing Then
point.Controls.Add(ctl)
End If
Case "AfterBody"
point = Page.FindControl("body")
If point IsNot Nothing Then
point.Controls.AddAt(0, ctl)
End If
Case "BeforeBody"
point = Page.FindControl("body")
If point IsNot Nothing Then
point.Controls.Add(ctl)
End If
End Select
End Sub
Private Function UserRoles() As String
Dim roleController As New Security.Roles.RoleController
Return String.Join(",", UserInfo.Roles)
End Function
Private Function UserRolesArray() As String
Return "'" & UserRoles.Replace(",", "','") & "'"
End Function
Private Function MakeReplacements(ByVal s As String) As String
' returns string with all token replacements made
Dim ctrl As New DotNetNuke.Entities.Modules.ModuleController
Dim settings As Hashtable = ctrl.GetModuleSettings(ModuleId)
Dim ret As String = s
' define tokens
' NOTE: make sure to add to exclusions in the Settings.ascx.vb method, InitTokenListFromString
Dim justTokens As Hashtable = JustTokenSettings()
justTokens.Add("STANDARDFOLDER", ResolveUrl("standard"))
justTokens.Add("ROLES", UserRoles())
justTokens.Add("ROLESARRAY", UserRolesArray())
' merge in querystring
Dim qsTokens As Hashtable = DNNStuff.Common.StandardVariables.QueryString(Me.Request)
justTokens = DNNStuff.Common.HashTableHelpers.Merge(justTokens, qsTokens)
' merge in servervars
Dim svTokens As Hashtable = DNNStuff.Common.StandardVariables.ServerVars(Me.Request)
justTokens = DNNStuff.Common.HashTableHelpers.Merge(justTokens, svTokens)
' merge in formvars
Dim fvTokens As Hashtable = DNNStuff.Common.StandardVariables.FormVars(Me.Request)
justTokens = DNNStuff.Common.HashTableHelpers.Merge(justTokens, fvTokens)
' logic replacement
Dim logicReplacer As New DNNStuff.Utilities.RegularExpression.IfDefinedTokenReplacement(justTokens)
ret = logicReplacer.Replace(ret)
' then built in replacement
Dim replacer As New DNNStuff.Utilities.RegularExpression.TokenReplacement(justTokens)
replacer.ReplaceIfNotFound = False
ret = replacer.Replace(ret)
' standard dnn replacement first
ret = Compatibility.ReplaceGenericTokens(Me, ret)
' replace escaped characters
ret = ReplaceEscapedCharacters(ret)
Return ret
End Function
Public Shared Function ReplaceEscapedCharacters(ByVal text As String) As String
text = Regex.Replace(text, "0x5B", "[", RegexOptions.IgnoreCase)
text = Regex.Replace(text, "0x5D", "]", RegexOptions.IgnoreCase)
Return text
End Function
Private Function JustTokenSettings() As Hashtable
Const STR_TOKEN_PREFIX As String = "Token_"
Dim justTokens As New Hashtable
For Each key As String In Settings.Keys
If key.StartsWith(STR_TOKEN_PREFIX) Then
justTokens.Add(key.Substring(STR_TOKEN_PREFIX.Length, key.Length - STR_TOKEN_PREFIX.Length), Settings(key))
End If
Next
Return justTokens
End Function
#End Region
#Region " Optional Interfaces"
Public ReadOnly Property ModuleActions() As Entities.Modules.Actions.ModuleActionCollection Implements Entities.Modules.IActionable.ModuleActions
Get
Dim Actions As New Entities.Modules.Actions.ModuleActionCollection
Actions.Add(GetNextActionID, DotNetNuke.Services.Localization.Localization.GetString(Entities.Modules.Actions.ModuleActionType.ContentOptions, LocalResourceFile), Entities.Modules.Actions.ModuleActionType.EditContent, "", "", EditUrl(), False, Security.SecurityAccessLevel.Edit, True, False)
Actions.Add(GetNextActionID, DotNetNuke.Services.Localization.Localization.GetString("UploadScript", LocalResourceFile), Entities.Modules.Actions.ModuleActionType.ImportModule, "", "", EditUrl("UploadScript"), False, Security.SecurityAccessLevel.Host, True, False)
Return Actions
End Get
End Property
#End Region
End Class
End Namespace