GeetestLib.php 7.1 KB

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