Skip to content

Commit f5d3d66

Browse files
authored
Merge pull request #23 from rubekx/feature/hcaptcha-enable-disable
Feature/hcaptcha enable disable
2 parents 5b7d5ec + 31f4f53 commit f5d3d66

File tree

4 files changed

+69
-11
lines changed

4 files changed

+69
-11
lines changed

README.md

+30-3
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ php artisan vendor:publish --provider="Scyllaly\HCaptcha\HCaptchaServiceProvider
3838

3939
### Configuration
4040

41-
Add `HCAPTCHA_SECRET` and `HCAPTCHA_SITEKEY` in **.env** file :
41+
Add `HCAPTCHA_SECRET`, `HCAPTCHA_SITEKEY` and `HCAPTCHA_ENABLED` in **.env** file :
4242

4343
```
4444
HCAPTCHA_SECRET=secret-key
4545
HCAPTCHA_SITEKEY=site-key
46+
HCAPTCHA_ENABLED=true
4647
```
4748

4849
(You can obtain them from [Official Developer Guide](https://docs.hcaptcha.com/api#getapikey))
@@ -89,15 +90,41 @@ callback submit the form on a successful captcha verification.
8990

9091
#### Validation
9192

92-
Add `'h-captcha-response' => 'required|HCaptcha'` to rules array :
93+
There are two ways to apply HCaptcha validation to your form:
94+
95+
#### 1. Basic Approach
96+
97+
This method always applies the HCaptcha validation rule.
9398

9499
```php
95100
$validate = Validator::make(Input::all(), [
96-
'h-captcha-response' => 'required|HCaptcha'
101+
'h-captcha-response' => 'required|HCaptcha'
97102
]);
98103

99104
```
100105

106+
In this approach, the `h-captcha-response` field is required and validated using the `HCaptcha` rule without any conditions.
107+
108+
#### 2. Conditional Approach
109+
110+
This method applies the HCaptcha validation rule only if the `HCAPTCHA_ENABLED` environment variable is set to `true`.
111+
112+
```php
113+
$isHcaptchaEnabled = env('HCAPTCHA_ENABLED');
114+
$rules = [
115+
// Other validation rules...
116+
];
117+
118+
if ($isHcaptchaEnabled) {
119+
$rules['h-captcha-response'] = 'required|HCaptcha';
120+
}
121+
122+
$request->validate($rules);
123+
124+
```
125+
126+
In this approach, the `h-captcha-response` field will be required and validated using the `HCaptcha` rule only when `HCAPTCHA_ENABLED` is set to `true`. This adds flexibility to your validation logic, allowing you to enable or disable HCaptcha validation as needed.
127+
101128
##### Custom Validation Message
102129

103130
Add the following values to the `custom` array in the `validation` language file :

src/HCaptcha.php

+34-6
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,34 @@ class HCaptcha
3535
* @var array
3636
*/
3737
protected $verifiedResponses = [];
38-
38+
3939
/**
4040
* @var null
4141
* lastScore
4242
*/
4343
protected $lastScore = null;
44-
45-
44+
45+
/**
46+
* Whether to use hCaptcha or not.
47+
*
48+
* @var bool
49+
*/
50+
protected $enabled;
51+
4652
/**
4753
* HCaptcha.
4854
*
4955
* @param string $secret
5056
* @param string $sitekey
5157
* @param array $options
58+
* @param bool $enabled
5259
*/
53-
public function __construct($secret, $sitekey, $options = [])
60+
public function __construct($secret, $sitekey, $options = [], $enabled = true)
5461
{
5562
$this->secret = $secret;
5663
$this->sitekey = $sitekey;
5764
$this->http = new Client($options);
65+
$this->enabled = $enabled;
5866
}
5967

6068
/**
@@ -66,6 +74,10 @@ public function __construct($secret, $sitekey, $options = [])
6674
*/
6775
public function display($attributes = [])
6876
{
77+
if (!$this->enabled) {
78+
return '';
79+
}
80+
6981
$attributes = $this->prepareAttributes($attributes);
7082
return '<div' . $this->buildAttributes($attributes) . '></div>';
7183
}
@@ -89,6 +101,10 @@ public function displayWidget($attributes = [])
89101
*/
90102
public function displaySubmit($formIdentifier, $text = 'submit', $attributes = [])
91103
{
104+
if (!$this->enabled) {
105+
return sprintf('<button%s><span>%s</span></button>', $this->buildAttributes($attributes), $text);
106+
}
107+
92108
$javascript = '';
93109
if (!isset($attributes['data-callback'])) {
94110
$functionName = 'onSubmit' . str_replace(['-', '=', '\'', '"', '<', '>', '`'], '', $formIdentifier);
@@ -118,6 +134,10 @@ public function displaySubmit($formIdentifier, $text = 'submit', $attributes = [
118134
*/
119135
public function renderJs($lang = null, $callback = false, $onLoadClass = 'onloadCallBack')
120136
{
137+
if (!$this->enabled) {
138+
return '';
139+
}
140+
121141
return '<script src="' . $this->getJsLink($lang, $callback, $onLoadClass) . '" async defer></script>' . "\n";
122142
}
123143

@@ -131,11 +151,15 @@ public function renderJs($lang = null, $callback = false, $onLoadClass = 'onload
131151
*/
132152
public function verifyResponse($response, $clientIp = null)
133153
{
154+
if (!$this->enabled) {
155+
return true; // Always true if hCaptcha is disabled
156+
}
157+
134158
if (empty($response)) {
135159
return false;
136160
}
137161

138-
// Return true if response already verfied before.
162+
// Return true if response already verified before.
139163
if (in_array($response, $this->verifiedResponses)) {
140164
return true;
141165
}
@@ -194,6 +218,10 @@ public function verifyRequest(Request $request)
194218
*/
195219
public function getJsLink($lang = null, $callback = false, $onLoadClass = 'onloadCallBack')
196220
{
221+
if (!$this->enabled) {
222+
return '';
223+
}
224+
197225
$client_api = static::CLIENT_API;
198226
$params = [];
199227

@@ -202,7 +230,7 @@ public function getJsLink($lang = null, $callback = false, $onLoadClass = 'onloa
202230

203231
return $client_api . '?' . http_build_query($params);
204232
}
205-
233+
206234
/**
207235
* Get the score from the last successful hCaptcha verification.
208236
*

src/HCaptchaServiceProvider.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ public function register()
5858
return new HCaptcha(
5959
$hCaptcha['secret'],
6060
$hCaptcha['sitekey'],
61-
$hCaptcha['options']
61+
$hCaptcha['options'],
62+
$hCaptcha['enabled'],
6263
);
6364
} else {
6465
return new HCaptcha(
6566
$app['config']['HCaptcha.secret'],
6667
$app['config']['HCaptcha.sitekey'],
67-
$app['config']['HCaptcha.options']
68+
$app['config']['HCaptcha.options'],
69+
$app['config']['HCaptcha.enabled'],
6870
);
6971
}
7072
});

src/config/config.php

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
return [
44
'secret' => env('HCAPTCHA_SECRET'),
55
'sitekey' => env('HCAPTCHA_SITEKEY'),
6+
'enabled' => env('HCAPTCHA_ENABLED', true), //Enable or disable hCaptcha for development environments
67
'server-get-config' => false,
78
'options' => [
89
'timeout' => 30,

0 commit comments

Comments
 (0)