1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Del\Form\Field\Captcha; |
4
|
|
|
|
5
|
|
|
use Del\Form\Validator\ValidatorInterface; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
class ReCaptchaAdapter implements CaptchaAdapterInterface, ValidatorInterface |
9
|
|
|
{ |
10
|
|
|
/** @var string $siteKey */ |
11
|
|
|
private $siteKey; |
12
|
|
|
|
13
|
|
|
/** @var string $secretKey */ |
14
|
|
|
private $secretKey; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ReCaptchaAdapter constructor. |
18
|
|
|
* @param string $siteKey |
19
|
|
|
* @param string $secretKey |
20
|
|
|
*/ |
21
|
|
|
public function __construct(string $siteKey, string $secretKey) |
22
|
|
|
{ |
23
|
|
|
$this->siteKey = $siteKey; |
24
|
|
|
$this->secretKey = $secretKey; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function generate(): string |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function render(): string |
33
|
|
|
{ |
34
|
|
|
return '<script src="https://www.google.com/recaptcha/api.js?render=' . $this->siteKey . '"></script> |
35
|
|
|
<script> |
36
|
|
|
$(document).ready(function(){ |
37
|
|
|
$(\'#captcha\').parent().parent().parent().hide(); |
38
|
|
|
grecaptcha.ready(function() { |
39
|
|
|
grecaptcha.execute(\'' . $this->siteKey . '\', {action: \'homepage\'}).then(function(token) { |
40
|
|
|
$(\'#captcha\').val(token); |
41
|
|
|
}); |
42
|
|
|
}); |
43
|
|
|
}); |
44
|
|
|
</script>'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function isValid($value) |
48
|
|
|
{ |
49
|
|
|
$verify = curl_init(); |
50
|
|
|
curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify"); |
51
|
|
|
curl_setopt($verify, CURLOPT_POST, true); |
52
|
|
|
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query([ |
53
|
|
|
'secret' => $this->secretKey, |
54
|
|
|
'response' => $value, |
55
|
|
|
])); |
56
|
|
|
curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false); |
57
|
|
|
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true); |
58
|
|
|
$json = curl_exec($verify); |
59
|
|
|
$data = json_decode($json, true); |
60
|
|
|
|
61
|
|
|
return $data['success']; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public function getMessages() |
68
|
|
|
{ |
69
|
|
|
return []; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
} |