PageRenderTime 115ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/App_Code/Extensions/SimpleCaptcha/SimpleCaptchaControl.cs

#
C# | 219 lines | 122 code | 35 blank | 62 comment | 6 complexity | cd287092b6fcbfb4442dd10422bdb1bd MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. // Inspired by and interface heavily borrowed from Filip Stanek's ( http://www.bloodforge.com ) Recaptcha extension for blogengine.net
  2. // SimpleCaptcha created by Aaron Stannard (http://www.aaronstannard.com )
  3. namespace App_Code.Controls
  4. {
  5. using System;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using BlogEngine.Core;
  9. using BlogEngine.Core.Web.Extensions;
  10. /// <summary>
  11. /// This is the IValidator control that gets embedded on the comment form if the SimpleCaptcha extension is enabled.
  12. /// </summary>
  13. public class SimpleCaptchaControl : WebControl, IValidator
  14. {
  15. #region Constants and Fields
  16. /// <summary>
  17. /// The simple captcha answer field.
  18. /// </summary>
  19. private const string SimpleCaptchaAnswerField = "simpleCaptchaValue";
  20. /// <summary>
  21. /// The error message.
  22. /// </summary>
  23. private string errorMessage;
  24. /// <summary>
  25. /// Whether is valid.
  26. /// </summary>
  27. private bool valid;
  28. /// <summary>
  29. /// The simple captcha answer.
  30. /// </summary>
  31. private string simpleCaptchaAnswer;
  32. /// <summary>
  33. /// The simple captcha label.
  34. /// </summary>
  35. private string simpleCaptchaLabel;
  36. /// <summary>
  37. /// The skip simple captcha.
  38. /// </summary>
  39. private bool skipSimpleCaptcha = true;
  40. #endregion
  41. #region Properties
  42. /// <summary>
  43. /// Gets or sets ErrorMessage.
  44. /// </summary>
  45. public string ErrorMessage
  46. {
  47. get
  48. {
  49. return this.errorMessage ?? Resources.labels.incorrectSimpleCaptcha;
  50. }
  51. set
  52. {
  53. this.errorMessage = value;
  54. }
  55. }
  56. /// <summary>
  57. /// Gets or sets a value indicating whether valid.
  58. /// </summary>
  59. public bool IsValid
  60. {
  61. get
  62. {
  63. return this.valid;
  64. }
  65. set
  66. {
  67. }
  68. }
  69. /// <summary>
  70. /// Gets a value indicating whether the control has been enabled via the Extension Manager
  71. /// </summary>
  72. public bool SimpleCaptchaEnabled
  73. {
  74. get
  75. {
  76. var captchaExtension = ExtensionManager.GetExtension("SimpleCaptcha");
  77. return captchaExtension.Enabled;
  78. }
  79. }
  80. /// <summary>
  81. /// Gets a value indicating whether the recaptcha needs to be displayed for the current user
  82. /// </summary>
  83. public bool SimpleCaptchaNecessary
  84. {
  85. get
  86. {
  87. var settings = ExtensionManager.GetSettings("SimpleCaptcha");
  88. return !Security.IsAuthenticated ||
  89. Convert.ToBoolean(settings.GetSingleValue("ShowForAuthenticatedUsers"));
  90. }
  91. }
  92. #endregion
  93. #region Public Methods
  94. /// <summary>
  95. /// The validate.
  96. /// </summary>
  97. /// <param name="simpleCaptchaChallenge">
  98. /// The simple captcha challenge.
  99. /// </param>
  100. public void Validate(string simpleCaptchaChallenge)
  101. {
  102. this.valid = this.skipSimpleCaptcha || this.simpleCaptchaAnswer.Equals(simpleCaptchaChallenge);
  103. }
  104. #endregion
  105. #region Implemented Interfaces
  106. #region IValidator
  107. /// <summary>
  108. /// The validate.
  109. /// </summary>
  110. public void Validate()
  111. {
  112. var simpleCaptchaChallenge = this.Context.Request.Form[SimpleCaptchaAnswerField];
  113. this.Validate(simpleCaptchaChallenge);
  114. }
  115. #endregion
  116. #endregion
  117. #region Methods
  118. /// <summary>
  119. /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
  120. /// </summary>
  121. /// <param name="e">
  122. /// An <see cref="T:System.EventArgs"/> object that contains the event data.
  123. /// </param>
  124. protected override void OnInit(EventArgs e)
  125. {
  126. base.OnInit(e);
  127. var settings = ExtensionManager.GetSettings("SimpleCaptcha");
  128. this.simpleCaptchaAnswer = settings.GetSingleValue("CaptchaAnswer");
  129. this.simpleCaptchaLabel = settings.GetSingleValue("CaptchaLabel");
  130. if (this.SimpleCaptchaEnabled && this.SimpleCaptchaNecessary)
  131. {
  132. this.skipSimpleCaptcha = false;
  133. }
  134. if (String.IsNullOrEmpty(this.simpleCaptchaAnswer) || String.IsNullOrEmpty(this.simpleCaptchaLabel))
  135. {
  136. throw new ApplicationException(
  137. "SimpleCaptcha needs to be configured with an appropriate captcha label and a captcha value.");
  138. }
  139. }
  140. /// <summary>
  141. /// Renders the control to the specified HTML writer.
  142. /// </summary>
  143. /// <param name="writer">
  144. /// The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the control content.
  145. /// </param>
  146. protected override void Render(HtmlTextWriter writer)
  147. {
  148. if (!this.skipSimpleCaptcha)
  149. {
  150. this.RenderContents(writer);
  151. }
  152. }
  153. /// <summary>
  154. /// Renders the contents.
  155. /// </summary>
  156. /// <param name="output">
  157. /// The output.
  158. /// </param>
  159. protected override void RenderContents(HtmlTextWriter output)
  160. {
  161. output.RenderBeginTag(HtmlTextWriterTag.P);
  162. output.AddAttribute(HtmlTextWriterAttribute.For, SimpleCaptchaAnswerField);
  163. output.RenderBeginTag(HtmlTextWriterTag.Label);
  164. output.Write(this.simpleCaptchaLabel);
  165. output.RenderEndTag();
  166. output.AddAttribute(HtmlTextWriterAttribute.Id, SimpleCaptchaAnswerField);
  167. output.AddAttribute(HtmlTextWriterAttribute.Name, SimpleCaptchaAnswerField);
  168. output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
  169. output.AddAttribute(HtmlTextWriterAttribute.Tabindex, this.TabIndex.ToString());
  170. output.AddAttribute(HtmlTextWriterAttribute.Maxlength, Convert.ToString(SimpleCaptcha.MaxCaptchaLength));
  171. output.AddAttribute(HtmlTextWriterAttribute.Value, string.Empty);
  172. output.RenderBeginTag(HtmlTextWriterTag.Input);
  173. output.RenderEndTag();
  174. output.AddAttribute(HtmlTextWriterAttribute.Id, "spnSimpleCaptchaIncorrect");
  175. output.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");
  176. output.AddStyleAttribute(HtmlTextWriterStyle.Color, "Red");
  177. output.RenderBeginTag(HtmlTextWriterTag.Span);
  178. output.WriteLine(this.ErrorMessage);
  179. output.RenderEndTag();
  180. output.RenderEndTag();
  181. }
  182. #endregion
  183. }
  184. }