You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
GeekQu_sspanel/app/Utils/GeetestLib.php

371 lines
9.5 KiB
PHTML

9 years ago
<?php
namespace App\Utils;
/**
* 极验行为式验证安全平台,php 网站主后台包含的库文件
*
* @author Tanxu
*/
class GeetestLib
{
8 years ago
public const GT_SDK_VERSION = 'php_3.2.0';
9 years ago
public static $connectTimeout = 3;
8 years ago
public static $socketTimeout = 3;
9 years ago
private $response;
public function __construct($captcha_id, $private_key)
{
8 years ago
$this->captcha_id = $captcha_id;
9 years ago
$this->private_key = $private_key;
}
/**
* 判断极验服务器是否down机
*
* @param null $user_id
* @return int
*/
public function pre_process($user_id = null)
{
8 years ago
$url = 'http://api.geetest.com/register.php?gt=' . $this->captcha_id;
if (($user_id != null) and is_string($user_id)) {
$url = $url . '&user_id=' . $user_id;
9 years ago
}
$challenge = $this->send_request($url);
if (strlen($challenge) != 32) {
$this->failback_process();
return 0;
}
$this->success_process($challenge);
return 1;
}
/**
* @param $challenge
*/
private function success_process($challenge)
{
8 years ago
$challenge = md5($challenge . $this->private_key);
$result = array(
'success' => 1,
'gt' => $this->captcha_id,
9 years ago
'challenge' => $challenge
);
$this->response = $result;
}
/**
*
*/
private function failback_process()
{
8 years ago
$rnd1 = md5(random_int(0, 100));
$rnd2 = md5(random_int(0, 100));
8 years ago
$challenge = $rnd1 . substr($rnd2, 0, 2);
$result = array(
'success' => 0,
'gt' => $this->captcha_id,
9 years ago
'challenge' => $challenge
);
$this->response = $result;
}
/**
* @return mixed
*/
public function get_response_str()
{
return json_encode($this->response);
}
/**
* 返回数组方便扩展
*
* @return mixed
*/
public function get_response()
{
return $this->response;
}
/**
* 正常模式获取验证结果
*
* @param $challenge
* @param $validate
* @param $seccode
* @param null $user_id
* @return int
*/
public function success_validate($challenge, $validate, $seccode, $user_id = null)
{
if (!$this->check_validate($challenge, $validate)) {
return 0;
}
$data = array(
8 years ago
'seccode' => $seccode,
'sdk' => self::GT_SDK_VERSION,
9 years ago
);
8 years ago
if (($user_id != null) and is_string($user_id)) {
$data['user_id'] = $user_id;
9 years ago
}
8 years ago
$url = 'http://api.geetest.com/validate.php';
9 years ago
$codevalidate = $this->post_request($url, $data);
if ($codevalidate == md5($seccode)) {
return 1;
}
8 years ago
if ($codevalidate == 'false') {
return 0;
}
return 0;
9 years ago
}
/**
* 宕机模式获取验证结果
*
* @param $challenge
* @param $validate
* @param $seccode
* @return int
*/
public function fail_validate($challenge, $validate, $seccode)
{
if ($validate) {
8 years ago
$value = explode('_', $validate);
8 years ago
$ans = $this->decode_response($challenge, $value['0']);
$bg_idx = $this->decode_response($challenge, $value['1']);
9 years ago
$grp_idx = $this->decode_response($challenge, $value['2']);
8 years ago
$x_pos = $this->get_failback_pic_ans($bg_idx, $grp_idx);
$answer = abs($ans - $x_pos);
9 years ago
if ($answer < 4) {
return 1;
}
8 years ago
9 years ago
return 0;
}
8 years ago
return 0;
9 years ago
}
/**
* @param $challenge
* @param $validate
* @return bool
*/
private function check_validate($challenge, $validate)
{
if (strlen($validate) != 32) {
return false;
}
if (md5($this->private_key . 'geetest' . $challenge) != $validate) {
return false;
}
return true;
}
/**
* GET 请求
*
* @param $url
* @return mixed|string
*/
private function send_request($url)
{
if (function_exists('curl_exec')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
curl_setopt($ch, CURLOPT_TIMEOUT, self::$socketTimeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
if (curl_errno($ch)) {
8 years ago
$err = sprintf('curl[%s] error[%s]', $url, curl_errno($ch) . ':' . curl_error($ch));
9 years ago
$this->triggerError($err);
}
curl_close($ch);
} else {
8 years ago
$opts = array(
9 years ago
'http' => array(
8 years ago
'method' => 'GET',
9 years ago
'timeout' => self::$connectTimeout + self::$socketTimeout,
)
);
$context = stream_context_create($opts);
8 years ago
$data = file_get_contents($url, false, $context);
9 years ago
}
return $data;
}
/**
*
* @param $url
* @param array $postdata
* @return mixed|string
*/
private function post_request($url, $postdata = '')
{
if (!$postdata) {
return false;
}
$data = http_build_query($postdata);
if (function_exists('curl_exec')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
curl_setopt($ch, CURLOPT_TIMEOUT, self::$socketTimeout);
//不可能执行到的代码
if (!$postdata) {
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
} else {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$data = curl_exec($ch);
if (curl_errno($ch)) {
8 years ago
$err = sprintf('curl[%s] error[%s]', $url, curl_errno($ch) . ':' . curl_error($ch));
9 years ago
$this->triggerError($err);
}
curl_close($ch);
8 years ago
} elseif ($postdata) {
$opts = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n" . 'Content-Length: ' . strlen($data) . "\r\n",
'content' => $data,
'timeout' => self::$connectTimeout + self::$socketTimeout
)
);
$context = stream_context_create($opts);
$data = file_get_contents($url, false, $context);
9 years ago
}
return $data;
}
/**
* 解码随机参数
*
* @param $challenge
* @param $string
* @return int
*/
private function decode_response($challenge, $string)
{
if (strlen($string) > 100) {
return 0;
}
8 years ago
$key = array();
$chongfu = array();
8 years ago
$shuzi = array('0' => 1, '1' => 2, '2' => 5, '3' => 10, '4' => 50);
8 years ago
$count = 0;
$res = 0;
9 years ago
$array_challenge = str_split($challenge);
8 years ago
$array_value = str_split($string);
8 years ago
for ($i = 0, $iMax = strlen($challenge); $i < $iMax; $i++) {
9 years ago
$item = $array_challenge[$i];
if (in_array($item, $chongfu)) {
continue;
}
8 years ago
$value = $shuzi[$count % 5];
$chongfu[] = $item;
$count++;
$key[$item] = $value;
9 years ago
}
8 years ago
for ($j = 0, $jMax = strlen($string); $j < $jMax; $j++) {
9 years ago
$res += $key[$array_value[$j]];
}
8 years ago
$res -= $this->decodeRandBase($challenge);
9 years ago
return $res;
}
/**
* @param $x_str
* @return int
*/
private function get_x_pos_from_str($x_str)
{
if (strlen($x_str) != 5) {
return 0;
}
8 years ago
$sum_val = 0;
9 years ago
$x_pos_sup = 200;
8 years ago
$sum_val = base_convert($x_str, 16, 10);
$result = $sum_val % $x_pos_sup;
$result = ($result < 40) ? 40 : $result;
9 years ago
return $result;
}
/**
* @param $full_bg_index
* @param $img_grp_index
* @return int
*/
private function get_failback_pic_ans($full_bg_index, $img_grp_index)
{
$full_bg_name = substr(md5($full_bg_index), 0, 9);
8 years ago
$bg_name = substr(md5($img_grp_index), 10, 9);
9 years ago
8 years ago
$answer_decode = '';
9 years ago
// 通过两个字符串奇数和偶数位拼接产生答案位
for ($i = 0; $i < 9; $i++) {
if ($i % 2 == 0) {
8 years ago
$answer_decode .= $full_bg_name[$i];
9 years ago
} elseif ($i % 2 == 1) {
8 years ago
$answer_decode .= $bg_name[$i];
9 years ago
}
}
$x_decode = substr($answer_decode, 4, 5);
8 years ago
return $this->get_x_pos_from_str($x_decode);
9 years ago
}
/**
* 输入的两位的随机数字,解码出偏移量
*
* @param $challenge
* @return mixed
*/
private function decodeRandBase($challenge)
{
8 years ago
$base = substr($challenge, 32, 2);
9 years ago
$tempArray = array();
8 years ago
for ($i = 0, $iMax = strlen($base); $i < $iMax; $i++) {
9 years ago
$tempAscii = ord($base[$i]);
8 years ago
$result = ($tempAscii > 57) ? ($tempAscii - 87) : ($tempAscii - 48);
8 years ago
$tempArray[] = $result;
9 years ago
}
8 years ago
return $tempArray['0'] * 36 + $tempArray['1'];
9 years ago
}
/**
* @param $err
*/
private function triggerError($err)
{
trigger_error($err);
}
}