Skip to content

Commit 43ecdc8

Browse files
authored
Merge pull request #169 from mewebstudio/dev
Support Laravel 6
2 parents 2c9efa1 + 1f3a629 commit 43ecdc8

10 files changed

+120
-82
lines changed

.travis.yml

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
language: php
22

33
php:
4-
- 5.6
5-
- 7.0
6-
- 7.1
74
- 7.2
85
- 7.3
96

7+
# This triggers builds to run on the new TravisCI infrastructure.
8+
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
9+
sudo: false
10+
11+
## Cache composer
12+
cache:
13+
directories:
14+
- $HOME/.composer/cache
15+
1016
before_script:
1117
- travis_retry composer self-update
12-
- travis_retry composer install --prefer-source --no-interaction
18+
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist
1319

14-
script: make test
20+
script:
21+
- phpunit --coverage-text --coverage-clover=coverage.clover

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
# Captcha for Laravel 5
1+
# Captcha for Laravel 5 & 6
22

33
[![Build Status](https://travis-ci.org/mewebstudio/captcha.svg?branch=master)](https://travis-ci.org/mewebstudio/captcha) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mewebstudio/captcha/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/mewebstudio/captcha/?branch=master)
4+
[![Latest Stable Version](https://poser.pugx.org/mews/captcha/v/stable.svg)](https://packagist.org/packages/mews/captcha)
5+
[![Latest Stable Version](https://poser.pugx.org/mews/captcha/v/unstable.svg)](https://packagist.org/packages/mews/captcha)
6+
[![License](https://poser.pugx.org/mews/captcha/license.svg)](https://packagist.org/packages/mews/captcha)
7+
[![Total Downloads](https://poser.pugx.org/mews/captcha/downloads.svg)](https://packagist.org/packages/mews/captcha)
48

5-
A simple [Laravel 5](http://www.laravel.com/) service provider for including the [Captcha for Laravel 5](https://github.com/mewebstudio/captcha).
9+
A simple [Laravel 5/6](http://www.laravel.com/) service provider for including the [Captcha for Laravel 5](https://github.com/mewebstudio/captcha).
610

711
for Laravel 4 [Captcha for Laravel Laravel 4](https://github.com/mewebstudio/captcha/tree/master-l4)
812

composer.json

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "mews/captcha",
33
"type": "package",
4-
"description": "Laravel 5 Captcha Package",
5-
"keywords": ["laravel5 Captcha", "laravel5 Security", "Captcha"],
4+
"description": "Laravel 5 & 6 Captcha Package",
5+
"keywords": ["laravel6 Captcha", "laravel6 Security", "laravel6 Captcha", "laravel5 Security", "Captcha"],
66
"homepage": "https://github.com/mewebstudio/captcha",
77
"license": "MIT",
88
"authors": [
@@ -14,17 +14,18 @@
1414
}
1515
],
1616
"require": {
17-
"php": ">=5.4",
17+
"php": "^7.2",
1818
"ext-gd": "*",
19-
"illuminate/config": "~5.0",
20-
"illuminate/filesystem": "~5.0",
21-
"illuminate/support": "~5.0",
22-
"illuminate/hashing": "~5.0",
23-
"intervention/image": "~2.2"
19+
"illuminate/config": "~5.0|^6.0",
20+
"illuminate/filesystem": "~5.0|^6.0",
21+
"illuminate/support": "~5.0|^6.0",
22+
"illuminate/hashing": "~5.0|^6.0",
23+
"illuminate/session": "~5.0|^6.0",
24+
"intervention/image": "~2.5"
2425
},
2526
"require-dev": {
26-
"phpunit/phpunit": "~4.1",
27-
"mockery/mockery": "0.9.*"
27+
"phpunit/phpunit": "^8.0",
28+
"mockery/mockery": "^1.0"
2829
},
2930
"autoload": {
3031
"psr-4": {
@@ -42,7 +43,7 @@
4243
"Mews\\Test\\": "tests/"
4344
}
4445
},
45-
"minimum-stability": "dev",
46+
"minimum-stability": "stable",
4647
"extra": {
4748
"laravel": {
4849
"providers": [

src/Captcha.php

+41-33
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Mews\Captcha;
44

55
/**
6-
* Laravel 5 Captcha package
6+
* Laravel 5 & 6 Captcha package
77
*
88
* @copyright Copyright (c) 2015 MeWebStudio
99
* @version 2.x
@@ -18,8 +18,10 @@
1818
use Illuminate\Config\Repository;
1919
use Illuminate\Hashing\BcryptHasher as Hasher;
2020
use Illuminate\Filesystem\Filesystem;
21-
use Illuminate\Support\Facades\Crypt;
21+
use Illuminate\Http\File;
2222
use Illuminate\Support\Str;
23+
use Intervention\Image\Gd\Font;
24+
use Intervention\Image\Image;
2325
use Intervention\Image\ImageManager;
2426
use Illuminate\Session\Store as Session;
2527
use Illuminate\Support\HtmlString;
@@ -66,7 +68,7 @@ class Captcha
6668
protected $canvas;
6769

6870
/**
69-
* @var ImageManager->image
71+
* @var Image
7072
*/
7173
protected $image;
7274

@@ -194,8 +196,7 @@ public function __construct(
194196
Session $session,
195197
Hasher $hasher,
196198
Str $str
197-
)
198-
{
199+
) {
199200
$this->files = $files;
200201
$this->config = $config;
201202
$this->imageManager = $imageManager;
@@ -223,16 +224,18 @@ protected function configure($config)
223224
* Create captcha image
224225
*
225226
* @param string $config
226-
* @param boolean $api
227-
* @return ImageManager->response
227+
* @param bool $api
228+
* @return array|mixed
229+
* @throws Exception
228230
*/
229-
public function create($config = 'default', $api = false)
231+
public function create(string $config = 'default', bool $api = false)
230232
{
231233
$this->backgrounds = $this->files->files(__DIR__ . '/../assets/backgrounds');
232234
$this->fonts = $this->files->files($this->fontsDirectory);
233235

234236
if (version_compare(app()->version(), '5.5.0', '>=')) {
235237
$this->fonts = array_map(function ($file) {
238+
/* @var File $file */
236239
return $file->getPathName();
237240
}, $this->fonts);
238241
}
@@ -272,7 +275,7 @@ public function create($config = 'default', $api = false)
272275
$this->image->sharpen($this->sharpen);
273276
}
274277
if ($this->invert) {
275-
$this->image->invert($this->invert);
278+
$this->image->invert();
276279
}
277280
if ($this->blur) {
278281
$this->image->blur($this->blur);
@@ -290,22 +293,22 @@ public function create($config = 'default', $api = false)
290293
*
291294
* @return string
292295
*/
293-
protected function background()
296+
protected function background(): string
294297
{
295298
return $this->backgrounds[rand(0, count($this->backgrounds) - 1)];
296299
}
297300

298301
/**
299302
* Generate captcha text
300303
*
301-
* @return string
304+
* @return array
305+
* @throws Exception
302306
*/
303-
protected function generate()
307+
protected function generate(): array
304308
{
305309
$characters = is_string($this->characters) ? str_split($this->characters) : $this->characters;
306310

307311
$bag = [];
308-
$key = '';
309312

310313
if ($this->math) {
311314
$x = random_int(10, 30);
@@ -336,8 +339,10 @@ protected function generate()
336339

337340
/**
338341
* Writing captcha text
342+
*
343+
* @return void
339344
*/
340-
protected function text()
345+
protected function text(): void
341346
{
342347
$marginTop = $this->image->height() / $this->length;
343348

@@ -350,6 +355,7 @@ protected function text()
350355
$marginLeft = $this->textLeftPadding + ($key * ($this->image->width() - $this->textLeftPadding) / $this->length);
351356

352357
$this->image->text($char, $marginLeft, $marginTop, function ($font) {
358+
/* @var Font $font */
353359
$font->file($this->font());
354360
$font->size($this->fontSize());
355361
$font->color($this->fontColor());
@@ -365,32 +371,32 @@ protected function text()
365371
*
366372
* @return string
367373
*/
368-
protected function font()
374+
protected function font(): string
369375
{
370376
return $this->fonts[rand(0, count($this->fonts) - 1)];
371377
}
372378

373379
/**
374380
* Random font size
375381
*
376-
* @return integer
382+
* @return int
377383
*/
378-
protected function fontSize()
384+
protected function fontSize(): int
379385
{
380386
return rand($this->image->height() - 10, $this->image->height());
381387
}
382388

383389
/**
384390
* Random font color
385391
*
386-
* @return array
392+
* @return string
387393
*/
388-
protected function fontColor()
394+
protected function fontColor(): string
389395
{
390396
if (!empty($this->fontColors)) {
391397
$color = $this->fontColors[rand(0, count($this->fontColors) - 1)];
392398
} else {
393-
$color = [rand(0, 255), rand(0, 255), rand(0, 255)];
399+
$color = '#' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
394400
}
395401

396402
return $color;
@@ -401,15 +407,15 @@ protected function fontColor()
401407
*
402408
* @return int
403409
*/
404-
protected function angle()
410+
protected function angle(): int
405411
{
406412
return rand((-1 * $this->angle), $this->angle);
407413
}
408414

409415
/**
410416
* Random image lines
411417
*
412-
* @return \Intervention\Image\Image
418+
* @return Image|ImageManager
413419
*/
414420
protected function lines()
415421
{
@@ -420,6 +426,7 @@ protected function lines()
420426
rand(0, $this->image->width()),
421427
rand(0, $this->image->height()),
422428
function ($draw) {
429+
/* @var Font $draw */
423430
$draw->color($this->fontColor());
424431
}
425432
);
@@ -431,10 +438,10 @@ function ($draw) {
431438
/**
432439
* Captcha check
433440
*
434-
* @param $value
441+
* @param string $value
435442
* @return bool
436443
*/
437-
public function check($value)
444+
public function check(string $value): bool
438445
{
439446
if (!$this->session->has('captcha')) {
440447
return false;
@@ -448,7 +455,7 @@ public function check($value)
448455
}
449456

450457
$check = $this->hasher->check($value, $key);
451-
// if verify pass,remove session
458+
// if verify pass,remove session
452459
if ($check) {
453460
$this->session->remove('captcha');
454461
}
@@ -459,34 +466,35 @@ public function check($value)
459466
/**
460467
* Captcha check
461468
*
462-
* @param $value
469+
* @param string $value
470+
* @param string $key
463471
* @return bool
464472
*/
465-
public function check_api($value, $key)
473+
public function check_api($value, $key): bool
466474
{
467475
return $this->hasher->check($value, $key);
468476
}
469477

470478
/**
471479
* Generate captcha image source
472480
*
473-
* @param null $config
481+
* @param string|null $config
474482
* @return string
475483
*/
476-
public function src($config = null)
484+
public function src(?string $config): string
477485
{
478486
return url('captcha' . ($config ? '/' . $config : '/default')) . '?' . $this->str->random(8);
479487
}
480488

481489
/**
482490
* Generate captcha image html tag
483491
*
484-
* @param null $config
485-
* @param array $attrs HTML attributes supplied to the image tag where key is the attribute
486-
* and the value is the attribute value
492+
* @param string|null $config
493+
* @param array|null $attrs
494+
* $attrs -> HTML attributes supplied to the image tag where key is the attribute and the value is the attribute value
487495
* @return string
488496
*/
489-
public function img($config = null, $attrs = [])
497+
public function img(?string $config, ?array $attrs): string
490498
{
491499
$attrs_str = '';
492500
foreach ($attrs as $attr => $value) {

src/CaptchaController.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Mews\Captcha;
44

5+
use Exception;
56
use Illuminate\Routing\Controller;
67

78
/**
@@ -13,11 +14,12 @@ class CaptchaController extends Controller
1314
/**
1415
* get CAPTCHA
1516
*
16-
* @param \Mews\Captcha\Captcha $captcha
17+
* @param Captcha $captcha
1718
* @param string $config
18-
* @return \Intervention\Image\ImageManager->response
19+
* @return array|mixed
20+
* @throws Exception
1921
*/
20-
public function getCaptcha(Captcha $captcha, $config = 'default')
22+
public function getCaptcha(Captcha $captcha, string $config = 'default')
2123
{
2224
if (ob_get_contents()) {
2325
ob_clean();
@@ -29,11 +31,12 @@ public function getCaptcha(Captcha $captcha, $config = 'default')
2931
/**
3032
* get CAPTCHA api
3133
*
32-
* @param \Mews\Captcha\Captcha $captcha
34+
* @param Captcha $captcha
3335
* @param string $config
34-
* @return \Intervention\Image\ImageManager->response
36+
* @return array|mixed
37+
* @throws Exception
3538
*/
36-
public function getCaptchaApi(Captcha $captcha, $config = 'default')
39+
public function getCaptchaApi(Captcha $captcha, string $config = 'default')
3740
{
3841
return $captcha->create($config, true);
3942
}

0 commit comments

Comments
 (0)