-
Notifications
You must be signed in to change notification settings - Fork 2
/
recaptcha.class.php
71 lines (63 loc) · 1.98 KB
/
recaptcha.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
class Recaptcha {
protected $form_id;
protected $sitekey;
private $secret;
public function __construct($form_id, $sitekey, $secret) {
$this->form_id = $form_id;
$this->sitekey = $sitekey;
$this->secret = $secret;
}
public function get_response() {
if(!empty($_POST['g-recaptcha-response'])) {
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
'secret' => $this->secret,
'response' => $_POST['g-recaptcha-response'],
'remoteip' => (!empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '')
);
$options = array (
'http' => array (
'header' => 'Content-type: application/x-www-form-urlencoded\r\n',
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = json_decode(file_get_contents($url, false, $context));
return $result;
} else {
return false;
}
}
public function success() {
return $this->get_response()->success;
}
public function script() {
ob_start();
?>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
window.onload = function() {
var element = document.getElementById('submit<?php echo $this->form_id; ?>Form');
if(element) {
element.onclick = function(e) {
e.preventDefault();
grecaptcha.execute();
};
}
};
function onSubmitCaptchaForm<?php echo $this->form_id; ?>(token) {
document.getElementById('<?php echo $this->form_id; ?>').submit();
};
</script>
<?php
return ob_get_clean();
}
public function button($value = "Submit") {
$html = '<button id="submit'. $this->form_id .'Form">'. $value .'</button>'."\n";
$html .= '<div id="recaptcha" class="g-recaptcha" data-sitekey="'. $this->sitekey .'" data-badge="inline" data-callback="onSubmitCaptchaForm'. $this->form_id .'" data-size="invisible"></div>'."\n";
return $html;
}
}
?>