diff --git a/inc/bans.php b/inc/bans.php index efdfd059..48fd1eab 100644 --- a/inc/bans.php +++ b/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) { 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; - $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 = 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 { - if ($ban['post']) { - $ban['post'] = json_decode($ban['post'], true); - } - $ban['mask'] = self::range_to_string(array($ban['ipstart'], $ban['ipend'])); - $ban_list[] = $ban; - } + if ($auto_gc) { + return self::findAutoGc($ip, $board, $get_mod_info, $config['require_ban_view']); + } else { + return self::findNoGc($ip, $board, $get_mod_info); } - - 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 = []) { diff --git a/inc/functions.php b/inc/functions.php index 2ac3b3c8..b54ea7b0 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -876,7 +876,7 @@ function checkBan($board = false) { } 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) { if ($ban['expires'] && $ban['expires'] < time()) { diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 88e3161d..2c2bc82e 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -926,7 +926,7 @@ function mod_page_ip($ip) { $args['token'] = make_secure_link_token('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'])) {