CaptchaValidator.php 3.29 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;

Qiang Xue committed
10 11 12
use Yii;
use yii\base\InvalidConfigException;

w  
Qiang Xue committed
13
/**
w  
Qiang Xue committed
14
 * CaptchaValidator validates that the attribute value is the same as the verification code displayed in the CAPTCHA.
w  
Qiang Xue committed
15
 *
w  
Qiang Xue committed
16
 * CaptchaValidator should be used together with [[CaptchaAction]].
w  
Qiang Xue committed
17 18
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
19
 * @since 2.0
w  
Qiang Xue committed
20
 */
w  
Qiang Xue committed
21
class CaptchaValidator extends Validator
w  
Qiang Xue committed
22 23 24 25 26 27
{
	/**
	 * @var boolean whether the comparison is case sensitive. Defaults to false.
	 */
	public $caseSensitive = false;
	/**
Qiang Xue committed
28
	 * @var string the route of the controller action that renders the CAPTCHA image.
w  
Qiang Xue committed
29
	 */
Qiang Xue committed
30 31
	public $captchaAction = 'site/captcha';

w  
Qiang Xue committed
32

Qiang Xue committed
33 34 35 36 37 38 39 40 41 42 43
	/**
	 * Initializes the validator.
	 */
	public function init()
	{
		parent::init();
		if ($this->message === null) {
			$this->message = Yii::t('yii|The verification code is incorrect.');
		}
	}

w  
Qiang Xue committed
44 45 46
	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
47
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
48 49
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
50
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
51 52
	{
		$value = $object->$attribute;
Qiang Xue committed
53
		if (!$this->validateValue($value)) {
Qiang Xue committed
54
			$this->addError($object, $attribute, $this->message);
w  
Qiang Xue committed
55 56 57
		}
	}

Qiang Xue committed
58 59 60 61 62 63 64 65
	/**
	 * Validates the given value.
	 * @param mixed $value the value to be validated.
	 * @return boolean whether the value is valid.
	 */
	public function validateValue($value)
	{
		$captcha = $this->getCaptchaAction();
66
		return !is_array($value) && $captcha->validate($value, $this->caseSensitive);
Qiang Xue committed
67 68
	}

w  
Qiang Xue committed
69 70
	/**
	 * Returns the CAPTCHA action object.
Alexander Makarov committed
71
	 * @throws InvalidConfigException
Qiang Xue committed
72
	 * @return CaptchaAction the action object
w  
Qiang Xue committed
73
	 */
w  
Qiang Xue committed
74
	public function getCaptchaAction()
w  
Qiang Xue committed
75
	{
Qiang Xue committed
76 77 78 79 80 81 82
		$ca = Yii::$app->createController($this->captchaAction);
		if ($ca !== false) {
			/** @var \yii\base\Controller $controller */
			list($controller, $actionID) = $ca;
			$action = $controller->createAction($actionID);
			if ($action !== null) {
				return $action;
w  
Qiang Xue committed
83
			}
w  
Qiang Xue committed
84
		}
Qiang Xue committed
85
		throw new InvalidConfigException('Invalid CAPTCHA action ID: ' . $this->captchaAction);
w  
Qiang Xue committed
86 87 88 89
	}

	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
90
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
91 92 93 94 95 96
	 * @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();
Qiang Xue committed
97
		$message = strtr($this->message, array(
w  
Qiang Xue committed
98
			'{attribute}' => $object->getAttributeLabel($attribute),
w  
Qiang Xue committed
99
			'{value}' => $object->$attribute,
w  
Qiang Xue committed
100 101 102 103 104 105 106 107 108 109 110
		));
		$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
111
	messages.push(" . json_encode($message) . ");
w  
Qiang Xue committed
112 113 114
}
";

Qiang Xue committed
115
		if ($this->skipOnEmpty) {
w  
Qiang Xue committed
116 117 118 119 120 121 122 123 124 125 126
			$js = "
if($.trim(value)!='') {
	$js
}
";
		}

		return $js;
	}
}