CaptchaValidator.php 3.67 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/
 */

Qiang Xue committed
8
namespace yii\captcha;
w  
Qiang Xue committed
9

Qiang Xue committed
10 11
use Yii;
use yii\base\InvalidConfigException;
Qiang Xue committed
12
use yii\helpers\Html;
Qiang Xue committed
13 14
use yii\validators\ValidationAsset;
use yii\validators\Validator;
Qiang Xue committed
15

w  
Qiang Xue committed
16
/**
w  
Qiang Xue committed
17
 * CaptchaValidator validates that the attribute value is the same as the verification code displayed in the CAPTCHA.
w  
Qiang Xue committed
18
 *
w  
Qiang Xue committed
19
 * CaptchaValidator should be used together with [[CaptchaAction]].
w  
Qiang Xue committed
20
 *
21
 * @property \yii\captcha\CaptchaAction $captchaAction The action object. This property is read-only.
22
 *
w  
Qiang Xue committed
23
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
24
 * @since 2.0
w  
Qiang Xue committed
25
 */
w  
Qiang Xue committed
26
class CaptchaValidator extends Validator
w  
Qiang Xue committed
27
{
28 29 30
	/**
	 * @var boolean whether to skip this validator if the input is empty.
	 */
31
	public $skipOnEmpty = false;
w  
Qiang Xue committed
32 33 34 35 36
	/**
	 * @var boolean whether the comparison is case sensitive. Defaults to false.
	 */
	public $caseSensitive = false;
	/**
Qiang Xue committed
37
	 * @var string the route of the controller action that renders the CAPTCHA image.
w  
Qiang Xue committed
38
	 */
Qiang Xue committed
39 40
	public $captchaAction = 'site/captcha';

w  
Qiang Xue committed
41

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

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

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

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

	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
99
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
100
	 * @param string $attribute the name of the attribute to be validated.
101 102
	 * @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
103 104
	 * @return string the client-side validation script.
	 */
105
	public function clientValidateAttribute($object, $attribute, $view)
w  
Qiang Xue committed
106 107 108 109
	{
		$captcha = $this->getCaptchaAction();
		$code = $captcha->getVerifyCode(false);
		$hash = $captcha->generateValidationHash($this->caseSensitive ? $code : strtolower($code));
Qiang Xue committed
110 111 112 113 114 115 116 117 118
		$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
119
		if ($this->skipOnEmpty) {
Qiang Xue committed
120
			$options['skipOnEmpty'] = 1;
w  
Qiang Xue committed
121 122
		}

123
		ValidationAsset::register($view);
Qiang Xue committed
124
		return 'yii.validation.captcha(value, messages, ' . json_encode($options) . ');';
w  
Qiang Xue committed
125 126
	}
}