Révision e326068a
Ajouté par Assos Assos il y a plus de 5 ans
drupal7/sites/all/modules/recaptcha/recaptcha-php/src/ReCaptcha/ReCaptcha.php | ||
---|---|---|
3 | 3 |
* This is a PHP library that handles calling reCAPTCHA. |
4 | 4 |
* |
5 | 5 |
* @copyright Copyright (c) 2015, Google Inc. |
6 |
* @link http://www.google.com/recaptcha |
|
6 |
* @link https://www.google.com/recaptcha
|
|
7 | 7 |
* |
8 | 8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | 9 |
* of this software and associated documentation files (the "Software"), to deal |
... | ... | |
35 | 35 |
* Version of this client library. |
36 | 36 |
* @const string |
37 | 37 |
*/ |
38 |
const VERSION = 'php_1.1.2'; |
|
38 |
const VERSION = 'php_1.2.1'; |
|
39 |
|
|
40 |
/** |
|
41 |
* URL for reCAPTCHA sitevrerify API |
|
42 |
* @const string |
|
43 |
*/ |
|
44 |
const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; |
|
45 |
|
|
46 |
/** |
|
47 |
* Invalid JSON received |
|
48 |
* @const string |
|
49 |
*/ |
|
50 |
const E_INVALID_JSON = 'invalid-json'; |
|
51 |
|
|
52 |
/** |
|
53 |
* Could not connect to service |
|
54 |
* @const string |
|
55 |
*/ |
|
56 |
const E_CONNECTION_FAILED = 'connection-failed'; |
|
57 |
|
|
58 |
/** |
|
59 |
* Did not receive a 200 from the service |
|
60 |
* @const string |
|
61 |
*/ |
|
62 |
const E_BAD_RESPONSE = 'bad-response'; |
|
63 |
|
|
64 |
/** |
|
65 |
* Not a success, but no error codes received! |
|
66 |
* @const string |
|
67 |
*/ |
|
68 |
const E_UNKNOWN_ERROR = 'unknown-error'; |
|
69 |
|
|
70 |
/** |
|
71 |
* ReCAPTCHA response not provided |
|
72 |
* @const string |
|
73 |
*/ |
|
74 |
const E_MISSING_INPUT_RESPONSE = 'missing-input-response'; |
|
75 |
|
|
76 |
/** |
|
77 |
* Expected hostname did not match |
|
78 |
* @const string |
|
79 |
*/ |
|
80 |
const E_HOSTNAME_MISMATCH = 'hostname-mismatch'; |
|
81 |
|
|
82 |
/** |
|
83 |
* Expected APK package name did not match |
|
84 |
* @const string |
|
85 |
*/ |
|
86 |
const E_APK_PACKAGE_NAME_MISMATCH = 'apk_package_name-mismatch'; |
|
87 |
|
|
88 |
/** |
|
89 |
* Expected action did not match |
|
90 |
* @const string |
|
91 |
*/ |
|
92 |
const E_ACTION_MISMATCH = 'action-mismatch'; |
|
93 |
|
|
94 |
/** |
|
95 |
* Score threshold not met |
|
96 |
* @const string |
|
97 |
*/ |
|
98 |
const E_SCORE_THRESHOLD_NOT_MET = 'score-threshold-not-met'; |
|
99 |
|
|
100 |
/** |
|
101 |
* Challenge timeout |
|
102 |
* @const string |
|
103 |
*/ |
|
104 |
const E_CHALLENGE_TIMEOUT = 'challenge-timeout'; |
|
39 | 105 |
|
40 | 106 |
/** |
41 | 107 |
* Shared secret for the site. |
42 |
* @var type string
|
|
108 |
* @var string |
|
43 | 109 |
*/ |
44 | 110 |
private $secret; |
45 | 111 |
|
46 | 112 |
/** |
47 |
* Method used to communicate with service. Defaults to POST request.
|
|
113 |
* Method used to communicate with service. Defaults to POST request. |
|
48 | 114 |
* @var RequestMethod |
49 | 115 |
*/ |
50 | 116 |
private $requestMethod; |
... | ... | |
52 | 118 |
/** |
53 | 119 |
* Create a configured instance to use the reCAPTCHA service. |
54 | 120 |
* |
55 |
* @param string $secret shared secret between site and reCAPTCHA server.
|
|
121 |
* @param string $secret The shared key between your site and reCAPTCHA.
|
|
56 | 122 |
* @param RequestMethod $requestMethod method used to send the request. Defaults to POST. |
123 |
* @throws \RuntimeException if $secret is invalid |
|
57 | 124 |
*/ |
58 | 125 |
public function __construct($secret, RequestMethod $requestMethod = null) |
59 | 126 |
{ |
... | ... | |
66 | 133 |
} |
67 | 134 |
|
68 | 135 |
$this->secret = $secret; |
69 |
|
|
70 |
if (!is_null($requestMethod)) { |
|
71 |
$this->requestMethod = $requestMethod; |
|
72 |
} else { |
|
73 |
$this->requestMethod = new RequestMethod\Post(); |
|
74 |
} |
|
136 |
$this->requestMethod = (is_null($requestMethod)) ? new RequestMethod\Post() : $requestMethod; |
|
75 | 137 |
} |
76 | 138 |
|
77 | 139 |
/** |
78 | 140 |
* Calls the reCAPTCHA siteverify API to verify whether the user passes |
79 |
* CAPTCHA test.
|
|
141 |
* CAPTCHA test and additionally runs any specified additional checks
|
|
80 | 142 |
* |
81 |
* @param string $response The value of 'g-recaptcha-response' in the submitted form.
|
|
143 |
* @param string $response The user response token provided by reCAPTCHA, verifying the user on your site.
|
|
82 | 144 |
* @param string $remoteIp The end user's IP address. |
83 | 145 |
* @return Response Response from the service. |
84 | 146 |
*/ |
... | ... | |
86 | 148 |
{ |
87 | 149 |
// Discard empty solution submissions |
88 | 150 |
if (empty($response)) { |
89 |
$recaptchaResponse = new Response(false, array('missing-input-response'));
|
|
151 |
$recaptchaResponse = new Response(false, array(self::E_MISSING_INPUT_RESPONSE));
|
|
90 | 152 |
return $recaptchaResponse; |
91 | 153 |
} |
92 | 154 |
|
93 | 155 |
$params = new RequestParameters($this->secret, $response, $remoteIp, self::VERSION); |
94 | 156 |
$rawResponse = $this->requestMethod->submit($params); |
95 |
return Response::fromJson($rawResponse); |
|
157 |
$initialResponse = Response::fromJson($rawResponse); |
|
158 |
$validationErrors = array(); |
|
159 |
|
|
160 |
if (isset($this->hostname) && strcasecmp($this->hostname, $initialResponse->getHostname()) !== 0) { |
|
161 |
$validationErrors[] = self::E_HOSTNAME_MISMATCH; |
|
162 |
} |
|
163 |
|
|
164 |
if (isset($this->apkPackageName) && strcasecmp($this->apkPackageName, $initialResponse->getApkPackageName()) !== 0) { |
|
165 |
$validationErrors[] = self::E_APK_PACKAGE_NAME_MISMATCH; |
|
166 |
} |
|
167 |
|
|
168 |
if (isset($this->action) && strcasecmp($this->action, $initialResponse->getAction()) !== 0) { |
|
169 |
$validationErrors[] = self::E_ACTION_MISMATCH; |
|
170 |
} |
|
171 |
|
|
172 |
if (isset($this->threshold) && $this->threshold > $initialResponse->getScore()) { |
|
173 |
$validationErrors[] = self::E_SCORE_THRESHOLD_NOT_MET; |
|
174 |
} |
|
175 |
|
|
176 |
if (isset($this->timeoutSeconds)) { |
|
177 |
$challengeTs = strtotime($initialResponse->getChallengeTs()); |
|
178 |
|
|
179 |
if ($challengeTs > 0 && time() - $challengeTs > $this->timeoutSeconds) { |
|
180 |
$validationErrors[] = self::E_CHALLENGE_TIMEOUT; |
|
181 |
} |
|
182 |
} |
|
183 |
|
|
184 |
if (empty($validationErrors)) { |
|
185 |
return $initialResponse; |
|
186 |
} |
|
187 |
|
|
188 |
return new Response( |
|
189 |
false, |
|
190 |
array_merge($initialResponse->getErrorCodes(), $validationErrors), |
|
191 |
$initialResponse->getHostname(), |
|
192 |
$initialResponse->getChallengeTs(), |
|
193 |
$initialResponse->getApkPackageName(), |
|
194 |
$initialResponse->getScore(), |
|
195 |
$initialResponse->getAction() |
|
196 |
); |
|
197 |
} |
|
198 |
|
|
199 |
/** |
|
200 |
* Provide a hostname to match against in verify() |
|
201 |
* This should be without a protocol or trailing slash, e.g. www.google.com |
|
202 |
* |
|
203 |
* @param string $hostname Expected hostname |
|
204 |
* @return ReCaptcha Current instance for fluent interface |
|
205 |
*/ |
|
206 |
public function setExpectedHostname($hostname) |
|
207 |
{ |
|
208 |
$this->hostname = $hostname; |
|
209 |
return $this; |
|
210 |
} |
|
211 |
|
|
212 |
/** |
|
213 |
* Provide an APK package name to match against in verify() |
|
214 |
* |
|
215 |
* @param string $apkPackageName Expected APK package name |
|
216 |
* @return ReCaptcha Current instance for fluent interface |
|
217 |
*/ |
|
218 |
public function setExpectedApkPackageName($apkPackageName) |
|
219 |
{ |
|
220 |
$this->apkPackageName = $apkPackageName; |
|
221 |
return $this; |
|
222 |
} |
|
223 |
|
|
224 |
/** |
|
225 |
* Provide an action to match against in verify() |
|
226 |
* This should be set per page. |
|
227 |
* |
|
228 |
* @param string $action Expected action |
|
229 |
* @return ReCaptcha Current instance for fluent interface |
|
230 |
*/ |
|
231 |
public function setExpectedAction($action) |
|
232 |
{ |
|
233 |
$this->action = $action; |
|
234 |
return $this; |
|
235 |
} |
|
236 |
|
|
237 |
/** |
|
238 |
* Provide a threshold to meet or exceed in verify() |
|
239 |
* Threshold should be a float between 0 and 1 which will be tested as response >= threshold. |
|
240 |
* |
|
241 |
* @param float $threshold Expected threshold |
|
242 |
* @return ReCaptcha Current instance for fluent interface |
|
243 |
*/ |
|
244 |
public function setScoreThreshold($threshold) |
|
245 |
{ |
|
246 |
$this->threshold = floatval($threshold); |
|
247 |
return $this; |
|
248 |
} |
|
249 |
|
|
250 |
/** |
|
251 |
* Provide a timeout in seconds to test against the challenge timestamp in verify() |
|
252 |
* |
|
253 |
* @param int $timeoutSeconds Expected hostname |
|
254 |
* @return ReCaptcha Current instance for fluent interface |
|
255 |
*/ |
|
256 |
public function setChallengeTimeout($timeoutSeconds) |
|
257 |
{ |
|
258 |
$this->timeoutSeconds = $timeoutSeconds; |
|
259 |
return $this; |
|
96 | 260 |
} |
97 | 261 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules