1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Torralbodavid\DuckFunkCore\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Closure; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
use Illuminate\Support\Facades\Route; |
9
|
|
|
use Torralbodavid\DuckFunkCore\Models\Arcturus\Bans; |
10
|
|
|
|
11
|
|
|
class BanMiddleware |
12
|
|
|
{ |
13
|
|
|
private $currentTimestamp; |
14
|
|
|
private Bans $ban; |
|
|
|
|
15
|
|
|
private $banRoute; |
16
|
|
|
|
17
|
|
|
public function handle($request, Closure $next) |
18
|
|
|
{ |
19
|
|
|
$this->currentTimestamp = Carbon::now()->timestamp; |
20
|
|
|
$this->banRoute = Route::current()->getAction('as') == 'ban'; |
21
|
|
|
|
22
|
|
|
$accountBan = $this->accountBan(); |
23
|
|
|
|
24
|
|
|
if ($accountBan || $this->ipBan() || $this->machineBan() || $this->superBan()) { |
25
|
|
|
if ($this->banRoute) { |
26
|
|
|
app()->instance('expulsion', $this->ban); |
27
|
|
|
app()->instance('user_session', core()->user()->username); |
28
|
|
|
|
29
|
|
|
Auth::logout(); |
30
|
|
|
|
31
|
|
|
return $next($request); |
32
|
|
|
} else { |
33
|
|
|
return redirect()->route('ban'); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (! $accountBan && $this->banRoute) { |
38
|
|
|
return redirect()->route('home'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $next($request); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/* |
45
|
|
|
* Get the longest ban for an account ban type |
46
|
|
|
*/ |
47
|
|
|
private function accountBan(): bool |
48
|
|
|
{ |
49
|
|
|
if (Auth::check()) { |
50
|
|
|
$ban = Bans::with('user') |
51
|
|
|
->where('user_id', core()->user()->id) |
52
|
|
|
->where('ban_expire', '>', $this->currentTimestamp) |
53
|
|
|
->where('type', 'account') |
54
|
|
|
->get() |
55
|
|
|
->sortByDesc('ban_expire') |
56
|
|
|
->first(); |
57
|
|
|
|
58
|
|
|
if (! is_null($ban)) { |
59
|
|
|
$this->ban = $ban; |
60
|
|
|
|
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/* |
69
|
|
|
* Get the longest ban for an IP ban type |
70
|
|
|
*/ |
71
|
|
|
private function ipBan(): bool |
72
|
|
|
{ |
73
|
|
|
if (Auth::check()) { |
74
|
|
|
$ban = Bans::with('user') |
75
|
|
|
->where('ip', request()->ip()) |
76
|
|
|
->where('ban_expire', '>', $this->currentTimestamp) |
77
|
|
|
->where('type', 'ip') |
78
|
|
|
->get() |
79
|
|
|
->sortByDesc('ban_expire') |
80
|
|
|
->first(); |
81
|
|
|
|
82
|
|
|
if (! is_null($ban)) { |
83
|
|
|
$this->ban = $ban; |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/* |
93
|
|
|
* Get the longest ban for a machine ban type |
94
|
|
|
*/ |
95
|
|
|
private function machineBan(): bool |
96
|
|
|
{ |
97
|
|
|
if (Auth::check()) { |
98
|
|
|
$ban = Bans::with('user') |
99
|
|
|
->where('machine_id', core()->user()->machine_id) |
100
|
|
|
->where('ban_expire', '>', $this->currentTimestamp) |
101
|
|
|
->where('type', 'machine') |
102
|
|
|
->get() |
103
|
|
|
->sortByDesc('ban_expire') |
104
|
|
|
->first(); |
105
|
|
|
|
106
|
|
|
if (! is_null($ban)) { |
107
|
|
|
$this->ban = $ban; |
108
|
|
|
|
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/* |
117
|
|
|
* Get the longest ban for a superban type |
118
|
|
|
*/ |
119
|
|
|
private function superBan(): bool |
120
|
|
|
{ |
121
|
|
|
if (Auth::check()) { |
122
|
|
|
$ban = Bans::with('user') |
123
|
|
|
->where('user_id', core()->user()->id) |
124
|
|
|
->where('ban_expire', '>', $this->currentTimestamp) |
125
|
|
|
->where('type', 'super') |
126
|
|
|
->get() |
127
|
|
|
->sortByDesc('ban_expire') |
128
|
|
|
->first(); |
129
|
|
|
|
130
|
|
|
if (! is_null($ban)) { |
131
|
|
|
$this->ban = $ban; |
132
|
|
|
|
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|