CCaptcha

优质
小牛编辑
122浏览
2023-12-01
所有包 | 属性 | 方法
system.web.widgets.captcha
继承class CCaptcha » CWidget » CBaseController » CComponent
源自1.0
版本$Id: CCaptcha.php 3515 2011-12-28 12:29:24Z mdomba $
源码framework/web/widgets/captcha/CCaptcha.php
CCaptcha用于渲染验证码图片元素。

CCaptcha和CCaptchaAction一起用来支持CAPTCHA -一种阻止网站的垃圾信息的方法。

由CCaptcha生成的图像元素显示一张由当前控制器中的CCaptchaAction动作 生成的验证码图片。 默认是,动作ID应该为‘captcha’,可以通过修改captchaAction设置。

CCaptcha同时可以在验证码图片旁边渲染一个按钮。点击这个按钮 就可以通过AJAX方法来改变验证码图片。

如果clickableImage设为true,那么点击验证码图片 就会刷新验证码。

CCaptchaValidator可以用来验证用户输入 的验证码是否跟显示的验证码图片相同。

公共属性

隐藏继承属性

属性类型描述定义在
actionPrefixstringactions的ID的前缀。 当微件在CController::actions中声明了 动作提供者,可以为其动作的ID指定前缀以区别 于别的微件或控制器。当微件用于控制器 的视图中时,必须配置同样的前缀。CWidget
buttonLabelstring刷新按钮的标签。默认为‘Get a new code’。CCaptcha
buttonOptionsarray用于渲染按钮元素的HTML属性。CCaptcha
buttonTypestring刷新按钮类型。可以是‘link’或者是‘button’。 前者是超链接按钮,后者是普通的按钮。 默认的是‘link’。CCaptcha
captchaActionstring提供验证码图片的动作ID。默认为‘captcha’, 意味着当前控制器的‘captcha’动作。这个属性也可以是‘ControllerID/ActionID’ 这种格式。下面,这个属性是由CController::createUrl来 创建使用于验证码图片的URL。 这个动作必须为CCaptchaAction。CCaptcha
clickableImageboolean是否允许通过点击验证码图片刷新验证码字符。 默认为false。注意:如果你想要设置这个属性为true,你应该 设置showRefreshButton为false,因为他们提供相同的功能。 为了增强访问性,你应该设置imageOptions以通知用户, 图片是可以点击的。CCaptcha
controllerCController返回此微件所属的控制器。CWidget
idstring返回此微件的ID。如果需要的话,将生产一个新的ID并将其返回。CWidget
imageOptionsarray用于渲染图像元素的HTML属性。CCaptcha
ownerCBaseController返回此微件的所有者或创建者。CWidget
showRefreshButtonboolean是否显示用来刷新验证码图片的按钮。点击按钮时会刷新 验证码图片。默认为true。CCaptcha
skinmixed微件使用的皮肤的名称。默认为“default”。 如果此属性设置为false,微件将不会有皮肤被使用。CWidget
viewPathstring返回包含此微件所需的视图文件的路径。CWidget

公共方法

隐藏继承方法

方法描述定义在
__call()如果类中没有调的方法名,则调用这个方法。CComponent
__construct()构造器。CWidget
__get()返回一个属性值、一个事件处理程序列表或一个行为名称。CComponent
__isset()检查一个属性是否为null。CComponent
__set()设置一个组件的属性值。CComponent
__unset()设置一个组件的属性为null。CComponent
actions()返回此widget使用的动作的列表。CWidget
asa()返回这个名字的行为对象。CComponent
attachBehavior()附加一个行为到组件。CComponent
attachBehaviors()附加一个行为列表到组件。CComponent
attachEventHandler()为事件附加一个事件处理程序。CComponent
beginCache()Begins fragment caching.CBaseController
beginClip()Begins recording a clip.CBaseController
beginContent()Begins the rendering of content that is to be decorated by the specified view.CBaseController
beginWidget()Creates a widget and executes it.CBaseController
canGetProperty()确定属性是否可读。CComponent
canSetProperty()确定属性是否可写。CComponent
checkRequirements()检查是否支持GD库。CCaptcha
createWidget()Creates a widget and initializes it.CBaseController
detachBehavior()从组件中分离一个行为。CComponent
detachBehaviors()从组件中分离所有行为。CComponent
detachEventHandler()分离一个存在的事件处理程序。CComponent
disableBehavior()禁用一个附加行为。CComponent
disableBehaviors()禁用组件附加的所有行为。CComponent
enableBehavior()启用一个附加行为。CComponent
enableBehaviors()启用组件附加的所有行为。CComponent
endCache()Ends fragment caching.CBaseController
endClip()Ends recording a clip.CBaseController
endContent()Ends the rendering of content.CBaseController
endWidget()Ends the execution of the named widget.CBaseController
evaluateExpression()计算一个PHP表达式,或根据组件上下文执行回调。CComponent
getController()返回此微件所属的控制器。CWidget
getEventHandlers()返回一个事件的附加处理程序列表。CComponent
getId()返回此微件的ID。如果需要的话,将生产一个新的ID并将其返回。CWidget
getOwner()返回此微件的所有者或创建者。CWidget
getViewFile()根据视图名查找视图文件。CWidget
getViewPath()返回包含此微件所需的视图文件的路径。CWidget
hasEvent()确定一个事件是否定义。CComponent
hasEventHandler()检查事件是否有附加的处理程序。CComponent
hasProperty()确定属性是否被定义。CComponent
init()初始化此微件。CWidget
raiseEvent()发起一个事件。CComponent
registerClientScript()加载需要的客户端脚本。CCaptcha
render()渲染一个视图。CWidget
renderFile()Renders a view file.CBaseController
renderInternal()Renders a view file.CBaseController
run()渲染挂件。CCaptcha
setId()设置此微件的ID。CWidget
widget()Creates a widget and executes it.CBaseController

