@@ -125,7 +125,7 @@ public function __construct($secret, RequestMethod $requestMethod = null)
125
125
126
126
/**
127
127
* Calls the reCAPTCHA siteverify API to verify whether the user passes
128
- * CAPTCHA test.
128
+ * CAPTCHA test and additionally runs any specified additional checks
129
129
*
130
130
* @param string $response The user response token provided by reCAPTCHA, verifying the user on your site.
131
131
* @param string $remoteIp The end user's IP address.
@@ -141,11 +141,50 @@ public function verify($response, $remoteIp = null)
141
141
142
142
$ params = new RequestParameters ($ this ->secret , $ response , $ remoteIp , self ::VERSION );
143
143
$ rawResponse = $ this ->requestMethod ->submit ($ params );
144
- return Response::fromJson ($ rawResponse );
144
+ $ initialResponse = Response::fromJson ($ rawResponse );
145
+ $ validationErrors = array ();
146
+
147
+ if (isset ($ this ->hostname ) && strcasecmp ($ this ->hostname , $ initialResponse ->getHostname ()) !== 0 ) {
148
+ $ validationErrors [] = self ::E_HOSTNAME_MISMATCH ;
149
+ }
150
+
151
+ if (isset ($ this ->apkPackageName ) && strcasecmp ($ this ->apkPackageName , $ initialResponse ->getApkPackageName ()) !== 0 ) {
152
+ $ validationErrors [] = self ::E_APK_PACKAGE_NAME_MISMATCH ;
153
+ }
154
+
155
+ if (isset ($ this ->action ) && strcasecmp ($ this ->action , $ initialResponse ->getAction ()) !== 0 ) {
156
+ $ validationErrors [] = self ::E_ACTION_MISMATCH ;
157
+ }
158
+
159
+ if (isset ($ this ->threshold ) && $ this ->threshold > $ initialResponse ->getScore ()) {
160
+ $ validationErrors [] = self ::E_SCORE_THRESHOLD_NOT_MET ;
161
+ }
162
+
163
+ if (isset ($ this ->timeoutSeconds )) {
164
+ $ challengeTs = strtotime ($ initialResponse ->getChallengeTs ());
165
+
166
+ if ($ challengeTs > 0 && time () - $ challengeTs > $ this ->timeoutSeconds ) {
167
+ $ validationErrors [] = self ::E_CHALLENGE_TIMEOUT ;
168
+ }
169
+ }
170
+
171
+ if (empty ($ validationErrors )) {
172
+ return $ initialResponse ;
173
+ }
174
+
175
+ return new Response (
176
+ false ,
177
+ array_merge ($ initialResponse ->getErrorCodes (), $ validationErrors ),
178
+ $ initialResponse ->getHostname (),
179
+ $ initialResponse ->getChallengeTs (),
180
+ $ initialResponse ->getApkPackageName (),
181
+ $ initialResponse ->getScore (),
182
+ $ initialResponse ->getAction ()
183
+ );
145
184
}
146
185
147
186
/**
148
- * Provide a hostname to match against in verifyAndValidate ()
187
+ * Provide a hostname to match against in verify ()
149
188
* This should be without a protocol or trailing slash, e.g. www.google.com
150
189
*
151
190
* @param string $hostname Expected hostname
@@ -158,7 +197,7 @@ public function setExpectedHostname($hostname)
158
197
}
159
198
160
199
/**
161
- * Provide an APK package name to match against in verifyAndValidate ()
200
+ * Provide an APK package name to match against in verify ()
162
201
*
163
202
* @param string $apkPackageName Expected APK package name
164
203
* @return ReCaptcha Current instance for fluent interface
@@ -170,7 +209,7 @@ public function setExpectedApkPackageName($apkPackageName)
170
209
}
171
210
172
211
/**
173
- * Provide an action to match against in verifyAndValidate ()
212
+ * Provide an action to match against in verify ()
174
213
* This should be set per page.
175
214
*
176
215
* @param string $action Expected action
@@ -183,7 +222,7 @@ public function setExpectedAction($action)
183
222
}
184
223
185
224
/**
186
- * Provide a threshold to meet or exceed in verifyAndValidate ()
225
+ * Provide a threshold to meet or exceed in verify ()
187
226
* Threshold should be a float between 0 and 1 which will be tested as response >= threshold.
188
227
*
189
228
* @param float $threshold Expected threshold
@@ -196,7 +235,7 @@ public function setScoreThreshold($threshold)
196
235
}
197
236
198
237
/**
199
- * Provide a timeout in seconds to test against the challenge timestamp in verifyAndValidate ()
238
+ * Provide a timeout in seconds to test against the challenge timestamp in verify ()
200
239
*
201
240
* @param int $timeoutSeconds Expected hostname
202
241
* @return ReCaptcha Current instance for fluent interface
@@ -206,56 +245,4 @@ public function setChallengeTimeout($timeoutSeconds)
206
245
$ this ->timeoutSeconds = $ timeoutSeconds ;
207
246
return $ this ;
208
247
}
209
-
210
- /**
211
- * Calls the reCAPTCHA siteverify API to verify whether the user passes
212
- * CAPTCHA test and additionally runs any specified additional checks
213
- *
214
- * @param string $response The user response token provided by reCAPTCHA, verifying the user on your site.
215
- * @param string $remoteIp The end user's IP address.
216
- * @return Response Response from the service.
217
- */
218
- public function verifyAndValidate ($ response , $ remoteIp = null )
219
- {
220
- $ initialResponse = $ this ->verify ($ response , $ remoteIp );
221
- $ validationErrors = array ();
222
-
223
- if (isset ($ this ->hostname ) && strcasecmp ($ this ->hostname , $ initialResponse ->getHostname ()) !== 0 ) {
224
- $ validationErrors [] = self ::E_HOSTNAME_MISMATCH ;
225
- }
226
-
227
- if (isset ($ this ->apkPackageName ) && strcasecmp ($ this ->apkPackageName , $ initialResponse ->getApkPackageName ()) !== 0 ) {
228
- $ validationErrors [] = self ::E_APK_PACKAGE_NAME_MISMATCH ;
229
- }
230
-
231
- if (isset ($ this ->action ) && strcasecmp ($ this ->action , $ initialResponse ->getAction ()) !== 0 ) {
232
- $ validationErrors [] = self ::E_ACTION_MISMATCH ;
233
- }
234
-
235
- if (isset ($ this ->threshold ) && $ this ->threshold > $ initialResponse ->getScore ()) {
236
- $ validationErrors [] = self ::E_SCORE_THRESHOLD_NOT_MET ;
237
- }
238
-
239
- if (isset ($ this ->timeoutSeconds )) {
240
- $ challengeTs = strtotime ($ initialResponse ->getChallengeTs ());
241
-
242
- if ($ challengeTs > 0 && time () - $ challengeTs > $ this ->timeoutSeconds ) {
243
- $ validationErrors [] = self ::E_CHALLENGE_TIMEOUT ;
244
- }
245
- }
246
-
247
- if (empty ($ validationErrors )) {
248
- return $ initialResponse ;
249
- }
250
-
251
- return new Response (
252
- false ,
253
- array_merge ($ initialResponse ->getErrorCodes (), $ validationErrors ),
254
- $ initialResponse ->getHostname (),
255
- $ initialResponse ->getChallengeTs (),
256
- $ initialResponse ->getApkPackageName (),
257
- $ initialResponse ->getScore (),
258
- $ initialResponse ->getAction ()
259
- );
260
- }
261
248
}
0 commit comments