Merge pull request 'Fix #139 Stream the ban list from the database' (#140) from bans-json into config

Reviewed-on: leftypol/leftypol#140
This commit is contained in:
Zankaria 2025-07-23 14:06:40 -05:00
commit cbbc267b65
3 changed files with 96 additions and 98 deletions

View file

@ -285,33 +285,27 @@ class Bans {
} }
} }
static public function stream_json($out = false, $filter_ips = false, $filter_staff = false, $board_access = false, $hide_regexes = []) { static public function stream_json($filter_ips = false, $filter_staff = false, $board_access = false, $hide_message = false) {
$query = query("SELECT ``bans``.*, `username` FROM ``bans``
LEFT JOIN ``mods`` ON ``mods``.`id` = `creator`
ORDER BY `created` DESC") or error(db_error());
$bans = $query->fetchAll(PDO::FETCH_ASSOC);
if ($board_access && $board_access[0] == '*') { if ($board_access && $board_access[0] == '*') {
$board_access = false; $board_access = false;
} }
$out ? fputs($out, "[") : print("["); $query = query("SELECT ``bans``.*, `username` FROM ``bans``
LEFT JOIN ``mods`` ON ``mods``.`id` = `creator`
ORDER BY `created` DESC") or error(db_error());
$end = end($bans); print('[');
foreach ($bans as &$ban) { $has_previous = false;
while (true) {
$ban = $query->fetch(PDO::FETCH_ASSOC);
if (\is_array($ban)) {
$ban['mask'] = self::range_to_string([$ban['ipstart'], $ban['ipend']]); $ban['mask'] = self::range_to_string([$ban['ipstart'], $ban['ipend']]);
$hide_message = false;
foreach ($hide_regexes as $regex) {
if(preg_match($regex, $ban['reason'])) {
$hide_message = true;
break;
}
}
if ($ban['post'] && !$hide_message) { if ($ban['post'] && !$hide_message) {
$post = json_decode($ban['post']); $post = \json_decode($ban['post']);
$ban['message'] = isset($post->body) ? $post->body : 0; $ban['message'] = isset($post->body) ? $post->body : 0;
} }
unset($ban['ipstart'], $ban['ipend'], $ban['post'], $ban['creator']); unset($ban['ipstart'], $ban['ipend'], $ban['post'], $ban['creator']);
@ -323,30 +317,36 @@ class Bans {
if (filter_var($ban['mask'], FILTER_VALIDATE_IP) !== false) { if (filter_var($ban['mask'], FILTER_VALIDATE_IP) !== false) {
$ban['single_addr'] = true; $ban['single_addr'] = true;
} }
if ($filter_staff || ($board_access !== false && !in_array($ban['board'], $board_access))) { if ($filter_staff || ($board_access !== false && !\in_array($ban['board'], $board_access))) {
$ban['username'] = '?'; $ban['username'] = '?';
} }
if ($filter_ips || ($board_access !== false && !in_array($ban['board'], $board_access))) { if ($filter_ips || ($board_access !== false && !\in_array($ban['board'], $board_access))) {
@list($ban['mask'], $subnet) = explode("/", $ban['mask']); @list($ban['mask'], $subnet) = explode("/", $ban['mask']);
$ban['mask'] = preg_split("/[\.:]/", $ban['mask']); $ban['mask'] = \preg_split("/[\.:]/", $ban['mask']);
$ban['mask'] = array_slice($ban['mask'], 0, 2); $ban['mask'] = \array_slice($ban['mask'], 0, 2);
$ban['mask'] = implode(".", $ban['mask']); $ban['mask'] = \implode(".", $ban['mask']);
$ban['mask'] .= ".x.x"; $ban['mask'] .= ".x.x";
if (isset ($subnet)) { if (isset($subnet)) {
$ban['mask'] .= "/$subnet"; $ban['mask'] .= "/$subnet";
} }
$ban['masked'] = true; $ban['masked'] = true;
} }
$json = json_encode($ban); $json = \json_encode($ban);
$out ? fputs($out, $json) : print($json);
if ($ban['id'] != $end['id']) { // Add a comma if there's a previous row.
$out ? fputs($out, ",") : print(","); if ($has_previous) {
print(',');
}
$has_previous = true;
print($json);
} else {
break;
} }
} }
$out ? fputs($out, "]") : print("]"); print(']');
} }
static public function seen($ban_id) { static public function seen($ban_id) {

View file

@ -1192,7 +1192,7 @@ function mod_bans_json(Context $ctx) {
// Compress the json for faster loads // Compress the json for faster loads
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler");
Bans::stream_json(false, false, !hasPermission($config['mod']['view_banstaff']), $mod['boards']); Bans::stream_json(false, !hasPermission($config['mod']['view_banstaff']), $mod['boards']);
} }
function mod_ban_appeals(Context $ctx) { function mod_ban_appeals(Context $ctx) {

View file

@ -1,7 +1,7 @@
<?php <?php
require 'info.php'; require 'info.php';
function pbanlist_build($action, $settings, $board) { function pbanlist_build($action, $settings, $board) {
// Possible values for $action: // Possible values for $action:
// - all (rebuild everything, initialization) // - all (rebuild everything, initialization)
// - news (news has been updated) // - news (news has been updated)
@ -9,25 +9,27 @@
// - bans (ban list changed) // - bans (ban list changed)
PBanlist::build($action, $settings); PBanlist::build($action, $settings);
} }
// Wrap functions in a class so they don't interfere with normal Tinyboard operations // Wrap functions in a class so they don't interfere with normal Tinyboard operations
class PBanlist { class PBanlist {
public static function build($action, $settings) { public static function build($action, $settings) {
global $config; global $config;
if ($action == 'all') if ($action == 'all') {
file_write($config['dir']['home'] . $settings['file_bans'], PBanlist::homepage($settings)); file_write($config['dir']['home'] . $settings['file_bans'], PBanlist::homepage($settings));
}
if ($action == 'all' || $action == 'bans') if ($action == 'all' || $action == 'bans') {
file_write($config['dir']['home'] . $settings['file_json'], PBanlist::gen_json($settings)); file_write($config['dir']['home'] . $settings['file_json'], PBanlist::gen_json($settings));
} }
}
public static function gen_json($settings) { public static function gen_json($settings) {
ob_start(); \ob_start();
Bans::stream_json(false, true, true, array(), array("/\bcp\b/i", "/porn/i")); Bans::stream_json(true, true, [], true);
$out = ob_get_contents(); $out = \ob_get_contents();
ob_end_clean(); \ob_end_clean();
return $out; return $out;
} }
@ -35,23 +37,19 @@
public static function homepage($settings) { public static function homepage($settings) {
global $config; global $config;
return Element('page.html', array( return Element('page.html', [
'config' => $config, 'config' => $config,
'mod' => false, 'mod' => false,
#'hide_dashboard_link' => true,
'title' => _("Ban list"), 'title' => _("Ban list"),
'subtitle' => "", 'subtitle' => "",
'boardlist' => createBoardlist(), 'boardlist' => createBoardlist(),
#'nojavascript' => true, 'body' => Element('mod/ban_list.html', [
'body' => Element('mod/ban_list.html', array(
'mod' => false, 'mod' => false,
'boards' => "[]", 'boards' => "[]",
'token' => false, 'token' => false,
'token_json' => false, 'token_json' => false,
'uri_json' => $config['dir']['home'] . $settings['file_json'], 'uri_json' => $config['dir']['home'] . $settings['file_json'],
)) ])
)); ]);
} }
}; };
?>