-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaptchaAction.php
98 lines (88 loc) · 3.16 KB
/
CaptchaAction.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace daimakuai\captcha;
use Yii;
use yii\helpers\Url;
use yii\web\Response;
class CaptchaAction extends \yii\captcha\CaptchaAction
{
public $fontFile = '@daimakuai/captcha/font/LithosPro-Regular.otf';
public $foreColor = 0x999999;
public $type = 'default'; // numbers & letters
public $offset = -2;
public function run()
{
if($this->type == 'numbers')
{
$this->offset = -10;
}
if (Yii::$app->request->getQueryParam(self::REFRESH_GET_VAR) !== null) {
// AJAX request for regenerating code
$code = $this->getVerifyCode(true);
Yii::$app->response->format = Response::FORMAT_JSON;
return [
'hash1' => $this->generateValidationHash($code),
'hash2' => $this->generateValidationHash(strtolower($code)),
// we add a random 'v' parameter so that FireFox can refresh the image
// when src attribute of image tag is changed
'url' => Url::to([$this->id, 'v' => uniqid()]),
];
} else {
$this->setHttpHeaders();
Yii::$app->response->format = Response::FORMAT_RAW;
return $this->renderImage($this->getVerifyCode());
}
// $view->registerJs('yii.captcha.validation.js');
}
protected function generateVerifyCode()
{
if ($this->minLength > $this->maxLength) {
$this->maxLength = $this->minLength;
}
if ($this->minLength < 4) {
$this->minLength = 4;
}
if ($this->maxLength > 9) {
$this->maxLength = 9;
}
$length = mt_rand($this->minLength, $this->maxLength);
$str = '0123456789bcdfghjklmnpqrstvwxyz';
$code = '';
for ($i = 0; $i < $length; $i++) {
switch ($this->type)
{
case 'numbers':
$code .= $str[mt_rand(0, 9)];
break;
case 'letters':
$code .= $str[mt_rand(10, 30)];
break;
default:
$code .= $str[mt_rand(0, 30)];
}
}
return $code;
}
public function validate($input, $caseSensitive)
{
$input = $this->_changeInvalidNumbers($input);
$code = $this->getVerifyCode();
$valid = $caseSensitive ? ($input === $code) : strcasecmp($input, $code) === 0;
$session = Yii::$app->getSession();
$session->open();
$name = $this->getSessionKey() . 'count';
$session[$name] = $session[$name] + 1;
if ($valid || $session[$name] > $this->testLimit && $this->testLimit > 0) {
$this->getVerifyCode(true);
}
return $valid;
}
private function _changeInvalidNumbers($input)
{
$enNumbers = ['0','1','2','3','4','5','6','7','8','9'];
$faNumbers = ['۰','۱','۲','۳','۴','۵','۶','۷','۸','۹'];
$arNumbers = ['٠','١','٢','٣','٤','٥','٦','٧','٨','٩'];
$input = str_replace($faNumbers,$enNumbers,$input);
$input = str_replace($arNumbers,$enNumbers,$input);
return $input;
}
}