From 5748ad0ada26e2f569295554db8f777a7bea9f95 Mon Sep 17 00:00:00 2001 From: jzaaa <2509642078@qq.com> Date: Wed, 20 Feb 2019 13:24:21 +0800 Subject: [PATCH] first commit --- .gitignore | 4 + composer.json | 21 +++ config/bootstrap.php | 7 + config/routes.php | 20 +++ src/Captcha.php | 212 +++++++++++++++++++++++++++ src/Controller/AppController.php | 11 ++ src/Controller/CaptchaController.php | 14 ++ src/Template/Captcha/index.ctp | 0 8 files changed, 289 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 config/bootstrap.php create mode 100644 config/routes.php create mode 100644 src/Captcha.php create mode 100644 src/Controller/AppController.php create mode 100644 src/Controller/CaptchaController.php create mode 100644 src/Template/Captcha/index.ctp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a7d1ac7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/vendor/* +/tmp/* +.idea/* +composer.lock \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..b1056d9 --- /dev/null +++ b/composer.json @@ -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/" + } + } +} diff --git a/config/bootstrap.php b/config/bootstrap.php new file mode 100644 index 0000000..f8e37f5 --- /dev/null +++ b/config/bootstrap.php @@ -0,0 +1,7 @@ + '/' +], 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'] + ); + } +}); \ No newline at end of file diff --git a/src/Captcha.php b/src/Captcha.php new file mode 100644 index 0000000..25aa75f --- /dev/null +++ b/src/Captcha.php @@ -0,0 +1,212 @@ +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 ''; + } + + 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); + } +} \ No newline at end of file diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php new file mode 100644 index 0000000..16c1dcc --- /dev/null +++ b/src/Controller/AppController.php @@ -0,0 +1,11 @@ +