forked from dnnsoftware/Dnn.Platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHtmlTextController.cs
823 lines (705 loc) · 37.1 KB
/
HtmlTextController.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
// 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 DotNetNuke.Modules.Html
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Xml;
using DotNetNuke.Abstractions;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Content.Taxonomy;
using DotNetNuke.Entities.Content.Workflow;
using DotNetNuke.Entities.Content.Workflow.Entities;
using DotNetNuke.Entities.Content.Workflow.Repositories;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Users;
using DotNetNuke.Internal.SourceGenerators;
using DotNetNuke.Modules.Html.Components;
using DotNetNuke.Security;
using DotNetNuke.Security.Permissions;
using DotNetNuke.Security.Roles;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Personalization;
using DotNetNuke.Services.Search.Entities;
using DotNetNuke.Services.Social.Notifications;
using DotNetNuke.Services.Tokens;
using Microsoft.Extensions.DependencyInjection;
/// <summary>The HtmlTextController is the Controller class for managing HtmlText information the HtmlText module.</summary>
public partial class HtmlTextController : ModuleSearchBase, IPortable, IUpgradeable, IVersionable
{
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore", Justification = "Breaking Change")]
public const int MAX_DESCRIPTION_LENGTH = 100;
private const string PortalRootToken = "{{PortalRoot}}";
private readonly IWorkflowManager workflowManager = WorkflowManager.Instance;
/// <summary>Initializes a new instance of the <see cref="HtmlTextController"/> class.</summary>
public HtmlTextController()
: this(Globals.GetCurrentServiceProvider().GetRequiredService<INavigationManager>())
{
}
/// <summary>Initializes a new instance of the <see cref="HtmlTextController"/> class.</summary>
/// <param name="navigationManager"></param>
public HtmlTextController(INavigationManager navigationManager)
{
this.NavigationManager = navigationManager;
}
protected INavigationManager NavigationManager { get; }
/// <summary>FormatHtmlText formats HtmlText content for display in the browser.</summary>
/// <param name="moduleId">The ModuleID.</param>
/// <param name="content">The HtmlText Content.</param>
/// <param name="settings">Module Settings.</param>
/// <param name="portalSettings">The Portal Settings.</param>
/// <param name="page">The Page Instance.</param>
/// <returns>The formatted HTML content.</returns>
public static string FormatHtmlText(int moduleId, string content, HtmlModuleSettings settings, PortalSettings portalSettings, Page page)
{
// Html decode content
content = HttpUtility.HtmlDecode(content);
// token replace
if (settings.ReplaceTokens)
{
var tr = new HtmlTokenReplace(page)
{
AccessingUser = UserController.Instance.GetCurrentUserInfo(),
DebugMessages = Personalization.GetUserMode() != PortalSettings.Mode.View,
ModuleId = moduleId,
PortalSettings = portalSettings,
};
content = tr.ReplaceEnvironmentTokens(content);
}
// manage relative paths
content = ManageRelativePaths(content, portalSettings.HomeDirectory, "src", portalSettings.PortalId);
content = ManageRelativePaths(content, portalSettings.HomeDirectory, "background", portalSettings.PortalId);
return content;
}
[DnnDeprecated(9, 11, 0, "Use overload without int")]
public static partial string ManageRelativePaths(string htmlContent, string strUploadDirectory, string strToken, int intPortalID)
{
return ManageRelativePaths(htmlContent, strUploadDirectory, strToken);
}
public static string ManageRelativePaths(string htmlContent, string uploadDirectory, string token)
{
var htmlContentIndex = 0;
var sbBuff = new StringBuilder(string.Empty);
if (string.IsNullOrEmpty(htmlContent))
{
return string.Empty;
}
token = token + "=\"";
var tokenLength = token.Length;
uploadDirectory = uploadDirectory.ToLowerInvariant();
// find position of first occurrence:
var tokenIndex = htmlContent.IndexOf(token, StringComparison.InvariantCultureIgnoreCase);
while (tokenIndex != -1)
{
sbBuff.Append(htmlContent.Substring(htmlContentIndex, tokenIndex - htmlContentIndex + tokenLength));
// keep characters left of URL
htmlContentIndex = tokenIndex + tokenLength;
// save start position of URL
var urlEndIndex = htmlContent.IndexOf('\"', htmlContentIndex);
// end of URL
string strURL;
if (urlEndIndex >= 0 && urlEndIndex < htmlContent.Length - 2)
{
strURL = htmlContent.Substring(htmlContentIndex, urlEndIndex - htmlContentIndex).ToLowerInvariant();
}
else
{
tokenIndex = -1;
continue;
}
if (htmlContent.Substring(tokenIndex + tokenLength, 10).Equals("data:image", StringComparison.InvariantCultureIgnoreCase))
{
tokenIndex = htmlContent.IndexOf(token, htmlContentIndex + strURL.Length + 2, StringComparison.InvariantCultureIgnoreCase);
continue;
}
// if we are linking internally
if (!strURL.Contains("://"))
{
// remove the leading portion of the path if the URL contains the upload directory structure
var strDirectory = uploadDirectory;
if (!strDirectory.EndsWith("/", StringComparison.Ordinal))
{
strDirectory += "/";
}
if (strURL.IndexOf(strDirectory, StringComparison.InvariantCultureIgnoreCase) != -1)
{
htmlContentIndex = htmlContentIndex + strURL.IndexOf(strDirectory, StringComparison.InvariantCultureIgnoreCase) + strDirectory.Length;
strURL = strURL.Substring(strURL.IndexOf(strDirectory, StringComparison.InvariantCultureIgnoreCase) + strDirectory.Length);
}
// add upload directory
// We don't write the UploadDirectory if the token/attribute has not value. Therefore we will avoid an unnecessary request
if (!strURL.StartsWith("/", StringComparison.Ordinal) && !string.IsNullOrWhiteSpace(strURL))
{
sbBuff.Append(uploadDirectory);
}
}
// find position of next occurrence
tokenIndex = htmlContent.IndexOf(token, htmlContentIndex + strURL.Length + 2, StringComparison.InvariantCultureIgnoreCase);
}
// append characters of last URL and behind
if (htmlContentIndex > -1)
{
sbBuff.Append(htmlContent.Substring(htmlContentIndex));
}
return sbBuff.ToString();
}
public string ReplaceWithRootToken(Match m)
{
var domain = m.Groups["domain"].Value;
// Relative url
if (string.IsNullOrEmpty(domain))
{
return PortalRootToken;
}
var aliases = PortalAliasController.Instance.GetPortalAliases();
if (!aliases.Contains(domain))
{
// this is no not a portal url so even if it contains /portals/..
// we do not need to replace it with a token
return m.ToString();
}
// full qualified portal url that needs to be tokenized
var result = domain + PortalRootToken;
var protocol = m.Groups["protocol"].Value;
return string.IsNullOrEmpty(protocol) ? result : protocol + result;
}
/// <summary>DeleteHtmlText deletes an HtmlTextInfo object for the Module and Item.</summary>
/// <param name="moduleID">The ID of the Module.</param>
/// <param name="itemID">The ID of the Item.</param>
public void DeleteHtmlText(int moduleID, int itemID)
{
var moduleVersion = this.GetHtmlText(moduleID, itemID).Version;
DataProvider.Instance().DeleteHtmlText(moduleID, itemID);
// refresh output cache
ModuleController.SynchronizeModule(moduleID);
TrackModuleModification(moduleID, moduleVersion);
}
/// <summary>GetAllHtmlText gets a collection of HtmlTextInfo objects for the Module and Workflow.</summary>
/// <param name="moduleID">The ID of the Module.</param>
/// <returns>A <see cref="List{T}"/> of <see cref="HtmlTextInfo"/> instances.</returns>
public List<HtmlTextInfo> GetAllHtmlText(int moduleID)
{
return CBO.FillCollection<HtmlTextInfo>(DataProvider.Instance().GetAllHtmlText(moduleID));
}
/// <summary>GetHtmlText gets the HtmlTextInfo object for the Module, Item, and Workflow.</summary>
/// <param name="moduleID">The ID of the Module.</param>
/// <param name="itemID">The ID of the Item.</param>
/// <returns>An <see cref="HtmlTextInfo"/> instance or <see langword="null"/>.</returns>
public HtmlTextInfo GetHtmlText(int moduleID, int itemID)
{
return CBO.FillObject<HtmlTextInfo>(DataProvider.Instance().GetHtmlText(moduleID, itemID));
}
/// <summary>GetTopHtmlText gets the most recent HtmlTextInfo object for the Module, Workflow, and State.</summary>
/// <param name="moduleId">The ID of the Module.</param>
/// <param name="isPublished">Whether the content has been published or not.</param>
/// <param name="workflowId">The Workflow ID.</param>
/// <returns>An <see cref="HtmlTextInfo"/> instance or <see langword="null"/>.</returns>
public HtmlTextInfo GetTopHtmlText(int moduleId, bool isPublished, int workflowId)
{
var htmlText = CBO.FillObject<HtmlTextInfo>(DataProvider.Instance().GetTopHtmlText(moduleId, isPublished));
if (htmlText != null)
{
// check if workflow has changed
if (isPublished == false && htmlText.WorkflowID != workflowId)
{
// get proper state for workflow
htmlText.WorkflowID = workflowId;
htmlText.WorkflowName = "[REPAIR_WORKFLOW]";
htmlText.StateID = htmlText.IsPublished
? this.workflowManager.GetWorkflow(workflowId).LastState.StateID
: this.workflowManager.GetWorkflow(workflowId).FirstState.StateID;
// update object
this.UpdateHtmlText(htmlText, this.GetMaximumVersionHistory(htmlText.PortalID));
// get object again
htmlText = CBO.FillObject<HtmlTextInfo>(DataProvider.Instance().GetTopHtmlText(moduleId, false));
}
}
return htmlText;
}
/// <summary>GetWorkFlow retrieves the currently active Workflow for the Portal.</summary>
/// <param name="moduleId">The ID of the Module.</param>
/// <param name="tabId">The Tab ID.</param>
/// <param name="portalId">The ID of the Portal.</param>
/// <returns>A <see cref="KeyValuePair{TKey,TValue}"/> where the <see cref="KeyValuePair{TKey,TValue}.Key"/> is the workflow type, and the <see cref="KeyValuePair{TKey,TValue}.Value"/> is the workflow ID.</returns>
public KeyValuePair<string, int> GetWorkflow(int moduleId, int tabId, int portalId)
{
var tab = TabController.Instance.GetTab(tabId, portalId);
var workFlowId = tab.StateID == Null.NullInteger
? TabWorkflowSettings.Instance.GetDefaultTabWorkflowId(portalId)
: WorkflowStateManager.Instance.GetWorkflowState(tab.StateID).WorkflowID;
var workFlowType = WorkflowManager.Instance.GetWorkflow(workFlowId).WorkflowName;
return new KeyValuePair<string, int>(workFlowType, workFlowId);
}
/// <summary>UpdateHtmlText creates a new HtmlTextInfo object or updates an existing HtmlTextInfo object.</summary>
/// <param name="htmlContent">An HtmlTextInfo object.</param>
/// <param name="maximumVersionHistory">The maximum number of versions to retain.</param>
public void UpdateHtmlText(HtmlTextInfo htmlContent, int maximumVersionHistory)
{
bool blnCreateNewVersion = false;
// determine if we are creating a new version of content or updating an existing version
if (htmlContent.ItemID != -1)
{
if (htmlContent.WorkflowName != "[REPAIR_WORKFLOW]")
{
HtmlTextInfo objContent = this.GetTopHtmlText(htmlContent.ModuleID, false, htmlContent.WorkflowID);
if (objContent != null)
{
if (objContent.StateID == this.workflowManager.GetWorkflow(htmlContent.WorkflowID).LastState.StateID)
{
blnCreateNewVersion = true;
}
}
}
}
else
{
blnCreateNewVersion = true;
}
// determine if content is published
if (htmlContent.StateID == this.workflowManager.GetWorkflow(htmlContent.WorkflowID).LastState.StateID)
{
htmlContent.IsPublished = true;
}
else
{
htmlContent.IsPublished = false;
}
if (blnCreateNewVersion)
{
// add content
htmlContent.ItemID = DataProvider.Instance().AddHtmlText(
htmlContent.ModuleID,
htmlContent.Content,
htmlContent.Summary,
htmlContent.StateID,
htmlContent.IsPublished,
UserController.Instance.GetCurrentUserInfo().UserID,
maximumVersionHistory);
}
else
{
// update content
DataProvider.Instance().UpdateHtmlText(htmlContent.ItemID, htmlContent.Content, htmlContent.Summary, htmlContent.StateID, htmlContent.IsPublished, UserController.Instance.GetCurrentUserInfo().UserID);
}
// refresh to get version
htmlContent = this.GetHtmlText(htmlContent.ModuleID, htmlContent.ItemID);
// add log history
var logInfo = new HtmlTextLogInfo();
logInfo.ItemID = htmlContent.ItemID;
logInfo.StateID = htmlContent.StateID;
logInfo.Approved = htmlContent.Approved;
logInfo.Comment = htmlContent.Comment;
var objLogs = new HtmlTextLogController();
objLogs.AddHtmlTextLog(logInfo);
// create user notifications
this.CreateUserNotifications(htmlContent);
// refresh output cache
ModuleController.SynchronizeModule(htmlContent.ModuleID);
var moduleVersion = htmlContent.Version < 1 ? 1 : htmlContent.Version;
TrackModuleModification(htmlContent.ModuleID, moduleVersion);
}
/// <summary>UpdateWorkFlow updates the currently active Workflow.</summary>
/// <param name="objectID">The ID of the object to apply the update to (depends on WorkFlowType).</param>
/// <param name="workFlowType">The type of workflow (Module | Page | Site).</param>
/// <param name="workflowID">The ID of the Workflow.</param>
/// <param name="replaceExistingSettings">Should existing settings be overwritten?.</param>
public void UpdateWorkflow(int objectID, string workFlowType, int workflowID, bool replaceExistingSettings)
{
switch (workFlowType)
{
case "Module":
ModuleController.Instance.UpdateModuleSetting(objectID, "WorkflowID", workflowID.ToString());
break;
case "Page":
TabController.Instance.UpdateTabSetting(objectID, "WorkflowID", workflowID.ToString());
if (replaceExistingSettings)
{
// Get All Modules on the current Tab
foreach (var kvp in ModuleController.Instance.GetTabModules(objectID))
{
this.ClearModuleSettings(kvp.Value);
}
}
break;
case "Site":
PortalController.UpdatePortalSetting(objectID, "WorkflowID", workflowID.ToString());
if (replaceExistingSettings)
{
// Get All Tabs aon the Site
foreach (var kvp in TabController.Instance.GetTabsByPortal(objectID))
{
TabController.Instance.DeleteTabSetting(kvp.Value.TabID, "WorkFlowID");
}
// Get All Modules in the current Site
foreach (ModuleInfo objModule in ModuleController.Instance.GetModules(objectID))
{
this.ClearModuleSettings(objModule);
}
}
break;
}
}
/// <summary>GetMaximumVersionHistory retrieves the maximum number of versions to store for a module.</summary>
/// <param name="portalID">The ID of the Portal.</param>
/// <returns>The maximum number of versions for the portal (defaults to <c>5</c>).</returns>
public int GetMaximumVersionHistory(int portalID)
{
int intMaximumVersionHistory = -1;
// get from portal settings
intMaximumVersionHistory = int.Parse(PortalController.GetPortalSetting("MaximumVersionHistory", portalID, "-1"));
// if undefined at portal level, set portal default
if (intMaximumVersionHistory == -1)
{
intMaximumVersionHistory = 5;
// default
PortalController.UpdatePortalSetting(portalID, "MaximumVersionHistory", intMaximumVersionHistory.ToString());
}
return intMaximumVersionHistory;
}
/// <summary>UpdateWorkFlowID updates the currently active WorkflowID for the Portal.</summary>
/// <param name="portalID">The ID of the Portal.</param>
/// <param name="maximumVersionHistory">The MaximumVersionHistory.</param>
public void UpdateMaximumVersionHistory(int portalID, int maximumVersionHistory)
{
// data integrity check
if (maximumVersionHistory < 0)
{
maximumVersionHistory = 5;
// default
}
// save portal setting
PortalSettings objPortalSettings = PortalController.Instance.GetCurrentPortalSettings();
if (PortalSecurity.IsInRole(objPortalSettings.AdministratorRoleName))
{
PortalController.UpdatePortalSetting(portalID, "MaximumVersionHistory", maximumVersionHistory.ToString());
}
}
public void DeleteVersion(int moduleId, int version)
{
var htmlContent = this.GetAllHtmlText(moduleId).First(h => h.Version == version || h.Version == this.GetLatestVersion(moduleId));
this.DeleteHtmlText(moduleId, htmlContent.ItemID);
}
public int RollBackVersion(int moduleId, int version)
{
throw new NotImplementedException();
}
public void PublishVersion(int moduleId, int version)
{
var htmlContent = this.GetAllHtmlText(moduleId).First(h => h.Version == version || h.Version == this.GetLatestVersion(moduleId));
htmlContent.StateID = this.workflowManager.GetWorkflow(htmlContent.WorkflowID).LastState.StateID;
this.UpdateHtmlText(htmlContent, this.GetMaximumVersionHistory(htmlContent.PortalID));
}
public int GetPublishedVersion(int moduleId)
{
var htmlText = CBO.FillObject<HtmlTextInfo>(DataProvider.Instance().GetTopHtmlText(moduleId, true));
return htmlText.Version;
}
public int GetLatestVersion(int moduleId)
{
var htmlText = CBO.FillObject<HtmlTextInfo>(DataProvider.Instance().GetTopHtmlText(moduleId, false));
return htmlText.Version;
}
/// <inheritdoc />
public string ExportModule(int moduleId)
{
string xml = string.Empty;
ModuleInfo module = ModuleController.Instance.GetModule(moduleId, Null.NullInteger, true);
int workflowID = this.GetWorkflow(moduleId, module.TabID, module.PortalID).Value;
HtmlTextInfo content = this.GetTopHtmlText(moduleId, true, workflowID);
if (content != null)
{
xml += "<htmltext>";
xml += "<content>" + XmlUtils.XMLEncode(this.TokeniseLinks(content.Content, module.PortalID)) + "</content>";
xml += "</htmltext>";
}
return xml;
}
/// <inheritdoc />
public void ImportModule(int moduleID, string content, string version, int userId)
{
ModuleInfo module = ModuleController.Instance.GetModule(moduleID, Null.NullInteger, true);
int workflowID = this.GetWorkflow(moduleID, module.TabID, module.PortalID).Value;
XmlNode xml = Globals.GetContent(content, "htmltext");
var htmlContent = new HtmlTextInfo();
htmlContent.ModuleID = moduleID;
// convert Version to System.Version
var objVersion = new Version(version);
if (objVersion >= new Version(5, 1, 0))
{
// current module content
htmlContent.Content = this.DeTokeniseLinks(xml.SelectSingleNode("content").InnerText, module.PortalID);
}
else
{
// legacy module content
htmlContent.Content = this.DeTokeniseLinks(xml.SelectSingleNode("desktophtml").InnerText, module.PortalID);
}
htmlContent.WorkflowID = workflowID;
htmlContent.StateID = this.workflowManager.GetWorkflow(workflowID).FirstState.StateID;
// import
this.UpdateHtmlText(htmlContent, this.GetMaximumVersionHistory(module.PortalID));
}
/// <inheritdoc/>
public override IList<SearchDocument> GetModifiedSearchDocuments(ModuleInfo modInfo, DateTime beginDateUtc)
{
var workflowId = this.GetWorkflow(modInfo.ModuleID, modInfo.TabID, modInfo.PortalID).Value;
var searchDocuments = new List<SearchDocument>();
var htmlTextInfo = this.GetTopHtmlText(modInfo.ModuleID, true, workflowId);
var repo = new HtmlModuleSettingsRepository();
var settings = repo.GetSettings(modInfo);
if (htmlTextInfo != null &&
(htmlTextInfo.LastModifiedOnDate.ToUniversalTime() > beginDateUtc &&
htmlTextInfo.LastModifiedOnDate.ToUniversalTime() < DateTime.UtcNow))
{
var strContent = HtmlUtils.Clean(htmlTextInfo.Content, false);
// Get the description string
var description = strContent.Length <= settings.SearchDescLength ? strContent : HtmlUtils.Shorten(strContent, settings.SearchDescLength, "...");
var searchDoc = new SearchDocument
{
UniqueKey = modInfo.ModuleID.ToString(),
PortalId = modInfo.PortalID,
Title = modInfo.ModuleTitle,
Description = description,
Body = strContent,
ModifiedTimeUtc = htmlTextInfo.LastModifiedOnDate.ToUniversalTime(),
};
if (modInfo.Terms != null && modInfo.Terms.Count > 0)
{
searchDoc.Tags = CollectHierarchicalTags(modInfo.Terms);
}
searchDocuments.Add(searchDoc);
}
return searchDocuments;
}
/// <inheritdoc/>
public string UpgradeModule(string version)
{
switch (version)
{
case "05.01.02":
// remove the Code SubDirectory
Config.RemoveCodeSubDirectory("HTML");
// Once the web.config entry is done we can safely remove the HTML folder
var arrPaths = new string[1];
arrPaths[0] = "App_Code\\HTML\\";
FileSystemUtils.DeleteFiles(arrPaths);
break;
case "06.00.00":
DesktopModuleInfo desktopModule = DesktopModuleController.GetDesktopModuleByModuleName("DNN_HTML", Null.NullInteger);
desktopModule.Category = "Common";
DesktopModuleController.SaveDesktopModule(desktopModule, false, false);
break;
case "06.02.00":
this.AddNotificationTypes();
break;
}
return string.Empty;
}
private static void AddHtmlNotification(string subject, string body, UserInfo user)
{
var notificationType = NotificationsController.Instance.GetNotificationType("HtmlNotification");
var portalSettings = PortalController.Instance.GetCurrentPortalSettings();
var sender = UserController.GetUserById(portalSettings.PortalId, portalSettings.AdministratorId);
var notification = new Notification { NotificationTypeID = notificationType.NotificationTypeId, Subject = subject, Body = body, IncludeDismissAction = true, SenderUserID = sender.UserID };
NotificationsController.Instance.SendNotification(notification, portalSettings.PortalId, null, new List<UserInfo> { user });
}
private static List<string> CollectHierarchicalTags(List<Term> terms)
{
Func<List<Term>, List<string>, List<string>> collectTagsFunc = null;
collectTagsFunc = (ts, tags) =>
{
if (ts != null && ts.Count > 0)
{
foreach (var t in ts)
{
tags.Add(t.Name);
tags.AddRange(collectTagsFunc(t.ChildTerms, new List<string>()));
}
}
return tags;
};
return collectTagsFunc(terms, new List<string>());
}
private static void TrackModuleModification(int moduleId, int moduleVersion)
{
var moduleInfo = ModuleController.Instance.GetModule(moduleId, Null.NullInteger, true);
TabChangeTracker.Instance.TrackModuleModification(moduleInfo, moduleVersion, UserController.Instance.GetCurrentUserInfo().UserID);
}
private void ClearModuleSettings(ModuleInfo objModule)
{
if (objModule.ModuleDefinition.FriendlyName == "Text/HTML")
{
ModuleController.Instance.DeleteModuleSetting(objModule.ModuleID, "WorkFlowID");
}
}
/// <summary>CreateUserNotifications creates HtmlTextUser records and optionally sends email notifications to participants in a Workflow.</summary>
/// <param name="objHtmlText">An HtmlTextInfo object.</param>
private void CreateUserNotifications(HtmlTextInfo objHtmlText)
{
var htmlTextUserController = new HtmlTextUserController();
HtmlTextUserInfo htmlTextUser = null;
UserInfo user = null;
// clean up old user notification records
htmlTextUserController.DeleteHtmlTextUsers();
// ensure we have latest htmltext object loaded
objHtmlText = this.GetHtmlText(objHtmlText.ModuleID, objHtmlText.ItemID);
// build collection of users to notify
var arrUsers = new ArrayList();
// if not published
if (objHtmlText.IsPublished == false)
{
arrUsers.Add(objHtmlText.CreatedByUserID); // include content owner
}
// if not draft and not published
if (objHtmlText.StateID != this.workflowManager.GetWorkflow(objHtmlText.WorkflowID).FirstState.StateID && objHtmlText.IsPublished == false)
{
// get users from permissions for state
foreach (var permission in WorkflowStatePermissionsRepository.Instance.GetWorkflowStatePermissionByState(objHtmlText.StateID))
{
if (permission.AllowAccess)
{
if (Null.IsNull(permission.UserID))
{
int roleId = permission.RoleID;
RoleInfo objRole = RoleController.Instance.GetRole(objHtmlText.PortalID, r => r.RoleID == roleId);
if (objRole != null)
{
foreach (UserRoleInfo objUserRole in RoleController.Instance.GetUserRoles(objHtmlText.PortalID, null, objRole.RoleName))
{
if (!arrUsers.Contains(objUserRole.UserID))
{
arrUsers.Add(objUserRole.UserID);
}
}
}
}
else
{
if (!arrUsers.Contains(permission.UserID))
{
arrUsers.Add(permission.UserID);
}
}
}
}
}
// process notifications
if (arrUsers.Count > 0 || (objHtmlText.IsPublished && objHtmlText.Notify))
{
// get tabid from module
ModuleInfo objModule = ModuleController.Instance.GetModule(objHtmlText.ModuleID, Null.NullInteger, true);
PortalSettings objPortalSettings = PortalController.Instance.GetCurrentPortalSettings();
if (objPortalSettings != null)
{
string strResourceFile = string.Format(
"{0}/DesktopModules/{1}/{2}/{3}",
Globals.ApplicationPath,
objModule.DesktopModule.FolderName,
Localization.LocalResourceDirectory,
Localization.LocalSharedResourceFile);
string strSubject = Localization.GetString("NotificationSubject", strResourceFile);
string strBody = Localization.GetString("NotificationBody", strResourceFile);
strBody = strBody.Replace("[URL]", this.NavigationManager.NavigateURL(objModule.TabID));
strBody = strBody.Replace("[STATE]", objHtmlText.StateName);
// process user notification collection
foreach (int intUserID in arrUsers)
{
// create user notification record
htmlTextUser = new HtmlTextUserInfo();
htmlTextUser.ItemID = objHtmlText.ItemID;
htmlTextUser.StateID = objHtmlText.StateID;
htmlTextUser.ModuleID = objHtmlText.ModuleID;
htmlTextUser.TabID = objModule.TabID;
htmlTextUser.UserID = intUserID;
htmlTextUserController.AddHtmlTextUser(htmlTextUser);
// send an email notification to a user if the state indicates to do so
if (objHtmlText.Notify)
{
user = UserController.GetUserById(objHtmlText.PortalID, intUserID);
if (user != null)
{
AddHtmlNotification(strSubject, strBody, user);
}
}
}
// if published and the published state specifies to notify members of the workflow
if (objHtmlText.IsPublished && objHtmlText.Notify)
{
// send email notification to the author
user = UserController.GetUserById(objHtmlText.PortalID, objHtmlText.CreatedByUserID);
if (user != null)
{
try
{
Services.Mail.Mail.SendEmail(objPortalSettings.Email, objPortalSettings.Email, strSubject, strBody);
}
catch (Exception exc)
{
Exceptions.LogException(exc);
}
}
}
}
}
}
private string DeTokeniseLinks(string content, int portalId)
{
var portal = PortalController.Instance.GetPortal(portalId);
var portalRoot = UrlUtils.Combine(Globals.ApplicationPath, portal.HomeDirectory);
if (!portalRoot.StartsWith("/"))
{
portalRoot = "/" + portalRoot;
}
if (!portalRoot.EndsWith("/"))
{
portalRoot = portalRoot + "/";
}
content = Regex.Replace(content, PortalRootToken + "\\/{0,1}", portalRoot, RegexOptions.IgnoreCase);
return content;
}
private string TokeniseLinks(string content, int portalId)
{
// Replace any relative portal root reference by a token "{{PortalRoot}}"
var portal = PortalController.Instance.GetPortal(portalId);
var portalRoot = UrlUtils.Combine(Globals.ApplicationPath, portal.HomeDirectory);
if (!portalRoot.StartsWith("/"))
{
portalRoot = "/" + portalRoot;
}
if (!portalRoot.EndsWith("/"))
{
portalRoot = portalRoot + "/";
}
// Portal Root regular expression
var regex = @"(?<url>
(?<host>
(?<protocol>[A-Za-z]{3,9}:(?:\/\/)?)
(?<domain>(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+))?
(?<portalRoot>" + portalRoot + "))";
var matchEvaluator = new MatchEvaluator(this.ReplaceWithRootToken);
var exp = RegexUtils.GetCachedRegex(regex, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
return exp.Replace(content, matchEvaluator);
}
private void AddNotificationTypes()
{
var type = new NotificationType { Name = "HtmlNotification", Description = "Html Module Notification" };
if (NotificationsController.Instance.GetNotificationType(type.Name) == null)
{
NotificationsController.Instance.CreateNotificationType(type);
}
}
}
}