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