CaptchaValidator.php 3.52 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
	/**
	 * @var boolean whether to skip this validator if the input is empty.
	 */
27
	public $skipOnEmpty = false;
w  
Qiang Xue committed
28 29 30 31 32
	/**
	 * @var boolean whether the comparison is case sensitive. Defaults to false.
	 */
	public $caseSensitive = false;
	/**
Qiang Xue committed
33
	 * @var string the route of the controller action that renders the CAPTCHA image.
w  
Qiang Xue committed
34
	 */
Qiang Xue committed
35 36
	public $captchaAction = 'site/captcha';

w  
Qiang Xue committed
37

Qiang Xue committed
38 39 40 41 42 43 44
	/**
	 * Initializes the validator.
	 */
	public function init()
	{
		parent::init();
		if ($this->message === null) {
45
			$this->message = Yii::t('yii', 'The verification code is incorrect.');
Qiang Xue committed
46 47 48
		}
	}

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

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

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

	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
95
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
96
	 * @param string $attribute the name of the attribute to be validated.
97 98
	 * @param \yii\base\View $view the view object that is going to be used to render views or view files
	 * containing a model form with this validator applied.
w  
Qiang Xue committed
99 100
	 * @return string the client-side validation script.
	 */
101
	public function clientValidateAttribute($object, $attribute, $view)
w  
Qiang Xue committed
102 103 104 105
	{
		$captcha = $this->getCaptchaAction();
		$code = $captcha->getVerifyCode(false);
		$hash = $captcha->generateValidationHash($this->caseSensitive ? $code : strtolower($code));
Qiang Xue committed
106 107 108 109 110 111 112 113 114
		$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
115
		if ($this->skipOnEmpty) {
Qiang Xue committed
116
			$options['skipOnEmpty'] = 1;
w  
Qiang Xue committed
117 118
		}

119
		$view->registerAssetBundle('yii/validation');
Qiang Xue committed
120
		return 'yii.validation.captcha(value, messages, ' . json_encode($options) . ');';
w  
Qiang Xue committed
121 122
	}
}