Skip to content

Commit

Permalink
init work
Browse files Browse the repository at this point in the history
  • Loading branch information
DevKCode committed Jun 22, 2023
0 parents commit 781d3ad
Show file tree
Hide file tree
Showing 20 changed files with 678 additions and 0 deletions.
15 changes: 15 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Contributing

- Maintenance on this module is a shared effort of those who use it
- To contribute improvements to the code, ensure you raise a pull request and discuss with the module maintainers
- Please follow the Silverstripe CMS [code contribution guidelines](https://docs.silverstripe.org/en/contributing/code/) and [Module Standard](https://docs.silverstripe.org/en/developer_guides/extending/modules/#module-standard)
- Supply documentation that follows the [GitHub Flavored Markdown](https://help.github.com/articles/markdown-basics/) conventions
- When having discussions about this module in issues or pull request please adhere to the [Silverstripe CMS Community Code of Conduct](https://docs.silverstripe.org/en/project_governance/code_of_conduct/)

## Contributor license agreement

By supplying code to this module in patches, tickets and pull requests, you agree to assign copyright
of that code to MODULE_COPYRIGHT_HOLDER_HERE., on the condition that these code changes are released under the
same BSD license as the original module. We ask for this so that the ownership in the license is clear
and unambiguous. By releasing this code under a permissive license such as BSD, this copyright assignment
won't prevent you from using the code in any way you see fit.
12 changes: 12 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Copyright (c)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
143 changes: 143 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
Turnstile Smart CAPTCHA
=================
(Please note this is a initial work, Have not done any Test yet not ideal for production environment)
Adds a "spam protection" field to SilverStripe userforms using Cloudflare's
[smart CAPTCHA](https://developers.cloudflare.com/turnstile) service.

## Requirements
* SilverStripe 5.x
* [SilverStripe Spam Protection
3.x](https://github.com/silverstripe/silverstripe-spamprotection/)
* PHP CURL

## Installation
```
composer require silverstripe-terraformers/turnstile-captcha
```

After installing the module via composer or manual install you must set the spam
protector to NocaptchaProtector, this needs to be set in your site's config file
normally this is mysite/\_config/config.yml.
```yml
SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension:
default_spam_protector: Terraformers\TurnstileCaptcha\Forms\TurnstileCaptchaProtector
```
Finally, add the "spam protection" field to your form by calling
``enableSpamProtection()`` on the form object.
```php
$form->enableSpamProtection();
```

## Configuration
There are multiple configuration options for the field, you must set the
site_key and the secret_key which you can get from the [reCAPTCHA
page](https://www.google.com/recaptcha). These configuration options must be
added to your site's yaml config typically this is mysite/\_config/config.yml.
```yml
Terraformers\TurnstileCaptcha\Forms\TurnstileCaptchaField:
site_key: "YOUR_SITE_KEY" #Your site key (required)
secret_key: "YOUR_SECRET_KEY" #Your secret key (required)
verify_ssl: true #Allows you to disable php-curl's SSL peer verification by setting this to false (optional, defaults to true)
default_theme: "light" #Default theme color (optional, light or dark, defaults to light)
default_handle_submit: true #Default setting for whether nocaptcha should handle form submission. See "Handling form submission" below.
proxy_server: "" #Your proxy server address (optional)
proxy_port: "" #Your proxy server address port (optional)
proxy_auth: "" #Your proxy server authentication information (optional)
```
## Adding field labels
If you want to add a field label or help text to the Captcha field you can do so
like this:
```php
$form->enableSpamProtection()
->fields()->fieldByName('Captcha')
->setTitle("Spam protection")
->setDescription("Please tick the box to prove you're a human and help us stop spam.");
```

### Commenting Module
When your using the
[silverstripe/comments](https://github.com/silverstripe/silverstripe-comments)
module you must add the following (per their documentation) to your \_config.php
in order to use Terraformers\TurnstileCaptcha on comment forms.

```php
CommentingController::add_extension('CommentSpamProtection');
```

## Retrieving the Verify Response

If you wish to manually retrieve the Site Verify response in you form action use
the `getVerifyResponse()` method

```php
function doSubmit($data, $form) {
$captchaResponse = $form->Fields()->fieldByName('Captcha')->getVerifyResponse();

// $captchaResponse = array (size=5) [
// 'success' => boolean true
// 'challenge_ts' => string '2020-09-08T20:48:34Z' (length=20)
// 'hostname' => string 'localhost' (length=9)
// 'score' => float 0.9
// 'action' => string 'submit' (length=6)
// ];
}
```

## Handling form submission
By default, the javascript included with this module will add a submit event handler to your form.

If you need to handle form submissions in a special way (for example to support front-end validation),
you can choose to handle form submit events yourself.

This can be configured site-wide using the Config API
```yml
Terraformers\TurnstileCaptcha\Forms\TurnstileCaptchaField:
default_handle_submit: false
```
Or on a per form basis:
```php
$captchaField = $form->Fields()->fieldByName('Captcha');
$captchaField->setHandleSubmitEvents(false);
```

With this configuration no event handlers will be added by this module to your form. Instead, a
function will be provided called `nocaptcha_handleCaptcha` which you can call from your code
when you're ready to submit your form. It has the following signature:
```js
function nocaptcha_handleCaptcha(form, callback)
```
`form` must be the form element, and `callback` should be a function that finally submits the form,
though it is optional.

In the simplest case, you can use it like this:
```js
document.addEventListener("DOMContentLoaded", function(event) {
// where formID is the element ID for your form
const form = document.getElementById(formID);
const submitListener = function(event) {
event.preventDefault();
let valid = true;
/* Your validation logic here */
if (valid) {
nocaptcha_handleCaptcha(form, form.submit.bind(form));
}
};
form.addEventListener('submit', submitListener);
});
```

## Reporting an issue

When you're reporting an issue please ensure you specify what version of
SilverStripe you are using i.e. 3.1.3, 3.2beta, master etc. Also be sure to
include any JavaScript or PHP errors you receive, for PHP errors please ensure
you include the full stack trace. Also please include how you produced the
issue. You may also be asked to provide some of the classes to aid in
re-producing the issue. Stick with the issue, remember that you seen the issue
not the maintainer of the module so it may take allot of questions to arrive at
a fix or answer.
4 changes: 4 additions & 0 deletions _config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

// You need this file if you don't have anything in the _config folder. If that folder exists
// and is not empty then you can delete this file.
41 changes: 41 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "silverstripe-terraformers/turnstile-captcha",
"description": "Silverstripe CMS Turnstile Captcha Spam Protection Field",
"type": "silverstripe-vendormodule",
"keywords": [
"silverstripe",
"recaptcha",
"spamprotection",
"turnstile"
],
"license": "BSD-3-Clause",
"authors": [
{
"name": "Dev Sundar",
"email": "[email protected]"
}
],
"require": {
"php": "^8.1",
"silverstripe/framework": "^5.0",
"silverstripe/spamprotection": "^3 | ^4"
},
"autoload": {
"psr-4": {
"Terraformers\\TurnstileCaptcha\\": "src/"
}
},
"extra": {
"expose": [
"client/dist"
]
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"allow-plugins": {
"composer/installers": true,
"silverstripe/vendor-plugin": true
}
}
}
8 changes: 8 additions & 0 deletions docs/en/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Documentation

Add your documentation in the `docs/en/` folder as markdown. Note the "en/" part of the path refers to the language (English in this case) used in the documentation you provide.
If your documentation is vast, you can split it into many markdown files and sub folders.

Look over the [guidance on documentation](https://docs.silverstripe.org/en/contributing/documentation/)

Make sure to remove this readme in your actual module!
4 changes: 4 additions & 0 deletions lang/de.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
de:
Terraformers\TurnstileCaptcha\Forms\CaptchaField:
NOSCRIPT: "Sie müssen JavaScript aktivieren um dieses Formular zu übermitteln"
VALIDATE_ERROR: "Spam-Schutz konnte nicht geprüft werden"
4 changes: 4 additions & 0 deletions lang/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
Terraformers\TurnstileCaptcha\Forms\CaptchaField:
NOSCRIPT: "You must enable JavaScript to submit this form"
VALIDATE_ERROR: "Captcha could not be validated"
4 changes: 4 additions & 0 deletions lang/fr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fr:
Terraformers\TurnstileCaptcha\Forms\CaptchaField:
NOSCRIPT: "Vous devez activer JavaScript pour soumettre ce formulaire"
VALIDATE_ERROR: "Captcha n'a pas pu être validé"
4 changes: 4 additions & 0 deletions lang/ru.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ru:
Terraformers\TurnstileCaptcha\Forms\CaptchaField:
NOSCRIPT: "Укажите ответ на капчу, если вы её не видите вам необходимо включить JavaScript."
VALIDATE_ERROR: "Ошибка проверки капчи - попробуйте ещё раз."
13 changes: 13 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Silverstripe">
<description>CodeSniffer ruleset for Silverstripe coding conventions.</description>

<file>src</file>
<file>tests</file>

<!-- base rules are PSR-12 -->
<rule ref="PSR12" >
<!-- You may need to use this exclusion if you override some core methods -->
<!--<exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps" />-->
</rule>
</ruleset>
8 changes: 8 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="Default">
<directory>tests/php</directory>
</testsuite>
</testsuites>
</phpunit>
Loading

0 comments on commit 781d3ad

Please sign in to comment.