Skip to content

Commit

Permalink
lift up
Browse files Browse the repository at this point in the history
  • Loading branch information
Raruto committed Jan 29, 2021
1 parent 41c6b7c commit 126a699
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ composer.phar

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
composer.lock
54 changes: 53 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
# cockpit-honeypot
Honeypot addon for Cockpit CMS
Honeypot addon for [Cockpit CMS](http://getcockpit.com/)

## Features

- Add "honeypot" switch to Forms settings
- Automatically append "honeypot" field to cockpit `Forms::open` api
- Automatically validate "honeypot" submitted field to cockpit `Forms::submit`
- Automatically exclude invalid submissions from saved entries

## Installation

### Manual

Download [latest release](https://github.com/Raruto/cockpit-honeypot) and extract to `COCKPIT_PATH/addons/Honeypot` directory

### Git

```sh
git clone https://github.com/Raruto/cockpit-honeypot.git ./addons/Honeypot
```

### Cockpit CLI

```sh
php ./cp install/addon --name Honeypot --url https://github.com/Raruto/cockpit-honeypot.git
```

### Composer

1. Make sure path to cockpit addons is defined in your projects' _composer.json_ file:

```json
{
"name": "MY_PROJECT",
"extra": {
"installer-paths": {
"cockpit/addons/{$name}": ["type:cockpit-module"]
}
}
}
```

2. In your project root run:

```sh
composer require raruto/cockpit-honeypot
```

---

**Related projects:** [ExtendedForms](https://github.com/Raruto/cockpit-extended-forms), [FormValidation](https://github.com/raffaelj/cockpit_FormValidation)

**Compatibile with:** [![Cockpit CMS](https://img.shields.io/badge/cockpit-0.11.2-1EB300.svg?style=flat)](https://github.com/agentejo/cockpit)
94 changes: 94 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Cockpit honeypot addon
*
* @author Raruto
* @package cockpit-blog
* @license MIT
*/

/**
* Helper function to generate unique honeypot names
*/
$app->module('honeypot')->extend([

'getHoneypotFieldName' => function($name) {
return "hp-$name";
},

]);

/**
* Append "honeypot" field to cockpit forms api
*
* @link https://github.com/Raruto/cockpit-extended-forms
* @see { cockpit( 'forms:open' , 'form-name' ) }
*/
if ($app->module('extendedforms')) {

$app->on('forms.open.after', function($name, &$options) {
$frm = $this->module('forms')->form($name);
if (!empty($frm['honeypot'])) {
$this->renderView('honeypot:views/honeypot.php', ['name' => $name]);
}
});

} else {

/**
* Save default form "open" function for later override.
*
* @see { cockpit/modules/forms/bootstrap.php }
*/
$OPEN = $app->module('forms')->open;

$app->module('forms')->extend([
'open' => function($name, $options = []) use($OPEN) {
$frm = $this->form($name);
$OPEN($name, $options);
if(!empty($frm['honeypot'])) {
$this->app->renderView('honeypot:views/honeypot.php');
}
},
]);

}

/**
* Custom form validation hook
*
* @param string $form submitted form name
* @param array $data submitted form entry
* @param array $frm form collection
* @param array $options form options
*
* @return void
*
* @see { cockpit/modules/forms/bootstrap.php }
*/
$app->on('forms.submit.before', function($form, &$data, $frm, &$options) {

// check for [ "honeypot" => true ] in form collection
if (empty( $frm['honeypot'])) return;

$hp_name = $this->module('honeypot')->getHoneypotFieldName($form);

// validate "honeypot" field in submitted entry
if (!empty($data[$hp_name])) {
$this->stop(200);
}
// exclude "honeypot" field from saved entries
unset($data[$hp_name]);

});

/**
* Add honeypot button to form settings
*
* @return void
*
* @see { cockpit/modules/forms/views/form.php }
*/
$app->on('forms.settings.aside', function() {
$this->app->renderView('honeypot:views/settings-field.php');
});
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "raruto/cockpit-honeypot",
"description": "Honeypot addon for Cockpit CMS",
"homepage": "https://github.com/Raruto/cockpit-honeypot/",
"keywords": ["cockpit", "forms", "addon"],
"type": "cockpit-module",
"license": "MIT",
"authors": [
{
"name": "Raruto",
"homepage": "https://raruto.github.io/"
}
],
"require": {
"php": ">= 7.3",
"composer/installers": "^1.10"
},
"suggest": {
"aheinze/cockpit": "Please install Cockpit before installing this addon"
},
"extra": {
"installer-name": "Honeypot"
}
}
7 changes: 7 additions & 0 deletions views/honeypot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<fieldset class="d-none">
<legend>@lang('Before you begin')</legend>
<div>
<label for="{{ $app->module('honeypot')->getHoneypotFieldName($name) }}">@lang('Please read this text carefully and avoid:')</label>
<input name="form[{{ $app->module('honeypot')->getHoneypotFieldName($name) }}]" type="text" value="" tabindex="-1" autocomplete="nope" placeholder="@lang('filling in this field')">
</div>
</fieldset>
3 changes: 3 additions & 0 deletions views/settings-field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="uk-margin">
<field-boolean bind="form.honeypot" label="@lang('Honeypot')"></field-boolean>
</div>

0 comments on commit 126a699

Please sign in to comment.