CCaptchaValidator.php
3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* CCaptchaValidator class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CCaptchaValidator validates that the attribute value is the same as the verification code displayed in the CAPTCHA.
*
* CCaptchaValidator should be used together with {@link CCaptchaAction}.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.validators
* @since 1.0
*/
class CCaptchaValidator extends CValidator
{
/**
* @var boolean whether the comparison is case sensitive. Defaults to false.
*/
public $caseSensitive=false;
/**
* @var string ID of the action that renders the CAPTCHA image. Defaults to 'captcha',
* meaning the 'captcha' action declared in the current controller.
* This can also be a route consisting of controller ID and action ID.
*/
public $captchaAction='captcha';
/**
* @var boolean whether the attribute value can be null or empty.
* Defaults to false, meaning the attribute is invalid if it is empty.
*/
public $allowEmpty=false;
/**
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* @param CModel $object the object being validated
* @param string $attribute the attribute being validated
*/
protected function validateAttribute($object,$attribute)
{
$value=$object->$attribute;
if($this->allowEmpty && $this->isEmpty($value))
return;
$captcha=$this->getCaptchaAction();
if(!$captcha->validate($value,$this->caseSensitive))
{
$message=$this->message!==null?$this->message:Yii::t('yii','The verification code is incorrect.');
$this->addError($object,$attribute,$message);
}
}
/**
* Returns the CAPTCHA action object.
* @return CCaptchaAction the action object
* @since 1.1.7
*/
protected function getCaptchaAction()
{
if(($captcha=Yii::app()->getController()->createAction($this->captchaAction))===null)
{
if(strpos($this->captchaAction,'/')!==false) // contains controller or module
{
if(($ca=Yii::app()->createController($this->captchaAction))!==null)
{
list($controller,$actionID)=$ca;
$captcha=$controller->createAction($actionID);
}
}
if($captcha===null)
throw new CException(Yii::t('yii','CCaptchaValidator.action "{id}" is invalid. Unable to find such an action in the current controller.',
array('{id}'=>$this->captchaAction)));
}
return $captcha;
}
/**
* Returns the JavaScript needed for performing client-side validation.
* @param CModel $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script.
* @see CActiveForm::enableClientValidation
* @since 1.1.7
*/
public function clientValidateAttribute($object,$attribute)
{
$captcha=$this->getCaptchaAction();
$message=$this->message!==null ? $this->message : Yii::t('yii','The verification code is incorrect.');
$message=strtr($message, array(
'{attribute}'=>$object->getAttributeLabel($attribute),
));
$code=$captcha->getVerifyCode(false);
$hash=$captcha->generateValidationHash($this->caseSensitive ? $code : strtolower($code));
$js="
var hash = $('body').data('{$this->captchaAction}.hash');
if (hash == null)
hash = $hash;
else
hash = hash[".($this->caseSensitive ? 0 : 1)."];
for(var i=value.length-1, h=0; i >= 0; --i) h+=value.".($this->caseSensitive ? '' : 'toLowerCase().')."charCodeAt(i);
if(h != hash) {
messages.push(".CJSON::encode($message).");
}
";
if($this->allowEmpty)
{
$js="
if($.trim(value)!='') {
$js
}
";
}
return $js;
}
}