forked from sachatrauwaen/OpenUrlRewriter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.ascx.cs
519 lines (404 loc) · 21.1 KB
/
View.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
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Satrabel.HttpModules.Config;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using Satrabel.Services.Log.UrlLog;
using Satrabel.Services.Log.UrlRule;
using DotNetNuke.Common;
using DotNetNuke.Services.Localization;
using Satrabel.HttpModules;
using Satrabel.HttpModules.Provider;
using DotNetNuke.Entities.Portals;
using System.Xml;
using DotNetNuke.Services.Installer;
using System.Runtime.Serialization.Formatters.Binary;
using DotNetNuke.Entities.Tabs;
#if DNN71
using DotNetNuke.Entities.Urls;
#endif
namespace Satrabel.Modules.OpenUrlRewriter
{
public partial class View : PortalModuleBase, IActionable
{
string cssSucces = "dnnFormMessage dnnFormSuccess";
string cssError = "dnnFormMessage dnnFormWarning";
protected void Page_Load(object sender, EventArgs e)
{
MyEditUrl(lbTestHtml, "TestHtml");
MyEditUrl(lbViewRules, "rules_view");
MyEditUrl(lbAddRule, "rules_edit");
MyEditUrl(lbViewCache, "cache_view");
MyEditUrl(lbViewLog, "log_view");
//lbLogSettings.Attributes.Add("onclick", "return " + MyEditUrl("log_settings"));
ShowCache();
ShowLogs();
ShowRules();
ShowProviders();
//ShowPortals();
ddlTab.DataSource = TabController.GetPortalTabs(PortalId, -1, false, true);
ddlTab.DataBind();
if (!Page.IsPostBack)
{
cbDisableSiteIndex.Checked = UrlRewiterSettings.IsDisableSiteIndex(PortalId);
cbDisableTermsIndex.Checked = UrlRewiterSettings.IsDisableTermsIndex(PortalId);
cbDisablePivacyIndex.Checked = UrlRewiterSettings.IsDisablePrivacyIndex(PortalId);
cbLogAuthentificatedUsers.Checked = UrlRewiterSettings.IsLogAuthentificatedUsers(PortalId);
cbLogEachUrlOneTime.Checked = UrlRewiterSettings.IsLogEachUrlOneTime(PortalId);
cbLogStatusCode200.Checked = UrlRewiterSettings.IsLogStatusCode200(PortalId);
cbLogEnabled.Checked = UrlRewiterSettings.IsLogEnabled(PortalId);
cbW3C.Checked = UrlRewiterSettings.IsW3C(PortalId);
cbEnhanced404.Checked = UrlRewiterSettings.IsManage404(PortalId);
ddlTab.SelectedValue = PortalController.GetPortalSetting(UrlRewiterSettings.ErrorPage404Setting, PortalId, "-1");
XmlDocument xmlConfig = Config.Load();
XmlNode xmlSitemap = xmlConfig.SelectSingleNode("/configuration/dotnetnuke/sitemap/providers/add[@name='openUrlRewriterSitemapProvider']");
XmlNode xmlFriendlyUrl = xmlConfig.SelectSingleNode("/configuration/dotnetnuke/friendlyUrl/providers/add[@name='OpenFriendlyUrl']");
XmlNode xmlFriendlyUrl2 = xmlConfig.SelectSingleNode("/configuration/dotnetnuke/friendlyUrl");
XmlNode xmlUrlRewriter1 = xmlConfig.SelectSingleNode("/configuration/system.webServer/modules/add[@name='UrlRewrite']");
XmlNode xmlUrlRewriter2 = xmlConfig.SelectSingleNode("/configuration/system.web/httpModules/add[@name='UrlRewrite']");
//XmlNode xmlCaching = xmlConfig.SelectSingleNode("/configuration/dotnetnuke/caching/providers/add[@name='OpenUrlRewriterFBCachingProvider']");
XmlNode xmlSitemapHandler1 = xmlConfig.SelectSingleNode("/configuration/system.webServer/handlers/add[@name='SitemapHandler']");
XmlNode xmlSitemapHandler2 = xmlConfig.SelectSingleNode("/configuration/system.web/httpHandlers/add[@path='Sitemap.aspx']");
//cbSitemapProvider.Checked = xmlSitemap != null;
if (xmlFriendlyUrl != null && xmlFriendlyUrl2.Attributes["defaultProvider"].Value == "OpenFriendlyUrl")
lFriendlyUrlProvider.CssClass = cssSucces;
else
lFriendlyUrlProvider.CssClass = cssError;
if ( HttpRuntime.UsingIntegratedPipeline)
{
if (xmlUrlRewriter1 != null && xmlUrlRewriter1.Attributes["type"].Value == "Satrabel.HttpModules.UrlRewriteModule, Satrabel.OpenUrlRewriter")
lUrlRewriter.CssClass = cssSucces;
else
lUrlRewriter.CssClass = cssError;
}
else {
if (xmlUrlRewriter2 != null && xmlUrlRewriter2.Attributes["type"].Value == "Satrabel.HttpModules.UrlRewriteModule, Satrabel.OpenUrlRewriter")
lUrlRewriter.CssClass = cssSucces;
else
lUrlRewriter.CssClass = cssError;
}
if (xmlSitemap != null)
lSitemapProvider.CssClass = cssSucces;
else
lSitemapProvider.CssClass = cssError;
if (xmlSitemap != null)
lSitemapProvider.CssClass = cssSucces;
else
lSitemapProvider.CssClass = cssError;
/*
if (xmlCaching != null)
lCachingProvider.CssClass = cssSucces;
else
lCachingProvider.CssClass = cssError;
*/
if (HttpRuntime.UsingIntegratedPipeline)
{
if (xmlSitemapHandler1 != null && xmlSitemapHandler1.Attributes["type"].Value == "Satrabel.Services.Sitemap.OpenSitemapHandler, Satrabel.OpenUrlRewriter")
lSitemapHandler.CssClass = cssSucces;
else
lSitemapHandler.CssClass = cssError;
}
else
{
if (xmlSitemapHandler2 != null && xmlSitemapHandler2.Attributes["type"].Value == "Satrabel.Services.Sitemap.OpenSitemapHandler, Satrabel.OpenUrlRewriter")
lSitemapHandler.CssClass = cssSucces;
else
lSitemapHandler.CssClass = cssError;
}
lbSaveMeta.Enabled = EditMode;
lbTestHtml.Visible = UserInfo.IsSuperUser;
}
/*
Locale DefaultLocale = LocaleController.Instance.GetDefaultLocale(PortalId);
PortalInfo objPortal = new PortalController().GetPortal(PortalId, DefaultLocale.Code);
int DefaultHomeTabId = -1;
if (objPortal != null)
DefaultHomeTabId = objPortal.HomeTabId;
bool RemoveHomePage = PortalController.GetPortalSettingAsBoolean( "TabUrlRuleProvider" + "_"+ "RemoveHomePage", PortalId, false);
lTrace.Text = PortalId + "/" + DefaultLocale.Code + "/" + DefaultHomeTabId + "/" + RemoveHomePage;
*/
}
private void ShowProviders()
{
var builder = new UrlBuilder();
gvProviders.DataSource = builder.Providers;
gvProviders.DataBind();
}
private void ShowPortals()
{
#if DEBUG
gvPortals.Columns[2].Visible = true;
#endif
PortalController pc = new PortalController();
gvPortals.DataSource = pc.GetPortals();
gvPortals.DataBind();
}
protected void gvPortals_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
PortalInfo portal = (PortalInfo)e.Row.DataItem;
int RulesCount = UrlRuleConfiguration.GetConfig(portal.PortalID).Rules.Count();
e.Row.Cells[1].Text = RulesCount.ToString();
/*
int Memsize = 0;
foreach (var rule in UrlRuleConfiguration.GetConfig(portal.PortalID).Rules) {
Memsize += sizeof(UrlRuleType);
if (rule.CultureCode != null)
Memsize += sizeof(char) * rule.CultureCode.Length;
Memsize += sizeof(int); // tabid
if (rule.Parameters != null)
Memsize += sizeof(char) * rule.Parameters.Length; // Parameters
Memsize += sizeof(Boolean); //RemoveTab
Memsize += sizeof(UrlRuleAction);
if (rule.Url != null)
Memsize += sizeof(char) * rule.Url.Length; // Url
if (rule.RedirectDestination != null)
Memsize += sizeof(char) * rule.RedirectDestination.Length; //
Memsize += sizeof(int); // RedirectionStatus
}
*/
#if DEBUG
System.IO.MemoryStream stream = new System.IO.MemoryStream();
BinaryFormatter objFormatter = new BinaryFormatter();
objFormatter.Serialize(stream, UrlRuleConfiguration.GetConfig(portal.PortalID));
long Memsize = stream.Length;
e.Row.Cells[2].Text = Memsize / 1000 + " kb"; //(RulesCount * sizeof(UrlRule) / 1000) + "";
#endif
}
}
private void MyEditUrl(LinkButton lb, string ctl)
{
var editUrl = ModuleContext.EditUrl(ctl);
if (ModuleContext.PortalSettings.EnablePopUps)
{
editUrl = UrlUtils.PopUpUrl(editUrl, this, ModuleContext.PortalSettings, true, false);
lb.Attributes.Add("onclick", "return " + editUrl);
}
else {
lb.Click += delegate (object sender, EventArgs e) {
Response.Redirect(editUrl, true);
};
}
}
private void ShowCache()
{
var Rules = UrlRuleConfiguration.GetConfig(PortalId).Rules;
var stats = from r in Rules
group r by new { r.RuleTypeString, r.ActionString } into g
orderby g.Key.RuleTypeString, g.Key.ActionString
select new { Type = g.Key.RuleTypeString, Action = g.Key.ActionString, Count = g.Count() };
gvCache.DataSource = stats;
gvCache.DataBind();
var clashes = from r in Rules /* .Where(r=> r.RuleType != UrlRuleType.Custom)*/
group r by new { r.RuleTypeString, r.CultureCode, r.TabId, r.Url } into g
where g.Count() > 1
select new { Type = g.Key.RuleTypeString, Culture = g.Key.CultureCode, g.Key.TabId, g.Key.Url, Count = g.Count() };
//clashes = clashes.Where(c => c.Count > 1);
gvClashes.DataSource = clashes;
gvClashes.DataBind();
}
private void ShowRules()
{
var Rules = UrlRuleController.GetUrlRules(PortalId);
var stats = from r in Rules
group r by new { r.RuleTypeString, r.RuleActionString } into g
orderby g.Key.RuleTypeString, g.Key.RuleActionString
select new { Type = g.Key.RuleTypeString, Action = g.Key.RuleActionString, Count = g.Count() };
gvRules.DataSource = stats;
gvRules.DataBind();
}
private void ShowLogs()
{
var Logs = UrlLogController.GetUrlLog(PortalId, DateTime.Now.AddDays(-30), DateTime.Now);
var stats = from l in Logs
group l by new { l.StatusCode } into g
select new { Status = g.Key.StatusCode, Count = g.Count() };
gvUrlLogs.DataSource = stats;
gvUrlLogs.DataBind();
var logsByURL = from l in Logs
group l by new { l.OriginalURL, l.URL} into g
select new { g.Key.OriginalURL, g.Key.URL };
var duplicates = from l in logsByURL
group l by new { l.OriginalURL } into g
where g.Count() > 1
select new { g.Key.OriginalURL, Count = g.Count() };
gvDuplicates.DataSource = duplicates;
gvDuplicates.DataBind();
}
protected void ClearCache_Click(object sender, EventArgs e)
{
//DataCache.RemoveCache("UrlRuleConfig");
DataCache.ClearCache();
ShowCache();
lStatus.Text = "The cache is cleared!";
lStatus.Visible = true;
}
protected void ViewRules_Click(object sender, EventArgs e)
{
//Response.Redirect(this.ModuleContext.EditUrl("view_rules"));
string redirectUrl = UrlUtils.PopUpUrl(EditUrl("urlrule_view"), this, PortalSettings, false, true);
Response.Redirect(redirectUrl, true);
}
protected void ClearLogs_Click(object sender, EventArgs e)
{
UrlLogController.DeleteUrlLog(DateTime.Now, PortalId);
ShowLogs();
}
protected void ClearRules_Click(object sender, EventArgs e)
{
var Rules = UrlRuleController.GetUrlRules(PortalId).Where(r=> r.RuleTypeEnum != UrlRuleType.Custom);
foreach (var rule in Rules) {
UrlRuleController.DeleteUrlRule(rule.UrlRuleId);
}
ShowRules();
}
protected void SaveMeta_Click(object sender, EventArgs e)
{
UrlRewiterSettings.SetDisableSiteIndex(PortalId, cbDisableSiteIndex.Checked);
UrlRewiterSettings.SetDisableTermsIndex(PortalId, cbDisableTermsIndex.Checked);
UrlRewiterSettings.SetDisablePrivacyIndex(PortalId, cbDisablePivacyIndex.Checked);
UrlRewiterSettings.SetLogAuthentificatedUsers(PortalId, cbLogAuthentificatedUsers.Checked);
UrlRewiterSettings.SetLogEachUrlOneTime(PortalId, cbLogEachUrlOneTime.Checked);
UrlRewiterSettings.SetLogStatusCode200(PortalId, cbLogStatusCode200.Checked);
UrlRewiterSettings.SetLogEnabled(PortalId, cbLogEnabled.Checked);
UrlRewiterSettings.SetW3C(PortalId, cbW3C.Checked);
UrlRewiterSettings.SetManage404(PortalId, cbEnhanced404.Checked);
PortalController.UpdatePortalSetting(PortalId, UrlRewiterSettings.ErrorPage404Setting, ddlTab.SelectedValue);
foreach (GridViewRow row in gvProviders.Rows)
{
CheckBox cbEnabled = (CheckBox)row.FindControl("cbEnabled");
PortalController.UpdatePortalSetting(PortalId, row.Cells[1].Text + "_Enabled", cbEnabled.Checked.ToString());
CheckBoxList cblSettings = (CheckBoxList)row.FindControl("cblSettings");
foreach (ListItem li in cblSettings.Items)
{
PortalController.UpdatePortalSetting(PortalId, row.Cells[1].Text + "_" + li.Value, li.Selected.ToString());
}
}
/*
XmlDocument xmlConfig = Config.Load();
XmlNode xmlSitemap = xmlConfig.SelectSingleNode("/configuration/dotnetnuke/sitemap/providers/add[@name='openUrlRewriterSitemapProvider']");
if (cbSitemapProvider.Checked && xmlSitemap == null) {
string install =
@"<configuration>
<nodes>
<node path=""/configuration/dotnetnuke/sitemap/providers""
action=""update"" key=""name"" collision=""ignore"">
<add name=""openUrlRewriterSitemapProvider"" type=""Satrabel.SitemapProviders.OpenUrlRewriterSitemapProvider, Satrabel.OpenUrlRewriter"" providerPath=""~\Providers\MembershipProviders\Sitemap\OpenUrlRewriterSitemapProvider\""/>
</node>
<node path=""/configuration/dotnetnuke/sitemap"" action=""updateattribute"" name=""defaultProvider"" value=""openUrlRewriterSitemapProvider"" />
<node path=""/configuration/dotnetnuke/sitemap/providers/add[@name='coreSitemapProvider']"" action=""remove"" />
</nodes>
</configuration>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(install);
var app = DotNetNuke.Application.DotNetNukeContext.Current.Application;
var merge = new XmlMerge(doc, Globals.FormatVersion(app.Version), app.Description);
merge.UpdateConfig(xmlConfig);
//XmlNode xmlSitemapProviders = xmlConfig.SelectSingleNode("/configuration/dotnetnuke/sitemap/providers");
//xmlSitemap = xmlConfig.CreateElement("add");
//XmlUtils.CreateAttribute(xmlConfig, xmlSitemap, "name", "openUrlRewriterSitemapProvider");
//XmlUtils.CreateAttribute(xmlConfig, xmlSitemap, "type", "Satrabel.SitemapProviders.OpenUrlRewriterSitemapProvider, Satrabel.OpenUrlRewriter");
//XmlUtils.CreateAttribute(xmlConfig, xmlSitemap, "providerPath", @"~\Providers\MembershipProviders\Sitemap\OpenUrlRewriterSitemapProvider\");
//xmlSitemapProviders.AppendChild(xmlSitemap);
Config.Save(xmlConfig);
}
else if (!cbSitemapProvider.Checked && xmlSitemap != null)
{
}
*/
DataCache.ClearCache();
this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
}
protected void gvProviders_RowDataBound(Object sender, GridViewRowEventArgs e){
if (e.Row.RowType == DataControlRowType.DataRow)
{
UrlRuleProvider provider = (UrlRuleProvider)e.Row.DataItem;
var Rules = provider.GetRules(PortalId);
e.Row.Cells[2].Text = Rules == null ? "" : Rules.Count.ToString();
bool isProviderEnabled = PortalController.GetPortalSettingAsBoolean(provider.Name + "_Enabled", PortalId, true);
CheckBox cbEnabled = (CheckBox)e.Row.FindControl("cbEnabled");
cbEnabled.Checked = isProviderEnabled;
CheckBoxList cblSettings = (CheckBoxList)e.Row.FindControl("cblSettings");
if (provider.Settings != null)
{
foreach (var set in provider.Settings)
{
cblSettings.Items.Add(new ListItem()
{
Value = set.Name,
Text = set.Name,
Selected = provider.GetPortalSettingAsBoolean(PortalId, set.Name)
});
}
}
}
}
protected void cbEnabled_OnCheckedChanged(Object sender, EventArgs e) {
CheckBox cbEnabled = (CheckBox)sender;
PortalController.UpdatePortalSetting(PortalId, cbEnabled.ToolTip + "_Enabled", cbEnabled.Checked.ToString());
}
#region IActionable Membres
public ModuleActionCollection ModuleActions
{
get
{
ModuleActionCollection Actions = new ModuleActionCollection();
Actions.Add(this.GetNextActionID(), "Url rules", ModuleActionType.EditContent, "", "", this.EditUrl("urlrule_view"), false, SecurityAccessLevel.View, true, false);
return Actions;
}
}
#endregion
protected void lbShowPortals_Click(object sender, EventArgs e)
{
ShowPortals();
lbShowPortals.Visible = false;
}
protected void lbTestUrl_Click(object sender, EventArgs e)
{
string portalAlias;
PortalAliasInfo objPortalAlias;
RewriterAction action;
try
{
Uri url = new Uri(tbUrl.Text);
UrlRewriteModuleUtils.RewriteUrl(url, out portalAlias, out objPortalAlias, out action);
if (action.DoRedirect)
{
if (action.Status == 302)
{
lUrlResult.Text = "Redirect " + action.Status + " (" + action.Raison + ") : " + action.RedirectUrl;
}
else
{
lUrlResult.Text = "Redirect 301 (" + action.Raison + ") : " + action.RedirectUrl;
}
}
else if (action.DoNotFound)
{
lUrlResult.Text = "NotFound 404 (" + action.Raison + ")";
}
else if (action.DoReWrite)
{
lUrlResult.Text = "Rewrite to : " + action.RewriteUrl;
}
lUrlResult.Text = action.RedirectUrl;
}
catch (Exception ex) {
lUrlResult.Text = "Error : " + ex.Message;
}
}
}
}