|
1
|
|
|
<?php |
|
2
|
|
|
namespace ReCaptcha2\Captcha; |
|
3
|
|
|
|
|
4
|
|
|
use Traversable; |
|
5
|
|
|
use Zend\Captcha\Exception; |
|
6
|
|
|
use Zend\Http\Client as HttpClient; |
|
7
|
|
|
|
|
8
|
|
|
class NoCaptchaService extends AbstractService |
|
9
|
|
|
{ |
|
10
|
|
|
const API_SERVER = 'https://www.google.com/recaptcha/api'; |
|
11
|
|
|
const VERIFY_SERVER = 'https://www.google.com/recaptcha/api/siteverify'; |
|
12
|
|
|
const CACERT_PATH = __DIR__ . '/../../ssl/cacert.pem'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $ip; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $params = [ |
|
23
|
|
|
'onload' => null, |
|
24
|
|
|
'render' => null, |
|
25
|
|
|
'hl' => null, |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* |
|
30
|
|
|
* @var HttpClient |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $httpClient; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param array|Traversable $options |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($options = []) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->setOptions($options); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getServiceUri() |
|
46
|
|
|
{ |
|
47
|
|
|
return self::API_SERVER; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return HttpClient |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getHttpClient() |
|
54
|
|
|
{ |
|
55
|
|
|
if (null === $this->httpClient) { |
|
56
|
|
|
$this->httpClient = new HttpClient; |
|
57
|
|
|
} |
|
58
|
|
|
return $this->httpClient; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param array|Traversable|HttpClient $httpClient |
|
63
|
|
|
* @return NoCaptchaService |
|
64
|
|
|
*/ |
|
65
|
|
|
public function setHttpClient($httpClient) |
|
66
|
|
|
{ |
|
67
|
|
|
$clientOptions = []; |
|
68
|
|
|
if (is_array($httpClient)) { |
|
69
|
|
|
$clientOptions = isset($httpClient['options']) ? $httpClient['options'] : []; |
|
70
|
|
|
$httpClient = isset($httpClient['class']) ? $httpClient['class'] : HttpClient::class; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (is_string($httpClient)) { |
|
74
|
|
|
if (!class_exists($httpClient)) { |
|
75
|
|
|
throw new Exception\InvalidArgumentException(sprintf( |
|
76
|
|
|
'Unable to locate HttpClient class "%s"', |
|
77
|
|
|
$httpClient |
|
78
|
|
|
)); |
|
79
|
|
|
} |
|
80
|
|
|
$httpClient = new $httpClient(null, $clientOptions); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (!$httpClient instanceof HttpClient) { |
|
84
|
|
|
throw new Exception\DomainException(sprintf( |
|
85
|
|
|
'%s expects a valid implementation of Zend\Http\Client; received "%s"', |
|
86
|
|
|
__METHOD__, |
|
87
|
|
|
(is_object($httpClient) ? get_class($httpClient) : gettype($httpClient)) |
|
88
|
|
|
)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$this->httpClient = $httpClient; |
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getIp() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->ip; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $ip |
|
105
|
|
|
* @return NoCaptchaService |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setIp($ip) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->ip = $ip; |
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param array $params |
|
115
|
|
|
* @return NoCaptchaService |
|
116
|
|
|
*/ |
|
117
|
|
|
public function setParams(array $params) |
|
118
|
|
|
{ |
|
119
|
|
|
foreach ($this->params as $key => $param) { |
|
120
|
|
|
if (array_key_exists($key, $params)) { |
|
121
|
|
|
$this->params[$key] = $params[$key]; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param array|Traversable $options |
|
129
|
|
|
* @throws Exception\InvalidArgumentException |
|
130
|
|
|
* @return NoCaptchaService |
|
131
|
|
|
*/ |
|
132
|
|
|
public function setOptions($options = []) |
|
133
|
|
|
{ |
|
134
|
|
|
if (!is_array($options) && !$options instanceof Traversable) { |
|
135
|
|
|
throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable'); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
foreach ($options as $name => $option) { |
|
139
|
|
|
$fname = 'set' . ucfirst($name); |
|
140
|
|
|
if (($fname != 'setOptions') && method_exists($this, $fname)) { |
|
141
|
|
|
$this->{$fname}($option); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $responseField |
|
150
|
|
|
* @return \Zend\Http\Response |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function post($responseField) |
|
153
|
|
|
{ |
|
154
|
|
|
if ($this->secretKey === null) { |
|
155
|
|
|
throw new Exception\DomainException('Missing secret key'); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/* Fetch an instance of the http client */ |
|
159
|
|
|
$httpClient = $this->getHttpClient(); |
|
160
|
|
|
|
|
161
|
|
|
$params = [ |
|
162
|
|
|
'secret' => $this->secretKey, |
|
163
|
|
|
'response' => $responseField, |
|
164
|
|
|
]; |
|
165
|
|
|
if (null !== $this->ip) { |
|
166
|
|
|
$params['remoteip'] = $this->ip; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$request = new \Zend\Http\Request; |
|
170
|
|
|
$request->setUri(self::VERIFY_SERVER); |
|
171
|
|
|
$request->getPost()->fromArray($params); |
|
172
|
|
|
$request->setMethod(\Zend\Http\Request::METHOD_POST); |
|
173
|
|
|
$httpClient->setEncType(HttpClient::ENC_URLENCODED); |
|
174
|
|
|
|
|
175
|
|
|
return $httpClient->send($request); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param string $value |
|
180
|
|
|
* @return \ReCaptcha2\Captcha\Result |
|
181
|
|
|
*/ |
|
182
|
|
|
public function verify($value) |
|
183
|
|
|
{ |
|
184
|
|
|
$response = $this->post($value); |
|
185
|
|
|
return new Result($response); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|