1717 * Class ReCaptchaBuilder
1818 * @package Biscolab\ReCaptcha
1919 */
20- class ReCaptchaBuilder {
20+ class ReCaptchaBuilder
21+ {
2122
2223 /**
2324 * @var string
@@ -50,13 +51,6 @@ class ReCaptchaBuilder {
5051 */
5152 protected $ version ;
5253
53- /**
54- * The curl timeout
55- * please visit https://curl.haxx.se/libcurl/c/CURLOPT_TIMEOUT.html
56- * @var int
57- */
58- protected $ curl_timeout ;
59-
6054 /**
6155 * Whether is true the ReCAPTCHA is inactive
6256 * @var boolean
@@ -74,19 +68,16 @@ class ReCaptchaBuilder {
7468 * @param string $api_site_key
7569 * @param string $api_secret_key
7670 * @param null|string $version
77- * @param int|null $curl_timeout
7871 */
7972 public function __construct (
8073 string $ api_site_key ,
8174 string $ api_secret_key ,
82- ?string $ version = self ::DEFAULT_API_VERSION ,
83- ?int $ curl_timeout = self ::DEFAULT_CURL_TIMEOUT
75+ ?string $ version = self ::DEFAULT_API_VERSION
8476 ) {
8577
8678 $ this ->setApiSiteKey ($ api_site_key );
8779 $ this ->setApiSecretKey ($ api_secret_key );
8880 $ this ->setVersion ($ version );
89- $ this ->setCurlTimeout ($ curl_timeout );
9081 $ this ->setSkipByIp ($ this ->skipByIp ());
9182 }
9283
@@ -95,7 +86,8 @@ public function __construct(
9586 *
9687 * @return ReCaptchaBuilder
9788 */
98- public function setApiSiteKey (string $ api_site_key ): ReCaptchaBuilder {
89+ public function setApiSiteKey (string $ api_site_key ): ReCaptchaBuilder
90+ {
9991
10092 $ this ->api_site_key = $ api_site_key ;
10193
@@ -107,42 +99,30 @@ public function setApiSiteKey(string $api_site_key): ReCaptchaBuilder {
10799 *
108100 * @return ReCaptchaBuilder
109101 */
110- public function setApiSecretKey (string $ api_secret_key ): ReCaptchaBuilder {
102+ public function setApiSecretKey (string $ api_secret_key ): ReCaptchaBuilder
103+ {
111104
112105 $ this ->api_secret_key = $ api_secret_key ;
113106
114107 return $ this ;
115108 }
116109
117- /**
118- * @param int|null $curl_timeout
119- *
120- * @return ReCaptchaBuilder
121- */
122- public function setCurlTimeout (?int $ curl_timeout = null ): ReCaptchaBuilder {
123-
124- if ($ curl_timeout === null ) {
125- $ curl_timeout = config ('recaptcha.curl_timeout ' , ReCaptchaBuilder::DEFAULT_CURL_TIMEOUT );
126- }
127- $ this ->curl_timeout = $ curl_timeout ;
128-
129- return $ this ;
130- }
131-
132110 /**
133111 * @return int
134112 */
135- public function getCurlTimeout (): int {
113+ public function getCurlTimeout (): int
114+ {
136115
137- return $ this -> curl_timeout ;
116+ return config ( ' recaptcha. curl_timeout' , self :: DEFAULT_CURL_TIMEOUT ) ;
138117 }
139118
140119 /**
141120 * @param string $version
142121 *
143122 * @return ReCaptchaBuilder
144123 */
145- public function setVersion (string $ version ): ReCaptchaBuilder {
124+ public function setVersion (string $ version ): ReCaptchaBuilder
125+ {
146126
147127 $ this ->version = $ version ;
148128
@@ -152,7 +132,8 @@ public function setVersion(string $version): ReCaptchaBuilder {
152132 /**
153133 * @return string
154134 */
155- public function getVersion (): string {
135+ public function getVersion (): string
136+ {
156137
157138 return $ this ->version ;
158139 }
@@ -162,7 +143,8 @@ public function getVersion(): string {
162143 *
163144 * @return ReCaptchaBuilder
164145 */
165- public function setSkipByIp (bool $ skip_by_ip ): ReCaptchaBuilder {
146+ public function setSkipByIp (bool $ skip_by_ip ): ReCaptchaBuilder
147+ {
166148
167149 $ this ->skip_by_ip = $ skip_by_ip ;
168150
@@ -172,7 +154,8 @@ public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder {
172154 /**
173155 * @return array|mixed
174156 */
175- public function getIpWhitelist () {
157+ public function getIpWhitelist ()
158+ {
176159
177160 $ whitelist = config ('recaptcha.skip_ip ' , []);
178161
@@ -188,7 +171,8 @@ public function getIpWhitelist() {
188171 *
189172 * @return boolean
190173 */
191- public function skipByIp (): bool {
174+ public function skipByIp (): bool
175+ {
192176
193177 return (in_array (request ()->ip (), $ this ->getIpWhitelist ()));
194178 }
@@ -203,7 +187,8 @@ public function skipByIp(): bool {
203187 * @return string
204188 * @throws Exception
205189 */
206- public function htmlScriptTagJsApi (?string $ formId = '' , ?array $ configuration = []): string {
190+ public function htmlScriptTagJsApi (?string $ formId = '' , ?array $ configuration = []): string
191+ {
207192
208193 if ($ this ->skip_by_ip ) {
209194 return '' ;
@@ -214,7 +199,9 @@ public function htmlScriptTagJsApi(?string $formId = '', ?array $configuration =
214199 $ html = "<script src= \"https://www.google.com/recaptcha/api.js?render= {$ this ->api_site_key }\"></script> " ;
215200 break ;
216201 default :
217- $ html = "<script src= \"https://www.google.com/recaptcha/api.js \" async defer></script> " ;
202+ $ language = config ('recaptcha.default_language ' , null );
203+ $ query = ($ language ) ? "?hl= " . $ language : "" ;
204+ $ html = "<script src= \"https://www.google.com/recaptcha/api.js " . $ query . "\" async defer></script> " ;
218205 }
219206
220207 if ($ this ->version == 'invisible ' ) {
@@ -226,8 +213,7 @@ function biscolabLaravelReCaptcha(token) {
226213 document.getElementById(" ' . $ formId . '").submit();
227214 }
228215 </script> ' ;
229- }
230- elseif ($ this ->version == 'v3 ' ) {
216+ } elseif ($ this ->version == 'v3 ' ) {
231217
232218 $ action = Arr::get ($ configuration , 'action ' , 'homepage ' );
233219
@@ -237,8 +223,7 @@ function biscolabLaravelReCaptcha(token) {
237223 if ($ js_custom_validation ) {
238224
239225 $ validate_function = ($ js_custom_validation ) ? "{$ js_custom_validation }(token); " : '' ;
240- }
241- else {
226+ } else {
242227
243228 $ js_then_callback = Arr::get ($ configuration , 'callback_then ' , '' );
244229 $ js_callback_catch = Arr::get ($ configuration , 'callback_catch ' , '' );
@@ -281,7 +266,8 @@ function biscolabLaravelReCaptcha(token) {
281266 *
282267 * @return string
283268 */
284- public function htmlScriptTagJsApiV3 (?array $ configuration = []): string {
269+ public function htmlScriptTagJsApiV3 (?array $ configuration = []): string
270+ {
285271
286272 return $ this ->htmlScriptTagJsApi ('' , $ configuration );
287273 }
@@ -293,7 +279,8 @@ public function htmlScriptTagJsApiV3(?array $configuration = []): string {
293279 *
294280 * @return boolean|array
295281 */
296- public function validate ($ response ) {
282+ public function validate ($ response )
283+ {
297284
298285 if ($ this ->skip_by_ip ) {
299286 if ($ this ->returnArray ()) {
@@ -317,14 +304,14 @@ public function validate($response) {
317304 $ url = $ this ->api_url . '? ' . $ params ;
318305
319306 if (function_exists ('curl_version ' )) {
307+
320308 $ curl = curl_init ($ url );
321309 curl_setopt ($ curl , CURLOPT_HEADER , false );
322310 curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , true );
323- curl_setopt ($ curl , CURLOPT_TIMEOUT , $ this ->curl_timeout );
311+ curl_setopt ($ curl , CURLOPT_TIMEOUT , $ this ->getCurlTimeout () );
324312 curl_setopt ($ curl , CURLOPT_SSL_VERIFYPEER , false );
325313 $ curl_response = curl_exec ($ curl );
326- }
327- else {
314+ } else {
328315 $ curl_response = file_get_contents ($ url );
329316 }
330317
@@ -350,10 +337,29 @@ public function validate($response) {
350337
351338 }
352339
340+ /**
341+ * @return string
342+ */
343+ public function getApiSiteKey (): string
344+ {
345+
346+ return $ this ->api_site_key ;
347+ }
348+
349+ /**
350+ * @return string
351+ */
352+ public function getApiSecretKey (): string
353+ {
354+
355+ return $ this ->api_secret_key ;
356+ }
357+
353358 /**
354359 * @return bool
355360 */
356- protected function returnArray (): bool {
361+ protected function returnArray (): bool
362+ {
357363
358364 return ($ this ->version == 'v3 ' );
359365 }
0 commit comments