Skip to content

Commit

Permalink
Code and tests revision.
Browse files Browse the repository at this point in the history
  • Loading branch information
mewebstudio committed Apr 19, 2019
1 parent 8a1de32 commit 77ee353
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 204 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
language: php

php:
- 5.5
- 5.6
- 7.0
- hhvm
- 7.1
- 7.2

before_script:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction

script: make test
script: make test
77 changes: 37 additions & 40 deletions config/captcha.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,48 @@
<?php

return [

'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'],

'default' => [
'length' => 9,
'width' => 120,
'height' => 36,
'quality' => 90,
'math' => false,
'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'],
'default' => [
'length' => 9,
'width' => 120,
'height' => 36,
'quality' => 90,
'math' => false,
],
'math' => [
'length' => 9,
'width' => 120,
'height' => 36,
'quality' => 90,
'math' => true,
'math' => [
'length' => 9,
'width' => 120,
'height' => 36,
'quality' => 90,
'math' => true,
],

'flat' => [
'length' => 6,
'width' => 160,
'height' => 46,
'quality' => 90,
'lines' => 6,
'bgImage' => false,
'bgColor' => '#ecf2f4',
'fontColors'=> ['#2c3e50', '#c0392b', '#16a085', '#c0392b', '#8e44ad', '#303f9f', '#f57c00', '#795548'],
'contrast' => -5,
'flat' => [
'length' => 6,
'width' => 160,
'height' => 46,
'quality' => 90,
'lines' => 6,
'bgImage' => false,
'bgColor' => '#ecf2f4',
'fontColors' => ['#2c3e50', '#c0392b', '#16a085', '#c0392b', '#8e44ad', '#303f9f', '#f57c00', '#795548'],
'contrast' => -5,
],
'mini' => [
'length' => 3,
'width' => 60,
'height' => 32,
'mini' => [
'length' => 3,
'width' => 60,
'height' => 32,
],
'inverse' => [
'length' => 5,
'width' => 120,
'height' => 36,
'quality' => 90,
'inverse' => [
'length' => 5,
'width' => 120,
'height' => 36,
'quality' => 90,
'sensitive' => true,
'angle' => 12,
'sharpen' => 10,
'blur' => 2,
'invert' => true,
'contrast' => -5,
'angle' => 12,
'sharpen' => 10,
'blur' => 2,
'invert' => true,
'contrast' => -5,
]

];
150 changes: 67 additions & 83 deletions src/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
*/
class Captcha
{

/**
* @var Filesystem
*/
Expand Down Expand Up @@ -198,7 +197,7 @@ public function __construct(
$this->session = $session;
$this->hasher = $hasher;
$this->str = $str;
$this->characters = config('captcha.characters',['1','2','3','4','6','7','8','9']);
$this->characters = config('captcha.characters', ['1', '2', '3', '4', '6', '7', '8', '9']);
}

/**
Expand All @@ -207,10 +206,8 @@ public function __construct(
*/
protected function configure($config)
{
if ($this->config->has('captcha.' . $config))
{
foreach($this->config->get('captcha.' . $config) as $key => $val)
{
if ($this->config->has('captcha.' . $config)) {
foreach ($this->config->get('captcha.' . $config) as $key => $val) {
$this->{$key} = $val;
}
}
Expand All @@ -228,8 +225,8 @@ public function create($config = 'default', $api = false)
$this->backgrounds = $this->files->files(__DIR__ . '/../assets/backgrounds');
$this->fonts = $this->files->files(__DIR__ . '/../assets/fonts');

if (app()->version() >= 5.5){
$this->fonts = array_map(function($file) {
if (app()->version() >= 5.5) {
$this->fonts = array_map(function ($file) {
return $file->getPathName();
}, $this->fonts);
}
Expand All @@ -247,45 +244,38 @@ public function create($config = 'default', $api = false)
$this->bgColor
);

if ($this->bgImage)
{
if ($this->bgImage) {
$this->image = $this->imageManager->make($this->background())->resize(
$this->width,
$this->height
);
$this->canvas->insert($this->image);
}
else
{
} else {
$this->image = $this->canvas;
}

if ($this->contrast != 0)
{
if ($this->contrast != 0) {
$this->image->contrast($this->contrast);
}

$this->text();

$this->lines();

if ($this->sharpen)
{
if ($this->sharpen) {
$this->image->sharpen($this->sharpen);
}
if ($this->invert)
{
if ($this->invert) {
$this->image->invert($this->invert);
}
if ($this->blur)
{
if ($this->blur) {
$this->image->blur($this->blur);
}

return $api ? [
'sensitive' => $generator['sensitive'],
'key' => $generator['key'],
'img' => $this->image->encode('data-url')->encoded
'sensitive' => $generator['sensitive'],
'key' => $generator['key'],
'img' => $this->image->encode('data-url')->encoded
] : $this->image->response('png', $this->quality);
}

Expand Down Expand Up @@ -319,7 +309,7 @@ protected function generate()
$key .= '';
} else {
for ($i = 0; $i < $this->length; $i++) {
$char = $characters[rand(0, count($characters) - 1)];
$char = $characters[rand(0, count($characters) - 1)];
$bag[] = $this->sensitive ? $char : $this->str->lower($char);
}
$key = implode('', $bag);
Expand All @@ -328,13 +318,13 @@ protected function generate()
$hash = $this->hasher->make($key);
$this->session->put('captcha', [
'sensitive' => $this->sensitive,
'key' => $hash
'key' => $hash
]);

return [
'value' => $bag,
'sensitive' => $this->sensitive,
'key' => $hash
'value' => $bag,
'sensitive' => $this->sensitive,
'key' => $hash
];
}

Expand All @@ -343,17 +333,17 @@ protected function generate()
*/
protected function text()
{
$marginTop = $this->image->height() / $this->length;
$marginTop = $this->image->height() / $this->length;

$text = $this->text;
if (is_string($text)) {
$text = $this->text;
if (is_string($text)) {
$text = str_split($text);
}

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

$this->image->text($char, $marginLeft, $marginTop, function($font) {
$this->image->text($char, $marginLeft, $marginTop, function ($font) {
$font->file($this->font());
$font->size($this->fontSize());
$font->color($this->fontColor());
Expand Down Expand Up @@ -391,12 +381,9 @@ protected function fontSize()
*/
protected function fontColor()
{
if ( ! empty($this->fontColors))
{
if (!empty($this->fontColors)) {
$color = $this->fontColors[rand(0, count($this->fontColors) - 1)];
}
else
{
} else {
$color = [rand(0, 255), rand(0, 255), rand(0, 255)];
}

Expand All @@ -420,8 +407,7 @@ protected function angle()
*/
protected function lines()
{
for($i = 0; $i <= $this->lines; $i++)
{
for ($i = 0; $i <= $this->lines; $i++) {
$this->image->line(
rand(0, $this->image->width()) + $i * rand(0, $this->image->height()),
rand(0, $this->image->height()),
Expand All @@ -432,50 +418,48 @@ function ($draw) {
}
);
}

return $this->image;
}

/**
* Captcha check
*
* @param $value
* @return bool
*/
public function check($value)
{
if ( ! $this->session->has('captcha'))
{
return false;
}

$key = $this->session->get('captcha.key');
$sensitive = $this->session->get('captcha.sensitive');

if ( ! $sensitive)
{
$value = $this->str->lower($value);
}


$res = $this->hasher->check($value, $key);
/**
* Captcha check
*
* @param $value
* @return bool
*/
public function check($value)
{
if (!$this->session->has('captcha')) {
return false;
}

$key = $this->session->get('captcha.key');
$sensitive = $this->session->get('captcha.sensitive');

if (!$sensitive) {
$value = $this->str->lower($value);
}

$check = $this->hasher->check($value, $key);
// if verify pass,remove session
if ($res) {
if ($check) {
$this->session->remove('captcha');
}

return $res;
}
return $check;
}

/**
* Captcha check
*
* @param $value
* @return bool
*/
public function check_api($value, $key)
{
return $this->hasher->check($value, $key);
}
/**
* Captcha check
*
* @param $value
* @return bool
*/
public function check_api($value, $key)
{
return $this->hasher->check($value, $key);
}

/**
* Generate captcha image source
Expand All @@ -499,14 +483,14 @@ public function src($config = null)
public function img($config = null, $attrs = [])
{
$attrs_str = '';
foreach($attrs as $attr => $value){
if ($attr == 'src'){
foreach ($attrs as $attr => $value) {
if ($attr == 'src') {
//Neglect src attribute
continue;
}
$attrs_str .= $attr.'="'.$value.'" ';

$attrs_str .= $attr . '="' . $value . '" ';
}
return new HtmlString('<img src="'.$this->src($config).'" '.trim($attrs_str).'>');
return new HtmlString('<img src="' . $this->src($config) . '" ' . trim($attrs_str) . '>');
}

}

0 comments on commit 77ee353

Please sign in to comment.