PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/vzfsrc/VZF.NET/pages/admin/bannedip_edit.ascx.cs

https://github.com/vzrus/VZF
C# | 182 lines | 107 code | 28 blank | 47 comment | 14 complexity | 11990efe42d888379a436b12c2cf4021 MD5 | raw file
  1. namespace YAF.Pages.Admin
  2. {
  3. #region Using
  4. using System;
  5. using System.Data;
  6. using System.Text;
  7. using VZF.Data.Common;
  8. using YAF.Classes;
  9. using YAF.Core;
  10. using YAF.Types;
  11. using YAF.Types.Constants;
  12. using YAF.Types.Interfaces;
  13. using VZF.Utils;
  14. #endregion
  15. /// <summary>
  16. /// Admin Banned ip edit/add page.
  17. /// </summary>
  18. public partial class bannedip_edit : AdminPage
  19. {
  20. #region Methods
  21. /// <summary>
  22. /// The cancel_ click.
  23. /// </summary>
  24. /// <param name="sender">
  25. /// The sender.
  26. /// </param>
  27. /// <param name="e">
  28. /// The e.
  29. /// </param>
  30. protected void Cancel_Click([NotNull] object sender, [NotNull] EventArgs e)
  31. {
  32. YafBuildLink.Redirect(ForumPages.admin_bannedip);
  33. }
  34. /// <summary>
  35. /// The page_ load.
  36. /// </summary>
  37. /// <param name="sender">
  38. /// The sender.
  39. /// </param>
  40. /// <param name="e">
  41. /// The e.
  42. /// </param>
  43. protected void Page_Load([NotNull] object sender, [NotNull] EventArgs e)
  44. {
  45. if (this.IsPostBack)
  46. {
  47. return;
  48. }
  49. this.PageLinks.AddLink(this.Get<YafBoardSettings>().Name, YafBuildLink.GetLink(ForumPages.forum));
  50. this.PageLinks.AddLink(this.GetText("ADMIN_ADMIN", "Administration"), YafBuildLink.GetLink(ForumPages.admin_admin));
  51. this.PageLinks.AddLink(this.GetText("ADMIN_BANNEDIP", "TITLE"), YafBuildLink.GetLink(ForumPages.admin_bannedip));
  52. // current page label (no link)
  53. this.PageLinks.AddLink(this.GetText("ADMIN_BANNEDIP_EDIT", "TITLE"), string.Empty);
  54. this.Page.Header.Title = "{0} - {1} - {2}".FormatWith(
  55. this.GetText("ADMIN_ADMIN", "Administration"),
  56. this.GetText("ADMIN_BANNEDIP", "TITLE"),
  57. this.GetText("ADMIN_BANNEDIP_EDIT", "TITLE"));
  58. this.save.Text = this.GetText("COMMON", "SAVE");
  59. this.cancel.Text = this.GetText("COMMON", "CANCEL");
  60. this.BindData();
  61. }
  62. /// <summary>
  63. /// The save_ click.
  64. /// </summary>
  65. /// <param name="sender">
  66. /// The sender.
  67. /// </param>
  68. /// <param name="e">
  69. /// The e.
  70. /// </param>
  71. protected void Save_Click([NotNull] object sender, [NotNull] EventArgs e)
  72. {
  73. // IPv4
  74. if (!System.Text.RegularExpressions.Regex.IsMatch(this.mask.Text.Trim(), @"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
  75. {
  76. // IPv4 CIDR Range
  77. if (!System.Text.RegularExpressions.Regex.IsMatch(this.mask.Text.Trim(), @"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
  78. {
  79. // leave it for coptability, checks for * in address for ranges
  80. string[] ipParts = this.mask.Text.Trim().Split('.');
  81. // do some validation...
  82. var ipError = new StringBuilder();
  83. if (ipParts.Length != 4)
  84. {
  85. ipError.AppendLine(this.GetText("ADMIN_BANNEDIP_EDIT", "INVALID_ADRESS"));
  86. }
  87. foreach (string ip in ipParts)
  88. {
  89. // see if they are numbers...
  90. ulong number;
  91. if (!ulong.TryParse(ip, out number))
  92. {
  93. if (ip.Trim() != "*")
  94. {
  95. if (ip.Trim().Length == 0)
  96. {
  97. ipError.AppendLine(this.GetText("ADMIN_BANNEDIP_EDIT", "INVALID_VALUE"));
  98. }
  99. else
  100. {
  101. ipError.AppendFormat(this.GetText("ADMIN_BANNEDIP_EDIT", "INVALID_SECTION"), ip);
  102. }
  103. break;
  104. }
  105. }
  106. else
  107. {
  108. // try parse succeeded... verify number amount...
  109. if (number > 255)
  110. {
  111. ipError.AppendFormat(this.GetText("ADMIN_BANNEDIP_EDIT", "INVALID_LESS"), ip);
  112. }
  113. }
  114. }
  115. // show error(s) if not valid...
  116. if (ipError.Length > 0)
  117. {
  118. this.PageContext.AddLoadMessage(ipError.ToString());
  119. return;
  120. }
  121. }
  122. }
  123. // IPv6 CIDR Range
  124. // System.Text.RegularExpressions.Regex.Matches(this.mask.Text.Trim(), @"^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  125. // IPv6 CIDR Range
  126. // System.Text.RegularExpressions.Regex.Matches(this.mask.Text.Trim(), @"^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/(d|dd|1[0-1]d|12[0-8]))$", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  127. CommonDb.bannedip_save(PageContext.PageModuleID, this.Request.QueryString.GetFirstOrDefault("i"),
  128. this.PageContext.PageBoardID,
  129. this.mask.Text.Trim(),
  130. this.BanReason.Text.Trim(),
  131. this.PageContext.PageUserID);
  132. this.Get<ILogger>().IpBanSet(this.PageContext.PageUserID, "YAF.Pages.Admin.bannedip_edit", "IP or mask {0} was saved by {1}.".FormatWith(this.mask.Text.Trim(), this.Get<YafBoardSettings>().EnableDisplayName ? this.PageContext.CurrentUserData.DisplayName : this.PageContext.CurrentUserData.UserName));
  133. // clear cache of banned IPs for this board
  134. this.Get<IDataCache>().Remove(Constants.Cache.BannedIP);
  135. // go back to banned IP's administration page
  136. YafBuildLink.Redirect(ForumPages.admin_bannedip);
  137. }
  138. /// <summary>
  139. /// The bind data.
  140. /// </summary>
  141. private void BindData()
  142. {
  143. if (this.Request.QueryString.GetFirstOrDefault("i") == null)
  144. {
  145. return;
  146. }
  147. DataRow row =
  148. CommonDb.bannedip_list(PageContext.PageModuleID, this.PageContext.PageBoardID, this.Request.QueryString.GetFirstOrDefault("i"), null, null).Rows[0];
  149. this.mask.Text = row["Mask"].ToString();
  150. this.BanReason.Text = row["Reason"].ToString();
  151. }
  152. #endregion
  153. }
  154. }