1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\captcha; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
use yii\validators\ValidationAsset; |
13
|
|
|
use yii\validators\Validator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* CaptchaValidator validates that the attribute value is the same as the verification code displayed in the CAPTCHA. |
17
|
|
|
* |
18
|
|
|
* CaptchaValidator should be used together with [[CaptchaAction]]. |
19
|
|
|
* |
20
|
|
|
* Note that once CAPTCHA validation succeeds, a new CAPTCHA will be generated automatically. As a result, |
21
|
|
|
* CAPTCHA validation should not be used in AJAX validation mode because it may fail the validation |
22
|
|
|
* even if a user enters the same code as shown in the CAPTCHA image which is actually different from the latest CAPTCHA code. |
23
|
|
|
* |
24
|
|
|
* @author Qiang Xue <[email protected]> |
25
|
|
|
* @since 2.0 |
26
|
|
|
*/ |
27
|
|
|
class CaptchaValidator extends Validator |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var bool whether to skip this validator if the input is empty. |
31
|
|
|
*/ |
32
|
|
|
public $skipOnEmpty = false; |
33
|
|
|
/** |
34
|
|
|
* @var bool whether the comparison is case sensitive. Defaults to false. |
35
|
|
|
*/ |
36
|
|
|
public $caseSensitive = false; |
37
|
|
|
/** |
38
|
|
|
* @var string the route of the controller action that renders the CAPTCHA image. |
39
|
|
|
*/ |
40
|
|
|
public $captchaAction = 'site/captcha'; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
2 |
|
public function init() |
47
|
|
|
{ |
48
|
2 |
|
parent::init(); |
49
|
2 |
|
if ($this->message === null) { |
50
|
2 |
|
$this->message = Yii::t('yii', 'The verification code is incorrect.'); |
51
|
2 |
|
} |
52
|
2 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
|
|
protected function validateValue($value) |
58
|
|
|
{ |
59
|
|
|
$captcha = $this->createCaptchaAction(); |
60
|
|
|
$valid = !is_array($value) && $captcha->validate($value, $this->caseSensitive); |
61
|
|
|
|
62
|
|
|
return $valid ? null : [$this->message, []]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Creates the CAPTCHA action object from the route specified by [[captchaAction]]. |
67
|
|
|
* @return \yii\captcha\CaptchaAction the action object |
68
|
|
|
* @throws InvalidConfigException |
69
|
|
|
*/ |
70
|
|
|
public function createCaptchaAction() |
71
|
|
|
{ |
72
|
|
|
$ca = Yii::$app->createController($this->captchaAction); |
73
|
|
|
if ($ca !== false) { |
74
|
|
|
/* @var $controller \yii\base\Controller */ |
75
|
|
|
list($controller, $actionID) = $ca; |
76
|
|
|
$action = $controller->createAction($actionID); |
77
|
|
|
if ($action !== null) { |
78
|
|
|
return $action; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
throw new InvalidConfigException('Invalid CAPTCHA action ID: ' . $this->captchaAction); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritdoc |
86
|
|
|
*/ |
87
|
|
|
public function clientValidateAttribute($model, $attribute, $view) |
88
|
|
|
{ |
89
|
|
|
ValidationAsset::register($view); |
90
|
|
|
$options = $this->getClientOptions($model, $attribute); |
91
|
|
|
|
92
|
|
|
return 'yii.validation.captcha(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritdoc |
97
|
|
|
*/ |
98
|
|
|
public function getClientOptions($model, $attribute) |
99
|
|
|
{ |
100
|
|
|
$captcha = $this->createCaptchaAction(); |
101
|
|
|
$code = $captcha->getVerifyCode(false); |
102
|
|
|
$hash = $captcha->generateValidationHash($this->caseSensitive ? $code : strtolower($code)); |
103
|
|
|
$options = [ |
104
|
|
|
'hash' => $hash, |
105
|
|
|
'hashKey' => 'yiiCaptcha/' . $captcha->getUniqueId(), |
106
|
|
|
'caseSensitive' => $this->caseSensitive, |
107
|
|
|
'message' => Yii::$app->getI18n()->format($this->message, [ |
108
|
|
|
'attribute' => $model->getAttributeLabel($attribute), |
109
|
|
|
], Yii::$app->language), |
110
|
|
|
]; |
111
|
|
|
if ($this->skipOnEmpty) { |
112
|
|
|
$options['skipOnEmpty'] = 1; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $options; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|