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