-
Notifications
You must be signed in to change notification settings - Fork 2
/
CaptchaAction.php
343 lines (306 loc) · 11.8 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
/**
* @link https://github.com/softark/yii2-mb-captcha
* @copyright Copyright (c) 2013 - 2015 Nobuo Kihara
* @license https://github.com/softark/yii2-mb-captcha/blob/master/LICENSE
* @author Nobuo Kihara <[email protected]>
*/
namespace softark\mbcaptcha;
use Yii;
use yii\web\Response;
use yii\helpers\Url;
/**
* softark\mbcaptcha\CaptchaAction is an extension to [[yii\captcha\CaptchaAction]].
*
* While [[yii\captcha\CaptchaAction]] renders a CAPTCHA image only with English alphabets,
* softark\mbcaptcha\CaptchaAction can render it with multibyte characters (Japanese Hirakana
* by default, but you may use any multibyte characters by providing the appropriate font).
*
* softark\mbcaptcha\CaptchaAction must be used together with softark\mbcaptcha\Captcha
* and [[yii\validators\CaptchaValidator]] to provide its feature.
*/
class CaptchaAction extends \yii\captcha\CaptchaAction
{
/**
* The name of the GET parameter indicating whether the CAPTCHA type (multibyte character/alphabet) should be toggled.
*/
const TOGGLE_GET_VAR = 'toggle';
/**
* @var integer the minimum length for randomly generated word. Defaults to 5.
*/
public $mbMinLength = 5;
/**
* @var integer the maximum length for randomly generated word. Defaults to 5.
*/
public $mbMaxLength = 5;
/**
* @var integer the offset between characters. Defaults to 2. You can adjust this property
* in order to decrease or increase the readability of the non alphabetical captcha.
**/
public $mbOffset = 2;
/**
* @var boolean whether to use multibyte characters. Defaults to true.
*/
public $useMbChars = true;
/**
* @var string multibyte font file. Defaults to seto-mini.ttf, a subset of
* setofont.ttf (http://setofont.sourceforge.jp/) created and shared
* by Nozomi Seto.. Special thanks to Nozomi Seto for the wonderful font.
* Note that seto-mini.ttf supports only ASCII, Hirakana and Katakana.
*/
public $mbFontFile;
/**
* @var boolean whether to render the captcha image with a fixed angle. Defaults to false.
* You may want to set this to true if you have trouble rendering your font.
*/
public $fixedAngle = false;
/**
* @var string The string used for generating the random string of captcha.
* Defaults to a series of Japanese Hirakana characters. You may want to set your own.
*/
public $seeds;
/**
* Runs the action.
*/
public function run()
{
// Character type ... defaults to non alphabetical characters
$session = Yii::$app->session;
$session->open();
$name = $this->getSessionKey();
if ($session[$name . 'type'] !== null && $session[$name . 'type'] === 'abc') {
$this->useMbChars = false;
}
$refresh = Yii::$app->request->getQueryParam(self::REFRESH_GET_VAR);
$toggle = Yii::$app->request->getQueryParam(self::TOGGLE_GET_VAR);
if ($refresh !== null || $toggle !== null) {
if ($toggle !== null) {
$this->useMbChars = !$this->useMbChars;
}
// 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());
}
}
/**
* Generates a hash code that can be used for client side validation.
* @param string $code the CAPTCHA code
* @return int a hash code generated from the CAPTCHA code
*/
public function generateValidationHash($code)
{
$hash = 0;
for ($i = mb_strlen($code) - 1; $i >= 0; --$i) {
$char = mb_substr($code, $i, 1, 'UTF-8');
$char = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
$hash += (int)hexdec(bin2hex($char)) << $i;
}
return $hash;
}
/**
* Gets the verification code.
* @param boolean $regenerate whether the verification code should be regenerated.
* @return string the verification code.
*/
public function getVerifyCode($regenerate = false)
{
if ($this->fixedVerifyCode !== null) {
return $this->fixedVerifyCode;
}
$session = Yii::$app->getSession();
$session->open();
$name = $this->getSessionKey();
if ($session[$name] === null || $regenerate) {
$session[$name] = $this->generateVerifyCode();
$session[$name . 'type'] = $this->useMbChars ? 'mb' : 'abc';
$session[$name . 'count'] = 1;
}
return $session[$name];
}
/**
* Generates a new verification code.
* @return string the generated verification code
*/
protected function generateVerifyCode()
{
// alphabets ?
if (!$this->useMbChars) {
return parent::generateVerifyCode();
}
$seeds = $this->getCaptchaSeeds();
$len = mb_strlen($seeds, 'UTF-8');
for ($code = '', $i = 0; $i < $this->getCaptchaLength(); ++$i) {
$code .= mb_substr($seeds, mt_rand(0, $len - 1), 1, 'UTF-8');
}
return $code;
}
/**
* @return int length of the CAPTCHA string
*/
private function getCaptchaLength()
{
if ($this->mbMinLength < 3) {
$this->mbMinLength = 3;
}
if ($this->mbMaxLength > 20) {
$this->mbMaxLength = 20;
}
if ($this->mbMinLength >= $this->mbMaxLength) {
return $this->mbMaxLength;
} else {
return mt_rand($this->mbMinLength, $this->mbMaxLength);
}
}
/**
* @return string the seeds of the CAPTCHA string
*/
private function getCaptchaSeeds()
{
if ($this->seeds !== null) {
return $this->seeds;
} else {
return 'あいうえおかきくけこがぎぐげごさしすせそざじずぜぞたちつてとだぢづでどなにぬねのはひふへほはひふへほはひふへほばびぶべぼぱぴぷぺぽまみむめもやゆよらりるれろわをん';
}
}
/**
* Renders the CAPTCHA image.
* @param string $code the verification code
* @return string image contents
*/
protected function renderImage($code)
{
// alphabets ?
if (!$this->useMbChars) {
return parent::renderImage($code);
}
// font defaults to seto-mini.ttf
if ($this->mbFontFile === null) {
$this->mbFontFile = __DIR__ . DIRECTORY_SEPARATOR . 'seto-mini.ttf';
}
$encoding = 'UTF-8';
if (Captcha::checkRequirements() === 'gd') {
// check if conversion to Shift_JIS is needed
$gd_info = gd_info();
if ($gd_info['JIS-mapped Japanese Font Support']) {
$code = mb_convert_encoding($code, 'SJIS', 'UTF-8');
$encoding = 'SJIS';
}
return $this->mbRenderImageByGD($code, $encoding);
} else {
return $this->mbRenderImageByImagick($code, $encoding);
}
}
/**
* Renders the CAPTCHA image based on the code using GD library.
* @param string $code the verification code
* @param string $encoding character encoding
* @return string image contents in PNG format.
*/
protected function mbRenderImageByGD($code, $encoding)
{
$image = imagecreatetruecolor($this->width, $this->height);
$backColor = imagecolorallocate($image,
(int)($this->backColor % 0x1000000 / 0x10000),
(int)($this->backColor % 0x10000 / 0x100),
$this->backColor % 0x100);
imagefilledrectangle($image, 0, 0, $this->width, $this->height, $backColor);
imagecolordeallocate($image, $backColor);
if ($this->transparent) {
imagecolortransparent($image, $backColor);
}
$foreColor = imagecolorallocate($image,
(int)($this->foreColor % 0x1000000 / 0x10000),
(int)($this->foreColor % 0x10000 / 0x100),
$this->foreColor % 0x100);
$length = mb_strlen($code, $encoding);
$box = imagettfbbox(30, 0, $this->mbFontFile, $code);
$w = $box[4] - $box[0] + $this->mbOffset * ($length - 1);
$h = $box[1] - $box[5];
if ($h <= 0) {
$h = $w / $length;
}
$scale = min(($this->width - $this->padding * 2) / $w, ($this->height - $this->padding * 2) / $h);
$x = 8;
// font size and angle
$fontSize = (int)(30 * $scale * 0.90);
$angle = 0;
// base line
$yBottom = $this->height - $this->padding * 4;
$yTop = (int)($h * $scale * 0.95) + $this->padding * 4;
if ($yTop > $yBottom) {
$yTop = $yBottom;
}
for ($i = 0; $i < $length; ++$i) {
$letter = mb_substr($code, $i, 1, $encoding);
$y = mt_rand($yTop, $yBottom);
if (!$this->fixedAngle) {
$angle = mt_rand(-15, 15);
}
$box = imagettftext($image, $fontSize, $angle, $x, $y, $foreColor, $this->mbFontFile, $letter);
$x = $box[2] + $this->mbOffset;
}
imagecolordeallocate($image, $foreColor);
ob_start();
imagepng($image);
imagedestroy($image);
return ob_get_clean();
}
/**
* Renders the CAPTCHA image based on the code using ImageMagick library.
* @param string $code the verification code
* @param string $encoding character encoding
* @return string image contents in PNG format.
*/
protected function mbRenderImageByImagick($code, $encoding)
{
$backColor = $this->transparent ? new \ImagickPixel('transparent') : new \ImagickPixel(sprintf('#%06x', $this->backColor));
$foreColor = new \ImagickPixel(sprintf('#%06x', $this->foreColor));
$image = new \Imagick();
$image->newImage($this->width, $this->height, $backColor);
$draw = new \ImagickDraw();
$draw->setFont($this->mbFontFile);
$draw->setFontSize(30);
$fontMetrics = $image->queryFontMetrics($draw, $code);
$length = mb_strlen($code, $encoding);
$w = (int)($fontMetrics['textWidth']) + $this->mbOffset * ($length - 1);
$h = (int)($fontMetrics['textHeight']);
$scale = min(($this->width - $this->padding * 2) / $w, ($this->height - $this->padding * 2) / $h);
$x = 8;
// font size and angle
$fontSize = (int)(30 * $scale * 0.90);
$angle = 0;
// base line
$yBottom = $this->height - $this->padding * 4;
$yTop = (int)($h * $scale * 0.95) + $this->padding * 4;
if ($yTop > $yBottom) {
$yTop = $yBottom;
}
for ($i = 0; $i < $length; ++$i) {
$letter = mb_substr($code, $i, 1, $encoding);
$y = mt_rand($yTop, $yBottom);
if (!$this->fixedAngle) {
$angle = mt_rand(-15, 15);
}
$draw = new \ImagickDraw();
$draw->setFont($this->mbFontFile);
$draw->setFontSize($fontSize);
$draw->setFillColor($foreColor);
$image->annotateImage($draw, $x, $y, $angle, $letter);
$fontMetrics = $image->queryFontMetrics($draw, $letter);
$x += (int)($fontMetrics['textWidth']) + $this->mbOffset;
}
$image->setImageFormat('png');
return $image->getImageBlob();
}
}