Skip to content

Commit 0f5c5e9

Browse files
committed
change invalid numbers
1 parent f70d7f5 commit 0f5c5e9

File tree

5 files changed

+139
-2
lines changed

5 files changed

+139
-2
lines changed

Captcha.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace developit\captcha;
3+
use Yii;
4+
use developit\captcha\yii2CaptchaAsset;
5+
6+
class Captcha extends \yii\captcha\Captcha
7+
{
8+
public function run()
9+
{
10+
$this->registerClientScript();
11+
parent::run();
12+
}
13+
14+
public function registerClientScript()
15+
{
16+
yii2CaptchaAsset::register(Yii::$app->getView());
17+
parent::registerClientScript();
18+
}
19+
}

CaptchaAction.php

+51
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
<?php
22

33
namespace developit\captcha;
4+
use Yii;
5+
use yii\helpers\Url;
6+
use yii\web\Response;
47

58
class CaptchaAction extends \yii\captcha\CaptchaAction
69
{
710
public $fontFile = '@developit/captcha/font/LithosPro-Regular.otf';
811
public $foreColor = 0x999999;
912
public $type = 'default'; // numbers & letters
1013

14+
public function run()
15+
{
16+
if (Yii::$app->request->getQueryParam(self::REFRESH_GET_VAR) !== null) {
17+
// AJAX request for regenerating code
18+
$code = $this->getVerifyCode(true);
19+
Yii::$app->response->format = Response::FORMAT_JSON;
20+
return [
21+
'hash1' => $this->generateValidationHash($code),
22+
'hash2' => $this->generateValidationHash(strtolower($code)),
23+
// we add a random 'v' parameter so that FireFox can refresh the image
24+
// when src attribute of image tag is changed
25+
'url' => Url::to([$this->id, 'v' => uniqid()]),
26+
];
27+
} else {
28+
$this->setHttpHeaders();
29+
Yii::$app->response->format = Response::FORMAT_RAW;
30+
return $this->renderImage($this->getVerifyCode());
31+
}
32+
// $view->registerJs('yii.captcha.validation.js');
33+
}
34+
1135
protected function generateVerifyCode()
1236
{
1337
if ($this->minLength > $this->maxLength) {
@@ -39,4 +63,31 @@ protected function generateVerifyCode()
3963

4064
return $code;
4165
}
66+
67+
public function validate($input, $caseSensitive)
68+
{
69+
$input = $this->_changeInvalidNumbers($input);
70+
$code = $this->getVerifyCode();
71+
$valid = $caseSensitive ? ($input === $code) : strcasecmp($input, $code) === 0;
72+
$session = Yii::$app->getSession();
73+
$session->open();
74+
$name = $this->getSessionKey() . 'count';
75+
$session[$name] = $session[$name] + 1;
76+
if ($valid || $session[$name] > $this->testLimit && $this->testLimit > 0) {
77+
$this->getVerifyCode(true);
78+
}
79+
80+
return $valid;
81+
}
82+
83+
private function _changeInvalidNumbers($input)
84+
{
85+
$enNumbers = ['0','1','2','3','4','5','6','7','8','9'];
86+
$faNumbers = ['۰','۱','۲','۳','۴','۵','۶','۷','۸','۹'];
87+
$arNumbers = ['٠','١','٢','٣','٤','٥','٦','٧','٨','٩'];
88+
$input = str_replace($faNumbers,$enNumbers,$input);
89+
$input = str_replace($arNumbers,$enNumbers,$input);
90+
91+
return $input;
92+
}
4293
}

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ The preferred way to install this extension is through [composer](http://getcomp
1111
Either run
1212

1313
```
14-
php composer.phar require --prefer-dist developit/yii2-captcha "dev-master"
14+
php composer.phar require --prefer-dist developit/yii2-captcha "*"
1515
```
1616

1717
or add
1818

1919
```
20-
"developit/yii2-captcha": "dev-master"
20+
"developit/yii2-captcha": "*"
2121
```
2222

2323
to the require section of your `composer.json` file.
@@ -48,6 +48,7 @@ Once the extension is installed, simply modify your controler, add or change met
4848

4949
In view
5050
```php
51+
use developit\captcha\Captcha;
5152
<?=
5253
$form->field($model, 'verifyCode')->widget(Captcha::className())
5354
?>

assets/yii.captcha.validation.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var validation = yii.validation;
2+
3+
validation.changeInvalidNumbers = function(input){
4+
var enNumbers = {
5+
'۰': '0',
6+
'۱': '1',
7+
'۲': '2',
8+
'۳': '3',
9+
'۴': '4',
10+
'۵': '5',
11+
'۶': '6',
12+
'۷': '7',
13+
'۸': '8',
14+
'۹': '9',
15+
'٠': '0',
16+
'١': '1',
17+
'٢': '2',
18+
'٣': '3',
19+
'٤': '4',
20+
'٥': '5',
21+
'٦': '6',
22+
'٧': '7',
23+
'٨': '8',
24+
'٩': '9',
25+
}
26+
input = input.replace(/[۰-۹٠-٩]/g,function (i) {
27+
return enNumbers[i];
28+
});
29+
return input;
30+
};
31+
validation.captcha = function (value, messages, options) {
32+
value = validation.changeInvalidNumbers(value);
33+
if (options.skipOnEmpty && pub.isEmpty(value)) {
34+
return;
35+
}
36+
37+
// CAPTCHA may be updated via AJAX and the updated hash is stored in body data
38+
var hash = $('body').data(options.hashKey);
39+
if (hash == null) {
40+
hash = options.hash;
41+
} else {
42+
hash = hash[options.caseSensitive ? 0 : 1];
43+
}
44+
var v = options.caseSensitive ? value : value.toLowerCase();
45+
for (var i = v.length - 1, h = 0; i >= 0; --i) {
46+
h += v.charCodeAt(i);
47+
}
48+
if (h != hash) {
49+
validation.addMessage(messages, options.message, value);
50+
}
51+
};
52+

yii2CaptchaAsset.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
namespace developit\captcha;
3+
4+
use yii\web\AssetBundle;
5+
use Yii;
6+
7+
class yii2CaptchaAsset extends AssetBundle
8+
{
9+
public $sourcePath = '@vendor/developit/yii2-captcha/assets';
10+
public $js = ['yii.captcha.validation.js'];
11+
public $depends = [
12+
'yii\web\YiiAsset',
13+
];
14+
}

0 commit comments

Comments
 (0)