-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Honey.php
99 lines (77 loc) · 2.11 KB
/
Honey.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
92
93
94
95
96
97
98
99
<?php
namespace Lukeraymonddowning\Honey;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Lukeraymonddowning\Honey\InputNameSelectors\InputNameSelector;
use Lukeraymonddowning\Honey\Models\Spammer;
class Honey
{
protected static Collection $checks;
protected static $failUsing;
protected static $config;
protected $isEnabled = false;
protected $hooks = [
'beforeFailing' => []
];
public function __construct(Collection $checks, callable $failUsing, $config)
{
static::$checks = $checks;
static::$failUsing = $failUsing;
static::$config = $config;
if (Features::spammerIpTrackingIsEnabled()) {
$this->registerSpammerTracking();
}
$this->isEnabled = in_array(config('app.env'), $config['environments']);
}
public function isEnabled()
{
return $this->isEnabled;
}
public function disable()
{
$this->isEnabled = false;
}
public function enable()
{
$this->isEnabled = true;
}
protected function registerSpammerTracking()
{
$this->beforeFailing(fn(Request $request) => Spammer::markAttempt($request->ip()));
}
public function check($data)
{
if (!$this->isEnabled()) {
return true;
}
return static::$checks->map->passes($data)->filter()->count() === static::$checks->count();
}
public static function failUsing(callable $function)
{
static::$failUsing = $function;
}
public function beforeFailing(callable $hook)
{
$this->hooks['beforeFailing'][] = $hook;
}
public function fail()
{
if (!$this->isEnabled()) {
return;
}
$this->runHooks('beforeFailing');
return app()->call(static::$failUsing);
}
public function runHooks($type)
{
collect($this->hooks[$type])->each(fn($hook) => app()->call($hook));
}
public function recaptcha()
{
return app('honey-recaptcha');
}
public function inputs()
{
return app(InputNameSelector::class);
}
}