PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/admin/Pages/RecaptchaLogViewer.aspx.cs

#
C# | 195 lines | 126 code | 34 blank | 35 comment | 11 complexity | cf7d573e8f8a87f23e75ccd7b1b22ea7 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <summary>
  3. // The admin pages recaptcha log viewer.
  4. // </summary>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace Admin.Pages
  7. {
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.Globalization;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Web.Security;
  15. using BlogEngine.Core;
  16. using Recaptcha;
  17. using Page = System.Web.UI.Page;
  18. /// <summary>
  19. /// The admin pages recaptcha log viewer.
  20. /// </summary>
  21. public partial class RecaptchaLogViewer : Page
  22. {
  23. #region Constants and Fields
  24. /// <summary>
  25. /// The gravatar image.
  26. /// </summary>
  27. private const string GravatarImage = "<img class=\"photo\" src=\"{0}\" alt=\"{1}\" />";
  28. #endregion
  29. #region Public Methods
  30. /// <summary>
  31. /// Gets the website.
  32. /// </summary>
  33. /// <param name="website">The website.</param>
  34. /// <returns>A website string.</returns>
  35. public static string GetWebsite(object website)
  36. {
  37. if (website == null)
  38. {
  39. return string.Empty;
  40. }
  41. const string Templ = "<a href='{0}' target='_new' rel='{0}'>{1}</a>";
  42. var site = website.ToString();
  43. site = site.Replace("http://www.", string.Empty);
  44. site = site.Replace("http://", string.Empty);
  45. site = site.Length < 20 ? site : string.Format("{0}...", site.Substring(0, 17));
  46. return string.Format(Templ, website, site);
  47. }
  48. #endregion
  49. #region Methods
  50. /// <summary>
  51. /// Gravatars the specified email.
  52. /// </summary>
  53. /// <param name="email">
  54. /// The email.
  55. /// </param>
  56. /// <param name="author">
  57. /// The author.
  58. /// </param>
  59. /// <returns>
  60. /// The gravatar.
  61. /// </returns>
  62. protected string Gravatar(string email, string author)
  63. {
  64. if (BlogSettings.Instance.Avatar == "none")
  65. {
  66. return null;
  67. }
  68. if (String.IsNullOrEmpty(email) || !email.Contains("@"))
  69. {
  70. return string.Format(
  71. "<img src=\"{0}themes/{1}/noavatar.jpg\" alt=\"{2}\" width=\"28\" height=\"28\" />",
  72. Utils.AbsoluteWebRoot,
  73. BlogSettings.Instance.Theme,
  74. author);
  75. }
  76. var hash = FormsAuthentication.HashPasswordForStoringInConfigFile(email.ToLowerInvariant().Trim(), "MD5");
  77. if (hash != null)
  78. {
  79. hash = hash.ToLowerInvariant();
  80. }
  81. var gravatar = string.Format("http://www.gravatar.com/avatar/{0}.jpg?s=28&amp;d=", hash);
  82. string link;
  83. switch (BlogSettings.Instance.Avatar)
  84. {
  85. case "identicon":
  86. link = string.Format("{0}identicon", gravatar);
  87. break;
  88. case "wavatar":
  89. link = string.Format("{0}wavatar", gravatar);
  90. break;
  91. default:
  92. link = string.Format("{0}monsterid", gravatar);
  93. break;
  94. }
  95. return string.Format(CultureInfo.InvariantCulture, GravatarImage, link, author);
  96. }
  97. /// <summary>
  98. /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event to initialize the page.
  99. /// </summary>
  100. /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
  101. protected override void OnInit(EventArgs e)
  102. {
  103. Security.DemandUserHasRight(BlogEngine.Core.Rights.AccessAdminPages, true);
  104. this.BindGrid();
  105. base.OnInit(e);
  106. }
  107. /// <summary>
  108. /// Binds the grid.
  109. /// </summary>
  110. private void BindGrid()
  111. {
  112. var log = RecaptchaLogger.ReadLogItems();
  113. var comments = Post.Posts.SelectMany(post => post.Comments).ToDictionary(comment => comment.Id);
  114. var logView = new DataTable("LogView");
  115. logView.Columns.Add("Email");
  116. logView.Columns.Add("Date", typeof(DateTime));
  117. logView.Columns.Add("Author");
  118. logView.Columns.Add("Website");
  119. logView.Columns.Add("IP");
  120. logView.Columns.Add("RecaptchaAttempts", typeof(ushort));
  121. logView.Columns.Add("CommentTime", typeof(double));
  122. logView.Columns.Add("RecaptchaTime", typeof(double));
  123. var orphanedRecords = new List<RecaptchaLogItem>();
  124. foreach (var item in log)
  125. {
  126. if (comments.ContainsKey(item.CommentId))
  127. {
  128. var comment = comments[item.CommentId];
  129. logView.Rows.Add(
  130. comment.Email,
  131. comment.DateCreated,
  132. comment.Author,
  133. comment.Website,
  134. comment.IP,
  135. item.NumberOfAttempts,
  136. item.TimeToComment,
  137. item.TimeToSolveCapcha);
  138. }
  139. else
  140. {
  141. orphanedRecords.Add(item);
  142. }
  143. }
  144. if (orphanedRecords.Count > 0)
  145. {
  146. foreach (var orphan in orphanedRecords)
  147. {
  148. log.Remove(orphan);
  149. }
  150. RecaptchaLogger.SaveLogItems(log);
  151. }
  152. using (var view = new DataView(logView))
  153. {
  154. view.Sort = "Date DESC";
  155. this.RecaptchaLog.DataSource = view;
  156. this.RecaptchaLog.DataBind();
  157. }
  158. }
  159. #endregion
  160. }
  161. }