|
1
|
|
|
<?php namespace Arcanedev\NoCaptcha\Utilities; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
4
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
|
5
|
|
|
use JsonSerializable; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class AbstractResponse |
|
9
|
|
|
* |
|
10
|
|
|
* @package Arcanedev\NoCaptcha\Utilities |
|
11
|
|
|
* @author ARCANEDEV <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class AbstractResponse implements Arrayable, Jsonable, JsonSerializable |
|
14
|
|
|
{ |
|
15
|
|
|
/* ----------------------------------------------------------------- |
|
16
|
|
|
| Constants |
|
17
|
|
|
| ----------------------------------------------------------------- |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Did not receive a 200 from the service |
|
22
|
|
|
*/ |
|
23
|
|
|
const E_BAD_RESPONSE = 'bad-response'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* ReCAPTCHA response not provided |
|
27
|
|
|
*/ |
|
28
|
|
|
const E_MISSING_INPUT_RESPONSE = 'missing-input-response'; |
|
29
|
|
|
|
|
30
|
|
|
/* ----------------------------------------------------------------- |
|
31
|
|
|
| Properties |
|
32
|
|
|
| ----------------------------------------------------------------- |
|
33
|
|
|
*/ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Success or failure. |
|
37
|
|
|
* |
|
38
|
|
|
* @var boolean |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $success = false; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Error code strings. |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $errorCodes = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The hostname of the site where the reCAPTCHA was solved. |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $hostname; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ) |
|
57
|
|
|
* |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $challengeTs; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* APK package name |
|
64
|
|
|
* |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $apkPackageName; |
|
68
|
|
|
|
|
69
|
|
|
/* ----------------------------------------------------------------- |
|
70
|
|
|
| Constructor |
|
71
|
|
|
| ----------------------------------------------------------------- |
|
72
|
|
|
*/ |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Response constructor. |
|
76
|
|
|
* |
|
77
|
|
|
* @param bool $success |
|
78
|
|
|
* @param array $errorCodes |
|
79
|
|
|
* @param string|null $hostname |
|
80
|
|
|
* @param string|null $challengeTs |
|
81
|
|
|
* @param string|null $apkPackageName |
|
82
|
|
|
* @param float|null $score |
|
83
|
|
|
* @param string|null $action |
|
84
|
|
|
*/ |
|
85
|
100 |
|
public function __construct($success, array $errorCodes = [], $hostname = null, $challengeTs = null, $apkPackageName = null, $score = null, $action = null) |
|
86
|
|
|
{ |
|
87
|
100 |
|
$this->success = $success; |
|
88
|
100 |
|
$this->errorCodes = $errorCodes; |
|
89
|
100 |
|
$this->hostname = $hostname; |
|
90
|
100 |
|
$this->challengeTs = $challengeTs; |
|
91
|
100 |
|
$this->apkPackageName = $apkPackageName; |
|
92
|
100 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/* ----------------------------------------------------------------- |
|
95
|
|
|
| Getters |
|
96
|
|
|
| ----------------------------------------------------------------- |
|
97
|
|
|
*/ |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get error codes. |
|
101
|
|
|
* |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
28 |
|
public function getErrorCodes() |
|
105
|
|
|
{ |
|
106
|
28 |
|
return $this->errorCodes; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get hostname. |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
32 |
|
public function getHostname() |
|
115
|
|
|
{ |
|
116
|
32 |
|
return $this->hostname; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get challenge timestamp |
|
121
|
|
|
* |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
24 |
|
public function getChallengeTs() |
|
125
|
|
|
{ |
|
126
|
24 |
|
return $this->challengeTs; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get APK package name |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
24 |
|
public function getApkPackageName() |
|
135
|
|
|
{ |
|
136
|
24 |
|
return $this->apkPackageName; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/* ----------------------------------------------------------------- |
|
140
|
|
|
| Main Methods |
|
141
|
|
|
| ----------------------------------------------------------------- |
|
142
|
|
|
*/ |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Build the response from the expected JSON returned by the service. |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $json |
|
148
|
|
|
* |
|
149
|
|
|
* @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse |
|
150
|
|
|
*/ |
|
151
|
44 |
|
public static function fromJson($json) |
|
152
|
|
|
{ |
|
153
|
44 |
|
$responseData = json_decode($json, true); |
|
154
|
|
|
|
|
155
|
44 |
|
if ( ! $responseData) |
|
156
|
4 |
|
return new static(false, [ResponseV3::E_INVALID_JSON]); |
|
157
|
|
|
|
|
158
|
40 |
|
return static::fromArray($responseData); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Build the response from an array. |
|
163
|
|
|
* |
|
164
|
|
|
* @param array $array |
|
165
|
|
|
* |
|
166
|
|
|
* @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse|mixed |
|
167
|
|
|
*/ |
|
168
|
|
|
abstract public static function fromArray(array $array); |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Convert the response object to array. |
|
172
|
|
|
* |
|
173
|
|
|
* @return array |
|
174
|
|
|
*/ |
|
175
|
|
|
abstract public function toArray(); |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Convert the object to its JSON representation. |
|
179
|
|
|
* |
|
180
|
|
|
* @param int $options |
|
181
|
|
|
* |
|
182
|
|
|
* @return string |
|
183
|
|
|
*/ |
|
184
|
8 |
|
public function toJson($options = 0) |
|
185
|
|
|
{ |
|
186
|
8 |
|
return json_encode($this->jsonSerialize(), $options); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Convert the response object to array. |
|
191
|
|
|
* |
|
192
|
|
|
* @return array |
|
193
|
|
|
*/ |
|
194
|
8 |
|
public function jsonSerialize() |
|
195
|
|
|
{ |
|
196
|
8 |
|
return $this->toArray(); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/* ----------------------------------------------------------------- |
|
200
|
|
|
| Check Methods |
|
201
|
|
|
| ----------------------------------------------------------------- |
|
202
|
|
|
*/ |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Check if the response is successful. |
|
206
|
|
|
* |
|
207
|
|
|
* @return bool |
|
208
|
|
|
*/ |
|
209
|
84 |
|
public function isSuccess() |
|
210
|
|
|
{ |
|
211
|
84 |
|
return $this->success === true; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Check the hostname. |
|
216
|
|
|
* |
|
217
|
|
|
* @param string $hostname |
|
218
|
|
|
* |
|
219
|
|
|
* @return bool |
|
220
|
|
|
*/ |
|
221
|
8 |
|
public function isHostname($hostname) |
|
222
|
|
|
{ |
|
223
|
8 |
|
return $this->getHostname() === $hostname; |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|