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