· 6 years ago · Oct 10, 2019, 10:38 AM
1<?php
2
3public function validate_recaptcha($ver)
4 {
5 $error_str = '';
6
7 $captcha = filter_input(INPUT_POST, 'g-recaptcha-response', FILTER_SANITIZE_STRING);
8// debug($captcha, 1);
9 if (!$captcha) {
10 $error_str = $this->lang->get_str('captcha_error');
11 }
12
13 if ($ver == 2) {
14 $secretKey = $this->config['recaptcha_key_v2'];
15 }
16 else {
17 $secretKey = $this->config['recaptcha_key_v3'];
18 }
19
20 $url = 'https://www.google.com/recaptcha/api/siteverify';
21 $data = ['secret' => $secretKey, 'response' => $captcha];
22
23// debug($data);
24
25 $options = [
26 'http' => [
27 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
28 'method' => 'POST',
29 'content' => http_build_query($data),
30 ],
31 ];
32 $context = stream_context_create($options);
33 $result = file_get_contents($url, false, $context);
34// debug($result);
35 $responseKeys = json_decode($result, true);
36 if (!$responseKeys["success"]) {
37 $error_str = $this->lang->get_str('captcha_error');
38 }
39
40 return $error_str;
41 }