Skip to content

Commit

Permalink
initial publish
Browse files Browse the repository at this point in the history
  • Loading branch information
sirtoobii committed Feb 25, 2022
0 parents commit d1d6bc9
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Readme.md
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 added _config/config.yml
Empty file.
30 changes: 30 additions & 0 deletions composer.json
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"
}
Binary file added img/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions lang/de.yml
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"
5 changes: 5 additions & 0 deletions lang/en.yml
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"
133 changes: 133 additions & 0 deletions src/Froms/hCaptchaField.php
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');
}


}
26 changes: 26 additions & 0 deletions src/hCaptchaProtector.php
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.
}
}
11 changes: 11 additions & 0 deletions templates/Oetiker/hCaptcha/Forms/hCaptchaField.ss
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>

0 comments on commit d1d6bc9

Please sign in to comment.