CaptchaValidator.php 3.19 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
use Yii;
use yii\base\InvalidConfigException;
Qiang Xue committed
12
use yii\helpers\Html;
Qiang Xue committed
13

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

w  
Qiang Xue committed
33

Qiang Xue committed
34 35 36 37 38 39 40 41 42 43 44
	/**
	 * 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
45 46 47
	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
48
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
49 50
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
51
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
52 53
	{
		$value = $object->$attribute;
Qiang Xue committed
54
		if (!$this->validateValue($value)) {
Qiang Xue committed
55
			$this->addError($object, $attribute, $this->message);
w  
Qiang Xue committed
56 57 58
		}
	}

Qiang Xue committed
59 60 61 62 63 64 65 66
	/**
	 * 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();
67
		return !is_array($value) && $captcha->validate($value, $this->caseSensitive);
Qiang Xue committed
68 69
	}

w  
Qiang Xue committed
70 71
	/**
	 * Returns the CAPTCHA action object.
Alexander Makarov committed
72
	 * @throws InvalidConfigException
Qiang Xue committed
73
	 * @return CaptchaAction the action object
w  
Qiang Xue committed
74
	 */
w  
Qiang Xue committed
75
	public function getCaptchaAction()
w  
Qiang Xue committed
76
	{
Qiang Xue committed
77 78 79 80 81 82 83
		$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
84
			}
w  
Qiang Xue committed
85
		}
Qiang Xue committed
86
		throw new InvalidConfigException('Invalid CAPTCHA action ID: ' . $this->captchaAction);
w  
Qiang Xue committed
87 88 89 90
	}

	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
91
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
92 93 94 95 96 97 98 99
	 * @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();
		$code = $captcha->getVerifyCode(false);
		$hash = $captcha->generateValidationHash($this->caseSensitive ? $code : strtolower($code));
Qiang Xue committed
100 101 102 103 104 105 106 107 108
		$options = array(
			'hash' => $hash,
			'hashKey' => 'yiiCaptcha/' . $this->captchaAction,
			'caseSensitive' => $this->caseSensitive,
			'message' => Html::encode(strtr($this->message, array(
				'{attribute}' => $object->getAttributeLabel($attribute),
				'{value}' => $object->$attribute,
			))),
		);
Qiang Xue committed
109
		if ($this->skipOnEmpty) {
Qiang Xue committed
110
			$options['skipOnEmpty'] = 1;
w  
Qiang Xue committed
111 112
		}

Qiang Xue committed
113
		return 'yii.validation.captcha(value, messages, ' . json_encode($options) . ');';
w  
Qiang Xue committed
114 115 116
	}
}