-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathviewsource.ascx.cs
558 lines (497 loc) · 23.3 KB
/
viewsource.ascx.cs
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information
namespace Dnn.Module.ModuleCreator
{
using System;
using System.IO;
using System.Web.UI.WebControls;
using DotNetNuke.Abstractions;
using DotNetNuke.Abstractions.Application;
using DotNetNuke.Abstractions.Portals;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Controllers;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Definitions;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Framework;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Installer.Packages;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Log.EventLog;
using DotNetNuke.UI.Skins.Controls;
using Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Codebehind implementation for the ViewSource view.
/// </summary>
public partial class ViewSource : PortalModuleBase
{
private readonly INavigationManager navigationManager;
private readonly IApplicationStatusInfo applicationStatusInfo;
private readonly IPortalSettings portalSettings;
/// <summary>
/// Initializes a new instance of the <see cref="ViewSource"/> class.
/// </summary>
public ViewSource()
{
this.navigationManager = this.DependencyProvider.GetRequiredService<INavigationManager>();
this.applicationStatusInfo = this.DependencyProvider.GetRequiredService<IApplicationStatusInfo>();
this.portalSettings = PortalController.Instance.GetCurrentSettings();
}
/// <summary>
/// Gets the module control id.
/// </summary>
protected int ModuleControlId
{
get
{
var moduleControlId = Null.NullInteger;
if (this.Request.QueryString["ctlid"] != null)
{
moduleControlId = int.Parse(this.Request.QueryString["ctlid"]);
}
return moduleControlId;
}
}
private string ReturnURL
{
get
{
return UrlUtils.ValidReturnUrl(this.Request.Params["ReturnURL"]) ?? this.navigationManager.NavigateURL();
}
}
/// <inheritdoc/>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
ServicesFramework.Instance.RequestAjaxAntiForgerySupport();
this.cboFile.SelectedIndexChanged += this.OnFileIndexChanged;
this.optLanguage.SelectedIndexChanged += this.OnLanguageSelectedIndexChanged;
this.cboTemplate.SelectedIndexChanged += this.CboTemplateSelectedIndexChanged;
this.cmdUpdate.Click += this.OnUpdateClick;
this.cmdPackage.Click += this.OnPackageClick;
this.cmdConfigure.Click += this.OnConfigureClick;
this.cmdCreate.Click += this.OnCreateClick;
if (this.Page.IsPostBack == false)
{
this.cmdCancel1.NavigateUrl = this.ReturnURL;
this.cmdCancel2.NavigateUrl = this.ReturnURL;
var objModuleControl = ModuleControlController.GetModuleControl(this.ModuleControlId);
if (objModuleControl != null)
{
this.BindFiles(objModuleControl.ControlSrc);
this.LoadFile();
}
if (this.Request.UrlReferrer != null)
{
this.ViewState["UrlReferrer"] = Convert.ToString(this.Request.UrlReferrer);
}
else
{
this.ViewState["UrlReferrer"] = string.Empty;
}
this.LoadLanguages();
this.LoadModuleTemplates();
if (this.cboTemplate.Items.FindByText("Module - User Control") != null)
{
this.cboTemplate.Items.FindByText("Module - User Control").Selected = true;
}
this.LoadReadMe();
}
}
/// <summary>
/// Fires up when the file selector changed selection.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The event arguments.</param>
protected void OnFileIndexChanged(object sender, EventArgs e)
{
this.LoadFile();
}
/// <summary>
/// Fires up when the language selector changed.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The event arguments.</param>
protected void OnLanguageSelectedIndexChanged(object sender, EventArgs e)
{
this.LoadModuleTemplates();
}
/// <summary>
/// Fires up when the selected template changed.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The event arguments.</param>
protected void CboTemplateSelectedIndexChanged(object sender, EventArgs e)
{
this.LoadReadMe();
}
private void BindFiles(string controlSrc)
{
string[] fileList;
this.cboFile.Items.Clear();
var objModuleControl = ModuleControlController.GetModuleControl(this.ModuleControlId);
var objModuleDefinition = ModuleDefinitionController.GetModuleDefinitionByID(objModuleControl.ModuleDefID);
var objDesktopModule = DesktopModuleController.GetDesktopModule(objModuleDefinition.DesktopModuleID, this.PortalId);
var relativePath = $"DesktopModules/{(objModuleControl.ControlSrc.EndsWith(".mvc") ? "MVC/" : string.Empty)}{objDesktopModule.FolderName}/";
var modulePath = this.Server.MapPath(relativePath);
if (Directory.Exists(modulePath))
{
// iterate through files in desktopmodules folder
fileList = Directory.GetFiles(modulePath, "*", SearchOption.AllDirectories);
foreach (string filePath in fileList)
{
switch (Path.GetExtension(filePath).ToLowerInvariant())
{
case ".ascx":
this.cboFile.Items.Add(new ListItem(filePath.Substring(modulePath.Length), filePath));
var resxPath = filePath.Replace(Path.GetFileName(filePath), "App_LocalResources\\" + Path.GetFileName(filePath)) + ".resx";
if (File.Exists(resxPath))
{
this.cboFile.Items.Add(new ListItem(filePath.Substring(modulePath.Length), resxPath));
}
break;
case ".vb":
case ".cs":
case ".vbhtml":
case ".cshtml":
case ".css":
case ".js":
case ".txt":
case ".html":
case ".xml":
case ".xslt":
case ".sql":
case ".sqldataprovider":
this.cboFile.Items.Add(new ListItem(filePath.Substring(modulePath.Length), filePath));
break;
}
}
}
else
{
DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("FolderNameInvalid", this.LocalResourceFile), ModuleMessage.ModuleMessageType.RedError);
}
// iterate through files in app_code folder
modulePath = this.applicationStatusInfo.ApplicationMapPath + "\\App_Code\\" + objDesktopModule.FolderName.Replace("/", "\\") + "\\";
if (Directory.Exists(modulePath))
{
fileList = Directory.GetFiles(modulePath);
foreach (string filePath in fileList)
{
switch (Path.GetExtension(filePath).ToLowerInvariant())
{
case ".vb":
this.cboFile.Items.Add(new ListItem(Path.GetFileName(filePath), filePath));
break;
case ".cs":
this.cboFile.Items.Add(new ListItem(Path.GetFileName(filePath), filePath));
break;
}
}
}
// select file
if (this.cboFile.Items.FindByValue(this.applicationStatusInfo.ApplicationMapPath + "\\" + controlSrc.Replace("/", "\\")) != null)
{
this.cboFile.Items.FindByValue(this.applicationStatusInfo.ApplicationMapPath + "\\" + controlSrc.Replace("/", "\\")).Selected = true;
}
}
private void LoadFile()
{
this.lblPath.Text = this.cboFile.SelectedValue;
var objStreamReader = File.OpenText(this.lblPath.Text);
this.txtSource.Text = objStreamReader.ReadToEnd();
objStreamReader.Close();
this.SetFileType(this.lblPath.Text);
}
private void SetFileType(string filePath)
{
string mimeType;
switch (Path.GetExtension(filePath).ToLowerInvariant())
{
case ".vb":
mimeType = "text/x-vb";
break;
case ".cs":
mimeType = "text/x-csharp";
break;
case ".css":
mimeType = "text/css";
break;
case ".js":
mimeType = "text/javascript";
break;
case ".xml":
case ".xslt":
mimeType = "application/xml";
break;
case ".sql":
case ".sqldataprovider":
mimeType = "text/x-sql";
break;
default:
mimeType = "text/html";
break;
}
DotNetNuke.UI.Utilities.ClientAPI.RegisterClientVariable(this.Page, "mimeType", mimeType, true);
}
private void SaveFile()
{
try
{
File.SetAttributes(this.cboFile.SelectedValue, FileAttributes.Normal);
var objStream = File.CreateText(this.cboFile.SelectedValue);
objStream.WriteLine(this.txtSource.Text);
objStream.Close();
DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("ControlUpdated", this.LocalResourceFile), ModuleMessage.ModuleMessageType.GreenSuccess);
}
catch (Exception exc)
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
private void LoadLanguages()
{
this.optLanguage.Items.Clear();
var moduleTemplatePath = this.Server.MapPath(this.ControlPath) + "Templates";
string[] folderList = Directory.GetDirectories(moduleTemplatePath);
foreach (string folderPath in folderList)
{
this.optLanguage.Items.Add(new ListItem(Path.GetFileName(folderPath)));
}
var language = string.Empty;
foreach (ListItem objFile in this.cboFile.Items)
{
if (objFile.Text.EndsWith(".vb"))
{
language = "VB";
}
if (objFile.Text.EndsWith(".cs"))
{
language = "C#";
}
}
if (language == string.Empty)
{
this.optLanguage.SelectedIndex = 0;
}
else
{
this.optLanguage.Items.FindByValue(language).Selected = true;
}
}
private void LoadModuleTemplates()
{
this.cboTemplate.Items.Clear();
var moduleTemplatePath = this.Server.MapPath(this.ControlPath) + "Templates\\" + this.optLanguage.SelectedValue;
string[] folderList = Directory.GetDirectories(moduleTemplatePath);
foreach (string folderPath in folderList)
{
this.cboTemplate.Items.Add(new ListItem(Path.GetFileName(folderPath)));
}
this.cboTemplate.Items.Insert(0, new ListItem("<" + Localization.GetString("Not_Specified", Localization.SharedResourceFile) + ">", string.Empty));
}
private void LoadReadMe()
{
var templatePath = this.Server.MapPath(this.ControlPath) + "Templates\\" + this.optLanguage.SelectedValue + "\\" + this.cboTemplate.SelectedItem.Value;
if (File.Exists(templatePath + "\\readme.txt"))
{
var readMe = Null.NullString;
using (TextReader tr = new StreamReader(templatePath + "\\readme.txt"))
{
readMe = tr.ReadToEnd();
tr.Close();
}
this.lblDescription.Text = readMe.Replace("\n", "<br/>");
}
else
{
this.lblDescription.Text = string.Empty;
}
// Determine if Control Name is required
var controlNameRequired = false;
var controlName = "<Not Required>";
string[] fileList = Directory.GetFiles(templatePath);
foreach (string filePath in fileList)
{
if (Path.GetFileName(filePath).ToLowerInvariant().IndexOf("template") > -1)
{
controlNameRequired = true;
controlName = "Edit";
}
else
{
if (Path.GetFileName(filePath).EndsWith(".ascx"))
{
controlName = Path.GetFileNameWithoutExtension(filePath);
}
}
}
this.txtControl.Text = controlName;
this.txtControl.Enabled = controlNameRequired;
if (this.txtControl.Enabled)
{
if (!this.cboTemplate.SelectedItem.Value.ToLowerInvariant().StartsWith("module"))
{
var objModuleControl = ModuleControlController.GetModuleControl(this.ModuleControlId);
var objModuleDefinition = ModuleDefinitionController.GetModuleDefinitionByID(objModuleControl.ModuleDefID);
var objDesktopModule = DesktopModuleController.GetDesktopModule(objModuleDefinition.DesktopModuleID, this.PortalId);
this.txtControl.Text = objDesktopModule.FriendlyName;
}
}
}
private string CreateModuleControl()
{
var objModuleControl = ModuleControlController.GetModuleControl(this.ModuleControlId);
var objModuleDefinition = ModuleDefinitionController.GetModuleDefinitionByID(objModuleControl.ModuleDefID);
var objDesktopModule = DesktopModuleController.GetDesktopModule(objModuleDefinition.DesktopModuleID, this.PortalId);
var objPackage = PackageController.Instance.GetExtensionPackage(Null.NullInteger, p => p.PackageID == objDesktopModule.PackageID);
var moduleTemplatePath = this.Server.MapPath(this.ControlPath) + "Templates\\" + this.optLanguage.SelectedValue + "\\" + this.cboTemplate.SelectedValue + "\\";
EventLogController.Instance.AddLog("Processing Template Folder", moduleTemplatePath, this.portalSettings, -1, EventLogController.EventLogType.HOST_ALERT);
var controlName = Null.NullString;
var fileName = Null.NullString;
var modulePath = Null.NullString;
var sourceCode = Null.NullString;
// iterate through files in template folder
string[] fileList = Directory.GetFiles(moduleTemplatePath);
foreach (string filePath in fileList)
{
modulePath = this.Server.MapPath("DesktopModules/" + objDesktopModule.FolderName + "/");
// open file
using (TextReader tr = new StreamReader(filePath))
{
sourceCode = tr.ReadToEnd();
tr.Close();
}
// replace tokens
var owner = objPackage.Owner.Replace(" ", string.Empty);
if (string.IsNullOrEmpty(owner))
{
owner = "DNN";
}
sourceCode = sourceCode.Replace("_OWNER_", owner);
sourceCode = sourceCode.Replace("_MODULE_", objDesktopModule.FriendlyName.Replace(" ", string.Empty));
sourceCode = sourceCode.Replace("_CONTROL_", this.GetControl());
sourceCode = sourceCode.Replace("_YEAR_", DateTime.Now.Year.ToString());
// get filename
fileName = Path.GetFileName(filePath);
fileName = fileName.Replace("template", this.GetControl());
fileName = fileName.Replace("_OWNER_", objPackage.Owner.Replace(" ", string.Empty));
fileName = fileName.Replace("_MODULE_", objDesktopModule.FriendlyName.Replace(" ", string.Empty));
fileName = fileName.Replace("_CONTROL_", this.GetControl());
switch (Path.GetExtension(filePath).ToLowerInvariant())
{
case ".ascx":
controlName = fileName;
break;
case ".vbhtml":
controlName = fileName;
break;
case ".cshtml":
controlName = fileName;
break;
case ".resx":
modulePath = modulePath + "\\App_LocalResources\\";
break;
case ".vb":
if (filePath.ToLowerInvariant().IndexOf(".ascx") == -1)
{
modulePath = modulePath.Replace("DesktopModules", "App_Code");
}
break;
case ".cs":
if (filePath.ToLowerInvariant().IndexOf(".ascx") == -1)
{
modulePath = modulePath.Replace("DesktopModules", "App_Code");
}
break;
case ".js":
modulePath = modulePath + "\\js\\";
break;
}
// check if folder exists
if (!Directory.Exists(modulePath))
{
Directory.CreateDirectory(modulePath);
}
// check if file already exists
if (!File.Exists(modulePath + fileName))
{
// create file
using (TextWriter tw = new StreamWriter(modulePath + fileName))
{
tw.WriteLine(sourceCode);
tw.Close();
}
EventLogController.Instance.AddLog("Created File", modulePath + fileName, this.portalSettings, -1, EventLogController.EventLogType.HOST_ALERT);
}
}
// Create module control
if (controlName != Null.NullString)
{
try
{
objModuleControl = new ModuleControlInfo();
objModuleControl.ModuleControlID = Null.NullInteger;
objModuleControl.ModuleDefID = objModuleDefinition.ModuleDefID;
objModuleControl.ControlKey = this.GetControl();
objModuleControl.ControlSrc = "DesktopModules/" + objDesktopModule.FolderName + "/" + controlName;
objModuleControl.ControlTitle = this.txtControl.Text;
objModuleControl.ControlType = (SecurityAccessLevel)Enum.Parse(typeof(SecurityAccessLevel), this.cboType.SelectedItem.Value);
objModuleControl.HelpURL = string.Empty;
objModuleControl.IconFile = string.Empty;
objModuleControl.ViewOrder = 0;
objModuleControl.SupportsPartialRendering = true;
objModuleControl.SupportsPopUps = true;
ModuleControlController.AddModuleControl(objModuleControl);
controlName = objModuleControl.ControlSrc;
}
catch
{
// Suppress error
}
}
DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("ControlCreated", this.LocalResourceFile), ModuleMessage.ModuleMessageType.GreenSuccess);
return controlName;
}
private string GetControl()
{
return this.txtControl.Text.Replace(" ", string.Empty);
}
private void OnUpdateClick(object sender, EventArgs e)
{
this.SaveFile();
}
private void OnPackageClick(object sender, EventArgs e)
{
var objModuleControl = ModuleControlController.GetModuleControl(this.ModuleControlId);
var objModuleDefinition = ModuleDefinitionController.GetModuleDefinitionByID(objModuleControl.ModuleDefID);
var objDesktopModule = DesktopModuleController.GetDesktopModule(objModuleDefinition.DesktopModuleID, this.PortalId);
ModuleInfo objModule = ModuleController.Instance.GetModuleByDefinition(-1, "Extensions");
this.Response.Redirect(this.navigationManager.NavigateURL(objModule.TabID, "PackageWriter", "rtab=" + this.TabId.ToString(), "packageId=" + objDesktopModule.PackageID.ToString(), "mid=" + objModule.ModuleID.ToString()) + "?popUp=true", true);
}
private void OnConfigureClick(object sender, EventArgs e)
{
var objModuleControl = ModuleControlController.GetModuleControl(this.ModuleControlId);
var objModuleDefinition = ModuleDefinitionController.GetModuleDefinitionByID(objModuleControl.ModuleDefID);
var objDesktopModule = DesktopModuleController.GetDesktopModule(objModuleDefinition.DesktopModuleID, this.PortalId);
ModuleInfo objModule = ModuleController.Instance.GetModuleByDefinition(-1, "Extensions");
this.Response.Redirect(this.navigationManager.NavigateURL(objModule.TabID, "Edit", "mid=" + objModule.ModuleID.ToString(), "PackageID=" + objDesktopModule.PackageID.ToString()) + "?popUp=true", true);
}
private void OnCreateClick(object sender, EventArgs e)
{
if (this.cboTemplate.SelectedIndex > 0 && this.txtControl.Text != string.Empty)
{
var controlSrc = this.CreateModuleControl();
this.BindFiles(controlSrc);
this.LoadFile();
}
else
{
DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("AddControlError", this.LocalResourceFile), ModuleMessage.ModuleMessageType.YellowWarning);
}
}
}
}