-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5748ad0
Showing
8 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/vendor/* | ||
/tmp/* | ||
.idea/* | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "jzaaa/cake-captcha", | ||
"description": "CakePHP3 Captcha Package", | ||
"keywords": ["CakePHP5 Captcha", "CakePHP", "Captcha"], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "jzaaa" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.6", | ||
"cakephp/cakephp": "^3.6", | ||
"gregwar/captcha": "^1.1" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"JZaaa\\CakeCaptcha\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
use Cake\Core\Configure; | ||
$config = 'captcha'; | ||
$configPath = CONFIG . $config . '.php'; | ||
if (file_exists($configPath)) { | ||
Configure::load($config, 'default'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
use Cake\Core\Configure; | ||
use Cake\Routing\Router; | ||
|
||
Router::plugin('JZaaa/CakeCaptcha', [ | ||
'path' => '/' | ||
], function (\Cake\Routing\RouteBuilder $routes) { | ||
// route | ||
if (Configure::read('captcha.route')) { | ||
$routes->connect( | ||
Configure::read('captcha.route'), | ||
['plugin' => 'JZaaa/CakeCaptcha', 'controller' => 'Captcha', 'action' => 'index'] | ||
); | ||
} else { | ||
$routes->connect( | ||
'/jzaaa/cake-captcha/', | ||
['plugin' => 'JZaaa/CakeCaptcha', 'controller' => 'Captcha', 'action' => 'index'] | ||
); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
<?php | ||
|
||
namespace JZaaa\CakeCaptcha; | ||
|
||
use Cake\Http\Session; | ||
use Gregwar\Captcha\CaptchaBuilder; | ||
use Gregwar\Captcha\PhraseBuilder; | ||
|
||
class Captcha | ||
{ | ||
|
||
/** | ||
* session | ||
* @var \Cake\Http\Session | ||
*/ | ||
protected $session; | ||
|
||
/** | ||
* @var $sessionKey | ||
*/ | ||
protected $sessionKey = 'captcha'; | ||
|
||
|
||
/** | ||
* 是否对大小写敏感 | ||
* @var bool $sensitive | ||
*/ | ||
protected $sensitive = false; | ||
|
||
/** | ||
* 验证码图像宽 | ||
* @var int $width | ||
*/ | ||
protected $width = 150; | ||
|
||
/** | ||
* 验证码图像高 | ||
* @var int $height | ||
*/ | ||
protected $height = 40; | ||
|
||
/** | ||
* 验证码长度 | ||
* @var int $length | ||
*/ | ||
protected $length = 4; | ||
|
||
/** | ||
* 验证码字符集 | ||
* @var string $charset | ||
*/ | ||
protected $charset = '2346789abcdefghjmnpqrtuxyzABCDEFGHJMNPQRTUXYZ'; | ||
|
||
/** | ||
* @var \Gregwar\Captcha\CaptchaBuilder | ||
*/ | ||
protected $captchaBuilder; | ||
|
||
/** | ||
* 验证码 | ||
* @var $phrase | ||
*/ | ||
protected $phrase; | ||
|
||
/** | ||
* @var \Gregwar\Captcha\PhraseBuilder | ||
*/ | ||
protected $phraseBuilder; | ||
|
||
/** | ||
* Captcha constructor. | ||
* @param array $config | ||
*/ | ||
public function __construct($config = []) | ||
{ | ||
$this->configure($config); | ||
$this->init(); | ||
} | ||
|
||
protected function init() | ||
{ | ||
if ($this->session instanceof Session) { | ||
$this->session; | ||
} | ||
$this->session = new Session(); | ||
|
||
if (!$this->sensitive) { | ||
$this->charset = strtolower($this->charset); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* 配置 | ||
* @param $config | ||
*/ | ||
protected function configure($config) | ||
{ | ||
$ignore = ['captchaBuilder', 'phraseBuilder', 'session']; | ||
|
||
foreach ($config as $key => $item) { | ||
if (isset($this->$key) && !in_array($key, $ignore)) { | ||
$this->$key = $item; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 初始化Captcha | ||
*/ | ||
protected function initCaptcha() | ||
{ | ||
if (!($this->phraseBuilder instanceof PhraseBuilder)) { | ||
$this->phraseBuilder = new PhraseBuilder($this->length, $this->charset); | ||
} | ||
if (!$this->captchaBuilder instanceof CaptchaBuilder) { | ||
$this->captchaBuilder = new CaptchaBuilder(null, $this->phraseBuilder); | ||
} | ||
} | ||
|
||
/** | ||
* captcha生成器 | ||
* @return array | ||
*/ | ||
protected function generate() | ||
{ | ||
$this->initCaptcha(); | ||
|
||
$this->captchaBuilder->build($this->width, $this->height); | ||
|
||
$this->phrase = $this->captchaBuilder->getPhrase(); | ||
|
||
$this->session->write($this->sessionKey, $this->phrase); | ||
|
||
return [ | ||
'phrase' => $this->phrase, | ||
'sensitive' => $this->sensitive | ||
]; | ||
} | ||
|
||
/** | ||
* 生成captcha | ||
* @return array | ||
*/ | ||
public function create() | ||
{ | ||
return $this->generate(); | ||
} | ||
|
||
|
||
/** | ||
* 获取验证码值 | ||
* @return mixed | ||
*/ | ||
public function getPhrase() | ||
{ | ||
return $this->phrase; | ||
} | ||
|
||
|
||
/** | ||
* 返回验证码图像src | ||
* @return string | ||
*/ | ||
public function base64() | ||
{ | ||
return $this->captchaBuilder->inline(); | ||
} | ||
|
||
/** | ||
* 返回base64验证码图像 | ||
* @return string | ||
*/ | ||
public function imgBase64() | ||
{ | ||
return '<img src="' . $this->base64() . '"/>'; | ||
} | ||
|
||
public function img() | ||
{ | ||
header('Content-type: image/jpeg'); | ||
$this->captchaBuilder->output(); | ||
} | ||
|
||
|
||
/** | ||
* 核对验证码 | ||
* @param string $value | ||
* @return bool | ||
*/ | ||
public function check($value) | ||
{ | ||
$phrase = $this->session->read($this->sessionKey); | ||
|
||
if (!$this->sensitive) { | ||
$value = strtolower($value); | ||
} | ||
|
||
$result = ($value == $phrase); | ||
|
||
if ($result) { | ||
$this->session->delete($this->sessionKey); | ||
} | ||
return $result; | ||
} | ||
|
||
|
||
public function read() | ||
{ | ||
return $this->session->read($this->sessionKey); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace JZaaa\CakeCaptcha\Controller; | ||
|
||
use Cake\Controller\Controller as BaseController; | ||
|
||
|
||
class AppController extends BaseController | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace JZaaa\CakeCaptcha\Controller; | ||
|
||
use JZaaa\CakeCaptcha\Captcha; | ||
|
||
class CaptchaController extends AppController | ||
{ | ||
|
||
public function index() | ||
{ | ||
} | ||
|
||
} |
Empty file.