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