-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
6 changed files
with
182 additions
and
2 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
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 |
---|---|---|
@@ -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) |
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,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'); | ||
}); |
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,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" | ||
} | ||
} |
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,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> |
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,3 @@ | ||
<div class="uk-margin"> | ||
<field-boolean bind="form.honeypot" label="@lang('Honeypot')"></field-boolean> | ||
</div> |