forked from leftypol/leftypol
bans.php: split find implementations
This commit is contained in:
parent
b88abb1c33
commit
870d3812d3
3 changed files with 72 additions and 33 deletions
99
inc/bans.php
99
inc/bans.php
|
@ -85,6 +85,71 @@ class Bans {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static private function findAutoGc($ip, $board, $get_mod_info, $require_ban_view) {
|
||||||
|
$query = prepare('SELECT ``bans``.*' . ($get_mod_info ? ', `username`' : '') . ' FROM ``bans``
|
||||||
|
' . ($get_mod_info ? 'LEFT JOIN ``mods`` ON ``mods``.`id` = `creator`' : '') . '
|
||||||
|
WHERE
|
||||||
|
(' . ($board !== false ? '(`board` IS NULL OR `board` = :board) AND' : '') . '
|
||||||
|
(`ipstart` = :ip OR (:ip >= `ipstart` AND :ip <= `ipend`)))
|
||||||
|
ORDER BY `expires` IS NULL, `expires` DESC');
|
||||||
|
|
||||||
|
if ($board !== false) {
|
||||||
|
$query->bindValue(':board', $board, PDO::PARAM_STR);
|
||||||
|
}
|
||||||
|
|
||||||
|
$query->bindValue(':ip', inet_pton($ip));
|
||||||
|
$query->execute() or error(db_error($query));
|
||||||
|
|
||||||
|
$ban_list = [];
|
||||||
|
$to_delete_list = [];
|
||||||
|
|
||||||
|
while ($ban = $query->fetch(PDO::FETCH_ASSOC)) {
|
||||||
|
if ($ban['expires'] && ($ban['seen'] || !$require_ban_view) && $ban['expires'] < time()) {
|
||||||
|
$to_delete_list[] = $ban['id'];
|
||||||
|
} else {
|
||||||
|
if ($ban['post']) {
|
||||||
|
$ban['post'] = json_decode($ban['post'], true);
|
||||||
|
}
|
||||||
|
$ban['mask'] = self::range_to_string(array($ban['ipstart'], $ban['ipend']));
|
||||||
|
$ban_list[] = $ban;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self::deleteBans($to_delete_list);
|
||||||
|
|
||||||
|
return $ban_list;
|
||||||
|
}
|
||||||
|
|
||||||
|
static private function findNoGc($ip, $board, $get_mod_info) {
|
||||||
|
$query = prepare('SELECT ``bans``.*' . ($get_mod_info ? ', `username`' : '') . ' FROM ``bans``
|
||||||
|
' . ($get_mod_info ? 'LEFT JOIN ``mods`` ON ``mods``.`id` = `creator`' : '') . '
|
||||||
|
WHERE
|
||||||
|
(' . ($board !== false ? '(`board` IS NULL OR `board` = :board) AND' : '') . '
|
||||||
|
(`ipstart` = :ip OR (:ip >= `ipstart` AND :ip <= `ipend`)))
|
||||||
|
AND `expires` IS NULL OR `expires` >= :curr_time
|
||||||
|
ORDER BY `expires` IS NULL, `expires` DESC');
|
||||||
|
|
||||||
|
if ($board !== false) {
|
||||||
|
$query->bindValue(':board', $board, PDO::PARAM_STR);
|
||||||
|
}
|
||||||
|
|
||||||
|
$query->bindValue(':ip', inet_pton($ip));
|
||||||
|
$query->bindValue(':curr_time', time());
|
||||||
|
$query->execute() or error(db_error($query));
|
||||||
|
|
||||||
|
$ban_list = [];
|
||||||
|
|
||||||
|
while ($ban = $query->fetch(PDO::FETCH_ASSOC)) {
|
||||||
|
if ($ban['post']) {
|
||||||
|
$ban['post'] = json_decode($ban['post'], true);
|
||||||
|
}
|
||||||
|
$ban['mask'] = self::range_to_string(array($ban['ipstart'], $ban['ipend']));
|
||||||
|
$ban_list[] = $ban;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $ban_list;
|
||||||
|
}
|
||||||
|
|
||||||
static public function range_to_string($mask) {
|
static public function range_to_string($mask) {
|
||||||
list($ipstart, $ipend) = $mask;
|
list($ipstart, $ipend) = $mask;
|
||||||
|
|
||||||
|
@ -205,40 +270,14 @@ class Bans {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static public function find($ip, $board = false, $get_mod_info = false, $banid = null) {
|
static public function find($ip, $board, $get_mod_info, $auto_gc) {
|
||||||
global $config;
|
global $config;
|
||||||
|
|
||||||
$query = prepare('SELECT ``bans``.*' . ($get_mod_info ? ', `username`' : '') . ' FROM ``bans``
|
if ($auto_gc) {
|
||||||
' . ($get_mod_info ? 'LEFT JOIN ``mods`` ON ``mods``.`id` = `creator`' : '') . '
|
return self::findAutoGc($ip, $board, $get_mod_info, $config['require_ban_view']);
|
||||||
WHERE
|
|
||||||
(' . ($board !== false ? '(`board` IS NULL OR `board` = :board) AND' : '') . '
|
|
||||||
(`ipstart` = :ip OR (:ip >= `ipstart` AND :ip <= `ipend`)))
|
|
||||||
ORDER BY `expires` IS NULL, `expires` DESC');
|
|
||||||
|
|
||||||
if ($board !== false)
|
|
||||||
$query->bindValue(':board', $board, PDO::PARAM_STR);
|
|
||||||
|
|
||||||
$query->bindValue(':ip', inet_pton($ip));
|
|
||||||
$query->execute() or error(db_error($query));
|
|
||||||
|
|
||||||
$ban_list = array();
|
|
||||||
$to_delete_list = [];
|
|
||||||
|
|
||||||
while ($ban = $query->fetch(PDO::FETCH_ASSOC)) {
|
|
||||||
if ($ban['expires'] && ($ban['seen'] || !$config['require_ban_view']) && $ban['expires'] < time()) {
|
|
||||||
$to_delete_list[] = $ban['id'];
|
|
||||||
} else {
|
} else {
|
||||||
if ($ban['post']) {
|
return self::findNoGc($ip, $board, $get_mod_info);
|
||||||
$ban['post'] = json_decode($ban['post'], true);
|
|
||||||
}
|
}
|
||||||
$ban['mask'] = self::range_to_string(array($ban['ipstart'], $ban['ipend']));
|
|
||||||
$ban_list[] = $ban;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self::deleteBans($to_delete_list);
|
|
||||||
|
|
||||||
return $ban_list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static public function stream_json($out = false, $filter_ips = false, $filter_staff = false, $board_access = false, $hide_regexes = []) {
|
static public function stream_json($out = false, $filter_ips = false, $filter_staff = false, $board_access = false, $hide_regexes = []) {
|
||||||
|
|
|
@ -876,7 +876,7 @@ function checkBan($board = false) {
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($ips as $ip) {
|
foreach ($ips as $ip) {
|
||||||
$bans = Bans::find($_SERVER['REMOTE_ADDR'], $board, $config['show_modname']);
|
$bans = Bans::find($_SERVER['REMOTE_ADDR'], $board, $config['show_modname'], true);
|
||||||
|
|
||||||
foreach ($bans as &$ban) {
|
foreach ($bans as &$ban) {
|
||||||
if ($ban['expires'] && $ban['expires'] < time()) {
|
if ($ban['expires'] && $ban['expires'] < time()) {
|
||||||
|
|
|
@ -926,7 +926,7 @@ function mod_page_ip($ip) {
|
||||||
$args['token'] = make_secure_link_token('ban');
|
$args['token'] = make_secure_link_token('ban');
|
||||||
|
|
||||||
if (hasPermission($config['mod']['view_ban'])) {
|
if (hasPermission($config['mod']['view_ban'])) {
|
||||||
$args['bans'] = Bans::find($ip, false, true);
|
$args['bans'] = Bans::find($ip, false, true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasPermission($config['mod']['view_notes'])) {
|
if (hasPermission($config['mod']['view_notes'])) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue