CaptchaValidator.php 3.57 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
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
22
 * @since 2.0
w  
Qiang Xue committed
23
 */
w  
Qiang Xue committed
24
class CaptchaValidator extends Validator
w  
Qiang Xue committed
25
{
26 27 28
	/**
	 * @var boolean whether to skip this validator if the input is empty.
	 */
29
	public $skipOnEmpty = false;
w  
Qiang Xue committed
30 31 32 33 34
	/**
	 * @var boolean whether the comparison is case sensitive. Defaults to false.
	 */
	public $caseSensitive = false;
	/**
Qiang Xue committed
35
	 * @var string the route of the controller action that renders the CAPTCHA image.
w  
Qiang Xue committed
36
	 */
Qiang Xue committed
37 38
	public $captchaAction = 'site/captcha';

w  
Qiang Xue committed
39

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

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

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

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

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

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