@@ -20,29 +20,29 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class TestCase extends OrchestraTestCase { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Load package alias |
|
| 25 | - * |
|
| 26 | - * @param \Illuminate\Foundation\Application $app |
|
| 27 | - * |
|
| 28 | - * @return array |
|
| 29 | - */ |
|
| 30 | - protected function getPackageAliases($app) { |
|
| 23 | + /** |
|
| 24 | + * Load package alias |
|
| 25 | + * |
|
| 26 | + * @param \Illuminate\Foundation\Application $app |
|
| 27 | + * |
|
| 28 | + * @return array |
|
| 29 | + */ |
|
| 30 | + protected function getPackageAliases($app) { |
|
| 31 | 31 | |
| 32 | - return [ |
|
| 33 | - 'ReCaptcha' => ReCaptcha::class, |
|
| 34 | - ]; |
|
| 35 | - } |
|
| 32 | + return [ |
|
| 33 | + 'ReCaptcha' => ReCaptcha::class, |
|
| 34 | + ]; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Load package service provider |
|
| 39 | - * |
|
| 40 | - * @param \Illuminate\Foundation\Application $app |
|
| 41 | - * |
|
| 42 | - * @return array |
|
| 43 | - */ |
|
| 44 | - protected function getPackageProviders($app) { |
|
| 37 | + /** |
|
| 38 | + * Load package service provider |
|
| 39 | + * |
|
| 40 | + * @param \Illuminate\Foundation\Application $app |
|
| 41 | + * |
|
| 42 | + * @return array |
|
| 43 | + */ |
|
| 44 | + protected function getPackageProviders($app) { |
|
| 45 | 45 | |
| 46 | - return [ReCaptchaServiceProvider::class]; |
|
| 47 | - } |
|
| 46 | + return [ReCaptchaServiceProvider::class]; |
|
| 47 | + } |
|
| 48 | 48 | } |
@@ -13,52 +13,52 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | return [ |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * |
|
| 18 | - * The site key |
|
| 19 | - * get site key @ www.google.com/recaptcha/admin |
|
| 20 | - * |
|
| 21 | - */ |
|
| 22 | - 'api_site_key' => env('RECAPTCHA_SITE_KEY', ''), |
|
| 16 | + /** |
|
| 17 | + * |
|
| 18 | + * The site key |
|
| 19 | + * get site key @ www.google.com/recaptcha/admin |
|
| 20 | + * |
|
| 21 | + */ |
|
| 22 | + 'api_site_key' => env('RECAPTCHA_SITE_KEY', ''), |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * |
|
| 26 | - * The secret key |
|
| 27 | - * get secret key @ www.google.com/recaptcha/admin |
|
| 28 | - * |
|
| 29 | - */ |
|
| 30 | - 'api_secret_key' => env('RECAPTCHA_SECRET_KEY', ''), |
|
| 24 | + /** |
|
| 25 | + * |
|
| 26 | + * The secret key |
|
| 27 | + * get secret key @ www.google.com/recaptcha/admin |
|
| 28 | + * |
|
| 29 | + */ |
|
| 30 | + 'api_secret_key' => env('RECAPTCHA_SECRET_KEY', ''), |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * |
|
| 34 | - * ReCATCHA version |
|
| 35 | - * Supported: "v2", "invisible", "v3", |
|
| 36 | - * |
|
| 37 | - * get more info @ https://developers.google.com/recaptcha/docs/versions |
|
| 38 | - * |
|
| 39 | - */ |
|
| 40 | - 'version' => env('RECAPTCHA_DEFAULT_VERSION', 'v2'), |
|
| 32 | + /** |
|
| 33 | + * |
|
| 34 | + * ReCATCHA version |
|
| 35 | + * Supported: "v2", "invisible", "v3", |
|
| 36 | + * |
|
| 37 | + * get more info @ https://developers.google.com/recaptcha/docs/versions |
|
| 38 | + * |
|
| 39 | + */ |
|
| 40 | + 'version' => env('RECAPTCHA_DEFAULT_VERSION', 'v2'), |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * |
|
| 44 | - * IP addresses for which validation will be skipped |
|
| 45 | - * |
|
| 46 | - */ |
|
| 47 | - 'skip_ip' => [], |
|
| 42 | + /** |
|
| 43 | + * |
|
| 44 | + * IP addresses for which validation will be skipped |
|
| 45 | + * |
|
| 46 | + */ |
|
| 47 | + 'skip_ip' => [], |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * |
|
| 51 | - * Default route called to check the Google reCAPTCHA token |
|
| 52 | - * @since v3.2.0 |
|
| 53 | - * |
|
| 54 | - */ |
|
| 55 | - 'default_validation_route' => env('RECAPTCHA_DEFAULT_VALIDATION_ROUTE', 'biscolab-recaptcha/validate'), |
|
| 49 | + /** |
|
| 50 | + * |
|
| 51 | + * Default route called to check the Google reCAPTCHA token |
|
| 52 | + * @since v3.2.0 |
|
| 53 | + * |
|
| 54 | + */ |
|
| 55 | + 'default_validation_route' => env('RECAPTCHA_DEFAULT_VALIDATION_ROUTE', 'biscolab-recaptcha/validate'), |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * |
|
| 59 | - * The name of the parameter used to send Google reCAPTCHA token to verify route |
|
| 60 | - * @since v3.2.0 |
|
| 61 | - * |
|
| 62 | - */ |
|
| 63 | - 'default_token_parameter_name' => env('RECAPTCHA_DEFAULT_TOKEN_PARAMETER_NAME', 'token') |
|
| 57 | + /** |
|
| 58 | + * |
|
| 59 | + * The name of the parameter used to send Google reCAPTCHA token to verify route |
|
| 60 | + * @since v3.2.0 |
|
| 61 | + * |
|
| 62 | + */ |
|
| 63 | + 'default_token_parameter_name' => env('RECAPTCHA_DEFAULT_TOKEN_PARAMETER_NAME', 'token') |
|
| 64 | 64 | ]; |
| 65 | 65 | \ No newline at end of file |
@@ -18,13 +18,13 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class ReCaptcha extends Facade { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Get the registered name of the component. |
|
| 23 | - * |
|
| 24 | - * @return string |
|
| 25 | - */ |
|
| 26 | - protected static function getFacadeAccessor() { |
|
| 21 | + /** |
|
| 22 | + * Get the registered name of the component. |
|
| 23 | + * |
|
| 24 | + * @return string |
|
| 25 | + */ |
|
| 26 | + protected static function getFacadeAccessor() { |
|
| 27 | 27 | |
| 28 | - return 'recaptcha'; |
|
| 29 | - } |
|
| 28 | + return 'recaptcha'; |
|
| 29 | + } |
|
| 30 | 30 | } |
@@ -11,13 +11,13 @@ discard block |
||
| 11 | 11 | use Biscolab\ReCaptcha\Facades\ReCaptcha; |
| 12 | 12 | |
| 13 | 13 | if (!function_exists('recaptcha')) { |
| 14 | - /** |
|
| 15 | - * @return Biscolab\ReCaptcha\ReCaptchaBuilder |
|
| 16 | - */ |
|
| 17 | - function recaptcha() { |
|
| 14 | + /** |
|
| 15 | + * @return Biscolab\ReCaptcha\ReCaptchaBuilder |
|
| 16 | + */ |
|
| 17 | + function recaptcha() { |
|
| 18 | 18 | |
| 19 | - return app('recaptcha'); |
|
| 20 | - } |
|
| 19 | + return app('recaptcha'); |
|
| 20 | + } |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | /** |
@@ -29,15 +29,15 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | if (!function_exists('htmlScriptTagJsApi')) { |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @param string $formId |
|
| 34 | - * |
|
| 35 | - * @return string |
|
| 36 | - */ |
|
| 37 | - function htmlScriptTagJsApi($formId = ''): string { |
|
| 32 | + /** |
|
| 33 | + * @param string $formId |
|
| 34 | + * |
|
| 35 | + * @return string |
|
| 36 | + */ |
|
| 37 | + function htmlScriptTagJsApi($formId = ''): string { |
|
| 38 | 38 | |
| 39 | - return ReCaptcha::htmlScriptTagJsApi($formId); |
|
| 40 | - } |
|
| 39 | + return ReCaptcha::htmlScriptTagJsApi($formId); |
|
| 40 | + } |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
@@ -49,15 +49,15 @@ discard block |
||
| 49 | 49 | */ |
| 50 | 50 | if (!function_exists('htmlScriptTagJsApiV3')) { |
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * @param array $config |
|
| 54 | - * |
|
| 55 | - * @return string |
|
| 56 | - */ |
|
| 57 | - function htmlScriptTagJsApiV3($config = []): string { |
|
| 52 | + /** |
|
| 53 | + * @param array $config |
|
| 54 | + * |
|
| 55 | + * @return string |
|
| 56 | + */ |
|
| 57 | + function htmlScriptTagJsApiV3($config = []): string { |
|
| 58 | 58 | |
| 59 | - return ReCaptcha::htmlScriptTagJsApiV3($config); |
|
| 60 | - } |
|
| 59 | + return ReCaptcha::htmlScriptTagJsApiV3($config); |
|
| 60 | + } |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | /** |
@@ -71,15 +71,15 @@ discard block |
||
| 71 | 71 | */ |
| 72 | 72 | if (!function_exists('htmlFormButton')) { |
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * @param null|string $buttonInnerHTML |
|
| 76 | - * |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - function htmlFormButton(?string $buttonInnerHTML = 'Submit'): string { |
|
| 74 | + /** |
|
| 75 | + * @param null|string $buttonInnerHTML |
|
| 76 | + * |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + function htmlFormButton(?string $buttonInnerHTML = 'Submit'): string { |
|
| 80 | 80 | |
| 81 | - return ReCaptcha::htmlFormButton($buttonInnerHTML); |
|
| 82 | - } |
|
| 81 | + return ReCaptcha::htmlFormButton($buttonInnerHTML); |
|
| 82 | + } |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -91,12 +91,12 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | if (!function_exists('htmlFormSnippet')) { |
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * @return string |
|
| 96 | - */ |
|
| 97 | - function htmlFormSnippet(): string { |
|
| 94 | + /** |
|
| 95 | + * @return string |
|
| 96 | + */ |
|
| 97 | + function htmlFormSnippet(): string { |
|
| 98 | 98 | |
| 99 | - return ReCaptcha::htmlFormSnippet(); |
|
| 100 | - } |
|
| 99 | + return ReCaptcha::htmlFormSnippet(); |
|
| 100 | + } |
|
| 101 | 101 | } |
| 102 | 102 | |
@@ -16,25 +16,25 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class ReCaptchaBuilderV2 extends ReCaptchaBuilder { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * ReCaptchaBuilderV2 constructor. |
|
| 21 | - * |
|
| 22 | - * @param string $api_site_key |
|
| 23 | - * @param string $api_secret_key |
|
| 24 | - */ |
|
| 25 | - public function __construct(string $api_site_key, string $api_secret_key) { |
|
| 19 | + /** |
|
| 20 | + * ReCaptchaBuilderV2 constructor. |
|
| 21 | + * |
|
| 22 | + * @param string $api_site_key |
|
| 23 | + * @param string $api_secret_key |
|
| 24 | + */ |
|
| 25 | + public function __construct(string $api_site_key, string $api_secret_key) { |
|
| 26 | 26 | |
| 27 | - parent::__construct($api_site_key, $api_secret_key, 'v2'); |
|
| 28 | - } |
|
| 27 | + parent::__construct($api_site_key, $api_secret_key, 'v2'); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Write ReCAPTCHA HTML tag in your FORM |
|
| 32 | - * Insert before </form> tag |
|
| 33 | - * @return string |
|
| 34 | - */ |
|
| 35 | - public function htmlFormSnippet(): string { |
|
| 30 | + /** |
|
| 31 | + * Write ReCAPTCHA HTML tag in your FORM |
|
| 32 | + * Insert before </form> tag |
|
| 33 | + * @return string |
|
| 34 | + */ |
|
| 35 | + public function htmlFormSnippet(): string { |
|
| 36 | 36 | |
| 37 | - return ($this->version == 'v2') ? '<div class="g-recaptcha" data-sitekey="' . $this->api_site_key . '"></div>' : ''; |
|
| 38 | - } |
|
| 37 | + return ($this->version == 'v2') ? '<div class="g-recaptcha" data-sitekey="' . $this->api_site_key . '"></div>' : ''; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | 40 | } |
| 41 | 41 | \ No newline at end of file |
@@ -34,7 +34,7 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | public function htmlFormSnippet(): string { |
| 36 | 36 | |
| 37 | - return ($this->version == 'v2') ? '<div class="g-recaptcha" data-sitekey="' . $this->api_site_key . '"></div>' : ''; |
|
| 37 | + return ($this->version == 'v2') ? '<div class="g-recaptcha" data-sitekey="'.$this->api_site_key.'"></div>' : ''; |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | } |
| 41 | 41 | \ No newline at end of file |
@@ -16,15 +16,15 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class ReCaptchaBuilderV3 extends ReCaptchaBuilder { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * ReCaptchaBuilderV3 constructor. |
|
| 21 | - * |
|
| 22 | - * @param string $api_site_key |
|
| 23 | - * @param string $api_secret_key |
|
| 24 | - */ |
|
| 25 | - public function __construct(string $api_site_key, string $api_secret_key) { |
|
| 19 | + /** |
|
| 20 | + * ReCaptchaBuilderV3 constructor. |
|
| 21 | + * |
|
| 22 | + * @param string $api_site_key |
|
| 23 | + * @param string $api_secret_key |
|
| 24 | + */ |
|
| 25 | + public function __construct(string $api_site_key, string $api_secret_key) { |
|
| 26 | 26 | |
| 27 | - parent::__construct($api_site_key, $api_secret_key, 'v3'); |
|
| 28 | - } |
|
| 27 | + parent::__construct($api_site_key, $api_secret_key, 'v3'); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | 30 | } |
| 31 | 31 | \ No newline at end of file |
@@ -16,28 +16,28 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class ReCaptchaBuilderInvisible extends ReCaptchaBuilder { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * ReCaptchaBuilderInvisible constructor. |
|
| 21 | - * |
|
| 22 | - * @param string $api_site_key |
|
| 23 | - * @param string $api_secret_key |
|
| 24 | - */ |
|
| 25 | - public function __construct(string $api_site_key, string $api_secret_key) { |
|
| 19 | + /** |
|
| 20 | + * ReCaptchaBuilderInvisible constructor. |
|
| 21 | + * |
|
| 22 | + * @param string $api_site_key |
|
| 23 | + * @param string $api_secret_key |
|
| 24 | + */ |
|
| 25 | + public function __construct(string $api_site_key, string $api_secret_key) { |
|
| 26 | 26 | |
| 27 | - parent::__construct($api_site_key, $api_secret_key, 'invisible'); |
|
| 28 | - } |
|
| 27 | + parent::__construct($api_site_key, $api_secret_key, 'invisible'); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Write HTML <button> tag in your HTML code |
|
| 32 | - * Insert before </form> tag |
|
| 33 | - * |
|
| 34 | - * @param string $buttonInnerHTML |
|
| 35 | - * |
|
| 36 | - * @return string |
|
| 37 | - */ |
|
| 38 | - public function htmlFormButton($buttonInnerHTML = 'Submit'): string { |
|
| 30 | + /** |
|
| 31 | + * Write HTML <button> tag in your HTML code |
|
| 32 | + * Insert before </form> tag |
|
| 33 | + * |
|
| 34 | + * @param string $buttonInnerHTML |
|
| 35 | + * |
|
| 36 | + * @return string |
|
| 37 | + */ |
|
| 38 | + public function htmlFormButton($buttonInnerHTML = 'Submit'): string { |
|
| 39 | 39 | |
| 40 | - return ($this->version == 'invisible') ? '<button class="g-recaptcha" data-sitekey="' . $this->api_site_key . '" data-callback="biscolabLaravelReCaptcha">' . $buttonInnerHTML . '</button>' : ''; |
|
| 41 | - } |
|
| 40 | + return ($this->version == 'invisible') ? '<button class="g-recaptcha" data-sitekey="' . $this->api_site_key . '" data-callback="biscolabLaravelReCaptcha">' . $buttonInnerHTML . '</button>' : ''; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | 43 | } |
| 44 | 44 | \ No newline at end of file |
@@ -37,7 +37,7 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | public function htmlFormButton($buttonInnerHTML = 'Submit'): string { |
| 39 | 39 | |
| 40 | - return ($this->version == 'invisible') ? '<button class="g-recaptcha" data-sitekey="' . $this->api_site_key . '" data-callback="biscolabLaravelReCaptcha">' . $buttonInnerHTML . '</button>' : ''; |
|
| 40 | + return ($this->version == 'invisible') ? '<button class="g-recaptcha" data-sitekey="'.$this->api_site_key.'" data-callback="biscolabLaravelReCaptcha">'.$buttonInnerHTML.'</button>' : ''; |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | } |
| 44 | 44 | \ No newline at end of file |
@@ -18,12 +18,12 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class ReCaptchaController extends Controller { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @return array |
|
| 23 | - */ |
|
| 24 | - public function validateV3(): array { |
|
| 21 | + /** |
|
| 22 | + * @return array |
|
| 23 | + */ |
|
| 24 | + public function validateV3(): array { |
|
| 25 | 25 | |
| 26 | - $token = request()->input(config('recaptcha.default_token_parameter_name', 'token'), ''); |
|
| 27 | - return recaptcha()->validate($token); |
|
| 28 | - } |
|
| 26 | + $token = request()->input(config('recaptcha.default_token_parameter_name', 'token'), ''); |
|
| 27 | + return recaptcha()->validate($token); |
|
| 28 | + } |
|
| 29 | 29 | } |
| 30 | 30 | \ No newline at end of file |
@@ -14,179 +14,179 @@ discard block |
||
| 14 | 14 | |
| 15 | 15 | class ReCaptchaBuilder { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The Site key |
|
| 19 | - * please visit https://developers.google.com/recaptcha/docs/start |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - protected $api_site_key; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * The Secret key |
|
| 26 | - * please visit https://developers.google.com/recaptcha/docs/start |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - protected $api_secret_key; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * The chosen ReCAPTCHA version |
|
| 33 | - * please visit https://developers.google.com/recaptcha/docs/start |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - protected $version; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Whether is true the ReCAPTCHA is inactive |
|
| 40 | - * @var boolean |
|
| 41 | - */ |
|
| 42 | - protected $skip_by_ip = false; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * The API request URI |
|
| 46 | - */ |
|
| 47 | - protected $api_url = 'https://www.google.com/recaptcha/api/siteverify'; |
|
| 48 | - |
|
| 49 | - public function __construct($api_site_key, $api_secret_key, $version = 'v2') { |
|
| 50 | - |
|
| 51 | - $this->setApiSiteKey($api_site_key); |
|
| 52 | - $this->setApiSecretKey($api_secret_key); |
|
| 53 | - $this->setVersion($version); |
|
| 54 | - $this->setSkipByIp($this->skipByIp()); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param string $api_site_key |
|
| 59 | - * |
|
| 60 | - * @return ReCaptchaBuilder |
|
| 61 | - */ |
|
| 62 | - public function setApiSiteKey(string $api_site_key): ReCaptchaBuilder { |
|
| 63 | - |
|
| 64 | - $this->api_site_key = $api_site_key; |
|
| 65 | - |
|
| 66 | - return $this; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @param string $api_secret_key |
|
| 71 | - * |
|
| 72 | - * @return ReCaptchaBuilder |
|
| 73 | - */ |
|
| 74 | - public function setApiSecretKey(string $api_secret_key): ReCaptchaBuilder { |
|
| 75 | - |
|
| 76 | - $this->api_secret_key = $api_secret_key; |
|
| 77 | - |
|
| 78 | - return $this; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @param string $version |
|
| 83 | - * |
|
| 84 | - * @return ReCaptchaBuilder |
|
| 85 | - */ |
|
| 86 | - public function setVersion(string $version): ReCaptchaBuilder { |
|
| 87 | - |
|
| 88 | - $this->version = $version; |
|
| 89 | - |
|
| 90 | - return $this; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @return string |
|
| 95 | - */ |
|
| 96 | - public function getVersion(): string { |
|
| 97 | - |
|
| 98 | - return $this->version; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @param bool $skip_by_ip |
|
| 103 | - * |
|
| 104 | - * @return ReCaptchaBuilder |
|
| 105 | - */ |
|
| 106 | - public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder { |
|
| 107 | - |
|
| 108 | - $this->skip_by_ip = $skip_by_ip; |
|
| 109 | - |
|
| 110 | - return $this; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @return array|mixed |
|
| 115 | - */ |
|
| 116 | - public function getIpWhitelist() { |
|
| 117 | - $whitelist = config('recaptcha.skip_ip', []); |
|
| 118 | - |
|
| 119 | - if(!is_array($whitelist)) { |
|
| 120 | - $whitelist = explode(',', $whitelist); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - return $whitelist; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Checks whether the user IP address is among IPs "to be skipped" |
|
| 128 | - * |
|
| 129 | - * @return boolean |
|
| 130 | - */ |
|
| 131 | - public function skipByIp(): bool { |
|
| 132 | - |
|
| 133 | - return (in_array(request()->ip(), $this->getIpWhitelist())); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Write script HTML tag in you HTML code |
|
| 138 | - * Insert before </head> tag |
|
| 139 | - * |
|
| 140 | - * @param string|null $formId |
|
| 141 | - * @param array|null $configuration |
|
| 142 | - * |
|
| 143 | - * @return string |
|
| 144 | - * @throws Exception |
|
| 145 | - */ |
|
| 146 | - public function htmlScriptTagJsApi(?string $formId = '', ?array $configuration = []): string { |
|
| 147 | - |
|
| 148 | - if ($this->skip_by_ip) { |
|
| 149 | - return ''; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - switch ($this->version) { |
|
| 153 | - case 'v3': |
|
| 154 | - $html = "<script src=\"https://www.google.com/recaptcha/api.js?render={$this->api_site_key}\"></script>"; |
|
| 155 | - break; |
|
| 156 | - default: |
|
| 157 | - $html = "<script src=\"https://www.google.com/recaptcha/api.js\" async defer></script>"; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - if ($this->version == 'invisible') { |
|
| 161 | - if (!$formId) { |
|
| 162 | - throw new Exception("formId required", 1); |
|
| 163 | - } |
|
| 164 | - $html .= '<script> |
|
| 17 | + /** |
|
| 18 | + * The Site key |
|
| 19 | + * please visit https://developers.google.com/recaptcha/docs/start |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + protected $api_site_key; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * The Secret key |
|
| 26 | + * please visit https://developers.google.com/recaptcha/docs/start |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + protected $api_secret_key; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * The chosen ReCAPTCHA version |
|
| 33 | + * please visit https://developers.google.com/recaptcha/docs/start |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + protected $version; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Whether is true the ReCAPTCHA is inactive |
|
| 40 | + * @var boolean |
|
| 41 | + */ |
|
| 42 | + protected $skip_by_ip = false; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * The API request URI |
|
| 46 | + */ |
|
| 47 | + protected $api_url = 'https://www.google.com/recaptcha/api/siteverify'; |
|
| 48 | + |
|
| 49 | + public function __construct($api_site_key, $api_secret_key, $version = 'v2') { |
|
| 50 | + |
|
| 51 | + $this->setApiSiteKey($api_site_key); |
|
| 52 | + $this->setApiSecretKey($api_secret_key); |
|
| 53 | + $this->setVersion($version); |
|
| 54 | + $this->setSkipByIp($this->skipByIp()); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param string $api_site_key |
|
| 59 | + * |
|
| 60 | + * @return ReCaptchaBuilder |
|
| 61 | + */ |
|
| 62 | + public function setApiSiteKey(string $api_site_key): ReCaptchaBuilder { |
|
| 63 | + |
|
| 64 | + $this->api_site_key = $api_site_key; |
|
| 65 | + |
|
| 66 | + return $this; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @param string $api_secret_key |
|
| 71 | + * |
|
| 72 | + * @return ReCaptchaBuilder |
|
| 73 | + */ |
|
| 74 | + public function setApiSecretKey(string $api_secret_key): ReCaptchaBuilder { |
|
| 75 | + |
|
| 76 | + $this->api_secret_key = $api_secret_key; |
|
| 77 | + |
|
| 78 | + return $this; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @param string $version |
|
| 83 | + * |
|
| 84 | + * @return ReCaptchaBuilder |
|
| 85 | + */ |
|
| 86 | + public function setVersion(string $version): ReCaptchaBuilder { |
|
| 87 | + |
|
| 88 | + $this->version = $version; |
|
| 89 | + |
|
| 90 | + return $this; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @return string |
|
| 95 | + */ |
|
| 96 | + public function getVersion(): string { |
|
| 97 | + |
|
| 98 | + return $this->version; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @param bool $skip_by_ip |
|
| 103 | + * |
|
| 104 | + * @return ReCaptchaBuilder |
|
| 105 | + */ |
|
| 106 | + public function setSkipByIp(bool $skip_by_ip): ReCaptchaBuilder { |
|
| 107 | + |
|
| 108 | + $this->skip_by_ip = $skip_by_ip; |
|
| 109 | + |
|
| 110 | + return $this; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @return array|mixed |
|
| 115 | + */ |
|
| 116 | + public function getIpWhitelist() { |
|
| 117 | + $whitelist = config('recaptcha.skip_ip', []); |
|
| 118 | + |
|
| 119 | + if(!is_array($whitelist)) { |
|
| 120 | + $whitelist = explode(',', $whitelist); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + return $whitelist; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Checks whether the user IP address is among IPs "to be skipped" |
|
| 128 | + * |
|
| 129 | + * @return boolean |
|
| 130 | + */ |
|
| 131 | + public function skipByIp(): bool { |
|
| 132 | + |
|
| 133 | + return (in_array(request()->ip(), $this->getIpWhitelist())); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Write script HTML tag in you HTML code |
|
| 138 | + * Insert before </head> tag |
|
| 139 | + * |
|
| 140 | + * @param string|null $formId |
|
| 141 | + * @param array|null $configuration |
|
| 142 | + * |
|
| 143 | + * @return string |
|
| 144 | + * @throws Exception |
|
| 145 | + */ |
|
| 146 | + public function htmlScriptTagJsApi(?string $formId = '', ?array $configuration = []): string { |
|
| 147 | + |
|
| 148 | + if ($this->skip_by_ip) { |
|
| 149 | + return ''; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + switch ($this->version) { |
|
| 153 | + case 'v3': |
|
| 154 | + $html = "<script src=\"https://www.google.com/recaptcha/api.js?render={$this->api_site_key}\"></script>"; |
|
| 155 | + break; |
|
| 156 | + default: |
|
| 157 | + $html = "<script src=\"https://www.google.com/recaptcha/api.js\" async defer></script>"; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + if ($this->version == 'invisible') { |
|
| 161 | + if (!$formId) { |
|
| 162 | + throw new Exception("formId required", 1); |
|
| 163 | + } |
|
| 164 | + $html .= '<script> |
|
| 165 | 165 | function biscolabLaravelReCaptcha(token) { |
| 166 | 166 | document.getElementById("' . $formId . '").submit(); |
| 167 | 167 | } |
| 168 | 168 | </script>'; |
| 169 | - } |
|
| 170 | - elseif ($this->version == 'v3') { |
|
| 169 | + } |
|
| 170 | + elseif ($this->version == 'v3') { |
|
| 171 | 171 | |
| 172 | - $action = array_get($configuration, 'action', 'homepage'); |
|
| 172 | + $action = array_get($configuration, 'action', 'homepage'); |
|
| 173 | 173 | |
| 174 | - $js_custom_validation = array_get($configuration, 'custom_validation', ''); |
|
| 174 | + $js_custom_validation = array_get($configuration, 'custom_validation', ''); |
|
| 175 | 175 | |
| 176 | - // Check if set custom_validation. That function will override default fetch validation function |
|
| 177 | - if ($js_custom_validation) { |
|
| 176 | + // Check if set custom_validation. That function will override default fetch validation function |
|
| 177 | + if ($js_custom_validation) { |
|
| 178 | 178 | |
| 179 | - $validate_function = ($js_custom_validation) ? "{$js_custom_validation}(token);" : ''; |
|
| 180 | - } |
|
| 181 | - else { |
|
| 179 | + $validate_function = ($js_custom_validation) ? "{$js_custom_validation}(token);" : ''; |
|
| 180 | + } |
|
| 181 | + else { |
|
| 182 | 182 | |
| 183 | - $js_then_callback = array_get($configuration, 'callback_then', ''); |
|
| 184 | - $js_callback_catch = array_get($configuration, 'callback_catch', ''); |
|
| 183 | + $js_then_callback = array_get($configuration, 'callback_then', ''); |
|
| 184 | + $js_callback_catch = array_get($configuration, 'callback_catch', ''); |
|
| 185 | 185 | |
| 186 | - $js_then_callback = ($js_then_callback) ? "{$js_then_callback}(response)" : ''; |
|
| 187 | - $js_callback_catch = ($js_callback_catch) ? "{$js_callback_catch}(err)" : ''; |
|
| 186 | + $js_then_callback = ($js_then_callback) ? "{$js_then_callback}(response)" : ''; |
|
| 187 | + $js_callback_catch = ($js_callback_catch) ? "{$js_callback_catch}(err)" : ''; |
|
| 188 | 188 | |
| 189 | - $validate_function = " |
|
| 189 | + $validate_function = " |
|
| 190 | 190 | fetch('/" . config('recaptcha.default_validation_route', 'biscolab-recaptcha/validate') . "?" . config('recaptcha.default_token_parameter_name', 'token') . "=' + token, { |
| 191 | 191 | headers: { |
| 192 | 192 | \"X-Requested-With\": \"XMLHttpRequest\", |
@@ -199,9 +199,9 @@ discard block |
||
| 199 | 199 | .catch(function(err) { |
| 200 | 200 | {$js_callback_catch} |
| 201 | 201 | });"; |
| 202 | - } |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | - $html .= "<script> |
|
| 204 | + $html .= "<script> |
|
| 205 | 205 | var csrfToken = document.head.querySelector('meta[name=\"csrf-token\"]'); |
| 206 | 206 | grecaptcha.ready(function() { |
| 207 | 207 | grecaptcha.execute('{$this->api_site_key}', {action: '{$action}'}).then(function(token) { |
@@ -209,90 +209,90 @@ discard block |
||
| 209 | 209 | }); |
| 210 | 210 | }); |
| 211 | 211 | </script>"; |
| 212 | - } |
|
| 213 | - |
|
| 214 | - return $html; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * @param array|null $configuration |
|
| 219 | - * |
|
| 220 | - * @return string |
|
| 221 | - */ |
|
| 222 | - public function htmlScriptTagJsApiV3(?array $configuration = []): string { |
|
| 223 | - |
|
| 224 | - return $this->htmlScriptTagJsApi('', $configuration); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Call out to reCAPTCHA and process the response |
|
| 229 | - * |
|
| 230 | - * @param string $response |
|
| 231 | - * |
|
| 232 | - * @return boolean|array |
|
| 233 | - */ |
|
| 234 | - public function validate($response) { |
|
| 235 | - |
|
| 236 | - if ($this->skip_by_ip) { |
|
| 237 | - if ($this->returnArray()) { |
|
| 238 | - // Add 'skip_by_ip' field to response |
|
| 239 | - return [ |
|
| 240 | - 'skip_by_ip' => true, |
|
| 241 | - 'score' => 0.9, |
|
| 242 | - 'success' => true |
|
| 243 | - ]; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - return true; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - $params = http_build_query([ |
|
| 250 | - 'secret' => $this->api_secret_key, |
|
| 251 | - 'remoteip' => request()->getClientIp(), |
|
| 252 | - 'response' => $response, |
|
| 253 | - ]); |
|
| 254 | - |
|
| 255 | - $url = $this->api_url . '?' . $params; |
|
| 256 | - |
|
| 257 | - if (function_exists('curl_version')) { |
|
| 258 | - $curl = curl_init($url); |
|
| 259 | - curl_setopt($curl, CURLOPT_HEADER, false); |
|
| 260 | - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
| 261 | - curl_setopt($curl, CURLOPT_TIMEOUT, 1); |
|
| 262 | - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
|
| 263 | - $curl_response = curl_exec($curl); |
|
| 264 | - } |
|
| 265 | - else { |
|
| 266 | - $curl_response = file_get_contents($url); |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - if (is_null($curl_response) || empty($curl_response)) { |
|
| 270 | - if ($this->returnArray()) { |
|
| 271 | - // Add 'error' field to response |
|
| 272 | - return [ |
|
| 273 | - 'error' => 'cURL response empty', |
|
| 274 | - 'score' => 0.1, |
|
| 275 | - 'success' => false |
|
| 276 | - ]; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - return false; |
|
| 280 | - } |
|
| 281 | - $response = json_decode(trim($curl_response), true); |
|
| 282 | - |
|
| 283 | - if ($this->returnArray()) { |
|
| 284 | - return $response; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - return $response['success']; |
|
| 288 | - |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * @return bool |
|
| 293 | - */ |
|
| 294 | - protected function returnArray(): bool { |
|
| 295 | - |
|
| 296 | - return ($this->version == 'v3'); |
|
| 297 | - } |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + return $html; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * @param array|null $configuration |
|
| 219 | + * |
|
| 220 | + * @return string |
|
| 221 | + */ |
|
| 222 | + public function htmlScriptTagJsApiV3(?array $configuration = []): string { |
|
| 223 | + |
|
| 224 | + return $this->htmlScriptTagJsApi('', $configuration); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Call out to reCAPTCHA and process the response |
|
| 229 | + * |
|
| 230 | + * @param string $response |
|
| 231 | + * |
|
| 232 | + * @return boolean|array |
|
| 233 | + */ |
|
| 234 | + public function validate($response) { |
|
| 235 | + |
|
| 236 | + if ($this->skip_by_ip) { |
|
| 237 | + if ($this->returnArray()) { |
|
| 238 | + // Add 'skip_by_ip' field to response |
|
| 239 | + return [ |
|
| 240 | + 'skip_by_ip' => true, |
|
| 241 | + 'score' => 0.9, |
|
| 242 | + 'success' => true |
|
| 243 | + ]; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + return true; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + $params = http_build_query([ |
|
| 250 | + 'secret' => $this->api_secret_key, |
|
| 251 | + 'remoteip' => request()->getClientIp(), |
|
| 252 | + 'response' => $response, |
|
| 253 | + ]); |
|
| 254 | + |
|
| 255 | + $url = $this->api_url . '?' . $params; |
|
| 256 | + |
|
| 257 | + if (function_exists('curl_version')) { |
|
| 258 | + $curl = curl_init($url); |
|
| 259 | + curl_setopt($curl, CURLOPT_HEADER, false); |
|
| 260 | + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
| 261 | + curl_setopt($curl, CURLOPT_TIMEOUT, 1); |
|
| 262 | + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
|
| 263 | + $curl_response = curl_exec($curl); |
|
| 264 | + } |
|
| 265 | + else { |
|
| 266 | + $curl_response = file_get_contents($url); |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + if (is_null($curl_response) || empty($curl_response)) { |
|
| 270 | + if ($this->returnArray()) { |
|
| 271 | + // Add 'error' field to response |
|
| 272 | + return [ |
|
| 273 | + 'error' => 'cURL response empty', |
|
| 274 | + 'score' => 0.1, |
|
| 275 | + 'success' => false |
|
| 276 | + ]; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + return false; |
|
| 280 | + } |
|
| 281 | + $response = json_decode(trim($curl_response), true); |
|
| 282 | + |
|
| 283 | + if ($this->returnArray()) { |
|
| 284 | + return $response; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + return $response['success']; |
|
| 288 | + |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * @return bool |
|
| 293 | + */ |
|
| 294 | + protected function returnArray(): bool { |
|
| 295 | + |
|
| 296 | + return ($this->version == 'v3'); |
|
| 297 | + } |
|
| 298 | 298 | } |
| 299 | 299 | \ No newline at end of file |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | public function getIpWhitelist() { |
| 117 | 117 | $whitelist = config('recaptcha.skip_ip', []); |
| 118 | 118 | |
| 119 | - if(!is_array($whitelist)) { |
|
| 119 | + if (!is_array($whitelist)) { |
|
| 120 | 120 | $whitelist = explode(',', $whitelist); |
| 121 | 121 | } |
| 122 | 122 | |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | } |
| 164 | 164 | $html .= '<script> |
| 165 | 165 | function biscolabLaravelReCaptcha(token) { |
| 166 | - document.getElementById("' . $formId . '").submit(); |
|
| 166 | + document.getElementById("' . $formId.'").submit(); |
|
| 167 | 167 | } |
| 168 | 168 | </script>'; |
| 169 | 169 | } |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | $js_callback_catch = ($js_callback_catch) ? "{$js_callback_catch}(err)" : ''; |
| 188 | 188 | |
| 189 | 189 | $validate_function = " |
| 190 | - fetch('/" . config('recaptcha.default_validation_route', 'biscolab-recaptcha/validate') . "?" . config('recaptcha.default_token_parameter_name', 'token') . "=' + token, { |
|
| 190 | + fetch('/" . config('recaptcha.default_validation_route', 'biscolab-recaptcha/validate')."?".config('recaptcha.default_token_parameter_name', 'token')."=' + token, { |
|
| 191 | 191 | headers: { |
| 192 | 192 | \"X-Requested-With\": \"XMLHttpRequest\", |
| 193 | 193 | \"X-CSRF-TOKEN\": csrfToken.content |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | 'response' => $response, |
| 253 | 253 | ]); |
| 254 | 254 | |
| 255 | - $url = $this->api_url . '?' . $params; |
|
| 255 | + $url = $this->api_url.'?'.$params; |
|
| 256 | 256 | |
| 257 | 257 | if (function_exists('curl_version')) { |
| 258 | 258 | $curl = curl_init($url); |