-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns-blacklist.php
91 lines (75 loc) · 2.44 KB
/
dns-blacklist.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use Grav\Common\Plugin;
use Grav\Common\Twig\Twig;
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],
'onTwigPageVariables' => ['onTwigVariables', 0],
'onTwigSiteVariables' => ['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'];
$params = $event['params'];
switch ($action) {
case 'dns-blacklist':
if (!is_bool($params)) {
$ip = $this->grav['twig']->processString((string)$params, array('form' => $form));
} else {
$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;
}
}