1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2019. Volodymyr Hryvinskyi. All rights reserved. |
4
|
|
|
* @author: <mailto:[email protected]> |
5
|
|
|
* @github: <https://github.com/hryvinskyi> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Hryvinskyi\InvisibleCaptcha\Model\ReCaptcha; |
9
|
|
|
|
10
|
|
|
use Hryvinskyi\Base\Helper\ArrayHelper; |
11
|
|
|
use Hryvinskyi\Base\Helper\Json; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The response returned from the service. |
15
|
|
|
*/ |
16
|
|
|
class Response |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Success or failure. |
20
|
|
|
* @var boolean |
21
|
|
|
*/ |
22
|
|
|
private $success = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Error code strings. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $errorCodes = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The hostname of the site where the reCAPTCHA was solved. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $hostname; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ) |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $challengeTs; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Score assigned to the request |
47
|
|
|
* |
48
|
|
|
* @var float |
49
|
|
|
*/ |
50
|
|
|
private $score; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Action as specified by the page |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $action; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Build the response from the expected JSON returned by the service. |
61
|
|
|
* |
62
|
|
|
* @param string $json |
63
|
|
|
* |
64
|
|
|
* @return Response |
65
|
|
|
*/ |
66
|
|
|
public static function fromJson($json): Response |
67
|
|
|
{ |
68
|
|
|
$responseData = Json::decode($json); |
69
|
|
|
|
70
|
|
|
if (!$responseData) { |
71
|
|
|
return new Response(false, [VerifyReCaptcha::E_INVALID_JSON]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$hostname = ArrayHelper::getValue($responseData, 'hostname'); |
75
|
|
|
$challengeTs = ArrayHelper::getValue($responseData, 'challenge_ts'); |
76
|
|
|
$score = floatval(ArrayHelper::getValue($responseData, 'score')); |
77
|
|
|
$action = ArrayHelper::getValue($responseData, 'action'); |
78
|
|
|
$success = ArrayHelper::getValue($responseData, 'success'); |
79
|
|
|
$errorCodes = ArrayHelper::getValue($responseData, 'error-codes'); |
80
|
|
|
|
81
|
|
|
if ($success == true) { |
82
|
|
|
return new Response(true, [], $hostname, $challengeTs, $score, $action); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($errorCodes && is_array($errorCodes)) { |
86
|
|
|
return new Response( |
87
|
|
|
false, |
88
|
|
|
$responseData['error-codes'], |
89
|
|
|
$hostname, |
90
|
|
|
$challengeTs, |
91
|
|
|
$score, |
92
|
|
|
$action |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return new Response( |
97
|
|
|
false, |
98
|
|
|
[VerifyReCaptcha::E_UNKNOWN_ERROR], |
99
|
|
|
$hostname, |
100
|
|
|
$challengeTs, |
101
|
|
|
$score, |
102
|
|
|
$action |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Response constructor. |
108
|
|
|
* |
109
|
|
|
* @param bool $success |
110
|
|
|
* @param array $errorCodes |
111
|
|
|
* @param string|null $hostname |
112
|
|
|
* @param string|null $challengeTs |
113
|
|
|
* @param float|null $score |
114
|
|
|
* @param string|null $action |
115
|
|
|
*/ |
116
|
|
|
public function __construct( |
117
|
|
|
bool $success, |
118
|
|
|
array $errorCodes = [], |
119
|
|
|
?string $hostname = null, |
120
|
|
|
?string $challengeTs = null, |
121
|
|
|
?float $score = null, |
122
|
|
|
?string $action = null |
123
|
|
|
) { |
124
|
|
|
$this->success = $success; |
125
|
|
|
$this->hostname = $hostname; |
126
|
|
|
$this->challengeTs = $challengeTs; |
127
|
|
|
$this->score = $score; |
128
|
|
|
$this->action = $action; |
129
|
|
|
$this->errorCodes = $errorCodes; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Is success? |
134
|
|
|
* |
135
|
|
|
* @return boolean |
136
|
|
|
*/ |
137
|
|
|
public function isSuccess(): bool |
138
|
|
|
{ |
139
|
|
|
return $this->success; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get error codes. |
144
|
|
|
* |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
public function getErrorCodes(): array |
148
|
|
|
{ |
149
|
|
|
return $this->errorCodes; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get hostname. |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function getHostname(): ?string |
158
|
|
|
{ |
159
|
|
|
return $this->hostname; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get challenge timestamp |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
public function getChallengeTs(): ?string |
168
|
|
|
{ |
169
|
|
|
return $this->challengeTs; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get score |
174
|
|
|
* |
175
|
|
|
* @return float |
176
|
|
|
*/ |
177
|
|
|
public function getScore(): ?float |
178
|
|
|
{ |
179
|
|
|
return $this->score; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get action |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function getAction(): ?string |
188
|
|
|
{ |
189
|
|
|
return $this->action; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return array |
194
|
|
|
*/ |
195
|
|
|
public function toArray(): array |
196
|
|
|
{ |
197
|
|
|
return [ |
198
|
|
|
'success' => $this->isSuccess(), |
199
|
|
|
'hostname' => $this->getHostname(), |
200
|
|
|
'challenge_ts' => $this->getChallengeTs(), |
201
|
|
|
'score' => $this->getScore(), |
202
|
|
|
'action' => $this->getAction(), |
203
|
|
|
'error-codes' => $this->getErrorCodes(), |
204
|
|
|
]; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|