Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ GtWeb Php Demo!
- 获取短信前的一次验证逻辑控制
- ./VerifyMsgServlet.php
- 提交短信验证示例Servlet
6. src/
- ./Laravel/*
- Laravel 框架扩展包


发布日志(由新到旧)
======================
Expand Down
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
12 changes: 7 additions & 5 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
<?php
/**
* Geetest配置文件
* @author Tanxu
*/
define("CAPTCHA_ID", "3386e03c620a4067f18fa92c370f1594");
define("PRIVATE_KEY", "5fe89444b54d3a3b8e49594c42a770cf");

?>
// defined('CAPTCHA_ID') or define('CAPTCHA_ID', '3386e03c620a4067f18fa92c370f1594');
// defined('PRIVATE_KEY') or define('PRIVATE_KEY', '5fe89444b54d3a3b8e49594c42a770cf');
return [
'captcha_id' => '3386e03c620a4067f18fa92c370f1594',
'private_key' => '5fe89444b54d3a3b8e49594c42a770cf'
];
30 changes: 19 additions & 11 deletions lib/class.geetestlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
}


Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -170,7 +178,7 @@ private function decodeRandBase($challenge) {

/**
* 得到答案
*
*
* @param validate
* @return
*/
Expand Down
6 changes: 3 additions & 3 deletions msg/StartMsgCaptchaServlet.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php
/**
* 使用Get的方式返回:challenge和capthca_id 此方式以实现前后端完全分离的开发模式 专门实现failback
* @author Tanxu
Expand All @@ -13,7 +13,7 @@
$_SESSION['gtserver'] = 1;
$result = array(
'success' => 1,
'gt' => CAPTCHA_ID,
'gt' => $GtMsgSdk->captcha_id,
'challenge' => $GtMsgSdk->challenge
);
echo json_encode($result);
Expand All @@ -24,6 +24,6 @@
);
echo json_encode($result);
}


?>
8 changes: 4 additions & 4 deletions msg/VerifyGeetestServlet.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?php
<?php
/**
* Msg套件下的拼图一次验证
* @author Tanxu
* $result 1:验证成功;
* $result 1:验证成功;
*/
require_once dirname(dirname(__FILE__)) . '/lib/class.geetestlib.php';
require_once dirname(dirname(__FILE__)) . '/lib/class.geetestmsg.php';
session_start();
$GtMsgSdk = $_SESSION['gtmsgsdk'];
$data = json_decode($_POST['value'],true);
if ($data['geetest_validate'] == md5(PRIVATE_KEY . 'geetest' . $data['geetest_challenge'])) {
if ($data['geetest_validate'] == md5($GtMsgSdk->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);
Expand Down
4 changes: 2 additions & 2 deletions msg/VerifyMsgServlet.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php
/**
* 二次的短信验证
* @author Tanxu
Expand All @@ -15,7 +15,7 @@
$phone = $_POST['phone'];
$data = array(
'phone' => $phone,
'msg_id' => CAPTCHA_ID,
'msg_id' => $GtMsgSdk->captcha_id,
'code' => $code
);
$result = $GtMsgSdk->send_msg_request($action,$data);
Expand Down
18 changes: 18 additions & 0 deletions src/Laravel/Facades/Geetest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace GeeTeam\Geetest\Laravel\Facades;

use Illuminate\Support\Facades\Facade;

class Geetest extends Facade
{

/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'geetest';
}
}
26 changes: 26 additions & 0 deletions src/Laravel/Geetest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace GeeTeam\Geetest\Laravel;

require_once __DIR__ . '/../../lib/class.geetestlib.php';

/**
* 极验行为式验证
*
* @author Latrell Chan
*/
class Geetest extends \GeetestLib
{

public function getWidget($product, $popupbtnid = '')
{
$params = array(
'gt' => $this->captcha_id,
'challenge' => $this->challenge,
'product' => $product
);
if ($product == "popup") {
$params["popupbtnid"] = $popupbtnid;
}
return '<script type="text/javascript" src="http://api.geetest.com/get.php?' . http_build_query($params) . '"></script>';
}
}
57 changes: 57 additions & 0 deletions src/Laravel/GeetestServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace GeeTeam\Geetest\Laravel;

use Illuminate\Support\ServiceProvider;

/**
* 极验行为式验证
*
* @author Latrell Chan
*/
class GeetestServiceProvider extends ServiceProvider
{

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->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'
];
}
}
68 changes: 68 additions & 0 deletions src/Laravel/readme.md
Original file line number Diff line number Diff line change
@@ -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
<input type="text" class="form-control" placeholder="验证码" name="captcha" autocomplete="off">
<img class="img-rounded" src="{{ Captcha::url() }}" alt="图形验证码" id="CaptchaImg">
@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);
}
```
Loading