PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/App_Code/Extensions/Recaptcha/RecaptchaLogger.cs

#
C# | 93 lines | 67 code | 11 blank | 15 comment | 4 complexity | 24d8e82b533a88ff3f3b17571889c7f5 MD5 | raw file
 1namespace Recaptcha
 2{
 3    using System;
 4    using System.Collections.Generic;
 5    using System.Data;
 6    using System.Linq;
 7
 8    using BlogEngine.Core.Web.Extensions;
 9
10    /// <summary>
11    /// Methods to save and retrieve reCaptcha logs
12    /// </summary>
13    public static class RecaptchaLogger
14    {
15        #region Public Methods
16
17        /// <summary>
18        /// Read log data from data store
19        /// </summary>
20        /// <returns>
21        /// List of log items
22        /// </returns>
23        public static List<RecaptchaLogItem> ReadLogItems()
24        {
25            var settings = ExtensionManager.GetSettings("Recaptcha", "RecaptchaLog");
26            var table = settings.GetDataTable();
27            var log = new List<RecaptchaLogItem>();
28
29            if (table.Rows.Count > 0)
30            {
31                log.AddRange(
32                    table.Rows.Cast<DataRow>().Select(
33                        row =>
34                        new RecaptchaLogItem
35                            {
36                                Response = (string)row["Response"],
37                                Challenge = (string)row["Challenge"],
38                                CommentId = new Guid((string)row["CommentID"]),
39                                Enabled = bool.Parse(row["Enabled"].ToString()),
40                                Necessary = bool.Parse(row["Necessary"].ToString()),
41                                NumberOfAttempts = ushort.Parse(row["NumberOfAttempts"].ToString()),
42                                TimeToComment = double.Parse(row["TimeToComment"].ToString()),
43                                TimeToSolveCapcha = double.Parse(row["TimeToSolveCapcha"].ToString())
44                            }));
45            }
46
47            return log;
48        }
49
50        /// <summary>
51        /// Saves log data to datastore as extension settings
52        /// </summary>
53        /// <param name="items">
54        /// List of log items
55        /// </param>
56        public static void SaveLogItems(List<RecaptchaLogItem> items)
57        {
58            if (items.Count <= 0)
59            {
60                return;
61            }
62
63            var settings = ExtensionManager.GetSettings("Recaptcha", "RecaptchaLog");
64            var table = settings.GetDataTable();
65
66            if (table.Rows.Count > 0)
67            {
68                for (var i = table.Rows.Count - 1; i > -1; i--)
69                {
70                    foreach (var par in settings.Parameters)
71                    {
72                        par.DeleteValue(i);
73                    }
74                }
75            }
76
77            foreach (var item in items)
78            {
79                settings.AddValues(
80                    new[]
81                        {
82                            item.Response, item.Challenge, item.CommentId.ToString(), item.TimeToComment.ToString(), 
83                            item.TimeToSolveCapcha.ToString(), item.NumberOfAttempts.ToString(), 
84                            item.Enabled.ToString(), item.Necessary.ToString()
85                        });
86            }
87
88            ExtensionManager.SaveSettings("Recaptcha", settings);
89        }
90
91        #endregion
92    }
93}