1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the ReCaptcha Library. |
4
|
|
|
* |
5
|
|
|
* (c) Ilya Pokamestov <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DS\Library\ReCaptcha; |
12
|
|
|
|
13
|
|
|
use DS\Library\ReCaptcha\Http\Client\ClientInterface; |
14
|
|
|
use DS\Library\ReCaptcha\Http\Client\SimpleClient; |
15
|
|
|
use DS\Library\ReCaptcha\Http\Request; |
16
|
|
|
use DS\Library\ReCaptcha\Http\Stream; |
17
|
|
|
use DS\Library\ReCaptcha\Http\Uri; |
18
|
|
|
use Psr\Http\Message\RequestInterface; |
19
|
|
|
use Psr\Http\Message\ResponseInterface; |
20
|
|
|
use Psr\Http\Message\StreamInterface; |
21
|
|
|
use Psr\Http\Message\UriInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* ReCaptcha library, Google reCaptcha v2 client. |
25
|
|
|
* |
26
|
|
|
* @author Ilya Pokamestov <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class Client |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Google reCaptcha verify url. |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public static $siteVerifyUri = 'https://www.google.com/recaptcha/api/siteverify'; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
protected $secret; |
38
|
|
|
/** @var ClientInterface */ |
39
|
|
|
protected $httpClient; |
40
|
|
|
/** @var UriInterface */ |
41
|
|
|
protected $uri; |
42
|
|
|
/** @var RequestInterface */ |
43
|
|
|
protected $request; |
44
|
|
|
/** @var ResponseInterface */ |
45
|
|
|
protected $response; |
46
|
|
|
/** @var string */ |
47
|
|
|
protected $method = 'POST'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Client constructor. |
51
|
|
|
* @param string $secret |
52
|
|
|
* @param UriInterface|string|null $verifyUri |
53
|
|
|
* @param ClientInterface|null $httpClient |
54
|
|
|
* @param string $requestMethod |
55
|
|
|
*/ |
56
|
9 |
|
public function __construct($secret, $httpClient = null, $verifyUri = null, $requestMethod = 'POST') |
57
|
|
|
{ |
58
|
9 |
|
$this->secret = $secret; |
59
|
|
|
|
60
|
9 |
|
if (is_string($verifyUri)) { |
61
|
1 |
|
$this->uri = new Uri($verifyUri); |
62
|
9 |
|
} elseif ($verifyUri instanceof UriInterface) { |
63
|
1 |
|
$this->uri = $verifyUri; |
64
|
1 |
|
} else { |
65
|
8 |
|
$this->uri = new Uri(self::$siteVerifyUri); |
66
|
|
|
} |
67
|
|
|
|
68
|
9 |
|
$this->httpClient = (null === $httpClient) ? new SimpleClient() : $httpClient; |
69
|
9 |
|
$this->method = $requestMethod; |
70
|
9 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get reCaptcha secret key. |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
1 |
|
public function getSecret() |
78
|
|
|
{ |
79
|
1 |
|
return $this->secret; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $googleResponseToken |
84
|
|
|
* @param null|string $ip |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
1 |
|
public function validate($googleResponseToken, $ip = null) |
88
|
|
|
{ |
89
|
1 |
|
$this->request = new Request(); |
90
|
1 |
|
$this->request->withUri($this->uri) |
91
|
1 |
|
->withMethod($this->method) |
92
|
1 |
|
->withBody($this->createBody($googleResponseToken, $ip)) |
93
|
1 |
|
->withHeader('Content-type', 'application/x-www-form-urlencoded'); |
94
|
|
|
|
95
|
1 |
|
return $this->validateRequest($this->request); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $googleResponseToken |
100
|
|
|
* @param null|string $ip |
101
|
|
|
* @return StreamInterface |
102
|
|
|
*/ |
103
|
2 |
|
public function createBody($googleResponseToken, $ip = null) |
104
|
|
|
{ |
105
|
|
|
$parameters = array( |
106
|
2 |
|
'secret' => $this->secret, |
107
|
2 |
|
'response' => $googleResponseToken, |
108
|
2 |
|
); |
109
|
|
|
|
110
|
2 |
|
if (null !== $ip) { |
111
|
2 |
|
$parameters['remoteip'] = $ip; |
112
|
2 |
|
} |
113
|
|
|
|
114
|
2 |
|
return new Stream($parameters); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param RequestInterface $request |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
2 |
|
public function validateRequest(RequestInterface $request) |
122
|
|
|
{ |
123
|
2 |
|
$response = $this->send($request); |
124
|
|
|
|
125
|
2 |
|
return $this->isValid($response); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param ResponseInterface $response |
130
|
|
|
* @return bool |
131
|
|
|
* @throws ValidationException |
132
|
|
|
*/ |
133
|
5 |
|
public function isValid(ResponseInterface $response) |
134
|
|
|
{ |
135
|
5 |
|
$responseArray = json_decode($response->getBody()->getContents(), true); |
136
|
|
|
|
137
|
5 |
|
if (isset($responseArray['error-codes'])) { |
138
|
1 |
|
throw new ValidationException($responseArray['error-codes'], $response, $this->request); |
139
|
|
|
} |
140
|
|
|
|
141
|
4 |
|
if (isset($responseArray['success']) && $responseArray['success'] === true) { |
142
|
3 |
|
return true; |
143
|
|
|
} |
144
|
|
|
|
145
|
1 |
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Send verification request |
150
|
|
|
* |
151
|
|
|
* @param RequestInterface $request |
152
|
|
|
* @return ResponseInterface |
153
|
|
|
*/ |
154
|
4 |
|
public function send(RequestInterface $request) |
155
|
|
|
{ |
156
|
4 |
|
$this->request = $request; |
157
|
|
|
|
158
|
4 |
|
if ($this->httpClient instanceof ClientInterface || method_exists($this->httpClient, 'send')) { |
159
|
4 |
|
$this->response = $this->httpClient->send($request); |
160
|
4 |
|
} else { |
161
|
1 |
|
throw new \RuntimeException('HTTP Client must implement method send, and return ResponseInterface'); |
162
|
|
|
} |
163
|
|
|
|
164
|
4 |
|
return $this->response; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|