CCaptchaAction
鍖 | system.web.widgets.captcha |
---|---|
缁ф壙 | class CCaptchaAction » CAction » CComponent |
瀹炵幇 | IAction |
婧愯嚜 | 1.0 |
鐗堟湰 | $Id: CCaptchaAction.php 3515 2011-12-28 12:29:24Z mdomba $ |
婧愮爜 | framework/web/widgets/captcha/CCaptchaAction.php |
CCaptchaAction鐢ㄤ簬鐢熸垚楠岃瘉鐮佸浘鐗囥
CCaptchaAction闇瑕CCaptcha鍜CCaptchaValidator 閰嶅悎浣跨敤鏉ョ敓鎴愭墍闇鐨勯獙璇佺爜CAPTCHA鍥剧墖銆
浣犲繀椤婚厤缃瓹CaptchaAction鐨勫睘鎬 鏉ュ畾鍒舵墍闇鐨勯獙璇佺爜鏍峰紡銆
娉ㄦ剰锛孋CaptchaAction瑕佹眰寮鍚疨HP鐨凣D2鎷撳睍銆
浣跨敤楠岃瘉鐮佸寘鍚互涓嬫楠わ細
CCaptchaAction闇瑕CCaptcha鍜CCaptchaValidator 閰嶅悎浣跨敤鏉ョ敓鎴愭墍闇鐨勯獙璇佺爜CAPTCHA鍥剧墖銆
浣犲繀椤婚厤缃瓹CaptchaAction鐨勫睘鎬 鏉ュ畾鍒舵墍闇鐨勯獙璇佺爜鏍峰紡銆
娉ㄦ剰锛孋CaptchaAction瑕佹眰寮鍚疨HP鐨凣D2鎷撳睍銆
浣跨敤楠岃瘉鐮佸寘鍚互涓嬫楠わ細
- 閲嶅啓CController::actions()骞朵笖娉ㄥ唽涓涓户鎵夸簬CCaptchaAction绫籌D涓衡榗aptcha鈥欑殑鏂规硶銆
- 鍦╢orm妯″瀷涓紝澹版槑涓涓彉閲忕敤浜庡瓨鍌ㄧ敤鎴疯緭鍏ョ殑楠岃瘉鐮佸苟涓 鐢ㄢ檆aptcha鈥橀獙璇佽繖涓彉閲忋
- 鍦ㄦ帶鍒跺櫒鐨勮鍥捐〃鍗曚腑鎻掑叆涓涓CCaptcha缁勪欢銆
鍏叡灞炴
灞炴 | 绫诲瀷 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|---|
backColor | integer | 鑳屾櫙棰滆壊銆傚锛0x55FF00銆 榛樿涓0xFFFFFF锛屾剰鍛崇潃鐧借壊銆 | CCaptchaAction |
controller | CController | 鎷ユ湁杩欎釜鍔ㄤ綔鐨勬帶鍒跺櫒銆 | CAction |
fixedVerifyCode | string | 鍥哄畾鐨勯獙璇佺爜銆傝缃繖涓睘鎬т互鍚庯紝 getVerifyCode鏂规硶浼氫竴鐩磋繑鍥炶繖涓笺 褰撴垜浠湪鑷姩娴嬭瘯涓兂姣忔杩斿洖 鐩稿悓鐨勯獙璇佺爜鍊兼椂浼氱敤鍒般 榛樿鏄痭ull锛岃繖鎰忓懗鐫灏嗛殢鏈虹敓鎴愰獙璇佺爜銆 | CCaptchaAction |
fontFile | string | TrueType鏂囧瓧鏂囦欢銆傞粯璁や负Duality. | CCaptchaAction |
foreColor | integer | 瀛椾綋棰滆壊銆傚锛0x55FF00銆傞粯璁ゆ槸0x2040A0锛堣摑鑹诧級銆 | CCaptchaAction |
height | integer | 楠岃瘉鐮佸浘鐗囬珮搴︺傞粯璁や负50銆 | CCaptchaAction |
id | string | 鍔ㄤ綔鐨処D銆 | CAction |
maxLength | integer | 鐢熸垚瀛楃涓茬殑鏈澶ч暱搴︺傞粯璁ゆ槸7銆 | CCaptchaAction |
minLength | integer | 鐢熸垚瀛楃涓茬殑鏈灏忛暱搴︺傞粯璁ゆ槸6銆 | CCaptchaAction |
offset | integer | 瀛楃闂村亸绉婚噺銆傞粯璁ゆ槸-2銆 浣犲彲浠ヨ皟鏁磋繖涓兼潵澧炲姞鎴栧噺灏戦獙璇佺爜鐨勫彲璇绘с | CCaptchaAction |
padding | integer | 鏂囧瓧鍛ㄨ竟濉厖澶у皬銆傞粯璁や负2銆 | CCaptchaAction |
testLimit | integer | 鐩稿悓楠岃瘉鐮佸嚭鐜扮殑娆℃暟銆傞粯璁や负3銆 灏忎簬绛変簬0鐨勫兼剰鍛崇潃涓嶉檺鍒讹紙鑷増鏈1. | CCaptchaAction |
transparent | boolean | 鏄惁浣跨敤閫忔槑鑳屾櫙銆傞粯璁や负false銆 | CCaptchaAction |
verifyCode | string | 鑾峰彇楠岃瘉鐮併 | CCaptchaAction |
width | integer | 楠岃瘉鐮佸浘鐗囧搴︺傞粯璁や负120銆 | CCaptchaAction |
鍏叡鏂规硶
鍙椾繚鎶ゆ柟娉
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
generateVerifyCode() | 鐢熸垚涓涓柊楠岃瘉鐮併 | CCaptchaAction |
getSessionKey() | 杩斿洖瀛樺偍楠岃瘉鐮佺殑浼氳瘽鍙橀噺鍚嶃 | CCaptchaAction |
renderImage() | 娓叉煋鍩轰簬楠岃瘉鐮佸肩殑楠岃瘉鐮佸浘鐗囥 | CCaptchaAction |
runWithParamsInternal() | 鎵ц涓涓甫鏈夊懡鍚嶅弬鏁扮殑瀵硅薄鐨勬柟娉曘 | CAction |
灞炴ц缁
backColor
灞炴
public integer $backColor;
鑳屾櫙棰滆壊銆傚锛0x55FF00銆 榛樿涓0xFFFFFF锛屾剰鍛崇潃鐧借壊銆
fixedVerifyCode
灞炴
锛堝彲鐢ㄨ嚜 v1.1.4锛
public string $fixedVerifyCode;
鍥哄畾鐨勯獙璇佺爜銆傝缃繖涓睘鎬т互鍚庯紝 getVerifyCode鏂规硶浼氫竴鐩磋繑鍥炶繖涓笺 褰撴垜浠湪鑷姩娴嬭瘯涓兂姣忔杩斿洖 鐩稿悓鐨勯獙璇佺爜鍊兼椂浼氱敤鍒般 榛樿鏄痭ull锛岃繖鎰忓懗鐫灏嗛殢鏈虹敓鎴愰獙璇佺爜銆
fontFile
灞炴
public string $fontFile;
TrueType鏂囧瓧鏂囦欢銆傞粯璁や负Duality.ttf锛岀敱Yii 鍙戣鐗堜腑鎻愪緵銆
foreColor
灞炴
public integer $foreColor;
瀛椾綋棰滆壊銆傚锛0x55FF00銆傞粯璁ゆ槸0x2040A0锛堣摑鑹诧級銆
height
灞炴
public integer $height;
楠岃瘉鐮佸浘鐗囬珮搴︺傞粯璁や负50銆
maxLength
灞炴
public integer $maxLength;
鐢熸垚瀛楃涓茬殑鏈澶ч暱搴︺傞粯璁ゆ槸7銆
minLength
灞炴
public integer $minLength;
鐢熸垚瀛楃涓茬殑鏈灏忛暱搴︺傞粯璁ゆ槸6銆
offset
灞炴
锛堝彲鐢ㄨ嚜 v1.1.7锛
public integer $offset;
瀛楃闂村亸绉婚噺銆傞粯璁ゆ槸-2銆 浣犲彲浠ヨ皟鏁磋繖涓兼潵澧炲姞鎴栧噺灏戦獙璇佺爜鐨勫彲璇绘с
padding
灞炴
public integer $padding;
鏂囧瓧鍛ㄨ竟濉厖澶у皬銆傞粯璁や负2銆
sessionKey
灞炴
鍙
protected string getSessionKey()
杩斿洖瀛樺偍楠岃瘉鐮佺殑浼氳瘽鍙橀噺鍚嶃
testLimit
灞炴
public integer $testLimit;
鐩稿悓楠岃瘉鐮佸嚭鐜扮殑娆℃暟銆傞粯璁や负3銆 灏忎簬绛変簬0鐨勫兼剰鍛崇潃涓嶉檺鍒讹紙鑷増鏈1.1.2寮濮嬫敮鎸侊級銆
transparent
灞炴
public boolean $transparent;
鏄惁浣跨敤閫忔槑鑳屾櫙銆傞粯璁や负false銆
verifyCode
灞炴
鍙
public string getVerifyCode(boolean $regenerate=false)
鑾峰彇楠岃瘉鐮併
width
灞炴
public integer $width;
楠岃瘉鐮佸浘鐗囧搴︺傞粯璁や负120銆
鏂规硶璇︾粏
generateValidationHash()
鏂规硶
锛堝彲鐢ㄨ嚜 v1.1.7锛
public string generateValidationHash(string $code)
| ||
$code | string | 楠岃瘉鐮 |
{return} | string | 鐢遍獙璇佺爜鐢熸垚鐨勪竴涓搱甯屽 |
婧愮爜锛 framework/web/widgets/captcha/CCaptchaAction.php#134 (鏄剧ず)
public function generateValidationHash($code)
{
for($h=0,$i=strlen($code)-1;$i>=0;--$i)
$h+=ord($code[$i]);
return $h;
}
鐢熸垚鍙敤浜庡鎴风鐨勫搱甯屽笺
generateVerifyCode()
鏂规硶
protected string generateVerifyCode()
| ||
{return} | string | 鐢熸垚鐨勯獙璇佺爜銆 |
婧愮爜锛 framework/web/widgets/captcha/CCaptchaAction.php#185 (鏄剧ず)
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 = mt_rand($this->minLength,$this->maxLength);
$letters = 'bcdfghjklmnpqrstvwxyz';
$vowels = 'aeiou';
$code = '';
for($i = 0; $i < $length; ++$i)
{
if($i % 2 && mt_rand(0,10) > 2 || !($i % 2) && mt_rand(0,10) > 9)
$code.=$vowels[mt_rand(0,4)];
else
$code.=$letters[mt_rand(0,20)];
}
return $code;
}
鐢熸垚涓涓柊楠岃瘉鐮併
getSessionKey()
鏂规硶
protected string getSessionKey()
| ||
{return} | string | 浼氳瘽鍙橀噺鍚嶇О |
婧愮爜锛 framework/web/widgets/captcha/CCaptchaAction.php#213 (鏄剧ず)
protected function getSessionKey()
{
return self::SESSION_VAR_PREFIX . Yii::app()->getId() . '.' . $this->getController()->getUniqueId() . '.' . $this->getId();
}
杩斿洖瀛樺偍楠岃瘉鐮佺殑浼氳瘽鍙橀噺鍚嶃
getVerifyCode()
鏂规硶
public string getVerifyCode(boolean $regenerate=false)
| ||
$regenerate | boolean | 楠岃瘉鐮佹槸鍚﹂噸鏂扮敓鎴愩 |
{return} | string | 楠岃瘉鐮併 |
婧愮爜锛 framework/web/widgets/captcha/CCaptchaAction.php#146 (鏄剧ず)
public function getVerifyCode($regenerate=false)
{
if($this->fixedVerifyCode !== null)
return $this->fixedVerifyCode;
$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];
}
鑾峰彇楠岃瘉鐮併
renderImage()
鏂规硶
protected string renderImage(string $code)
| ||
$code | string | 楠岃瘉鐮佸 |
{return} | string | 鍥剧墖鍐呭 |
婧愮爜锛 framework/web/widgets/captcha/CCaptchaAction.php#223 (鏄剧ず)
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';
$length = strlen($code);
$box = imagettfbbox(30,0,$this->fontFile,$code);
$w = $box[4] - $box[0] + $this->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] + $this->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);
}
娓叉煋鍩轰簬楠岃瘉鐮佸肩殑楠岃瘉鐮佸浘鐗囥
run()
鏂规硶
public void run()
|
婧愮爜锛 framework/web/widgets/captcha/CCaptchaAction.php#110 (鏄剧ず)
public function run()
{
if(isset($_GET[self::REFRESH_GET_VAR])) // AJAX request for regenerating code
{
$code=$this->getVerifyCode(true);
echo CJSON::encode(array(
'hash1'=>$this->generateValidationHash($code),
'hash2'=>$this->generateValidationHash(strtolower($code)),
// we add a random 'v' parameter so that FireFox can refresh the image
// when src attribute of image tag is changed
'url'=>$this->getController()->createUrl($this->getId(),array('v' => uniqid())),
));
}
else
$this->renderImage($this->getVerifyCode());
Yii::app()->end();
}
鎵ц鍔ㄤ綔銆
validate()
鏂规硶
public boolean validate(string $input, boolean $caseSensitive)
| ||
$input | string | 鐢ㄦ埛杈撳叆鍊 |
$caseSensitive | boolean | 鍖归厤鏃舵槸鍚﹀尯鍒嗗ぇ灏忓啓 |
{return} | boolean | 杈撳叆鏄惁鏈夋晥 |
婧愮爜锛 framework/web/widgets/captcha/CCaptchaAction.php#168 (鏄剧ず)
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->testLimit > 0)
$this->getVerifyCode(true);
return $valid;
}
楠岃瘉鐢ㄦ埛杈撳叆鏄惁鍖归厤鐢熸垚鐨勯獙璇佺爜銆