0 follower

CCaptcha

Package system.web.widgets.captcha
Inheritance class CCaptcha » CWidget » CBaseController » CComponent
Since 1.0
Version $Id$
Source Code framework/web/widgets/captcha/CCaptcha.php
CCaptcha renders a CAPTCHA image element.

CCaptcha is used together with CCaptchaAction to provide CAPTCHA - a way of preventing site spam.

The image element rendered by CCaptcha will display a CAPTCHA image generated by an action of class CCaptchaAction belonging to the current controller. By default, the action ID should be 'captcha', which can be changed by setting captchaAction.

CCaptcha may also render a button next to the CAPTCHA image. Clicking on the button will change the CAPTCHA image to be a new one in an AJAX way.

Since version 1.0.8, if clickableImage is set true, clicking on the CAPTCHA image will refresh the CAPTCHA.

A CCaptchaValidator may be used to validate that the user enters a verification code matching the code displayed in the CAPTCHA image.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
actionPrefix string the prefix to the IDs of the actions. CWidget
buttonLabel string the label for the refresh button. CCaptcha
buttonOptions array HTML attributes to be applied to the rendered refresh button element. CCaptcha
buttonType string the type of the refresh button. CCaptcha
captchaAction string the ID of the action that should provide CAPTCHA image. CCaptcha
clickableImage boolean whether to allow clicking on the CAPTCHA image to refresh the CAPTCHA letters. CCaptcha
controller CController the controller that this widget belongs to. CWidget
id string id of the widget. CWidget
imageOptions array HTML attributes to be applied to the rendered image element. CCaptcha
owner CBaseController owner/creator of this widget. CWidget
showRefreshButton boolean whether to display a button next to the CAPTCHA image. CCaptcha
viewPath string Returns the directory containing the view files for this widget. CWidget

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CWidget
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
actions() Returns a list of actions that are used by this widget. CWidget
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. 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() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
createWidget() Creates a widget and initializes it. CBaseController
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. 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
getController() Returns the controller that this widget belongs to. CWidget
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getId() Returns id of the widget. CWidget
getOwner() Returns owner/creator of this widget. It could be either a widget or a controller. CWidget
getViewFile() Looks for the view script file according to the view name. CWidget
getViewPath() Returns the directory containing the view files for this widget. CWidget
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
init() Initializes the widget. CWidget
raiseEvent() Raises an event. CComponent
registerClientScript() Registers the needed client scripts. CCaptcha
render() Renders a view. CWidget
renderFile() Renders a view file. CBaseController
renderInternal() Renders a view file. CBaseController
run() Renders the widget. CCaptcha
setId() Sets id of the widget. CWidget
widget() Creates a widget and executes it. CBaseController

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
renderImage() Renders the CAPTCHA image. CCaptcha

Property Details

buttonLabel property
public string $buttonLabel;

the label for the refresh button. Defaults to 'Get a new code'.

buttonOptions property
public array $buttonOptions;

HTML attributes to be applied to the rendered refresh button element.

buttonType property
public string $buttonType;

the type of the refresh button. This should be either 'link' or 'button'. The former refers to hyperlink button while the latter a normal push button. Defaults to 'link'.

captchaAction property
public string $captchaAction;

the ID of the action that should provide CAPTCHA image. Defaults to 'captcha', meaning the 'captcha' action of the current controller. This property may also be in the format of 'ControllerID/ActionID'. Underneath, this property is used by CController::createUrl to create the URL that would serve the CAPTCHA image. The action has to be of CCaptchaAction.

clickableImage property (available since v1.0.8)
public boolean $clickableImage;

whether to allow clicking on the CAPTCHA image to refresh the CAPTCHA letters. Defaults to false. Hint: you may want to set showRefreshButton to false if you set this property to be true because they serve for the same purpose. To enhance accessibility, you may set imageOptions to provide hints to end-users that the image is clickable.

imageOptions property
public array $imageOptions;

HTML attributes to be applied to the rendered image element.

showRefreshButton property
public boolean $showRefreshButton;

whether to display a button next to the CAPTCHA image. Clicking on the button will cause the CAPTCHA image to be changed to a new one. Defaults to true.

Method Details

registerClientScript() method (available since v1.0.2)
public void registerClientScript()
Source Code: framework/web/widgets/captcha/CCaptcha.php#106 (show)
public function registerClientScript()
{
    
$cs=Yii::app()->clientScript;
    
$id=$this->imageOptions['id'];
    
$url=$this->getController()->createUrl($this->captchaAction,array(CCaptchaAction::REFRESH_GET_VAR=>true));

    if(
$this->showRefreshButton)
    {
        
$cs->registerScript('Yii.CCaptcha#'.$id,'dummy');
        
$label=$this->buttonLabel===null?Yii::t('yii','Get a new code'):$this->buttonLabel;
        
$button=$this->buttonType==='button'?'ajaxButton':'ajaxLink';
        
$html=CHtml::$button($label,$url,array('success'=>'js:function(html){jQuery("#'.$id.'").attr("src",html)}'),$this->buttonOptions);
        
$js="jQuery('img#$id').after(\"".CJavaScript::quote($html).'");';
        
$cs->registerScript('Yii.CCaptcha#'.$id,$js);
    }

    if(
$this->clickableImage)
    {
        
$js="jQuery('#$id').click(function(){"
            
.CHtml::ajax(array(
                
'url'=>$url,
                
'success'=>"js:function(html){jQuery('#$id').attr('src',html)}",
            )).
'});';
        
$cs->registerScript('Yii.CCaptcha#2'.$id,$js);
    }
}

Registers the needed client scripts.

renderImage() method
protected void renderImage()
Source Code: framework/web/widgets/captcha/CCaptcha.php#91 (show)
protected function renderImage()
{
    if(isset(
$this->imageOptions['id']))
        
$id=$this->imageOptions['id'];
    else
        
$id=$this->imageOptions['id']=$this->getId();
    
$url=$this->getController()->createUrl($this->captchaAction);
    
$alt=isset($this->imageOptions['alt'])?$this->imageOptions['alt']:'';
    echo 
CHtml::image($url,$alt,$this->imageOptions);
}

Renders the CAPTCHA image.

run() method
public void run()
Source Code: framework/web/widgets/captcha/CCaptcha.php#82 (show)
public function run()
{
    
$this->renderImage();
    
$this->registerClientScript();
}

Renders the widget.