受保护方法

隐藏继承方法

方法描述定义在
renderImage()渲染验证码图片。CCaptcha

属性详细

buttonLabel 属性 public string $buttonLabel;

刷新按钮的标签。默认为‘Get a new code’。

buttonOptions 属性 public array $buttonOptions;

用于渲染按钮元素的HTML属性。

buttonType 属性 public string $buttonType;

刷新按钮类型。可以是‘link’或者是‘button’。 前者是超链接按钮,后者是普通的按钮。 默认的是‘link’。

captchaAction 属性 public string $captchaAction;

提供验证码图片的动作ID。默认为‘captcha’, 意味着当前控制器的‘captcha’动作。这个属性也可以是‘ControllerID/ActionID’ 这种格式。下面,这个属性是由CController::createUrl来 创建使用于验证码图片的URL。 这个动作必须为CCaptchaAction。

clickableImage 属性 public boolean $clickableImage;

是否允许通过点击验证码图片刷新验证码字符。 默认为false。注意:如果你想要设置这个属性为true,你应该 设置showRefreshButton为false,因为他们提供相同的功能。 为了增强访问性,你应该设置imageOptions以通知用户, 图片是可以点击的。

imageOptions 属性 public array $imageOptions;

用于渲染图像元素的HTML属性。

showRefreshButton 属性 public boolean $showRefreshButton;

是否显示用来刷新验证码图片的按钮。点击按钮时会刷新 验证码图片。默认为true。

方法详细

checkRequirements() 方法 (可用自 v1.1.5)
public static boolean checkRequirements()
{return}booleantrue if GD with FreeType support is loaded, otherwise false
源码: framework/web/widgets/captcha/CCaptcha.php#160 (显示) publicstaticfunctioncheckRequirements()
{
if(extension_loaded('gd'))
{
$gdinfo=gd_info();
if($gdinfo['FreeTypeSupport'])
returntrue;
}
returnfalse;
}

检查是否支持GD库。

registerClientScript() 方法
public void registerClientScript()
源码: framework/web/widgets/captcha/CCaptcha.php#108 (显示) publicfunctionregisterClientScript()
{
$cs=Yii::app()->clientScript;
$id=$this->imageOptions['id'];
$url=$this->getController()->createUrl($this->captchaAction,array(CCaptchaAction::REFRESH_GET_VAR=>true));

$js="";
if($this->showRefreshButton)
{
$cs->registerScript('Yii.CCaptcha#'.$id,'dummy');
$label=$this->buttonLabel===null?Yii::t('yii','Getanewcode'):$this->buttonLabel;
$options=$this->buttonOptions;
if(isset($options['id']))
$buttonID=$options['id'];
else
$buttonID=$options['id']=$id.'_button';
if($this->buttonType==='button')
$html=CHtml::button($label,$options);
else
$html=CHtml::link($label,$url,$options);
$js="jQuery('#$id').after(".CJSON::encode($html).");";
$selector="#$buttonID";
}

if($this->clickableImage)
$selector=isset($selector)?"$selector,#$id":"#$id";

if(!isset($selector))
return;

$js.="
jQuery('$selector').live('click',function(){
jQuery.ajax({
url:".CJSON::encode($url).",
dataType:'json',
cache:false,
success:function(data){
jQuery('#$id').attr('src',data['url']);
jQuery('body').data('{$this->captchaAction}.hash',[data['hash1'],data['hash2']]);
}
});
returnfalse;
});
";
$cs->registerScript('Yii.CCaptcha#'.$id,$js);
}

加载需要的客户端脚本。

renderImage() 方法
protected void renderImage()
源码: framework/web/widgets/captcha/CCaptcha.php#95 (显示) protectedfunctionrenderImage()
{
if(!isset($this->imageOptions['id']))
$this->imageOptions['id']=$this->getId();

$url=$this->getController()->createUrl($this->captchaAction,array('v'=>uniqid()));
$alt=isset($this->imageOptions['alt'])?$this->imageOptions['alt']:'';
echoCHtml::image($url,$alt,$this->imageOptions);
}

渲染验证码图片。

run() 方法
public void run()
源码: framework/web/widgets/captcha/CCaptcha.php#81 (显示) publicfunctionrun()
{
if(self::checkRequirements())
{
$this->renderImage();
$this->registerClientScript();
}
else
thrownewCException(Yii::t('yii','GDandFreeTypePHPextensionsarerequired.'));
}

渲染挂件。