Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Mar 4, 2021
1 parent 62e52c8 commit 86b0015
Show file tree
Hide file tree
Showing 22 changed files with 1,328 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# v0.1.0
## 03/03/2021

1. [](#new)
* ChangeLog started...
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2021 Trilby Media

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
112 changes: 110 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,110 @@
# grav-plugin-dns-blacklist
A simple yet useful Grav plugin to check if an IP address is DNS Blacklisted
# DNSBlacklist Plugin

The **DNS Blacklist** Plugin is an extension for [Grav CMS](http://github.com/getgrav/grav). Checks an IP address via mutliple DNS Blacklists to see if it's banned. It can be used as a PHP function, Twig function as well as via a Form action.

## Installation

Installing the Dns Blacklist plugin can be done in one of three ways: The GPM (Grav Package Manager) installation method lets you quickly install the plugin with a simple terminal command, the manual method lets you do so via a zip file, and the admin method lets you do so via the Admin Plugin.

### GPM Installation (Preferred)

To install the plugin via the [GPM](http://learn.getgrav.org/advanced/grav-gpm), through your system's terminal (also called the command line), navigate to the root of your Grav-installation, and enter:

bin/gpm install dns-blacklist

This will install the Dns Blacklist plugin into your `/user/plugins`-directory within Grav. Its files can be found under `/your/site/grav/user/plugins/dns-blacklist`.

### Admin Plugin

If you use the Admin Plugin, you can install the plugin directly by browsing the `Plugins`-menu and clicking on the `Add` button.

## Configuration

If you are not using the admin, you should copy the `user/plugins/dns-blacklist/dns-blacklist.yaml` to `user/config/plugins/dns-blacklist.yaml` and only edit that copy.

Here is the default configuration and an explanation of available options:

```yaml
enabled: true
form_error:
list:
- dnsbl-1.uceprotect.net
- dnsbl-2.uceprotect.net
- dnsbl-3.uceprotect.net
- dnsbl.dronebl.org
- dnsbl.sorbs.net
- zen.spamhaus.org
- bl.spamcop.net
- list.dsbl.org
```
Note that if you use the Admin Plugin, a file with your configuration named `dns-blacklist.yaml` will be saved in the `user/config/plugins/`-folder once the configuration is saved in the Admin.

## Usage

#### PHP Usage

You can use this plugin in your own plugin or theme specific PHP code by accessing it via the global Grav object. For example:

```php
$blacklisted = Grav::instance()['dns-blacklist']->isBlacklisted();
if (!empty($blacklisted)) {
echo "Your IP is blacklisted by: " . json_encode($blacklisted);
} else {
echo "Your IP is good!";
}
```

You can also pass in a specific IP address to check:

```php
$blacklisted = Grav::instance()['dns-blacklist']->isBlacklisted('127.0.0.1');
```

#### Twig Usage

Very similar to the PHP usage, you can use the same blacklist class via Twig. Notice the name is `dns_blacklist` compared to `dns-blacklist` from regular PHP to make it more Twig-friendly:

```twig
{% if dns_blacklist.isBlacklisted %}
<h2 class="Error">Your IP is blacklisted, no for for you!</h2>
{% else %}
{% include "forms/form.html.twig" with {form: forms('contact-form')} %}
{% endif %}
```

#### Form Action Usage

You can also use this logic directly in a form action, so that it's checked during form submission. For example, this is a sample page which defines a very simple form and simply checks for blacklisted IPs.

```yaml
---
title: 'DNS Blacklist'
form:
name: dns-blacklist
fields:
name:
label: Name
placeholder: Name
type: text
validate:
required: true
buttons:
-
type: submit
html: true
value: Submit
process:
dns-blacklist: true
message: '<b>Thanks!</b> All good'
---
# IP Blacklist Testing
This is a simple blacklisting form action test page.
```

## Blacklist Providers

There are many blacklist providers. The plugin includes just a few, I suggest checking out https://w3dt.net/tools/dnsbl to test an IP and get a huge list or providers to potentially include.

41 changes: 41 additions & 0 deletions blueprints.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Dns Blacklist
slug: dns-blacklist
type: plugin
version: 0.1.0
description: Checks an IP address via mutliple DNS Blacklists
icon: plug
author:
name: Trilby Media
email: [email protected]
homepage: https://github.com/trilbymedia/grav-plugin-dns-blacklist
demo: http://demo.yoursite.com
keywords: grav, plugin, etc
bugs: https://github.com/trilbymedia/grav-plugin-dns-blacklist/issues
docs: https://github.com/trilbymedia/grav-plugin-dns-blacklist/blob/develop/README.md
license: MIT

dependencies:
- { name: grav, version: '>=1.6.0' }

form:
validation: loose
fields:
enabled:
type: toggle
label: PLUGIN_ADMIN.PLUGIN_STATUS
highlight: 1
default: 0
options:
1: PLUGIN_ADMIN.ENABLED
0: PLUGIN_ADMIN.DISABLED
validate:
type: bool
form_error:
type: text
label: PLUGIN_DNS_BLACKLIST.FORM_ERROR
help: PLUGIN_DNS_BLACKLIST.FORM_ERROR_HELP
list:
type: array
value_only: true
label: PLUGIN_DNS_BLACKLIST.LIST
help: PLUGIN_DNS_BLACKLIST.LIST_HELP
31 changes: 31 additions & 0 deletions classes/Blacklist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Grav\Plugin\DNSBlacklist;

use Grav\Common\Grav;
use Grav\Common\Uri;

class Blacklist
{
public function isBlacklisted($ip = null)
{
$dnsbl_lookup = $this->getDNSBLs();
$ip = $ip ?? Uri::ip();
$listed = [];

if ($ip) {
$reverse_ip = implode(".", array_reverse(explode(".", $ip)));
foreach ($dnsbl_lookup as $host) {
if (checkdnsrr($reverse_ip . "." . $host . ".", "A")) {
$listed[] = $host;
}
}
}
return $listed;
}

protected function getDNSBLs()
{
return Grav::instance()['config']->get('plugins.dns-blacklist.list', []);
}
}
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "trilbymedia/dns-blacklist",
"type": "grav-plugin",
"description": "Checks an IP address via mutliple DNS Blacklists",
"keywords": ["plugin"],
"homepage": "https://github.com/trilbymedia/grav-plugin-dns-blacklist",
"license": "MIT",
"authors": [
{
"name": "Trilby Media",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
"php": ">=7.1.3"
},
"autoload": {
"psr-4": {
"Grav\\Plugin\\DNSBlacklist\\": "classes/"
},
"classmap": ["dns-blacklist.php"]
},
"config": {
"platform": {
"php": "7.1.3"
}
}
}
23 changes: 23 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions dns-blacklist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
namespace Grav\Plugin;

use Composer\Autoload\ClassLoader;
use Grav\Common\Plugin;
use Grav\Common\Uri;
use Grav\Plugin\DNSBlacklist\Blacklist;
use RocketTheme\Toolbox\Event\Event;

/**
* Class DNSBlacklistPlugin
* @package Grav\Plugin
*/
class DNSBlacklistPlugin extends Plugin
{
protected $blacklist;

public static function getSubscribedEvents(): array
{
return [
'onPluginsInitialized' => [
['onPluginsInitialized', 0]
]
];
}

public function autoload(): ClassLoader
{
return require __DIR__ . '/vendor/autoload.php';
}

public function onPluginsInitialized(): void
{
if ($this->isAdmin()) {
return;
}

$this->enable([
'onFormProcessed' => ['onFormProcessed', 0],
'onTwigVariables' => ['onTwigVariables', 0],
]);

$this->blacklist = new Blacklist();
$this->grav['dns-blacklist'] = $this->blacklist;
}

public function onFormProcessed(Event $event)
{
/** @var Form $form */
$form = $event['form'];
$action = $event['action'];

switch ($action) {
case 'dns-blacklist':

$ip = Uri::ip();
$blacklisted = $this->blacklist->isBlacklisted($ip);

if (!empty($blacklisted)) {
$custom_form_error = $this->config->get('plugins.dns-blacklist.form_error');
$msg = 'Your IP address: ' . $ip . ' is blacklisted by ' . json_encode($blacklisted);
$this->grav['log']->notice($msg);

$msg = $custom_form_error ?: $msg;
$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $form,
'message' => $msg,
]));
$event->stopPropagation();
return;
}

break;
}
}

public function onTwigVariables(Event $event = null): void
{
$twig = $this->grav['twig'];
$twig->twig_vars['dns_blacklist'] = $this->blacklist;
}

}
11 changes: 11 additions & 0 deletions dns-blacklist.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
enabled: true
form_error:
list:
- dnsbl-1.uceprotect.net
- dnsbl-2.uceprotect.net
- dnsbl-3.uceprotect.net
- dnsbl.dronebl.org
- dnsbl.sorbs.net
- zen.spamhaus.org
- bl.spamcop.net
- list.dsbl.org
6 changes: 6 additions & 0 deletions languages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
PLUGIN_DNS_BLACKLIST:
FORM_ERROR: Custom Form Error
FORM_ERROR_HELP: If provided any blacklisted IPs will see this message instead of the detailed one
LIST: Blacklist Providers
LIST_HELP: Remove or add any blacklist providers you wish here
7 changes: 7 additions & 0 deletions vendor/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitb1a5f64cba3f6a4af6d79a422380f4c4::getLoader();
Loading

0 comments on commit 86b0015

Please sign in to comment.