Yii Framework v1.1.10 绫诲弬鑰

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闇瑕CCaptchaCCaptchaValidator 閰嶅悎浣跨敤鏉ョ敓鎴愭墍闇鐨勯獙璇佺爜CAPTCHA鍥剧墖銆

浣犲繀椤婚厤缃瓹CaptchaAction鐨勫睘鎬 鏉ュ畾鍒舵墍闇鐨勯獙璇佺爜鏍峰紡銆

娉ㄦ剰锛孋CaptchaAction瑕佹眰寮鍚疨HP鐨凣D2鎷撳睍銆

浣跨敤楠岃瘉鐮佸寘鍚互涓嬫楠わ細
  1. 閲嶅啓CController::actions()骞朵笖娉ㄥ唽涓涓户鎵夸簬CCaptchaAction绫籌D涓衡榗aptcha鈥欑殑鏂规硶銆
  2. 鍦╢orm妯″瀷涓紝澹版槑涓涓彉閲忕敤浜庡瓨鍌ㄧ敤鎴疯緭鍏ョ殑楠岃瘉鐮佸苟涓 鐢ㄢ檆aptcha鈥橀獙璇佽繖涓彉閲忋
  3. 鍦ㄦ帶鍒跺櫒鐨勮鍥捐〃鍗曚腑鎻掑叆涓涓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

鍙椾繚鎶ゅ睘鎬

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
sessionKey string 杩斿洖瀛樺偍楠岃瘉鐮佺殑浼氳瘽鍙橀噺鍚嶃 CCaptchaAction

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__construct() 鏋勯犳柟娉曘 CAction
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
evaluateExpression() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
generateValidationHash() 鐢熸垚鍙敤浜庡鎴风鐨勫搱甯屽笺 CCaptchaAction
getController() 杩斿洖鎷ユ湁杩欎釜鍔ㄤ綔鐨勬帶鍒跺櫒銆 CAction
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
getId() 杩斿洖鍔ㄤ綔鐨処D銆 CAction
getVerifyCode() 鑾峰彇楠岃瘉鐮併 CCaptchaAction
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent
run() 鎵ц鍔ㄤ綔銆 CCaptchaAction
runWithParams() 杩愯甯︽湁璇锋眰鍙傛暟鐨勫璞° CAction
validate() 楠岃瘉鐢ㄦ埛杈撳叆鏄惁鍖归厤鐢熸垚鐨勯獙璇佺爜銆 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 && mt_rand(0,10) > || !($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;
}

楠岃瘉鐢ㄦ埛杈撳叆鏄惁鍖归厤鐢熸垚鐨勯獙璇佺爜銆

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.