-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBans.php
More file actions
381 lines (298 loc) · 13.9 KB
/
Bans.php
File metadata and controls
381 lines (298 loc) · 13.9 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/**
* Copyright (C) 2015-2019 FeatherBB
* based on code by (C) 2008-2015 FluxBB
* and Rickard Andersson (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
namespace FeatherBB\Model\Admin;
use FeatherBB\Core\Database as DB;
use FeatherBB\Core\Email;
use FeatherBB\Core\Error;
use FeatherBB\Core\Interfaces\Cache as CacheInterface;
use FeatherBB\Core\Interfaces\Container;
use FeatherBB\Core\Interfaces\ForumEnv;
use FeatherBB\Core\Interfaces\Hooks;
use FeatherBB\Core\Interfaces\Input;
use FeatherBB\Core\Interfaces\Perms;
use FeatherBB\Core\Interfaces\Router;
use FeatherBB\Core\Interfaces\User;
use FeatherBB\Core\Utils;
use FeatherBB\Model\Cache;
class Bans
{
public function addBanInfo($id = null)
{
$ban = [];
$id = Hooks::fire('model.admin.bans.add_ban_info_start', $id);
// If the ID of the user to ban was provided through GET (a link from profile.php)
if (is_numeric($id)) {
$ban['user_id'] = $id;
if ($ban['user_id'] < 2) {
throw new Error(__('Bad request'), 404);
}
$selectAddBanInfo = ['group_id', 'username', 'email'];
$result = DB::table('users')->selectMany($selectAddBanInfo)
->where('id', $ban['user_id']);
$result = Hooks::fireDB('model.admin.bans.add_ban_info_query', $result);
$result = $result->findOne();
if ($result) {
$groupId = $result['group_id'];
$ban['ban_user'] = $result['username'];
$ban['email'] = $result['email'];
} else {
throw new Error(__('No user ID message'), 404);
}
} else {
// Otherwise the username is in POST
$ban['ban_user'] = Utils::trim(Input::post('new_ban_user'));
if ($ban['ban_user'] != '') {
$selectAddBanInfo = ['id', 'group_id', 'username', 'email'];
$result = DB::table('users')->selectMany($selectAddBanInfo)
->where('username', $ban['ban_user'])
->whereGt('id', 1);
$result = Hooks::fireDB('model.admin.bans.add_ban_info_query', $result);
$result = $result->findOne();
if ($result) {
$ban['user_id'] = $result['id'];
$groupId = $result['group_id'];
$ban['ban_user'] = $result['username'];
$ban['email'] = $result['email'];
} else {
throw new Error(__('No user message'), 404);
}
}
}
// Make sure we're not banning an admin or moderator
if (isset($groupId)) {
if ($groupId == ForumEnv::get('FEATHER_ADMIN')) {
throw new Error(sprintf(__('User is admin message'), Utils::escape($ban['ban_user'])), 403);
}
$isModeratorGroup = Perms::getGroupPermissions($groupId, 'mod.is_mod');
if ($isModeratorGroup) {
throw new Error(sprintf(__('User is mod message'), Utils::escape($ban['ban_user'])), 403);
}
}
// If we have a $ban['user_id'], we can try to find the last known IP of that user
if (isset($ban['user_id'])) {
$ban['ip'] = DB::table('posts')->where('poster_id', $ban['user_id'])
->orderByDesc('posted')
->findOneCol('poster_ip');
if (!$ban['ip']) {
$ban['ip'] = DB::table('users')->where('id', $ban['user_id'])
->findOneCol('registration_ip');
}
}
$ban['mode'] = 'add';
$ban = Hooks::fire('model.admin.bans.add_ban_info', $ban);
return $ban;
}
public function editBanInfo($id)
{
$ban = [];
$id = Hooks::fire('model.admin.bans.edit_ban_info_start', $id);
$ban['id'] = $id;
$selectEditBanInfo = ['username', 'ip', 'email', 'message', 'expire'];
$result = DB::table('bans')->selectMany($selectEditBanInfo)
->where('id', $ban['id']);
$result = Hooks::fireDB('model.admin.bans.edit_ban_info_query', $result);
$result = $result->findOne();
if ($result) {
$ban['ban_user'] = $result['username'];
$ban['ip'] = $result['ip'];
$ban['email'] = $result['email'];
$ban['message'] = $result['message'];
$ban['expire'] = $result['expire'];
} else {
throw new Error(__('Bad request'), 404);
}
$diff = (User::getPref('timezone') + User::getPref('dst')) * 3600;
$ban['expire'] = ($ban['expire'] != '') ? gmdate('Y-m-d', $ban['expire'] + $diff) : '';
$ban['mode'] = 'edit';
$ban = Hooks::fire('model.admin.bans.edit_ban_info', $ban);
return $ban;
}
public function insertBan()
{
$banUser = Utils::trim(Input::post('ban_user'));
$banIp = Utils::trim(Input::post('ban_ip'));
$banEmail = strtolower(Utils::trim(Input::post('ban_email')));
$banMessage = Utils::trim(Input::post('ban_message'));
$banExpire = Utils::trim(Input::post('ban_expire'));
Hooks::fire('model.admin.bans.insert_ban_start', $banUser, $banIp, $banEmail, $banMessage, $banExpire);
if ($banUser == '' && $banIp == '' && $banEmail == '') {
throw new Error(__('Must enter message'), 400);
} elseif (strtolower($banUser) == 'guest') {
throw new Error(__('Cannot ban guest message'), 400);
}
// Make sure we're not banning an admin or moderator
if (!empty($banUser)) {
$groupId = DB::table('users')->where('username', $banUser)
->whereGt('id', 1)
->findOneCol('group_id');
if ($groupId) {
if ($groupId == ForumEnv::get('FEATHER_ADMIN')) {
throw new Error(sprintf(__('User is admin message'), Utils::escape($banUser)), 403);
}
$isModeratorGroup = Perms::getGroupPermissions($groupId, 'mod.is_mod');
if ($isModeratorGroup) {
throw new Error(sprintf(__('User is mod message'), Utils::escape($banUser)), 403);
}
}
}
// Validate IP/IP range (it's overkill, I know)
if ($banIp != '') {
$banIp = preg_replace('%\s{2,}%S', ' ', $banIp);
$addresses = explode(' ', $banIp);
$addresses = array_map('trim', $addresses);
for ($i = 0; $i < count($addresses); ++$i) {
if (strpos($addresses[$i], ':') !== false) {
$octets = explode(':', $addresses[$i]);
for ($c = 0; $c < count($octets); ++$c) {
$octets[$c] = ltrim($octets[$c], "0");
if ($c > 7 || (!empty($octets[$c]) && !ctype_xdigit($octets[$c])) || intval($octets[$c], 16) > 65535) {
throw new Error(__('Invalid IP message'), 400);
}
}
$curAddress = implode(':', $octets);
$addresses[$i] = $curAddress;
} else {
$octets = explode('.', $addresses[$i]);
for ($c = 0; $c < count($octets); ++$c) {
$octets[$c] = (strlen($octets[$c]) > 1) ? ltrim($octets[$c], "0") : $octets[$c];
if ($c > 3 || preg_match('%[^0-9]%', $octets[$c]) || intval($octets[$c]) > 255) {
throw new Error(__('Invalid IP message'), 400);
}
}
$curAddress = implode('.', $octets);
$addresses[$i] = $curAddress;
}
}
$banIp = implode(' ', $addresses);
}
if ($banEmail != '' && !Email::isValidEmail($banEmail)) {
if (!preg_match('%^[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,63})$%', $banEmail)) {
throw new Error(__('Invalid e-mail message'), 400);
}
}
if ($banExpire != '' && $banExpire != 'Never') {
$banExpire = strtotime($banExpire.' GMT');
if ($banExpire == -1 || !$banExpire) {
throw new Error(__('Invalid date message').' '.__('Invalid date reasons'), 400);
}
$diff = (User::getPref('timezone') + User::getPref('dst')) * 3600;
$banExpire -= $diff;
if ($banExpire <= time()) {
throw new Error(__('Invalid date message').' '.__('Invalid date reasons'), 400);
}
} else {
$banExpire = 'NULL';
}
$banUser = ($banUser != '') ? $banUser : 'NULL';
$banIp = ($banIp != '') ? $banIp : 'NULL';
$banEmail = ($banEmail != '') ? $banEmail : 'NULL';
$banMessage = ($banMessage != '') ? $banMessage : 'NULL';
$insertUpdateBan = [
'username' => $banUser,
'ip' => $banIp,
'email' => $banEmail,
'message' => $banMessage,
'expire' => $banExpire,
];
$insertUpdateBan = Hooks::fire('model.admin.bans.insert_ban_data', $insertUpdateBan);
if (Input::post('mode') == 'add') {
$insertUpdateBan['ban_creator'] = User::get()->id;
$result = DB::table('bans')
->create()
->set($insertUpdateBan)
->save();
} else {
$result = DB::table('bans')
->where('id', Input::post('ban_id'))
->findOne()
->set($insertUpdateBan)
->save();
}
// Regenerate the bans cache
CacheInterface::store('bans', Cache::getBans());
return Router::redirect(Router::pathFor('adminBans'), __('Ban edited redirect'));
}
public function removeBan($banId)
{
$banId = Hooks::fire('model.admin.bans.remove_ban', $banId);
$result = DB::table('bans')->where('id', $banId)
->findOne();
$result = Hooks::fireDB('model.admin.bans.remove_ban_query', $result);
$result = $result->delete();
// Regenerate the bans cache
CacheInterface::store('bans', Cache::getBans());
return Router::redirect(Router::pathFor('adminBans'), __('Ban removed redirect'));
}
public function findBan($startFrom = false)
{
$banInfo = [];
Hooks::fire('model.admin.bans.find_ban_start');
// trim() all elements in $form
$banInfo['conditions'] = $banInfo['query_str'] = [];
$expireAfter = Input::query('expire_after') ? Utils::trim(Input::query('expire_after')) : '';
$expireBefore = Input::query('expire_before') ? Utils::trim(Input::query('expire_before')) : '';
$banInfo['order_by'] = Input::query('order_by') && in_array(Input::query('order_by'), ['username', 'ip', 'email', 'expire']) ? 'b.'.Input::query('order_by') : 'b.username';
$banInfo['direction'] = Input::query('direction') && Input::query('direction') == 'DESC' ? 'DESC' : 'ASC';
$banInfo['query_str'][] = 'order_by='.$banInfo['order_by'];
$banInfo['query_str'][] = 'direction='.$banInfo['direction'];
// Build the query
$result = DB::table('bans')->tableAlias('b')
->whereGt('b.id', 0);
// Try to convert date/time to timestamps
if ($expireAfter != '') {
$banInfo['query_str'][] = 'expire_after='.$expireAfter;
$expireAfter = strtotime($expireAfter);
if ($expireAfter === false || $expireAfter == -1) {
throw new Error(__('Invalid date message'), 400);
}
$result = $result->whereGt('b.expire', $expireAfter);
}
if ($expireBefore != '') {
$banInfo['query_str'][] = 'expire_before='.$expireBefore;
$expireBefore = strtotime($expireBefore);
if ($expireBefore === false || $expireBefore == -1) {
throw new Error(__('Invalid date message'), 400);
}
$result = $result->whereLt('b.expire', $expireBefore);
}
if (Input::query('username')) {
$result = $result->whereLike('b.username', str_replace('*', '%', Input::query('username')));
$banInfo['query_str'][] = 'username=' . urlencode(Input::query('username'));
}
if (Input::query('ip')) {
$result = $result->whereLike('b.ip', str_replace('*', '%', Input::query('ip')));
$banInfo['query_str'][] = 'ip=' . urlencode(Input::query('ip'));
}
if (Input::query('email')) {
$result = $result->whereLike('b.email', str_replace('*', '%', Input::query('email')));
$banInfo['query_str'][] = 'email=' . urlencode(Input::query('email'));
}
if (Input::query('message')) {
$result = $result->whereLike('b.message', str_replace('*', '%', Input::query('message')));
$banInfo['query_str'][] = 'message=' . urlencode(Input::query('message'));
}
// Fetch ban count
if (is_numeric($startFrom)) {
$banInfo['data'] = [];
$selectBans = ['b.id', 'b.username', 'b.ip', 'b.email', 'b.message', 'b.expire', 'b.ban_creator', 'ban_creator_username' => 'u.username'];
$result = $result->selectMany($selectBans)
->leftOuterJoin('users', ['b.ban_creator', '=', 'u.id'], 'u')
->orderBy($banInfo['order_by'], $banInfo['direction'])
->offset($startFrom)
->limit(50)
->findMany();
foreach ($result as $curBan) {
$banInfo['data'][] = $curBan;
}
} else {
$banInfo['num_bans'] = $result->count('id');
}
Hooks::fire('model.admin.bans.find_ban', $banInfo);
return $banInfo;
}
}