CaptchaValidator.php 3.58 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
w  
Qiang Xue committed
3
 * CaptchaValidator class file.
w  
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
w  
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
w  
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
10 11
namespace yii\validators;

w  
Qiang Xue committed
12
/**
w  
Qiang Xue committed
13
 * CaptchaValidator validates that the attribute value is the same as the verification code displayed in the CAPTCHA.
w  
Qiang Xue committed
14
 *
w  
Qiang Xue committed
15
 * CaptchaValidator should be used together with [[CaptchaAction]].
w  
Qiang Xue committed
16 17
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
18
 * @since 2.0
w  
Qiang Xue committed
19
 */
w  
Qiang Xue committed
20
class CaptchaValidator extends Validator
w  
Qiang Xue committed
21 22 23 24 25 26
{
	/**
	 * @var boolean whether the comparison is case sensitive. Defaults to false.
	 */
	public $caseSensitive = false;
	/**
w  
Qiang Xue committed
27 28 29
	 * @var string the 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 (e.g. 'site/captcha').
w  
Qiang Xue committed
30 31 32 33 34 35 36 37 38 39 40
	 */
	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.
w  
Qiang Xue committed
41
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
42 43
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
44
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
45 46
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
47
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
48
			return;
w  
Qiang Xue committed
49
		}
w  
Qiang Xue committed
50
		$captcha = $this->getCaptchaAction();
w  
Qiang Xue committed
51
		if (!$captcha->validate($value, $this->caseSensitive)) {
Alexander Makarov committed
52
			$message = $this->message !== null ? $this->message : \Yii::t('yii', 'The verification code is incorrect.');
w  
Qiang Xue committed
53 54 55 56 57 58 59 60
			$this->addError($object, $attribute, $message);
		}
	}

	/**
	 * Returns the CAPTCHA action object.
	 * @return CCaptchaAction the action object
	 */
w  
Qiang Xue committed
61
	public function getCaptchaAction()
w  
Qiang Xue committed
62
	{
w  
Qiang Xue committed
63
		if (strpos($this->captchaAction, '/') !== false) {  // contains controller or module
Qiang Xue committed
64
			$ca = \Yii::$application->createController($this->captchaAction);
w  
Qiang Xue committed
65 66 67
			if ($ca !== null) {
				list($controller, $actionID) = $ca;
				$action = $controller->createAction($actionID);
w  
Qiang Xue committed
68
			}
Qiang Xue committed
69
		} else {
Qiang Xue committed
70
			$action = \Yii::$application->getController()->createAction($this->captchaAction);
w  
Qiang Xue committed
71 72 73 74 75 76
		}

		if ($action === null) {
			throw new \yii\base\Exception('Invalid captcha action ID: ' . $this->captchaAction);
		}
		return $action;
w  
Qiang Xue committed
77 78 79 80
	}

	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
81
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
82 83 84 85 86 87
	 * @param string $attribute the name of the attribute to be validated.
	 * @return string the client-side validation script.
	 */
	public function clientValidateAttribute($object, $attribute)
	{
		$captcha = $this->getCaptchaAction();
Alexander Makarov committed
88
		$message = $this->message !== null ? $this->message : \Yii::t('yii', 'The verification code is incorrect.');
w  
Qiang Xue committed
89 90
		$message = strtr($message, array(
			'{attribute}' => $object->getAttributeLabel($attribute),
w  
Qiang Xue committed
91
			'{value}' => $object->$attribute,
w  
Qiang Xue committed
92 93 94 95 96 97 98 99 100 101 102
		));
		$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) {
w  
Qiang Xue committed
103
	messages.push(" . json_encode($message) . ");
w  
Qiang Xue committed
104 105 106
}
";

w  
Qiang Xue committed
107
		if ($this->allowEmpty) {
w  
Qiang Xue committed
108 109 110 111 112 113 114 115 116 117 118
			$js = "
if($.trim(value)!='') {
	$js
}
";
		}

		return $js;
	}
}