CaptchaValidator.php 2.99 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
 *
Qiang Xue committed
21 22 23 24
 * Note that once CAPTCHA validation succeeds, a new CAPTCHA will be generated automatically. As a result,
 * CAPTCHA validation should not be used in AJAX validation mode because it may fail the validation
 * even if a user enters the same code as shown in the CAPTCHA image which is actually different from the latest CAPTCHA code.
 *
w  
Qiang Xue committed
25
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
26
 * @since 2.0
w  
Qiang Xue committed
27
 */
w  
Qiang Xue committed
28
class CaptchaValidator extends Validator
w  
Qiang Xue committed
29
{
30 31 32
	/**
	 * @var boolean whether to skip this validator if the input is empty.
	 */
33
	public $skipOnEmpty = false;
w  
Qiang Xue committed
34 35 36 37 38
	/**
	 * @var boolean whether the comparison is case sensitive. Defaults to false.
	 */
	public $caseSensitive = false;
	/**
Qiang Xue committed
39
	 * @var string the route of the controller action that renders the CAPTCHA image.
w  
Qiang Xue committed
40
	 */
Qiang Xue committed
41 42
	public $captchaAction = 'site/captcha';

w  
Qiang Xue committed
43

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

w  
Qiang Xue committed
55
	/**
Qiang Xue committed
56
	 * @inheritdoc
w  
Qiang Xue committed
57
	 */
Qiang Xue committed
58
	protected function validateValue($value)
Qiang Xue committed
59
	{
60
		$captcha = $this->createCaptchaAction();
Qiang Xue committed
61 62
		$valid = !is_array($value) && $captcha->validate($value, $this->caseSensitive);
		return $valid ? null : [$this->message, []];
Qiang Xue committed
63 64
	}

w  
Qiang Xue committed
65
	/**
66
	 * Creates the CAPTCHA action object from the route specified by [[captchaAction]].
Qiang Xue committed
67
	 * @return \yii\captcha\CaptchaAction the action object
68
	 * @throws InvalidConfigException
w  
Qiang Xue committed
69
	 */
70
	public function createCaptchaAction()
w  
Qiang Xue committed
71
	{
Qiang Xue committed
72 73 74 75 76 77 78
		$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
79
			}
w  
Qiang Xue committed
80
		}
Qiang Xue committed
81
		throw new InvalidConfigException('Invalid CAPTCHA action ID: ' . $this->captchaAction);
w  
Qiang Xue committed
82 83 84
	}

	/**
Qiang Xue committed
85
	 * @inheritdoc
w  
Qiang Xue committed
86
	 */
87
	public function clientValidateAttribute($object, $attribute, $view)
w  
Qiang Xue committed
88
	{
89
		$captcha = $this->createCaptchaAction();
w  
Qiang Xue committed
90 91
		$code = $captcha->getVerifyCode(false);
		$hash = $captcha->generateValidationHash($this->caseSensitive ? $code : strtolower($code));
Alexander Makarov committed
92
		$options = [
Qiang Xue committed
93 94 95
			'hash' => $hash,
			'hashKey' => 'yiiCaptcha/' . $this->captchaAction,
			'caseSensitive' => $this->caseSensitive,
96
			'message' => strtr($this->message, [
Qiang Xue committed
97
				'{attribute}' => $object->getAttributeLabel($attribute),
98
			]),
Alexander Makarov committed
99
		];
Qiang Xue committed
100
		if ($this->skipOnEmpty) {
Qiang Xue committed
101
			$options['skipOnEmpty'] = 1;
w  
Qiang Xue committed
102 103
		}

104
		ValidationAsset::register($view);
Qiang Xue committed
105
		return 'yii.validation.captcha(value, messages, ' . json_encode($options) . ');';
w  
Qiang Xue committed
106 107
	}
}