PageRenderTime 68ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/BlogEngine/DotNetSlave.BusinessLogic/Web/HttpHandlers/RatingHandler.cs

#
C# | 133 lines | 78 code | 19 blank | 36 comment | 14 complexity | 508c12897e51364725c6a7cd5445bb36 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace BlogEngine.Core.Web.HttpHandlers
  2. {
  3. using System;
  4. using System.Web;
  5. /// <summary>
  6. /// Receives and records all ratings comming in from the rating control.
  7. /// </summary>
  8. public class RatingHandler : IHttpHandler
  9. {
  10. #region Properties
  11. /// <summary>
  12. /// Gets a value indicating whether another request can use the <see cref = "T:System.Web.IHttpHandler"></see> instance.
  13. /// </summary>
  14. /// <value></value>
  15. /// <returns>true if the <see cref = "T:System.Web.IHttpHandler"></see> instance is reusable; otherwise, false.</returns>
  16. public bool IsReusable
  17. {
  18. get
  19. {
  20. return false;
  21. }
  22. }
  23. #endregion
  24. #region Implemented Interfaces
  25. #region IHttpHandler
  26. /// <summary>
  27. /// Enables processing of HTTP Web requests by a custom HttpHandler that
  28. /// implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
  29. /// </summary>
  30. /// <param name="context">
  31. /// An <see cref="T:System.Web.HttpContext"></see>
  32. /// object that provides references to the intrinsic server objects
  33. /// (for example, Request, Response, Session, and Server) used to service HTTP requests.
  34. /// </param>
  35. public void ProcessRequest(HttpContext context)
  36. {
  37. if (!BlogSettings.Instance.EnableRating || !Security.IsAuthorizedTo(Rights.SubmitRatingsOnPosts))
  38. {
  39. throw new System.Security.SecurityException();
  40. }
  41. else
  42. {
  43. var rating = context.Request.QueryString["rating"];
  44. int rate;
  45. if (rating != null && int.TryParse(rating, out rate))
  46. {
  47. var id = context.Request.QueryString["id"];
  48. if (id != null && id.Length == 36 && rate > 0 && rate < 6)
  49. {
  50. try
  51. {
  52. if (HasRated(id))
  53. {
  54. context.Response.Write(string.Format("{0}HASRATED", rate));
  55. context.Response.End();
  56. }
  57. else
  58. {
  59. var post = Post.GetPost(new Guid(id));
  60. post.Rate(rate);
  61. SetCookie(id, context);
  62. context.Response.Write(string.Format("{0}OK", rate));
  63. context.Response.End();
  64. }
  65. }
  66. catch(Exception)
  67. {
  68. // Something failed.
  69. }
  70. }
  71. }
  72. context.Response.Write("FAIL");
  73. }
  74. }
  75. #endregion
  76. #endregion
  77. #region Methods
  78. /// <summary>
  79. /// Determines whether the specified post id has rated.
  80. /// </summary>
  81. /// <param name="postId">The post id.</param>
  82. /// <returns>
  83. /// <c>true</c> if the specified post id has rated; otherwise, <c>false</c>.
  84. /// </returns>
  85. private static bool HasRated(string postId)
  86. {
  87. // This seems like a bad idea. Someone without cookies disabled
  88. // they could repeatedly rate a post. Also, if someone rates
  89. // a lot of posts, it's going to continue to increase the size
  90. // of their rating cookie, increasing bandwidth.
  91. //
  92. // -rossisdead 10/28/2010
  93. var ratingCookie = HttpContext.Current.Request.Cookies["rating"];
  94. if (ratingCookie != null)
  95. {
  96. return ratingCookie.Value.Contains(postId);
  97. }
  98. return false;
  99. }
  100. /// <summary>
  101. /// Sets the cookie.
  102. /// </summary>
  103. /// <param name="id">The cookie id.</param>
  104. /// <param name="context">The context.</param>
  105. private static void SetCookie(string id, HttpContext context)
  106. {
  107. var cookie = context.Request.Cookies["rating"] ?? new HttpCookie("rating");
  108. cookie.Expires = DateTime.Now.AddYears(2);
  109. cookie.Value += id;
  110. context.Response.Cookies.Add(cookie);
  111. }
  112. #endregion
  113. }
  114. }