1111namespace Biscolab \ReCaptcha ;
1212
1313use Exception ;
14+ use Illuminate \Support \Arr ;
1415
16+ /**
17+ * Class ReCaptchaBuilder
18+ * @package Biscolab\ReCaptcha
19+ */
1520class ReCaptchaBuilder {
1621
22+ /**
23+ * @var string
24+ */
25+ const DEFAULT_API_VERSION = 'v2 ' ;
26+
27+ /**
28+ * @var int
29+ */
30+ const DEFAULT_CURL_TIMEOUT = 10 ;
31+
1732 /**
1833 * The Site key
1934 * please visit https://developers.google.com/recaptcha/docs/start
@@ -35,6 +50,13 @@ class ReCaptchaBuilder {
3550 */
3651 protected $ version ;
3752
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+
3860 /**
3961 * Whether is true the ReCAPTCHA is inactive
4062 * @var boolean
@@ -46,11 +68,25 @@ class ReCaptchaBuilder {
4668 */
4769 protected $ api_url = 'https://www.google.com/recaptcha/api/siteverify ' ;
4870
49- public function __construct ($ api_site_key , $ api_secret_key , $ version = 'v2 ' ) {
71+ /**
72+ * ReCaptchaBuilder constructor.
73+ *
74+ * @param string $api_site_key
75+ * @param string $api_secret_key
76+ * @param null|string $version
77+ * @param int|null $curl_timeout
78+ */
79+ public function __construct (
80+ string $ api_site_key ,
81+ string $ api_secret_key ,
82+ ?string $ version = self ::DEFAULT_API_VERSION ,
83+ ?int $ curl_timeout = self ::DEFAULT_CURL_TIMEOUT
84+ ) {
5085
5186 $ this ->setApiSiteKey ($ api_site_key );
5287 $ this ->setApiSecretKey ($ api_secret_key );
5388 $ this ->setVersion ($ version );
89+ $ this ->setCurlTimeout ($ curl_timeout );
5490 $ this ->setSkipByIp ($ this ->skipByIp ());
5591 }
5692
@@ -78,6 +114,26 @@ public function setApiSecretKey(string $api_secret_key): ReCaptchaBuilder {
78114 return $ this ;
79115 }
80116
117+ /**
118+ * @param int $curl_timeout
119+ *
120+ * @return ReCaptchaBuilder
121+ */
122+ public function setCurlTimeout (int $ curl_timeout ): ReCaptchaBuilder {
123+
124+ $ this ->curl_timeout = $ curl_timeout ;
125+
126+ return $ this ;
127+ }
128+
129+ /**
130+ * @return int
131+ */
132+ public function getCurlTimeout (): int {
133+
134+ return $ this ->curl_timeout ;
135+ }
136+
81137 /**
82138 * @param string $version
83139 *
@@ -114,9 +170,10 @@ public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder {
114170 * @return array|mixed
115171 */
116172 public function getIpWhitelist () {
173+
117174 $ whitelist = config ('recaptcha.skip_ip ' , []);
118175
119- if (!is_array ($ whitelist )) {
176+ if (!is_array ($ whitelist )) {
120177 $ whitelist = explode (', ' , $ whitelist );
121178 }
122179
@@ -169,9 +226,9 @@ function biscolabLaravelReCaptcha(token) {
169226 }
170227 elseif ($ this ->version == 'v3 ' ) {
171228
172- $ action = array_get ($ configuration , 'action ' , 'homepage ' );
229+ $ action = Arr:: get ($ configuration , 'action ' , 'homepage ' );
173230
174- $ js_custom_validation = array_get ($ configuration , 'custom_validation ' , '' );
231+ $ js_custom_validation = Arr:: get ($ configuration , 'custom_validation ' , '' );
175232
176233 // Check if set custom_validation. That function will override default fetch validation function
177234 if ($ js_custom_validation ) {
@@ -180,14 +237,16 @@ function biscolabLaravelReCaptcha(token) {
180237 }
181238 else {
182239
183- $ js_then_callback = array_get ($ configuration , 'callback_then ' , '' );
184- $ js_callback_catch = array_get ($ configuration , 'callback_catch ' , '' );
240+ $ js_then_callback = Arr:: get ($ configuration , 'callback_then ' , '' );
241+ $ js_callback_catch = Arr:: get ($ configuration , 'callback_catch ' , '' );
185242
186243 $ js_then_callback = ($ js_then_callback ) ? "{$ js_then_callback }(response) " : '' ;
187244 $ js_callback_catch = ($ js_callback_catch ) ? "{$ js_callback_catch }(err) " : '' ;
188245
189246 $ validate_function = "
190- fetch('/ " . config ('recaptcha.default_validation_route ' , 'biscolab-recaptcha/validate ' ) . "? " . config ('recaptcha.default_token_parameter_name ' , 'token ' ) . "=' + token, {
247+ fetch('/ " . config ('recaptcha.default_validation_route ' ,
248+ 'biscolab-recaptcha/validate ' ) . "? " . config ('recaptcha.default_token_parameter_name ' ,
249+ 'token ' ) . "=' + token, {
191250 headers: {
192251 \"X-Requested-With \": \"XMLHttpRequest \",
193252 \"X-CSRF-TOKEN \": csrfToken.content
@@ -258,7 +317,7 @@ public function validate($response) {
258317 $ curl = curl_init ($ url );
259318 curl_setopt ($ curl , CURLOPT_HEADER , false );
260319 curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , true );
261- curl_setopt ($ curl , CURLOPT_TIMEOUT , 1 );
320+ curl_setopt ($ curl , CURLOPT_TIMEOUT , $ this -> curl_timeout );
262321 curl_setopt ($ curl , CURLOPT_SSL_VERIFYPEER , false );
263322 $ curl_response = curl_exec ($ curl );
264323 }
0 commit comments