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

w  
Qiang Xue committed
34

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

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

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

	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
92
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
93 94 95 96 97 98 99 100
	 * @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
101 102 103 104 105 106 107 108 109
		$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
110
		if ($this->skipOnEmpty) {
Qiang Xue committed
111
			$options['skipOnEmpty'] = 1;
w  
Qiang Xue committed
112 113
		}

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