1 using System;
2 using System.Globalization;
3 using System.Linq;
4 using System.Text.RegularExpressions;
5 using System.Web;
6 using BlogEngine.Core;
7 using BlogEngine.Core.Web.Controls;
8
9 /// <summary>
10 /// Summary description for PostViews
11 /// </summary>
12 [Extension("Counts and displays number of views for a post", "1.0", "<a href=\"http://www.funkymule.com\">Funky Mule</a>")]
13 public class PostViews
14 {
15 private static ExtensionSettings _settings;
16
17 public PostViews()
18 {
19 Post.Serving += new EventHandler<ServingEventArgs>(Post_Serving);
20 InitSettings();
21 }
22
23 void Post_Serving(object sender, ServingEventArgs e)
24 {
25 IPublishable ipub = ((IPublishable)sender);
26
27 int viewCount = -1;
28
29 using (Extensions.PostViewsDataContext ctx = new Extensions.PostViewsDataContext())
30 {
31 //Check For Single Post View, When viewing Specific Post, basically through post.aspx
32 if (e.Location == ServingLocation.SinglePost)
33 {
34 string pattern = _settings.GetSingleValue("ExecludedIPs");
35 string ip = HttpContext.Current.Request.UserHostAddress;
36 bool matchedIp = false;
37 if (!string.IsNullOrEmpty(pattern) )
38 {
39 matchedIp = Regex.IsMatch(ip, pattern);
40 }
41
42 //Do not count view of authenticated users and users who have IPs match execluded IPs pattern
43 if (!matchedIp && !HttpContext.Current.Request.IsAuthenticated)
44 {
45 Extensions.PostView pv = new Extensions.PostView();
46 pv.Id = Guid.NewGuid();
47 pv.PostId = ipub.Id.ToString();
48 pv.ViewDate = DateTime.UtcNow;
49 pv.ViewReferral = HttpContext.Current.Request.UrlReferrer == null
50 ? string.Empty : HttpContext.Current.Request.UrlReferrer.ToString();
51 pv.ViewSource = HttpContext.Current.Request.UserHostAddress;
52
53 ctx.PostViews.InsertOnSubmit(pv);
54 ctx.SubmitChanges();
55 }
56 }
57
58 if (e.Location == ServingLocation.PostList || e.Location == ServingLocation.SinglePost)
59 {
60 string postId = ipub.Id.ToString();
61 viewCount = ctx.PostViews.Where(pv => pv.PostId == postId).Count();
62 }
63 }
64
65 if (bool.Parse(_settings.GetSingleValue("AuthenticatedOnly")) && !HttpContext.Current.Request.IsAuthenticated)
66 return;
67
68 if (viewCount > 0)
69 {
70 e.Body += string.Format(CultureInfo.InvariantCulture, "<br /> {0} Views", viewCount);
71 }
72 }
73
74 private void InitSettings()
75 {
76 ExtensionSettings settings = new ExtensionSettings(GetType().Name);
77 settings.IsScalar = true;
78 settings.AddParameter("AuthenticatedOnly", "Display to authenticated only", 5, true);
79 settings.AddParameter("ExcludedIPs",
80 "<a href=\"https://www.google.com/support/googleanalytics/bin/answer.py?answer=55572\" target=\"_blank\">Execlude IPs</a>",
81 255, false);
82 settings.AddValues(new string[] { "True", string.Empty });
83 settings.Help = "<p>Set <strong>Authenticated Only</strong> field to <strong>True</strong> if you wish " +
84 "<strong>only authenticated users</strong> to view total number of views of each post.<br/>" +
85 "Set <strong>Execlude IPs(regex)</strong> field if you wish to execlude range of IP addresses " +
86 "from accumulating post view count. This is Regular Expression field.</p>" +
87 "You can use <a href=\"https://www.google.com/support/googleanalytics/bin/answer.py?answer=55572\" target=\"_blank\">this tool</a> " +
88 "to generate your range of IPs ";
89 ExtensionManager.ImportSettings(settings);
90 _settings = ExtensionManager.GetSettings(this.GetType().Name);
91 }
92 }