Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JZaaa committed Feb 20, 2019
0 parents commit 5748ad0
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor/*
/tmp/*
.idea/*
composer.lock
21 changes: 21 additions & 0 deletions composer.json
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/"
}
}
}
7 changes: 7 additions & 0 deletions config/bootstrap.php
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');
}
20 changes: 20 additions & 0 deletions config/routes.php
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']
);
}
});
212 changes: 212 additions & 0 deletions src/Captcha.php
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);
}
}
11 changes: 11 additions & 0 deletions src/Controller/AppController.php
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
{

}
14 changes: 14 additions & 0 deletions src/Controller/CaptchaController.php
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 added src/Template/Captcha/index.ctp
Empty file.

0 comments on commit 5748ad0

Please sign in to comment.