diff --git a/README.md b/README.md index e64f24d..6ae2316 100755 --- a/README.md +++ b/README.md @@ -50,6 +50,10 @@ GtWeb Php Demo! - 获取短信前的一次验证逻辑控制 - ./VerifyMsgServlet.php - 提交短信验证示例Servlet +6. src/ + - ./Laravel/* + - Laravel 框架扩展包 + 发布日志(由新到旧) ====================== diff --git a/composer.json b/composer.json index 1c7b41c..3a88e27 100644 --- a/composer.json +++ b/composer.json @@ -10,10 +10,17 @@ "require": { "php": ">=5.0.0" }, + "require-dev": { + "illuminate/support": "5.*", + "illuminate/validation": "5.*" + }, "autoload": { "classmap": [ "lib" - ] + ], + "psr-4": { + "GeeTeam\\Geetest\\": "src/" + } }, "license": "MIT" } \ No newline at end of file diff --git a/config/config.php b/config/config.php index 0f57fe2..3d0af23 100644 --- a/config/config.php +++ b/config/config.php @@ -1,9 +1,11 @@ - \ No newline at end of file +// defined('CAPTCHA_ID') or define('CAPTCHA_ID', '3386e03c620a4067f18fa92c370f1594'); +// defined('PRIVATE_KEY') or define('PRIVATE_KEY', '5fe89444b54d3a3b8e49594c42a770cf'); +return [ + 'captcha_id' => '3386e03c620a4067f18fa92c370f1594', + 'private_key' => '5fe89444b54d3a3b8e49594c42a770cf' +]; diff --git a/lib/class.geetestlib.php b/lib/class.geetestlib.php index 0949d2a..9c7886c 100755 --- a/lib/class.geetestlib.php +++ b/lib/class.geetestlib.php @@ -3,11 +3,19 @@ * 极验行为式验证安全平台,php 网站主后台包含的库文件 *@author Tanxu */ -require_once dirname(dirname(__FILE__)) . '/config/config.php'; +// require_once dirname(dirname(__FILE__)) . '/config/config.php'; class GeetestLib{ const GT_SDK_VERSION = 'php_2.15.7.6.1'; public function __construct() { $this->challenge = ""; + + $config = include __DIR__ . '/../config/config.php'; + $this->setConfig($config); + } + + public function setConfig($config){ + $this->captcha_id = $config['captcha_id']; + $this->private_key = $config['private_key']; } /** @@ -16,7 +24,7 @@ public function __construct() { * @return */ public function register() { - $url = "http://api.geetest.com/register.php?gt=" . CAPTCHA_ID; + $url = "http://api.geetest.com/register.php?gt=" . $this->captcha_id; $this->challenge = $this->send_request($url); if (strlen($this->challenge) != 32) { return 0; @@ -45,7 +53,7 @@ private function check_validate($challenge, $validate) { if (strlen($validate) != 32) { return FALSE; } - if (md5(PRIVATE_KEY.'geetest'.$challenge) != $validate) { + if (md5($this->private_key.'geetest'.$challenge) != $validate) { return FALSE; } return TRUE; @@ -63,7 +71,7 @@ private function send_request($url){ 'http'=>array( 'method'=>"GET", 'timeout'=>2, - ) + ) ); $context = stream_context_create($opts); $data = file_get_contents($url, false, $context); @@ -89,7 +97,7 @@ private function decode_response($challenge,$string) { $res = 0; $array_challenge = str_split($challenge); $array_value = str_split($string); - for ($i=0; $i < strlen($challenge); $i++) { + for ($i=0; $i < strlen($challenge); $i++) { $item = $array_challenge[$i]; if (in_array($item, $chongfu)) { continue; @@ -101,11 +109,11 @@ private function decode_response($challenge,$string) { } } - for ($j=0; $j < strlen($string); $j++) { + for ($j=0; $j < strlen($string); $j++) { $res += $key[$array_value[$j]]; } $res = $res - $this->decodeRandBase($challenge); - return $res; + return $res; } @@ -138,7 +146,7 @@ private function get_failback_pic_ans($full_bg_index,$img_grp_index) { $answer_decode = ""; // 通过两个字符串奇数和偶数位拼接产生答案位 - for ($i=0; $i < 9; $i++) { + for ($i=0; $i < 9; $i++) { if ($i % 2 == 0) { $answer_decode = $answer_decode . $full_bg_name[$i]; }elseif ($i % 2 == 1) { @@ -152,14 +160,14 @@ private function get_failback_pic_ans($full_bg_index,$img_grp_index) { /** * 输入的两位的随机数字,解码出偏移量 - * + * * @param challenge * @return */ private function decodeRandBase($challenge) { $base = substr($challenge, 32, 2); $tempArray = array(); - for ($i=0; $i < strlen($base); $i++) { + for ($i=0; $i < strlen($base); $i++) { $tempAscii = ord($base[$i]); $result = ($tempAscii > 57) ? ($tempAscii - 87) : ($tempAscii -48); array_push($tempArray,$result); @@ -170,7 +178,7 @@ private function decodeRandBase($challenge) { /** * 得到答案 - * + * * @param validate * @return */ diff --git a/msg/StartMsgCaptchaServlet.php b/msg/StartMsgCaptchaServlet.php index e845594..19d3cf3 100644 --- a/msg/StartMsgCaptchaServlet.php +++ b/msg/StartMsgCaptchaServlet.php @@ -1,4 +1,4 @@ - 1, - 'gt' => CAPTCHA_ID, + 'gt' => $GtMsgSdk->captcha_id, 'challenge' => $GtMsgSdk->challenge ); echo json_encode($result); @@ -24,6 +24,6 @@ ); echo json_encode($result); } - + ?> \ No newline at end of file diff --git a/msg/VerifyGeetestServlet.php b/msg/VerifyGeetestServlet.php index 334c5f1..2e8e517 100644 --- a/msg/VerifyGeetestServlet.php +++ b/msg/VerifyGeetestServlet.php @@ -1,20 +1,20 @@ -private_key . 'geetest' . $data['geetest_challenge'])) { $codedata = array( "seccode" => $data['geetest_seccode'], "sdk" => "php_2.15.4.1.1", "phone" =>$data['phone'], - "msg_id" => CAPTCHA_ID + "msg_id" => $GtMsgSdk->captcha_id ); $action = "send"; $result = $GtMsgSdk->send_msg_request($action,$codedata); diff --git a/msg/VerifyMsgServlet.php b/msg/VerifyMsgServlet.php index 69f5bc4..392e85f 100644 --- a/msg/VerifyMsgServlet.php +++ b/msg/VerifyMsgServlet.php @@ -1,4 +1,4 @@ - $phone, - 'msg_id' => CAPTCHA_ID, + 'msg_id' => $GtMsgSdk->captcha_id, 'code' => $code ); $result = $GtMsgSdk->send_msg_request($action,$data); diff --git a/src/Laravel/Facades/Geetest.php b/src/Laravel/Facades/Geetest.php new file mode 100644 index 0000000..df45c5d --- /dev/null +++ b/src/Laravel/Facades/Geetest.php @@ -0,0 +1,18 @@ + $this->captcha_id, + 'challenge' => $this->challenge, + 'product' => $product + ); + if ($product == "popup") { + $params["popupbtnid"] = $popupbtnid; + } + return ''; + } +} diff --git a/src/Laravel/GeetestServiceProvider.php b/src/Laravel/GeetestServiceProvider.php new file mode 100644 index 0000000..2df9727 --- /dev/null +++ b/src/Laravel/GeetestServiceProvider.php @@ -0,0 +1,57 @@ +publishes([ + __DIR__ . '/../../config/config.php' => config_path('geetest.php') + ]); + + // 注册验证器扩展。 + require __DIR__ . '/validation.php'; + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->mergeConfigFrom(__DIR__ . '/../../config/config.php', 'geetest'); + + $this->app->singleton('geetest', function ($app) { + $config = $app->config->get('geetest'); + $geetest = new Geetest(); + $geetest->setConfig($config); + return $geetest; + }); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return [ + 'geetest' + ]; + } +} diff --git a/src/Laravel/readme.md b/src/Laravel/readme.md new file mode 100755 index 0000000..22818e0 --- /dev/null +++ b/src/Laravel/readme.md @@ -0,0 +1,68 @@ +gt-php-sdk +============ + +极验行为式验证 for Laravel 扩展包。 + +## 使用 + +要使用本服务提供者,你必须自己注册服务提供者到Laravel服务提供者列表中。 + +打开配置文件 `config/app.php`。 + +找到key为 `providers` 的数组,在数组中添加服务提供者。 + +```php + 'providers' => [ + // ... + GeeTeam\Geetest\Laravel\GeetestServiceProvider::class, + ] +``` + +找到key为 `aliases` 的数组,在数组中注册Facades。 + +```php + 'aliases' => [ + // ... + 'Geetest' => GeeTeam\Geetest\Laravel\Facades\Geetest::class, + ] +``` + +运行 `php artisan vendor:publish` 命令,发布配置文件到你的项目中。 + +修改配置文件 `config/geetest.php` 内的配置信息。 + +## 例子 + +### 视图 login.blade.php +``` + ... + @if (Geetest::register()) + {!! Geetest::getWidget('float')!!} + @else + + 图形验证码 + @endif + ... +``` + +### 控制器 AuthController.php +```php + // 验证输入。 + $validator = $this->getValidationFactory()->make($request->all(), [ + // ... + ], [ + 'captcha.required' => '请填写验证图片中的文字。', + 'geetest_challenge.required' => '请拖动滑块完成验证。' + ]); + $validator->sometimes('captcha', 'required|captcha', function ($input) { + // 其它验证方式,比如图形验证码。 + return is_null($input->geetest_challenge) || is_null($input->geetest_validate) || is_null($input->geetest_seccode); + }); + $validator->sometimes('geetest_challenge', 'required|geetest', function ($input) { + // 极验行为式验证。 + return ! is_null($input->geetest_challenge) && ! is_null($input->geetest_validate) && ! is_null($input->geetest_seccode); + }); + if ($validator->fails()) { + $this->throwValidationException($request, $validator); + } +``` diff --git a/src/Laravel/validation.php b/src/Laravel/validation.php new file mode 100644 index 0000000..f39cd59 --- /dev/null +++ b/src/Laravel/validation.php @@ -0,0 +1,9 @@ +getData(); + if (key_exists('geetest_challenge', $data) && key_exists('geetest_validate', $data) && key_exists('geetest_seccode', $data)) { + return true === app('geetest')->validate($data['geetest_challenge'], $data['geetest_validate'], $data['geetest_seccode']); + } + return false; +}); diff --git a/web/StartCaptchaServlet.php b/web/StartCaptchaServlet.php index 890eea3..41862a8 100644 --- a/web/StartCaptchaServlet.php +++ b/web/StartCaptchaServlet.php @@ -1,4 +1,4 @@ - 1, - 'gt' => CAPTCHA_ID, + 'gt' => $GtSdk->captcha_id, 'challenge' => $GtSdk->challenge ); echo json_encode($result); @@ -23,12 +23,12 @@ $challenge = $rnd1 . substr($rnd2,0,2); $result = array( 'success' => 0, - 'gt' => CAPTCHA_ID, + 'gt' => $GtSdk->captcha_id, 'challenge' => $challenge ); $_SESSION['challenge'] = $result['challenge']; echo json_encode($result); } - + ?> \ No newline at end of file