using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.CMSHelper;
using CMS.FormControls;
using CMS.FormEngine;
using CMS.SettingsProvider;
using CMS.SiteProvider;
using CMS.ExtendedControls;
public partial class CMSFormControls_Captcha_SecurityCode : FormEngineUserControl, ICaptchaControl
{
#region "Variables"
private FormEngineUserControl mCaptchaControl = null;
#endregion
#region "Methods"
protected override void OnInit(EventArgs e)
{
Controls.Add(CaptchaControl);
}
///
/// Gets selected captcha from settings.
///
/// Site name
protected CaptchaEnum CaptchaSetting(string siteName)
{
CaptchaEnum selectedCaptcha = CaptchaEnum.Default;
selectedCaptcha = (CaptchaEnum)SettingsKeyProvider.GetIntValue(siteName + ".CMSCaptchaControl");
return selectedCaptcha;
}
#endregion
#region "Properties"
///
/// Gets captcha control.
///
protected FormEngineUserControl CaptchaControl
{
get
{
if (mCaptchaControl == null)
{
CaptchaEnum selectedCaptcha = CaptchaSetting(CMSContext.CurrentSiteName);
string captchaControlName = null;
switch (selectedCaptcha)
{
// Default simple captcha
case CaptchaEnum.Default:
captchaControlName = "SimpleCaptcha";
//captchaPath = "~/CMSFormControls/Captcha/SimpleCaptcha.ascx";
break;
// Logic captcha
case CaptchaEnum.Logic:
captchaControlName = "LogicCaptcha";
//captchaPath = "~/CMSFormControls/Captcha/LogicCaptcha.ascx";
break;
// Text captcha
case CaptchaEnum.Text:
captchaControlName = "TextCaptcha";
//captchaPath = "~/CMSFormControls/Captcha/TextCaptcha.ascx";
break;
}
// Get form control
FormUserControlInfo ctrl = FormUserControlInfoProvider.GetFormUserControlInfo(captchaControlName);
if (ctrl == null)
{
throw new Exception(string.Format("[SecurityCode]: The CAPTCHA control with name '{0}' doesn't exist.", captchaControlName));
}
mCaptchaControl = Page.LoadUserControl(ctrl.UserControlFileName) as FormEngineUserControl;
mCaptchaControl.ID = "captchaControl";
// Load the default properties of the form control
FormInfo properties = FormHelper.GetFormControlParameters(captchaControlName, ctrl.UserControlParameters, false);
mCaptchaControl.LoadDefaultProperties(properties);
}
return mCaptchaControl;
}
}
///
/// Captcha as interface ICaptchaControl.
///
protected ICaptchaControl Captcha
{
get
{
return CaptchaControl as ICaptchaControl;
}
}
///
/// Gets or sets value.
///
public override object Value
{
get
{
return CaptchaControl.Value;
}
set
{
CaptchaControl.Value = value;
}
}
///
/// Sets whether the captcha control should display info label.
///
public bool ShowInfoLabel
{
set
{
CaptchaControl.SetValue("ShowInfoLabel", value);
}
}
///
/// Gets or sets enabled state of captcha controls.
///
public override bool Enabled
{
get
{
return base.Enabled;
}
set
{
CaptchaControl.Enabled = base.Enabled = value;
}
}
///
/// Returns true if entered data is valid.
///
public override bool IsValid()
{
bool isValid = CaptchaControl.IsValid();
if (!isValid)
{
ValidationError = GetString("SecurityCode.ValidationError");
}
return isValid;
}
#endregion
#region ICaptchaControl Members
///
/// Generates new captcha.
///
public void GenerateNew()
{
Captcha.GenerateNew();
}
///
/// Indicates whether the after text should be displayed.
///
public bool ShowAfterText
{
get
{
return Captcha.ShowAfterText;
}
set
{
Captcha.ShowAfterText = value;
}
}
///
/// Indicates if CAPTCHA code is always generated and stored to the session.
///
public bool KeepCodeAutomatically
{
get
{
return Captcha.KeepCodeAutomatically;
}
set
{
Captcha.KeepCodeAutomatically = value;
}
}
///
/// Indicates whether new captcha is generated after the wrong input was entered.
///
public bool AlwaysGenerate
{
get
{
return Captcha.AlwaysGenerate;
}
set
{
Captcha.AlwaysGenerate = value;
}
}
#endregion
}