class.geetestlib.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * 极验行为式验证安全平台,php 网站主后台包含的库文件
  4. *
  5. * @author Tanxu
  6. */
  7. class GeetestLib {
  8. const GT_SDK_VERSION = 'php_3.0.0';
  9. public static $connectTimeout = 1;
  10. public static $socketTimeout = 1;
  11. private $response;
  12. public function __construct($captcha_id, $private_key) {
  13. $this->captcha_id = $captcha_id;
  14. $this->private_key = $private_key;
  15. }
  16. /**
  17. * 判断极验服务器是否down机
  18. *
  19. * @param array $data
  20. * @return int
  21. */
  22. public function pre_process($param, $new_captcha=1) {
  23. $data = array('gt'=>$this->captcha_id,
  24. 'new_captcha'=>$new_captcha
  25. );
  26. $data = array_merge($data,$param);
  27. $query = http_build_query($data);
  28. $url = "http://api.geetest.com/register.php?" . $query;
  29. $challenge = $this->send_request($url);
  30. if (strlen($challenge) != 32) {
  31. $this->failback_process();
  32. return 0;
  33. }
  34. $this->success_process($challenge);
  35. return 1;
  36. }
  37. /**
  38. * @param $challenge
  39. */
  40. private function success_process($challenge) {
  41. $challenge = md5($challenge . $this->private_key);
  42. $result = array(
  43. 'success' => 1,
  44. 'gt' => $this->captcha_id,
  45. 'challenge' => $challenge,
  46. 'new_captcha'=>1
  47. );
  48. $this->response = $result;
  49. }
  50. /**
  51. *
  52. */
  53. private function failback_process() {
  54. $rnd1 = md5(rand(0, 100));
  55. $rnd2 = md5(rand(0, 100));
  56. $challenge = $rnd1 . substr($rnd2, 0, 2);
  57. $result = array(
  58. 'success' => 0,
  59. 'gt' => $this->captcha_id,
  60. 'challenge' => $challenge,
  61. 'new_captcha'=>1
  62. );
  63. $this->response = $result;
  64. }
  65. /**
  66. * @return mixed
  67. */
  68. public function get_response_str() {
  69. return json_encode($this->response);
  70. }
  71. /**
  72. * 返回数组方便扩展
  73. *
  74. * @return mixed
  75. */
  76. public function get_response() {
  77. return $this->response;
  78. }
  79. /**
  80. * 正常模式获取验证结果
  81. *
  82. * @param string $challenge
  83. * @param string $validate
  84. * @param string $seccode
  85. * @param array $param
  86. * @return int
  87. */
  88. public function success_validate($challenge, $validate, $seccode,$param, $json_format=1) {
  89. if (!$this->check_validate($challenge, $validate)) {
  90. return 0;
  91. }
  92. $query = array(
  93. "seccode" => $seccode,
  94. "timestamp"=>time(),
  95. "challenge"=>$challenge,
  96. "captchaid"=>$this->captcha_id,
  97. "json_format"=>$json_format,
  98. "sdk" => self::GT_SDK_VERSION
  99. );
  100. $query = array_merge($query,$param);
  101. $url = "http://api.geetest.com/validate.php";
  102. $codevalidate = $this->post_request($url, $query);
  103. $obj = json_decode($codevalidate,true);
  104. if ($obj === false){
  105. return 0;
  106. }
  107. if ($obj['seccode'] == md5($seccode)) {
  108. return 1;
  109. } else {
  110. return 0;
  111. }
  112. }
  113. /**
  114. * 宕机模式获取验证结果
  115. *
  116. * @param $challenge
  117. * @param $validate
  118. * @param $seccode
  119. * @return int
  120. */
  121. public function fail_validate($challenge, $validate, $seccode) {
  122. if(md5($challenge) == $validate){
  123. return 1;
  124. }else{
  125. return 0;
  126. }
  127. }
  128. /**
  129. * @param $challenge
  130. * @param $validate
  131. * @return bool
  132. */
  133. private function check_validate($challenge, $validate) {
  134. if (strlen($validate) != 32) {
  135. return false;
  136. }
  137. if (md5($this->private_key . 'geetest' . $challenge) != $validate) {
  138. return false;
  139. }
  140. return true;
  141. }
  142. /**
  143. * GET 请求
  144. *
  145. * @param $url
  146. * @return mixed|string
  147. */
  148. private function send_request($url) {
  149. if (function_exists('curl_exec')) {
  150. $ch = curl_init();
  151. curl_setopt($ch, CURLOPT_URL, $url);
  152. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
  153. curl_setopt($ch, CURLOPT_TIMEOUT, self::$socketTimeout);
  154. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  155. $data = curl_exec($ch);
  156. $curl_errno = curl_errno($ch);
  157. curl_close($ch);
  158. if ($curl_errno >0) {
  159. return 0;
  160. }else{
  161. return $data;
  162. }
  163. } else {
  164. $opts = array(
  165. 'http' => array(
  166. 'method' => "GET",
  167. 'timeout' => self::$connectTimeout + self::$socketTimeout,
  168. )
  169. );
  170. $context = stream_context_create($opts);
  171. $data = @file_get_contents($url, false, $context);
  172. if($data){
  173. return $data;
  174. }else{
  175. return 0;
  176. }
  177. }
  178. }
  179. /**
  180. *
  181. * @param $url
  182. * @param array $postdata
  183. * @return mixed|string
  184. */
  185. private function post_request($url, $postdata = '') {
  186. if (!$postdata) {
  187. return false;
  188. }
  189. $data = http_build_query($postdata);
  190. if (function_exists('curl_exec')) {
  191. $ch = curl_init();
  192. curl_setopt($ch, CURLOPT_URL, $url);
  193. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  194. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
  195. curl_setopt($ch, CURLOPT_TIMEOUT, self::$socketTimeout);
  196. //不可能执行到的代码
  197. if (!$postdata) {
  198. curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  199. } else {
  200. curl_setopt($ch, CURLOPT_POST, 1);
  201. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  202. }
  203. $data = curl_exec($ch);
  204. if (curl_errno($ch)) {
  205. $err = sprintf("curl[%s] error[%s]", $url, curl_errno($ch) . ':' . curl_error($ch));
  206. $this->triggerError($err);
  207. }
  208. curl_close($ch);
  209. } else {
  210. if ($postdata) {
  211. $opts = array(
  212. 'http' => array(
  213. 'method' => 'POST',
  214. 'header' => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Length: " . strlen($data) . "\r\n",
  215. 'content' => $data,
  216. 'timeout' => self::$connectTimeout + self::$socketTimeout
  217. )
  218. );
  219. $context = stream_context_create($opts);
  220. $data = file_get_contents($url, false, $context);
  221. }
  222. }
  223. return $data;
  224. }
  225. /**
  226. * @param $err
  227. */
  228. private function triggerError($err) {
  229. trigger_error($err);
  230. }
  231. }