-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d1d6bc9
Showing
9 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# hCaptcha spam protection | ||
|
||
Protect your forms with hCaptcha | ||
|
||
## Installation | ||
|
||
As soon this module is published: | ||
|
||
``` | ||
composer require oetiker/silverstripe-hcaptcha dev | ||
``` | ||
|
||
## Configuration | ||
|
||
In your `app/_config/hcaptcha.yml` | ||
|
||
```yaml | ||
|
||
Oetiker\hCaptcha\Forms\hCaptchaField: | ||
site_key: 'your_site_key' | ||
secret_key: 'your_secret_key' | ||
|
||
``` | ||
|
||
## Usage | ||
|
||
In php: | ||
|
||
```injectablephp | ||
use Oetiker\hCaptcha\hCaptchaProtector\Forms | ||
new hCaptchaField('SpamProtection', 'SpamProtection', null); | ||
``` | ||
|
||
Or if you use Userforms: | ||
|
||
![img.png](img/img.png) | ||
|
||
|
||
Contributions are welcome :) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "oetiker/silverstripe-hcaptcha", | ||
"type": "silverstripe-vendormodule", | ||
"description": "Protect your forms with hCaptcha", | ||
"homepage": "https://oetiker.ch", | ||
"license": "MIT", | ||
"keywords": [ | ||
"silverstripe", | ||
"Spamprotection", | ||
"captcha", | ||
"hcaptcha" | ||
], | ||
"authors": [ | ||
{"name": "Tobias Bossert", "email": "[email protected]"} | ||
], | ||
"require": { | ||
"silverstripe/framework": "^4", | ||
"silverstripe/vendor-plugin": "*", | ||
"php": "^7.4 || ^8.0", | ||
"ext-curl": "*", | ||
"ext-json": "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Oetiker\\hCaptcha\\": "src/", | ||
"Oetiker\\hCaptcha\\Forms\\" : "src/Froms" | ||
} | ||
}, | ||
"version": "0.0.1" | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
en: | ||
Oetiker\hCaptcha\Forms\hCaptchaField: | ||
EMPTY: "Bitte captcha beantworten. Wenn nicht sichtbar, Javascript von js.hcaptcha.com erlauben" | ||
NOSCRIPT: "Javascript muss aktiv sein um dieses Formular absenden zu kommen" | ||
VALIDATE_ERROR: "Captcha konnte nicht verfiziert werden" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
en: | ||
Oetiker\hCaptcha\Forms\hCaptchaField: | ||
EMPTY: "Please answer the captcha, if you do not see the captcha please enable JavaScript" | ||
NOSCRIPT: "You must enable JavaScript to submit this form" | ||
VALIDATE_ERROR: "Captcha could not be validated" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
namespace Oetiker\hCaptcha\Forms; | ||
|
||
use SilverStripe\Control\Controller; | ||
use SilverStripe\Forms\FormField; | ||
use SilverStripe\View\Requirements; | ||
|
||
class hCaptchaField extends FormField | ||
{ | ||
/** | ||
* @var string Your siteKey | ||
*/ | ||
private static $site_key = ""; | ||
|
||
/** | ||
* @var string Your secret key | ||
*/ | ||
private static $secret_key = ""; | ||
|
||
/** | ||
* @var string Currently supports [light|dark] | ||
*/ | ||
private static $data_theme = "light"; | ||
|
||
/** | ||
* @var string Currently supports [normal|invisible|compact] | ||
*/ | ||
private static $data_size = "normal"; | ||
|
||
/** | ||
* @var string Set tabindex | ||
*/ | ||
private static $data_tabindex = "0"; | ||
|
||
/** | ||
* @var bool Submit remote ip in validation requests | ||
*/ | ||
private static $submit_remote_ip = false; | ||
|
||
/** | ||
* Validates the h-captcha-response | ||
* @inerhitDoc | ||
*/ | ||
public function validate($validator): bool | ||
{ | ||
|
||
$captchaResponse = Controller::curr()->getRequest()->requestVar('h-captcha-response'); | ||
$submit_remote_ip = self::config()->get('submit_remote_ip'); | ||
|
||
// Empty response | ||
if (!isset($captchaResponse)) { | ||
$validator->validationError($this->name, _t('Oetiker\hCaptcha\Forms\hCaptchaField.EMPTY', '_Please answer the captcha, if you do not see the captcha you must enable JavaScript')); | ||
return false; | ||
} | ||
|
||
$validatorRequest = [ | ||
'secret' => self::config()->get('secret_key'), | ||
'response' => $captchaResponse, | ||
'sitekey' => self::config()->get('site_key'), | ||
]; | ||
|
||
// Submit remote ip if enabled | ||
if ($submit_remote_ip) $validatorRequest['remoteip'] = Controller::curr()->getRequest()->getIP(); | ||
|
||
$curlOptions = [ | ||
'CURLOPT_URL' => 'https://hcaptcha.com/siteverify', | ||
'CURLOPT_POST' => true, | ||
'CURLOPT_POSTFIELDS' => http_build_query($validatorRequest), | ||
'CURLOPT_RETURNTRANSFER' => true, | ||
]; | ||
$verify = curl_init(); | ||
curl_setopt_array($verify, $curlOptions); | ||
$response = curl_exec($verify); | ||
|
||
$responseData = json_decode($response); | ||
if ($responseData->success) { | ||
return true; | ||
} else { | ||
$validator->validationError($this->name, _t('Oetiker\hCaptcha\Forms\hCaptchaField.VALIDATE_ERROR', '_Captcha could not be validated')); | ||
return false; | ||
} | ||
|
||
} | ||
|
||
public function Field($properties = []) | ||
{ | ||
$siteKey = self::config()->get('site_key'); | ||
$secretKey = self::config()->get('secret_key'); | ||
|
||
if (empty($siteKey) || empty($secretKey)) { | ||
user_error('You must configure hCaptcha $site_key and $secret_key', E_USER_ERROR); | ||
} | ||
|
||
Requirements::javascript('https://js.hcaptcha.com/1/api.js'); | ||
|
||
return parent::Field($properties); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public static function getSiteKey(): string | ||
{ | ||
return self::config()->get('site_key'); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public static function getDataTheme(): string | ||
{ | ||
return self::config()->get('data_theme'); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public static function getDataSize(): string | ||
{ | ||
return self::config()->get('data_size'); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public static function getDataTabindex(): string | ||
{ | ||
return self::config()->get('data_tabindex'); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Oetiker\hCaptcha; | ||
|
||
|
||
use Oetiker\hCaptcha\Forms\hCaptchaField; | ||
|
||
class hCaptchaProtector implements \SilverStripe\SpamProtection\SpamProtector | ||
{ | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getFormField($name = 'hCaptcha', $title = 'hCaptcha', $value = null) | ||
{ | ||
return new hCaptchaField($name, $title, $value); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function setFieldMapping($fieldMapping) | ||
{ | ||
// TODO: Implement setFieldMapping() method. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<noscript> | ||
<p><%t Oetiker\hCaptcha\Forms\hCaptchaField.NOSCRIPT "You must enable JavaScript to submit this form" %></p> | ||
</noscript> | ||
|
||
<div class="h-captcha" | ||
data-sitekey="$SiteKey" | ||
data-theme="$DataTheme" | ||
data-size="$DataSize" | ||
data-tabindex="$DataTabindex" | ||
></div> | ||
|