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 = []) {
$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);
static public function stream_json($filter_ips = false, $filter_staff = false, $board_access = false, $hide_message = false) {
if ($board_access && $board_access[0] == '*') {
$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']]);
$hide_message = false;
foreach ($hide_regexes as $regex) {
if(preg_match($regex, $ban['reason'])) {
$hide_message = true;
break;
}
}
if ($ban['post'] && !$hide_message) {
$post = json_decode($ban['post']);
$post = \json_decode($ban['post']);
$ban['message'] = isset($post->body) ? $post->body : 0;
}
unset($ban['ipstart'], $ban['ipend'], $ban['post'], $ban['creator']);
@ -323,30 +317,36 @@ class Bans {
if (filter_var($ban['mask'], FILTER_VALIDATE_IP) !== false) {
$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'] = '?';
}
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']);
$ban['mask'] = preg_split("/[\.:]/", $ban['mask']);
$ban['mask'] = array_slice($ban['mask'], 0, 2);
$ban['mask'] = implode(".", $ban['mask']);
$ban['mask'] = \preg_split("/[\.:]/", $ban['mask']);
$ban['mask'] = \array_slice($ban['mask'], 0, 2);
$ban['mask'] = \implode(".", $ban['mask']);
$ban['mask'] .= ".x.x";
if (isset ($subnet)) {
if (isset($subnet)) {
$ban['mask'] .= "/$subnet";
}
$ban['masked'] = true;
}
$json = json_encode($ban);
$out ? fputs($out, $json) : print($json);
$json = \json_encode($ban);
if ($ban['id'] != $end['id']) {
$out ? fputs($out, ",") : print(",");
// Add a comma if there's a previous row.
if ($has_previous) {
print(',');
}
$has_previous = true;
print($json);
} else {
break;
}
}
$out ? fputs($out, "]") : print("]");
print(']');
}
static public function seen($ban_id) {

View file

@ -1192,7 +1192,7 @@ function mod_bans_json(Context $ctx) {
// Compress the json for faster loads
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) {

View file

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