Package | system.web.widgets.captcha |
---|---|
Inheritance | class CCaptchaAction » CAction » CComponent |
Implements | IAction |
Since | 1.0 |
Version | $Id$ |
Source Code | framework/web/widgets/captcha/CCaptchaAction.php |
Property | Type | Description | Defined By |
---|---|---|---|
backColor | integer | the background color. | CCaptchaAction |
controller | CController | the controller who owns this action. | CAction |
fontFile | string | the TrueType font file. | CCaptchaAction |
foreColor | integer | the font color. | CCaptchaAction |
height | integer | the height of the generated CAPTCHA image. | CCaptchaAction |
id | string | id of this action | CAction |
maxLength | integer | the maximum length for randomly generated word. | CCaptchaAction |
minLength | integer | the minimum length for randomly generated word. | CCaptchaAction |
padding | integer | padding around the text. | CCaptchaAction |
testLimit | integer | how many times should the same CAPTCHA be displayed. | CCaptchaAction |
transparent | boolean | whether to use transparent background. | CCaptchaAction |
verifyCode | string | Gets the verification code. | CCaptchaAction |
width | integer | the width of the generated CAPTCHA image. | CCaptchaAction |
Property | Type | Description | Defined By |
---|---|---|---|
sessionKey | string | Returns the session variable name used to store verification code. | CCaptchaAction |
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | CComponent |
__construct() | Constructor. | CAction |
__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 |
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 |
canGetProperty() | Determines whether a property can be read. | CComponent |
canSetProperty() | Determines whether a property can be set. | CComponent |
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 |
getController() | Returns the controller who owns this action. | CAction |
getEventHandlers() | Returns the list of attached event handlers for an event. | CComponent |
getId() | Returns id of this action | CAction |
getVerifyCode() | Gets the verification code. | CCaptchaAction |
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 |
raiseEvent() | Raises an event. | CComponent |
run() | Runs the action. | CCaptchaAction |
validate() | Validates the input to see if it matches the generated code. | CCaptchaAction |
Method | Description | Defined By |
---|---|---|
generateVerifyCode() | Generates a new verification code. | CCaptchaAction |
getSessionKey() | Returns the session variable name used to store verification code. | CCaptchaAction |
renderImage() | Renders the CAPTCHA image based on the code. | CCaptchaAction |
the background color. For example, 0x55FF00. Defaults to 0xFFFFFF, meaning white color.
the TrueType font file. Defaults to Duality.ttf which is provided with the Yii release.
the font color. For example, 0x55FF00. Defaults to 0x2040A0 (blue color).
the height of the generated CAPTCHA image. Defaults to 50.
the maximum length for randomly generated word. Defaults to 7.
the minimum length for randomly generated word. Defaults to 6.
padding around the text. Defaults to 2.
Returns the session variable name used to store verification code.
how many times should the same CAPTCHA be displayed. Defaults to 3.
whether to use transparent background. Defaults to false.
Gets the verification code.
the width of the generated CAPTCHA image. Defaults to 120.
protected string generateVerifyCode()
| ||
{return} | string | the generated verification code |
protected function generateVerifyCode()
{
if($this->minLength<3)
$this->minLength=3;
if($this->maxLength>20)
$this->maxLength=20;
if($this->minLength>$this->maxLength)
$this->maxLength=$this->minLength;
$length=rand($this->minLength,$this->maxLength);
$letters='bcdfghjklmnpqrstvwxyz';
$vowels='aeiou';
$code='';
for($i=0;$i<$length;++$i)
{
if($i%2 && rand(0,10)>2 || !($i%2) && rand(0,10)>9)
$code.=$vowels[rand(0,4)];
else
$code.=$letters[rand(0,20)];
}
return $code;
}
Generates a new verification code.
protected string getSessionKey()
| ||
{return} | string | the session variable name |
protected function getSessionKey()
{
return self::SESSION_VAR_PREFIX.Yii::app()->getId().'.'.$this->getController()->getUniqueId().'.'.$this->getId();
}
Returns the session variable name used to store verification code.
public string getVerifyCode(string $regenerate=false)
| ||
$regenerate | string | whether the verification code should be regenerated. |
{return} | string | the verification code. |
public function getVerifyCode($regenerate=false)
{
$session=Yii::app()->session;
$session->open();
$name=$this->getSessionKey();
if($session[$name]===null || $regenerate)
{
$session[$name]=$this->generateVerifyCode();
$session[$name.'count']=1;
}
return $session[$name];
}
Gets the verification code.
protected string renderImage(string $code)
| ||
$code | string | the verification code |
{return} | string | image content |
protected function renderImage($code)
{
$image=imagecreatetruecolor($this->width,$this->height);
$backColor=imagecolorallocate($image,
(int)($this->backColor%0x1000000/0x10000),
(int)($this->backColor%0x10000/0x100),
$this->backColor%0x100);
imagefilledrectangle($image,0,0,$this->width,$this->height,$backColor);
imagecolordeallocate($image,$backColor);
if($this->transparent)
imagecolortransparent($image,$backColor);
$foreColor=imagecolorallocate($image,
(int)($this->foreColor%0x1000000/0x10000),
(int)($this->foreColor%0x10000/0x100),
$this->foreColor%0x100);
if($this->fontFile===null)
$this->fontFile=dirname(__FILE__).'/Duality.ttf';
$offset=2;
$length=strlen($code);
$box=imagettfbbox(30,0,$this->fontFile,$code);
$w=$box[4]-$box[0]-$offset*($length-1);
$h=$box[1]-$box[5];
$scale=min(($this->width-$this->padding*2)/$w,($this->height-$this->padding*2)/$h);
$x=10;
$y=round($this->height*27/40);
for($i=0;$i<$length;++$i)
{
$fontSize=(int)(rand(26,32)*$scale*0.8);
$angle=rand(-10,10);
$letter=$code[$i];
$box=imagettftext($image,$fontSize,$angle,$x,$y,$foreColor,$this->fontFile,$letter);
$x=$box[2]-$offset;
}
imagecolordeallocate($image,$foreColor);
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
}
Renders the CAPTCHA image based on the code.
public void run()
|
public function run()
{
if(isset($_GET[self::REFRESH_GET_VAR])) // AJAX request for regenerating code
{
$code=$this->getVerifyCode(true);
// we add a random 'v' parameter so that FireFox can refresh the image
// when src attribute of image tag is changed
echo $this->getController()->createUrl($this->getId(),array('v'=>rand(0,10000)));
}
else
{
$this->renderImage($this->getVerifyCode());
Yii::app()->end();
}
}
Runs the action. If the GET parameter wsdlVar exists, the action will serve WSDL content; If not, the action will handle the remote method invocation.
public whether validate(string $input, boolean $caseSensitive)
| ||
$input | string | user input |
$caseSensitive | boolean | whether the comparison should be case-sensitive |
{return} | whether | the input is valid |
public function validate($input,$caseSensitive)
{
$code=$this->getVerifyCode();
$valid=$caseSensitive?($input===$code):!strcasecmp($input,$code);
$session=Yii::app()->session;
$session->open();
$name=$this->getSessionKey().'count';
$session[$name]=$session[$name]+1;
if($session[$name]>$this->testLimit)
$this->getVerifyCode(true);
return $valid;
}
Validates the input to see if it matches the generated code.
Signup or Login in order to comment.