Skip to content

Commit 77ee353

Browse files
committed
Code and tests revision.
1 parent 8a1de32 commit 77ee353

8 files changed

+179
-204
lines changed

Diff for: .travis.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
language: php
22

33
php:
4-
- 5.5
54
- 5.6
65
- 7.0
7-
- hhvm
6+
- 7.1
7+
- 7.2
88

99
before_script:
1010
- travis_retry composer self-update
1111
- travis_retry composer install --prefer-source --no-interaction
1212

13-
script: make test
13+
script: make test

Diff for: config/captcha.php

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

33
return [
4-
5-
'characters' => ['2','3','4','6','7','8','9','a','b','c','d','e','f','g','h','j','m','n','p','q','r','t','u','x','y','z','A','B','C','D','E','F','G','H','J','M','N','P','Q','R','T','U','X','Y','Z'],
6-
7-
'default' => [
8-
'length' => 9,
9-
'width' => 120,
10-
'height' => 36,
11-
'quality' => 90,
12-
'math' => false,
4+
'characters' => ['2', '3', '4', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'm', 'n', 'p', 'q', 'r', 't', 'u', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'M', 'N', 'P', 'Q', 'R', 'T', 'U', 'X', 'Y', 'Z'],
5+
'default' => [
6+
'length' => 9,
7+
'width' => 120,
8+
'height' => 36,
9+
'quality' => 90,
10+
'math' => false,
1311
],
14-
'math' => [
15-
'length' => 9,
16-
'width' => 120,
17-
'height' => 36,
18-
'quality' => 90,
19-
'math' => true,
12+
'math' => [
13+
'length' => 9,
14+
'width' => 120,
15+
'height' => 36,
16+
'quality' => 90,
17+
'math' => true,
2018
],
2119

22-
'flat' => [
23-
'length' => 6,
24-
'width' => 160,
25-
'height' => 46,
26-
'quality' => 90,
27-
'lines' => 6,
28-
'bgImage' => false,
29-
'bgColor' => '#ecf2f4',
30-
'fontColors'=> ['#2c3e50', '#c0392b', '#16a085', '#c0392b', '#8e44ad', '#303f9f', '#f57c00', '#795548'],
31-
'contrast' => -5,
20+
'flat' => [
21+
'length' => 6,
22+
'width' => 160,
23+
'height' => 46,
24+
'quality' => 90,
25+
'lines' => 6,
26+
'bgImage' => false,
27+
'bgColor' => '#ecf2f4',
28+
'fontColors' => ['#2c3e50', '#c0392b', '#16a085', '#c0392b', '#8e44ad', '#303f9f', '#f57c00', '#795548'],
29+
'contrast' => -5,
3230
],
33-
'mini' => [
34-
'length' => 3,
35-
'width' => 60,
36-
'height' => 32,
31+
'mini' => [
32+
'length' => 3,
33+
'width' => 60,
34+
'height' => 32,
3735
],
38-
'inverse' => [
39-
'length' => 5,
40-
'width' => 120,
41-
'height' => 36,
42-
'quality' => 90,
36+
'inverse' => [
37+
'length' => 5,
38+
'width' => 120,
39+
'height' => 36,
40+
'quality' => 90,
4341
'sensitive' => true,
44-
'angle' => 12,
45-
'sharpen' => 10,
46-
'blur' => 2,
47-
'invert' => true,
48-
'contrast' => -5,
42+
'angle' => 12,
43+
'sharpen' => 10,
44+
'blur' => 2,
45+
'invert' => true,
46+
'contrast' => -5,
4947
]
50-
5148
];

Diff for: src/Captcha.php

+67-83
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
*/
3131
class Captcha
3232
{
33-
3433
/**
3534
* @var Filesystem
3635
*/
@@ -198,7 +197,7 @@ public function __construct(
198197
$this->session = $session;
199198
$this->hasher = $hasher;
200199
$this->str = $str;
201-
$this->characters = config('captcha.characters',['1','2','3','4','6','7','8','9']);
200+
$this->characters = config('captcha.characters', ['1', '2', '3', '4', '6', '7', '8', '9']);
202201
}
203202

204203
/**
@@ -207,10 +206,8 @@ public function __construct(
207206
*/
208207
protected function configure($config)
209208
{
210-
if ($this->config->has('captcha.' . $config))
211-
{
212-
foreach($this->config->get('captcha.' . $config) as $key => $val)
213-
{
209+
if ($this->config->has('captcha.' . $config)) {
210+
foreach ($this->config->get('captcha.' . $config) as $key => $val) {
214211
$this->{$key} = $val;
215212
}
216213
}
@@ -228,8 +225,8 @@ public function create($config = 'default', $api = false)
228225
$this->backgrounds = $this->files->files(__DIR__ . '/../assets/backgrounds');
229226
$this->fonts = $this->files->files(__DIR__ . '/../assets/fonts');
230227

231-
if (app()->version() >= 5.5){
232-
$this->fonts = array_map(function($file) {
228+
if (app()->version() >= 5.5) {
229+
$this->fonts = array_map(function ($file) {
233230
return $file->getPathName();
234231
}, $this->fonts);
235232
}
@@ -247,45 +244,38 @@ public function create($config = 'default', $api = false)
247244
$this->bgColor
248245
);
249246

250-
if ($this->bgImage)
251-
{
247+
if ($this->bgImage) {
252248
$this->image = $this->imageManager->make($this->background())->resize(
253249
$this->width,
254250
$this->height
255251
);
256252
$this->canvas->insert($this->image);
257-
}
258-
else
259-
{
253+
} else {
260254
$this->image = $this->canvas;
261255
}
262256

263-
if ($this->contrast != 0)
264-
{
257+
if ($this->contrast != 0) {
265258
$this->image->contrast($this->contrast);
266259
}
267260

268261
$this->text();
269262

270263
$this->lines();
271264

272-
if ($this->sharpen)
273-
{
265+
if ($this->sharpen) {
274266
$this->image->sharpen($this->sharpen);
275267
}
276-
if ($this->invert)
277-
{
268+
if ($this->invert) {
278269
$this->image->invert($this->invert);
279270
}
280-
if ($this->blur)
281-
{
271+
if ($this->blur) {
282272
$this->image->blur($this->blur);
283273
}
284274

285275
return $api ? [
286-
'sensitive' => $generator['sensitive'],
287-
'key' => $generator['key'],
288-
'img' => $this->image->encode('data-url')->encoded
276+
'sensitive' => $generator['sensitive'],
277+
'key' => $generator['key'],
278+
'img' => $this->image->encode('data-url')->encoded
289279
] : $this->image->response('png', $this->quality);
290280
}
291281

@@ -319,7 +309,7 @@ protected function generate()
319309
$key .= '';
320310
} else {
321311
for ($i = 0; $i < $this->length; $i++) {
322-
$char = $characters[rand(0, count($characters) - 1)];
312+
$char = $characters[rand(0, count($characters) - 1)];
323313
$bag[] = $this->sensitive ? $char : $this->str->lower($char);
324314
}
325315
$key = implode('', $bag);
@@ -328,13 +318,13 @@ protected function generate()
328318
$hash = $this->hasher->make($key);
329319
$this->session->put('captcha', [
330320
'sensitive' => $this->sensitive,
331-
'key' => $hash
321+
'key' => $hash
332322
]);
333323

334324
return [
335-
'value' => $bag,
336-
'sensitive' => $this->sensitive,
337-
'key' => $hash
325+
'value' => $bag,
326+
'sensitive' => $this->sensitive,
327+
'key' => $hash
338328
];
339329
}
340330

@@ -343,17 +333,17 @@ protected function generate()
343333
*/
344334
protected function text()
345335
{
346-
$marginTop = $this->image->height() / $this->length;
336+
$marginTop = $this->image->height() / $this->length;
347337

348-
$text = $this->text;
349-
if (is_string($text)) {
338+
$text = $this->text;
339+
if (is_string($text)) {
350340
$text = str_split($text);
351341
}
352342

353-
foreach($text as $key => $char) {
354-
$marginLeft = $this->textLeftPadding + ($key * ($this->image->width() - $this->textLeftPadding) / $this->length);
343+
foreach ($text as $key => $char) {
344+
$marginLeft = $this->textLeftPadding + ($key * ($this->image->width() - $this->textLeftPadding) / $this->length);
355345

356-
$this->image->text($char, $marginLeft, $marginTop, function($font) {
346+
$this->image->text($char, $marginLeft, $marginTop, function ($font) {
357347
$font->file($this->font());
358348
$font->size($this->fontSize());
359349
$font->color($this->fontColor());
@@ -391,12 +381,9 @@ protected function fontSize()
391381
*/
392382
protected function fontColor()
393383
{
394-
if ( ! empty($this->fontColors))
395-
{
384+
if (!empty($this->fontColors)) {
396385
$color = $this->fontColors[rand(0, count($this->fontColors) - 1)];
397-
}
398-
else
399-
{
386+
} else {
400387
$color = [rand(0, 255), rand(0, 255), rand(0, 255)];
401388
}
402389

@@ -420,8 +407,7 @@ protected function angle()
420407
*/
421408
protected function lines()
422409
{
423-
for($i = 0; $i <= $this->lines; $i++)
424-
{
410+
for ($i = 0; $i <= $this->lines; $i++) {
425411
$this->image->line(
426412
rand(0, $this->image->width()) + $i * rand(0, $this->image->height()),
427413
rand(0, $this->image->height()),
@@ -432,50 +418,48 @@ function ($draw) {
432418
}
433419
);
434420
}
421+
435422
return $this->image;
436423
}
437424

438-
/**
439-
* Captcha check
440-
*
441-
* @param $value
442-
* @return bool
443-
*/
444-
public function check($value)
445-
{
446-
if ( ! $this->session->has('captcha'))
447-
{
448-
return false;
449-
}
450-
451-
$key = $this->session->get('captcha.key');
452-
$sensitive = $this->session->get('captcha.sensitive');
453-
454-
if ( ! $sensitive)
455-
{
456-
$value = $this->str->lower($value);
457-
}
458-
459-
460-
$res = $this->hasher->check($value, $key);
425+
/**
426+
* Captcha check
427+
*
428+
* @param $value
429+
* @return bool
430+
*/
431+
public function check($value)
432+
{
433+
if (!$this->session->has('captcha')) {
434+
return false;
435+
}
436+
437+
$key = $this->session->get('captcha.key');
438+
$sensitive = $this->session->get('captcha.sensitive');
439+
440+
if (!$sensitive) {
441+
$value = $this->str->lower($value);
442+
}
443+
444+
$check = $this->hasher->check($value, $key);
461445
// if verify pass,remove session
462-
if ($res) {
446+
if ($check) {
463447
$this->session->remove('captcha');
464448
}
465449

466-
return $res;
467-
}
450+
return $check;
451+
}
468452

469-
/**
470-
* Captcha check
471-
*
472-
* @param $value
473-
* @return bool
474-
*/
475-
public function check_api($value, $key)
476-
{
477-
return $this->hasher->check($value, $key);
478-
}
453+
/**
454+
* Captcha check
455+
*
456+
* @param $value
457+
* @return bool
458+
*/
459+
public function check_api($value, $key)
460+
{
461+
return $this->hasher->check($value, $key);
462+
}
479463

480464
/**
481465
* Generate captcha image source
@@ -499,14 +483,14 @@ public function src($config = null)
499483
public function img($config = null, $attrs = [])
500484
{
501485
$attrs_str = '';
502-
foreach($attrs as $attr => $value){
503-
if ($attr == 'src'){
486+
foreach ($attrs as $attr => $value) {
487+
if ($attr == 'src') {
504488
//Neglect src attribute
505489
continue;
506490
}
507-
$attrs_str .= $attr.'="'.$value.'" ';
491+
492+
$attrs_str .= $attr . '="' . $value . '" ';
508493
}
509-
return new HtmlString('<img src="'.$this->src($config).'" '.trim($attrs_str).'>');
494+
return new HtmlString('<img src="' . $this->src($config) . '" ' . trim($attrs_str) . '>');
510495
}
511-
512496
}

0 commit comments

Comments
 (0)