-
Notifications
You must be signed in to change notification settings - Fork 1
/
CaptchaExtendedValidator.php
84 lines (67 loc) · 2.07 KB
/
CaptchaExtendedValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* Yii2 Captcha Extended - client validator
*
* Project:
* https://github.com/lubosdz/captcha-extended
*/
namespace lubosdz\captchaExtended;
use Yii;
use yii\validators\ValidationAsset;
class CaptchaExtendedValidator extends \yii\captcha\CaptchaValidator
{
public function clientValidateAttribute($object, $attribute, $view){
/** @var \yii\web\View */
$view;
$captcha = $this->createCaptchaAction();
$result = $captcha->getVerifyResult();
if(!$this->caseSensitive){
$result = mb_convert_case($result, MB_CASE_LOWER, 'utf-8');
}
$hash = $captcha->generateValidationHash($result);
$options = [
'hash' => $hash,
'hashKey' => 'yiiCaptcha/' . $this->captchaAction,
'caseSensitive' => $this->caseSensitive,
'message' => Yii::$app->getI18n()->format($this->message, [
'attribute' => $object->getAttributeLabel($attribute),
], Yii::$app->language),
];
if ($this->skipOnEmpty) {
$options['skipOnEmpty'] = 1;
}
ValidationAsset::register($view);
// override default captcha validator in assets "yii.validation.js"
$js = <<<JS
yii.validation = yii.validation || {};
yii.validation = $.extend(yii.validation, {
captcha : function (value, messages, options) {
if (options.skipOnEmpty && this.isEmpty(value)) {
return;
}
if(options && options.hashKey != undefined){
options.hashKey = options.hashKey.replace('//', '/'); // fix double slash in URLs
}
value = value.replace(/\s+/g, '');
// CAPTCHA may be updated via AJAX and the updated hash is stored in body data
var hash = $('body').data(options.hashKey);
if (hash == null) {
hash = options.hash;
} else {
hash = hash[options.caseSensitive ? 0 : 1];
}
var v = options.caseSensitive ? value : value.toLowerCase();
v = encodeURIComponent(v);
for (var i = v.length - 1, h = 0; i >= 0; --i) {
h += v.charCodeAt(i);
}
if (h != hash) {
this.addMessage(messages, options.message, value);
}
}
});
JS;
$view->registerJs($js);
return 'yii.validation.captcha(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
}
}