CaptchaValidator.php 3.51 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
8 9
namespace yii\validators;

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

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

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

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

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

		return $js;
	}
}